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

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 7 04:41:32 PDT 2023


mstorsjo created this revision.
mstorsjo added reviewers: alvinhochun, mati865, jeremyd2019, rnk.
Herald added subscribers: ormris, steven_wu, hiraditya, inglorion.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

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, but I'm open for
discussion on the matter.

This goes on top of D152360 <https://reviews.llvm.org/D152360>, for people wanting to cherry pick it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152363

Files:
  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


Index: llvm/lib/ToolDrivers/llvm-dlltool/Options.td
===================================================================
--- llvm/lib/ToolDrivers/llvm-dlltool/Options.td
+++ llvm/lib/ToolDrivers/llvm-dlltool/Options.td
@@ -15,6 +15,9 @@
 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.
 //==============================================================================
Index: llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
===================================================================
--- llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -165,8 +165,9 @@
     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"
Index: llvm/lib/Object/COFFModuleDefinition.cpp
===================================================================
--- llvm/lib/Object/COFFModuleDefinition.cpp
+++ llvm/lib/Object/COFFModuleDefinition.cpp
@@ -138,8 +138,11 @@
 
 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 @@
       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 @@
       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 @@
   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
Index: llvm/include/llvm/Object/COFFModuleDefinition.h
===================================================================
--- llvm/include/llvm/Object/COFFModuleDefinition.h
+++ llvm/include/llvm/Object/COFFModuleDefinition.h
@@ -41,7 +41,7 @@
 
 Expected<COFFModuleDefinition>
 parseCOFFModuleDefinition(MemoryBufferRef MB, COFF::MachineTypes Machine,
-                          bool MingwDef = false);
+                          bool MingwDef = false, bool AddUnderscores = true);
 
 } // End namespace object.
 } // End namespace llvm.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152363.529254.patch
Type: text/x-patch
Size: 3866 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230607/a0d069ef/attachment.bin>


More information about the llvm-commits mailing list