Skip to content

Commit

Permalink
[ELF] relocateNonAlloc: clean up workaround code
Browse files Browse the repository at this point in the history
relocateNonAlloc is costly for .debug_* section relocating. We don't
want to burn CPU cycles on other targets' workarounds.

Remove a temporary workaround for Linux objtool after a proper fix
https://git.kernel.org/linus/b8ec60e1186cdcfce41e7db4c827cb107e459002

Move the R_386_GOTPC workaround for GCC<8 beside the R_PC workaround.
  • Loading branch information
MaskRay committed Dec 7, 2023
1 parent 7030aab commit 3fd1d69
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
26 changes: 9 additions & 17 deletions lld/ELF/InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,16 +910,8 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {

for (size_t i = 0, relsSize = rels.size(); i != relsSize; ++i) {
const RelTy &rel = rels[i];
RelType type = rel.getType(config->isMips64EL);

// GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
// against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed
// in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we
// need to keep this bug-compatible code for a while.
if (emachine == EM_386 && type == R_386_GOTPC)
continue;

uint64_t offset = rel.r_offset;
const RelType type = rel.getType(config->isMips64EL);
const uint64_t offset = rel.r_offset;
uint8_t *bufLoc = buf + offset;
int64_t addend = getAddend<ELFT>(rel);
if (!RelTy::IsRela)
Expand Down Expand Up @@ -1016,8 +1008,8 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
std::string msg = getLocation(offset) + ": has non-ABS relocation " +
toString(type) + " against symbol '" + toString(sym) +
"'";
if (expr != R_PC) {
error(msg);
if (expr != R_PC && !(emachine == EM_386 && type == R_386_GOTPC)) {
errorOrWarn(msg);
return;
}

Expand All @@ -1029,11 +1021,11 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
// address 0. For bug-compatibility, we accept them with warnings. We
// know Steel Bank Common Lisp as of 2018 have this bug.
//
// RELA -r stopped earlier and does not get the warning. Suppress the
// warning for REL -r as well
// (https://github.com/ClangBuiltLinux/linux/issues/1937).
if (RelTy::IsRela || !config->relocatable)
warn(msg);
// GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
// against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed in
// 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we need to
// keep this bug-compatible code for a while.
warn(msg);
target.relocateNoSym(
bufLoc, type,
SignExtend64<bits>(sym.getVA(addend - offset - outSecOff)));
Expand Down
3 changes: 2 additions & 1 deletion lld/test/ELF/i386-debug-noabs.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# REQUIRES: x86

# RUN: yaml2obj %s -o %t.o
# RUN: ld.lld %t.o -o /dev/null --entry 0 --fatal-warnings
# RUN: ld.lld %t.o -o /dev/null --entry 0 2>&1 | FileCheck %s
# CHECK: warning: {{.*}}:(.debug_info+0x41f): has non-ABS relocation R_386_GOTPC against symbol '_GLOBAL_OFFSET_TABLE_'

## This is for https://bugs.llvm.org//show_bug.cgi?id=34852. GCC 8.0 or
## earlier have a bug which creates non-absolute R_386_GOTPC relocations
Expand Down
3 changes: 2 additions & 1 deletion lld/test/ELF/non-abs-reloc.s
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// DISASM-NEXT: 6: call{{.}} 0x5

/// There is currently no error for -r. See also https://github.com/ClangBuiltLinux/linux/issues/1937
// RUN: ld.lld -T lds -r a.o -o /dev/null --fatal-warnings
// RUN: ld.lld -T lds -r a.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=REL-R --implicit-check-not=warning:
// REL-R: warning: {{.*}}:(.nonalloc1+0xa): has non-ABS relocation R_386_PC32 against symbol ''

// RUN: llvm-mc -filetype=obj -triple=x86_64 asm -o b.o
// RUN: ld.lld -T lds b.o -o b 2>&1 | FileCheck %s --check-prefix=CHECK2 --implicit-check-not=warning:
Expand Down

0 comments on commit 3fd1d69

Please sign in to comment.