2015-10-01 01:23:26 +08:00
|
|
|
//===- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
#include "LinkerScript.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "Config.h"
|
2016-02-13 05:47:28 +08:00
|
|
|
#include "InputSection.h"
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "Memory.h"
|
2016-04-16 18:10:32 +08:00
|
|
|
#include "OutputSections.h"
|
2016-06-29 16:01:32 +08:00
|
|
|
#include "Strings.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-10-29 04:57:25 +08:00
|
|
|
#include "Symbols.h"
|
2016-11-30 00:05:27 +08:00
|
|
|
#include "SyntheticSections.h"
|
2016-07-19 17:25:43 +08:00
|
|
|
#include "Writer.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2016-04-16 18:10:32 +08:00
|
|
|
#include "llvm/Support/ELF.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-10-13 08:09:21 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-10-01 01:23:26 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-04-16 18:10:32 +08:00
|
|
|
using namespace llvm::ELF;
|
2016-02-13 05:47:28 +08:00
|
|
|
using namespace llvm::object;
|
2016-09-27 03:22:50 +08:00
|
|
|
using namespace llvm::support::endian;
|
2015-10-01 01:23:26 +08:00
|
|
|
using namespace lld;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-10-01 01:23:26 +08:00
|
|
|
|
2017-03-22 07:03:09 +08:00
|
|
|
LinkerScript *elf::Script;
|
|
|
|
|
2017-03-17 21:05:04 +08:00
|
|
|
uint64_t ExprValue::getValue() const {
|
2017-05-10 22:23:33 +08:00
|
|
|
if (Sec) {
|
|
|
|
if (Sec->getOutputSection())
|
|
|
|
return Sec->getOffset(Val) + Sec->getOutputSection()->Addr;
|
|
|
|
error("unable to evaluate expression: input section " + Sec->Name +
|
|
|
|
" has no output section assigned");
|
|
|
|
}
|
2017-03-17 21:05:04 +08:00
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:55:36 +08:00
|
|
|
uint64_t ExprValue::getSecAddr() const {
|
|
|
|
if (Sec)
|
|
|
|
return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-10 02:36:57 +08:00
|
|
|
template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
|
2017-02-22 06:32:51 +08:00
|
|
|
Symbol *Sym;
|
2016-10-31 21:14:53 +08:00
|
|
|
uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
2017-02-22 06:32:51 +08:00
|
|
|
std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
|
|
|
|
Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
|
|
|
|
/*File*/ nullptr);
|
|
|
|
Sym->Binding = STB_GLOBAL;
|
2017-03-17 21:05:04 +08:00
|
|
|
ExprValue Value = Cmd->Expression();
|
|
|
|
SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
|
2017-04-14 17:23:26 +08:00
|
|
|
|
|
|
|
// We want to set symbol values early if we can. This allows us to use symbols
|
|
|
|
// as variables in linker scripts. Doing so allows us to write expressions
|
|
|
|
// like this: `alignment = 16; . = ALIGN(., alignment)`
|
|
|
|
uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0;
|
2017-03-01 03:29:55 +08:00
|
|
|
replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility,
|
2017-04-14 17:23:26 +08:00
|
|
|
STT_NOTYPE, SymValue, 0, Sec, nullptr);
|
2017-01-10 02:36:57 +08:00
|
|
|
return Sym->body();
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
OutputSection *LinkerScript::getOutputSection(const Twine &Loc,
|
|
|
|
StringRef Name) {
|
2017-03-14 18:15:53 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections)
|
|
|
|
if (Sec->Name == Name)
|
|
|
|
return Sec;
|
|
|
|
|
2017-04-05 08:42:45 +08:00
|
|
|
static OutputSection Dummy("", 0, 0);
|
2017-03-17 21:05:04 +08:00
|
|
|
if (ErrorOnMissingSection)
|
|
|
|
error(Loc + ": undefined section " + Name);
|
2017-04-05 08:42:45 +08:00
|
|
|
return &Dummy;
|
2017-03-14 18:15:53 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 18:24:47 +08:00
|
|
|
// This function is essentially the same as getOutputSection(Name)->Size,
|
|
|
|
// but it won't print out an error message if a given section is not found.
|
|
|
|
//
|
|
|
|
// Linker script does not create an output section if its content is empty.
|
|
|
|
// We want to allow SIZEOF(.foo) where .foo is a section which happened to
|
|
|
|
// be empty. That is why this function is different from getOutputSection().
|
2017-03-22 07:02:51 +08:00
|
|
|
uint64_t LinkerScript::getOutputSectionSize(StringRef Name) {
|
2017-03-14 18:24:47 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections)
|
|
|
|
if (Sec->Name == Name)
|
|
|
|
return Sec->Size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
|
2017-03-17 21:05:04 +08:00
|
|
|
uint64_t Val = E().getValue();
|
2017-02-18 00:26:13 +08:00
|
|
|
if (Val < Dot) {
|
|
|
|
if (InSec)
|
2017-02-21 22:50:38 +08:00
|
|
|
error(Loc + ": unable to move location counter backward for: " +
|
|
|
|
CurOutSec->Name);
|
2017-02-18 00:26:13 +08:00
|
|
|
else
|
2017-02-21 22:50:38 +08:00
|
|
|
error(Loc + ": unable to move location counter backward");
|
2017-02-18 00:26:13 +08:00
|
|
|
}
|
|
|
|
Dot = Val;
|
|
|
|
// Update to location counter means update to section size.
|
|
|
|
if (InSec)
|
|
|
|
CurOutSec->Size = Dot - CurOutSec->Addr;
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:23:28 +08:00
|
|
|
// Sets value of a symbol. Two kinds of symbols are processed: synthetic
|
|
|
|
// symbols, whose value is an offset from beginning of section and regular
|
|
|
|
// symbols whose value is absolute.
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
|
2017-02-18 00:01:51 +08:00
|
|
|
if (Cmd->Name == ".") {
|
2017-02-21 22:50:38 +08:00
|
|
|
setDot(Cmd->Expression, Cmd->Location, InSec);
|
2017-02-18 00:01:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:23:28 +08:00
|
|
|
if (!Cmd->Sym)
|
2017-01-10 02:36:57 +08:00
|
|
|
return;
|
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
auto *Sym = cast<DefinedRegular>(Cmd->Sym);
|
2017-03-17 21:05:04 +08:00
|
|
|
ExprValue V = Cmd->Expression();
|
|
|
|
if (V.isAbsolute()) {
|
|
|
|
Sym->Value = V.getValue();
|
|
|
|
} else {
|
|
|
|
Sym->Section = V.Sec;
|
|
|
|
if (Sym->Section->Flags & SHF_ALLOC)
|
|
|
|
Sym->Value = V.Val;
|
|
|
|
else
|
|
|
|
Sym->Value = V.getValue();
|
2017-01-10 02:36:57 +08:00
|
|
|
}
|
2016-09-07 15:08:43 +08:00
|
|
|
}
|
2017-01-10 02:36:57 +08:00
|
|
|
|
2017-03-20 18:09:58 +08:00
|
|
|
static SymbolBody *findSymbol(StringRef S) {
|
|
|
|
switch (Config->EKind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
return Symtab<ELF32LE>::X->find(S);
|
|
|
|
case ELF32BEKind:
|
|
|
|
return Symtab<ELF32BE>::X->find(S);
|
|
|
|
case ELF64LEKind:
|
|
|
|
return Symtab<ELF64LE>::X->find(S);
|
|
|
|
case ELF64BEKind:
|
|
|
|
return Symtab<ELF64BE>::X->find(S);
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown Config->EKind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) {
|
|
|
|
switch (Config->EKind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
return addRegular<ELF32LE>(Cmd);
|
|
|
|
case ELF32BEKind:
|
|
|
|
return addRegular<ELF32BE>(Cmd);
|
|
|
|
case ELF64LEKind:
|
|
|
|
return addRegular<ELF64LE>(Cmd);
|
|
|
|
case ELF64BEKind:
|
|
|
|
return addRegular<ELF64BE>(Cmd);
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown Config->EKind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
|
2016-08-12 07:22:52 +08:00
|
|
|
if (Cmd->Name == ".")
|
2017-01-10 02:36:57 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If a symbol was in PROVIDE(), we need to define it only when
|
|
|
|
// it is a referenced undefined symbol.
|
2017-03-20 18:09:58 +08:00
|
|
|
SymbolBody *B = findSymbol(Cmd->Name);
|
2017-01-10 02:36:57 +08:00
|
|
|
if (Cmd->Provide && (!B || B->isDefined()))
|
|
|
|
return;
|
|
|
|
|
2017-03-20 18:09:58 +08:00
|
|
|
Cmd->Sym = addRegularSymbol(Cmd);
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
bool SymbolAssignment::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == AssignmentKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSectionCommand::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == OutputSectionKind;
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:26:59 +08:00
|
|
|
bool InputSectionDescription::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == InputSectionKind;
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:29:31 +08:00
|
|
|
bool AssertCommand::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == AssertKind;
|
|
|
|
}
|
|
|
|
|
2016-09-27 03:22:50 +08:00
|
|
|
bool BytesDataCommand::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == BytesDataKind;
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
static StringRef basename(InputSectionBase *S) {
|
|
|
|
if (S->File)
|
|
|
|
return sys::path::filename(S->File->getName());
|
2016-11-21 10:10:12 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
bool LinkerScript::shouldKeep(InputSectionBase *S) {
|
2016-11-21 10:10:12 +08:00
|
|
|
for (InputSectionDescription *ID : Opt.KeptSections)
|
|
|
|
if (ID->FilePat.match(basename(S)))
|
|
|
|
for (SectionPattern &P : ID->SectionPatterns)
|
|
|
|
if (P.SectionPat.match(S->Name))
|
|
|
|
return true;
|
2016-07-21 22:26:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:43:25 +08:00
|
|
|
// A helper function for the SORT() command.
|
2017-02-23 10:32:18 +08:00
|
|
|
static std::function<bool(InputSectionBase *, InputSectionBase *)>
|
2016-09-17 04:21:55 +08:00
|
|
|
getComparator(SortSectionPolicy K) {
|
|
|
|
switch (K) {
|
|
|
|
case SortSectionPolicy::Alignment:
|
2017-04-05 08:43:25 +08:00
|
|
|
return [](InputSectionBase *A, InputSectionBase *B) {
|
|
|
|
// ">" is not a mistake. Sections with larger alignments are placed
|
|
|
|
// before sections with smaller alignments in order to reduce the
|
|
|
|
// amount of padding necessary. This is compatible with GNU.
|
|
|
|
return A->Alignment > B->Alignment;
|
|
|
|
};
|
2016-09-17 04:21:55 +08:00
|
|
|
case SortSectionPolicy::Name:
|
2017-04-05 08:43:25 +08:00
|
|
|
return [](InputSectionBase *A, InputSectionBase *B) {
|
|
|
|
return A->Name < B->Name;
|
|
|
|
};
|
2016-09-17 04:21:55 +08:00
|
|
|
case SortSectionPolicy::Priority:
|
2017-04-05 08:43:25 +08:00
|
|
|
return [](InputSectionBase *A, InputSectionBase *B) {
|
|
|
|
return getPriority(A->Name) < getPriority(B->Name);
|
|
|
|
};
|
2016-09-17 04:21:55 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown sort policy");
|
|
|
|
}
|
2016-08-05 06:27:00 +08:00
|
|
|
}
|
2016-07-29 23:32:46 +08:00
|
|
|
|
2017-04-05 08:43:25 +08:00
|
|
|
// A helper function for the SORT() command.
|
2017-02-23 10:28:28 +08:00
|
|
|
static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
|
2016-08-12 17:07:57 +08:00
|
|
|
ConstraintKind Kind) {
|
2016-08-13 04:38:20 +08:00
|
|
|
if (Kind == ConstraintKind::NoConstraint)
|
|
|
|
return true;
|
2017-04-05 08:43:45 +08:00
|
|
|
|
|
|
|
bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
|
|
|
|
return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
|
2016-08-12 17:07:57 +08:00
|
|
|
});
|
2017-04-05 08:43:45 +08:00
|
|
|
|
2016-09-22 02:33:44 +08:00
|
|
|
return (IsRW && Kind == ConstraintKind::ReadWrite) ||
|
|
|
|
(!IsRW && Kind == ConstraintKind::ReadOnly);
|
2016-08-12 17:07:57 +08:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:32:18 +08:00
|
|
|
static void sortSections(InputSectionBase **Begin, InputSectionBase **End,
|
2016-09-21 03:42:41 +08:00
|
|
|
SortSectionPolicy K) {
|
|
|
|
if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
|
2016-09-21 23:56:44 +08:00
|
|
|
std::stable_sort(Begin, End, getComparator(K));
|
2016-09-21 03:42:41 +08:00
|
|
|
}
|
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
// Compute and remember which sections the InputSectionDescription matches.
|
2017-04-05 10:05:48 +08:00
|
|
|
std::vector<InputSectionBase *>
|
|
|
|
LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
|
|
|
|
std::vector<InputSectionBase *> Ret;
|
|
|
|
|
|
|
|
// Collects all sections that satisfy constraints of Cmd.
|
|
|
|
for (const SectionPattern &Pat : Cmd->SectionPatterns) {
|
|
|
|
size_t SizeBefore = Ret.size();
|
|
|
|
|
|
|
|
for (InputSectionBase *Sec : InputSections) {
|
|
|
|
if (Sec->Assigned)
|
2016-11-06 06:37:59 +08:00
|
|
|
continue;
|
2017-04-05 10:05:48 +08:00
|
|
|
|
2017-02-16 22:36:09 +08:00
|
|
|
// For -emit-relocs we have to ignore entries like
|
|
|
|
// .rela.dyn : { *(.rela.data) }
|
|
|
|
// which are common because they are in the default bfd script.
|
2017-04-05 10:05:48 +08:00
|
|
|
if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
|
2017-02-16 22:36:09 +08:00
|
|
|
continue;
|
2016-11-06 06:37:59 +08:00
|
|
|
|
2017-04-05 10:05:48 +08:00
|
|
|
StringRef Filename = basename(Sec);
|
|
|
|
if (!Cmd->FilePat.match(Filename) ||
|
|
|
|
Pat.ExcludedFilePat.match(Filename) ||
|
|
|
|
!Pat.SectionPat.match(Sec->Name))
|
2016-11-21 10:10:12 +08:00
|
|
|
continue;
|
2017-04-05 10:05:48 +08:00
|
|
|
|
|
|
|
Ret.push_back(Sec);
|
|
|
|
Sec->Assigned = true;
|
2016-09-17 01:42:10 +08:00
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
|
2016-09-21 23:56:44 +08:00
|
|
|
// Sort sections as instructed by SORT-family commands and --sort-section
|
|
|
|
// option. Because SORT-family commands can be nested at most two depth
|
|
|
|
// (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
|
|
|
|
// line option is respected even if a SORT command is given, the exact
|
|
|
|
// behavior we have here is a bit complicated. Here are the rules.
|
|
|
|
//
|
|
|
|
// 1. If two SORT commands are given, --sort-section is ignored.
|
|
|
|
// 2. If one SORT command is given, and if it is not SORT_NONE,
|
|
|
|
// --sort-section is handled as an inner SORT command.
|
|
|
|
// 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
|
|
|
|
// 4. If no SORT command is given, sort according to --sort-section.
|
2017-04-05 10:05:48 +08:00
|
|
|
InputSectionBase **Begin = Ret.data() + SizeBefore;
|
|
|
|
InputSectionBase **End = Ret.data() + Ret.size();
|
2016-09-21 23:56:44 +08:00
|
|
|
if (Pat.SortOuter != SortSectionPolicy::None) {
|
|
|
|
if (Pat.SortInner == SortSectionPolicy::Default)
|
|
|
|
sortSections(Begin, End, Config->SortSection);
|
|
|
|
else
|
|
|
|
sortSections(Begin, End, Pat.SortInner);
|
|
|
|
sortSections(Begin, End, Pat.SortOuter);
|
|
|
|
}
|
2016-09-21 03:42:41 +08:00
|
|
|
}
|
2017-04-05 10:05:48 +08:00
|
|
|
return Ret;
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *S : V) {
|
2016-09-14 22:32:08 +08:00
|
|
|
S->Live = false;
|
2017-03-15 23:42:44 +08:00
|
|
|
if (S == InX::ShStrTab)
|
2017-02-18 01:35:07 +08:00
|
|
|
error("discarding .shstrtab section is not allowed");
|
2017-02-18 03:34:05 +08:00
|
|
|
discard(S->DependentSections);
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
std::vector<InputSectionBase *>
|
2017-03-22 07:02:51 +08:00
|
|
|
LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) {
|
2017-02-23 10:28:28 +08:00
|
|
|
std::vector<InputSectionBase *> Ret;
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : OutCmd.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<InputSectionDescription>(Base);
|
2016-09-17 05:05:36 +08:00
|
|
|
if (!Cmd)
|
2016-08-12 11:16:56 +08:00
|
|
|
continue;
|
2017-04-05 10:05:48 +08:00
|
|
|
|
|
|
|
Cmd->Sections = computeInputSections(Cmd);
|
2017-04-08 00:10:46 +08:00
|
|
|
Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
|
2016-08-12 11:16:56 +08:00
|
|
|
}
|
2016-09-17 04:34:02 +08:00
|
|
|
|
2016-08-12 11:16:56 +08:00
|
|
|
return Ret;
|
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::processCommands(OutputSectionFactory &Factory) {
|
2017-03-09 06:36:28 +08:00
|
|
|
// A symbol can be assigned before any section is mentioned in the linker
|
|
|
|
// script. In an DSO, the symbol values are addresses, so the only important
|
|
|
|
// section values are:
|
|
|
|
// * SHN_UNDEF
|
|
|
|
// * SHN_ABS
|
|
|
|
// * Any value meaning a regular section.
|
|
|
|
// To handle that, create a dummy aether section that fills the void before
|
|
|
|
// the linker scripts switches to another section. It has an index of one
|
|
|
|
// which will map to whatever the first actual section is.
|
|
|
|
Aether = make<OutputSection>("", 0, SHF_ALLOC);
|
|
|
|
Aether->SectionIndex = 1;
|
|
|
|
CurOutSec = Aether;
|
2017-03-20 22:33:33 +08:00
|
|
|
Dot = 0;
|
2017-03-09 06:36:28 +08:00
|
|
|
|
2017-04-06 00:07:44 +08:00
|
|
|
for (size_t I = 0; I < Opt.Commands.size(); ++I) {
|
2016-11-21 10:11:05 +08:00
|
|
|
// Handle symbol assignments outside of any output section.
|
2017-04-06 00:07:44 +08:00
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
|
2017-02-18 00:01:51 +08:00
|
|
|
addSymbol(Cmd);
|
2016-08-12 11:33:04 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2017-04-06 00:07:44 +08:00
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) {
|
2017-02-23 10:28:28 +08:00
|
|
|
std::vector<InputSectionBase *> V = createInputSectionList(*Cmd);
|
2016-09-13 00:05:16 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// The output section name `/DISCARD/' is special.
|
|
|
|
// Any input section assigned to it is discarded.
|
2016-08-12 08:27:23 +08:00
|
|
|
if (Cmd->Name == "/DISCARD/") {
|
2016-09-13 00:05:16 +08:00
|
|
|
discard(V);
|
2016-08-12 08:27:23 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
|
|
|
|
// ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
|
|
|
|
// sections satisfy a given constraint. If not, a directive is handled
|
2017-04-05 17:19:29 +08:00
|
|
|
// as if it wasn't present from the beginning.
|
2016-11-21 10:11:05 +08:00
|
|
|
//
|
|
|
|
// Because we'll iterate over Commands many more times, the easiest
|
2017-04-05 17:19:29 +08:00
|
|
|
// way to "make it as if it wasn't present" is to just remove it.
|
2017-03-14 19:23:33 +08:00
|
|
|
if (!matchConstraints(V, Cmd->Constraint)) {
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *S : V)
|
2016-11-21 07:15:52 +08:00
|
|
|
S->Assigned = false;
|
2017-04-06 00:07:44 +08:00
|
|
|
Opt.Commands.erase(Opt.Commands.begin() + I);
|
2017-04-05 17:19:29 +08:00
|
|
|
--I;
|
2016-09-17 05:05:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// A directive may contain symbol definitions like this:
|
|
|
|
// ".foo : { ...; bar = .; }". Handle them.
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Cmd->Commands)
|
|
|
|
if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
|
2017-02-18 00:01:51 +08:00
|
|
|
addSymbol(OutCmd);
|
2016-09-17 05:05:36 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
|
|
|
|
// is given, input sections are aligned to that value, whether the
|
|
|
|
// given value is larger or smaller than the original section alignment.
|
|
|
|
if (Cmd->SubalignExpr) {
|
2017-03-17 21:05:04 +08:00
|
|
|
uint32_t Subalign = Cmd->SubalignExpr().getValue();
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *S : V)
|
2016-11-21 10:11:05 +08:00
|
|
|
S->Alignment = Subalign;
|
2016-08-19 23:18:23 +08:00
|
|
|
}
|
2016-11-21 10:11:05 +08:00
|
|
|
|
|
|
|
// Add input sections to an output section.
|
2017-05-08 18:18:12 +08:00
|
|
|
for (InputSectionBase *S : V)
|
2017-04-27 06:30:15 +08:00
|
|
|
Factory.addInputSec(S, Cmd->Name, Cmd->Sec);
|
2017-05-06 05:34:26 +08:00
|
|
|
if (OutputSection *Sec = Cmd->Sec) {
|
|
|
|
assert(Sec->SectionIndex == INT_MAX);
|
|
|
|
Sec->SectionIndex = I;
|
2017-05-11 03:13:38 +08:00
|
|
|
SecToCommand[Sec] = Cmd;
|
2017-05-06 05:34:26 +08:00
|
|
|
}
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
2017-03-09 06:36:28 +08:00
|
|
|
CurOutSec = nullptr;
|
2016-09-16 23:30:47 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
|
2017-05-05 03:34:17 +08:00
|
|
|
void LinkerScript::fabricateDefaultCommands() {
|
2017-04-19 20:46:32 +08:00
|
|
|
std::vector<BaseCommand *> Commands;
|
|
|
|
|
|
|
|
// Define start address
|
2017-05-05 03:34:17 +08:00
|
|
|
uint64_t StartAddr = Config->ImageBase + elf::getHeaderSize();
|
2017-04-19 20:46:32 +08:00
|
|
|
|
2017-05-03 16:44:50 +08:00
|
|
|
// The Sections with -T<section> have been sorted in order of ascending
|
|
|
|
// address. We must lower StartAddr if the lowest -T<section address> as
|
|
|
|
// calls to setDot() must be monotonically increasing.
|
|
|
|
for (auto& KV : Config->SectionStartMap)
|
|
|
|
StartAddr = std::min(StartAddr, KV.second);
|
|
|
|
|
2017-04-19 20:46:32 +08:00
|
|
|
Commands.push_back(
|
|
|
|
make<SymbolAssignment>(".", [=] { return StartAddr; }, ""));
|
|
|
|
|
|
|
|
// For each OutputSection that needs a VA fabricate an OutputSectionCommand
|
|
|
|
// with an InputSectionDescription describing the InputSections
|
|
|
|
for (OutputSection *Sec : *OutputSections) {
|
2017-05-03 16:44:50 +08:00
|
|
|
auto *OSCmd = make<OutputSectionCommand>(Sec->Name);
|
|
|
|
OSCmd->Sec = Sec;
|
2017-05-11 03:13:38 +08:00
|
|
|
SecToCommand[Sec] = OSCmd;
|
2017-05-03 16:44:50 +08:00
|
|
|
|
|
|
|
// Prefer user supplied address over additional alignment constraint
|
2017-04-19 20:46:32 +08:00
|
|
|
auto I = Config->SectionStartMap.find(Sec->Name);
|
|
|
|
if (I != Config->SectionStartMap.end())
|
|
|
|
Commands.push_back(
|
|
|
|
make<SymbolAssignment>(".", [=] { return I->second; }, ""));
|
2017-05-03 16:44:50 +08:00
|
|
|
else if (Sec->PageAlign)
|
2017-04-19 20:46:32 +08:00
|
|
|
OSCmd->AddrExpr = [=] {
|
|
|
|
return alignTo(Script->getDot(), Config->MaxPageSize);
|
|
|
|
};
|
2017-05-03 16:44:50 +08:00
|
|
|
|
2017-04-19 20:46:32 +08:00
|
|
|
Commands.push_back(OSCmd);
|
|
|
|
if (Sec->Sections.size()) {
|
|
|
|
auto *ISD = make<InputSectionDescription>("");
|
|
|
|
OSCmd->Commands.push_back(ISD);
|
|
|
|
for (InputSection *ISec : Sec->Sections) {
|
|
|
|
ISD->Sections.push_back(ISec);
|
|
|
|
ISec->Assigned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// SECTIONS commands run before other non SECTIONS commands
|
|
|
|
Commands.insert(Commands.end(), Opt.Commands.begin(), Opt.Commands.end());
|
|
|
|
Opt.Commands = std::move(Commands);
|
|
|
|
}
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Add sections that didn't match any sections command.
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
|
2017-04-27 06:30:15 +08:00
|
|
|
for (InputSectionBase *S : InputSections) {
|
|
|
|
if (!S->Live || S->OutSec)
|
|
|
|
continue;
|
|
|
|
StringRef Name = getOutputSectionName(S->Name);
|
|
|
|
auto I = std::find_if(
|
|
|
|
Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
|
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
|
|
|
|
return Cmd->Name == Name;
|
|
|
|
return false;
|
|
|
|
});
|
2017-04-29 23:44:03 +08:00
|
|
|
if (I == Opt.Commands.end()) {
|
2017-04-27 06:30:15 +08:00
|
|
|
Factory.addInputSec(S, Name);
|
2017-04-29 23:44:03 +08:00
|
|
|
} else {
|
|
|
|
auto *Cmd = cast<OutputSectionCommand>(*I);
|
|
|
|
Factory.addInputSec(S, Name, Cmd->Sec);
|
2017-05-06 05:34:26 +08:00
|
|
|
if (OutputSection *Sec = Cmd->Sec) {
|
2017-05-11 03:13:38 +08:00
|
|
|
SecToCommand[Sec] = Cmd;
|
2017-05-06 05:34:26 +08:00
|
|
|
unsigned Index = std::distance(Opt.Commands.begin(), I);
|
|
|
|
assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
|
|
|
|
Sec->SectionIndex = Index;
|
|
|
|
}
|
2017-04-29 23:44:03 +08:00
|
|
|
auto *ISD = make<InputSectionDescription>("");
|
|
|
|
ISD->Sections.push_back(S);
|
|
|
|
Cmd->Commands.push_back(ISD);
|
|
|
|
}
|
2017-04-27 06:30:15 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 11:00:27 +08:00
|
|
|
uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
|
|
|
|
bool IsTbss = (CurOutSec->Flags & SHF_TLS) && CurOutSec->Type == SHT_NOBITS;
|
|
|
|
uint64_t Start = IsTbss ? Dot + ThreadBssOffset : Dot;
|
|
|
|
Start = alignTo(Start, Align);
|
|
|
|
uint64_t End = Start + Size;
|
|
|
|
|
|
|
|
if (IsTbss)
|
|
|
|
ThreadBssOffset = End - Dot;
|
|
|
|
else
|
|
|
|
Dot = End;
|
|
|
|
return End;
|
2016-09-22 20:35:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::output(InputSection *S) {
|
2017-05-04 11:00:27 +08:00
|
|
|
uint64_t Pos = advance(S->getSize(), S->Alignment);
|
|
|
|
S->OutSecOff = Pos - S->getSize() - CurOutSec->Addr;
|
2016-09-16 23:10:23 +08:00
|
|
|
|
|
|
|
// Update output section size after adding each section. This is so that
|
|
|
|
// SIZEOF works correctly in the case below:
|
|
|
|
// .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
|
2016-11-09 09:42:41 +08:00
|
|
|
CurOutSec->Size = Pos - CurOutSec->Addr;
|
2016-09-16 23:10:23 +08:00
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
// If there is a memory region associated with this input section, then
|
|
|
|
// place the section in that region and update the region index.
|
|
|
|
if (CurMemRegion) {
|
|
|
|
CurMemRegion->Offset += CurOutSec->Size;
|
|
|
|
uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin;
|
|
|
|
if (CurSize > CurMemRegion->Length) {
|
|
|
|
uint64_t OverflowAmt = CurSize - CurMemRegion->Length;
|
|
|
|
error("section '" + CurOutSec->Name + "' will not fit in region '" +
|
|
|
|
CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
|
|
|
|
" bytes");
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::switchTo(OutputSection *Sec) {
|
2016-09-16 23:10:23 +08:00
|
|
|
if (CurOutSec == Sec)
|
|
|
|
return;
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
CurOutSec = Sec;
|
2017-05-04 11:00:27 +08:00
|
|
|
CurOutSec->Addr = advance(0, CurOutSec->Alignment);
|
2016-10-06 17:39:28 +08:00
|
|
|
|
|
|
|
// If neither AT nor AT> is specified for an allocatable section, the linker
|
|
|
|
// will set the LMA such that the difference between VMA and LMA for the
|
|
|
|
// section is the same as the preceding output section in the same region
|
|
|
|
// https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
|
2017-02-23 15:57:55 +08:00
|
|
|
if (LMAOffset)
|
2017-02-24 22:34:12 +08:00
|
|
|
CurOutSec->LMAOffset = LMAOffset();
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
2016-09-01 17:55:57 +08:00
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::process(BaseCommand &Base) {
|
2017-04-05 11:18:46 +08:00
|
|
|
// This handles the assignments to symbol or to the dot.
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
|
|
|
|
assignSymbol(Cmd, true);
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-09-27 03:22:50 +08:00
|
|
|
|
|
|
|
// Handle BYTE(), SHORT(), LONG(), or QUAD().
|
2017-04-05 11:18:46 +08:00
|
|
|
if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
|
|
|
|
Cmd->Offset = Dot - CurOutSec->Addr;
|
|
|
|
Dot += Cmd->Size;
|
2016-11-09 09:42:41 +08:00
|
|
|
CurOutSec->Size = Dot - CurOutSec->Addr;
|
2016-09-27 03:22:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:18:46 +08:00
|
|
|
// Handle ASSERT().
|
|
|
|
if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
|
|
|
|
Cmd->Expression();
|
2016-11-23 02:01:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:18:46 +08:00
|
|
|
// Handle a single input section description command.
|
|
|
|
// It calculates and assigns the offsets for each section and also
|
2016-09-27 03:22:50 +08:00
|
|
|
// updates the output section size.
|
2017-04-05 11:18:46 +08:00
|
|
|
auto &Cmd = cast<InputSectionDescription>(Base);
|
|
|
|
for (InputSectionBase *Sec : Cmd.Sections) {
|
2016-11-30 00:05:27 +08:00
|
|
|
// We tentatively added all synthetic sections at the beginning and removed
|
|
|
|
// empty ones afterwards (because there is no way to know whether they were
|
|
|
|
// going be empty or not other than actually running linker scripts.)
|
|
|
|
// We need to ignore remains of empty sections.
|
2017-04-05 11:18:46 +08:00
|
|
|
if (auto *S = dyn_cast<SyntheticSection>(Sec))
|
|
|
|
if (S->empty())
|
2016-11-30 00:05:27 +08:00
|
|
|
continue;
|
|
|
|
|
2017-04-05 11:18:46 +08:00
|
|
|
if (!Sec->Live)
|
2017-02-21 23:46:43 +08:00
|
|
|
continue;
|
2017-04-29 23:44:03 +08:00
|
|
|
assert(CurOutSec == Sec->OutSec);
|
2017-04-05 11:18:46 +08:00
|
|
|
output(cast<InputSection>(Sec));
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
// This function searches for a memory region to place the given output
|
|
|
|
// section in. If found, a pointer to the appropriate memory region is
|
|
|
|
// returned. Otherwise, a nullptr is returned.
|
2017-04-07 05:26:03 +08:00
|
|
|
MemoryRegion *LinkerScript::findMemoryRegion(OutputSectionCommand *Cmd) {
|
2017-01-24 10:34:00 +08:00
|
|
|
// If a memory region name was specified in the output section command,
|
|
|
|
// then try to find that region first.
|
|
|
|
if (!Cmd->MemoryRegionName.empty()) {
|
|
|
|
auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName);
|
|
|
|
if (It != Opt.MemoryRegions.end())
|
|
|
|
return &It->second;
|
|
|
|
error("memory region '" + Cmd->MemoryRegionName + "' not declared");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:19:43 +08:00
|
|
|
// If at least one memory region is defined, all sections must
|
|
|
|
// belong to some memory region. Otherwise, we don't need to do
|
|
|
|
// anything for memory regions.
|
2017-04-05 11:19:24 +08:00
|
|
|
if (Opt.MemoryRegions.empty())
|
2017-01-24 10:34:00 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2017-04-07 05:26:03 +08:00
|
|
|
OutputSection *Sec = Cmd->Sec;
|
2017-01-24 10:34:00 +08:00
|
|
|
// See if a region can be found by matching section flags.
|
2017-04-05 11:18:46 +08:00
|
|
|
for (auto &Pair : Opt.MemoryRegions) {
|
|
|
|
MemoryRegion &M = Pair.second;
|
|
|
|
if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0)
|
|
|
|
return &M;
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, no suitable region was found.
|
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
|
|
|
error("no memory region specified for section '" + Sec->Name + "'");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// This function assigns offsets to input sections and an output section
|
|
|
|
// for a single sections command (e.g. ".text { *(.text); }").
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) {
|
2017-04-07 05:05:39 +08:00
|
|
|
OutputSection *Sec = Cmd->Sec;
|
2017-02-04 06:27:05 +08:00
|
|
|
if (!Sec)
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
2017-01-24 10:34:00 +08:00
|
|
|
|
2017-04-05 11:20:03 +08:00
|
|
|
if (Cmd->AddrExpr && (Sec->Flags & SHF_ALLOC))
|
2017-04-05 11:20:22 +08:00
|
|
|
setDot(Cmd->AddrExpr, Cmd->Location, false);
|
2017-02-18 00:26:13 +08:00
|
|
|
|
2017-03-14 16:57:09 +08:00
|
|
|
if (Cmd->LMAExpr) {
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t D = Dot;
|
2017-03-17 21:05:04 +08:00
|
|
|
LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; };
|
2017-03-14 16:57:09 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 05:31:24 +08:00
|
|
|
CurMemRegion = Cmd->MemRegion;
|
2017-01-24 10:34:00 +08:00
|
|
|
if (CurMemRegion)
|
|
|
|
Dot = CurMemRegion->Offset;
|
|
|
|
switchTo(Sec);
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2017-05-08 18:18:12 +08:00
|
|
|
// We do not support custom layout for compressed debug sectons.
|
|
|
|
// At this point we already know their size and have compressed content.
|
|
|
|
if (CurOutSec->Flags & SHF_COMPRESSED)
|
|
|
|
return;
|
|
|
|
|
2017-04-29 23:44:03 +08:00
|
|
|
for (BaseCommand *C : Cmd->Commands)
|
|
|
|
process(*C);
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::removeEmptyCommands() {
|
2016-09-20 21:12:07 +08:00
|
|
|
// It is common practice to use very generic linker scripts. So for any
|
|
|
|
// given run some of the output sections in the script will be empty.
|
|
|
|
// We could create corresponding empty output sections, but that would
|
|
|
|
// clutter the output.
|
|
|
|
// We instead remove trivially empty sections. The bfd linker seems even
|
|
|
|
// more aggressive at removing them.
|
|
|
|
auto Pos = std::remove_if(
|
2017-04-05 11:20:42 +08:00
|
|
|
Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
|
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
|
2017-04-27 06:30:15 +08:00
|
|
|
return std::find(OutputSections->begin(), OutputSections->end(),
|
|
|
|
Cmd->Sec) == OutputSections->end();
|
2016-11-21 10:11:05 +08:00
|
|
|
return false;
|
2016-09-20 21:12:07 +08:00
|
|
|
});
|
|
|
|
Opt.Commands.erase(Pos, Opt.Commands.end());
|
2016-11-14 22:23:35 +08:00
|
|
|
}
|
|
|
|
|
2016-11-14 22:33:49 +08:00
|
|
|
static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Cmd.Commands)
|
|
|
|
if (!isa<InputSectionDescription>(*Base))
|
2016-11-14 22:33:49 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-20 21:12:07 +08:00
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::adjustSectionsBeforeSorting() {
|
2016-09-22 22:40:50 +08:00
|
|
|
// If the output section contains only symbol assignments, create a
|
|
|
|
// corresponding output section. The bfd linker seems to only create them if
|
|
|
|
// '.' is assigned to, but creating these section should not have any bad
|
|
|
|
// consequeces and gives us a section to put the symbol in.
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t Flags = SHF_ALLOC;
|
2017-04-14 17:37:00 +08:00
|
|
|
uint32_t Type = SHT_PROGBITS;
|
2017-05-06 05:34:26 +08:00
|
|
|
|
|
|
|
for (int I = 0, E = Opt.Commands.size(); I != E; ++I) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]);
|
2016-09-22 22:40:50 +08:00
|
|
|
if (!Cmd)
|
|
|
|
continue;
|
2017-04-27 06:30:15 +08:00
|
|
|
if (OutputSection *Sec = Cmd->Sec) {
|
2017-02-04 06:27:05 +08:00
|
|
|
Flags = Sec->Flags;
|
|
|
|
Type = Sec->Type;
|
2016-09-22 22:40:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-14 22:33:49 +08:00
|
|
|
if (isAllSectionDescription(*Cmd))
|
|
|
|
continue;
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags);
|
2017-05-06 05:34:26 +08:00
|
|
|
OutSec->SectionIndex = I;
|
2016-09-22 22:40:50 +08:00
|
|
|
OutputSections->push_back(OutSec);
|
2017-04-07 05:05:39 +08:00
|
|
|
Cmd->Sec = OutSec;
|
2017-05-11 03:13:38 +08:00
|
|
|
SecToCommand[OutSec] = Cmd;
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
2016-11-14 23:39:38 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::adjustSectionsAfterSorting() {
|
2016-11-14 23:39:38 +08:00
|
|
|
placeOrphanSections();
|
|
|
|
|
2017-04-07 05:31:24 +08:00
|
|
|
// Try and find an appropriate memory region to assign offsets in.
|
2017-04-07 05:40:22 +08:00
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) {
|
2017-04-07 05:31:24 +08:00
|
|
|
Cmd->MemRegion = findMemoryRegion(Cmd);
|
2017-04-07 05:40:22 +08:00
|
|
|
// Handle align (e.g. ".foo : ALIGN(16) { ... }").
|
|
|
|
if (Cmd->AlignExpr)
|
|
|
|
Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue());
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 05:31:24 +08:00
|
|
|
|
2016-11-14 23:39:38 +08:00
|
|
|
// If output section command doesn't specify any segments,
|
|
|
|
// and we haven't previously assigned any section to segment,
|
|
|
|
// then we simply assign section to the very first load segment.
|
|
|
|
// Below is an example of such linker script:
|
|
|
|
// PHDRS { seg PT_LOAD; }
|
|
|
|
// SECTIONS { .aaa : { *(.aaa) } }
|
|
|
|
std::vector<StringRef> DefPhdrs;
|
|
|
|
auto FirstPtLoad =
|
|
|
|
std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
|
|
|
|
[](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
|
|
|
|
if (FirstPtLoad != Opt.PhdrsCommands.end())
|
|
|
|
DefPhdrs.push_back(FirstPtLoad->Name);
|
|
|
|
|
|
|
|
// Walk the commands and propagate the program headers to commands that don't
|
|
|
|
// explicitly specify them.
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
|
2016-11-14 23:39:38 +08:00
|
|
|
if (!Cmd)
|
|
|
|
continue;
|
2017-04-05 11:20:42 +08:00
|
|
|
|
2016-11-14 23:39:38 +08:00
|
|
|
if (Cmd->Phdrs.empty())
|
|
|
|
Cmd->Phdrs = DefPhdrs;
|
|
|
|
else
|
|
|
|
DefPhdrs = Cmd->Phdrs;
|
|
|
|
}
|
2016-11-14 22:33:49 +08:00
|
|
|
|
|
|
|
removeEmptyCommands();
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 02:05:49 +08:00
|
|
|
// When placing orphan sections, we want to place them after symbol assignments
|
|
|
|
// so that an orphan after
|
|
|
|
// begin_foo = .;
|
|
|
|
// foo : { *(foo) }
|
|
|
|
// end_foo = .;
|
|
|
|
// doesn't break the intended meaning of the begin/end symbols.
|
|
|
|
// We don't want to go over sections since Writer<ELFT>::sortSections is the
|
|
|
|
// one in charge of deciding the order of the sections.
|
|
|
|
// We don't want to go over alignments, since doing so in
|
|
|
|
// rx_sec : { *(rx_sec) }
|
|
|
|
// . = ALIGN(0x1000);
|
|
|
|
// /* The RW PT_LOAD starts here*/
|
|
|
|
// rw_sec : { *(rw_sec) }
|
|
|
|
// would mean that the RW PT_LOAD would become unaligned.
|
2017-04-05 11:52:28 +08:00
|
|
|
static bool shouldSkip(BaseCommand *Cmd) {
|
2016-09-23 02:05:49 +08:00
|
|
|
if (isa<OutputSectionCommand>(Cmd))
|
|
|
|
return false;
|
2017-04-05 11:52:28 +08:00
|
|
|
if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
|
|
|
|
return Assign->Name != ".";
|
|
|
|
return true;
|
2016-09-23 02:05:49 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 07:26:12 +08:00
|
|
|
// Orphan sections are sections present in the input files which are
|
|
|
|
// not explicitly placed into the output file by the linker script.
|
|
|
|
//
|
|
|
|
// When the control reaches this function, Opt.Commands contains
|
|
|
|
// output section commands for non-orphan sections only. This function
|
2017-03-24 08:15:57 +08:00
|
|
|
// adds new elements for orphan sections so that all sections are
|
|
|
|
// explicitly handled by Opt.Commands.
|
2017-02-03 07:26:12 +08:00
|
|
|
//
|
|
|
|
// Writer<ELFT>::sortSections has already sorted output sections.
|
|
|
|
// What we need to do is to scan OutputSections vector and
|
|
|
|
// Opt.Commands in parallel to find orphan sections. If there is an
|
|
|
|
// output section that doesn't have a corresponding entry in
|
|
|
|
// Opt.Commands, we will insert a new entry to Opt.Commands.
|
|
|
|
//
|
|
|
|
// There is some ambiguity as to where exactly a new entry should be
|
|
|
|
// inserted, because Opt.Commands contains not only output section
|
2017-03-24 08:15:57 +08:00
|
|
|
// commands but also other types of commands such as symbol assignment
|
2017-02-03 07:26:12 +08:00
|
|
|
// expressions. There's no correct answer here due to the lack of the
|
|
|
|
// formal specification of the linker script. We use heuristics to
|
|
|
|
// determine whether a new output command should be added before or
|
|
|
|
// after another commands. For the details, look at shouldSkip
|
|
|
|
// function.
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::placeOrphanSections() {
|
2016-09-17 05:29:07 +08:00
|
|
|
// The OutputSections are already in the correct order.
|
|
|
|
// This loops creates or moves commands as needed so that they are in the
|
|
|
|
// correct order.
|
|
|
|
int CmdIndex = 0;
|
2016-11-27 17:44:45 +08:00
|
|
|
|
|
|
|
// As a horrible special case, skip the first . assignment if it is before any
|
|
|
|
// section. We do this because it is common to set a load address by starting
|
|
|
|
// the script with ". = 0xabcd" and the expectation is that every section is
|
|
|
|
// after that.
|
2017-04-05 11:52:28 +08:00
|
|
|
auto FirstSectionOrDotAssignment =
|
|
|
|
std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
|
|
|
|
[](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
|
2016-11-27 17:44:45 +08:00
|
|
|
if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
|
|
|
|
CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
|
|
|
|
if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
|
|
|
|
++CmdIndex;
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections) {
|
2017-02-24 22:28:00 +08:00
|
|
|
StringRef Name = Sec->Name;
|
2016-09-17 05:29:07 +08:00
|
|
|
|
|
|
|
// Find the last spot where we can insert a command and still get the
|
2016-09-23 02:05:49 +08:00
|
|
|
// correct result.
|
2016-09-17 05:29:07 +08:00
|
|
|
auto CmdIter = Opt.Commands.begin() + CmdIndex;
|
|
|
|
auto E = Opt.Commands.end();
|
2017-04-05 11:52:28 +08:00
|
|
|
while (CmdIter != E && shouldSkip(*CmdIter)) {
|
2016-09-17 05:29:07 +08:00
|
|
|
++CmdIter;
|
|
|
|
++CmdIndex;
|
|
|
|
}
|
|
|
|
|
2017-04-29 23:44:03 +08:00
|
|
|
// If there is no command corresponding to this output section,
|
|
|
|
// create one and put a InputSectionDescription in it so that both
|
|
|
|
// representations agree on which input sections to use.
|
2017-05-11 03:00:23 +08:00
|
|
|
OutputSectionCommand *Cmd = getCmd(Sec);
|
|
|
|
if (!Cmd) {
|
|
|
|
Cmd = make<OutputSectionCommand>(Name);
|
2017-04-07 05:05:39 +08:00
|
|
|
Opt.Commands.insert(CmdIter, Cmd);
|
2016-09-23 02:05:49 +08:00
|
|
|
++CmdIndex;
|
2017-04-29 23:44:03 +08:00
|
|
|
|
|
|
|
Cmd->Sec = Sec;
|
2017-05-11 03:13:38 +08:00
|
|
|
SecToCommand[Sec] = Cmd;
|
2017-04-29 23:44:03 +08:00
|
|
|
auto *ISD = make<InputSectionDescription>("");
|
|
|
|
for (InputSection *IS : Sec->Sections)
|
|
|
|
ISD->Sections.push_back(IS);
|
|
|
|
Cmd->Commands.push_back(ISD);
|
|
|
|
|
2016-09-23 02:05:49 +08:00
|
|
|
continue;
|
2016-09-17 05:29:07 +08:00
|
|
|
}
|
2016-09-23 02:05:49 +08:00
|
|
|
|
|
|
|
// Continue from where we found it.
|
2017-05-11 03:00:23 +08:00
|
|
|
while (*CmdIter != Cmd) {
|
|
|
|
++CmdIter;
|
|
|
|
++CmdIndex;
|
|
|
|
}
|
|
|
|
++CmdIndex;
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2016-11-14 22:13:32 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::processNonSectionCommands() {
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
|
2017-04-05 11:20:22 +08:00
|
|
|
assignSymbol(Cmd, false);
|
2017-04-05 11:20:42 +08:00
|
|
|
else if (auto *Cmd = dyn_cast<AssertCommand>(Base))
|
2017-03-15 11:33:23 +08:00
|
|
|
Cmd->Expression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 23:44:03 +08:00
|
|
|
// Do a last effort at synchronizing the linker script "AST" and the section
|
|
|
|
// list. This is needed to account for last minute changes, like adding a
|
|
|
|
// .ARM.exidx terminator and sorting SHF_LINK_ORDER sections.
|
|
|
|
//
|
|
|
|
// FIXME: We should instead create the "AST" earlier and the above changes would
|
|
|
|
// be done directly in the "AST".
|
|
|
|
//
|
|
|
|
// This can only handle new sections being added and sections being reordered.
|
|
|
|
void LinkerScript::synchronize() {
|
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
|
|
|
|
if (!Cmd)
|
|
|
|
continue;
|
|
|
|
ArrayRef<InputSection *> Sections = Cmd->Sec->Sections;
|
|
|
|
std::vector<InputSectionBase **> ScriptSections;
|
|
|
|
DenseSet<InputSectionBase *> ScriptSectionsSet;
|
|
|
|
for (BaseCommand *Base : Cmd->Commands) {
|
|
|
|
auto *ISD = dyn_cast<InputSectionDescription>(Base);
|
|
|
|
if (!ISD)
|
|
|
|
continue;
|
|
|
|
for (InputSectionBase *&IS : ISD->Sections) {
|
|
|
|
if (IS->Live) {
|
|
|
|
ScriptSections.push_back(&IS);
|
|
|
|
ScriptSectionsSet.insert(IS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<InputSectionBase *> Missing;
|
|
|
|
for (InputSection *IS : Sections)
|
|
|
|
if (!ScriptSectionsSet.count(IS))
|
|
|
|
Missing.push_back(IS);
|
|
|
|
if (!Missing.empty()) {
|
|
|
|
auto ISD = make<InputSectionDescription>("");
|
|
|
|
ISD->Sections = Missing;
|
|
|
|
Cmd->Commands.push_back(ISD);
|
|
|
|
for (InputSectionBase *&IS : ISD->Sections)
|
|
|
|
if (IS->Live)
|
|
|
|
ScriptSections.push_back(&IS);
|
|
|
|
}
|
|
|
|
assert(ScriptSections.size() == Sections.size());
|
|
|
|
for (int I = 0, N = Sections.size(); I < N; ++I)
|
|
|
|
*ScriptSections[I] = Sections[I];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 03:34:17 +08:00
|
|
|
static bool allocateHeaders(std::vector<PhdrEntry> &Phdrs,
|
|
|
|
ArrayRef<OutputSection *> OutputSections,
|
|
|
|
uint64_t Min) {
|
|
|
|
auto FirstPTLoad =
|
|
|
|
std::find_if(Phdrs.begin(), Phdrs.end(),
|
|
|
|
[](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
|
|
|
|
if (FirstPTLoad == Phdrs.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t HeaderSize = getHeaderSize();
|
|
|
|
if (HeaderSize <= Min || Script->hasPhdrsCommands()) {
|
|
|
|
Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
|
|
|
|
Out::ElfHeader->Addr = Min;
|
|
|
|
Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(FirstPTLoad->First == Out::ElfHeader);
|
|
|
|
OutputSection *ActualFirst = nullptr;
|
|
|
|
for (OutputSection *Sec : OutputSections) {
|
|
|
|
if (Sec->FirstInPtLoad == Out::ElfHeader) {
|
|
|
|
ActualFirst = Sec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ActualFirst) {
|
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
if (Sec->FirstInPtLoad == Out::ElfHeader)
|
|
|
|
Sec->FirstInPtLoad = ActualFirst;
|
|
|
|
FirstPTLoad->First = ActualFirst;
|
|
|
|
} else {
|
|
|
|
Phdrs.erase(FirstPTLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto PhdrI = std::find_if(Phdrs.begin(), Phdrs.end(), [](const PhdrEntry &E) {
|
|
|
|
return E.p_type == PT_PHDR;
|
|
|
|
});
|
|
|
|
if (PhdrI != Phdrs.end())
|
|
|
|
Phdrs.erase(PhdrI);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
|
2016-04-19 05:00:40 +08:00
|
|
|
// Assign addresses as instructed by linker script SECTIONS sub-commands.
|
2016-09-30 08:16:11 +08:00
|
|
|
Dot = 0;
|
2017-03-17 21:05:04 +08:00
|
|
|
ErrorOnMissingSection = true;
|
2017-02-07 06:21:46 +08:00
|
|
|
switchTo(Aether);
|
|
|
|
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
|
2017-04-05 11:20:22 +08:00
|
|
|
assignSymbol(Cmd, false);
|
2016-07-12 14:39:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-16 18:10:32 +08:00
|
|
|
|
2017-04-05 11:20:42 +08:00
|
|
|
if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
|
2017-03-11 00:04:26 +08:00
|
|
|
Cmd->Expression();
|
2016-08-04 17:29:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:20:42 +08:00
|
|
|
auto *Cmd = cast<OutputSectionCommand>(Base);
|
2016-09-16 23:10:23 +08:00
|
|
|
assignOffsets(Cmd);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2016-07-01 18:42:25 +08:00
|
|
|
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t MinVA = std::numeric_limits<uint64_t>::max();
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
2016-11-10 07:23:45 +08:00
|
|
|
MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
|
2017-02-08 23:19:03 +08:00
|
|
|
else
|
|
|
|
Sec->Addr = 0;
|
|
|
|
}
|
2016-09-17 05:29:07 +08:00
|
|
|
|
2017-03-14 17:03:53 +08:00
|
|
|
allocateHeaders(Phdrs, *OutputSections, MinVA);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Creates program headers as instructed by PHDRS linker script command.
|
2017-03-22 07:02:51 +08:00
|
|
|
std::vector<PhdrEntry> LinkerScript::createPhdrs() {
|
2016-12-20 01:01:01 +08:00
|
|
|
std::vector<PhdrEntry> Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Process PHDRS and FILEHDR keywords because they are not
|
|
|
|
// real output sections and cannot be added in the following loop.
|
2016-07-19 17:25:43 +08:00
|
|
|
for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
|
2016-07-25 07:47:31 +08:00
|
|
|
Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &Phdr = Ret.back();
|
2016-07-19 17:25:43 +08:00
|
|
|
|
|
|
|
if (Cmd.HasFilehdr)
|
2017-02-27 10:31:26 +08:00
|
|
|
Phdr.add(Out::ElfHeader);
|
2016-07-19 17:25:43 +08:00
|
|
|
if (Cmd.HasPhdrs)
|
2017-02-27 10:31:26 +08:00
|
|
|
Phdr.add(Out::ProgramHeaders);
|
2016-09-09 17:46:16 +08:00
|
|
|
|
|
|
|
if (Cmd.LMAExpr) {
|
2017-03-17 21:05:04 +08:00
|
|
|
Phdr.p_paddr = Cmd.LMAExpr().getValue();
|
2016-09-09 17:46:16 +08:00
|
|
|
Phdr.HasLMA = true;
|
|
|
|
}
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Add output sections to program headers.
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (!(Sec->Flags & SHF_ALLOC))
|
2016-07-19 17:25:43 +08:00
|
|
|
break;
|
|
|
|
|
2016-10-19 23:04:49 +08:00
|
|
|
// Assign headers specified by linker script
|
2017-05-10 22:28:31 +08:00
|
|
|
for (size_t Id : getPhdrIndices(Sec)) {
|
2016-10-19 23:04:49 +08:00
|
|
|
Ret[Id].add(Sec);
|
|
|
|
if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
|
2016-12-20 01:01:01 +08:00
|
|
|
Ret[Id].p_flags |= Sec->getPhdrFlags();
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-25 07:47:31 +08:00
|
|
|
return Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
bool LinkerScript::ignoreInterpSection() {
|
2016-08-16 14:40:58 +08:00
|
|
|
// Ignore .interp section in case we have PHDRS specification
|
|
|
|
// and PT_INTERP isn't listed.
|
2017-04-05 13:06:17 +08:00
|
|
|
if (Opt.PhdrsCommands.empty())
|
|
|
|
return false;
|
|
|
|
for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
|
|
|
|
if (Cmd.Type == PT_INTERP)
|
|
|
|
return false;
|
|
|
|
return true;
|
2016-08-16 14:40:58 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 03:13:38 +08:00
|
|
|
OutputSectionCommand *LinkerScript::getCmd(OutputSection *Sec) const {
|
|
|
|
auto I = SecToCommand.find(Sec);
|
|
|
|
if (I == SecToCommand.end())
|
|
|
|
return nullptr;
|
|
|
|
return I->second;
|
2017-05-11 03:00:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Optional<uint32_t> LinkerScript::getFiller(OutputSection *Sec) {
|
|
|
|
if (OutputSectionCommand *Cmd = getCmd(Sec))
|
|
|
|
return Cmd->Filler;
|
2017-04-07 18:36:42 +08:00
|
|
|
return None;
|
2016-02-26 22:48:31 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 03:22:50 +08:00
|
|
|
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
|
2017-04-05 13:06:37 +08:00
|
|
|
if (Size == 1)
|
|
|
|
*Buf = Data;
|
|
|
|
else if (Size == 2)
|
2017-03-22 05:40:08 +08:00
|
|
|
write16(Buf, Data, Config->Endianness);
|
2017-04-05 13:06:37 +08:00
|
|
|
else if (Size == 4)
|
2017-03-22 05:40:08 +08:00
|
|
|
write32(Buf, Data, Config->Endianness);
|
2017-04-05 13:06:37 +08:00
|
|
|
else if (Size == 8)
|
2017-03-22 05:40:08 +08:00
|
|
|
write64(Buf, Data, Config->Endianness);
|
2017-04-05 13:06:37 +08:00
|
|
|
else
|
2016-09-27 03:22:50 +08:00
|
|
|
llvm_unreachable("unsupported Size argument");
|
|
|
|
}
|
|
|
|
|
2017-05-06 05:34:26 +08:00
|
|
|
void LinkerScript::writeDataBytes(OutputSection *Sec, uint8_t *Buf) {
|
2017-05-11 03:00:23 +08:00
|
|
|
if (OutputSectionCommand *Cmd = getCmd(Sec))
|
|
|
|
for (BaseCommand *Base : Cmd->Commands)
|
|
|
|
if (auto *Data = dyn_cast<BytesDataCommand>(Base))
|
|
|
|
writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
|
2016-09-27 03:22:50 +08:00
|
|
|
}
|
|
|
|
|
2017-05-10 22:12:02 +08:00
|
|
|
bool LinkerScript::hasLMA(OutputSection *Sec) {
|
2017-05-11 03:00:23 +08:00
|
|
|
if (OutputSectionCommand *Cmd = getCmd(Sec))
|
|
|
|
if (Cmd->LMAExpr)
|
|
|
|
return true;
|
2016-10-06 17:39:28 +08:00
|
|
|
return false;
|
2016-08-17 15:44:19 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
|
2017-03-11 00:04:26 +08:00
|
|
|
if (S == ".")
|
2017-03-17 21:05:04 +08:00
|
|
|
return {CurOutSec, Dot - CurOutSec->Addr};
|
2017-03-20 18:09:58 +08:00
|
|
|
if (SymbolBody *B = findSymbol(S)) {
|
2017-03-17 21:05:04 +08:00
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(B))
|
|
|
|
return {D->Section, D->Value};
|
2017-03-23 11:52:34 +08:00
|
|
|
if (auto *C = dyn_cast<DefinedCommon>(B))
|
|
|
|
return {InX::Common, C->Offset};
|
2017-03-17 21:05:04 +08:00
|
|
|
}
|
2016-12-22 21:13:12 +08:00
|
|
|
error(Loc + ": symbol not found: " + S);
|
2016-09-08 16:19:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; }
|
2016-09-23 21:17:23 +08:00
|
|
|
|
2017-05-10 22:28:31 +08:00
|
|
|
// Returns indices of ELF headers containing specific section. Each index is a
|
|
|
|
// zero based number of ELF header listed within PHDRS {} script block.
|
|
|
|
std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Sec) {
|
2017-05-11 03:00:23 +08:00
|
|
|
if (OutputSectionCommand *Cmd = getCmd(Sec)) {
|
2016-07-26 08:27:36 +08:00
|
|
|
std::vector<size_t> Ret;
|
|
|
|
for (StringRef PhdrName : Cmd->Phdrs)
|
2016-12-06 00:38:32 +08:00
|
|
|
Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
|
2016-07-26 08:27:36 +08:00
|
|
|
return Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
2016-07-21 00:43:03 +08:00
|
|
|
return {};
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
|
2016-07-26 08:27:36 +08:00
|
|
|
size_t I = 0;
|
|
|
|
for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
|
|
|
|
if (Cmd.Name == PhdrName)
|
|
|
|
return I;
|
|
|
|
++I;
|
|
|
|
}
|
2016-12-06 00:38:32 +08:00
|
|
|
error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
|
2016-07-26 08:27:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|