[lld] r286940 - [ELF] - Better diagnostic for relative relocation to an absolute value error.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 15 00:07:14 PST 2016


Author: grimar
Date: Tue Nov 15 02:07:14 2016
New Revision: 286940

URL: http://llvm.org/viewvc/llvm-project?rev=286940&view=rev
Log:
[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

Added:
    lld/trunk/test/ELF/Inputs/relocation-relative-absolute.s
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/Relocations.cpp
    lld/trunk/ELF/SymbolTable.cpp
    lld/trunk/ELF/SymbolTable.h
    lld/trunk/ELF/Symbols.h
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/relocation-relative-absolute.s

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Nov 15 02:07:14 2016
@@ -477,7 +477,7 @@ SymbolBody *elf::ObjectFile<ELFT>::creat
                                                 /*CanOmitFromDynSym*/ false,
                                                 this)
           ->body();
-    return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
+    return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec, this)->body();
   }
 }
 
@@ -805,11 +805,13 @@ template <class ELFT> void BinaryFile::p
   Sections.push_back(Section);
 
   elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, STT_OBJECT, 0, 0,
-                                   STB_GLOBAL, Section);
+                                   STB_GLOBAL, Section, nullptr);
   elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, STT_OBJECT,
-                                   Data.size(), 0, STB_GLOBAL, Section);
+                                   Data.size(), 0, STB_GLOBAL, Section,
+                                   nullptr);
   elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, STT_OBJECT,
-                                   Data.size(), 0, STB_GLOBAL, nullptr);
+                                   Data.size(), 0, STB_GLOBAL, nullptr,
+                                   nullptr);
 }
 
 static bool isBitcode(MemoryBufferRef MB) {

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Tue Nov 15 02:07:14 2016
@@ -65,7 +65,7 @@ ScriptConfiguration *elf::ScriptConfig;
 template <class ELFT> static void addRegular(SymbolAssignment *Cmd) {
   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
   Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, Visibility, STT_NOTYPE,
-                                            0, 0, STB_GLOBAL, nullptr);
+                                            0, 0, STB_GLOBAL, nullptr, nullptr);
   Cmd->Sym = Sym->body();
 
   // If we have no SECTIONS then we don't have '.' and don't call

Modified: lld/trunk/ELF/Relocations.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.cpp (original)
+++ lld/trunk/ELF/Relocations.cpp Tue Nov 15 02:07:14 2016
@@ -306,7 +306,9 @@ static bool isRelExpr(RelExpr Expr) {
 
 template <class ELFT>
 static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type,
-                                     const SymbolBody &Body) {
+                                     const SymbolBody &Body,
+                                     InputSectionBase<ELFT> &S,
+                                     typename ELFT::uint RelOff) {
   // These expressions always compute a constant
   if (E == R_SIZE || E == R_GOT_FROM_END || E == R_GOT_OFF ||
       E == R_MIPS_GOT_LOCAL_PAGE || E == R_MIPS_GOT_OFF ||
@@ -342,8 +344,9 @@ static bool isStaticLinkTimeConstant(Rel
   if (AbsVal && RelE) {
     if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
       return true;
-    error("relocation " + getRelName(Type) +
-          " cannot refer to absolute symbol " + Body.getName());
+    error(getLocation(S, RelOff) + ": relocation " + getRelName(Type) +
+          " cannot refer to absolute symbol '" + Body.getName() +
+          "' defined in " + getFilename(Body.File));
     return true;
   }
 
@@ -421,7 +424,8 @@ template <class ELFT> static void addCop
 template <class ELFT>
 static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
                           bool IsWrite, RelExpr Expr, uint32_t Type,
-                          const uint8_t *Data) {
+                          const uint8_t *Data, InputSectionBase<ELFT> &S,
+                          typename ELFT::uint RelOff) {
   bool Preemptible = isPreemptible(Body, Type);
   if (Body.isGnuIFunc()) {
     Expr = toPlt(Expr);
@@ -433,7 +437,7 @@ static RelExpr adjustExpr(const elf::Obj
   }
   Expr = Target->getThunkExpr(Expr, Type, File, Body);
 
-  if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body))
+  if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff))
     return Expr;
 
   // This relocation would require the dynamic linker to write a value to read
@@ -645,7 +649,8 @@ static void scanRelocs(InputSectionBase<
 
     RelExpr Expr = Target->getRelExpr(Type, Body);
     bool Preemptible = isPreemptible(Body, Type);
-    Expr = adjustExpr(*File, Body, IsWrite, Expr, Type, Buf + RI.r_offset);
+    Expr = adjustExpr(*File, Body, IsWrite, Expr, Type, Buf + RI.r_offset, C,
+                      RI.r_offset);
     if (HasError)
       continue;
 
@@ -690,7 +695,8 @@ static void scanRelocs(InputSectionBase<
         Expr == R_THUNK_PLT_PC || refersToGotEntry(Expr) ||
         !isPreemptible(Body, Type)) {
       // If the relocation points to something in the file, we can process it.
-      bool Constant = isStaticLinkTimeConstant<ELFT>(Expr, Type, Body);
+      bool Constant =
+          isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, C, RI.r_offset);
 
       // If the output being produced is position independent, the final value
       // is still not known. In that case we still need some help from the

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Tue Nov 15 02:07:14 2016
@@ -127,8 +127,8 @@ template <class ELFT> void SymbolTable<E
 template <class ELFT>
 DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
                                                      uint8_t Visibility) {
-  Symbol *Sym =
-      addRegular(Name, Visibility, STT_NOTYPE, 0, 0, STB_GLOBAL, nullptr);
+  Symbol *Sym = addRegular(Name, Visibility, STT_NOTYPE, 0, 0, STB_GLOBAL,
+                           nullptr, nullptr);
   return cast<DefinedRegular<ELFT>>(Sym->body());
 }
 
@@ -397,25 +397,26 @@ static void reportDuplicate(SymbolBody *
 
 template <typename ELFT>
 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
-                                      InputSectionBase<ELFT> *Section) {
+                                      InputSectionBase<ELFT> *Section,
+                                      InputFile *File) {
   return addRegular(Name, Sym.st_other, Sym.getType(), Sym.st_value,
-                    Sym.st_size, Sym.getBinding(), Section);
+                    Sym.st_size, Sym.getBinding(), Section, File);
 }
 
 template <typename ELFT>
 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther,
                                       uint8_t Type, uintX_t Value, uintX_t Size,
                                       uint8_t Binding,
-                                      InputSectionBase<ELFT> *Section) {
+                                      InputSectionBase<ELFT> *Section,
+                                      InputFile *File) {
   Symbol *S;
   bool WasInserted;
   std::tie(S, WasInserted) = insert(Name, Type, StOther & 3,
-                                    /*CanOmitFromDynSym*/ false,
-                                    Section ? Section->getFile() : nullptr);
+                                    /*CanOmitFromDynSym*/ false, File);
   int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
   if (Cmp > 0)
     replaceBody<DefinedRegular<ELFT>>(S, Name, StOther, Type, Value, Size,
-                                      Section);
+                                      Section, File);
   else if (Cmp == 0)
     reportDuplicate(S->body(), Section, Value);
   return S;

Modified: lld/trunk/ELF/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.h?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.h (original)
+++ lld/trunk/ELF/SymbolTable.h Tue Nov 15 02:07:14 2016
@@ -60,9 +60,9 @@ public:
 
   Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type,
                      uintX_t Value, uintX_t Size, uint8_t Binding,
-                     InputSectionBase<ELFT> *Section);
+                     InputSectionBase<ELFT> *Section, InputFile *File);
   Symbol *addRegular(StringRef Name, const Elf_Sym &Sym,
-                     InputSectionBase<ELFT> *Section);
+                     InputSectionBase<ELFT> *Section, InputFile *File);
 
   Symbol *addSynthetic(StringRef N, OutputSectionBase *Section, uintX_t Value,
                        uint8_t StOther);

Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Tue Nov 15 02:07:14 2016
@@ -194,11 +194,6 @@ public:
     this->File = File;
   }
 
-  DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, uintX_t Value,
-                 uintX_t Size, InputSectionBase<ELFT> *Section)
-      : DefinedRegular(Name, StOther, Type, Value, Size, Section,
-                       Section ? Section->getFile() : nullptr) {}
-
   DefinedRegular(StringRef Name, uint8_t StOther, uint8_t Type, BitcodeFile *F)
       : DefinedRegular(Name, StOther, Type, 0, 0, NullInputSection, F) {}
 

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Nov 15 02:07:14 2016
@@ -568,7 +568,7 @@ static Symbol *addRegular(StringRef Name
   typename ELFT::Sym LocalHidden = {};
   LocalHidden.setBindingAndType(STB_LOCAL, STT_NOTYPE);
   LocalHidden.setVisibility(STV_HIDDEN);
-  Symbol *S = Symtab<ELFT>::X->addRegular(Name, LocalHidden, IS);
+  Symbol *S = Symtab<ELFT>::X->addRegular(Name, LocalHidden, IS, nullptr);
   cast<DefinedRegular<ELFT>>(S->body())->Value = Value;
   return S;
 }

Added: lld/trunk/test/ELF/Inputs/relocation-relative-absolute.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/relocation-relative-absolute.s?rev=286940&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/relocation-relative-absolute.s (added)
+++ lld/trunk/test/ELF/Inputs/relocation-relative-absolute.s Tue Nov 15 02:07:14 2016
@@ -0,0 +1,2 @@
+.globl answer
+answer = 42

Modified: lld/trunk/test/ELF/relocation-relative-absolute.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/relocation-relative-absolute.s?rev=286940&r1=286939&r2=286940&view=diff
==============================================================================
--- lld/trunk/test/ELF/relocation-relative-absolute.s (original)
+++ lld/trunk/test/ELF/relocation-relative-absolute.s Tue Nov 15 02:07:14 2016
@@ -1,12 +1,12 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t -pie 2>&1 | FileCheck %s
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %tinput1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux \
+# RUN:   %S/Inputs/relocation-relative-absolute.s -o %tinput2.o
+# RUN: not ld.lld %tinput1.o %tinput2.o -o %t -pie 2>&1 | FileCheck %s
 
 .globl _start
 _start:
 
-# CHECK: relocation R_X86_64_PLT32 cannot refer to absolute symbol answer
-call answer at PLT
+# CHECK: {{.*}}input1.o (.text+0x1): relocation R_X86_64_PLT32 cannot refer to absolute symbol 'answer' defined in {{.*}}input2.o
 
-.globl answer
-answer = 42
+call answer at PLT




More information about the llvm-commits mailing list