ld: Add support for a new option, -exclude-symbols, in COFF object file directives
authorMartin Storsjö <martin@martin.st>
Tue, 19 Jul 2022 19:48:06 +0000 (22:48 +0300)
committerMartin Storsjö <martin@martin.st>
Mon, 1 Aug 2022 20:17:56 +0000 (23:17 +0300)
This maps to the same as ld's --exclude-symbols command line option,
but allowing specifying the option via directives embedded in the
object files instead of passed manually on the command line.

ld/deffile.h
ld/deffilep.y
ld/pe-dll.c
ld/testsuite/ld-pe/exclude-symbols-embedded-i386.d [new file with mode: 0644]
ld/testsuite/ld-pe/exclude-symbols-embedded-i386.s [new file with mode: 0644]
ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.d [new file with mode: 0644]
ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.s [new file with mode: 0644]
ld/testsuite/ld-pe/pe.exp

index d68fa8c6ba14b732f209f92235c457c1dcf2e7e9..306ae3a6f755deab04160a077c147943c0777205 100644 (file)
@@ -61,6 +61,11 @@ typedef struct def_file_aligncomm {
   unsigned int alignment;      /* log-2 alignment.        */
 } def_file_aligncomm;
 
+typedef struct def_file_exclude_symbol {
+  struct def_file_exclude_symbol *next;        /* Chain pointer.  */
+  char *symbol_name;           /* Name of excluded symbol.  */
+} def_file_exclude_symbol;
+
 typedef struct def_file {
   /* From the NAME or LIBRARY command.  */
   char *name;
@@ -94,6 +99,7 @@ typedef struct def_file {
 
   /* Only expected from .drectve sections, not .DEF files.  */
   def_file_aligncomm *aligncomms;
+  def_file_exclude_symbol *exclude_symbols;
 
 } def_file;
 
index 8c7a0dc47949b0436eb7325c80e563d8938558ef..dc0cb4db944a3d1fb1ec6bfc37da5e6a4efbae6a 100644 (file)
@@ -101,6 +101,7 @@ static void def_stacksize (int, int);
 static void def_version (int, int);
 static void def_directive (char *);
 static void def_aligncomm (char *str, int align);
+static void def_exclude_symbols (char *str);
 static int def_parse (void);
 static void def_error (const char *);
 static int def_lex (void);
@@ -121,7 +122,7 @@ static const char *lex_parse_string_end = 0;
 
 %token NAME LIBRARY DESCRIPTION STACKSIZE_K HEAPSIZE CODE DATAU DATAL
 %token SECTIONS EXPORTS IMPORTS VERSIONK BASE CONSTANTU CONSTANTL
-%token PRIVATEU PRIVATEL ALIGNCOMM
+%token PRIVATEU PRIVATEL ALIGNCOMM EXCLUDE_SYMBOLS
 %token READ WRITE EXECUTE SHARED_K NONAMEU NONAMEL DIRECTIVE EQUAL
 %token <id> ID
 %token <digits> DIGITS
@@ -131,7 +132,7 @@ static const char *lex_parse_string_end = 0;
 %type  <number> opt_ordinal
 %type  <number> attr attr_list opt_number exp_opt_list exp_opt
 %type  <id> opt_name opt_name2 opt_equal_name anylang_id opt_id
-%type  <id> opt_equalequal_name
+%type  <id> opt_equalequal_name symbol_list
 %type  <id_const> keyword_as_name
 
 %%
@@ -155,6 +156,7 @@ command:
        |       VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);}
        |       DIRECTIVE ID { def_directive ($2);}
        |       ALIGNCOMM anylang_id ',' NUMBER { def_aligncomm ($2, $4);}
+       |       EXCLUDE_SYMBOLS symbol_list
        ;
 
 
@@ -333,6 +335,11 @@ anylang_id: ID             { $$ = $1; }
          }
        ;
 
+symbol_list:
+       anylang_id { def_exclude_symbols ($1); }
+       |       symbol_list ',' anylang_id { def_exclude_symbols ($3); }
+       ;
+
 opt_digits: DIGITS     { $$ = $1; }
        |               { $$ = ""; }
        ;
@@ -488,6 +495,15 @@ def_file_free (def_file *fdef)
       free (c);
     }
 
+  while (fdef->exclude_symbols)
+    {
+      def_file_exclude_symbol *e = fdef->exclude_symbols;
+
+      fdef->exclude_symbols = fdef->exclude_symbols->next;
+      free (e->symbol_name);
+      free (e);
+    }
+
   free (fdef);
 }
 
@@ -943,6 +959,7 @@ diropts[] =
   { "-attr", SECTIONS },
   { "-export", EXPORTS },
   { "-aligncomm", ALIGNCOMM },
+  { "-exclude-symbols", EXCLUDE_SYMBOLS },
   { 0, 0 }
 };
 
@@ -1258,6 +1275,35 @@ def_aligncomm (char *str, int align)
     }
 }
 
+static void
+def_exclude_symbols (char *str)
+{
+  def_file_exclude_symbol *c, *p;
+
+  p = NULL;
+  c = def->exclude_symbols;
+  while (c != NULL)
+    {
+      int e = strcmp (c->symbol_name, str);
+      if (!e)
+        return;
+      c = (p = c)->next;
+    }
+
+  c = xmalloc (sizeof (def_file_exclude_symbol));
+  c->symbol_name = xstrdup (str);
+  if (!p)
+    {
+      c->next = def->exclude_symbols;
+      def->exclude_symbols = c;
+    }
+  else
+    {
+      c->next = p->next;
+      p->next = c;
+    }
+}
+
 static void
 def_error (const char *err)
 {
index 24a51189662ed0ca3654dd2b01f82be97861c52a..fbf180ec0f2210b70807a70ddd575e58402f0b5f 100644 (file)
@@ -718,6 +718,16 @@ process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *
        }
     }
 
+  if (pe_def_file->exclude_symbols)
+    {
+      def_file_exclude_symbol *ac = pe_def_file->exclude_symbols;
+      while (ac)
+       {
+         pe_dll_add_excludes (ac->symbol_name, EXCLUDESYMS);
+         ac = ac->next;
+       }
+    }
+
   /* If we are building an executable and there is nothing
      to export, we do not build an export table at all.  */
   if (bfd_link_executable (info) && pe_def_file->num_exports == 0
diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.d b/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.d
new file mode 100644 (file)
index 0000000..663e6b4
--- /dev/null
@@ -0,0 +1,10 @@
+#source: exclude-symbols-embedded-i386.s
+#target: i?86-*-cygwin* i?86-*-pe i?86-*-mingw*
+#ld: -shared
+#objdump: -p
+
+#...
+.*\[[  ]*0\] sym1
+.*\[[  ]*1\] sym3
+.*\[[  ]*2\] sym5
+#pass
diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.s b/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.s
new file mode 100644 (file)
index 0000000..d9da979
--- /dev/null
@@ -0,0 +1,15 @@
+.global _sym1
+.global _sym2
+.global _sym3
+.global _sym4
+.global _sym5
+_sym1:
+_sym2:
+_sym3:
+_sym4:
+_sym5:
+  ret
+
+.section .drectve,"yn"
+.ascii " -exclude-symbols:sym2,unknownsym"
+.ascii " -exclude-symbols:unknownsym,sym4"
diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.d b/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.d
new file mode 100644 (file)
index 0000000..1025695
--- /dev/null
@@ -0,0 +1,10 @@
+#source: exclude-symbols-embedded-x86_64.s
+#target: x86_64-*-cygwin* x86_64-*-pe x86_64-*-mingw*
+#ld: -shared
+#objdump: -p
+
+#...
+.*\[[  ]*0\] sym1
+.*\[[  ]*1\] sym3
+.*\[[  ]*2\] sym5
+#pass
diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.s b/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.s
new file mode 100644 (file)
index 0000000..2fb0d2a
--- /dev/null
@@ -0,0 +1,15 @@
+.global sym1
+.global sym2
+.global sym3
+.global sym4
+.global sym5
+sym1:
+sym2:
+sym3:
+sym4:
+sym5:
+  ret
+
+.section .drectve,"yn"
+.ascii " -exclude-symbols:sym2,unknownsym"
+.ascii " -exclude-symbols:unknownsym,sym4"
index 413b316f152ead41457146810404e797d9bf712d..b640b0891f237b7b225271cb3453224759b20443 100644 (file)
@@ -124,3 +124,6 @@ set foreign_sym_test {
 # MCore rearranges symbol order.
 setup_xfail mcore-*-pe
 run_ld_link_tests $foreign_sym_test
+
+run_dump_test "exclude-symbols-embedded-i386"
+run_dump_test "exclude-symbols-embedded-x86_64"