Skip to content

Commit 0b13ca2

Browse files
committed
[WebAssembly] Refactor relocation processing. NFC.
This is the remaining NFC part of https://reviews.llvm.org/D61539 which was reverted. Differential Revision: https://reviews.llvm.org/D61800 llvm-svn: 360598
1 parent eadbde3 commit 0b13ca2

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

lld/wasm/Writer.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -1152,35 +1152,38 @@ void Writer::processRelocations(InputChunk *Chunk) {
11521152
ObjFile *File = Chunk->File;
11531153
ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
11541154
for (const WasmRelocation &Reloc : Chunk->getRelocations()) {
1155+
if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) {
1156+
// Mark target type as live
1157+
File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
1158+
File->TypeIsUsed[Reloc.Index] = true;
1159+
continue;
1160+
}
1161+
1162+
// Other relocation types all have a corresponding symbol
1163+
auto *Sym = File->getSymbols()[Reloc.Index];
11551164
switch (Reloc.Type) {
11561165
case R_WASM_TABLE_INDEX_I32:
11571166
case R_WASM_TABLE_INDEX_SLEB:
11581167
case R_WASM_TABLE_INDEX_REL_SLEB: {
1159-
FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index);
1160-
if (Sym->hasTableIndex() || !Sym->hasFunctionIndex() || requiresGOTAccess(Sym))
1168+
auto *F = cast<FunctionSymbol>(Sym);
1169+
if (F->hasTableIndex() || !F->hasFunctionIndex() || requiresGOTAccess(F))
11611170
break;
1162-
Sym->setTableIndex(TableBase + IndirectFunctions.size());
1163-
IndirectFunctions.emplace_back(Sym);
1171+
F->setTableIndex(TableBase + IndirectFunctions.size());
1172+
IndirectFunctions.emplace_back(F);
11641173
break;
11651174
}
11661175
case R_WASM_TYPE_INDEX_LEB:
1167-
// Mark target type as live
1168-
File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
1169-
File->TypeIsUsed[Reloc.Index] = true;
11701176
break;
1171-
case R_WASM_GLOBAL_INDEX_LEB: {
1172-
auto* Sym = File->getSymbols()[Reloc.Index];
1177+
case R_WASM_GLOBAL_INDEX_LEB:
11731178
if (!isa<GlobalSymbol>(Sym) && !Sym->isInGOT()) {
11741179
Sym->setGOTIndex(NumImportedGlobals++);
11751180
GOTSymbols.push_back(Sym);
11761181
}
11771182
break;
1178-
}
11791183
case R_WASM_MEMORY_ADDR_SLEB:
11801184
case R_WASM_MEMORY_ADDR_LEB:
1181-
case R_WASM_MEMORY_ADDR_REL_SLEB: {
1185+
case R_WASM_MEMORY_ADDR_REL_SLEB:
11821186
if (!Config->Relocatable) {
1183-
auto* Sym = File->getSymbols()[Reloc.Index];
11841187
if (Sym->isUndefined() && !Sym->isWeak()) {
11851188
error(toString(File) + ": cannot resolve relocation of type " +
11861189
relocTypeToString(Reloc.Type) +
@@ -1189,34 +1192,29 @@ void Writer::processRelocations(InputChunk *Chunk) {
11891192
}
11901193
break;
11911194
}
1192-
}
11931195

11941196
if (Config->Pic) {
11951197
switch (Reloc.Type) {
11961198
case R_WASM_TABLE_INDEX_SLEB:
11971199
case R_WASM_MEMORY_ADDR_SLEB:
1198-
case R_WASM_MEMORY_ADDR_LEB: {
1200+
case R_WASM_MEMORY_ADDR_LEB:
11991201
// Certain relocation types can't be used when building PIC output, since
12001202
// they would require absolute symbol addresses at link time.
1201-
Symbol *Sym = File->getSymbols()[Reloc.Index];
12021203
error(toString(File) + ": relocation " +
12031204
relocTypeToString(Reloc.Type) + " cannot be used againt symbol " +
12041205
toString(*Sym) + "; recompile with -fPIC");
12051206
break;
1206-
}
12071207
case R_WASM_TABLE_INDEX_I32:
1208-
case R_WASM_MEMORY_ADDR_I32: {
1208+
case R_WASM_MEMORY_ADDR_I32:
12091209
// These relocation types are only present in the data section and
12101210
// will be converted into code by `generateRelocationCode`. This code
12111211
// requires the symbols to have GOT entires.
1212-
auto* Sym = File->getSymbols()[Reloc.Index];
12131212
if (requiresGOTAccess(Sym) && !Sym->isInGOT()) {
12141213
Sym->setGOTIndex(NumImportedGlobals++);
12151214
GOTSymbols.push_back(Sym);
12161215
}
12171216
break;
12181217
}
1219-
}
12201218
}
12211219
}
12221220
}

0 commit comments

Comments
 (0)