[llvm] fb19fa2 - [llvm-dlltool] Implement the --no-leading-underscore option

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 11:29:57 PDT 2023


Author: Martin Storsjö
Date: 2023-06-09T21:29:05+03:00
New Revision: fb19fa2f3dfdd60d42c12ef28467d6f8f5149d6a

URL: https://github.com/llvm/llvm-project/commit/fb19fa2f3dfdd60d42c12ef28467d6f8f5149d6a
DIFF: https://github.com/llvm/llvm-project/commit/fb19fa2f3dfdd60d42c12ef28467d6f8f5149d6a.diff

LOG: [llvm-dlltool] Implement the --no-leading-underscore option

This requires being able to opt out from adding the leading underscores
in COFFModuleDefinition. Normally it is added automatically for I386
type targets. We could either move the decision entirely to all
callers, letting the caller check the machine type and decide whether
underscores should be added, or keep the logic mostly as is, but allowing
opting out from the behaviour on I386.

I went with keeping the interface as is for now.

Differential Revision: https://reviews.llvm.org/D152363

Added: 
    llvm/test/tools/llvm-dlltool/no-leading-underscore.def

Modified: 
    llvm/include/llvm/Object/COFFModuleDefinition.h
    llvm/lib/Object/COFFModuleDefinition.cpp
    llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
    llvm/lib/ToolDrivers/llvm-dlltool/Options.td

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Object/COFFModuleDefinition.h b/llvm/include/llvm/Object/COFFModuleDefinition.h
index c9d8f428993c5..a4ed9978dcc0a 100644
--- a/llvm/include/llvm/Object/COFFModuleDefinition.h
+++ b/llvm/include/llvm/Object/COFFModuleDefinition.h
@@ -41,7 +41,7 @@ struct COFFModuleDefinition {
 
 Expected<COFFModuleDefinition>
 parseCOFFModuleDefinition(MemoryBufferRef MB, COFF::MachineTypes Machine,
-                          bool MingwDef = false);
+                          bool MingwDef = false, bool AddUnderscores = true);
 
 } // End namespace object.
 } // End namespace llvm.

diff  --git a/llvm/lib/Object/COFFModuleDefinition.cpp b/llvm/lib/Object/COFFModuleDefinition.cpp
index 0666970d5c608..a33949733c8e4 100644
--- a/llvm/lib/Object/COFFModuleDefinition.cpp
+++ b/llvm/lib/Object/COFFModuleDefinition.cpp
@@ -138,8 +138,11 @@ class Lexer {
 
 class Parser {
 public:
-  explicit Parser(StringRef S, MachineTypes M, bool B)
-      : Lex(S), Machine(M), MingwDef(B) {}
+  explicit Parser(StringRef S, MachineTypes M, bool B, bool AU)
+      : Lex(S), Machine(M), MingwDef(B), AddUnderscores(AU) {
+    if (Machine != IMAGE_FILE_MACHINE_I386)
+      AddUnderscores = false;
+  }
 
   Expected<COFFModuleDefinition> parse() {
     do {
@@ -234,7 +237,7 @@ class Parser {
       unget();
     }
 
-    if (Machine == IMAGE_FILE_MACHINE_I386) {
+    if (AddUnderscores) {
       if (!isDecorated(E.Name, MingwDef))
         E.Name = (std::string("_").append(E.Name));
       if (!E.ExtName.empty() && !isDecorated(E.ExtName, MingwDef))
@@ -279,7 +282,7 @@ class Parser {
       if (Tok.K == EqualEqual) {
         read();
         E.AliasTarget = std::string(Tok.Value);
-        if (Machine == IMAGE_FILE_MACHINE_I386 && !isDecorated(E.AliasTarget, MingwDef))
+        if (AddUnderscores && !isDecorated(E.AliasTarget, MingwDef))
           E.AliasTarget = std::string("_").append(E.AliasTarget);
         continue;
       }
@@ -349,12 +352,14 @@ class Parser {
   MachineTypes Machine;
   COFFModuleDefinition Info;
   bool MingwDef;
+  bool AddUnderscores;
 };
 
 Expected<COFFModuleDefinition> parseCOFFModuleDefinition(MemoryBufferRef MB,
                                                          MachineTypes Machine,
-                                                         bool MingwDef) {
-  return Parser(MB.getBuffer(), Machine, MingwDef).parse();
+                                                         bool MingwDef,
+                                                         bool AddUnderscores) {
+  return Parser(MB.getBuffer(), Machine, MingwDef, AddUnderscores).parse();
 }
 
 } // namespace object

diff  --git a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
index a5dcf6c1c45e1..39bb8dd8ec858 100644
--- a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -165,8 +165,9 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
     return 1;
   }
 
-  Expected<COFFModuleDefinition> Def =
-      parseCOFFModuleDefinition(*MB, Machine, /*MingwDef=*/true);
+  bool AddUnderscores = !Args.hasArg(OPT_no_leading_underscore);
+  Expected<COFFModuleDefinition> Def = parseCOFFModuleDefinition(
+      *MB, Machine, /*MingwDef=*/true, AddUnderscores);
 
   if (!Def) {
     llvm::errs() << "error parsing definition\n"

diff  --git a/llvm/lib/ToolDrivers/llvm-dlltool/Options.td b/llvm/lib/ToolDrivers/llvm-dlltool/Options.td
index 6da5dc8f5814e..fee408fd0e9a8 100644
--- a/llvm/lib/ToolDrivers/llvm-dlltool/Options.td
+++ b/llvm/lib/ToolDrivers/llvm-dlltool/Options.td
@@ -15,6 +15,9 @@ def d_long : JoinedOrSeparate<["--"], "input-def">, Alias<d>;
 def k: Flag<["-"], "k">, HelpText<"Kill @n Symbol from export">;
 def k_alias: Flag<["--"], "kill-at">, Alias<k>;
 
+def no_leading_underscore: Flag<["--"], "no-leading-underscore">,
+    HelpText<"Don't add leading underscores on symbols">;
+
 //==============================================================================
 // The flags below do nothing. They are defined only for dlltool compatibility.
 //==============================================================================

diff  --git a/llvm/test/tools/llvm-dlltool/no-leading-underscore.def b/llvm/test/tools/llvm-dlltool/no-leading-underscore.def
new file mode 100644
index 0000000000000..6b78e15d2b5f6
--- /dev/null
+++ b/llvm/test/tools/llvm-dlltool/no-leading-underscore.def
@@ -0,0 +1,19 @@
+; RUN: llvm-dlltool -k -m i386 --input-def %s --output-lib %t.a --no-leading-underscore --kill-at
+; RUN: llvm-readobj %t.a | FileCheck %s
+; RUN: llvm-nm %t.a | FileCheck %s -check-prefix=CHECK-NM
+
+LIBRARY test.dll
+EXPORTS
+func
+alias == func
+DecoratedFunction at 4
+
+; CHECK:      Name type: name
+; CHECK-NEXT: Symbol: __imp_func
+; CHECK-NEXT: Symbol: func
+; CHECK:      Name type: undecorate
+; CHECK-NEXT: Symbol: __imp_DecoratedFunction at 4
+; CHECK-NEXT: Symbol: DecoratedFunction at 4
+
+; CHECK-NM: W alias
+; CHECK-NM: U func


        


More information about the llvm-commits mailing list