Index: lld/trunk/ELF/OutputSections.cpp =================================================================== --- lld/trunk/ELF/OutputSections.cpp +++ lld/trunk/ELF/OutputSections.cpp @@ -1496,7 +1496,7 @@ SymbolTableSection::getOutputSection(SymbolBody *Sym) { switch (Sym->kind()) { case SymbolBody::DefinedSyntheticKind: - return &cast>(Sym)->Section; + return cast>(Sym)->Section; case SymbolBody::DefinedRegularKind: { auto &D = cast>(*Sym); if (D.Section) Index: lld/trunk/ELF/SymbolTable.h =================================================================== --- lld/trunk/ELF/SymbolTable.h +++ lld/trunk/ELF/SymbolTable.h @@ -64,7 +64,7 @@ Symbol *addRegular(StringRef Name, const Elf_Sym &Sym, InputSectionBase *Section); Symbol *addRegular(StringRef Name, uint8_t Binding, uint8_t StOther); - Symbol *addSynthetic(StringRef N, OutputSectionBase &Section, + Symbol *addSynthetic(StringRef N, OutputSectionBase *Section, uintX_t Value); void addShared(SharedFile *F, StringRef Name, const Elf_Sym &Sym, const typename ELFT::Verdef *Verdef); Index: lld/trunk/ELF/SymbolTable.cpp =================================================================== --- lld/trunk/ELF/SymbolTable.cpp +++ lld/trunk/ELF/SymbolTable.cpp @@ -388,7 +388,7 @@ template Symbol *SymbolTable::addSynthetic(StringRef N, - OutputSectionBase &Section, + OutputSectionBase *Section, uintX_t Value) { Symbol *S; bool WasInserted; Index: lld/trunk/ELF/Symbols.h =================================================================== --- lld/trunk/ELF/Symbols.h +++ lld/trunk/ELF/Symbols.h @@ -237,11 +237,12 @@ // The difference from the regular symbol is that DefinedSynthetic symbols // don't belong to any input files or sections. Thus, its constructor // takes an output section to calculate output VA, etc. +// If Section is null, this symbol is relative to the image base. template class DefinedSynthetic : public Defined { public: typedef typename ELFT::uint uintX_t; DefinedSynthetic(StringRef N, uintX_t Value, - OutputSectionBase &Section); + OutputSectionBase *Section); static bool classof(const SymbolBody *S) { return S->kind() == SymbolBody::DefinedSyntheticKind; @@ -252,7 +253,7 @@ static const uintX_t SectionEnd = uintX_t(-1); uintX_t Value; - const OutputSectionBase &Section; + const OutputSectionBase *Section; }; class Undefined : public SymbolBody { Index: lld/trunk/ELF/Symbols.cpp =================================================================== --- lld/trunk/ELF/Symbols.cpp +++ lld/trunk/ELF/Symbols.cpp @@ -36,9 +36,12 @@ switch (Body.kind()) { case SymbolBody::DefinedSyntheticKind: { auto &D = cast>(Body); + const OutputSectionBase *Sec = D.Section; + if (!Sec) + return D.Value; if (D.Value == DefinedSynthetic::SectionEnd) - return D.Section.getVA() + D.Section.getSize(); - return D.Section.getVA() + D.Value; + return Sec->getVA() + Sec->getSize(); + return Sec->getVA() + D.Value; } case SymbolBody::DefinedRegularKind: { auto &D = cast>(Body); @@ -208,7 +211,7 @@ template DefinedSynthetic::DefinedSynthetic(StringRef N, uintX_t Value, - OutputSectionBase &Section) + OutputSectionBase *Section) : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */), Value(Value), Section(Section) {} Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -454,6 +454,20 @@ if (!AbsVal && RelE) return true; + // Relative relocation to an absolute value. This is normally unrepresentable, + // but if the relocation refers to a weak undefined symbol, we allow it to + // resolve to the image base. This is a little strange, but it allows us to + // link function calls to such symbols. Normally such a call will be guarded + // with a comparison, which will load a zero from the GOT. + if (AbsVal && RelE) { + if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) + return true; + StringRef S = getELFRelocationTypeName(Config->EMachine, Type); + error("relocation " + S + " cannot refer to absolute symbol " + + Body.getName()); + return true; + } + return Target->usesOnlyLowPageBits(Type); } @@ -1067,7 +1081,7 @@ template static Symbol *addOptionalSynthetic(SymbolTable &Table, StringRef Name, - OutputSectionBase &Sec, + OutputSectionBase *Sec, typename ELFT::uint Val) { if (!Table.find(Name)) return nullptr; @@ -1084,10 +1098,10 @@ if (isOutputDynamic() || !Out::RelaPlt) return; StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start"; - addOptionalSynthetic(Symtab, S, *Out::RelaPlt, 0); + addOptionalSynthetic(Symtab, S, Out::RelaPlt, 0); S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end"; - addOptionalSynthetic(Symtab, S, *Out::RelaPlt, + addOptionalSynthetic(Symtab, S, Out::RelaPlt, DefinedSynthetic::SectionEnd); } @@ -1189,18 +1203,18 @@ // so that it points to an absolute address which is relative to GOT. // See "Global Data Symbols" in Chapter 6 in the following document: // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf - Symtab.addSynthetic("_gp", *Out::Got, MipsGPOffset); + Symtab.addSynthetic("_gp", Out::Got, MipsGPOffset); // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between // start of function and 'gp' pointer into GOT. ElfSym::MipsGpDisp = - addOptionalSynthetic(Symtab, "_gp_disp", *Out::Got, MipsGPOffset) + addOptionalSynthetic(Symtab, "_gp_disp", Out::Got, MipsGPOffset) ->body(); // The __gnu_local_gp is a magic symbol equal to the current value of 'gp' // pointer. This symbol is used in the code generated by .cpload pseudo-op // in case of using -mno-shared option. // https://sourceware.org/ml/binutils/2004-12/msg00094.html - addOptionalSynthetic(Symtab, "__gnu_local_gp", *Out::Got, + addOptionalSynthetic(Symtab, "__gnu_local_gp", Out::Got, MipsGPOffset); } @@ -1327,7 +1341,7 @@ // Even the author of gold doesn't remember why gold behaves that way. // https://sourceware.org/ml/binutils/2002-03/msg00360.html if (isOutputDynamic()) - Symtab.addSynthetic("_DYNAMIC", *Out::Dynamic, 0); + Symtab.addSynthetic("_DYNAMIC", Out::Dynamic, 0); // Define __rel[a]_iplt_{start,end} symbols if needed. addRelIpltSymbols(); @@ -1487,11 +1501,13 @@ auto Define = [&](StringRef Start, StringRef End, OutputSectionBase *OS) { if (OS) { - this->Symtab.addSynthetic(Start, *OS, 0); - this->Symtab.addSynthetic(End, *OS, DefinedSynthetic::SectionEnd); + this->Symtab.addSynthetic(Start, OS, 0); + this->Symtab.addSynthetic(End, OS, DefinedSynthetic::SectionEnd); } else { - this->Symtab.addIgnored(Start); - this->Symtab.addIgnored(End); + addOptionalSynthetic(this->Symtab, Start, + (OutputSectionBase *)nullptr, 0); + addOptionalSynthetic(this->Symtab, End, + (OutputSectionBase *)nullptr, 0); } }; @@ -1518,10 +1534,10 @@ StringRef Stop = Saver.save("__stop_" + S); if (SymbolBody *B = Symtab.find(Start)) if (B->isUndefined()) - Symtab.addSynthetic(Start, *Sec, 0); + Symtab.addSynthetic(Start, Sec, 0); if (SymbolBody *B = Symtab.find(Stop)) if (B->isUndefined()) - Symtab.addSynthetic(Stop, *Sec, DefinedSynthetic::SectionEnd); + Symtab.addSynthetic(Stop, Sec, DefinedSynthetic::SectionEnd); } template static bool needsPtLoad(OutputSectionBase *Sec) { Index: lld/trunk/test/ELF/relocation-relative-absolute.s =================================================================== --- lld/trunk/test/ELF/relocation-relative-absolute.s +++ lld/trunk/test/ELF/relocation-relative-absolute.s @@ -0,0 +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 + +.globl _start +_start: + +# CHECK: relocation R_X86_64_PLT32 cannot refer to absolute symbol answer +call answer@PLT + +.globl answer +answer = 42 Index: lld/trunk/test/ELF/relocation-relative-synthetic.s =================================================================== --- lld/trunk/test/ELF/relocation-relative-synthetic.s +++ lld/trunk/test/ELF/relocation-relative-synthetic.s @@ -0,0 +1,11 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: ld.lld %t.o -o %t -pie +# RUN: llvm-readobj -dyn-relocations %t | FileCheck %s + +# CHECK: Dynamic Relocations { +# CHECK-NEXT: } + +.globl _start +_start: +call __init_array_start@PLT Index: lld/trunk/test/ELF/relocation-relative-weak.s =================================================================== --- lld/trunk/test/ELF/relocation-relative-weak.s +++ lld/trunk/test/ELF/relocation-relative-weak.s @@ -0,0 +1,14 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: ld.lld %t.o -o %t -pie +# RUN: llvm-readobj -dyn-relocations %t | FileCheck %s + +# CHECK: Dynamic Relocations { +# CHECK-NEXT: } + +.globl _start +_start: + +.globl w +.weak w +call w@PLT