blob: 37b6a031a5d7dc8122c9c743d5d3702e19ee299d [file] [log] [blame]
Rui Ueyama717677a2016-02-11 21:17:59 +00001//===- LinkerScript.h -------------------------------------------*- C++ -*-===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLD_ELF_LINKER_SCRIPT_H
11#define LLD_ELF_LINKER_SCRIPT_H
12
Eugene Leviantbbe38602016-07-19 09:25:43 +000013#include "Writer.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000014#include "lld/Core/LLVM.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/MapVector.h"
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000017#include "llvm/Support/Allocator.h"
18#include "llvm/Support/MemoryBuffer.h"
Rui Ueyama708019c2016-07-24 18:19:40 +000019#include <functional>
Rui Ueyama717677a2016-02-11 21:17:59 +000020
21namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000022namespace elf {
Eugene Leviante63d81b2016-07-20 14:43:20 +000023template <class ELFT> class InputSectionBase;
24template <class ELFT> class OutputSectionBase;
25template <class ELFT> class OutputSectionFactory;
Rui Ueyama717677a2016-02-11 21:17:59 +000026
Rui Ueyama708019c2016-07-24 18:19:40 +000027typedef std::function<uint64_t(uint64_t)> Expr;
28
Rui Ueyama07320e42016-04-20 20:13:41 +000029// Parses a linker script. Calling this function updates
30// Config and ScriptConfig.
31void readLinkerScript(MemoryBufferRef MB);
32
Rui Ueyama717677a2016-02-11 21:17:59 +000033class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000034template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000035template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000036
George Rimareea31142016-07-21 14:26:59 +000037// This enum is used to implement linker script SECTIONS command.
38// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
39enum SectionsCommandKind {
40 AssignmentKind,
41 OutputSectionKind,
42 InputSectionKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000043};
44
George Rimar076fe152016-07-21 06:43:01 +000045struct BaseCommand {
46 BaseCommand(int K) : Kind(K) {}
47 virtual ~BaseCommand() {}
48 int Kind;
49};
50
51struct SymbolAssignment : BaseCommand {
Rui Ueyama708019c2016-07-24 18:19:40 +000052 SymbolAssignment(StringRef Name, Expr E)
53 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
George Rimar076fe152016-07-21 06:43:01 +000054 static bool classof(const BaseCommand *C);
55 StringRef Name;
Rui Ueyama708019c2016-07-24 18:19:40 +000056 Expr Expression;
Eugene Levianta31c91b2016-07-22 07:38:40 +000057 bool Provide = false;
58 // Hidden and Ignore can be true, only if Provide is true
59 bool Hidden = false;
60 bool Ignore = false;
George Rimar076fe152016-07-21 06:43:01 +000061};
62
Davide Italiano246f6812016-07-22 03:36:24 +000063// Linker scripts allow additional constraints to be put on ouput sections.
64// An output section will only be created if all of its input sections are
65// read-only
66// or all of its input sections are read-write by using the keyword ONLY_IF_RO
67// and ONLY_IF_RW respectively.
68enum ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
69
George Rimar076fe152016-07-21 06:43:01 +000070struct OutputSectionCommand : BaseCommand {
71 OutputSectionCommand(StringRef Name)
72 : BaseCommand(OutputSectionKind), Name(Name) {}
73 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000074 StringRef Name;
George Rimareea31142016-07-21 14:26:59 +000075 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000076 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000077 std::vector<uint8_t> Filler;
Davide Italiano246f6812016-07-22 03:36:24 +000078 ConstraintKind Constraint = NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000079};
80
George Rimareea31142016-07-21 14:26:59 +000081struct InputSectionDescription : BaseCommand {
82 InputSectionDescription() : BaseCommand(InputSectionKind) {}
83 static bool classof(const BaseCommand *C);
84 std::vector<StringRef> Patterns;
85};
86
Eugene Leviantbbe38602016-07-19 09:25:43 +000087struct PhdrsCommand {
88 StringRef Name;
89 unsigned Type;
90 bool HasFilehdr;
91 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +000092 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +000093};
94
Rui Ueyama07320e42016-04-20 20:13:41 +000095// ScriptConfiguration holds linker script parse results.
96struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +000097 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +000098 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +000099
Eugene Leviantbbe38602016-07-19 09:25:43 +0000100 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000101 std::vector<PhdrsCommand> PhdrsCommands;
102
Rui Ueyama07320e42016-04-20 20:13:41 +0000103 bool DoLayout = false;
104
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000105 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000106
107 // List of section patterns specified with KEEP commands. They will
108 // be kept even if they are unused and --gc-sections is specified.
109 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000110};
111
Rui Ueyama07320e42016-04-20 20:13:41 +0000112extern ScriptConfiguration *ScriptConfig;
113
114// This is a runner of the linker script.
115template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000116 typedef typename ELFT::uint uintX_t;
117
Rui Ueyama07320e42016-04-20 20:13:41 +0000118public:
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000119 std::vector<OutputSectionBase<ELFT> *>
120 createSections(OutputSectionFactory<ELFT> &Factory);
121
Rui Ueyamaadca2452016-07-23 14:18:48 +0000122 std::vector<PhdrEntry<ELFT>>
123 createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
124
Rui Ueyama07320e42016-04-20 20:13:41 +0000125 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000126 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000127 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000128 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000129 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000130 bool hasPhdrsCommands();
Rui Ueyama07320e42016-04-20 20:13:41 +0000131
132private:
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000133 // "ScriptConfig" is a bit too long, so define a short name for it.
134 ScriptConfiguration &Opt = *ScriptConfig;
135
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000136 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000137 std::vector<size_t> getPhdrIndices(StringRef SectionName);
George Rimar10e576e2016-07-21 16:07:40 +0000138 void dispatchAssignment(SymbolAssignment *Cmd);
Rui Ueyama07320e42016-04-20 20:13:41 +0000139
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000140 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000141};
142
143// Variable template is a C++14 feature, so we can't template
144// a global variable. Use a struct to workaround.
145template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
146template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000147
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000148} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000149} // namespace lld
150
151#endif