Skip to content

[Object][COFF] Avoid underscore prefix for forwarding exports #135433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025

Conversation

cjacek
Copy link
Contributor

@cjacek cjacek commented Apr 11, 2025

Fixes #132411.

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-coff

Author: Jacek Caban (cjacek)

Changes

Fixes #132411.


Full diff: https://github.com/llvm/llvm-project/pull/135433.diff

3 Files Affected:

  • (added) lld/test/COFF/export-alias.test (+16)
  • (modified) llvm/lib/Object/COFFModuleDefinition.cpp (+4-1)
  • (added) llvm/test/tools/llvm-lib/underscore.test (+23)
diff --git a/lld/test/COFF/export-alias.test b/lld/test/COFF/export-alias.test
new file mode 100644
index 0000000000000..a13fdf1504fb2
--- /dev/null
+++ b/lld/test/COFF/export-alias.test
@@ -0,0 +1,16 @@
+; REQUIRES: x86
+
+; RUN: echo > %t.s
+; RUN: llvm-mc -filetype=obj -triple=i686-windows %t.s -o %t.obj
+; RUN: lld-link -machine:x86 -dll -noentry -safeseh:no -out:%t.dll -def:%s %t.obj
+; RUN: llvm-readobj --coff-exports %t.dll | FileCheck %s
+
+; CHECK:      Export {
+; CHECK-NEXT:   Ordinal: 1
+; CHECK-NEXT:   Name: func
+; CHECK-NEXT:   ForwardedTo: other.dll.func
+; CHECK-NEXT: }
+
+LIBRARY test.dll
+EXPORTS
+        func = other.dll.func
diff --git a/llvm/lib/Object/COFFModuleDefinition.cpp b/llvm/lib/Object/COFFModuleDefinition.cpp
index 82c18539658e8..67b0dc4fa2976 100644
--- a/llvm/lib/Object/COFFModuleDefinition.cpp
+++ b/llvm/lib/Object/COFFModuleDefinition.cpp
@@ -238,7 +238,10 @@ class Parser {
     }
 
     if (AddUnderscores) {
-      if (!isDecorated(E.Name, MingwDef))
+      // Don't add underscore if the name is already mangled or if it's a
+      // forward target.
+      if (!isDecorated(E.Name, MingwDef) &&
+          (E.ExtName.empty() || !StringRef(E.Name).contains(".")))
         E.Name = (std::string("_").append(E.Name));
       if (!E.ExtName.empty() && !isDecorated(E.ExtName, MingwDef))
         E.ExtName = (std::string("_").append(E.ExtName));
diff --git a/llvm/test/tools/llvm-lib/underscore.test b/llvm/test/tools/llvm-lib/underscore.test
new file mode 100644
index 0000000000000..61eee00bcf9bc
--- /dev/null
+++ b/llvm/test/tools/llvm-lib/underscore.test
@@ -0,0 +1,23 @@
+; RUN: llvm-lib -machine:x86 -out:%t.lib -def:%s
+; RUN: llvm-readobj %t.lib | FileCheck %s
+
+; CHECK:      File: test.dll
+; CHECK-NEXT: Format: COFF-import-file-i386
+; CHECK-NEXT: Type: code
+; CHECK-NEXT: Name type: noprefix
+; CHECK-NEXT: Export name: func
+; CHECK-NEXT: Symbol: __imp__func
+; CHECK-NEXT: Symbol: _func
+; CHECK-EMPTY:
+; CHECK-NEXT: File: test.dll
+; CHECK-NEXT: Format: COFF-import-file-i386
+; CHECK-NEXT: Type: code
+; CHECK-NEXT: Name type: noprefix
+; CHECK-NEXT: Export name: test.func
+; CHECK-NEXT: Symbol: __imp__test.func
+; CHECK-NEXT: Symbol: _test.func
+
+LIBRARY test.dll
+EXPORTS
+        func
+        test.func

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-llvm-binary-utilities

Author: Jacek Caban (cjacek)

Changes

Fixes #132411.


Full diff: https://github.com/llvm/llvm-project/pull/135433.diff

3 Files Affected:

  • (added) lld/test/COFF/export-alias.test (+16)
  • (modified) llvm/lib/Object/COFFModuleDefinition.cpp (+4-1)
  • (added) llvm/test/tools/llvm-lib/underscore.test (+23)
diff --git a/lld/test/COFF/export-alias.test b/lld/test/COFF/export-alias.test
new file mode 100644
index 0000000000000..a13fdf1504fb2
--- /dev/null
+++ b/lld/test/COFF/export-alias.test
@@ -0,0 +1,16 @@
+; REQUIRES: x86
+
+; RUN: echo > %t.s
+; RUN: llvm-mc -filetype=obj -triple=i686-windows %t.s -o %t.obj
+; RUN: lld-link -machine:x86 -dll -noentry -safeseh:no -out:%t.dll -def:%s %t.obj
+; RUN: llvm-readobj --coff-exports %t.dll | FileCheck %s
+
+; CHECK:      Export {
+; CHECK-NEXT:   Ordinal: 1
+; CHECK-NEXT:   Name: func
+; CHECK-NEXT:   ForwardedTo: other.dll.func
+; CHECK-NEXT: }
+
+LIBRARY test.dll
+EXPORTS
+        func = other.dll.func
diff --git a/llvm/lib/Object/COFFModuleDefinition.cpp b/llvm/lib/Object/COFFModuleDefinition.cpp
index 82c18539658e8..67b0dc4fa2976 100644
--- a/llvm/lib/Object/COFFModuleDefinition.cpp
+++ b/llvm/lib/Object/COFFModuleDefinition.cpp
@@ -238,7 +238,10 @@ class Parser {
     }
 
     if (AddUnderscores) {
-      if (!isDecorated(E.Name, MingwDef))
+      // Don't add underscore if the name is already mangled or if it's a
+      // forward target.
+      if (!isDecorated(E.Name, MingwDef) &&
+          (E.ExtName.empty() || !StringRef(E.Name).contains(".")))
         E.Name = (std::string("_").append(E.Name));
       if (!E.ExtName.empty() && !isDecorated(E.ExtName, MingwDef))
         E.ExtName = (std::string("_").append(E.ExtName));
diff --git a/llvm/test/tools/llvm-lib/underscore.test b/llvm/test/tools/llvm-lib/underscore.test
new file mode 100644
index 0000000000000..61eee00bcf9bc
--- /dev/null
+++ b/llvm/test/tools/llvm-lib/underscore.test
@@ -0,0 +1,23 @@
+; RUN: llvm-lib -machine:x86 -out:%t.lib -def:%s
+; RUN: llvm-readobj %t.lib | FileCheck %s
+
+; CHECK:      File: test.dll
+; CHECK-NEXT: Format: COFF-import-file-i386
+; CHECK-NEXT: Type: code
+; CHECK-NEXT: Name type: noprefix
+; CHECK-NEXT: Export name: func
+; CHECK-NEXT: Symbol: __imp__func
+; CHECK-NEXT: Symbol: _func
+; CHECK-EMPTY:
+; CHECK-NEXT: File: test.dll
+; CHECK-NEXT: Format: COFF-import-file-i386
+; CHECK-NEXT: Type: code
+; CHECK-NEXT: Name type: noprefix
+; CHECK-NEXT: Export name: test.func
+; CHECK-NEXT: Symbol: __imp__test.func
+; CHECK-NEXT: Symbol: _test.func
+
+LIBRARY test.dll
+EXPORTS
+        func
+        test.func

@cjacek
Copy link
Contributor Author

cjacek commented Apr 11, 2025

Exporting names with dots is a bit unusual, but MSVC’s lib.exe allows it. So, comparing to the original version I posted in the issue, I’ve added a check that ExtName is not empty, meaning it's the forward name, not the export name. I also added a test for that.

CC @lhmouse

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cjacek cjacek merged commit 2b270df into llvm:main Apr 12, 2025
15 checks passed
@cjacek cjacek deleted the forward-export branch April 12, 2025 11:25
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 12, 2025

LLVM Buildbot has detected a new failure on builder sanitizer-x86_64-linux-bootstrap-asan running on sanitizer-buildbot1 while building lld,llvm at step 2 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/52/builds/7525

Here is the relevant piece of the build log for the reference
Step 2 (annotate) failure: 'python ../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py' (failure)
...
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/lld-link
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld64.lld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/wasm-ld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld.lld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/lld-link
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld64.lld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/wasm-ld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/main.py:72: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 900 seconds was requested on the command line. Forcing timeout to be 900 seconds.
-- Testing: 90169 tests, 88 workers --
Testing:  0.. 10
FAIL: Clang :: Interpreter/inline-virtual.cpp (12972 of 90169)
******************** TEST 'Clang :: Interpreter/inline-virtual.cpp' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
cat /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp | /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/clang-repl -Xcc -fno-rtti -Xcc -fno-sized-deallocation      | /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/FileCheck /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp # RUN: at line 6
+ cat /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp
+ /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/FileCheck /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp
+ /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/clang-repl -Xcc -fno-rtti -Xcc -fno-sized-deallocation
JIT session error: In graph incr_module_18-jitted-objectbuffer, section .text._ZN1AC2Ei: relocation target "_ZTV1A" at address 0x7ba991a0c000 is out of range of Delta32 fixup at 0x7fa993026082 (_ZN1AC2Ei, 0x7fa993026070 + 0x12)
JIT session error: Failed to materialize symbols: { (main, { DW.ref.__gxx_personality_v0 }) }
error: Failed to materialize symbols: { (main, { a1, _ZN1AC2Ei, $.incr_module_18.__inits.0, DW.ref.__gxx_personality_v0, __orc_init_func.incr_module_18 }) }
JIT session error: Failed to materialize symbols: { (main, { a1 }) }
error: Failed to materialize symbols: { (main, { __orc_init_func.incr_module_19, $.incr_module_19.__inits.0 }) }
error: Failed to materialize symbols: { (main, { __orc_init_func.incr_module_18 }) }
JIT session error: Failed to materialize symbols: { (main, { _ZN1AC2Ei }) }
error: Failed to materialize symbols: { (main, { $.incr_module_23.__inits.0, __orc_init_func.incr_module_23, a2 }) }
error: Failed to materialize symbols: { (main, { __orc_init_func.incr_module_23 }) }
/home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp:22:11: error: CHECK: expected string not found in input
// CHECK: ~A(1)
          ^
<stdin>:1:1: note: scanning from here
clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl... clang-repl> clang-repl... clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
^

Input file: <stdin>
Check file: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
          1: clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl... clang-repl> clang-repl... clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl>  
check:22     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
>>>>>>

--

Step 11 (stage2/asan check) failure: stage2/asan check (failure)
...
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/lld-link
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld64.lld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/wasm-ld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld.lld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/lld-link
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/ld64.lld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/wasm-ld
llvm-lit: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/llvm/utils/lit/lit/main.py:72: note: The test suite configuration requested an individual test timeout of 0 seconds but a timeout of 900 seconds was requested on the command line. Forcing timeout to be 900 seconds.
-- Testing: 90169 tests, 88 workers --
Testing:  0.. 10
FAIL: Clang :: Interpreter/inline-virtual.cpp (12972 of 90169)
******************** TEST 'Clang :: Interpreter/inline-virtual.cpp' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
cat /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp | /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/clang-repl -Xcc -fno-rtti -Xcc -fno-sized-deallocation      | /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/FileCheck /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp # RUN: at line 6
+ cat /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp
+ /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/FileCheck /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp
+ /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm_build_asan/bin/clang-repl -Xcc -fno-rtti -Xcc -fno-sized-deallocation
JIT session error: In graph incr_module_18-jitted-objectbuffer, section .text._ZN1AC2Ei: relocation target "_ZTV1A" at address 0x7ba991a0c000 is out of range of Delta32 fixup at 0x7fa993026082 (_ZN1AC2Ei, 0x7fa993026070 + 0x12)
JIT session error: Failed to materialize symbols: { (main, { DW.ref.__gxx_personality_v0 }) }
error: Failed to materialize symbols: { (main, { a1, _ZN1AC2Ei, $.incr_module_18.__inits.0, DW.ref.__gxx_personality_v0, __orc_init_func.incr_module_18 }) }
JIT session error: Failed to materialize symbols: { (main, { a1 }) }
error: Failed to materialize symbols: { (main, { __orc_init_func.incr_module_19, $.incr_module_19.__inits.0 }) }
error: Failed to materialize symbols: { (main, { __orc_init_func.incr_module_18 }) }
JIT session error: Failed to materialize symbols: { (main, { _ZN1AC2Ei }) }
error: Failed to materialize symbols: { (main, { $.incr_module_23.__inits.0, __orc_init_func.incr_module_23, a2 }) }
error: Failed to materialize symbols: { (main, { __orc_init_func.incr_module_23 }) }
/home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp:22:11: error: CHECK: expected string not found in input
// CHECK: ~A(1)
          ^
<stdin>:1:1: note: scanning from here
clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl... clang-repl> clang-repl... clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> 
^

Input file: <stdin>
Check file: /home/b/sanitizer-x86_64-linux-bootstrap-asan/build/llvm-project/clang/test/Interpreter/inline-virtual.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
          1: clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl... clang-repl> clang-repl... clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl> clang-repl>  
check:22     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
>>>>>>

--


var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[lld-link] LLD-LINK produces broken forward imports in source DEF
4 participants