2015-09-22 05:38:08 +08:00
|
|
|
//===- OutputSections.cpp -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "Config.h"
|
2016-11-02 07:09:07 +08:00
|
|
|
#include "LinkerScript.h"
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "Memory.h"
|
2016-06-29 17:08:02 +08:00
|
|
|
#include "Strings.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-11-06 07:05:47 +08:00
|
|
|
#include "SyntheticSections.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
#include "Target.h"
|
2016-12-04 05:24:51 +08:00
|
|
|
#include "Threads.h"
|
2016-01-15 21:34:52 +08:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
2016-04-08 06:49:21 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2015-10-22 16:21:35 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2016-09-28 00:43:49 +08:00
|
|
|
#include "llvm/Support/SHA1.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-02-10 05:46:11 +08:00
|
|
|
using namespace llvm::dwarf;
|
2015-09-22 05:38:08 +08:00
|
|
|
using namespace llvm::object;
|
2015-10-07 07:56:53 +08:00
|
|
|
using namespace llvm::support::endian;
|
2015-09-22 05:38:08 +08:00
|
|
|
using namespace llvm::ELF;
|
|
|
|
|
|
|
|
using namespace lld;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type,
|
|
|
|
uint64_t Flags)
|
2015-09-22 05:38:08 +08:00
|
|
|
: Name(Name) {
|
2016-11-09 09:42:41 +08:00
|
|
|
this->Type = Type;
|
|
|
|
this->Flags = Flags;
|
|
|
|
this->Addralign = 1;
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
uint32_t OutputSectionBase::getPhdrFlags() const {
|
2016-07-27 22:10:56 +08:00
|
|
|
uint32_t Ret = PF_R;
|
|
|
|
if (Flags & SHF_WRITE)
|
|
|
|
Ret |= PF_W;
|
|
|
|
if (Flags & SHF_EXECINSTR)
|
|
|
|
Ret |= PF_X;
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2016-03-13 14:50:33 +08:00
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) {
|
2016-11-09 09:42:41 +08:00
|
|
|
Shdr->sh_entsize = Entsize;
|
|
|
|
Shdr->sh_addralign = Addralign;
|
|
|
|
Shdr->sh_type = Type;
|
|
|
|
Shdr->sh_offset = Offset;
|
|
|
|
Shdr->sh_flags = Flags;
|
|
|
|
Shdr->sh_info = Info;
|
|
|
|
Shdr->sh_link = Link;
|
2017-02-08 23:19:03 +08:00
|
|
|
Shdr->sh_addr = Addr;
|
2016-11-09 09:42:41 +08:00
|
|
|
Shdr->sh_size = Size;
|
|
|
|
Shdr->sh_name = ShName;
|
2016-03-13 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
2016-11-10 06:32:42 +08:00
|
|
|
template <class ELFT> static uint64_t getEntsize(uint32_t Type) {
|
|
|
|
switch (Type) {
|
|
|
|
case SHT_RELA:
|
|
|
|
return sizeof(typename ELFT::Rela);
|
|
|
|
case SHT_REL:
|
|
|
|
return sizeof(typename ELFT::Rel);
|
|
|
|
case SHT_MIPS_REGINFO:
|
|
|
|
return sizeof(Elf_Mips_RegInfo<ELFT>);
|
|
|
|
case SHT_MIPS_OPTIONS:
|
|
|
|
return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
|
|
|
|
case SHT_MIPS_ABIFLAGS:
|
|
|
|
return sizeof(Elf_Mips_ABIFlags<ELFT>);
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-26 01:19:10 +08:00
|
|
|
template <class ELFT>
|
2016-02-25 16:23:37 +08:00
|
|
|
OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
|
2016-11-10 07:23:45 +08:00
|
|
|
: OutputSectionBase(Name, Type, Flags) {
|
2016-11-10 06:32:42 +08:00
|
|
|
this->Entsize = getEntsize<ELFT>(Type);
|
2016-02-25 16:23:37 +08:00
|
|
|
}
|
|
|
|
|
2016-11-19 03:02:15 +08:00
|
|
|
template <typename ELFT>
|
2017-02-24 00:49:07 +08:00
|
|
|
static bool compareByFilePosition(InputSection *A, InputSection *B) {
|
2016-11-24 19:43:55 +08:00
|
|
|
// Synthetic doesn't have link order dependecy, stable_sort will keep it last
|
2017-02-23 10:32:18 +08:00
|
|
|
if (A->kind() == InputSectionBase::Synthetic ||
|
|
|
|
B->kind() == InputSectionBase::Synthetic)
|
2016-11-24 19:43:55 +08:00
|
|
|
return false;
|
2017-02-24 00:49:07 +08:00
|
|
|
auto *LA = cast<InputSection>(A->template getLinkOrderDep<ELFT>());
|
|
|
|
auto *LB = cast<InputSection>(B->template getLinkOrderDep<ELFT>());
|
2016-11-19 03:02:15 +08:00
|
|
|
OutputSectionBase *AOut = LA->OutSec;
|
|
|
|
OutputSectionBase *BOut = LB->OutSec;
|
|
|
|
if (AOut != BOut)
|
|
|
|
return AOut->SectionIndex < BOut->SectionIndex;
|
|
|
|
return LA->OutSecOff < LB->OutSecOff;
|
|
|
|
}
|
|
|
|
|
2016-02-25 16:23:37 +08:00
|
|
|
template <class ELFT> void OutputSection<ELFT>::finalize() {
|
2016-11-16 18:02:27 +08:00
|
|
|
if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
|
2016-11-19 03:02:15 +08:00
|
|
|
std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>);
|
|
|
|
Size = 0;
|
|
|
|
assignOffsets();
|
|
|
|
|
2016-11-18 07:16:39 +08:00
|
|
|
// 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-02-23 10:28:28 +08:00
|
|
|
if (auto *D = this->Sections.front()->template getLinkOrderDep<ELFT>())
|
2016-11-16 18:02:27 +08:00
|
|
|
this->Link = D->OutSec->SectionIndex;
|
2016-10-21 19:25:33 +08:00
|
|
|
}
|
2016-11-14 17:16:00 +08:00
|
|
|
|
2016-11-18 07:16:39 +08:00
|
|
|
uint32_t Type = this->Type;
|
2017-02-09 00:18:10 +08:00
|
|
|
if (!Config->copyRelocs() || (Type != SHT_RELA && Type != SHT_REL))
|
2016-02-25 16:23:37 +08:00
|
|
|
return;
|
2016-11-18 07:16:39 +08:00
|
|
|
|
2017-02-24 00:49:07 +08:00
|
|
|
InputSection *First = Sections[0];
|
2017-02-16 22:23:43 +08:00
|
|
|
if (isa<SyntheticSection<ELFT>>(First))
|
|
|
|
return;
|
|
|
|
|
2016-11-17 17:16:34 +08:00
|
|
|
this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
|
2016-02-25 16:23:37 +08:00
|
|
|
// sh_info for SHT_REL[A] sections should contain the section header index of
|
|
|
|
// the section to which the relocation applies.
|
2017-02-24 00:49:07 +08:00
|
|
|
InputSectionBase *S = First->getRelocatedSection<ELFT>();
|
2016-11-09 09:42:41 +08:00
|
|
|
this->Info = S->OutSec->SectionIndex;
|
2016-02-25 16:23:37 +08:00
|
|
|
}
|
2015-09-26 01:19:10 +08:00
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:32:18 +08:00
|
|
|
void OutputSection<ELFT>::addSection(InputSectionBase *C) {
|
2016-02-26 02:43:51 +08:00
|
|
|
assert(C->Live);
|
2017-02-24 00:49:07 +08:00
|
|
|
auto *S = cast<InputSection>(C);
|
2015-12-26 13:51:07 +08:00
|
|
|
Sections.push_back(S);
|
|
|
|
S->OutSec = this;
|
2016-06-17 09:18:46 +08:00
|
|
|
this->updateAlignment(S->Alignment);
|
2016-10-05 15:49:18 +08:00
|
|
|
// Keep sh_entsize value of the input section to be able to perform merging
|
|
|
|
// later during a final linking using the generated relocatable object.
|
2016-10-26 20:36:56 +08:00
|
|
|
if (Config->Relocatable && (S->Flags & SHF_MERGE))
|
2016-11-09 09:42:41 +08:00
|
|
|
this->Entsize = S->Entsize;
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
|
|
|
|
2017-01-14 05:05:46 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void OutputSection<ELFT>::forEachInputSection(
|
2017-02-23 10:32:18 +08:00
|
|
|
std::function<void(InputSectionBase *)> F) {
|
2017-02-24 00:49:07 +08:00
|
|
|
for (InputSection *S : Sections)
|
2017-01-14 05:05:46 +08:00
|
|
|
F(S);
|
|
|
|
}
|
|
|
|
|
2016-06-23 12:33:42 +08:00
|
|
|
// This function is called after we sort input sections
|
|
|
|
// and scan relocations to setup sections' offsets.
|
|
|
|
template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
|
2016-11-09 09:42:41 +08:00
|
|
|
uintX_t Off = this->Size;
|
2017-02-24 00:49:07 +08:00
|
|
|
for (InputSection *S : Sections) {
|
2016-06-23 12:33:42 +08:00
|
|
|
Off = alignTo(Off, S->Alignment);
|
|
|
|
S->OutSecOff = Off;
|
2017-02-23 10:28:28 +08:00
|
|
|
Off += S->template getSize<ELFT>();
|
2016-06-23 12:33:42 +08:00
|
|
|
}
|
2016-11-09 09:42:41 +08:00
|
|
|
this->Size = Off;
|
2016-02-12 07:41:38 +08:00
|
|
|
}
|
|
|
|
|
2016-11-10 17:05:20 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:32:18 +08:00
|
|
|
void OutputSection<ELFT>::sort(std::function<int(InputSectionBase *S)> Order) {
|
2017-02-24 00:49:07 +08:00
|
|
|
typedef std::pair<unsigned, InputSection *> Pair;
|
2016-02-11 07:43:16 +08:00
|
|
|
auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
|
|
|
|
|
2016-02-11 07:20:42 +08:00
|
|
|
std::vector<Pair> V;
|
2017-02-24 00:49:07 +08:00
|
|
|
for (InputSection *S : Sections)
|
2016-11-10 17:05:20 +08:00
|
|
|
V.push_back({Order(S), S});
|
2016-02-11 07:43:16 +08:00
|
|
|
std::stable_sort(V.begin(), V.end(), Comp);
|
2016-02-11 07:20:42 +08:00
|
|
|
Sections.clear();
|
|
|
|
for (Pair &P : V)
|
|
|
|
Sections.push_back(P.second);
|
2016-02-12 07:41:38 +08:00
|
|
|
}
|
2016-02-11 07:20:42 +08:00
|
|
|
|
2016-11-10 17:05:20 +08:00
|
|
|
// 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.
|
|
|
|
template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
|
|
|
|
// Sort sections by priority.
|
2017-02-23 10:32:18 +08:00
|
|
|
sort([](InputSectionBase *S) { return getPriority(S->Name); });
|
2016-11-10 17:05:20 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 07:41:38 +08:00
|
|
|
// 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.
|
|
|
|
template <class ELFT>
|
2017-02-24 00:49:07 +08:00
|
|
|
static bool compCtors(const InputSection *A, const InputSection *B) {
|
2017-02-23 10:28:28 +08:00
|
|
|
bool BeginA = isCrtbegin(A->template getFile<ELFT>()->getName());
|
|
|
|
bool BeginB = isCrtbegin(B->template getFile<ELFT>()->getName());
|
2016-02-12 07:41:38 +08:00
|
|
|
if (BeginA != BeginB)
|
|
|
|
return BeginA;
|
2017-02-23 10:28:28 +08:00
|
|
|
bool EndA = isCrtend(A->template getFile<ELFT>()->getName());
|
|
|
|
bool EndB = isCrtend(B->template getFile<ELFT>()->getName());
|
2016-02-12 07:41:38 +08:00
|
|
|
if (EndA != EndB)
|
|
|
|
return EndB;
|
2016-09-08 22:06:08 +08:00
|
|
|
StringRef X = A->Name;
|
|
|
|
StringRef Y = B->Name;
|
2016-02-12 07:41:38 +08:00
|
|
|
assert(X.startswith(".ctors") || X.startswith(".dtors"));
|
|
|
|
assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
|
|
|
|
X = X.substr(6);
|
|
|
|
Y = Y.substr(6);
|
2016-02-12 08:38:46 +08:00
|
|
|
if (X.empty() && Y.empty())
|
|
|
|
return false;
|
2016-02-12 07:41:38 +08:00
|
|
|
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.
|
|
|
|
template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
|
|
|
|
std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
|
2016-02-11 07:20:42 +08:00
|
|
|
}
|
|
|
|
|
2016-11-20 02:05:56 +08:00
|
|
|
// Fill [Buf, Buf + Size) with Filler. Filler is written in big
|
|
|
|
// endian order. This is used for linker script "=fillexp" command.
|
|
|
|
void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
|
|
|
|
uint8_t V[4];
|
|
|
|
write32be(V, Filler);
|
2016-02-26 22:48:31 +08:00
|
|
|
size_t I = 0;
|
2016-11-20 02:05:56 +08:00
|
|
|
for (; I + 4 < Size; I += 4)
|
|
|
|
memcpy(Buf + I, V, 4);
|
|
|
|
memcpy(Buf + I, V, Size - I);
|
2016-02-26 22:48:31 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
|
2016-11-29 16:05:44 +08:00
|
|
|
Loc = Buf;
|
2016-11-20 02:05:56 +08:00
|
|
|
if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name))
|
2016-11-09 09:42:41 +08:00
|
|
|
fill(Buf, this->Size, Filler);
|
2016-11-20 02:05:56 +08:00
|
|
|
|
2017-02-24 00:49:07 +08:00
|
|
|
auto Fn = [=](InputSection *IS) { IS->writeTo<ELFT>(Buf); };
|
2016-12-04 05:24:51 +08:00
|
|
|
forEach(Sections.begin(), Sections.end(), Fn);
|
2016-11-20 02:05:56 +08:00
|
|
|
|
2016-09-27 03:22:50 +08:00
|
|
|
// Linker scripts may have BYTE()-family commands with which you
|
|
|
|
// can write arbitrary bytes to the output. Process them if any.
|
|
|
|
Script<ELFT>::X->writeDataBytes(this->Name, Buf);
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:23:14 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static typename ELFT::uint getOutFlags(InputSectionBase *S) {
|
2016-10-26 20:36:56 +08:00
|
|
|
return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
|
2016-09-13 22:23:14 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 20:25:30 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
|
2017-01-05 22:20:35 +08:00
|
|
|
// The ELF spec just says
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// In the first phase, input sections that match in name, type and
|
|
|
|
// attribute flags should be concatenated into single sections.
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// However, it is clear that at least some flags have to be ignored for
|
|
|
|
// section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
|
|
|
|
// ignored. We should not have two output .text sections just because one was
|
|
|
|
// in a group and another was not for example.
|
|
|
|
//
|
|
|
|
// It also seems that that wording was a late addition and didn't get the
|
|
|
|
// necessary scrutiny.
|
|
|
|
//
|
|
|
|
// Merging sections with different flags is expected by some users. One
|
|
|
|
// reason is that if one file has
|
|
|
|
//
|
|
|
|
// int *const bar __attribute__((section(".foo"))) = (int *)0;
|
|
|
|
//
|
|
|
|
// gcc with -fPIC will produce a read only .foo section. But if another
|
|
|
|
// file has
|
|
|
|
//
|
|
|
|
// int zed;
|
|
|
|
// int *const bar __attribute__((section(".foo"))) = (int *)&zed;
|
|
|
|
//
|
|
|
|
// gcc with -fPIC will produce a read write section.
|
|
|
|
//
|
|
|
|
// Last but not least, when using linker script the merge rules are forced by
|
|
|
|
// the script. Unfortunately, linker scripts are name based. This means that
|
|
|
|
// expressions like *(.foo*) can refer to multiple input sections with
|
|
|
|
// different flags. We cannot put them in different output sections or we
|
|
|
|
// would produce wrong results for
|
|
|
|
//
|
|
|
|
// start = .; *(.foo.*) end = .; *(.bar)
|
|
|
|
//
|
|
|
|
// and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
|
|
|
|
// another. The problem is that there is no way to layout those output
|
|
|
|
// sections such that the .foo sections are the only thing between the start
|
|
|
|
// and end symbols.
|
|
|
|
//
|
|
|
|
// Given the above issues, we instead merge sections by name and error on
|
|
|
|
// incompatible types and flags.
|
|
|
|
|
2016-09-13 20:25:30 +08:00
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
|
|
|
|
uintX_t Alignment = 0;
|
2017-02-03 21:06:18 +08:00
|
|
|
uintX_t Flags = 0;
|
|
|
|
if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
|
2016-10-26 20:36:56 +08:00
|
|
|
Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
|
2017-02-03 21:06:18 +08:00
|
|
|
Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
|
|
|
|
}
|
2016-09-13 20:25:30 +08:00
|
|
|
|
2017-01-05 22:35:41 +08:00
|
|
|
return SectionKey{OutsecName, Flags, Alignment};
|
2016-09-13 20:25:30 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 01:32:26 +08:00
|
|
|
template <class ELFT>
|
|
|
|
OutputSectionFactory<ELFT>::OutputSectionFactory(
|
|
|
|
std::vector<OutputSectionBase *> &OutputSections)
|
|
|
|
: OutputSections(OutputSections) {}
|
2016-09-13 22:23:14 +08:00
|
|
|
|
2017-01-05 22:20:35 +08:00
|
|
|
static uint64_t getIncompatibleFlags(uint64_t Flags) {
|
|
|
|
return Flags & (SHF_ALLOC | SHF_TLS);
|
|
|
|
}
|
|
|
|
|
2017-01-31 18:26:52 +08:00
|
|
|
// We allow sections of types listed below to merged into a
|
|
|
|
// single progbits section. This is typically done by linker
|
|
|
|
// scripts. Merging nobits and progbits will force disk space
|
|
|
|
// to be allocated for nobits sections. Other ones don't require
|
|
|
|
// any special treatment on top of progbits, so there doesn't
|
|
|
|
// seem to be a harm in merging them.
|
|
|
|
static bool canMergeToProgbits(unsigned Type) {
|
|
|
|
return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
|
|
|
|
Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
|
|
|
|
Type == SHT_NOTE;
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> static void reportDiscarded(InputSectionBase *IS) {
|
2017-02-18 01:35:07 +08:00
|
|
|
if (!Config->PrintGcSections)
|
|
|
|
return;
|
2017-02-22 07:22:56 +08:00
|
|
|
message("removing unused section from '" + IS->Name + "' in file '" +
|
2017-02-23 10:28:28 +08:00
|
|
|
IS->getFile<ELFT>()->getName());
|
2017-02-18 01:35:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:23:14 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
void OutputSectionFactory<ELFT>::addInputSec(InputSectionBase *IS,
|
2017-02-17 01:32:26 +08:00
|
|
|
StringRef OutsecName) {
|
|
|
|
if (!IS->Live) {
|
2017-02-23 10:28:28 +08:00
|
|
|
reportDiscarded<ELFT>(IS);
|
2017-02-17 01:32:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
SectionKey Key = createKey<ELFT>(IS, OutsecName);
|
|
|
|
uintX_t Flags = getOutFlags<ELFT>(IS);
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase *&Sec = Map[Key];
|
2016-09-13 22:23:14 +08:00
|
|
|
if (Sec) {
|
2017-02-17 01:32:26 +08:00
|
|
|
if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
|
2017-01-05 22:20:35 +08:00
|
|
|
error("Section has flags incompatible with others with the same name " +
|
2017-02-17 01:32:26 +08:00
|
|
|
toString(IS));
|
|
|
|
if (Sec->Type != IS->Type) {
|
|
|
|
if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
|
2017-01-31 18:26:52 +08:00
|
|
|
Sec->Type = SHT_PROGBITS;
|
|
|
|
else
|
|
|
|
error("Section has different type from others with the same name " +
|
2017-02-17 01:32:26 +08:00
|
|
|
toString(IS));
|
2017-01-31 18:26:52 +08:00
|
|
|
}
|
2016-11-09 09:42:41 +08:00
|
|
|
Sec->Flags |= Flags;
|
2017-02-17 01:32:26 +08:00
|
|
|
} else {
|
|
|
|
uint32_t Type = IS->Type;
|
2017-02-23 10:28:28 +08:00
|
|
|
if (IS->kind() == InputSectionBase::EHFrame) {
|
2017-02-24 06:06:28 +08:00
|
|
|
In<ELFT>::EhFrame->addSection(IS);
|
2017-02-17 01:32:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
|
|
|
|
OutputSections.push_back(Sec);
|
2016-09-13 22:23:14 +08:00
|
|
|
}
|
2016-07-12 17:49:43 +08:00
|
|
|
|
2017-02-17 01:32:26 +08:00
|
|
|
Sec->addSection(IS);
|
2016-07-12 17:49:43 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 01:32:26 +08:00
|
|
|
template <class ELFT> OutputSectionFactory<ELFT>::~OutputSectionFactory() {}
|
|
|
|
|
2017-01-05 22:35:41 +08:00
|
|
|
SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
|
|
|
|
return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
|
2016-07-12 17:49:43 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 22:35:41 +08:00
|
|
|
SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
|
|
|
|
return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
|
2016-07-12 17:49:43 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 22:35:41 +08:00
|
|
|
unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
|
2017-01-05 22:20:35 +08:00
|
|
|
return hash_combine(Val.Name, Val.Flags, Val.Alignment);
|
2016-07-12 17:49:43 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 22:35:41 +08:00
|
|
|
bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
|
|
|
|
const SectionKey &RHS) {
|
2016-07-12 17:49:43 +08:00
|
|
|
return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
|
2017-01-05 22:20:35 +08:00
|
|
|
LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
|
2016-07-12 17:49:43 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
namespace lld {
|
2016-02-28 08:25:54 +08:00
|
|
|
namespace elf {
|
2016-11-10 07:23:45 +08:00
|
|
|
|
|
|
|
template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
|
|
|
|
template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
|
|
|
|
template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
|
|
|
|
template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
template class OutputSection<ELF32LE>;
|
|
|
|
template class OutputSection<ELF32BE>;
|
|
|
|
template class OutputSection<ELF64LE>;
|
|
|
|
template class OutputSection<ELF64BE>;
|
|
|
|
|
2016-07-12 17:49:43 +08:00
|
|
|
template class OutputSectionFactory<ELF32LE>;
|
|
|
|
template class OutputSectionFactory<ELF32BE>;
|
|
|
|
template class OutputSectionFactory<ELF64LE>;
|
|
|
|
template class OutputSectionFactory<ELF64BE>;
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
|
|
|
}
|