[PATCH] D134169: [LLD][COFF] Improve symbol table info for import thunk

Alvin Wong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 19 01:42:41 PDT 2022


alvinhochun created this revision.
alvinhochun added reviewers: rnk, mstorsjo.
Herald added a project: All.
alvinhochun requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Import thunks themselves contains a jump or branch, which is code by
nature. Therefore the import thunk symbol should be marked as function
type in the symbol table to help with debugging.

The `__imp_` import symbol associated to the import thunk is also useful
for debugging. However, when the import symbol isn't directly referenced
outside of the import thunk, it doesn't normally get added to the symbol
table. This change teaches LLD to add the import symbol explicitly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134169

Files:
  lld/COFF/Writer.cpp
  lld/test/COFF/symtab.test


Index: lld/test/COFF/symtab.test
===================================================================
--- lld/test/COFF/symtab.test
+++ lld/test/COFF/symtab.test
@@ -1,10 +1,10 @@
 # RUN: yaml2obj %s -o %t.obj
 # RUN: lld-link /debug:dwarf /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
-# RUN: llvm-readobj --symbols %t.exe | FileCheck %s
+# RUN: llvm-readobj --symbols %t.exe | FileCheck --check-prefixes=CHECK,DWARF %s
 # RUN: lld-link /debug:dwarf /opt:noref /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
-# RUN: llvm-readobj --symbols %t.exe | FileCheck %s
+# RUN: llvm-readobj --symbols %t.exe | FileCheck --check-prefixes=CHECK,DWARF %s
 # RUN: lld-link /debug:symtab /opt:noref /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
-# RUN: llvm-readobj --symbols %t.exe | FileCheck %s
+# RUN: llvm-readobj --symbols %t.exe | FileCheck --check-prefixes=CHECK,NODWF %s
 
 # RUN: lld-link /debug /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
 # RUN: llvm-readobj --symbols %t.exe | FileCheck -check-prefix=NO %s
@@ -15,6 +15,16 @@
 # CHECK-NEXT:     Value: 80
 # CHECK-NEXT:     Section: .text (1)
 # CHECK-NEXT:     BaseType: Null (0x0)
+# CHECK-NEXT:     ComplexType: Function (0x2)
+# CHECK-NEXT:     StorageClass: External (0x2)
+# CHECK-NEXT:     AuxSymbolCount: 0
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Name: __imp_MessageBoxA
+# DWARF-NEXT:     Value: 128
+# NODWF-NEXT:     Value: 72
+# CHECK-NEXT:     Section: .rdata (2)
+# CHECK-NEXT:     BaseType: Null (0x0)
 # CHECK-NEXT:     ComplexType: Null (0x0)
 # CHECK-NEXT:     StorageClass: External (0x2)
 # CHECK-NEXT:     AuxSymbolCount: 0
@@ -24,6 +34,16 @@
 # CHECK-NEXT:     Value: 64
 # CHECK-NEXT:     Section: .text (1)
 # CHECK-NEXT:     BaseType: Null (0x0)
+# CHECK-NEXT:     ComplexType: Function (0x2)
+# CHECK-NEXT:     StorageClass: External (0x2)
+# CHECK-NEXT:     AuxSymbolCount: 0
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Name: __imp_ExitProcess
+# DWARF-NEXT:     Value: 120
+# NODWF-NEXT:     Value: 64
+# CHECK-NEXT:     Section: .rdata (2)
+# CHECK-NEXT:     BaseType: Null (0x0)
 # CHECK-NEXT:     ComplexType: Null (0x0)
 # CHECK-NEXT:     StorageClass: External (0x2)
 # CHECK-NEXT:     AuxSymbolCount: 0
Index: lld/COFF/Writer.cpp
===================================================================
--- lld/COFF/Writer.cpp
+++ lld/COFF/Writer.cpp
@@ -1183,6 +1183,10 @@
     COFFSymbolRef ref = d->getCOFFSymbol();
     sym.Type = ref.getType();
     sym.StorageClass = ref.getStorageClass();
+  } else if (def->kind() == Symbol::DefinedImportThunkKind) {
+    sym.Type = (IMAGE_SYM_DTYPE_FUNCTION << SCT_COMPLEX_TYPE_SHIFT) |
+               IMAGE_SYM_TYPE_NULL;
+    sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
   } else {
     sym.Type = IMAGE_SYM_TYPE_NULL;
     sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
@@ -1229,6 +1233,14 @@
 
         if (Optional<coff_symbol16> sym = createSymbol(d))
           outputSymtab.push_back(*sym);
+
+        if (auto *dthunk = dyn_cast<DefinedImportThunk>(d)) {
+          if (!dthunk->wrappedSym->writtenToSymtab) {
+            dthunk->wrappedSym->writtenToSymtab = true;
+            if (Optional<coff_symbol16> sym = createSymbol(dthunk->wrappedSym))
+              outputSymtab.push_back(*sym);
+          }
+        }
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134169.461158.patch
Type: text/x-patch
Size: 3334 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220919/629663fb/attachment.bin>


More information about the llvm-commits mailing list