From faac567e681142b5f3fb518db9d6e9021fef6684 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 15 Apr 2016 22:39:27 +0000 Subject: [PATCH] Revert r266457: "[ELF] - Implemented basic location counter support." This reverts commit r266457 as it breaks "hello world" both on Linux and FreeBSD. llvm-svn: 266485 --- lld/ELF/LinkerScript.cpp | 131 +--------------------------- lld/ELF/LinkerScript.h | 22 ----- lld/ELF/Writer.cpp | 13 +-- lld/test/ELF/end.s | 8 +- lld/test/ELF/linkerscript-locationcounter.s | 74 ---------------- lld/test/ELF/linkerscript-sections-keep.s | 20 ++--- lld/test/ELF/linkerscript-va.s | 24 ----- lld/test/ELF/wildcards.s | 16 ++-- 8 files changed, 28 insertions(+), 280 deletions(-) delete mode 100644 lld/test/ELF/linkerscript-locationcounter.s delete mode 100644 lld/test/ELF/linkerscript-va.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 0b49c94..96afdb9 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -17,55 +17,20 @@ #include "Config.h" #include "Driver.h" #include "InputSection.h" -#include "OutputSections.h" #include "ScriptParser.h" #include "SymbolTable.h" -#include "llvm/Support/ELF.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/StringSaver.h" using namespace llvm; -using namespace llvm::ELF; using namespace llvm::object; using namespace lld; using namespace lld::elf; LinkerScript *elf::Script; -static uint64_t getInteger(StringRef S) { - uint64_t V; - if (S.getAsInteger(0, V)) { - error("malformed number: " + S); - return 0; - } - return V; -} - -// Evaluates the expression given by list of tokens. -uint64_t LinkerScript::evaluate(std::vector &Tokens, - uint64_t LocCounter) { - uint64_t Result = 0; - for (size_t I = 0, E = Tokens.size(); I < E; ++I) { - // Each second token should be '+' as this is the - // only operator we support now. - if (I % 2 == 1) { - if (Tokens[I] == "+") - continue; - error("error in location counter expression"); - return 0; - } - - StringRef Tok = Tokens[I]; - if (Tok == ".") - Result += LocCounter; - else - Result += getInteger(Tok); - } - return Result; -} - template SectionRule *LinkerScript::find(InputSectionBase *S) { for (SectionRule &R : Sections) @@ -90,66 +55,6 @@ template bool LinkerScript::shouldKeep(InputSectionBase *S) { return R && R->Keep; } -// This method finalizes the Locations list. Adds neccesary locations for -// orphan sections, what prepares it for futher use without -// changes in LinkerScript::assignAddresses(). -template -void LinkerScript::fixupLocations(std::vector *> &S) { - // Orphan sections are sections present in the input files which - // are not explicitly placed into the output file by the linker - // script. We place orphan sections at end of file. Other linkers places - // them using some heuristics as described in - // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. - for (OutputSectionBase *Sec : S) { - StringRef Name = Sec->getName(); - auto I = std::find(SectionOrder.begin(), SectionOrder.end(), Name); - if (I == SectionOrder.end()) - Locations.push_back({Command::Section, {}, {Name}}); - } -} - -template -void LinkerScript::assignAddresses(std::vector *> &S) { - typedef typename ELFT::uint uintX_t; - - Script->fixupLocations(S); - - uintX_t ThreadBssOffset = 0; - uintX_t VA = - Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); - - for (LocationNode &Node : Locations) { - if (Node.Type == Command::Expr) { - VA = evaluate(Node.Expr, VA); - continue; - } - - auto I = - std::find_if(S.begin(), S.end(), [&](OutputSectionBase *Sec) { - return Sec->getName() == Node.SectionName; - }); - if (I == S.end()) - continue; - - OutputSectionBase *Sec = *I; - uintX_t Align = Sec->getAlign(); - if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { - uintX_t TVA = VA + ThreadBssOffset; - TVA = alignTo(TVA, Align); - Sec->setVA(TVA); - ThreadBssOffset = TVA - VA + Sec->getSize(); - continue; - } - - if (Sec->getFlags() & SHF_ALLOC) { - VA = alignTo(VA, Align); - Sec->setVA(VA); - VA += Sec->getSize(); - continue; - } - } -} - ArrayRef LinkerScript::getFiller(StringRef Name) { auto I = Filler.find(Name); if (I == Filler.end()) @@ -221,7 +126,6 @@ private: void readSearchDir(); void readSections(); - void readLocationCounterValue(); void readOutputSectionDescription(); void readSectionPatterns(StringRef OutSec, bool Keep); @@ -383,13 +287,8 @@ void ScriptParser::readSearchDir() { void ScriptParser::readSections() { expect("{"); - while (!Error && !skip("}")) { - StringRef Tok = peek(); - if (Tok == ".") - readLocationCounterValue(); - else - readOutputSectionDescription(); - } + while (!Error && !skip("}")) + readOutputSectionDescription(); } void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) { @@ -398,25 +297,9 @@ void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) { Script->Sections.emplace_back(OutSec, next(), Keep); } -void ScriptParser::readLocationCounterValue() { - expect("."); - expect("="); - Script->Locations.push_back({Command::Expr, {}, {}}); - LocationNode &Node = Script->Locations.back(); - while (!Error) { - StringRef Tok = next(); - if (Tok == ";") - break; - Node.Expr.push_back(Tok); - } - if (Node.Expr.empty()) - error("error in location counter expression"); -} - void ScriptParser::readOutputSectionDescription() { StringRef OutSec = next(); Script->SectionOrder.push_back(OutSec); - Script->Locations.push_back({Command::Section, {}, {OutSec}}); expect(":"); expect("{"); while (!Error && !skip("}")) { @@ -457,7 +340,6 @@ static bool isUnderSysroot(StringRef Path) { void LinkerScript::read(MemoryBufferRef MB) { StringRef Path = MB.getBufferIdentifier(); ScriptParser(&Alloc, MB.getBuffer(), isUnderSysroot(Path)).run(); - Exists = true; } template StringRef LinkerScript::getOutputSection(InputSectionBase *); @@ -474,12 +356,3 @@ template bool LinkerScript::shouldKeep(InputSectionBase *); template bool LinkerScript::shouldKeep(InputSectionBase *); template bool LinkerScript::shouldKeep(InputSectionBase *); template bool LinkerScript::shouldKeep(InputSectionBase *); - -template void -LinkerScript::assignAddresses(std::vector *> &); -template void -LinkerScript::assignAddresses(std::vector *> &); -template void -LinkerScript::assignAddresses(std::vector *> &); -template void -LinkerScript::assignAddresses(std::vector *> &); diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 0a78e2b..e4a7a90 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -21,7 +21,6 @@ namespace elf { class ScriptParser; template class InputSectionBase; -template class OutputSectionBase; // This class represents each rule in SECTIONS command. class SectionRule { @@ -41,17 +40,6 @@ private: StringRef SectionPattern; }; -// This enum represents what we can observe in SECTIONS tag of script: -// Expr is a location counter change, like ". = . + 0x1000" -// Section is a description of output section, like ".data :..." -enum class Command { Expr, Section }; - -struct LocationNode { - Command Type; - std::vector Expr; - StringRef SectionName; -}; - // This is a runner of the linker script. class LinkerScript { friend class ScriptParser; @@ -65,16 +53,9 @@ public: ArrayRef getFiller(StringRef Name); template bool isDiscarded(InputSectionBase *S); template bool shouldKeep(InputSectionBase *S); - template - void assignAddresses(std::vector *> &S); int compareSections(StringRef A, StringRef B); - bool Exists = false; - private: - template - void fixupLocations(std::vector *> &); - uint64_t evaluate(std::vector &Tokens, uint64_t LocCounter); template SectionRule *find(InputSectionBase *S); // SECTIONS commands. @@ -86,9 +67,6 @@ private: // Section fill attribute for each section. llvm::StringMap> Filler; - // Used to assign addresses to sections. - std::vector Locations; - llvm::BumpPtrAllocator Alloc; }; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 9b7cd5a..f673c79 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -220,12 +220,8 @@ template void Writer::run() { } else { createPhdrs(); fixHeaders(); - if (Script->Exists) { - Script->assignAddresses(OutputSections); - } else { - fixSectionAlignments(); - assignAddresses(); - } + fixSectionAlignments(); + assignAddresses(); assignFileOffsets(); setPhdrs(); fixAbsoluteSymbols(); @@ -1532,11 +1528,10 @@ template void Writer::fixSectionAlignments() { // sections. These are special, we do not include them into output sections // list, but have them to simplify the code. template void Writer::fixHeaders() { - uintX_t BaseVA = Script->Exists ? 0 : Target->getVAStart(); - Out::ElfHeader->setVA(BaseVA); + Out::ElfHeader->setVA(Target->getVAStart()); Out::ElfHeader->setFileOffset(0); uintX_t Off = Out::ElfHeader->getSize(); - Out::ProgramHeaders->setVA(Off + BaseVA); + Out::ProgramHeaders->setVA(Off + Target->getVAStart()); Out::ProgramHeaders->setFileOffset(Off); } diff --git a/lld/test/ELF/end.s b/lld/test/ELF/end.s index 689157f..390e444 100644 --- a/lld/test/ELF/end.s +++ b/lld/test/ELF/end.s @@ -36,13 +36,13 @@ // NOBSS-NEXT: SHF_ALLOC // NOBSS-NEXT: SHF_WRITE // NOBSS-NEXT: ] -// NOBSS-NEXT: Address: 0x159 +// NOBSS-NEXT: Address: 0x12000 // NOBSS-NEXT: Offset: // NOBSS-NEXT: Size: 2 // NOBSS: ] // NOBSS: Symbols [ // NOBSS: Name: _end -// NOBSS-NEXT: Value: 0x15B +// NOBSS-NEXT: Value: 0x12002 // NOBSS: ] // If the layout of the sections is changed, "_end" should point to the end of allocated address space. @@ -60,13 +60,13 @@ // TEXTATEND-NEXT: SHF_ALLOC // TEXTATEND-NEXT: SHF_EXECINSTR // TEXTATEND-NEXT: ] -// TEXTATEND-NEXT: Address: 0x160 +// TEXTATEND-NEXT: Address: 0x12000 // TEXTATEND-NEXT: Offset: // TEXTATEND-NEXT: Size: 1 // TEXTATEND: ] // TEXTATEND: Symbols [ // TEXTATEND: Name: _end -// TEXTATEND-NEXT: Value: 0x161 +// TEXTATEND-NEXT: Value: 0x12001 // TEXTATEND: ] .global _start,_end diff --git a/lld/test/ELF/linkerscript-locationcounter.s b/lld/test/ELF/linkerscript-locationcounter.s deleted file mode 100644 index be3874a..0000000 --- a/lld/test/ELF/linkerscript-locationcounter.s +++ /dev/null @@ -1,74 +0,0 @@ -# REQUIRES: x86 -# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t -# RUN: echo "SECTIONS { \ -# RUN: . = 0x12341; \ -# RUN: .data : { *(.data) } \ -# RUN: . = . + 0x10000; \ -# RUN: .text : { *(.text) } \ -# RUN: }" > %t.script - -# RUN: ld.lld %t --script %t.script -o %t2 -# RUN: llvm-readobj -s %t2 | FileCheck %s - -# CHECK: Sections [ -# CHECK-NEXT: Section { -# CHECK-NEXT: Index: 0 -# CHECK-NEXT: Name: (0) -# CHECK-NEXT: Type: SHT_NULL -# CHECK-NEXT: Flags [ -# CHECK-NEXT: ] -# CHECK-NEXT: Address: 0x0 -# CHECK-NEXT: Offset: 0x0 -# CHECK-NEXT: Size: 0 -# CHECK-NEXT: Link: 0 -# CHECK-NEXT: Info: 0 -# CHECK-NEXT: AddressAlignment: 0 -# CHECK-NEXT: EntrySize: 0 -# CHECK-NEXT: } -# CHECK-NEXT: Section { -# CHECK-NEXT: Index: 1 -# CHECK-NEXT: Name: .data -# CHECK-NEXT: Type: SHT_PROGBITS -# CHECK-NEXT: Flags [ -# CHECK-NEXT: SHF_ALLOC -# CHECK-NEXT: SHF_WRITE -# CHECK-NEXT: ] -# CHECK-NEXT: Address: 0x12341 -# CHECK-NEXT: Offset: 0x158 -# CHECK-NEXT: Size: 8 -# CHECK-NEXT: Link: 0 -# CHECK-NEXT: Info: 0 -# CHECK-NEXT: AddressAlignment: 1 -# CHECK-NEXT: EntrySize: 0 -# CHECK-NEXT: } -# CHECK-NEXT: Section { -# CHECK-NEXT: Index: 2 -# CHECK-NEXT: Name: .text -# CHECK-NEXT: Type: SHT_PROGBITS -# CHECK-NEXT: Flags [ -# CHECK-NEXT: SHF_ALLOC -# CHECK-NEXT: SHF_EXECINSTR -# CHECK-NEXT: ] -# CHECK-NEXT: Address: 0x2234C -# CHECK-NEXT: Offset: 0x160 -# CHECK-NEXT: Size: 1 -# CHECK-NEXT: Link: 0 -# CHECK-NEXT: Info: 0 -# CHECK-NEXT: AddressAlignment: 4 -# CHECK-NEXT: EntrySize: 0 -# CHECK-NEXT: } - -# RUN: echo "SECTIONS { \ -# RUN: . = 0x12Q41; \ -# RUN: }" > %t.script -# RUN: not ld.lld %t --script %t.script -o %t2 2>&1 | \ -# RUN: FileCheck --check-prefix=NUMERR %s -# NUMERR: malformed number: 0x12Q41 - -.globl _start; -_start: -nop - -.section .data -.quad 0 - diff --git a/lld/test/ELF/linkerscript-sections-keep.s b/lld/test/ELF/linkerscript-sections-keep.s index b77ac4c..3e24ec9 100644 --- a/lld/test/ELF/linkerscript-sections-keep.s +++ b/lld/test/ELF/linkerscript-sections-keep.s @@ -12,8 +12,8 @@ # SECGC: Sections: # SECGC-NEXT: Idx Name Size Address Type # SECGC-NEXT: 0 00000000 0000000000000000 -# SECGC-NEXT: 1 .text 00000007 0000000000000158 TEXT DATA -# SECGC-NEXT: 2 .temp 00000004 000000000000015f DATA +# SECGC-NEXT: 1 .text 00000007 0000000000011000 TEXT DATA +# SECGC-NEXT: 2 .temp 00000004 0000000000012000 DATA ## Now apply KEEP command to preserve the section. # RUN: echo "SECTIONS { \ @@ -26,9 +26,9 @@ # SECNOGC: Sections: # SECNOGC-NEXT: Idx Name Size Address Type # SECNOGC-NEXT: 0 00000000 0000000000000000 -# SECNOGC-NEXT: 1 .text 00000007 0000000000000158 TEXT DATA -# SECNOGC-NEXT: 2 .keep 00000004 000000000000015f DATA -# SECNOGC-NEXT: 3 .temp 00000004 0000000000000163 DATA +# SECNOGC-NEXT: 1 .text 00000007 0000000000011000 TEXT DATA +# SECNOGC-NEXT: 2 .keep 00000004 0000000000012000 DATA +# SECNOGC-NEXT: 3 .temp 00000004 0000000000012004 DATA ## A section name matches two entries in the SECTIONS directive. The ## first one doesn't have KEEP, the second one does. If section that have @@ -42,9 +42,9 @@ # KEEP-AT-FIRST: Sections: # KEEP-AT-FIRST-NEXT: Idx Name Size Address Type # KEEP-AT-FIRST-NEXT: 0 00000000 0000000000000000 -# KEEP-AT-FIRST-NEXT: 1 .keep 00000004 0000000000000120 DATA -# KEEP-AT-FIRST-NEXT: 2 .temp 00000004 0000000000000124 DATA -# KEEP-AT-FIRST-NEXT: 3 .text 00000007 0000000000000128 TEXT DATA +# KEEP-AT-FIRST-NEXT: 1 .keep 00000004 0000000000010120 DATA +# KEEP-AT-FIRST-NEXT: 2 .temp 00000004 0000000000010124 DATA +# KEEP-AT-FIRST-NEXT: 3 .text 00000007 0000000000011000 TEXT DATA # KEEP-AT-FIRST-NEXT: 4 .symtab 00000060 0000000000000000 # KEEP-AT-FIRST-NEXT: 5 .shstrtab 0000002d 0000000000000000 # KEEP-AT-FIRST-NEXT: 6 .strtab 00000012 0000000000000000 @@ -63,8 +63,8 @@ # KEEP-AT-SECOND: Sections: # KEEP-AT-SECOND-NEXT: Idx Name Size Address Type # KEEP-AT-SECOND-NEXT: 0 00000000 0000000000000000 -# KEEP-AT-SECOND-NEXT: 1 .temp 00000004 0000000000000120 DATA -# KEEP-AT-SECOND-NEXT: 2 .text 00000007 0000000000000124 TEXT DATA +# KEEP-AT-SECOND-NEXT: 1 .temp 00000004 0000000000010120 DATA +# KEEP-AT-SECOND-NEXT: 2 .text 00000007 0000000000011000 TEXT DATA # KEEP-AT-SECOND-NEXT: 3 .symtab 00000048 0000000000000000 # KEEP-AT-SECOND-NEXT: 4 .shstrtab 00000027 0000000000000000 # KEEP-AT-SECOND-NEXT: 5 .strtab 0000000d 0000000000000000 diff --git a/lld/test/ELF/linkerscript-va.s b/lld/test/ELF/linkerscript-va.s deleted file mode 100644 index 7f94e63..0000000 --- a/lld/test/ELF/linkerscript-va.s +++ /dev/null @@ -1,24 +0,0 @@ -# REQUIRES: x86 -# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t - -# RUN: echo "" > %t.script -# RUN: ld.lld -o %t1 --script %t.script %t -# RUN: llvm-objdump -section-headers %t1 | FileCheck %s -# CHECK: Sections: -# CHECK-NEXT: Idx Name Size Address Type -# CHECK-NEXT: 0 00000000 0000000000000000 -# CHECK-NEXT: 1 .foo 00000004 0000000000000120 DATA -# CHECK-NEXT: 2 .boo 00000004 0000000000000124 DATA -# CHECK-NEXT: 3 .text 00000001 0000000000000128 TEXT DATA - -.global _start -_start: - nop - -.section .foo, "a" -foo: - .long 0 - -.section .boo, "a" -boo: - .long 0 diff --git a/lld/test/ELF/wildcards.s b/lld/test/ELF/wildcards.s index 2fe0f88..9bacaec 100644 --- a/lld/test/ELF/wildcards.s +++ b/lld/test/ELF/wildcards.s @@ -10,10 +10,10 @@ # SEC-DEFAULT: Sections: # SEC-DEFAULT-NEXT: Idx Name Size Address Type # SEC-DEFAULT-NEXT: 0 00000000 0000000000000000 -# SEC-DEFAULT-NEXT: 1 .text 00000008 0000000000000120 TEXT DATA -# SEC-DEFAULT-NEXT: 2 .abcd 00000004 0000000000000128 TEXT DATA -# SEC-DEFAULT-NEXT: 3 .ad 00000004 000000000000012c TEXT DATA -# SEC-DEFAULT-NEXT: 4 .ag 00000004 0000000000000130 TEXT DATA +# SEC-DEFAULT-NEXT: 1 .text 00000008 0000000000011000 TEXT DATA +# SEC-DEFAULT-NEXT: 2 .abcd 00000004 0000000000011008 TEXT DATA +# SEC-DEFAULT-NEXT: 3 .ad 00000004 000000000001100c TEXT DATA +# SEC-DEFAULT-NEXT: 4 .ag 00000004 0000000000011010 TEXT DATA # SEC-DEFAULT-NEXT: 5 .symtab 00000030 0000000000000000 # SEC-DEFAULT-NEXT: 6 .shstrtab 0000002f 0000000000000000 # SEC-DEFAULT-NEXT: 7 .strtab 00000008 0000000000000000 @@ -34,9 +34,9 @@ # SEC-ALL: Sections: # SEC-ALL-NEXT: Idx Name Size Address Type # SEC-ALL-NEXT: 0 00000000 0000000000000000 -# SEC-ALL-NEXT: 1 .text 0000000c 0000000000000120 TEXT DATA -# SEC-ALL-NEXT: 2 .ad 00000004 000000000000012c TEXT DATA -# SEC-ALL-NEXT: 3 .ag 00000004 0000000000000130 TEXT DATA +# SEC-ALL-NEXT: 1 .text 0000000c 0000000000011000 TEXT DATA +# SEC-ALL-NEXT: 2 .ad 00000004 000000000001100c TEXT DATA +# SEC-ALL-NEXT: 3 .ag 00000004 0000000000011010 TEXT DATA # SEC-ALL-NEXT: 4 .symtab 00000030 0000000000000000 # SEC-ALL-NEXT: 5 .shstrtab 00000029 0000000000000000 # SEC-ALL-NEXT: 6 .strtab 00000008 0000000000000000 @@ -50,7 +50,7 @@ # SEC-NO: Sections: # SEC-NO-NEXT: Idx Name Size Address Type # SEC-NO-NEXT: 0 00000000 0000000000000000 -# SEC-NO-NEXT: 1 .text 00000014 0000000000000120 TEXT DATA +# SEC-NO-NEXT: 1 .text 00000014 0000000000011000 TEXT DATA # SEC-NO-NEXT: 2 .symtab 00000030 0000000000000000 # SEC-NO-NEXT: 3 .shstrtab 00000021 0000000000000000 # SEC-NO-NEXT: 4 .strtab 00000008 0000000000000000 -- 2.7.4