[ELF] relocateNonAlloc: clean up workaround code

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.
This commit is contained in:
Fangrui Song 2023-12-07 12:43:40 -08:00
parent 7030aab1d7
commit 3fd1d6953d
3 changed files with 13 additions and 19 deletions

View File

@ -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)
@ -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;
}
@ -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)));

View File

@ -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

View File

@ -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: