diff options
author | dim <dim@FreeBSD.org> | 2016-12-26 20:36:37 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2016-12-26 20:36:37 +0000 |
commit | 06210ae42d418d50d8d9365d5c9419308ae9e7ee (patch) | |
tree | ab60b4cdd6e430dda1f292a46a77ddb744723f31 /contrib/llvm/tools/lld/ELF/LinkerScript.cpp | |
parent | 2dd166267f53df1c3748b4325d294b9b839de74b (diff) | |
download | FreeBSD-src-06210ae42d418d50d8d9365d5c9419308ae9e7ee.zip FreeBSD-src-06210ae42d418d50d8d9365d5c9419308ae9e7ee.tar.gz |
MFC r309124:
Upgrade our copies of clang, llvm, lldb, compiler-rt and libc++ to 3.9.0
release, and add lld 3.9.0. Also completely revamp the build system for
clang, llvm, lldb and their related tools.
Please note that from 3.5.0 onwards, clang, llvm and lldb require C++11
support to build; see UPDATING for more information.
Release notes for llvm, clang and lld are available here:
<http://llvm.org/releases/3.9.0/docs/ReleaseNotes.html>
<http://llvm.org/releases/3.9.0/tools/clang/docs/ReleaseNotes.html>
<http://llvm.org/releases/3.9.0/tools/lld/docs/ReleaseNotes.html>
Thanks to Ed Maste, Bryan Drewery, Andrew Turner, Antoine Brodin and Jan
Beich for their help.
Relnotes: yes
MFC r309147:
Pull in r282174 from upstream llvm trunk (by Krzysztof Parzyszek):
[PPC] Set SP after loading data from stack frame, if no red zone is
present
Follow-up to r280705: Make sure that the SP is only restored after
all data is loaded from the stack frame, if there is no red zone.
This completes the fix for
https://llvm.org/bugs/show_bug.cgi?id=26519.
Differential Revision: https://reviews.llvm.org/D24466
Reported by: Mark Millard
PR: 214433
MFC r309149:
Pull in r283060 from upstream llvm trunk (by Hal Finkel):
[PowerPC] Refactor soft-float support, and enable PPC64 soft float
This change enables soft-float for PowerPC64, and also makes
soft-float disable all vector instruction sets for both 32-bit and
64-bit modes. This latter part is necessary because the PPC backend
canonicalizes many Altivec vector types to floating-point types, and
so soft-float breaks scalarization support for many operations. Both
for embedded targets and for operating-system kernels desiring
soft-float support, it seems reasonable that disabling hardware
floating-point also disables vector instructions (embedded targets
without hardware floating point support are unlikely to have Altivec,
etc. and operating system kernels desiring not to use floating-point
registers to lower syscall cost are unlikely to want to use vector
registers either). If someone needs this to work, we'll need to
change the fact that we promote many Altivec operations to act on
v4f32. To make it possible to disable Altivec when soft-float is
enabled, hardware floating-point support needs to be expressed as a
positive feature, like the others, and not a negative feature,
because target features cannot have dependencies on the disabling of
some other feature. So +soft-float has now become -hard-float.
Fixes PR26970.
Pull in r283061 from upstream clang trunk (by Hal Finkel):
[PowerPC] Enable soft-float for PPC64, and +soft-float -> -hard-float
Enable soft-float support on PPC64, as the backend now supports it.
Also, the backend now uses -hard-float instead of +soft-float, so set
the target features accordingly.
Fixes PR26970.
Reported by: Mark Millard
PR: 214433
MFC r309212:
Add a few missed clang 3.9.0 files to OptionalObsoleteFiles.
MFC r309262:
Fix packaging for clang, lldb and lld 3.9.0
During the upgrade of clang/llvm etc to 3.9.0 in r309124, the PACKAGE
directive in the usr.bin/clang/*.mk files got dropped accidentally.
Restore it, with a few minor changes and additions:
* Correct license in clang.ucl to NCSA
* Add PACKAGE=clang for clang and most of the "ll" tools
* Put lldb in its own package
* Put lld in its own package
Reviewed by: gjb, jmallett
Differential Revision: https://reviews.freebsd.org/D8666
MFC r309656:
During the bootstrap phase, when building the minimal llvm library on
PowerPC, add lib/Support/Atomic.cpp. This is needed because upstream
llvm revision r271821 disabled the use of std::call_once, which causes
some fallback functions from Atomic.cpp to be used instead.
Reported by: Mark Millard
PR: 214902
MFC r309835:
Tentatively apply https://reviews.llvm.org/D18730 to work around gcc PR
70528 (bogus error: constructor required before non-static data member).
This should fix buildworld with the external gcc package.
Reported by: https://jenkins.freebsd.org/job/FreeBSD_HEAD_amd64_gcc/
MFC r310194:
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
3.9.1 release.
Please note that from 3.5.0 onwards, clang, llvm and lldb require C++11
support to build; see UPDATING for more information.
Release notes for llvm, clang and lld will be available here:
<http://releases.llvm.org/3.9.1/docs/ReleaseNotes.html>
<http://releases.llvm.org/3.9.1/tools/clang/docs/ReleaseNotes.html>
<http://releases.llvm.org/3.9.1/tools/lld/docs/ReleaseNotes.html>
Relnotes: yes
Diffstat (limited to 'contrib/llvm/tools/lld/ELF/LinkerScript.cpp')
-rw-r--r-- | contrib/llvm/tools/lld/ELF/LinkerScript.cpp | 606 |
1 files changed, 606 insertions, 0 deletions
diff --git a/contrib/llvm/tools/lld/ELF/LinkerScript.cpp b/contrib/llvm/tools/lld/ELF/LinkerScript.cpp new file mode 100644 index 0000000..61abdc1 --- /dev/null +++ b/contrib/llvm/tools/lld/ELF/LinkerScript.cpp @@ -0,0 +1,606 @@ +//===- LinkerScript.cpp ---------------------------------------------------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains the parser/evaluator of the linker script. +// It does not construct an AST but consume linker script directives directly. +// Results are written to Driver or Config object. +// +//===----------------------------------------------------------------------===// + +#include "LinkerScript.h" +#include "Config.h" +#include "Driver.h" +#include "InputSection.h" +#include "OutputSections.h" +#include "ScriptParser.h" +#include "Strings.h" +#include "Symbols.h" +#include "SymbolTable.h" +#include "Target.h" +#include "llvm/ADT/StringSwitch.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; + +ScriptConfiguration *elf::ScriptConfig; + +// This is an operator-precedence parser to parse and evaluate +// a linker script expression. For each linker script arithmetic +// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser +// is created and ran. +namespace { +class ExprParser : public ScriptParserBase { +public: + ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot) + : ScriptParserBase(Tokens), Dot(Dot) {} + + uint64_t run(); + +private: + uint64_t parsePrimary(); + uint64_t parseTernary(uint64_t Cond); + uint64_t apply(StringRef Op, uint64_t L, uint64_t R); + uint64_t parseExpr1(uint64_t Lhs, int MinPrec); + uint64_t parseExpr(); + + uint64_t Dot; +}; +} + +static int precedence(StringRef Op) { + return StringSwitch<int>(Op) + .Case("*", 4) + .Case("/", 4) + .Case("+", 3) + .Case("-", 3) + .Case("<", 2) + .Case(">", 2) + .Case(">=", 2) + .Case("<=", 2) + .Case("==", 2) + .Case("!=", 2) + .Case("&", 1) + .Default(-1); +} + +static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) { + return ExprParser(Tokens, Dot).run(); +} + +uint64_t ExprParser::run() { + uint64_t V = parseExpr(); + if (!atEOF() && !Error) + setError("stray token: " + peek()); + return V; +} + +// This is a part of the operator-precedence parser to evaluate +// arithmetic expressions in SECTIONS command. This function evaluates an +// integer literal, a parenthesized expression, the ALIGN function, +// or the special variable ".". +uint64_t ExprParser::parsePrimary() { + StringRef Tok = next(); + if (Tok == ".") + return Dot; + if (Tok == "(") { + uint64_t V = parseExpr(); + expect(")"); + return V; + } + if (Tok == "ALIGN") { + expect("("); + uint64_t V = parseExpr(); + expect(")"); + return alignTo(Dot, V); + } + uint64_t V = 0; + if (Tok.getAsInteger(0, V)) + setError("malformed number: " + Tok); + return V; +} + +uint64_t ExprParser::parseTernary(uint64_t Cond) { + next(); + uint64_t V = parseExpr(); + expect(":"); + uint64_t W = parseExpr(); + return Cond ? V : W; +} + +uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) { + if (Op == "*") + return L * R; + if (Op == "/") { + if (R == 0) { + error("division by zero"); + return 0; + } + return L / R; + } + if (Op == "+") + return L + R; + if (Op == "-") + return L - R; + if (Op == "<") + return L < R; + if (Op == ">") + return L > R; + if (Op == ">=") + return L >= R; + if (Op == "<=") + return L <= R; + if (Op == "==") + return L == R; + if (Op == "!=") + return L != R; + if (Op == "&") + return L & R; + llvm_unreachable("invalid operator"); +} + +// This is a part of the operator-precedence parser. +// This function assumes that the remaining token stream starts +// with an operator. +uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) { + while (!atEOF()) { + // Read an operator and an expression. + StringRef Op1 = peek(); + if (Op1 == "?") + return parseTernary(Lhs); + if (precedence(Op1) < MinPrec) + return Lhs; + next(); + uint64_t Rhs = parsePrimary(); + + // Evaluate the remaining part of the expression first if the + // next operator has greater precedence than the previous one. + // For example, if we have read "+" and "3", and if the next + // operator is "*", then we'll evaluate 3 * ... part first. + while (!atEOF()) { + StringRef Op2 = peek(); + if (precedence(Op2) <= precedence(Op1)) + break; + Rhs = parseExpr1(Rhs, precedence(Op2)); + } + + Lhs = apply(Op1, Lhs, Rhs); + } + return Lhs; +} + +// Reads and evaluates an arithmetic expression. +uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); } + +template <class ELFT> +StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) { + for (SectionRule &R : Opt.Sections) + if (globMatch(R.SectionPattern, S->getSectionName())) + return R.Dest; + return ""; +} + +template <class ELFT> +bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) { + return getOutputSection(S) == "/DISCARD/"; +} + +template <class ELFT> +bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { + for (StringRef Pat : Opt.KeptSections) + if (globMatch(Pat, S->getSectionName())) + return true; + return false; +} + +template <class ELFT> +void LinkerScript<ELFT>::assignAddresses( + ArrayRef<OutputSectionBase<ELFT> *> Sections) { + // 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<ELFT> *Sec : Sections) { + StringRef Name = Sec->getName(); + if (getSectionIndex(Name) == INT_MAX) + Opt.Commands.push_back({SectionKind, {}, Name}); + } + + // Assign addresses as instructed by linker script SECTIONS sub-commands. + Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); + uintX_t MinVA = std::numeric_limits<uintX_t>::max(); + uintX_t ThreadBssOffset = 0; + + for (SectionsCommand &Cmd : Opt.Commands) { + if (Cmd.Kind == AssignmentKind) { + uint64_t Val = evalExpr(Cmd.Expr, Dot); + + if (Cmd.Name == ".") { + Dot = Val; + } else { + auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd.Name)); + D->Value = Val; + } + continue; + } + + // Find all the sections with required name. There can be more than + // ont section with such name, if the alignment, flags or type + // attribute differs. + assert(Cmd.Kind == SectionKind); + for (OutputSectionBase<ELFT> *Sec : Sections) { + if (Sec->getName() != Cmd.Name) + continue; + + if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { + uintX_t TVA = Dot + ThreadBssOffset; + TVA = alignTo(TVA, Sec->getAlignment()); + Sec->setVA(TVA); + ThreadBssOffset = TVA - Dot + Sec->getSize(); + continue; + } + + if (Sec->getFlags() & SHF_ALLOC) { + Dot = alignTo(Dot, Sec->getAlignment()); + Sec->setVA(Dot); + MinVA = std::min(MinVA, Dot); + Dot += Sec->getSize(); + continue; + } + } + } + + // ELF and Program headers need to be right before the first section in + // memory. + // Set their addresses accordingly. + MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - + Out<ELFT>::ProgramHeaders->getSize(), + Target->PageSize); + Out<ELFT>::ElfHeader->setVA(MinVA); + Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); +} + +template <class ELFT> +ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { + auto I = Opt.Filler.find(Name); + if (I == Opt.Filler.end()) + return {}; + return I->second; +} + +// Returns the index of the given section name in linker script +// SECTIONS commands. Sections are laid out as the same order as they +// were in the script. If a given name did not appear in the script, +// it returns INT_MAX, so that it will be laid out at end of file. +template <class ELFT> +int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { + auto Begin = Opt.Commands.begin(); + auto End = Opt.Commands.end(); + auto I = std::find_if(Begin, End, [&](SectionsCommand &N) { + return N.Kind == SectionKind && N.Name == Name; + }); + return I == End ? INT_MAX : (I - Begin); +} + +// A compartor to sort output sections. Returns -1 or 1 if +// A or B are mentioned in linker script. Otherwise, returns 0. +template <class ELFT> +int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { + int I = getSectionIndex(A); + int J = getSectionIndex(B); + if (I == INT_MAX && J == INT_MAX) + return 0; + return I < J ? -1 : 1; +} + +template <class ELFT> +void LinkerScript<ELFT>::addScriptedSymbols() { + for (SectionsCommand &Cmd : Opt.Commands) + if (Cmd.Kind == AssignmentKind) + if (Cmd.Name != "." && Symtab<ELFT>::X->find(Cmd.Name) == nullptr) + Symtab<ELFT>::X->addAbsolute(Cmd.Name, STV_DEFAULT); +} + +class elf::ScriptParser : public ScriptParserBase { + typedef void (ScriptParser::*Handler)(); + +public: + ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} + + void run(); + +private: + void addFile(StringRef Path); + + void readAsNeeded(); + void readEntry(); + void readExtern(); + void readGroup(); + void readInclude(); + void readNothing() {} + void readOutput(); + void readOutputArch(); + void readOutputFormat(); + void readSearchDir(); + void readSections(); + + void readLocationCounterValue(); + void readOutputSectionDescription(StringRef OutSec); + void readSymbolAssignment(StringRef Name); + std::vector<StringRef> readSectionsCommandExpr(); + + const static StringMap<Handler> Cmd; + ScriptConfiguration &Opt = *ScriptConfig; + StringSaver Saver = {ScriptConfig->Alloc}; + bool IsUnderSysroot; +}; + +const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { + {"ENTRY", &ScriptParser::readEntry}, + {"EXTERN", &ScriptParser::readExtern}, + {"GROUP", &ScriptParser::readGroup}, + {"INCLUDE", &ScriptParser::readInclude}, + {"INPUT", &ScriptParser::readGroup}, + {"OUTPUT", &ScriptParser::readOutput}, + {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, + {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, + {"SEARCH_DIR", &ScriptParser::readSearchDir}, + {"SECTIONS", &ScriptParser::readSections}, + {";", &ScriptParser::readNothing}}; + +void ScriptParser::run() { + while (!atEOF()) { + StringRef Tok = next(); + if (Handler Fn = Cmd.lookup(Tok)) + (this->*Fn)(); + else + setError("unknown directive: " + Tok); + } +} + +void ScriptParser::addFile(StringRef S) { + if (IsUnderSysroot && S.startswith("/")) { + SmallString<128> Path; + (Config->Sysroot + S).toStringRef(Path); + if (sys::fs::exists(Path)) { + Driver->addFile(Saver.save(Path.str())); + return; + } + } + + if (sys::path::is_absolute(S)) { + Driver->addFile(S); + } else if (S.startswith("=")) { + if (Config->Sysroot.empty()) + Driver->addFile(S.substr(1)); + else + Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); + } else if (S.startswith("-l")) { + Driver->addLibrary(S.substr(2)); + } else if (sys::fs::exists(S)) { + Driver->addFile(S); + } else { + std::string Path = findFromSearchPaths(S); + if (Path.empty()) + setError("unable to find " + S); + else + Driver->addFile(Saver.save(Path)); + } +} + +void ScriptParser::readAsNeeded() { + expect("("); + bool Orig = Config->AsNeeded; + Config->AsNeeded = true; + while (!Error) { + StringRef Tok = next(); + if (Tok == ")") + break; + addFile(Tok); + } + Config->AsNeeded = Orig; +} + +void ScriptParser::readEntry() { + // -e <symbol> takes predecence over ENTRY(<symbol>). + expect("("); + StringRef Tok = next(); + if (Config->Entry.empty()) + Config->Entry = Tok; + expect(")"); +} + +void ScriptParser::readExtern() { + expect("("); + while (!Error) { + StringRef Tok = next(); + if (Tok == ")") + return; + Config->Undefined.push_back(Tok); + } +} + +void ScriptParser::readGroup() { + expect("("); + while (!Error) { + StringRef Tok = next(); + if (Tok == ")") + return; + if (Tok == "AS_NEEDED") { + readAsNeeded(); + continue; + } + addFile(Tok); + } +} + +void ScriptParser::readInclude() { + StringRef Tok = next(); + auto MBOrErr = MemoryBuffer::getFile(Tok); + if (!MBOrErr) { + setError("cannot open " + Tok); + return; + } + std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; + StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); + std::vector<StringRef> V = tokenize(S); + Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); +} + +void ScriptParser::readOutput() { + // -o <file> takes predecence over OUTPUT(<file>). + expect("("); + StringRef Tok = next(); + if (Config->OutputFile.empty()) + Config->OutputFile = Tok; + expect(")"); +} + +void ScriptParser::readOutputArch() { + // Error checking only for now. + expect("("); + next(); + expect(")"); +} + +void ScriptParser::readOutputFormat() { + // Error checking only for now. + expect("("); + next(); + StringRef Tok = next(); + if (Tok == ")") + return; + if (Tok != ",") { + setError("unexpected token: " + Tok); + return; + } + next(); + expect(","); + next(); + expect(")"); +} + +void ScriptParser::readSearchDir() { + expect("("); + Config->SearchPaths.push_back(next()); + expect(")"); +} + +void ScriptParser::readSections() { + Opt.DoLayout = true; + expect("{"); + while (!Error && !skip("}")) { + StringRef Tok = peek(); + if (Tok == ".") { + readLocationCounterValue(); + continue; + } + next(); + if (peek() == "=") + readSymbolAssignment(Tok); + else + readOutputSectionDescription(Tok); + } +} + +void ScriptParser::readLocationCounterValue() { + expect("."); + expect("="); + std::vector<StringRef> Expr = readSectionsCommandExpr(); + if (Expr.empty()) + error("error in location counter expression"); + else + Opt.Commands.push_back({AssignmentKind, std::move(Expr), "."}); +} + +void ScriptParser::readOutputSectionDescription(StringRef OutSec) { + Opt.Commands.push_back({SectionKind, {}, OutSec}); + expect(":"); + expect("{"); + + while (!Error && !skip("}")) { + StringRef Tok = next(); + if (Tok == "*") { + expect("("); + while (!Error && !skip(")")) + Opt.Sections.emplace_back(OutSec, next()); + } else if (Tok == "KEEP") { + expect("("); + expect("*"); + expect("("); + while (!Error && !skip(")")) { + StringRef Sec = next(); + Opt.Sections.emplace_back(OutSec, Sec); + Opt.KeptSections.push_back(Sec); + } + expect(")"); + } else { + setError("unknown command " + Tok); + } + } + + StringRef Tok = peek(); + if (Tok.startswith("=")) { + if (!Tok.startswith("=0x")) { + setError("filler should be a hexadecimal value"); + return; + } + Tok = Tok.substr(3); + Opt.Filler[OutSec] = parseHex(Tok); + next(); + } +} + +void ScriptParser::readSymbolAssignment(StringRef Name) { + expect("="); + std::vector<StringRef> Expr = readSectionsCommandExpr(); + if (Expr.empty()) + error("error in symbol assignment expression"); + else + Opt.Commands.push_back({AssignmentKind, std::move(Expr), Name}); +} + +std::vector<StringRef> ScriptParser::readSectionsCommandExpr() { + std::vector<StringRef> Expr; + while (!Error) { + StringRef Tok = next(); + if (Tok == ";") + break; + Expr.push_back(Tok); + } + return Expr; +} + +static bool isUnderSysroot(StringRef Path) { + if (Config->Sysroot == "") + return false; + for (; !Path.empty(); Path = sys::path::parent_path(Path)) + if (sys::fs::equivalent(Config->Sysroot, Path)) + return true; + return false; +} + +// Entry point. +void elf::readLinkerScript(MemoryBufferRef MB) { + StringRef Path = MB.getBufferIdentifier(); + ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); +} + +template class elf::LinkerScript<ELF32LE>; +template class elf::LinkerScript<ELF32BE>; +template class elf::LinkerScript<ELF64LE>; +template class elf::LinkerScript<ELF64BE>; |