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"
|
2017-05-25 02:08:04 +08:00
|
|
|
#include "Target.h"
|
|
|
|
#include "Threads.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"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2017-06-02 00:57:48 +08:00
|
|
|
#include "llvm/Support/Compression.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) {
|
2017-06-01 03:53:40 +08:00
|
|
|
if (OutputSection *OS = Sec->getOutputSection())
|
|
|
|
return alignTo(Sec->getOffset(Val) + OS->Addr, Alignment);
|
2017-06-07 16:54:43 +08:00
|
|
|
error(Loc + ": unable to evaluate expression: input section " + Sec->Name +
|
2017-05-10 22:23:33 +08:00
|
|
|
" has no output section assigned");
|
|
|
|
}
|
2017-05-30 11:18:28 +08:00
|
|
|
return alignTo(Val, Alignment);
|
2017-03-17 21:05:04 +08:00
|
|
|
}
|
|
|
|
|
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-06-01 09:16:50 +08:00
|
|
|
OutputSectionCommand *
|
|
|
|
LinkerScript::createOutputSectionCommand(StringRef Name, StringRef Location) {
|
|
|
|
OutputSectionCommand *&CmdRef = NameToOutputSectionCommand[Name];
|
|
|
|
OutputSectionCommand *Cmd;
|
|
|
|
if (CmdRef && CmdRef->Location.empty()) {
|
|
|
|
// There was a forward reference.
|
|
|
|
Cmd = CmdRef;
|
|
|
|
} else {
|
|
|
|
Cmd = make<OutputSectionCommand>(Name);
|
|
|
|
if (!CmdRef)
|
|
|
|
CmdRef = Cmd;
|
|
|
|
}
|
|
|
|
Cmd->Location = Location;
|
|
|
|
return Cmd;
|
2017-03-14 18:15:53 +08:00
|
|
|
}
|
|
|
|
|
2017-06-01 09:16:50 +08:00
|
|
|
OutputSectionCommand *
|
|
|
|
LinkerScript::getOrCreateOutputSectionCommand(StringRef Name) {
|
|
|
|
OutputSectionCommand *&CmdRef = NameToOutputSectionCommand[Name];
|
|
|
|
if (!CmdRef)
|
|
|
|
CmdRef = make<OutputSectionCommand>(Name);
|
|
|
|
return CmdRef;
|
2017-03-14 18:24:47 +08:00
|
|
|
}
|
|
|
|
|
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-07-12 22:50:25 +08:00
|
|
|
if (Val < Dot && InSec)
|
|
|
|
error(Loc + ": unable to move location counter backward for: " +
|
|
|
|
CurAddressState->OutSec->Name);
|
2017-02-18 00:26:13 +08:00
|
|
|
Dot = Val;
|
|
|
|
// Update to location counter means update to section size.
|
|
|
|
if (InSec)
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
|
2017-02-18 00:26:13 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2017-06-14 04:57:43 +08:00
|
|
|
Sym->Value = alignTo(V.Val, V.Alignment);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:08:04 +08:00
|
|
|
// Fill [Buf, Buf + Size) with Filler.
|
|
|
|
// This is used for linker script "=fillexp" command.
|
|
|
|
static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
|
|
|
|
size_t I = 0;
|
|
|
|
for (; I + 4 < Size; I += 4)
|
|
|
|
memcpy(Buf + I, &Filler, 4);
|
|
|
|
memcpy(Buf + I, &Filler, Size - I);
|
|
|
|
}
|
|
|
|
|
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-07-14 04:30:35 +08:00
|
|
|
// If an input string is in the form of "foo.N" where N is a number,
|
|
|
|
// return N. Otherwise, returns 65536, which is one greater than the
|
|
|
|
// lowest priority.
|
|
|
|
static int getPriority(StringRef S) {
|
|
|
|
size_t Pos = S.rfind('.');
|
|
|
|
if (Pos == StringRef::npos)
|
|
|
|
return 65536;
|
|
|
|
int V;
|
|
|
|
if (!to_integer(S.substr(Pos + 1), V, 10))
|
|
|
|
return 65536;
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
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-05-24 06:47:31 +08:00
|
|
|
static void sortSections(InputSection **Begin, InputSection **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-05-24 06:47:31 +08:00
|
|
|
std::vector<InputSection *>
|
2017-04-05 10:05:48 +08:00
|
|
|
LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
|
2017-05-24 06:47:31 +08:00
|
|
|
std::vector<InputSection *> Ret;
|
2017-04-05 10:05:48 +08:00
|
|
|
|
|
|
|
// Collects all sections that satisfy constraints of Cmd.
|
|
|
|
for (const SectionPattern &Pat : Cmd->SectionPatterns) {
|
|
|
|
size_t SizeBefore = Ret.size();
|
|
|
|
|
|
|
|
for (InputSectionBase *Sec : InputSections) {
|
2017-05-31 04:40:03 +08:00
|
|
|
if (Sec->Assigned)
|
2017-05-30 13:17:58 +08:00
|
|
|
continue;
|
|
|
|
|
2017-05-31 04:40:03 +08:00
|
|
|
if (!Sec->Live) {
|
|
|
|
reportDiscarded(Sec);
|
2016-11-06 06:37:59 +08:00
|
|
|
continue;
|
2017-05-31 04:40:03 +08:00
|
|
|
}
|
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
|
|
|
|
2017-05-24 06:47:31 +08:00
|
|
|
Ret.push_back(cast<InputSection>(Sec));
|
2017-04-05 10:05:48 +08:00
|
|
|
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-05-24 06:47:31 +08:00
|
|
|
InputSection **Begin = Ret.data() + SizeBefore;
|
|
|
|
InputSection **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-06-17 07:53:36 +08:00
|
|
|
if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
|
|
|
|
S == InX::DynStrTab)
|
2017-06-17 07:45:35 +08:00
|
|
|
error("discarding " + S->Name + " 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;
|
2017-07-07 17:11:27 +08:00
|
|
|
auto State = make_unique<AddressState>(Opt);
|
2017-07-11 17:28:27 +08:00
|
|
|
// CurAddressState captures the local AddressState and makes it accessible
|
|
|
|
// deliberately. This is needed as there are some cases where we cannot just
|
|
|
|
// thread the current state through to a lambda function created by the
|
|
|
|
// script parser.
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState = State.get();
|
|
|
|
CurAddressState->OutSec = 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-06-08 00:31:08 +08:00
|
|
|
if (Cmd->Noload)
|
|
|
|
Sec->Type = SHT_NOBITS;
|
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-07-11 17:28:27 +08:00
|
|
|
CurAddressState = 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-06-08 12:17:04 +08:00
|
|
|
uint64_t StartAddr = -1;
|
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.
|
2017-07-18 19:55:35 +08:00
|
|
|
for (auto &KV : Config->SectionStartMap)
|
2017-05-03 16:44:50 +08:00
|
|
|
StartAddr = std::min(StartAddr, KV.second);
|
|
|
|
|
2017-06-08 12:17:04 +08:00
|
|
|
Commands.push_back(make<SymbolAssignment>(
|
|
|
|
".",
|
|
|
|
[=] {
|
|
|
|
return std::min(StartAddr, Config->ImageBase + elf::getHeaderSize());
|
|
|
|
},
|
|
|
|
""));
|
2017-04-19 20:46:32 +08:00
|
|
|
|
|
|
|
// For each OutputSection that needs a VA fabricate an OutputSectionCommand
|
|
|
|
// with an InputSectionDescription describing the InputSections
|
2017-06-14 07:26:31 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2017-06-01 09:16:50 +08:00
|
|
|
auto *OSCmd = createOutputSectionCommand(Sec->Name, "<internal>");
|
2017-05-03 16:44:50 +08:00
|
|
|
OSCmd->Sec = Sec;
|
2017-05-11 03:13:38 +08:00
|
|
|
SecToCommand[Sec] = OSCmd;
|
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-07-06 01:50:43 +08:00
|
|
|
unsigned NumCommands = Opt.Commands.size();
|
2017-04-27 06:30:15 +08:00
|
|
|
for (InputSectionBase *S : InputSections) {
|
2017-06-01 04:17:44 +08:00
|
|
|
if (!S->Live || S->Parent)
|
2017-04-27 06:30:15 +08:00
|
|
|
continue;
|
|
|
|
StringRef Name = getOutputSectionName(S->Name);
|
2017-07-06 01:50:43 +08:00
|
|
|
auto End = Opt.Commands.begin() + NumCommands;
|
|
|
|
auto I = std::find_if(Opt.Commands.begin(), End, [&](BaseCommand *Base) {
|
2017-07-04 21:10:37 +08:00
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
|
|
|
|
return Cmd->Name == Name;
|
|
|
|
return false;
|
|
|
|
});
|
2017-07-06 01:50:43 +08:00
|
|
|
OutputSectionCommand *Cmd;
|
|
|
|
if (I == End) {
|
2017-04-27 06:30:15 +08:00
|
|
|
Factory.addInputSec(S, Name);
|
2017-07-06 01:50:43 +08:00
|
|
|
OutputSection *Sec = S->getOutputSection();
|
|
|
|
assert(Sec->SectionIndex == INT_MAX);
|
|
|
|
OutputSectionCommand *&CmdRef = SecToCommand[Sec];
|
|
|
|
if (!CmdRef) {
|
|
|
|
CmdRef = createOutputSectionCommand(Sec->Name, "<internal>");
|
|
|
|
CmdRef->Sec = Sec;
|
|
|
|
Opt.Commands.push_back(CmdRef);
|
|
|
|
}
|
|
|
|
Cmd = CmdRef;
|
2017-04-29 23:44:03 +08:00
|
|
|
} else {
|
2017-07-06 01:50:43 +08:00
|
|
|
Cmd = cast<OutputSectionCommand>(*I);
|
2017-04-29 23:44:03 +08:00
|
|
|
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
|
|
|
}
|
2017-07-06 01:50:43 +08:00
|
|
|
auto *ISD = make<InputSectionDescription>("");
|
|
|
|
ISD->Sections.push_back(cast<InputSection>(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) {
|
2017-07-07 17:11:27 +08:00
|
|
|
bool IsTbss = (CurAddressState->OutSec->Flags & SHF_TLS) &&
|
|
|
|
CurAddressState->OutSec->Type == SHT_NOBITS;
|
|
|
|
uint64_t Start = IsTbss ? Dot + CurAddressState->ThreadBssOffset : Dot;
|
2017-05-04 11:00:27 +08:00
|
|
|
Start = alignTo(Start, Align);
|
|
|
|
uint64_t End = Start + Size;
|
|
|
|
|
|
|
|
if (IsTbss)
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->ThreadBssOffset = End - Dot;
|
2017-05-04 11:00:27 +08:00
|
|
|
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-07-25 16:29:29 +08:00
|
|
|
uint64_t Before = advance(0, 1);
|
2017-05-04 11:00:27 +08:00
|
|
|
uint64_t Pos = advance(S->getSize(), S->Alignment);
|
2017-07-07 17:11:27 +08:00
|
|
|
S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->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) }
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec->Size = Pos - CurAddressState->OutSec->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.
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->MemRegion) {
|
|
|
|
uint64_t &CurOffset =
|
|
|
|
CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
|
2017-07-25 16:29:29 +08:00
|
|
|
CurOffset += Pos - Before;
|
2017-07-07 17:11:27 +08:00
|
|
|
uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
|
|
|
|
if (CurSize > CurAddressState->MemRegion->Length) {
|
|
|
|
uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;
|
|
|
|
error("section '" + CurAddressState->OutSec->Name +
|
|
|
|
"' will not fit in region '" + CurAddressState->MemRegion->Name +
|
|
|
|
"': overflowed by " + Twine(OverflowAmt) + " bytes");
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::switchTo(OutputSection *Sec) {
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->OutSec == Sec)
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec = Sec;
|
|
|
|
CurAddressState->OutSec->Addr =
|
|
|
|
advance(0, CurAddressState->OutSec->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-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->LMAOffset)
|
|
|
|
CurAddressState->OutSec->LMAOffset = CurAddressState->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)) {
|
2017-07-07 17:11:27 +08:00
|
|
|
Cmd->Offset = Dot - CurAddressState->OutSec->Addr;
|
2017-04-05 11:18:46 +08:00
|
|
|
Dot += Cmd->Size;
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->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);
|
2017-05-31 04:24:52 +08:00
|
|
|
for (InputSection *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-07-07 17:11:27 +08:00
|
|
|
assert(CurAddressState->OutSec == Sec->getParent());
|
2017-05-31 04:24:52 +08:00
|
|
|
output(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-06-14 04:57:43 +08:00
|
|
|
if (!(Sec->Flags & SHF_ALLOC))
|
|
|
|
Dot = 0;
|
|
|
|
else if (Cmd->AddrExpr)
|
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-07-07 17:11:27 +08:00
|
|
|
CurAddressState->LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; };
|
2017-03-14 16:57:09 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->MemRegion = Cmd->MemRegion;
|
|
|
|
if (CurAddressState->MemRegion)
|
|
|
|
Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
|
2017-01-24 10:34:00 +08:00
|
|
|
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.
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->OutSec->Flags & SHF_COMPRESSED)
|
2017-05-08 18:18:12 +08:00
|
|
|
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-06-14 07:54:34 +08:00
|
|
|
return Cmd->Sec == nullptr;
|
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-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;
|
2016-09-22 22:40:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-14 22:33:49 +08:00
|
|
|
if (isAllSectionDescription(*Cmd))
|
|
|
|
continue;
|
|
|
|
|
2017-05-25 00:48:31 +08:00
|
|
|
auto *OutSec = make<OutputSection>(Cmd->Name, SHT_PROGBITS, Flags);
|
2017-05-06 05:34:26 +08:00
|
|
|
OutSec->SectionIndex = I;
|
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() {
|
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) { ... }").
|
2017-07-22 08:00:51 +08:00
|
|
|
if (Cmd->AlignExpr && Cmd->Sec)
|
2017-07-18 19:55:35 +08:00
|
|
|
Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue());
|
2017-04-07 05:40:22 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-07-03 18:11:25 +08:00
|
|
|
if (Cmd->Phdrs.empty()) {
|
|
|
|
OutputSection *Sec = Cmd->Sec;
|
|
|
|
// To match the bfd linker script behaviour, only propagate program
|
|
|
|
// headers to sections that are allocated.
|
|
|
|
if (Sec && (Sec->Flags & SHF_ALLOC))
|
|
|
|
Cmd->Phdrs = DefPhdrs;
|
|
|
|
} else {
|
2016-11-14 23:39:38 +08:00
|
|
|
DefPhdrs = Cmd->Phdrs;
|
2017-07-03 18:11:25 +08:00
|
|
|
}
|
2016-11-14 23:39:38 +08:00
|
|
|
}
|
2016-11-14 22:33:49 +08:00
|
|
|
|
|
|
|
removeEmptyCommands();
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-05 17:12:54 +08:00
|
|
|
void LinkerScript::allocateHeaders(std::vector<PhdrEntry> &Phdrs) {
|
|
|
|
uint64_t Min = std::numeric_limits<uint64_t>::max();
|
|
|
|
for (OutputSectionCommand *Cmd : OutputSectionCommands) {
|
|
|
|
OutputSection *Sec = Cmd->Sec;
|
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
|
|
|
Min = std::min<uint64_t>(Min, Sec->Addr);
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:05:51 +08:00
|
|
|
auto FirstPTLoad = llvm::find_if(
|
|
|
|
Phdrs, [](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
|
2017-05-05 03:34:17 +08:00
|
|
|
if (FirstPTLoad == Phdrs.end())
|
2017-07-04 00:05:51 +08:00
|
|
|
return;
|
2017-05-05 03:34:17 +08:00
|
|
|
|
|
|
|
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;
|
2017-07-04 00:05:51 +08:00
|
|
|
return;
|
2017-05-05 03:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(FirstPTLoad->First == Out::ElfHeader);
|
|
|
|
OutputSection *ActualFirst = nullptr;
|
2017-06-02 00:26:28 +08:00
|
|
|
for (OutputSectionCommand *Cmd : OutputSectionCommands) {
|
|
|
|
OutputSection *Sec = Cmd->Sec;
|
2017-05-05 03:34:17 +08:00
|
|
|
if (Sec->FirstInPtLoad == Out::ElfHeader) {
|
|
|
|
ActualFirst = Sec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ActualFirst) {
|
2017-06-02 00:26:28 +08:00
|
|
|
for (OutputSectionCommand *Cmd : OutputSectionCommands) {
|
|
|
|
OutputSection *Sec = Cmd->Sec;
|
2017-05-05 03:34:17 +08:00
|
|
|
if (Sec->FirstInPtLoad == Out::ElfHeader)
|
|
|
|
Sec->FirstInPtLoad = ActualFirst;
|
2017-06-02 00:26:28 +08:00
|
|
|
}
|
2017-05-05 03:34:17 +08:00
|
|
|
FirstPTLoad->First = ActualFirst;
|
|
|
|
} else {
|
|
|
|
Phdrs.erase(FirstPTLoad);
|
|
|
|
}
|
|
|
|
|
2017-07-04 00:05:51 +08:00
|
|
|
auto PhdrI = llvm::find_if(
|
|
|
|
Phdrs, [](const PhdrEntry &E) { return E.p_type == PT_PHDR; });
|
2017-05-05 03:34:17 +08:00
|
|
|
if (PhdrI != Phdrs.end())
|
|
|
|
Phdrs.erase(PhdrI);
|
|
|
|
}
|
|
|
|
|
2017-07-07 17:11:27 +08:00
|
|
|
LinkerScript::AddressState::AddressState(const ScriptConfiguration &Opt) {
|
|
|
|
for (auto &MRI : Opt.MemoryRegions) {
|
|
|
|
const MemoryRegion *MR = &MRI.second;
|
|
|
|
MemRegionOffset[MR] = MR->Origin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 17:12:54 +08:00
|
|
|
void LinkerScript::assignAddresses() {
|
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-07-07 17:11:27 +08:00
|
|
|
auto State = make_unique<AddressState>(Opt);
|
2017-07-11 17:28:27 +08:00
|
|
|
// CurAddressState captures the local AddressState and makes it accessible
|
|
|
|
// deliberately. This is needed as there are some cases where we cannot just
|
|
|
|
// thread the current state through to a lambda function created by the
|
|
|
|
// script parser.
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState = State.get();
|
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
|
|
|
}
|
2017-07-11 17:28:27 +08:00
|
|
|
CurAddressState = nullptr;
|
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-06-14 07:26:31 +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-06-11 06:12:32 +08:00
|
|
|
for (OutputSectionCommand *Cmd : OutputSectionCommands) {
|
2016-10-19 23:04:49 +08:00
|
|
|
// Assign headers specified by linker script
|
2017-07-06 06:30:04 +08:00
|
|
|
for (size_t Id : getPhdrIndices(Cmd)) {
|
|
|
|
OutputSection *Sec = Cmd->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
|
|
|
}
|
|
|
|
|
2017-07-06 07:36:24 +08:00
|
|
|
void OutputSectionCommand::sort(std::function<int(InputSectionBase *S)> Order) {
|
|
|
|
typedef std::pair<unsigned, InputSection *> Pair;
|
|
|
|
auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
|
|
|
|
|
|
|
|
std::vector<Pair> V;
|
|
|
|
assert(Commands.size() == 1);
|
|
|
|
auto *ISD = cast<InputSectionDescription>(Commands[0]);
|
|
|
|
for (InputSection *S : ISD->Sections)
|
|
|
|
V.push_back({Order(S), S});
|
|
|
|
std::stable_sort(V.begin(), V.end(), Comp);
|
|
|
|
ISD->Sections.clear();
|
|
|
|
for (Pair &P : V)
|
|
|
|
ISD->Sections.push_back(P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if S matches /Filename.?\.o$/.
|
|
|
|
static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
|
|
|
|
if (!S.endswith(".o"))
|
|
|
|
return false;
|
|
|
|
S = S.drop_back(2);
|
|
|
|
if (S.endswith(Filename))
|
|
|
|
return true;
|
|
|
|
return !S.empty() && S.drop_back().endswith(Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
|
|
|
|
static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
|
|
|
|
|
|
|
|
// .ctors and .dtors are sorted by this priority from highest to lowest.
|
|
|
|
//
|
|
|
|
// 1. The section was contained in crtbegin (crtbegin contains
|
|
|
|
// some sentinel value in its .ctors and .dtors so that the runtime
|
|
|
|
// can find the beginning of the sections.)
|
|
|
|
//
|
|
|
|
// 2. The section has an optional priority value in the form of ".ctors.N"
|
|
|
|
// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
|
|
|
|
// they are compared as string rather than number.
|
|
|
|
//
|
|
|
|
// 3. The section is just ".ctors" or ".dtors".
|
|
|
|
//
|
|
|
|
// 4. The section was contained in crtend, which contains an end marker.
|
|
|
|
//
|
|
|
|
// In an ideal world, we don't need this function because .init_array and
|
|
|
|
// .ctors are duplicate features (and .init_array is newer.) However, there
|
|
|
|
// are too many real-world use cases of .ctors, so we had no choice to
|
|
|
|
// support that with this rather ad-hoc semantics.
|
|
|
|
static bool compCtors(const InputSection *A, const InputSection *B) {
|
|
|
|
bool BeginA = isCrtbegin(A->File->getName());
|
|
|
|
bool BeginB = isCrtbegin(B->File->getName());
|
|
|
|
if (BeginA != BeginB)
|
|
|
|
return BeginA;
|
|
|
|
bool EndA = isCrtend(A->File->getName());
|
|
|
|
bool EndB = isCrtend(B->File->getName());
|
|
|
|
if (EndA != EndB)
|
|
|
|
return EndB;
|
|
|
|
StringRef X = A->Name;
|
|
|
|
StringRef Y = B->Name;
|
|
|
|
assert(X.startswith(".ctors") || X.startswith(".dtors"));
|
|
|
|
assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
|
|
|
|
X = X.substr(6);
|
|
|
|
Y = Y.substr(6);
|
|
|
|
if (X.empty() && Y.empty())
|
|
|
|
return false;
|
|
|
|
return X < Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorts input sections by the special rules for .ctors and .dtors.
|
|
|
|
// Unfortunately, the rules are different from the one for .{init,fini}_array.
|
|
|
|
// Read the comment above.
|
|
|
|
void OutputSectionCommand::sortCtorsDtors() {
|
|
|
|
assert(Commands.size() == 1);
|
|
|
|
auto *ISD = cast<InputSectionDescription>(Commands[0]);
|
|
|
|
std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorts input sections by section name suffixes, so that .foo.N comes
|
|
|
|
// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
|
|
|
|
// We want to keep the original order if the priorities are the same
|
|
|
|
// because the compiler keeps the original initialization order in a
|
|
|
|
// translation unit and we need to respect that.
|
|
|
|
// For more detail, read the section of the GCC's manual about init_priority.
|
|
|
|
void OutputSectionCommand::sortInitFini() {
|
|
|
|
// Sort sections by priority.
|
|
|
|
sort([](InputSectionBase *S) { return getPriority(S->Name); });
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:08:04 +08:00
|
|
|
uint32_t OutputSectionCommand::getFiller() {
|
|
|
|
if (Filler)
|
|
|
|
return *Filler;
|
|
|
|
if (Sec->Flags & SHF_EXECINSTR)
|
|
|
|
return Target->TrapInstr;
|
|
|
|
return 0;
|
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-06-07 03:40:01 +08:00
|
|
|
static bool compareByFilePosition(InputSection *A, InputSection *B) {
|
|
|
|
// Synthetic doesn't have link order dependecy, stable_sort will keep it last
|
|
|
|
if (A->kind() == InputSectionBase::Synthetic ||
|
|
|
|
B->kind() == InputSectionBase::Synthetic)
|
|
|
|
return false;
|
|
|
|
InputSection *LA = A->getLinkOrderDep();
|
|
|
|
InputSection *LB = B->getLinkOrderDep();
|
|
|
|
OutputSection *AOut = LA->getParent();
|
|
|
|
OutputSection *BOut = LB->getParent();
|
|
|
|
if (AOut != BOut)
|
|
|
|
return AOut->SectionIndex < BOut->SectionIndex;
|
|
|
|
return LA->OutSecOff < LB->OutSecOff;
|
|
|
|
}
|
|
|
|
|
2017-06-07 04:13:19 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static void finalizeShtGroup(OutputSection *OS,
|
|
|
|
ArrayRef<InputSection *> Sections) {
|
2017-06-27 01:11:36 +08:00
|
|
|
assert(Config->Relocatable && Sections.size() == 1);
|
|
|
|
|
2017-06-07 03:40:01 +08:00
|
|
|
// sh_link field for SHT_GROUP sections should contain the section index of
|
|
|
|
// the symbol table.
|
2017-06-07 04:13:19 +08:00
|
|
|
OS->Link = InX::SymTab->getParent()->SectionIndex;
|
2017-06-07 03:40:01 +08:00
|
|
|
|
|
|
|
// sh_info then contain index of an entry in symbol table section which
|
|
|
|
// provides signature of the section group.
|
2017-06-07 04:13:19 +08:00
|
|
|
elf::ObjectFile<ELFT> *Obj = Sections[0]->getFile<ELFT>();
|
2017-06-07 03:40:01 +08:00
|
|
|
ArrayRef<SymbolBody *> Symbols = Obj->getSymbols();
|
2017-06-07 04:13:19 +08:00
|
|
|
OS->Info = InX::SymTab->getSymbolIndex(Symbols[Sections[0]->Info - 1]);
|
2017-06-07 03:40:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void OutputSectionCommand::finalize() {
|
2017-06-07 04:13:19 +08:00
|
|
|
// Link order may be distributed across several InputSectionDescriptions
|
|
|
|
// but sort must consider them all at once.
|
|
|
|
std::vector<InputSection **> ScriptSections;
|
|
|
|
std::vector<InputSection *> Sections;
|
|
|
|
for (BaseCommand *Base : Commands)
|
|
|
|
if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
|
|
|
|
for (InputSection *&IS : ISD->Sections) {
|
|
|
|
ScriptSections.push_back(&IS);
|
|
|
|
Sections.push_back(IS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((Sec->Flags & SHF_LINK_ORDER)) {
|
2017-07-18 03:42:19 +08:00
|
|
|
std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
|
2017-06-07 03:40:01 +08:00
|
|
|
for (int I = 0, N = Sections.size(); I < N; ++I)
|
|
|
|
*ScriptSections[I] = Sections[I];
|
|
|
|
|
|
|
|
// We must preserve the link order dependency of sections with the
|
|
|
|
// SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
|
|
|
|
// need to translate the InputSection sh_link to the OutputSection sh_link,
|
|
|
|
// all InputSections in the OutputSection have the same dependency.
|
2017-06-07 04:13:19 +08:00
|
|
|
if (auto *D = Sections.front()->getLinkOrderDep())
|
2017-06-07 03:40:01 +08:00
|
|
|
Sec->Link = D->getParent()->SectionIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Type = Sec->Type;
|
|
|
|
if (Type == SHT_GROUP) {
|
2017-06-07 04:13:19 +08:00
|
|
|
finalizeShtGroup<ELFT>(Sec, Sections);
|
2017-06-07 03:40:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL))
|
|
|
|
return;
|
|
|
|
|
2017-06-07 04:13:19 +08:00
|
|
|
InputSection *First = Sections[0];
|
2017-06-07 03:40:01 +08:00
|
|
|
if (isa<SyntheticSection>(First))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Sec->Link = InX::SymTab->getParent()->SectionIndex;
|
|
|
|
// sh_info for SHT_REL[A] sections should contain the section header index of
|
|
|
|
// the section to which the relocation applies.
|
|
|
|
InputSectionBase *S = First->getRelocatedSection();
|
|
|
|
Sec->Info = S->getOutputSection()->SectionIndex;
|
|
|
|
Sec->Flags |= SHF_INFO_LINK;
|
|
|
|
}
|
|
|
|
|
2017-06-02 00:57:48 +08:00
|
|
|
// Compress section contents if this section contains debug info.
|
|
|
|
template <class ELFT> void OutputSectionCommand::maybeCompress() {
|
|
|
|
typedef typename ELFT::Chdr Elf_Chdr;
|
|
|
|
|
|
|
|
// Compress only DWARF debug sections.
|
|
|
|
if (!Config->CompressDebugSections || (Sec->Flags & SHF_ALLOC) ||
|
|
|
|
!Name.startswith(".debug_"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a section header.
|
|
|
|
Sec->ZDebugHeader.resize(sizeof(Elf_Chdr));
|
|
|
|
auto *Hdr = reinterpret_cast<Elf_Chdr *>(Sec->ZDebugHeader.data());
|
|
|
|
Hdr->ch_type = ELFCOMPRESS_ZLIB;
|
|
|
|
Hdr->ch_size = Sec->Size;
|
|
|
|
Hdr->ch_addralign = Sec->Alignment;
|
|
|
|
|
|
|
|
// Write section contents to a temporary buffer and compress it.
|
|
|
|
std::vector<uint8_t> Buf(Sec->Size);
|
|
|
|
writeTo<ELFT>(Buf.data());
|
|
|
|
if (Error E = zlib::compress(toStringRef(Buf), Sec->CompressedData))
|
|
|
|
fatal("compress failed: " + llvm::toString(std::move(E)));
|
|
|
|
|
|
|
|
// Update section headers.
|
|
|
|
Sec->Size = sizeof(Elf_Chdr) + Sec->CompressedData.size();
|
|
|
|
Sec->Flags |= SHF_COMPRESSED;
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:08:04 +08:00
|
|
|
template <class ELFT> void OutputSectionCommand::writeTo(uint8_t *Buf) {
|
2017-06-06 15:46:28 +08:00
|
|
|
if (Sec->Type == SHT_NOBITS)
|
|
|
|
return;
|
|
|
|
|
2017-05-25 02:08:04 +08:00
|
|
|
Sec->Loc = Buf;
|
|
|
|
|
2017-06-17 06:45:13 +08:00
|
|
|
// If -compress-debug-section is specified and if this is a debug seciton,
|
|
|
|
// we've already compressed section contents. If that's the case,
|
|
|
|
// just write it down.
|
2017-05-25 02:08:04 +08:00
|
|
|
if (!Sec->CompressedData.empty()) {
|
|
|
|
memcpy(Buf, Sec->ZDebugHeader.data(), Sec->ZDebugHeader.size());
|
|
|
|
memcpy(Buf + Sec->ZDebugHeader.size(), Sec->CompressedData.data(),
|
|
|
|
Sec->CompressedData.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write leading padding.
|
2017-06-01 04:22:27 +08:00
|
|
|
std::vector<InputSection *> Sections;
|
|
|
|
for (BaseCommand *Cmd : Commands)
|
|
|
|
if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
|
|
|
|
for (InputSection *IS : ISD->Sections)
|
|
|
|
if (IS->Live)
|
|
|
|
Sections.push_back(IS);
|
2017-05-25 02:08:04 +08:00
|
|
|
uint32_t Filler = getFiller();
|
|
|
|
if (Filler)
|
|
|
|
fill(Buf, Sections.empty() ? Sec->Size : Sections[0]->OutSecOff, Filler);
|
|
|
|
|
|
|
|
parallelForEachN(0, Sections.size(), [=](size_t I) {
|
|
|
|
InputSection *IS = Sections[I];
|
|
|
|
IS->writeTo<ELFT>(Buf);
|
|
|
|
|
|
|
|
// Fill gaps between sections.
|
|
|
|
if (Filler) {
|
|
|
|
uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
|
|
|
|
uint8_t *End;
|
|
|
|
if (I + 1 == Sections.size())
|
|
|
|
End = Buf + Sec->Size;
|
|
|
|
else
|
|
|
|
End = Buf + Sections[I + 1]->OutSecOff;
|
|
|
|
fill(Start, End - Start, Filler);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Linker scripts may have BYTE()-family commands with which you
|
|
|
|
// can write arbitrary bytes to the output. Process them if any.
|
|
|
|
for (BaseCommand *Base : 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-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-07-07 17:11:27 +08:00
|
|
|
return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr, Loc};
|
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))
|
2017-06-07 16:54:43 +08:00
|
|
|
return {D->Section, D->Value, Loc};
|
2017-03-23 11:52:34 +08:00
|
|
|
if (auto *C = dyn_cast<DefinedCommon>(B))
|
2017-06-07 16:54:43 +08:00
|
|
|
return {InX::Common, C->Offset, Loc};
|
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-06-19 23:28:58 +08:00
|
|
|
static const size_t NoPhdr = -1;
|
|
|
|
|
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.
|
2017-07-06 06:30:04 +08:00
|
|
|
std::vector<size_t> LinkerScript::getPhdrIndices(OutputSectionCommand *Cmd) {
|
|
|
|
std::vector<size_t> Ret;
|
|
|
|
for (StringRef PhdrName : Cmd->Phdrs) {
|
|
|
|
size_t Index = getPhdrIndex(Cmd->Location, PhdrName);
|
|
|
|
if (Index != NoPhdr)
|
|
|
|
Ret.push_back(Index);
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
2017-07-06 06:30:04 +08:00
|
|
|
return Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-06-19 23:28:58 +08:00
|
|
|
// Returns the index of the segment named PhdrName if found otherwise
|
|
|
|
// NoPhdr. When not found, if PhdrName is not the special case value 'NONE'
|
|
|
|
// (which can be used to explicitly specify that a section isn't assigned to a
|
|
|
|
// segment) then error.
|
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;
|
|
|
|
}
|
2017-06-19 23:28:58 +08:00
|
|
|
if (PhdrName != "NONE")
|
|
|
|
error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
|
|
|
|
return NoPhdr;
|
2016-07-26 08:27:36 +08:00
|
|
|
}
|
2017-05-25 02:08:04 +08:00
|
|
|
|
|
|
|
template void OutputSectionCommand::writeTo<ELF32LE>(uint8_t *Buf);
|
|
|
|
template void OutputSectionCommand::writeTo<ELF32BE>(uint8_t *Buf);
|
|
|
|
template void OutputSectionCommand::writeTo<ELF64LE>(uint8_t *Buf);
|
|
|
|
template void OutputSectionCommand::writeTo<ELF64BE>(uint8_t *Buf);
|
2017-06-02 00:57:48 +08:00
|
|
|
|
|
|
|
template void OutputSectionCommand::maybeCompress<ELF32LE>();
|
|
|
|
template void OutputSectionCommand::maybeCompress<ELF32BE>();
|
|
|
|
template void OutputSectionCommand::maybeCompress<ELF64LE>();
|
|
|
|
template void OutputSectionCommand::maybeCompress<ELF64BE>();
|
2017-06-07 03:40:01 +08:00
|
|
|
|
|
|
|
template void OutputSectionCommand::finalize<ELF32LE>();
|
|
|
|
template void OutputSectionCommand::finalize<ELF32BE>();
|
|
|
|
template void OutputSectionCommand::finalize<ELF64LE>();
|
|
|
|
template void OutputSectionCommand::finalize<ELF64BE>();
|