Skip to content

Commit acf7d08

Browse files
alvinhochunmstorsjo
authored andcommitted
[lldb][COFF] Add note to forwarder export symbols in symtab
Forwarder exports do not point to a real function or variable. Instead they point to a string describing which DLL and symbol to forward to. Any imports which uses them will be redirected by the loader transparently. These symbols do not have much use in LLDB, but keep them just in case someone find it useful. Also set a synthesized name with the forwarder string for informational purpose. Reviewed By: labath Differential Revision: https://reviews.llvm.org/D134518
1 parent 7ebff6a commit acf7d08

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,26 @@ ObjectFilePECOFF::AppendFromExportTable(SectionList *sect_list,
869869
llvm::cantFail(entry.getOrdinal(ordinal));
870870
symbol.SetID(ordinal);
871871

872+
bool is_forwarder;
873+
llvm::cantFail(entry.isForwarder(is_forwarder));
874+
if (is_forwarder) {
875+
// Forwarder exports are redirected by the loader transparently, but keep
876+
// it in symtab and make a note using the symbol name.
877+
llvm::StringRef forwarder_name;
878+
if (auto err = entry.getForwardTo(forwarder_name)) {
879+
LLDB_LOG(log,
880+
"ObjectFilePECOFF::AppendFromExportTable - failed to get "
881+
"forwarder name of forwarder export '{0}': {1}",
882+
sym_name, llvm::fmt_consume(std::move(err)));
883+
continue;
884+
}
885+
llvm::SmallString<256> new_name = {symbol.GetDisplayName().GetStringRef(),
886+
" (forwarded to ", forwarder_name,
887+
")"};
888+
symbol.GetMangled().SetDemangledName(ConstString(new_name.str()));
889+
symbol.SetDemangledNameIsSynthesized(true);
890+
}
891+
872892
uint32_t function_rva;
873893
if (auto err = entry.getExportRVA(function_rva)) {
874894
LLDB_LOG(log,
@@ -886,9 +906,10 @@ ObjectFilePECOFF::AppendFromExportTable(SectionList *sect_list,
886906
// An exported symbol may be either code or data. Guess by checking whether
887907
// the section containing the symbol is executable.
888908
symbol.SetType(lldb::eSymbolTypeData);
889-
if (auto section_sp = symbol.GetAddressRef().GetSection())
890-
if (section_sp->GetPermissions() & ePermissionsExecutable)
891-
symbol.SetType(lldb::eSymbolTypeCode);
909+
if (!is_forwarder)
910+
if (auto section_sp = symbol.GetAddressRef().GetSection())
911+
if (section_sp->GetPermissions() & ePermissionsExecutable)
912+
symbol.SetType(lldb::eSymbolTypeCode);
892913
symbol.SetExternal(true);
893914
uint32_t idx = symtab.AddSymbol(symbol);
894915
export_list.push_back(std::make_pair(function_rva, idx));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: lldb-test symbols %t | FileCheck %s
3+
4+
# CHECK: UserID DSX Type File Address/Value {{.*}} Size Flags Name
5+
# CHECK-NEXT: ------
6+
# CHECK-NEXT: 1 X Data 0x{{[0-9a-f]+}} 0x{{[0-9a-f]+}} 0x{{[0-9a-f]+}} LoadLibrary (forwarded to kernel32.LoadLibrary)
7+
# CHECK-EMPTY:
8+
9+
# Test file generated with:
10+
# clang -O2 --target=x86_64-windows-msvc test.c -nostdlib -c -o test.obj
11+
# lld-link -dll -out:test.dll -entry:entry -export:LoadLibrary=kernel32.LoadLibrary test.obj
12+
# test.c:
13+
# void entry(void) {}
14+
15+
--- !COFF
16+
OptionalHeader:
17+
AddressOfEntryPoint: 4096
18+
ImageBase: 6442450944
19+
SectionAlignment: 4096
20+
FileAlignment: 512
21+
MajorOperatingSystemVersion: 6
22+
MinorOperatingSystemVersion: 0
23+
MajorImageVersion: 0
24+
MinorImageVersion: 0
25+
MajorSubsystemVersion: 6
26+
MinorSubsystemVersion: 0
27+
Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI
28+
DLLCharacteristics: [ IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ]
29+
SizeOfStackReserve: 1048576
30+
SizeOfStackCommit: 4096
31+
SizeOfHeapReserve: 1048576
32+
SizeOfHeapCommit: 4096
33+
ExportTable:
34+
RelativeVirtualAddress: 8192
35+
Size: 110
36+
header:
37+
Machine: IMAGE_FILE_MACHINE_AMD64
38+
Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE, IMAGE_FILE_DLL ]
39+
sections:
40+
- Name: .text
41+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
42+
VirtualAddress: 4096
43+
VirtualSize: 1
44+
SectionData: C3
45+
- Name: .rdata
46+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
47+
VirtualAddress: 8192
48+
VirtualSize: 110
49+
SectionData: 0000000000000000000000002820000001000000010000000100000043200000472000004B2000006578706F72742D666F727761726465722E632E746D702E646C6C00592000004D20000000004C6F61644C696272617279006B65726E656C33322E4C6F61644C69627261727900
50+
symbols: []
51+
...

0 commit comments

Comments
 (0)