__edata and __end fixes
authorRobert Pengelly <robertapengelly@hotmail.com>
Wed, 27 Nov 2024 12:44:24 +0000 (12:44 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Wed, 27 Nov 2024 12:44:24 +0000 (12:44 +0000)
aout.c
link.c
pe.c
pe.h

diff --git a/aout.c b/aout.c
index 5d42839f1d9653f03ace84eb0ad7b28af989edec..f0fb39ebfca9bc2a8d649f4f126e1f4e3a6fa98b 100644 (file)
--- a/aout.c
+++ b/aout.c
@@ -66,7 +66,13 @@ static unsigned char *write_relocs_for_section (unsigned char *pos, struct secti
             symbol = part->reloc_arr[i].symbol;
             
             if (symbol_is_undefined (symbol)) {
+            
                 symbol = symbol_find (symbol->name);
+                
+                if (strcmp (symbol->name, "__edata") == 0 || strcmp (symbol->name, "__end") == 0) {
+                    continue;
+                }
+            
             }
             
             integer_to_array (part->rva - part->section->rva + part->reloc_arr[i].offset, rel.r_address, 4);
diff --git a/link.c b/link.c
index 2573f946546de1e2c3accd11ec1e1b530ff61d2c..9dc34a1cbf75951bcaada0ba3dac277ccd57fed7 100644 (file)
--- a/link.c
+++ b/link.c
@@ -397,12 +397,15 @@ void link (void) {
     collapse_subsections ();
     calculate_section_sizes_and_rvas ();
     
-    if (!(symbol = symbol_find ("__edata")) || symbol_is_undefined (symbol)) {
+    if ((symbol = symbol_find ("__edata")) && symbol_is_undefined (symbol)) {
     
         if ((section = section_find (".text"))) {
         
-            value += (/*section->rva + */section->total_size);
-            /*value -= state->size_of_headers;*/
+            if (state->format == LD_FORMAT_I386_PE) {
+                value += pe_align_to_file_alignment (section->total_size);
+            } else {
+                value += section->total_size;
+            }
         
         }
         
@@ -415,18 +418,31 @@ void link (void) {
         symbol->value = value / 16;
         
         symbol_record_external_symbol (symbol);
-        value %= 16;
     
     }
     
-    if (!(symbol = symbol_find ("__end")) || symbol_is_undefined (symbol)) {
+    value %= 16;
+    
+    if ((symbol = symbol_find ("__end")) && symbol_is_undefined (symbol)) {
     
         if ((section = section_find (".data"))) {
-            value += section->total_size;
+        
+            if (state->format == LD_FORMAT_I386_PE) {
+                value += pe_align_to_file_alignment (section->total_size);
+            } else {
+                value += section->total_size;
+            }
+        
         }
         
         if ((section = section_find (".bss"))) {
-            value += section->total_size;
+        
+            if (state->format == LD_FORMAT_I386_PE) {
+                value += pe_align_to_file_alignment (section->total_size);
+            } else {
+                value += section->total_size;
+            }
+        
         }
         
         of = object_file_make (FAKE_LD_FILENAME, 1);
diff --git a/pe.c b/pe.c
index dee77ccda810d76afc63945f7ac547300e37fd5d..cf1bffa4ba99b8178f2f4f07d87898d6d5465fda 100644 (file)
--- a/pe.c
+++ b/pe.c
@@ -870,3 +870,7 @@ void pe_print_help (void) {
 unsigned long pe_get_first_section_rva (void) {
     return ALIGN (state->size_of_headers, section_alignment);
 }
+
+unsigned long pe_align_to_file_alignment (unsigned long value) {
+    return ALIGN (value, file_alignment);
+}
diff --git a/pe.h b/pe.h
index 88723fc06962267b0cc85bb08a2ad5f45973e567..39c1d5d55cb54df3ac3675a8695644696ff7166a 100644 (file)
--- a/pe.h
+++ b/pe.h
@@ -198,6 +198,7 @@ void pe_after_link (void);
 void pe_before_link (void);
 void pe_print_help (void);
 
+unsigned long pe_align_to_file_alignment (unsigned long value);
 unsigned long pe_get_first_section_rva (void);
 
 void pe_write (const char *filename);