-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WebAssembly] Refactor synthetic sections and relocation processing. …
…NFC. Major refactor to better match the structure of the ELF linker. - Split out relocation processing into scanRelocations - Split out synthetic sections into their own classes. Differential Revision: https://reviews.llvm.org/D61811 llvm-svn: 361233
- Loading branch information
Showing
13 changed files
with
1,186 additions
and
931 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//===- Relocations.cpp ----------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Relocations.h" | ||
|
||
#include "InputChunks.h" | ||
#include "SyntheticSections.h" | ||
|
||
using namespace llvm; | ||
using namespace llvm::wasm; | ||
|
||
using namespace lld; | ||
using namespace lld::wasm; | ||
|
||
static bool requiresGOTAccess(const Symbol *Sym) { | ||
return Config->Pic && !Sym->isHidden() && !Sym->isLocal(); | ||
} | ||
|
||
void lld::wasm::scanRelocations(InputChunk *Chunk) { | ||
if (!Chunk->Live) | ||
return; | ||
ObjFile *File = Chunk->File; | ||
ArrayRef<WasmSignature> Types = File->getWasmObj()->types(); | ||
for (const WasmRelocation &Reloc : Chunk->getRelocations()) { | ||
if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) { | ||
// Mark target type as live | ||
File->TypeMap[Reloc.Index] = | ||
Out.TypeSec->registerType(Types[Reloc.Index]); | ||
File->TypeIsUsed[Reloc.Index] = true; | ||
continue; | ||
} | ||
|
||
// Other relocation types all have a corresponding symbol | ||
Symbol *Sym = File->getSymbols()[Reloc.Index]; | ||
|
||
switch (Reloc.Type) { | ||
case R_WASM_TABLE_INDEX_I32: | ||
case R_WASM_TABLE_INDEX_SLEB: | ||
case R_WASM_TABLE_INDEX_REL_SLEB: | ||
if (requiresGOTAccess(Sym)) | ||
break; | ||
Out.ElemSec->addEntry(cast<FunctionSymbol>(Sym)); | ||
break; | ||
case R_WASM_GLOBAL_INDEX_LEB: | ||
if (!isa<GlobalSymbol>(Sym)) | ||
Out.ImportSec->addGOTEntry(Sym); | ||
break; | ||
case R_WASM_MEMORY_ADDR_SLEB: | ||
case R_WASM_MEMORY_ADDR_LEB: | ||
case R_WASM_MEMORY_ADDR_REL_SLEB: | ||
if (!Config->Relocatable && Sym->isUndefined() && !Sym->isWeak()) { | ||
error(toString(File) + ": cannot resolve relocation of type " + | ||
relocTypeToString(Reloc.Type) + | ||
" against undefined (non-weak) data symbol: " + toString(*Sym)); | ||
} | ||
break; | ||
} | ||
|
||
if (Config->Pic) { | ||
switch (Reloc.Type) { | ||
case R_WASM_TABLE_INDEX_SLEB: | ||
case R_WASM_MEMORY_ADDR_SLEB: | ||
case R_WASM_MEMORY_ADDR_LEB: | ||
// Certain relocation types can't be used when building PIC output, | ||
// since they would require absolute symbol addresses at link time. | ||
error(toString(File) + ": relocation " + relocTypeToString(Reloc.Type) + | ||
" cannot be used againt symbol " + toString(*Sym) + | ||
"; recompile with -fPIC"); | ||
break; | ||
case R_WASM_TABLE_INDEX_I32: | ||
case R_WASM_MEMORY_ADDR_I32: | ||
// These relocation types are only present in the data section and | ||
// will be converted into code by `generateRelocationCode`. This code | ||
// requires the symbols to have GOT entires. | ||
if (requiresGOTAccess(Sym)) | ||
Out.ImportSec->addGOTEntry(Sym); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===- Relocations.h -------------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLD_WASM_RELOCATIONS_H | ||
#define LLD_WASM_RELOCATIONS_H | ||
|
||
namespace lld { | ||
namespace wasm { | ||
|
||
class InputChunk; | ||
|
||
void scanRelocations(InputChunk *Chunk); | ||
|
||
} // namespace wasm | ||
} // namespace lld | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.