From c136e5507a3f3d0a35e83c9db99c6081735aa5a0 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Fri, 31 Jul 2015 16:14:22 +0000 Subject: [PATCH] [COFF] Return symbol VAs instead of RVAs for PE files This makes llvm-nm consistent with binutils nm on executables and DLLs. For a vanilla hello world executable, the address of main should include the default image base of 0x400000. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243755 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/COFFObjectFile.cpp | 8 +++++ test/Object/Inputs/main-ret-zero-pe-i386.dll | Bin 0 -> 5120 bytes test/Object/Inputs/main-ret-zero-pe-i386.exe | Bin 0 -> 5120 bytes test/Object/nm-pe-image.test | 31 +++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100755 test/Object/Inputs/main-ret-zero-pe-i386.dll create mode 100755 test/Object/Inputs/main-ret-zero-pe-i386.exe create mode 100644 test/Object/nm-pe-image.test diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index 1ae8059a02166..ed63f3a64e099 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -171,6 +171,14 @@ ErrorOr COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const { if (std::error_code EC = getSection(SectionNumber, Section)) return EC; Result += Section->VirtualAddress; + + // The section VirtualAddress does not include ImageBase, and we want to + // return virtual addresses. + if (PE32Header) + Result += PE32Header->ImageBase; + else if (PE32PlusHeader) + Result += PE32Header->ImageBase; + return Result; } diff --git a/test/Object/Inputs/main-ret-zero-pe-i386.dll b/test/Object/Inputs/main-ret-zero-pe-i386.dll new file mode 100755 index 0000000000000000000000000000000000000000..d4a343088b1787ea77988cf06ec1de7996b0941d GIT binary patch literal 5120 zcmeZ`V!#R70qD^3=9cC(NQoO z0;3@?8UmvsFd71*Aut*OqaiRF0;3@?8UmvsFd6~_9s;4APs5tu@N~Nhbo=sjhjQ!# z_1imLe{}kO=?wkQ`Jwp;Ph9NbGoxUbh5&3F1C$U!QP0evmy%eL2;u?(R3xdm7{Z5% a#Jl9=_$FrNLDVP!2R}gLKBHjhg#ZBf>zYsi literal 0 HcmV?d00001 diff --git a/test/Object/Inputs/main-ret-zero-pe-i386.exe b/test/Object/Inputs/main-ret-zero-pe-i386.exe new file mode 100755 index 0000000000000000000000000000000000000000..366767b9b773aca94c13d5e00a63f9fbe6ed7489 GIT binary patch literal 5120 zcmeZ`V!#REx0}Kxwcl`r$MDr1r*u!T= z!3YWg*f<6#`awfB%nW)di6x03E)YOPl8TEVe3(dlZenI0REUv50XWbD8uu9mLoWmX DX6B#~ literal 0 HcmV?d00001 diff --git a/test/Object/nm-pe-image.test b/test/Object/nm-pe-image.test new file mode 100644 index 0000000000000..f95aeebf7271d --- /dev/null +++ b/test/Object/nm-pe-image.test @@ -0,0 +1,31 @@ +The executable was generated like so: +$ cat t.c +int main() { return 0; } +$ clang --target=i686-windows -c t.c -o t.o +$ lld-link2 t.o -out:t.exe -entry:main -debug + +It has a mingw-style symbol table in the executable, which MSVC-produced images +don't have. + +RUN: llvm-nm %p/Inputs/main-ret-zero-pe-i386.exe \ +RUN: | FileCheck %s -check-prefix PE-EXE + +PE-EXE: 00401000 t .bss +PE-EXE: 00401000 t .data +PE-EXE: 00401000 t .text +PE-EXE: 00401000 T _main + +The DLL was generated like so: +$ cat t.c +int DllMain(void *mod, long reason, void *reserved) { return 1; } +$ clang --target=i686-windows -c t.c -o t.o +$ lld-link2 t.o -out:t.exe -entry:DllMain -debug -dll + + +RUN: llvm-nm %p/Inputs/main-ret-zero-pe-i386.dll \ +RUN: | FileCheck %s -check-prefix PE-DLL + +PE-DLL: 10001000 t .bss +PE-DLL: 10001000 t .data +PE-DLL: 10001000 t .text +PE-DLL: 10001000 T _DllMain