Added and handle -N option
authorRobert Pengelly <robertapengelly@hotmail.com>
Fri, 10 Oct 2025 05:21:14 +0000 (06:21 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Fri, 10 Oct 2025 05:59:30 +0000 (06:59 +0100)
12 files changed:
Makefile.p32 [deleted file]
Makefile.pdw [deleted file]
Makefile.std [deleted file]
aout.c
aout.h
elks.c
elks.h
ld.c
ld.h
lib.c
mz.h
pe.h

diff --git a/Makefile.p32 b/Makefile.p32
deleted file mode 100644 (file)
index 91479e4..0000000
+++ /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 (file)
index 0799d40..0000000
+++ /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 (file)
index 0d0f811..0000000
+++ /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 c1567c7749cb0567634027823cee287eb0e241fa..5f676a77649b5006ddbb1e369357f72727a9de0c 100644 (file)
--- 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 574ed319e740581ab57f4a751ecb05259be7d2a0..f86d6d5b95e948e153fa7a88acab9494ef4934fb 100644 (file)
--- 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 1c7a2ae54b74838bcdf591924aa995b8cce49bf0..a46c69a31baf399db6fca3f481bfea07dd178db8 100644 (file)
--- 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 668dc2302f81675e5991b40bb2a1c735774db88f..babd6fae9cf1fe9ec52353fb904aa08fc43634fb 100644 (file)
--- 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 0d8adf99ab4259bfd92ced4aa5d0e951d49d6980..14553446a9a5d04af27decdaa5bf12f76160f8dc 100644 (file)
--- 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 5f1296dd193101c47548eecac1983ae314b81757..82292d0f4de667def19dfd75f5b2917ba28e1022 100644 (file)
--- 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 4c49308a0f73f535dfb3bdc0554b14a9ab204a3a..6eb89ffbc034d3314184106b2cdeb1ec9b8b3c46 100644 (file)
--- 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 dbf2fa5cf36d5d814986f474619334b03fc6bbe3..e5b9a3d6ba155bdd4fd075b22af1d4d447845a36 100644 (file)
--- 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 04f2631d9241e89bfa711937018ba15715c336fb..e63d8fa5c2fcb642d6f260b6351aa646efdceea7 100644 (file)
--- 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                         */