Sort by real name when provided and remove implib on error master
authorRobert Pengelly <robertapengelly@hotmail.com>
Thu, 20 Aug 2026 11:58:33 +0000 (12:58 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Thu, 20 Aug 2026 11:58:33 +0000 (12:58 +0100)
ld.c
ld.h
pe.c

diff --git a/ld.c b/ld.c
index cb87f8bd80f4d24fbaae658d09603da14b06200b..439e1ceda7c6537cdd68d58e02c3182a1a875ff8 100644 (file)
--- a/ld.c
+++ b/ld.c
@@ -31,6 +31,10 @@ static void cleanup (void) {
         if (state->output_filename) {
             remove (state->output_filename);
         }
+        
+        if (state->output_implib_filename) {
+            remove (state->output_implib_filename);
+        }
     
     }
 
diff --git a/ld.h b/ld.h
index 77a2c269c712f650fa78e1e34542640b8f77e729..558acf2e1cc31bd49a7b18886ac47f8e88f429a3 100644 (file)
--- a/ld.h
+++ b/ld.h
@@ -18,7 +18,7 @@ struct ld_state {
     const char *output_map_filename;
     uint64_t base_address;
     
-    const char *output_filename;
+    const char *output_filename, *output_implib_filename;
     int create_shared_library, format;
     
     int emit_relocs, use_custom_base_address;
diff --git a/pe.c b/pe.c
index 404130565c29743c8640b4347d4aae0b8f4fc7fa..7b65d039f875d6a989c86294b6a9233affd73906 100644 (file)
--- a/pe.c
+++ b/pe.c
 #include    "section.h"
 #include    "write7x.h"
 
-static char *output_implib_filename = 0;
-static int kill_at = 0;
-
 static unsigned short subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
+static int kill_at = 0;
 
 static uint64_t section_alignment = DEFAULT_SECTION_ALIGNMENT;
 static uint64_t file_alignment = DEFAULT_FILE_ALIGNMENT;
@@ -206,11 +204,11 @@ void pe_use_option (const char *cmd_arg, int idx, const char *optarg) {
         
         case LD_OPTION_OUT_IMPLIB: {
         
-            if (output_implib_filename) {
-                free (output_implib_filename);
+            if (state->output_implib_filename) {
+                free ((char *) state->output_implib_filename);
             }
             
-            output_implib_filename = xstrdup (optarg);
+            state->output_implib_filename = xstrdup (optarg);
             break;
         
         }
@@ -1070,7 +1068,7 @@ static unsigned long write_data (FILE *outfile, void *data, unsigned long data_s
 
     if (fwrite (data, data_size, 1, outfile) != 1) {
     
-        report_at (program_name, 0, REPORT_ERROR, "failed whilst writing data to '%s'", output_implib_filename);
+        report_at (program_name, 0, REPORT_ERROR, "failed whilst writing data to '%s'", state->output_implib_filename);
         return 0;
     
     }
@@ -1234,9 +1232,9 @@ static void write_implib (struct export_name *export_names, unsigned long num_na
     
     }
     
-    if (!(outfile = fopen (output_implib_filename, "wb"))) {
+    if (!(outfile = fopen (state->output_implib_filename, "wb"))) {
     
-        report_at (program_name, 0, REPORT_ERROR, "failed to open '%s' for writing", output_implib_filename);
+        report_at (program_name, 0, REPORT_ERROR, "failed to open '%s' for writing", state->output_implib_filename);
         return;
     
     }
@@ -1773,7 +1771,26 @@ static void write_implib (struct export_name *export_names, unsigned long num_na
 }
 
 static int export_name_compar (const void *a, const void *b) {
-    return strcmp (((struct export_name *) a)->name, ((struct export_name *) b)->name);
+
+    struct export_name *first = (struct export_name *) a;
+    struct export_name *second = (struct export_name *) b;
+    
+    if (first->real_name) {
+    
+        if (second->real_name) {
+            return strcmp (first->real_name, second->real_name);
+        }
+        
+        return strcmp (first->real_name, second->name);
+    
+    }
+    
+    if (second->real_name) {
+        return strcmp (first->name, second->real_name);
+    }
+    
+    return strcmp (first->name, second->name);
+
 }
 
 static char *unat_name (const char *orig_name) {
@@ -1854,7 +1871,7 @@ static void generate_edata (void) {
     
     qsort (export_names, num_names, sizeof (*export_names), &export_name_compar);
     
-    if (output_implib_filename) {
+    if (state->output_implib_filename) {
         write_implib (export_names, num_names, ordinal_base);
     }
     
@@ -2087,7 +2104,7 @@ void pe_before_link (void) {
     
     if (export_name_list) {
         generate_edata ();
-    } else if (output_implib_filename) {
+    } else if (state->output_implib_filename) {
         write_implib (0, 0, 0);
     }