[lld] r365308 - [lld][WebAssembly] Fix __start/__stop symbols when combining input segments
Sam Clegg via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 8 03:35:08 PDT 2019
Author: sbc
Date: Mon Jul 8 03:35:08 2019
New Revision: 365308
URL: http://llvm.org/viewvc/llvm-project?rev=365308&view=rev
Log:
[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
Added:
lld/trunk/test/wasm/Inputs/explicit-section.ll
Modified:
lld/trunk/test/wasm/startstop.ll
lld/trunk/wasm/InputChunks.cpp
lld/trunk/wasm/SymbolTable.cpp
lld/trunk/wasm/Symbols.h
lld/trunk/wasm/Writer.cpp
Added: lld/trunk/test/wasm/Inputs/explicit-section.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/Inputs/explicit-section.ll?rev=365308&view=auto
==============================================================================
--- lld/trunk/test/wasm/Inputs/explicit-section.ll (added)
+++ lld/trunk/test/wasm/Inputs/explicit-section.ll Mon Jul 8 03:35:08 2019
@@ -0,0 +1,4 @@
+target triple = "wasm32-unknown-unknown"
+
+ at var1 = global i32 42, section "mysection", align 4
+ at var2 = global i32 43, section "mysection", align 4
Modified: lld/trunk/test/wasm/startstop.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/startstop.ll?rev=365308&r1=365307&r2=365308&view=diff
==============================================================================
--- lld/trunk/test/wasm/startstop.ll (original)
+++ lld/trunk/test/wasm/startstop.ll Mon Jul 8 03:35:08 2019
@@ -1,5 +1,6 @@
; RUN: llc -filetype=obj -o %t.o %s
-; RUN: wasm-ld --no-gc-sections %t.o -o %t.wasm
+; RUN: llc -filetype=obj %p/Inputs/explicit-section.ll -o %t2.o
+; RUN: wasm-ld --export=get_start --export=get_end --export=foo --export=var1 %t.o %t2.o -o %t.wasm
; RUN: obj2yaml %t.wasm | FileCheck %s
target triple = "wasm32-unknown-unknown"
@@ -26,15 +27,12 @@ entry:
; CHECK-NEXT: Functions:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: Locals: []
-; CHECK-NEXT: Body: 0B
+; CHECK-NEXT: Body: 4180888080000B
; CHECK-NEXT: - Index: 1
; CHECK-NEXT: Locals: []
-; CHECK-NEXT: Body: 4180888080000B
+; CHECK-NEXT: Body: 4190888080000B
; CHECK-NEXT: - Index: 2
; CHECK-NEXT: Locals: []
-; CHECK-NEXT: Body: 4188888080000B
-; CHECK-NEXT: - Index: 3
-; CHECK-NEXT: Locals: []
; CHECK-NEXT: Body: 0B
; CHECK-NEXT: - Type: DATA
; CHECK-NEXT: Segments:
@@ -43,15 +41,13 @@ entry:
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 1024
-; CHECK-NEXT: Content: '0300000004000000'
+; CHECK-NEXT: Content: 03000000040000002A0000002B000000
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: name
; CHECK-NEXT: FunctionNames:
; CHECK-NEXT: - Index: 0
-; CHECK-NEXT: Name: __wasm_call_ctors
-; CHECK-NEXT: - Index: 1
; CHECK-NEXT: Name: get_start
-; CHECK-NEXT: - Index: 2
+; CHECK-NEXT: - Index: 1
; CHECK-NEXT: Name: get_end
-; CHECK-NEXT: - Index: 3
+; CHECK-NEXT: - Index: 2
; CHECK-NEXT: Name: _start
Modified: lld/trunk/wasm/InputChunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.cpp?rev=365308&r1=365307&r2=365308&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.cpp (original)
+++ lld/trunk/wasm/InputChunks.cpp Mon Jul 8 03:35:08 2019
@@ -107,8 +107,10 @@ void InputChunk::writeTo(uint8_t *Buf) c
for (const WasmRelocation &Rel : Relocations) {
uint8_t *Loc = Buf + Rel.Offset + Off;
uint32_t Value = File->calcNewValue(Rel);
- LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type)
- << " addend=" << Rel.Addend << " index=" << Rel.Index
+ LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type));
+ if (Rel.Type != R_WASM_TYPE_INDEX_LEB)
+ LLVM_DEBUG(dbgs() << " sym=" << File->getSymbols()[Rel.Index]->getName());
+ LLVM_DEBUG(dbgs() << " addend=" << Rel.Addend << " index=" << Rel.Index
<< " value=" << Value << " offset=" << Rel.Offset
<< "\n");
Modified: lld/trunk/wasm/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/SymbolTable.cpp?rev=365308&r1=365307&r2=365308&view=diff
==============================================================================
--- lld/trunk/wasm/SymbolTable.cpp (original)
+++ lld/trunk/wasm/SymbolTable.cpp Mon Jul 8 03:35:08 2019
@@ -228,6 +228,7 @@ DefinedData *SymbolTable::addOptionalDat
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << Name << "\n");
auto *rtn = replaceSymbol<DefinedData>(S, Name, Flags);
rtn->setVirtualAddress(Value);
+ rtn->Referenced = true;
return rtn;
}
Modified: lld/trunk/wasm/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.h?rev=365308&r1=365307&r2=365308&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.h (original)
+++ lld/trunk/wasm/Symbols.h Mon Jul 8 03:35:08 2019
@@ -131,9 +131,10 @@ protected:
uint32_t OutputSymbolIndex = INVALID_INDEX;
uint32_t GOTIndex = INVALID_INDEX;
Kind SymbolKind;
- bool Referenced : 1;
public:
+ bool Referenced : 1;
+
// True if the symbol was used for linking and thus need to be added to the
// output file's symbol table. This is true for all symbols except for
// unreferenced DSO symbols, lazy (archive) symbols, and bitcode symbols that
Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=365308&r1=365307&r2=365308&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Mon Jul 8 03:35:08 2019
@@ -74,7 +74,6 @@ private:
void addSection(OutputSection *Sec);
void addSections();
- void addStartStopSymbols(const InputSegment *Seg);
void createCustomSections();
void createSyntheticSections();
@@ -303,15 +302,15 @@ void Writer::addSection(OutputSection *S
// __stop_<secname> symbols. They are at beginning and end of the section,
// respectively. This is not requested by the ELF standard, but GNU ld and
// gold provide the feature, and used by many programs.
-void Writer::addStartStopSymbols(const InputSegment *Seg) {
- StringRef S = Seg->getName();
- LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << S << "\n");
- if (!isValidCIdentifier(S))
+static void addStartStopSymbols(const OutputSegment *Seg) {
+ StringRef Name = Seg->Name;
+ LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << Name << "\n");
+ if (!isValidCIdentifier(Name))
return;
- uint32_t Start = Seg->OutputSeg->StartVA + Seg->OutputSegmentOffset;
- uint32_t Stop = Start + Seg->getSize();
- Symtab->addOptionalDataSymbol(Saver.save("__start_" + S), Start);
- Symtab->addOptionalDataSymbol(Saver.save("__stop_" + S), Stop);
+ uint32_t Start = Seg->StartVA;
+ uint32_t Stop = Start + Seg->Size;
+ Symtab->addOptionalDataSymbol(Saver.save("__start_" + Name), Start);
+ Symtab->addOptionalDataSymbol(Saver.save("__stop_" + Name), Stop);
}
void Writer::addSections() {
@@ -811,8 +810,7 @@ void Writer::run() {
// Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
// This has to be done after memory layout is performed.
for (const OutputSegment *Seg : Segments)
- for (const InputSegment *S : Seg->InputSegments)
- addStartStopSymbols(S);
+ addStartStopSymbols(Seg);
}
log("-- scanRelocations");
More information about the llvm-commits
mailing list