Skip to content

Commit 463984d

Browse files
author
George Rimar
committedNov 15, 2016
[ELF] - Better diagnostic for relative relocation to an absolute value error.
Patch adds a filename to that error message. I faced next error when debugged one of FreeBSD port: error: relocation R_X86_64_PLT32 cannot refer to absolute symbol __tls_get_addr error message was poor and this patch improves it to show the locations of symbol declaration and using. Differential revision: https://reviews.llvm.org/D26508 llvm-svn: 286940
1 parent 308752e commit 463984d

9 files changed

+40
-34
lines changed
 

‎lld/ELF/InputFiles.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
477477
/*CanOmitFromDynSym*/ false,
478478
this)
479479
->body();
480-
return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
480+
return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec, this)->body();
481481
}
482482
}
483483

@@ -805,11 +805,13 @@ template <class ELFT> void BinaryFile::parse() {
805805
Sections.push_back(Section);
806806

807807
elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, STT_OBJECT, 0, 0,
808-
STB_GLOBAL, Section);
808+
STB_GLOBAL, Section, nullptr);
809809
elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, STT_OBJECT,
810-
Data.size(), 0, STB_GLOBAL, Section);
810+
Data.size(), 0, STB_GLOBAL, Section,
811+
nullptr);
811812
elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, STT_OBJECT,
812-
Data.size(), 0, STB_GLOBAL, nullptr);
813+
Data.size(), 0, STB_GLOBAL, nullptr,
814+
nullptr);
813815
}
814816

815817
static bool isBitcode(MemoryBufferRef MB) {

‎lld/ELF/LinkerScript.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ScriptConfiguration *elf::ScriptConfig;
6565
template <class ELFT> static void addRegular(SymbolAssignment *Cmd) {
6666
uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
6767
Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, Visibility, STT_NOTYPE,
68-
0, 0, STB_GLOBAL, nullptr);
68+
0, 0, STB_GLOBAL, nullptr, nullptr);
6969
Cmd->Sym = Sym->body();
7070

7171
// If we have no SECTIONS then we don't have '.' and don't call

‎lld/ELF/Relocations.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ static bool isRelExpr(RelExpr Expr) {
306306

307307
template <class ELFT>
308308
static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
309-
const SymbolBody &Body) {
309+
const SymbolBody &Body,
310+
InputSectionBase<ELFT> &S,
311+
typename ELFT::uint RelOff) {
310312
// These expressions always compute a constant
311313
if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF ||
312314
E == R_MIPS_GOT_LOCAL_PAGE || E == R_MIPS_GOT_OFF ||
@@ -342,8 +344,9 @@ static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
342344
if (AbsVal && RelE) {
343345
if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
344346
return true;
345-
error("relocation " + getRelName(Type) +
346-
" cannot refer to absolute symbol " + Body.getName());
347+
error(getLocation(S, RelOff) + ": relocation " + getRelName(Type) +
348+
" cannot refer to absolute symbol '" + Body.getName() +
349+
"' defined in " + getFilename(Body.File));
347350
return true;
348351
}
349352

@@ -421,7 +424,8 @@ template <class ELFT> static void addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
421424
template <class ELFT>
422425
static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
423426
bool IsWrite, RelExpr Expr, uint32_t Type,
424-
const uint8_t *Data) {
427+
const uint8_t *Data, InputSectionBase<ELFT> &S,
428+
typename ELFT::uint RelOff) {
425429
bool Preemptible = isPreemptible(Body, Type);
426430
if (Body.isGnuIFunc()) {
427431
Expr = toPlt(Expr);
@@ -433,7 +437,7 @@ static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
433437
}
434438
Expr = Target->getThunkExpr(Expr, Type, File, Body);
435439

436-
if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body))
440+
if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff))
437441
return Expr;
438442

439443
// This relocation would require the dynamic linker to write a value to read
@@ -645,7 +649,8 @@ static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
645649

646650
RelExpr Expr = Target->getRelExpr(Type, Body);
647651
bool Preemptible = isPreemptible(Body, Type);
648-
Expr = adjustExpr(*File, Body, IsWrite, Expr, Type, Buf + RI.r_offset);
652+
Expr = adjustExpr(*File, Body, IsWrite, Expr, Type, Buf + RI.r_offset, C,
653+
RI.r_offset);
649654
if (HasError)
650655
continue;
651656

@@ -690,7 +695,8 @@ static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
690695
Expr == R_THUNK_PLT_PC || refersToGotEntry(Expr) ||
691696
!isPreemptible(Body, Type)) {
692697
// If the relocation points to something in the file, we can process it.
693-
bool Constant = isStaticLinkTimeConstant<ELFT>(Expr, Type, Body);
698+
bool Constant =
699+
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, C, RI.r_offset);
694700

695701
// If the output being produced is position independent, the final value
696702
// is still not known. In that case we still need some help from the

‎lld/ELF/SymbolTable.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
127127
template <class ELFT>
128128
DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
129129
uint8_t Visibility) {
130-
Symbol *Sym =
131-
addRegular(Name, Visibility, STT_NOTYPE, 0, 0, STB_GLOBAL, nullptr);
130+
Symbol *Sym = addRegular(Name, Visibility, STT_NOTYPE, 0, 0, STB_GLOBAL,
131+
nullptr, nullptr);
132132
return cast<DefinedRegular<ELFT>>(Sym->body());
133133
}
134134

@@ -397,25 +397,26 @@ static void reportDuplicate(SymbolBody *Existing,
397397

398398
template <typename ELFT>
399399
Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
400-
InputSectionBase<ELFT> *Section) {
400+
InputSectionBase<ELFT> *Section,
401+
InputFile *File) {
401402
return addRegular(Name, Sym.st_other, Sym.getType(), Sym.st_value,
402-
Sym.st_size, Sym.getBinding(), Section);
403+
Sym.st_size, Sym.getBinding(), Section, File);
403404
}
404405

405406
template <typename ELFT>
406407
Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
407408
uint8_t Type, uintX_t Value, uintX_t Size,
408409
uint8_t Binding,
409-
InputSectionBase<ELFT> *Section) {
410+
InputSectionBase<ELFT> *Section,
411+
InputFile *File) {
410412
Symbol *S;
411413
bool WasInserted;
412414
std::tie(S, WasInserted) = insert(Name, Type, StOther & 3,
413-
/*CanOmitFromDynSym*/ false,
414-
Section ? Section->getFile() : nullptr);
415+
/*CanOmitFromDynSym*/ false, File);
415416
int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
416417
if (Cmp > 0)
417418
replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, Value, Size,
418-
Section);
419+
Section, File);
419420
else if (Cmp == 0)
420421
reportDuplicate(S->body(), Section, Value);
421422
return S;

‎lld/ELF/SymbolTable.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ template <class ELFT> class SymbolTable {
6060

6161
Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
6262
uintX_t Value, uintX_t Size, uint8_t Binding,
63-
InputSectionBase<ELFT> *Section);
63+
InputSectionBase<ELFT> *Section, InputFile *File);
6464
Symbol *addRegular(StringRef Name, const Elf_Sym &Sym,
65-
InputSectionBase<ELFT> *Section);
65+
InputSectionBase<ELFT> *Section, InputFile *File);
6666

6767
Symbol *addSynthetic(StringRef N, OutputSectionBase *Section, uintX_t Value,
6868
uint8_t StOther);

‎lld/ELF/Symbols.h

-5
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,6 @@ template <class ELFT> class DefinedRegular : public Defined {
194194
this->File = File;
195195
}
196196

197-
DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, uintX_t Value,
198-
uintX_t Size, InputSectionBase<ELFT> *Section)
199-
: DefinedRegular(Name, StOther, Type, Value, Size, Section,
200-
Section ? Section->getFile() : nullptr) {}
201-
202197
DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, BitcodeFile *F)
203198
: DefinedRegular(Name, StOther, Type, 0, 0, NullInputSection, F) {}
204199

‎lld/ELF/Writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ static Symbol *addRegular(StringRef Name, InputSectionBase<ELFT> *IS,
568568
typename ELFT::Sym LocalHidden = {};
569569
LocalHidden.setBindingAndType(STB_LOCAL, STT_NOTYPE);
570570
LocalHidden.setVisibility(STV_HIDDEN);
571-
Symbol *S = Symtab<ELFT>::X->addRegular(Name, LocalHidden, IS);
571+
Symbol *S = Symtab<ELFT>::X->addRegular(Name, LocalHidden, IS, nullptr);
572572
cast<DefinedRegular<ELFT>>(S->body())->Value = Value;
573573
return S;
574574
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.globl answer
2+
answer = 42
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# REQUIRES: x86
2-
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
3-
# RUN: not ld.lld %t.o -o %t -pie 2>&1 | FileCheck %s
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %tinput1.o
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux \
4+
# RUN: %S/Inputs/relocation-relative-absolute.s -o %tinput2.o
5+
# RUN: not ld.lld %tinput1.o %tinput2.o -o %t -pie 2>&1 | FileCheck %s
46

57
.globl _start
68
_start:
79

8-
# CHECK: relocation R_X86_64_PLT32 cannot refer to absolute symbol answer
9-
call answer@PLT
10+
# CHECK: {{.*}}input1.o (.text+0x1): relocation R_X86_64_PLT32 cannot refer to absolute symbol 'answer' defined in {{.*}}input2.o
1011

11-
.globl answer
12-
answer = 42
12+
call answer@PLT

0 commit comments

Comments
 (0)
Please sign in to comment.