From 09bee32c1fdb0311a798682118f081576c509ca4 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Wed, 27 Nov 2024 12:44:24 +0000 Subject: [PATCH] __edata and __end fixes --- aout.c | 6 ++++++ link.c | 30 +++++++++++++++++++++++------- pe.c | 4 ++++ pe.h | 1 + 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/aout.c b/aout.c index 5d42839..f0fb39e 100644 --- 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 2573f94..9dc34a1 100644 --- 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 dee77cc..cf1bffa 100644 --- 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 88723fc..39c1d5d 100644 --- 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); -- 2.34.1