From: Robert Pengelly Date: Fri, 10 Oct 2025 05:21:14 +0000 (+0100) Subject: Added and handle -N option X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=08ee1fccd975d1c47247397c90f26e32ad938118;p=slink.git Added and handle -N option --- diff --git a/Makefile.p32 b/Makefile.p32 deleted file mode 100644 index 91479e4..0000000 --- a/Makefile.p32 +++ /dev/null @@ -1,25 +0,0 @@ -#****************************************************************************** -# @file Makefile.p32 -#****************************************************************************** -AS=as386 -CC=gcc386 -LD=ld386 - -COPTS=-S -O2 -fno-common -ansi -I. -I./include -I../pdos/pdpclib -I../pdos/src -D__PDOS386__ -D__32BIT__ -D__NOBIVA__ -D__PDOS__ -Wall -Werror -ansi -m32 -pedantic -COBJ=aout.o coff.o elks.o hashtab.o ld.o lib.o link.o map.o mz.o omf.o pe.o report.o section.o symbol.o vector.o write7x.o - -all: clean slink.exe - -slink.exe: $(COBJ) - $(LD) -s -o slink.exe ../pdos/pdpclib/pdosst32.o $(COBJ) ../pdos/pdpclib/pdos.a - -.c.o: - $(CC) $(COPTS) -o $*.s $< - $(AS) -o $@ $*.s - rm -f $*.s - -clean: - for %f in ($(COBJ)) do ( rm -f %f ) - - rm -f slink - rm -f slink.exe diff --git a/Makefile.pdw b/Makefile.pdw deleted file mode 100644 index 0799d40..0000000 --- a/Makefile.pdw +++ /dev/null @@ -1,25 +0,0 @@ -#****************************************************************************** -# @file Makefile.pdw -#****************************************************************************** -AS=aswin -CC=gccwin -LD=ldwin - -COPTS=-S -O2 -fno-common -ansi -I. -I./include -I../pdos/pdpclib -I../pdos/src -D__WIN32__ -D__NOBIVA__ -D__PDOS__ -Wall -Werror -ansi -m32 -pedantic -COBJ=aout.o coff.o elks.o hashtab.o ld.o lib.o link.o map.o mz.o omf.o pe.o report.o section.o symbol.o vector.o write7x.o - -all: clean slink.exe - -slink.exe: $(COBJ) - $(LD) -s -o slink.exe ../pdos/pdpclib/w32start.o $(COBJ) ../pdos/pdpclib/msvcrt.a ../pdos/src/kernel32.a - -.c.o: - $(CC) $(COPTS) -o $*.s $< - $(AS) -o $@ $*.s - rm -f $*.s - -clean: - for %f in ($(COBJ)) do ( rm -f %f ) - - rm -f slink - rm -f slink.exe diff --git a/Makefile.std b/Makefile.std deleted file mode 100644 index 0d0f811..0000000 --- a/Makefile.std +++ /dev/null @@ -1,20 +0,0 @@ -AS=pdas --oformat coff -CC=gccwin -LD=pdld - -COPTS=-S -O2 -fno-common -ansi -I. -I../pdos/pdpclib -D__WIN32__ -D__NOBIVA__ -D__PDOS__ -COBJ=aout.obj coff.obj elks.obj hashtab.obj ld.obj lib.obj link.obj map.obj \ - mz.obj omf.obj pe.obj report.obj section.obj symbol.obj vector.obj write7x.obj - -all: clean slink.exe - -slink.exe: $(COBJ) - $(LD) -s -nostdlib --no-insert-timestamp -o slink.exe ../pdos/pdpclib/w32start.obj $(COBJ) ../pdos/pdpclib/msvcrt.lib - -.c.obj: - $(CC) $(COPTS) $< - $(AS) -o $@ $*.s - rm -f $*.s - -clean: - rm -f *.obj slink.exe diff --git a/aout.c b/aout.c index c1567c7..5f676a7 100644 --- a/aout.c +++ b/aout.c @@ -12,6 +12,8 @@ #include "section.h" #include "symbol.h" +static unsigned long section_alignment = DEFAULT_SECTION_ALIGNMENT; + static void translate_relocation (struct reloc_entry *reloc, struct aout_relocation_info *input_reloc, struct section_part *part, struct aout_exec *exec_p) { unsigned long r_symbolnum = array_to_integer (input_reloc->r_symbolnum, 4); @@ -463,9 +465,10 @@ void aout_write (const char *filename) { FILE *fp; unsigned char *data, *pos; - unsigned long data_size; + unsigned long image_size; struct section *text_section, *data_section, *bss_section; + unsigned long text_size = 0, data_size = 0, bss_size = 0; struct aout_exec exec; memset (&exec, 0, sizeof (exec)); @@ -483,29 +486,40 @@ void aout_write (const char *filename) { integer_to_array (OMAGIC, exec.a_info, 4); + if (text_section) { + + if (state->impure) { + text_size = text_section->total_size; + } else { + text_size = ALIGN (text_section->total_size, section_alignment); + } + + } + if (data_section) { - integer_to_array (data_section->rva, exec.a_text, 4); - - if (bss_section) { - integer_to_array (bss_section->rva - data_section->rva, exec.a_data, 4); + if (state->impure) { + data_size = data_section->total_size; } else { - integer_to_array (data_section->total_size, exec.a_data, 4); + data_size = ALIGN (data_section->total_size, section_alignment); } - } else { + } - integer_to_array (0, exec.a_data, 4); - - if (bss_section) { - integer_to_array (bss_section->rva, exec.a_text, 4); + if (bss_section) { + + if (state->impure) { + bss_size = bss_section->total_size; } else { - integer_to_array (text_section ? text_section->total_size : 0, exec.a_text, 4); + bss_size = ALIGN (bss_section->total_size, section_alignment); } } - integer_to_array (bss_section ? bss_section->total_size : 0, exec.a_bss, 4); + integer_to_array (text_size, exec.a_text, 4); + integer_to_array (data_size, exec.a_data, 4); + + integer_to_array (bss_size, exec.a_bss, 4); integer_to_array (state->entry_point, exec.a_entry, 4); if (text_section) { @@ -516,14 +530,14 @@ void aout_write (const char *filename) { integer_to_array (section_get_num_relocs (data_section) * sizeof (struct aout_relocation_info), exec.a_drsize, 4); } - data_size = sizeof (exec); + image_size = sizeof (exec); - data_size += (array_to_integer (exec.a_text, 4) + array_to_integer (exec.a_data, 4)); - data_size += (array_to_integer (exec.a_trsize, 4) + array_to_integer (exec.a_drsize, 4)); + image_size += (array_to_integer (exec.a_text, 4) + array_to_integer (exec.a_data, 4)); + image_size += (array_to_integer (exec.a_trsize, 4) + array_to_integer (exec.a_drsize, 4)); - data_size += 4; + image_size += 4; - data = xmalloc (data_size); + data = xmalloc (image_size); memcpy (data, &exec, sizeof (exec)); pos = data + sizeof (exec); @@ -550,7 +564,7 @@ void aout_write (const char *filename) { integer_to_array (4, pos, 4); - if (fwrite (data, data_size, 1, fp) != 1) { + if (fwrite (data, image_size, 1, fp) != 1) { report_at (program_name, 0, REPORT_ERROR, "failed to write data to '%s'", filename); } @@ -558,3 +572,22 @@ void aout_write (const char *filename) { fclose (fp); } + + +void aout_before_link (void) { + + if (!state->impure) { + + struct section *section; + + for (section = all_sections; section; section = section->next) { + + if (section->section_alignment < section_alignment) { + section->section_alignment = section_alignment; + } + + } + + } + +} diff --git a/aout.h b/aout.h index 574ed31..f86d6d5 100644 --- a/aout.h +++ b/aout.h @@ -47,9 +47,11 @@ struct aout_nlist { #define N_TYPE 0x1e -void aout_write (const char *filename); void read_aout_object (const char *filename, unsigned char *data, unsigned long data_size); +void aout_before_link (void); +void aout_write (const char *filename); + #define OMAGIC 0407 #define ZMAGIC 0413 diff --git a/elks.c b/elks.c index 1c7a2ae..a46c69a 100644 --- a/elks.c +++ b/elks.c @@ -13,6 +13,8 @@ #include "section.h" #include "symbol.h" +static unsigned long section_alignment = DEFAULT_SECTION_ALIGNMENT; + static void translate_relocation (struct reloc_entry *reloc, struct elks_relocation_info *input_reloc, struct section_part *part, struct elks_exec *exec_p) { unsigned long r_symbolnum = array_to_integer (input_reloc->r_symbolnum, 4); @@ -461,9 +463,10 @@ void elks_write (const char *filename) { FILE *fp; unsigned char *data, *pos; - unsigned long data_size; + unsigned long image_size; struct section *text_section, *data_section, *bss_section; + unsigned long text_size = 0, data_size = 0, bss_size = 0; struct elks_exec exec; memset (&exec, 0, sizeof (exec)); @@ -485,29 +488,40 @@ void elks_write (const char *filename) { exec.a_cpu = (state->format == LD_FORMAT_I386_ELKS) ? 0x10 : 0x04; exec.a_hdrlen = sizeof (exec); + if (text_section) { + + if (state->impure) { + text_size = text_section->total_size; + } else { + text_size = ALIGN (text_section->total_size, section_alignment); + } + + } + if (data_section) { - integer_to_array (data_section->rva, exec.a_text, 4); - - if (bss_section) { - integer_to_array (bss_section->rva - data_section->rva, exec.a_data, 4); + if (state->impure) { + data_size = data_section->total_size; } else { - integer_to_array (data_section->total_size, exec.a_data, 4); + data_size = ALIGN (data_section->total_size, section_alignment); } - } else { + } - integer_to_array (0, exec.a_data, 4); - - if (bss_section) { - integer_to_array (bss_section->rva, exec.a_text, 4); + if (bss_section) { + + if (state->impure) { + bss_size = bss_section->total_size; } else { - integer_to_array (text_section ? text_section->total_size : 0, exec.a_text, 4); + bss_size = ALIGN (bss_section->total_size, section_alignment); } } - integer_to_array (bss_section ? bss_section->total_size : 0, exec.a_bss, 4); + integer_to_array (text_size, exec.a_text, 4); + integer_to_array (data_size, exec.a_data, 4); + + integer_to_array (bss_size, exec.a_bss, 4); integer_to_array (state->entry_point, exec.a_entry, 4); integer_to_array (array_to_integer (exec.a_text, 4) + array_to_integer (exec.a_data, 4), exec.a_total, 4); @@ -520,13 +534,14 @@ void elks_write (const char *filename) { integer_to_array (section_get_num_relocs (data_section) * sizeof (struct elks_relocation_info), exec.a_drsize, 4); } - data_size = sizeof (exec); + image_size = sizeof (exec); - data_size += (array_to_integer (exec.a_text, 4) + array_to_integer (exec.a_data, 4)); - data_size += (array_to_integer (exec.a_trsize, 4) + array_to_integer (exec.a_drsize, 4)); + image_size += (array_to_integer (exec.a_text, 4) + array_to_integer (exec.a_data, 4)); + image_size += (array_to_integer (exec.a_trsize, 4) + array_to_integer (exec.a_drsize, 4)); - data_size += 4; + image_size += 4; + data = xmalloc (image_size); data = xmalloc (data_size); memcpy (data, &exec, sizeof (exec)); @@ -554,7 +569,7 @@ void elks_write (const char *filename) { integer_to_array (4, pos, 4); - if (fwrite (data, data_size, 1, fp) != 1) { + if (fwrite (data, image_size, 1, fp) != 1) { report_at (program_name, 0, REPORT_ERROR, "failed to write data to '%s'", filename); } @@ -562,3 +577,22 @@ void elks_write (const char *filename) { fclose (fp); } + + +void elks_before_link (void) { + + if (!state->impure) { + + struct section *section; + + for (section = all_sections; section; section = section->next) { + + if (section->section_alignment < section_alignment) { + section->section_alignment = section_alignment; + } + + } + + } + +} diff --git a/elks.h b/elks.h index 668dc23..babd6fa 100644 --- a/elks.h +++ b/elks.h @@ -61,7 +61,9 @@ struct elks_nlist { #define N_TYPE 0x1e #define ELKS_MAGIC 0403 -void elks_write (const char *filename); void read_elks_object (const char *filename, unsigned char *data, unsigned long data_size); +void elks_before_link (void); +void elks_write (const char *filename); + #endif /* _ELKS_H */ diff --git a/ld.c b/ld.c index 0d8adf9..1455344 100644 --- a/ld.c +++ b/ld.c @@ -361,8 +361,12 @@ int main (int argc, char **argv) { } - if (state->format == LD_FORMAT_I386_COFF) { + if (state->format == LD_FORMAT_I386_AOUT) { + aout_before_link (); + } else if (state->format == LD_FORMAT_I386_COFF) { coff_before_link (); + } else if (state->format == LD_FORMAT_I386_ELKS) { + elks_before_link (); } else if (state->format == LD_FORMAT_I386_PE) { pe_before_link (); } diff --git a/ld.h b/ld.h index 5f1296d..82292d0 100644 --- a/ld.h +++ b/ld.h @@ -23,8 +23,7 @@ struct ld_state { int emit_relocs, use_custom_base_address; unsigned long size_of_headers; - int generate_symbols_table; - int emulation; + int emulation, generate_symbols_table, impure; }; @@ -56,6 +55,9 @@ struct ld_state { extern struct ld_state *state; extern const char *program_name; +#define DEFAULT_SECTION_ALIGNMENT 0x1000 +#define DEFAULT_FILE_ALIGNMENT 0x0200 + void map_write (const char *filename); void link (void); diff --git a/lib.c b/lib.c index 4c49308..6eb89ff 100644 --- a/lib.c +++ b/lib.c @@ -41,16 +41,17 @@ static struct options_with_use emulation_table[] = { #define LD_OPTION_FORMAT 4 #define LD_OPTION_GENERATE_SYMBOLS_TABLE 5 #define LD_OPTION_HELP 6 -#define LD_OPTION_IMAGE_BASE 7 -#define LD_OPTION_MAP 8 -#define LD_OPTION_MAP_FILE 9 -#define LD_OPTION_OUTFILE 10 +#define LD_OPTION_IMPURE 7 +#define LD_OPTION_IMAGE_BASE 8 +#define LD_OPTION_MAP 9 +#define LD_OPTION_MAP_FILE 10 +#define LD_OPTION_OUTFILE 11 static struct ld_option opts[] = { { "-M", LD_OPTION_TYPE_SHORT, LD_OPTION_MAP, LD_OPTION_NO_ARG }, { "-Map", LD_OPTION_TYPE_SHORT, LD_OPTION_MAP_FILE, LD_OPTION_HAS_ARG }, - { "-N", LD_OPTION_TYPE_SHORT, LD_OPTION_IGNORED, LD_OPTION_NO_ARG }, + { "-N", LD_OPTION_TYPE_SHORT, LD_OPTION_IMPURE, LD_OPTION_NO_ARG }, { "-e", LD_OPTION_TYPE_SHORT, LD_OPTION_ENTRY, LD_OPTION_HAS_ARG }, { "-m", LD_OPTION_TYPE_SHORT, LD_OPTION_EMULATION, LD_OPTION_HAS_ARG }, @@ -64,7 +65,7 @@ static struct ld_option opts[] = { { "--help", LD_OPTION_TYPE_LONG, LD_OPTION_HELP, LD_OPTION_NO_ARG }, { "--image-base", LD_OPTION_TYPE_LONG, LD_OPTION_IMAGE_BASE, LD_OPTION_HAS_ARG }, { "--oformat", LD_OPTION_TYPE_LONG, LD_OPTION_FORMAT, LD_OPTION_HAS_ARG }, - { "--omagic", LD_OPTION_TYPE_LONG, LD_OPTION_IGNORED, LD_OPTION_NO_ARG }, + { "--omagic", LD_OPTION_TYPE_LONG, LD_OPTION_IMPURE, LD_OPTION_NO_ARG }, { "--output", LD_OPTION_TYPE_LONG, LD_OPTION_OUTFILE, LD_OPTION_HAS_ARG }, { "--print-map", LD_OPTION_TYPE_LONG, LD_OPTION_MAP, LD_OPTION_NO_ARG }, { "--strip-all", LD_OPTION_TYPE_LONG, LD_OPTION_IGNORED, LD_OPTION_NO_ARG }, @@ -96,7 +97,7 @@ static void print_usage (void) { fprintf (stderr, "\n"); fprintf (stderr, " -M, --print-map Print map file on standard output.\n"); - fprintf (stderr, " -N, --omagic Ignored.\n"); + fprintf (stderr, " -N, --omagic Do not page align data in a.out and elks.\n"); fprintf (stderr, "\n"); fprintf (stderr, " -Map FILE Write a linker map to FILE.\n"); @@ -341,6 +342,13 @@ static void use_option (const char *cmd_arg, int idx, const char *optarg) { } + case LD_OPTION_IMPURE: { + + state->impure = 1; + break; + + } + case LD_OPTION_MAP: { state->output_map_filename = ""; @@ -386,7 +394,7 @@ unsigned long array_to_integer (unsigned char *arr, int size) { int i; for (i = 0; i < size; i++) { - value |= arr[i] << (CHAR_BIT * i); + value |= (unsigned long) arr[i] << (CHAR_BIT * i); } return value; @@ -401,7 +409,7 @@ unsigned long byte_array_to_integer (unsigned char *arr, int size, int big_endia int i, j; for (i = size, j = 0; i > 0; i--, j++) { - value |= arr[j] << (CHAR_BIT * (i - 1)); + value |= (unsigned long) arr[j] << (CHAR_BIT * (i - 1)); } return value; diff --git a/mz.h b/mz.h index dbf2fa5..e5b9a3d 100644 --- a/mz.h +++ b/mz.h @@ -4,9 +4,6 @@ #ifndef _MZ_H #define _MZ_H -#define DEFAULT_SECTION_ALIGNMENT 0x1000 -#define DEFAULT_FILE_ALIGNMENT 0x0200 - struct mz_exec { unsigned char e_magic[2]; /* Magic number */ diff --git a/pe.h b/pe.h index 04f2631..e63d8fa 100644 --- a/pe.h +++ b/pe.h @@ -4,9 +4,6 @@ #ifndef _PE_H #define _PE_H -#define DEFAULT_SECTION_ALIGNMENT 0x1000 -#define DEFAULT_FILE_ALIGNMENT 0x0200 - struct msdos_header { unsigned char e_magic[2]; /* Magic number */