From 9bcba6260ee9305f7e849894732ae6b6ca23f41b Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Mon, 9 Jun 2025 12:37:37 +0100 Subject: [PATCH] Bug fixes --- coff.c | 34 ++++++++++++++++++++++------------ link.c | 8 +++----- pe.c | 13 ++++++------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/coff.c b/coff.c index fb67c78..96b51ee 100644 --- a/coff.c +++ b/coff.c @@ -18,6 +18,13 @@ #include "section.h" #include "write7x.h" +#define IMAGE_SYM_ABSOLUTE -1 +#define IMAGE_SYM_DEBUG -2 + +#define IMAGE_SYM_UNDEFINED 0 +#define IMAGE_SYM_CLASS_EXTERNAL 2 +#define IMAGE_SYM_CLASS_STATIC 3 + struct string_table_header { unsigned char StringTableSize[4]; }; @@ -30,8 +37,8 @@ struct symbol_table_entry { unsigned char SectionNumber[2]; unsigned char Type[2]; - unsigned char StorageClass; - unsigned char NumberOfAuxSymbols; + unsigned char StorageClass[1]; + unsigned char NumberOfAuxSymbols[1]; }; @@ -79,6 +86,10 @@ static void translate_relocation (struct reloc_entry *reloc, struct coff_relocat exit (EXIT_FAILURE); } + + if (reloc->symbol->section_number == IMAGE_SYM_CLASS_STATIC) { + reloc->n_type = reloc->symbol->section_number; + } } @@ -191,12 +202,6 @@ static unsigned long translate_Characteristics_to_section_flags (unsigned long C } -#define IMAGE_SYM_ABSOLUTE -1 -#define IMAGE_SYM_DEBUG -2 - -#define IMAGE_SYM_UNDEFINED 0 -#define IMAGE_SYM_CLASS_EXTERNAL 2 - void read_coff_object (const char *filename, unsigned char *data, unsigned long data_size) { struct coff_header *coff_hdr; @@ -363,6 +368,7 @@ void read_coff_object (const char *filename, unsigned char *data, unsigned long part_p_array[i + 1] = part; } + no_syms = array_to_integer (coff_hdr->NumberOfSymbols, 4); for (i = 0; i < no_syms; i++) { @@ -461,15 +467,19 @@ void read_coff_object (const char *filename, unsigned char *data, unsigned long } - if (coff_symbol->StorageClass == IMAGE_SYM_CLASS_EXTERNAL || section_no == IMAGE_SYM_UNDEFINED) { + if (coff_symbol->StorageClass[0] == IMAGE_SYM_CLASS_STATIC) { + symbol->n_type = 2; + } + + if (coff_symbol->StorageClass[0] == IMAGE_SYM_CLASS_EXTERNAL || section_no == IMAGE_SYM_UNDEFINED) { symbol_record_external_symbol (symbol); } - if (coff_symbol->NumberOfAuxSymbols) { + if (coff_symbol->NumberOfAuxSymbols[0]) { unsigned long j; - for (j = 0; j < coff_symbol->NumberOfAuxSymbols; j++) { + for (j = 0; j < coff_symbol->NumberOfAuxSymbols[0]; j++) { symbol = of->symbol_arr + i + 1 + j; @@ -478,7 +488,7 @@ void read_coff_object (const char *filename, unsigned char *data, unsigned long } - i += coff_symbol->NumberOfAuxSymbols; + i += coff_symbol->NumberOfAuxSymbols[1]; } diff --git a/link.c b/link.c index 373408f..3d72b05 100644 --- a/link.c +++ b/link.c @@ -363,12 +363,10 @@ static void reloc_generic (struct section_part *part, struct reloc_entry *rel, s if (state->format == LD_FORMAT_BIN || state->format == LD_FORMAT_COM) { - if (!rel->n_ext/* || (symbol->n_type & N_TYPE) == N_BSS || (symbol->n_type & N_TYPE) == N_DATA || (symbol->n_type & N_TYPE) == N_TEXT*/) { + if (symbol->section_number != ABSOLUTE_SECTION_NUMBER && !rel->n_ext) { - /*report_at (__FILE__, __LINE__, REPORT_FATAL_ERROR, "symbol: %s, %lx", symbol->name, ((rel->symbolnum) >> 28));*/ - - if (rel->n_type != 2) { - report_at (program_name, 0, REPORT_ERROR, "%s:(%s+%#lu): segment relocation", part->of->filename, part->section->name, offset); + if (rel->n_type != 2 && symbol->n_type != 2) { + report_at (program_name, 0, REPORT_ERROR, "%s:(%s+%#lu): segment relocation"); } } diff --git a/pe.c b/pe.c index 1cbd650..777c4c1 100644 --- a/pe.c +++ b/pe.c @@ -974,13 +974,14 @@ static void write_implib (struct export_name *export_names, unsigned long num_na unsigned long offset = 0, offset2 = 0, offset3 = 0; int bHeaderName; - FILE *outfile; unsigned long pad_length, pad[2]; + FILE *outfile; - struct data_entry data_entry; - struct data_descriptor data_descriptor; - struct section_descriptor_long section_descriptor_long; struct section_descriptor_short section_descriptor_short; + struct section_descriptor_long section_descriptor_long; + + struct data_descriptor data_descriptor; + struct data_entry data_entry; info_list = create_symbol_list (export_names, num_names); @@ -1017,9 +1018,7 @@ static void write_implib (struct export_name *export_names, unsigned long num_na } - offset += write_data (outfile, "!\n", 8); - - offsets[0] = offset; + offsets[0] = (offset += write_data (outfile, "!\n", 8)); fseek (outfile, sizeof (struct ar_header), SEEK_CUR); offset2 = ftell (outfile); -- 2.34.1