Skip to content

Commit 6267697

Browse files
committed
[JITLink] Truncate ELF symbol sizes to fit containing block.
LLVM currently emits dubious symbol sizes for aliases. E.g. assembling the following with LLVM top-of-tree... ``` $ cat foo.s <snip> .data .globl base base: .dword 42 .size base, 8 .set alias, base+4 ``` results in both base and alias having symbol size 8, even alias starts at base + 4. This also means that alias extends past the end of the .data section in this example. We should probably teach LLVM not to do this in the future, but as a short-term fix this patch teaches JITLink to simply truncate symbols that would extend past the end of their containing block. rdar://114207607
1 parent 945c269 commit 6267697

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,18 +499,21 @@ template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySymbols() {
499499
TargetFlagsType Flags = makeTargetFlags(Sym);
500500
orc::ExecutorAddrDiff Offset = getRawOffset(Sym, Flags);
501501

502+
// Truncate symbol if it would overflow -- ELF size fields can't be
503+
// trusted.
504+
uint64_t Size =
505+
std::min(static_cast<uint64_t>(Sym.st_size), B->getSize() - Offset);
506+
502507
// In RISCV, temporary symbols (Used to generate dwarf, eh_frame
503508
// sections...) will appear in object code's symbol table, and LLVM does
504509
// not use names on these temporary symbols (RISCV gnu toolchain uses
505510
// names on these temporary symbols). If the symbol is unnamed, add an
506511
// anonymous symbol.
507512
auto &GSym =
508513
Name->empty()
509-
? G->addAnonymousSymbol(*B, Offset, Sym.st_size,
510-
false, false)
511-
: G->addDefinedSymbol(*B, Offset, *Name, Sym.st_size, L,
512-
S, Sym.getType() == ELF::STT_FUNC,
513-
false);
514+
? G->addAnonymousSymbol(*B, Offset, Size, false, false)
515+
: G->addDefinedSymbol(*B, Offset, *Name, Size, L, S,
516+
Sym.getType() == ELF::STT_FUNC, false);
514517

515518
GSym.setTargetFlags(Flags);
516519
setGraphSymbol(SymIndex, GSym);

0 commit comments

Comments
 (0)