From 8b0f47bfa4b6aa1bafa10261444c93aba5a2d31d Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 7 Feb 2024 11:51:19 -0800 Subject: [PATCH] [Object][Wasm] Use file offset for section addresses in linked wasm files (#80529) Wasm has no unified virtual memory space as other object formats and architectures do, so previously WasmObjectFile reported 0 for all section addresses, and until 428cf71ff used section offsets for function symbols. Now we use file offsets for function symbols, and this change switches section addresses to do the same (in linked files). The main result of this is that objdump now reports VMAs in section listings, and also uses file offets rather than section offsets when disassembling linked binaries (matching the behavior of other disassemblers and stack traces produced by browwsers). To make this work, this PR also updates objdump's generation of synthetics fallback symbols to match lib/Object and also correctly plumbs symbol types for regular and dummy symbols through to the backend to avoid needing special knowledge of address 0. This also paves the way for generating symbols from name sections rather than symbol tables or imports (see #76107) by allowing the disassembler's synthetic fallback symbols match the name-section generated symbols (in a followup PR). --- lld/test/wasm/build-id.test | 12 ++++++------ lld/test/wasm/merge-string-debug.s | 14 +++++++------- lld/test/wasm/startstop.ll | 12 ++++++------ llvm/lib/Object/WasmObjectFile.cpp | 8 +++++++- .../Disassembler/WebAssemblyDisassembler.cpp | 3 ++- .../executable-without-symbols-debugnames.test | 12 ++++++------ .../wasm/executable-without-symbols.test | 12 ++++++------ .../test/tools/llvm-objdump/wasm/no-codesec.test | 6 +++--- llvm/tools/llvm-objdump/llvm-objdump.cpp | 16 +++++++++++++--- 9 files changed, 56 insertions(+), 39 deletions(-) diff --git a/lld/test/wasm/build-id.test b/lld/test/wasm/build-id.test index b767d8de4a423b..dd6e2237108f38 100644 --- a/lld/test/wasm/build-id.test +++ b/lld/test/wasm/build-id.test @@ -43,18 +43,18 @@ foo: # DEFAULT: Contents of section build_id: -# DEFAULT-NEXT: 0000 10299168 1e3c845a 3c8f80ae 2f16cc22 .).h.<.Z<.../.." -# DEFAULT-NEXT: 0010 2d +# DEFAULT-NEXT: 0079 10299168 1e3c845a 3c8f80ae 2f16cc22 .).h.<.Z<.../.." +# DEFAULT-NEXT: 0089 2d # SHA1: Contents of section build_id: -# SHA1-NEXT: 0000 145abdda 387a9bc4 e3aed3c3 3319cd37 .Z..8z......3..7 -# SHA1-NEXT: 0010 0212237c e4 ..#|. +# SHA1-NEXT: 0079 145abdda 387a9bc4 e3aed3c3 3319cd37 .Z..8z......3..7 +# SHA1-NEXT: 0089 0212237c e4 ..#|. # UUID: Contents of section build_id: -# UUID-NEXT: 0000 10 +# UUID-NEXT: 0079 10 # HEX: Contents of section build_id: -# HEX-NEXT: 0000 04123456 78 ..4Vx +# HEX-NEXT: 0079 04123456 78 ..4Vx # NONE-NOT: Contents of section build_id: diff --git a/lld/test/wasm/merge-string-debug.s b/lld/test/wasm/merge-string-debug.s index 73e31d5361f41f..0075a834e06477 100644 --- a/lld/test/wasm/merge-string-debug.s +++ b/lld/test/wasm/merge-string-debug.s @@ -29,13 +29,13 @@ # CHECK: Hex dump of section '.debug_str': -# CHECK-O0: 0x00000000 636c616e 67207665 7273696f 6e203133 clang version 13 -# CHECK-O0: 0x00000010 2e302e30 00666f6f 62617200 636c616e .0.0.foobar.clan -# CHECK-O0: 0x00000020 67207665 7273696f 6e203133 2e302e30 g version 13.0.0 -# CHECK-O0: 0x00000030 00626172 00666f6f 00 .bar.foo. +# CHECK-O0: 0x00000025 636c616e 67207665 7273696f 6e203133 clang version 13 +# CHECK-O0: 0x00000035 2e302e30 00666f6f 62617200 636c616e .0.0.foobar.clan +# CHECK-O0: 0x00000045 67207665 7273696f 6e203133 2e302e30 g version 13.0.0 +# CHECK-O0: 0x00000055 00626172 00666f6f 00 .bar.foo. -# CHECK-O1: 0x00000000 666f6f62 61720066 6f6f0063 6c616e67 foobar.foo.clang -# CHECK-O1: 0x00000010 20766572 73696f6e 2031332e 302e3000 version 13.0.0. +# CHECK-O1: 0x00000025 666f6f62 61720066 6f6f0063 6c616e67 foobar.foo.clang +# CHECK-O1: 0x00000035 20766572 73696f6e 2031332e 302e3000 version 13.0.0. # CHECK-OFFSETS: Hex dump of section '.debug_str_offsets': -# CHECK-OFFSETS: 0x00000000 00000000 00000000 00000000 ............ +# CHECK-OFFSETS: 0x0000007e 00000000 00000000 00000000 ............ diff --git a/lld/test/wasm/startstop.ll b/lld/test/wasm/startstop.ll index 4341d371f8e65e..e7a5c80f91f287 100644 --- a/lld/test/wasm/startstop.ll +++ b/lld/test/wasm/startstop.ll @@ -34,12 +34,12 @@ entry: ; CHECK-NEXT: Value: 1024 ; CHECK-NEXT: Content: 03000000040000002A0000002B000000 -; ASM: 00000001 : +; ASM: 0000006e : ; ASM-EMPTY: -; ASM-NEXT: 3: i32.const 1024 -; ASM-NEXT: 9: end +; ASM-NEXT: 70: i32.const 1024 +; ASM-NEXT: 76: end -; ASM: 0000000a : +; ASM: 00000077 : ; ASM-EMPTY: -; ASM-NEXT: c: i32.const 1040 -; ASM-NEXT: 12: end +; ASM-NEXT: 79: i32.const 1040 +; ASM-NEXT: 7f: end diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 4778562779bb7d..8c1bbe9c264593 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1906,7 +1906,13 @@ Expected WasmObjectFile::getSectionName(DataRefImpl Sec) const { return wasm::sectionTypeToString(S.Type); } -uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; } +uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { + // For object files, use 0 for section addresses, and section offsets for + // symbol addresses. For linked files, use file offsets. + // See also getSymbolAddress. + return isRelocatableObject() || isSharedObject() ? 0 + : Sections[Sec.d.a].Offset; +} uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const { return Sec.d.a; diff --git a/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp b/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp index ed7757be661582..861f9f8b5b1ba9 100644 --- a/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp +++ b/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp @@ -16,6 +16,7 @@ #include "MCTargetDesc/WebAssemblyMCTypeUtilities.h" #include "TargetInfo/WebAssemblyTargetInfo.h" +#include "llvm/BinaryFormat/Wasm.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCDecoderOps.h" #include "llvm/MC/MCDisassembler/MCDisassembler.h" @@ -127,7 +128,7 @@ WebAssemblyDisassembler::onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size, uint64_t Address, raw_ostream &CStream) const { Size = 0; - if (Address == 0) { + if (Symbol.Type == wasm::WASM_SYMBOL_TYPE_SECTION) { // Start of a code section: we're parsing only the function count. int64_t FunctionCount; if (!nextLEB(FunctionCount, Bytes, Size, false)) diff --git a/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols-debugnames.test b/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols-debugnames.test index 646891700b8664..0c7a438f2752ee 100644 --- a/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols-debugnames.test +++ b/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols-debugnames.test @@ -36,14 +36,14 @@ Sections: # CHECK: Disassembly of section CODE: # CHECK-EMPTY: -# CHECK-NEXT: 00000000 : +# CHECK-NEXT: 00000026 : # CHECK-NEXT: # 2 functions in section. # CHECK-EMPTY: -# CHECK-NEXT: 00000001 : +# CHECK-NEXT: 00000027 : # CHECK-EMPTY: -# CHECK-NEXT: 3: 0b end +# CHECK-NEXT: 29: 0b end # CHECK-EMPTY: -# CHECK-NEXT: 00000004 : +# CHECK-NEXT: 0000002a : # CHECK-EMPTY: -# CHECK-NEXT: 6: 20 00 local.get 0 -# CHECK-NEXT: 8: 0b end +# CHECK-NEXT: 2c: 20 00 local.get 0 +# CHECK-NEXT: 2e: 0b end diff --git a/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols.test b/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols.test index 633991eecfbf3b..58643cac90147f 100644 --- a/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols.test +++ b/llvm/test/tools/llvm-objdump/wasm/executable-without-symbols.test @@ -29,14 +29,14 @@ Sections: # CHECK: Disassembly of section CODE: # CHECK-EMPTY: -# CHECK-NEXT: 00000000 : +# CHECK-NEXT: 00000026 : # CHECK-NEXT: # 2 functions in section. # CHECK-EMPTY: -# CHECK-NEXT: 00000001 <>: +# CHECK-NEXT: 00000027 <>: # CHECK-EMPTY: -# CHECK-NEXT: 3: 0b end +# CHECK-NEXT: 29: 0b end # CHECK-EMPTY: -# CHECK-NEXT: 00000004 <>: +# CHECK-NEXT: 0000002a <>: # CHECK-EMPTY: -# CHECK-NEXT: 6: 20 00 local.get 0 -# CHECK-NEXT: 8: 0b end +# CHECK-NEXT: 2c: 20 00 local.get 0 +# CHECK-NEXT: 2e: 0b end diff --git a/llvm/test/tools/llvm-objdump/wasm/no-codesec.test b/llvm/test/tools/llvm-objdump/wasm/no-codesec.test index e66a8ddd8a0663..03fd1495066ceb 100644 --- a/llvm/test/tools/llvm-objdump/wasm/no-codesec.test +++ b/llvm/test/tools/llvm-objdump/wasm/no-codesec.test @@ -4,9 +4,9 @@ # CHECK: Sections: # CHECK-NEXT: Idx Name Size VMA Type -# CHECK-NEXT: 0 TYPE 00000004 00000000 -# CHECK-NEXT: 1 FUNCTION 00000002 00000000 -# CHECK-NEXT: 2 name 00000008 00000000 +# CHECK-NEXT: 0 TYPE 00000004 0000000e +# CHECK-NEXT: 1 FUNCTION 00000002 00000018 +# CHECK-NEXT: 2 name 00000008 00000020 --- !WASM FileHeader: diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index b4467ec163c342..de52ebcc72fded 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -29,6 +29,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/Twine.h" +#include "llvm/BinaryFormat/Wasm.h" #include "llvm/DebugInfo/BTF/BTFParser.h" #include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" @@ -1149,7 +1150,11 @@ addMissingWasmCodeSymbols(const WasmObjectFile &Obj, SymbolAddresses.insert(Sym.Addr); for (const wasm::WasmFunction &Function : Obj.functions()) { - uint64_t Address = Function.CodeSectionOffset; + // This adjustment mirrors the one in WasmObjectFile::getSymbolAddress. + uint32_t Adjustment = Obj.isRelocatableObject() || Obj.isSharedObject() + ? 0 + : Section->getAddress(); + uint64_t Address = Function.CodeSectionOffset + Adjustment; // Only add fallback symbols for functions not already present in the symbol // table. if (SymbolAddresses.count(Address)) @@ -1354,6 +1359,10 @@ SymbolInfoTy objdump::createSymbolInfo(const ObjectFile &Obj, const SymbolRef::Type SymType = unwrapOrError(Symbol.getType(), FileName); return SymbolInfoTy(Addr, Name, SymType, /*IsMappingSymbol=*/false, /*IsXCOFF=*/true); + } else if (Obj.isWasm()) { + uint8_t SymType = + cast(&Obj)->getWasmSymbol(Symbol).Info.Kind; + return SymbolInfoTy(Addr, Name, SymType, false); } else { uint8_t Type = Obj.isELF() ? getElfSymbolType(Obj, Symbol) : (uint8_t)ELF::STT_NOTYPE; @@ -1366,8 +1375,9 @@ static SymbolInfoTy createDummySymbolInfo(const ObjectFile &Obj, uint8_t Type) { if (Obj.isXCOFF() && (SymbolDescription || TracebackTable)) return SymbolInfoTy(std::nullopt, Addr, Name, std::nullopt, false); - else - return SymbolInfoTy(Addr, Name, Type); + if (Obj.isWasm()) + return SymbolInfoTy(Addr, Name, wasm::WASM_SYMBOL_TYPE_SECTION); + return SymbolInfoTy(Addr, Name, Type); } static void collectBBAddrMapLabels(