llvm.org GIT mirror llvm / eb7fd73
[WebAssembly] Remove debug names from symbol table Get rid of DEBUG_FUNCTION_NAME symbols. When we actually debug data, maybe we'll want somewhere to put it... but having a symbol that just stores the name of another symbol seems odd. It means you have multiple Symbols with the same name, one containing the actual function and another containing the name! Store the names in a vector on the WasmObjectFile when reading them in. Also stash them on the WasmFunctions themselves. The names are //not// "symbol names" or aliases or anything, they're just the name that a debugger should show against the function body itself. NB. The WasmObjectFile stores them so that they can be exported in the YAML losslessly, and hence the tests can be precise. Enforce that the CODE section has been read in before reading the "names" section. Requires minor adjustment to some tests. Patch by Nicholas Wilson! Differential Revision: https://reviews.llvm.org/D42075 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322741 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 1 year, 8 months ago
12 changed file(s) with 90 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
9494 ArrayRef Body;
9595 uint32_t CodeSectionOffset;
9696 uint32_t Size;
97 StringRef Comdat;
97 StringRef Name; // from the "names" section
98 StringRef Comdat; // from the "comdat info" section
9899 };
99100
100101 struct WasmDataSegment {
104105 StringRef Name;
105106 uint32_t Alignment;
106107 uint32_t Flags;
107 StringRef Comdat;
108 StringRef Comdat; // from the "comdat info" section
108109 };
109110
110111 struct WasmElemSegment {
115116
116117 struct WasmRelocation {
117118 uint32_t Type; // The type of the relocation.
118 uint32_t Index; // Index into function to global index space.
119 uint32_t Index; // Index into function or global index space.
119120 uint64_t Offset; // Offset from the start of the section.
120121 int64_t Addend; // A value to add to the symbol.
121122 };
123124 struct WasmInitFunc {
124125 uint32_t Priority;
125126 uint32_t FunctionIndex;
127 };
128
129 struct WasmFunctionName {
130 uint32_t Index;
131 StringRef Name;
126132 };
127133
128134 struct WasmLinkingData {
3838 FUNCTION_EXPORT,
3939 GLOBAL_IMPORT,
4040 GLOBAL_EXPORT,
41 DEBUG_FUNCTION_NAME,
4241 };
4342
4443 WasmSymbol(StringRef Name, SymbolType Type, uint32_t Section,
6968
7069 bool isFunction() const {
7170 return Type == WasmSymbol::SymbolType::FUNCTION_IMPORT ||
72 Type == WasmSymbol::SymbolType::FUNCTION_EXPORT ||
73 Type == WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME;
71 Type == WasmSymbol::SymbolType::FUNCTION_EXPORT;
7472 }
7573
7674
149147 ArrayRef dataSegments() const { return DataSegments; }
150148 ArrayRef functions() const { return Functions; }
151149 ArrayRef comdats() const { return Comdats; }
150 ArrayRef debugNames() const { return DebugNames; }
152151 uint32_t startFunction() const { return StartFunction; }
153152
154153 void moveSymbolNext(DataRefImpl &Symb) const override;
252251 std::vector Functions;
253252 std::vector Symbols;
254253 std::vector Comdats;
254 std::vector DebugNames;
255255 uint32_t StartFunction = -1;
256256 bool HasLinkingSection = false;
257257 wasm::WasmLinkingData LinkingData;
220220 FunctionTypeIndices;
221221 SmallVector FunctionTypes;
222222 SmallVector Globals;
223 unsigned NumFunctionImports = 0;
223224 unsigned NumGlobalImports = 0;
224225
225226 // TargetObjectWriter wrappers.
251252 FunctionTypes.clear();
252253 Globals.clear();
253254 MCObjectWriter::reset();
255 NumFunctionImports = 0;
254256 NumGlobalImports = 0;
255257 }
256258
285287 ArrayRef Functions);
286288 void writeDataSection(ArrayRef Segments);
287289 void writeNameSection(ArrayRef Functions,
288 ArrayRef Imports,
289 uint32_t NumFuncImports);
290 ArrayRef Imports);
290291 void writeCodeRelocSection();
291292 void writeDataRelocSection();
292293 void writeLinkingMetaDataSection(
851852 endSection(Section);
852853 }
853854
854 void WasmObjectWriter::writeNameSection(
855 ArrayRef Functions,
856 ArrayRef Imports,
857 unsigned NumFuncImports) {
858 uint32_t TotalFunctions = NumFuncImports + Functions.size();
855 void WasmObjectWriter::writeNameSection(ArrayRef Functions,
856 ArrayRef Imports) {
857 uint32_t TotalFunctions = NumFunctionImports + Functions.size();
859858 if (TotalFunctions == 0)
860859 return;
861860
10221021 SmallVector, 4> SymbolFlags;
10231022 SmallVector, 2> InitFuncs;
10241023 std::map> Comdats;
1025 unsigned NumFuncImports = 0;
10261024 SmallVector DataSegments;
10271025 uint32_t DataSize = 0;
10281026
11121110 const auto &WS = static_cast(S);
11131111
11141112 // Register types for all functions, including those with private linkage
1115 // (making them
1116 // because wasm always needs a type signature.
1113 // (because wasm always needs a type signature).
11171114 if (WS.isFunction())
11181115 registerFunctionType(WS);
11191116
11301127 if (WS.isFunction()) {
11311128 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
11321129 Import.Type = getFunctionType(WS);
1133 SymbolIndices[&WS] = NumFuncImports;
1134 ++NumFuncImports;
1130 SymbolIndices[&WS] = NumFunctionImports;
1131 ++NumFunctionImports;
11351132 } else {
11361133 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
11371134 Import.Type = int32_t(PtrType);
12151212 "function symbols must have a size set with .size");
12161213
12171214 // A definition. Take the next available index.
1218 Index = NumFuncImports + Functions.size();
1215 Index = NumFunctionImports + Functions.size();
12191216
12201217 // Prepare the function.
12211218 WasmFunction Func;
14091406 writeElemSection(TableElems);
14101407 writeCodeSection(Asm, Layout, Functions);
14111408 writeDataSection(DataSegments);
1412 writeNameSection(Functions, Imports, NumFuncImports);
1409 writeNameSection(Functions, Imports);
14131410 writeCodeRelocSection();
14141411 writeDataRelocSection();
14151412 writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
269269
270270 Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
271271 llvm::DenseSet Seen;
272 if (Functions.size() != FunctionTypes.size()) {
273 return make_error("Names must come after code section",
274 object_error::parse_failed);
275 }
272276
273277 while (Ptr < End) {
274278 uint8_t Type = readVarint7(Ptr);
283287 return make_error("Function named more than once",
284288 object_error::parse_failed);
285289 StringRef Name = readString(Ptr);
286 if (!Name.empty())
287 Symbols.emplace_back(Name,
288 WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME,
289 Sections.size(), Index);
290 if (!isValidFunctionIndex(Index) || Name.empty())
291 return make_error("Invalid name entry",
292 object_error::parse_failed);
293 DebugNames.push_back(wasm::WasmFunctionName{Index, Name});
294 if (Index >= NumImportedFunctions) {
295 // Override any existing name; the name specified by the "names"
296 // section is the Function's canonical name.
297 Functions[Index - NumImportedFunctions].Name = Name;
298 }
290299 }
291300 break;
292301 }
359368 << " sym index:" << SymIndex << "\n");
360369 }
361370 }
371 if (Export.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
372 auto &Function = Functions[Export.Index - NumImportedFunctions];
373 if (Function.Name.empty()) {
374 // Use the export's name to set a name for the Function, but only if one
375 // hasn't already been set.
376 Function.Name = Export.Name;
377 }
378 }
362379 }
363380 }
364381
365382 Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr,
366383 const uint8_t *End) {
367384 HasLinkingSection = true;
385 if (Functions.size() != FunctionTypes.size()) {
386 return make_error(
387 "Linking data must come after code section", object_error::parse_failed);
388 }
368389
369390 // Only populate the symbol table with imports and exports if the object
370391 // has a linking section (i.e. its a relocatable object file). Otherwise
866887 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
867888 Result |= SymbolRef::SF_Executable;
868889 break;
869 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
870 Result |= SymbolRef::SF_Executable;
871 Result |= SymbolRef::SF_FormatSpecific;
872 break;
873890 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
874891 Result |= SymbolRef::SF_Undefined;
875892 break;
913930 case WasmSymbol::SymbolType::FUNCTION_IMPORT:
914931 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
915932 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
916 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
917933 return Sym.ElementIndex;
918934 case WasmSymbol::SymbolType::GLOBAL_EXPORT: {
919935 uint32_t GlobalIndex = Sym.ElementIndex - NumImportedGlobals;
948964 switch (Sym.Type) {
949965 case WasmSymbol::SymbolType::FUNCTION_IMPORT:
950966 case WasmSymbol::SymbolType::FUNCTION_EXPORT:
951 case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
952967 return SymbolRef::ST_Function;
953968 case WasmSymbol::SymbolType::GLOBAL_IMPORT:
954969 case WasmSymbol::SymbolType::GLOBAL_EXPORT:
238238 ; CHECK-NEXT: ...
239239
240240 ; CHECK-SYMS: SYMBOL TABLE:
241 ; CHECK-SYMS-NEXT: 00000000 g F name foo_alias
242 ; CHECK-SYMS-NEXT: 00000001 g F name foo
243 ; CHECK-SYMS-NEXT: 00000002 g F name call_direct
244 ; CHECK-SYMS-NEXT: 00000003 g F name call_alias
245 ; CHECK-SYMS-NEXT: 00000004 g F name call_direct_ptr
246 ; CHECK-SYMS-NEXT: 00000005 g F name call_alias_ptr
247241 ; CHECK-SYMS-NEXT: 00000001 gw F EXPORT .hidden foo_alias
248242 ; CHECK-SYMS-NEXT: 00000000 gw EXPORT .hidden bar_alias
249243 ; CHECK-SYMS-NEXT: 00000001 g F EXPORT .hidden foo
2525 - Name: global_export
2626 Kind: GLOBAL
2727 Index: 0
28 - Type: CODE
29 Functions:
30 - Index: 0
31 Locals:
32 Body: 00
33 - Index: 1
34 Locals:
35 Body: 00
2836 - Type: CUSTOM
2937 Name: linking
3038 DataSize: 10
5353 - Name: bar
5454 Kind: GLOBAL
5555 Index: 0x00000003
56 - Type: CODE
57 Functions:
58 - Index: 1
59 Locals:
60 Body: 00
61 - Index: 2
62 Locals:
63 Body: 00
64 - Index: 3
65 Locals:
66 Body: 00
67 - Index: 4
68 Locals:
69 Body: 00
70 - Index: 5
71 Locals:
72 Body: 00
5673 - Type: CUSTOM
5774 Name: "linking"
5875 DataSize: 0
5353 - Name: weak_global_data
5454 Kind: GLOBAL
5555 Index: 0x00000003
56 - Type: CODE
57 Functions:
58 - Index: 1
59 Locals:
60 Body: 00
61 - Index: 2
62 Locals:
63 Body: 00
64 - Index: 3
65 Locals:
66 Body: 00
67 - Index: 4
68 Locals:
69 Body: 00
5670 - Type: CUSTOM
5771 Name: linking
5872 DataSize: 0
0 RUN: llvm-objdump -t %p/../Inputs/trivial.obj.wasm | FileCheck %s
11
22 CHECK: SYMBOL TABLE:
3 CHECK-NEXT: 00000000 g F name puts
4 CHECK-NEXT: 00000001 g F name SomeOtherFunction
5 CHECK-NEXT: 00000002 g F name main
63 CHECK-NEXT: 00000000 g F IMPORT puts
74 CHECK-NEXT: 00000000 g F IMPORT SomeOtherFunction
85 CHECK-NEXT: 00000002 g F EXPORT main
7373 WASM: Symbols [
7474 WASM-NEXT: Symbol {
7575 WASM-NEXT: Name: puts
76 WASM-NEXT: Type: DEBUG_FUNCTION_NAME (0x4)
77 WASM-NEXT: Flags: 0x0
78 WASM-NEXT: }
79 WASM-NEXT: Symbol {
80 WASM-NEXT: Name: SomeOtherFunction
81 WASM-NEXT: Type: DEBUG_FUNCTION_NAME (0x4)
82 WASM-NEXT: Flags: 0x0
83 WASM-NEXT: }
84 WASM-NEXT: Symbol {
85 WASM-NEXT: Name: main
86 WASM-NEXT: Type: DEBUG_FUNCTION_NAME (0x4)
87 WASM-NEXT: Flags: 0x0
88 WASM-NEXT: }
89 WASM-NEXT: Symbol {
90 WASM-NEXT: Name: puts
9176 WASM-NEXT: Type: FUNCTION_IMPORT (0x0)
9277 WASM-NEXT: Flags: 0x0
9378 WASM-NEXT: }
2727 ENUM_ENTRY(FUNCTION_EXPORT),
2828 ENUM_ENTRY(GLOBAL_IMPORT),
2929 ENUM_ENTRY(GLOBAL_EXPORT),
30 ENUM_ENTRY(DEBUG_FUNCTION_NAME),
3130 #undef ENUM_ENTRY
3231 };
3332
5252 std::unique_ptr CustomSec;
5353 if (WasmSec.Name == "name") {
5454 std::unique_ptr NameSec = make_unique();
55 for (const object::SymbolRef& Sym: Obj.symbols()) {
56 const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
57 if (Symbol.Type != object::WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME)
58 continue;
55 for (const llvm::wasm::WasmFunctionName &Func: Obj.debugNames()) {
5956 WasmYAML::NameEntry NameEntry;
60 NameEntry.Name = Symbol.Name;
61 NameEntry.Index = Sym.getValue();
57 NameEntry.Name = Func.Name;
58 NameEntry.Index = Func.Index;
6259 NameSec->FunctionNames.push_back(NameEntry);
6360 }
6461 CustomSec = std::move(NameSec);