Skip to content

Commit 1500646

Browse files
committed
[lld][WebAssembly] Fix __start/__stop symbols when combining input segments
We should be generating one __start/__stop pair per output segment not per input segment. The test wasn't catching this because it was only linking a single object file. Fixes PR41565 Differential Revision: https://reviews.llvm.org/D64148 llvm-svn: 365308
1 parent 1f7bd40 commit 1500646

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target triple = "wasm32-unknown-unknown"
2+
3+
@var1 = global i32 42, section "mysection", align 4
4+
@var2 = global i32 43, section "mysection", align 4

lld/test/wasm/startstop.ll

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; RUN: llc -filetype=obj -o %t.o %s
2-
; RUN: wasm-ld --no-gc-sections %t.o -o %t.wasm
2+
; RUN: llc -filetype=obj %p/Inputs/explicit-section.ll -o %t2.o
3+
; RUN: wasm-ld --export=get_start --export=get_end --export=foo --export=var1 %t.o %t2.o -o %t.wasm
34
; RUN: obj2yaml %t.wasm | FileCheck %s
45

56
target triple = "wasm32-unknown-unknown"
@@ -26,15 +27,12 @@ entry:
2627
; CHECK-NEXT: Functions:
2728
; CHECK-NEXT: - Index: 0
2829
; CHECK-NEXT: Locals: []
29-
; CHECK-NEXT: Body: 0B
30+
; CHECK-NEXT: Body: 4180888080000B
3031
; CHECK-NEXT: - Index: 1
3132
; CHECK-NEXT: Locals: []
32-
; CHECK-NEXT: Body: 4180888080000B
33+
; CHECK-NEXT: Body: 4190888080000B
3334
; CHECK-NEXT: - Index: 2
3435
; CHECK-NEXT: Locals: []
35-
; CHECK-NEXT: Body: 4188888080000B
36-
; CHECK-NEXT: - Index: 3
37-
; CHECK-NEXT: Locals: []
3836
; CHECK-NEXT: Body: 0B
3937
; CHECK-NEXT: - Type: DATA
4038
; CHECK-NEXT: Segments:
@@ -43,15 +41,13 @@ entry:
4341
; CHECK-NEXT: Offset:
4442
; CHECK-NEXT: Opcode: I32_CONST
4543
; CHECK-NEXT: Value: 1024
46-
; CHECK-NEXT: Content: '0300000004000000'
44+
; CHECK-NEXT: Content: 03000000040000002A0000002B000000
4745
; CHECK-NEXT: - Type: CUSTOM
4846
; CHECK-NEXT: Name: name
4947
; CHECK-NEXT: FunctionNames:
5048
; CHECK-NEXT: - Index: 0
51-
; CHECK-NEXT: Name: __wasm_call_ctors
52-
; CHECK-NEXT: - Index: 1
5349
; CHECK-NEXT: Name: get_start
54-
; CHECK-NEXT: - Index: 2
50+
; CHECK-NEXT: - Index: 1
5551
; CHECK-NEXT: Name: get_end
56-
; CHECK-NEXT: - Index: 3
52+
; CHECK-NEXT: - Index: 2
5753
; CHECK-NEXT: Name: _start

lld/wasm/InputChunks.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ void InputChunk::writeTo(uint8_t *Buf) const {
107107
for (const WasmRelocation &Rel : Relocations) {
108108
uint8_t *Loc = Buf + Rel.Offset + Off;
109109
uint32_t Value = File->calcNewValue(Rel);
110-
LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type)
111-
<< " addend=" << Rel.Addend << " index=" << Rel.Index
110+
LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type));
111+
if (Rel.Type != R_WASM_TYPE_INDEX_LEB)
112+
LLVM_DEBUG(dbgs() << " sym=" << File->getSymbols()[Rel.Index]->getName());
113+
LLVM_DEBUG(dbgs() << " addend=" << Rel.Addend << " index=" << Rel.Index
112114
<< " value=" << Value << " offset=" << Rel.Offset
113115
<< "\n");
114116

lld/wasm/SymbolTable.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ DefinedData *SymbolTable::addOptionalDataSymbol(StringRef Name, uint32_t Value,
228228
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << Name << "\n");
229229
auto *rtn = replaceSymbol<DefinedData>(S, Name, Flags);
230230
rtn->setVirtualAddress(Value);
231+
rtn->Referenced = true;
231232
return rtn;
232233
}
233234

lld/wasm/Symbols.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ class Symbol {
131131
uint32_t OutputSymbolIndex = INVALID_INDEX;
132132
uint32_t GOTIndex = INVALID_INDEX;
133133
Kind SymbolKind;
134-
bool Referenced : 1;
135134

136135
public:
136+
bool Referenced : 1;
137+
137138
// True if the symbol was used for linking and thus need to be added to the
138139
// output file's symbol table. This is true for all symbols except for
139140
// unreferenced DSO symbols, lazy (archive) symbols, and bitcode symbols that

lld/wasm/Writer.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class Writer {
7474
void addSection(OutputSection *Sec);
7575

7676
void addSections();
77-
void addStartStopSymbols(const InputSegment *Seg);
7877

7978
void createCustomSections();
8079
void createSyntheticSections();
@@ -303,15 +302,15 @@ void Writer::addSection(OutputSection *Sec) {
303302
// __stop_<secname> symbols. They are at beginning and end of the section,
304303
// respectively. This is not requested by the ELF standard, but GNU ld and
305304
// gold provide the feature, and used by many programs.
306-
void Writer::addStartStopSymbols(const InputSegment *Seg) {
307-
StringRef S = Seg->getName();
308-
LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << S << "\n");
309-
if (!isValidCIdentifier(S))
305+
static void addStartStopSymbols(const OutputSegment *Seg) {
306+
StringRef Name = Seg->Name;
307+
LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << Name << "\n");
308+
if (!isValidCIdentifier(Name))
310309
return;
311-
uint32_t Start = Seg->OutputSeg->StartVA + Seg->OutputSegmentOffset;
312-
uint32_t Stop = Start + Seg->getSize();
313-
Symtab->addOptionalDataSymbol(Saver.save("__start_" + S), Start);
314-
Symtab->addOptionalDataSymbol(Saver.save("__stop_" + S), Stop);
310+
uint32_t Start = Seg->StartVA;
311+
uint32_t Stop = Start + Seg->Size;
312+
Symtab->addOptionalDataSymbol(Saver.save("__start_" + Name), Start);
313+
Symtab->addOptionalDataSymbol(Saver.save("__stop_" + Name), Stop);
315314
}
316315

317316
void Writer::addSections() {
@@ -811,8 +810,7 @@ void Writer::run() {
811810
// Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
812811
// This has to be done after memory layout is performed.
813812
for (const OutputSegment *Seg : Segments)
814-
for (const InputSegment *S : Seg->InputSegments)
815-
addStartStopSymbols(S);
813+
addStartStopSymbols(Seg);
816814
}
817815

818816
log("-- scanRelocations");

0 commit comments

Comments
 (0)