Skip to content

Commit a59b17c

Browse files
authored
[lld][WebAssembly] Do not emit relocs against dead symbols (#129346)
When emitting relocs with linked output (i.e. --emit-relocs) skip relocs against dead symbols (which do not appear in the output) and do not emit them.
1 parent b44fbde commit a59b17c

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

lld/test/wasm/emit-relocs.s

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ foo:
2727
.int32 0
2828
.size foo, 4
2929

30+
.section .debug_info,"",@
31+
.p2align 2
32+
.int32 unused_function
33+
.int32 _start
34+
.int32 0
35+
3036
# CHECK: - Type: CODE
3137
# CHECK-NEXT: Relocations:
3238
# CHECK-NEXT: - Type: R_WASM_FUNCTION_INDEX_LEB
@@ -42,6 +48,15 @@ foo:
4248
# CHECK-NEXT: Value: 1024
4349
# CHECK-NEXT: Content: '00000000'
4450

51+
# There should be a single relocation in this section (just the live symbol)
52+
# CHECK-NEXT: - Type: CUSTOM
53+
# CHECK-NEXT: Relocations:
54+
# CHECK-NEXT: - Type: R_WASM_FUNCTION_OFFSET_I32
55+
# CHECK-NEXT: Index: 0
56+
# CHECK-NEXT: Offset: 0x4
57+
# CHECK-NEXT: Name: .debug_info
58+
# CHECK-NEXT: Payload: FFFFFFFF0200000000000000
59+
4560
# CHECK: - Type: CUSTOM
4661
# CHECK-NEXT: Name: linking
4762
# CHECK-NEXT: Version: 2

lld/wasm/InputChunks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "lld/Common/LLVM.h"
1515
#include "llvm/Support/LEB128.h"
1616
#include "llvm/Support/xxhash.h"
17+
#include <algorithm>
1718

1819
#define DEBUG_TYPE "lld"
1920

@@ -167,6 +168,17 @@ void InputChunk::relocate(uint8_t *buf) const {
167168
}
168169
}
169170

171+
static bool relocIsLive(const WasmRelocation &rel, ObjFile *file) {
172+
return rel.Type == R_WASM_TYPE_INDEX_LEB ||
173+
file->getSymbol(rel.Index)->isLive();
174+
}
175+
176+
size_t InputChunk::getNumLiveRelocations() const {
177+
return std::count_if(
178+
relocations.begin(), relocations.end(),
179+
[this](const WasmRelocation &rel) { return relocIsLive(rel, file); });
180+
}
181+
170182
// Copy relocation entries to a given output stream.
171183
// This function is used only when a user passes "-r". For a regular link,
172184
// we consume relocations instead of copying them to an output file.
@@ -179,6 +191,8 @@ void InputChunk::writeRelocations(raw_ostream &os) const {
179191
<< " offset=" << Twine(off) << "\n");
180192

181193
for (const WasmRelocation &rel : relocations) {
194+
if (!relocIsLive(rel, file))
195+
continue;
182196
writeUleb128(os, rel.Type, "reloc type");
183197
writeUleb128(os, rel.Offset + off, "reloc offset");
184198
writeUleb128(os, file->calcNewIndex(rel), "reloc index");

lld/wasm/InputChunks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class InputChunk {
7777
uint32_t getInputSectionOffset() const { return inputSectionOffset; }
7878

7979
size_t getNumRelocations() const { return relocations.size(); }
80+
size_t getNumLiveRelocations() const;
8081
void writeRelocations(llvm::raw_ostream &os) const;
8182
bool generateRelocationCode(raw_ostream &os) const;
8283

lld/wasm/OutputSections.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void CustomSection::writeTo(uint8_t *buf) {
274274
uint32_t CustomSection::getNumRelocations() const {
275275
uint32_t count = 0;
276276
for (const InputChunk *inputSect : inputSections)
277-
count += inputSect->getNumRelocations();
277+
count += inputSect->getNumLiveRelocations();
278278
return count;
279279
}
280280

lld/wasm/OutputSections.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class OutputSection {
4141
virtual void writeTo(uint8_t *buf) = 0;
4242
virtual void finalizeContents() = 0;
4343
virtual uint32_t getNumRelocations() const { return 0; }
44+
virtual uint32_t getNumLiveRelocations() const { return getNumRelocations(); }
4445
virtual void writeRelocations(raw_ostream &os) const {}
4546

4647
std::string header;

lld/wasm/Symbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void Symbol::markLive() {
183183
}
184184

185185
uint32_t Symbol::getOutputSymbolIndex() const {
186-
assert(outputSymbolIndex != INVALID_INDEX);
186+
assert(outputSymbolIndex != INVALID_INDEX || !isLive());
187187
return outputSymbolIndex;
188188
}
189189

0 commit comments

Comments
 (0)