llvm-capstone/lld/ELF/LinkerScript.h

299 lines
9.1 KiB
C
Raw Normal View History

//===- LinkerScript.h -------------------------------------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_ELF_LINKER_SCRIPT_H
#define LLD_ELF_LINKER_SCRIPT_H
#include "Config.h"
#include "Strings.h"
#include "Writer.h"
#include "lld/Core/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <vector>
namespace lld {
2016-02-28 00:25:54 +00:00
namespace elf {
class DefinedCommon;
class ScriptParser;
class SymbolBody;
template <class ELFT> class InputSectionBase;
template <class ELFT> class InputSection;
class OutputSectionBase;
template <class ELFT> class OutputSectionFactory;
class InputSectionData;
2016-10-13 23:08:33 +00:00
// This represents an expression in the linker script.
// ScriptParser::readExpr reads an expression and returns an Expr.
// Later, we evaluate the expression by calling the function
// with the value of special context variable ".".
struct Expr {
std::function<uint64_t(uint64_t)> Val;
std::function<bool()> IsAbsolute;
// If expression is section-relative the function below is used
// to get the output section pointer.
std::function<const OutputSectionBase *()> Section;
uint64_t operator()(uint64_t Dot) const { return Val(Dot); }
operator bool() const { return (bool)Val; }
Expr(std::function<uint64_t(uint64_t)> Val, std::function<bool()> IsAbsolute,
std::function<const OutputSectionBase *()> Section)
: Val(Val), IsAbsolute(IsAbsolute), Section(Section) {}
template <typename T>
Expr(T V) : Expr(V, [] { return true; }, [] { return nullptr; }) {}
Expr() : Expr(nullptr) {}
};
Make readExpr return an Expr object instead of a vector of tokens. Previously, we handled an expression as a vector of tokens. In other words, an expression was a vector of uncooked raw StringRefs. When we need a value of an expression, we used ExprParser to run the expression. The separation was needed essentially because parse time is too early to evaluate an expression. In order to evaluate an expression, we need to finalize section sizes. Because linker script parsing is done at very early stage of the linking process, we can't evaluate expressions while parsing. The above mechanism worked fairly well, but there were a few drawbacks. One thing is that we sometimes have to parse the same expression more than once in order to find the end of the expression. In some contexts, linker script expressions have no clear end marker. So, we needed to recognize balanced expressions and ternary operators. The other is poor error reporting. Since expressions are parsed basically twice, and some information that is available at the first stage is lost in the second stage, it was hard to print out apprpriate error messages. This patch fixes the issues with a new approach. Now the expression parsing is integrated into ScriptParser. ExprParser class is removed. Expressions are represented as lambdas instead of vectors of tokens. Lambdas captures information they need to run themselves when they are created. In this way, ends of expressions are naturally detected, and errors are handled in the usual way. This patch also reduces the amount of code. Differential Revision: https://reviews.llvm.org/D22728 llvm-svn: 276574
2016-07-24 18:19:40 +00:00
// Parses a linker script. Calling this function updates
// Config and ScriptConfig.
void readLinkerScript(MemoryBufferRef MB);
2016-10-13 23:08:33 +00:00
// Parses a version script.
void readVersionScript(MemoryBufferRef MB);
void readDynamicList(MemoryBufferRef MB);
// This enum is used to implement linker script SECTIONS command.
// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
enum SectionsCommandKind {
AssignmentKind, // . = expr or <sym> = expr
OutputSectionKind,
InputSectionKind,
AssertKind, // ASSERT(expr)
BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
};
struct BaseCommand {
BaseCommand(int K) : Kind(K) {}
virtual ~BaseCommand() = default;
int Kind;
};
2016-10-13 23:08:33 +00:00
// This represents ". = <expr>" or "<symbol> = <expr>".
struct SymbolAssignment : BaseCommand {
SymbolAssignment(StringRef Name, Expr E)
: BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
static bool classof(const BaseCommand *C);
2016-07-29 05:52:33 +00:00
// The LHS of an expression. Name is either a symbol name or ".".
StringRef Name;
2016-07-29 05:52:33 +00:00
SymbolBody *Sym = nullptr;
// The RHS of an expression.
Make readExpr return an Expr object instead of a vector of tokens. Previously, we handled an expression as a vector of tokens. In other words, an expression was a vector of uncooked raw StringRefs. When we need a value of an expression, we used ExprParser to run the expression. The separation was needed essentially because parse time is too early to evaluate an expression. In order to evaluate an expression, we need to finalize section sizes. Because linker script parsing is done at very early stage of the linking process, we can't evaluate expressions while parsing. The above mechanism worked fairly well, but there were a few drawbacks. One thing is that we sometimes have to parse the same expression more than once in order to find the end of the expression. In some contexts, linker script expressions have no clear end marker. So, we needed to recognize balanced expressions and ternary operators. The other is poor error reporting. Since expressions are parsed basically twice, and some information that is available at the first stage is lost in the second stage, it was hard to print out apprpriate error messages. This patch fixes the issues with a new approach. Now the expression parsing is integrated into ScriptParser. ExprParser class is removed. Expressions are represented as lambdas instead of vectors of tokens. Lambdas captures information they need to run themselves when they are created. In this way, ends of expressions are naturally detected, and errors are handled in the usual way. This patch also reduces the amount of code. Differential Revision: https://reviews.llvm.org/D22728 llvm-svn: 276574
2016-07-24 18:19:40 +00:00
Expr Expression;
2016-07-29 05:52:33 +00:00
// Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
bool Provide = false;
bool Hidden = false;
};
// Linker scripts allow additional constraints to be put on ouput sections.
2016-10-13 23:08:33 +00:00
// If an output section is marked as ONLY_IF_RO, the section is created
// only if its input sections are read-only. Likewise, an output section
// with ONLY_IF_RW is created if all input sections are RW.
enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
struct OutputSectionCommand : BaseCommand {
OutputSectionCommand(StringRef Name)
: BaseCommand(OutputSectionKind), Name(Name) {}
static bool classof(const BaseCommand *C);
StringRef Name;
Expr AddrExpr;
Expr AlignExpr;
Expr LMAExpr;
Expr SubalignExpr;
std::vector<std::unique_ptr<BaseCommand>> Commands;
std::vector<StringRef> Phdrs;
uint32_t Filler = 0;
ConstraintKind Constraint = ConstraintKind::NoConstraint;
std::string Location;
};
// This struct represents one section match pattern in SECTIONS() command.
// It can optionally have negative match pattern for EXCLUDED_FILE command.
// Also it may be surrounded with SORT() command, so contains sorting rules.
struct SectionPattern {
SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
: ExcludedFilePat(Pat1), SectionPat(Pat2) {}
StringMatcher ExcludedFilePat;
StringMatcher SectionPat;
SortSectionPolicy SortOuter;
SortSectionPolicy SortInner;
};
struct InputSectionDescription : BaseCommand {
InputSectionDescription(StringRef FilePattern)
: BaseCommand(InputSectionKind), FilePat({FilePattern}) {}
static bool classof(const BaseCommand *C);
StringMatcher FilePat;
// Input sections that matches at least one of SectionPatterns
// will be associated with this InputSectionDescription.
std::vector<SectionPattern> SectionPatterns;
std::vector<InputSectionData *> Sections;
};
2016-10-13 23:08:33 +00:00
// Represents an ASSERT().
struct AssertCommand : BaseCommand {
AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
static bool classof(const BaseCommand *C);
Expr Expression;
};
2016-10-13 23:08:33 +00:00
// Represents BYTE(), SHORT(), LONG(), or QUAD().
struct BytesDataCommand : BaseCommand {
BytesDataCommand(uint64_t Data, unsigned Size)
: BaseCommand(BytesDataKind), Data(Data), Size(Size) {}
static bool classof(const BaseCommand *C);
uint64_t Data;
unsigned Offset;
unsigned Size;
};
struct PhdrsCommand {
StringRef Name;
unsigned Type;
bool HasFilehdr;
bool HasPhdrs;
unsigned Flags;
Expr LMAExpr;
};
class LinkerScriptBase {
protected:
~LinkerScriptBase() = default;
public:
virtual uint64_t getHeaderSize() = 0;
virtual uint64_t getSymbolValue(StringRef S) = 0;
virtual bool isDefined(StringRef S) = 0;
virtual bool isAbsolute(StringRef S) = 0;
virtual const OutputSectionBase *getSymbolSection(StringRef S) = 0;
virtual const OutputSectionBase *getOutputSection(const Twine &Loc,
StringRef S) = 0;
virtual uint64_t getOutputSectionSize(StringRef S) = 0;
};
// ScriptConfiguration holds linker script parse results.
struct ScriptConfiguration {
// Used to assign addresses to sections.
std::vector<std::unique_ptr<BaseCommand>> Commands;
// Used to assign sections to headers.
std::vector<PhdrsCommand> PhdrsCommands;
bool HasSections = false;
// List of section patterns specified with KEEP commands. They will
// be kept even if they are unused and --gc-sections is specified.
std::vector<InputSectionDescription *> KeptSections;
};
extern ScriptConfiguration *ScriptConfig;
// This is a runner of the linker script.
template <class ELFT> class LinkerScript final : public LinkerScriptBase {
typedef typename ELFT::uint uintX_t;
public:
LinkerScript();
~LinkerScript();
void processCommands(OutputSectionFactory<ELFT> &Factory);
void addOrphanSections(OutputSectionFactory<ELFT> &Factory);
void removeEmptyCommands();
void adjustSectionsBeforeSorting();
void adjustSectionsAfterSorting();
std::vector<PhdrEntry<ELFT>> createPhdrs();
bool ignoreInterpSection();
uint32_t getFiller(StringRef Name);
void writeDataBytes(StringRef Name, uint8_t *Buf);
bool hasLMA(StringRef Name);
bool shouldKeep(InputSectionBase<ELFT> *S);
void assignOffsets(OutputSectionCommand *Cmd);
void placeOrphanSections();
void assignAddresses(std::vector<PhdrEntry<ELFT>> &Phdrs);
bool hasPhdrsCommands();
uint64_t getHeaderSize() override;
uint64_t getSymbolValue(StringRef S) override;
bool isDefined(StringRef S) override;
bool isAbsolute(StringRef S) override;
const OutputSectionBase *getSymbolSection(StringRef S) override;
const OutputSectionBase *getOutputSection(const Twine &Loc,
StringRef S) override;
uint64_t getOutputSectionSize(StringRef S) override;
std::vector<OutputSectionBase *> *OutputSections;
int getSectionIndex(StringRef Name);
private:
void computeInputSections(InputSectionDescription *);
void addSection(OutputSectionFactory<ELFT> &Factory,
InputSectionBase<ELFT> *Sec, StringRef Name);
void discard(ArrayRef<InputSectionBase<ELFT> *> V);
std::vector<InputSectionBase<ELFT> *>
createInputSectionList(OutputSectionCommand &Cmd);
// "ScriptConfig" is a bit too long, so define a short name for it.
ScriptConfiguration &Opt = *ScriptConfig;
2016-07-24 23:47:31 +00:00
std::vector<size_t> getPhdrIndices(StringRef SectionName);
size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
uintX_t Dot;
uintX_t LMAOffset = 0;
OutputSectionBase *CurOutSec = nullptr;
uintX_t ThreadBssOffset = 0;
void switchTo(OutputSectionBase *Sec);
void flush();
void output(InputSection<ELFT> *Sec);
void process(BaseCommand &Base);
llvm::DenseSet<OutputSectionBase *> AlreadyOutputOS;
llvm::DenseSet<InputSectionData *> AlreadyOutputIS;
};
// Variable template is a C++14 feature, so we can't template
// a global variable. Use a struct to workaround.
template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
extern LinkerScriptBase *ScriptBase;
} // end namespace elf
} // end namespace lld
#endif // LLD_ELF_LINKER_SCRIPT_H