2016-02-12 05:17:59 +08:00
|
|
|
//===- 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
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
#include "Writer.h"
|
2016-02-12 05:17:59 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/MapVector.h"
|
2016-02-12 05:38:55 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
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-25 02:19:40 +08:00
|
|
|
#include <functional>
|
2016-02-12 05:17:59 +08:00
|
|
|
|
|
|
|
namespace lld {
|
2016-02-28 08:25:54 +08:00
|
|
|
namespace elf {
|
2016-07-29 13:48:39 +08:00
|
|
|
class SymbolBody;
|
2016-07-20 22:43:20 +08:00
|
|
|
template <class ELFT> class InputSectionBase;
|
|
|
|
template <class ELFT> class OutputSectionBase;
|
|
|
|
template <class ELFT> class OutputSectionFactory;
|
2016-07-29 03:24:13 +08:00
|
|
|
template <class ELFT> class DefinedCommon;
|
2016-08-12 09:24:53 +08:00
|
|
|
template <class ELFT> class LayoutInputSection;
|
2016-02-12 05:17:59 +08:00
|
|
|
|
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-25 02:19:40 +08:00
|
|
|
typedef std::function<uint64_t(uint64_t)> Expr;
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
// Parses a linker script. Calling this function updates
|
|
|
|
// Config and ScriptConfig.
|
|
|
|
void readLinkerScript(MemoryBufferRef MB);
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
class ScriptParser;
|
2016-02-13 05:47:28 +08:00
|
|
|
template <class ELFT> class InputSectionBase;
|
2016-04-16 18:10:32 +08:00
|
|
|
template <class ELFT> class OutputSectionBase;
|
2016-02-12 05:17:59 +08:00
|
|
|
|
2016-07-21 22:26:59 +08:00
|
|
|
// This enum is used to implement linker script SECTIONS command.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
|
|
|
|
enum SectionsCommandKind {
|
|
|
|
AssignmentKind,
|
|
|
|
OutputSectionKind,
|
2016-08-04 17:29:31 +08:00
|
|
|
InputSectionKind,
|
|
|
|
AssertKind
|
2016-02-13 05:47:28 +08:00
|
|
|
};
|
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
struct BaseCommand {
|
|
|
|
BaseCommand(int K) : Kind(K) {}
|
|
|
|
virtual ~BaseCommand() {}
|
|
|
|
int Kind;
|
|
|
|
};
|
2016-04-16 18:10:32 +08:00
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
struct SymbolAssignment : BaseCommand {
|
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-25 02:19:40 +08:00
|
|
|
SymbolAssignment(StringRef Name, Expr E)
|
|
|
|
: BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
|
2016-07-21 14:43:01 +08:00
|
|
|
static bool classof(const BaseCommand *C);
|
2016-07-29 13:52:33 +08:00
|
|
|
|
|
|
|
// The LHS of an expression. Name is either a symbol name or ".".
|
2016-07-21 14:43:01 +08:00
|
|
|
StringRef Name;
|
2016-07-29 13:52:33 +08: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-25 02:19:40 +08:00
|
|
|
Expr Expression;
|
2016-07-29 13:52:33 +08:00
|
|
|
|
|
|
|
// Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
|
2016-07-22 15:38:40 +08:00
|
|
|
bool Provide = false;
|
|
|
|
bool Hidden = false;
|
2016-07-21 14:43:01 +08:00
|
|
|
};
|
|
|
|
|
2016-07-22 11:36:24 +08:00
|
|
|
// Linker scripts allow additional constraints to be put on ouput sections.
|
|
|
|
// An output section will only be created if all of its input sections are
|
|
|
|
// read-only
|
|
|
|
// or all of its input sections are read-write by using the keyword ONLY_IF_RO
|
|
|
|
// and ONLY_IF_RW respectively.
|
2016-07-26 06:00:10 +08:00
|
|
|
enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
|
2016-07-22 11:36:24 +08:00
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
struct OutputSectionCommand : BaseCommand {
|
|
|
|
OutputSectionCommand(StringRef Name)
|
|
|
|
: BaseCommand(OutputSectionKind), Name(Name) {}
|
|
|
|
static bool classof(const BaseCommand *C);
|
2016-07-12 14:39:48 +08:00
|
|
|
StringRef Name;
|
2016-07-25 16:29:46 +08:00
|
|
|
Expr AddrExpr;
|
2016-07-27 02:06:29 +08:00
|
|
|
Expr AlignExpr;
|
2016-08-17 15:44:19 +08:00
|
|
|
Expr LmaExpr;
|
2016-07-21 22:26:59 +08:00
|
|
|
std::vector<std::unique_ptr<BaseCommand>> Commands;
|
2016-07-19 17:25:43 +08:00
|
|
|
std::vector<StringRef> Phdrs;
|
2016-07-21 14:43:01 +08:00
|
|
|
std::vector<uint8_t> Filler;
|
2016-07-26 06:00:10 +08:00
|
|
|
ConstraintKind Constraint = ConstraintKind::NoConstraint;
|
2016-07-19 17:25:43 +08:00
|
|
|
};
|
|
|
|
|
2016-08-05 06:27:00 +08:00
|
|
|
enum SortKind { SortNone, SortByName, SortByAlignment };
|
2016-08-03 16:35:59 +08:00
|
|
|
|
2016-07-21 22:26:59 +08:00
|
|
|
struct InputSectionDescription : BaseCommand {
|
|
|
|
InputSectionDescription() : BaseCommand(InputSectionKind) {}
|
|
|
|
static bool classof(const BaseCommand *C);
|
2016-07-29 05:51:30 +08:00
|
|
|
StringRef FilePattern;
|
2016-08-05 06:27:00 +08:00
|
|
|
SortKind SortOuter = SortNone;
|
|
|
|
SortKind SortInner = SortNone;
|
2016-07-27 09:44:01 +08:00
|
|
|
std::vector<StringRef> ExcludedFiles;
|
2016-07-29 05:51:30 +08:00
|
|
|
std::vector<StringRef> SectionPatterns;
|
2016-07-21 22:26:59 +08:00
|
|
|
};
|
|
|
|
|
2016-08-04 17:29:31 +08:00
|
|
|
struct AssertCommand : BaseCommand {
|
|
|
|
AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
|
|
|
|
static bool classof(const BaseCommand *C);
|
|
|
|
Expr Expression;
|
|
|
|
};
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
struct PhdrsCommand {
|
|
|
|
StringRef Name;
|
|
|
|
unsigned Type;
|
|
|
|
bool HasFilehdr;
|
|
|
|
bool HasPhdrs;
|
2016-07-21 18:43:25 +08:00
|
|
|
unsigned Flags;
|
2016-04-16 18:10:32 +08:00
|
|
|
};
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
// ScriptConfiguration holds linker script parse results.
|
|
|
|
struct ScriptConfiguration {
|
2016-04-16 18:10:32 +08:00
|
|
|
// Used to assign addresses to sections.
|
2016-07-21 14:43:01 +08:00
|
|
|
std::vector<std::unique_ptr<BaseCommand>> Commands;
|
2016-04-16 18:10:32 +08:00
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
// Used to assign sections to headers.
|
2016-07-20 23:09:10 +08:00
|
|
|
std::vector<PhdrsCommand> PhdrsCommands;
|
|
|
|
|
2016-07-29 11:31:09 +08:00
|
|
|
bool HasContents = false;
|
2016-04-21 04:13:41 +08:00
|
|
|
|
2016-02-12 05:38:55 +08:00
|
|
|
llvm::BumpPtrAllocator Alloc;
|
2016-04-22 06:00:51 +08:00
|
|
|
|
|
|
|
// List of section patterns specified with KEEP commands. They will
|
|
|
|
// be kept even if they are unused and --gc-sections is specified.
|
|
|
|
std::vector<StringRef> KeptSections;
|
2016-02-12 05:17:59 +08:00
|
|
|
};
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
extern ScriptConfiguration *ScriptConfig;
|
|
|
|
|
|
|
|
// This is a runner of the linker script.
|
|
|
|
template <class ELFT> class LinkerScript {
|
2016-04-23 04:41:07 +08:00
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
public:
|
2016-08-12 09:24:53 +08:00
|
|
|
LinkerScript();
|
|
|
|
~LinkerScript();
|
2016-08-04 20:13:05 +08:00
|
|
|
void createSections(OutputSectionFactory<ELFT> &Factory);
|
2016-07-21 01:19:03 +08:00
|
|
|
|
2016-08-04 20:13:05 +08:00
|
|
|
std::vector<PhdrEntry<ELFT>> createPhdrs();
|
2016-08-16 14:40:58 +08:00
|
|
|
bool ignoreInterpSection();
|
2016-07-23 22:18:48 +08:00
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
ArrayRef<uint8_t> getFiller(StringRef Name);
|
2016-08-17 15:44:19 +08:00
|
|
|
Expr getLma(StringRef Name);
|
2016-04-21 04:13:41 +08:00
|
|
|
bool shouldKeep(InputSectionBase<ELFT> *S);
|
2016-08-04 20:13:05 +08:00
|
|
|
void assignAddresses();
|
2016-04-21 04:13:41 +08:00
|
|
|
int compareSections(StringRef A, StringRef B);
|
2016-07-19 17:25:43 +08:00
|
|
|
bool hasPhdrsCommands();
|
2016-07-30 00:18:47 +08:00
|
|
|
uintX_t getOutputSectionSize(StringRef Name);
|
2016-08-12 12:00:22 +08:00
|
|
|
uintX_t getHeaderSize();
|
2016-04-21 04:13:41 +08:00
|
|
|
|
2016-08-04 20:13:05 +08:00
|
|
|
std::vector<OutputSectionBase<ELFT> *> *OutputSections;
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
private:
|
2016-07-26 06:51:07 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *>
|
2016-07-29 05:05:04 +08:00
|
|
|
getInputSections(const InputSectionDescription *);
|
2016-07-26 06:51:07 +08:00
|
|
|
|
2016-08-12 08:27:23 +08:00
|
|
|
void discard(OutputSectionCommand &Cmd);
|
|
|
|
|
2016-08-12 11:16:56 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *>
|
|
|
|
createInputSectionList(OutputSectionCommand &Cmd);
|
|
|
|
|
2016-04-22 08:03:13 +08:00
|
|
|
// "ScriptConfig" is a bit too long, so define a short name for it.
|
|
|
|
ScriptConfiguration &Opt = *ScriptConfig;
|
|
|
|
|
2016-04-22 04:30:00 +08:00
|
|
|
int getSectionIndex(StringRef Name);
|
2016-07-25 07:47:31 +08:00
|
|
|
std::vector<size_t> getPhdrIndices(StringRef SectionName);
|
2016-07-26 08:27:36 +08:00
|
|
|
size_t getPhdrIndex(StringRef PhdrName);
|
2016-04-21 04:13:41 +08:00
|
|
|
|
2016-08-12 09:24:53 +08:00
|
|
|
llvm::SpecificBumpPtrAllocator<LayoutInputSection<ELFT>> LAlloc;
|
|
|
|
|
2016-04-23 04:41:07 +08:00
|
|
|
uintX_t Dot;
|
2016-04-21 04:13:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
2016-02-12 05:17:59 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
} // namespace elf
|
2016-02-12 05:17:59 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|