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"
|
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"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2018-03-01 01:38:19 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-10-14 02:22:55 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2017-07-28 03:22:43 +08:00
|
|
|
#include "llvm/Support/Compression.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;
|
|
|
|
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
|
|
|
|
2017-02-27 10:31:26 +08:00
|
|
|
uint8_t Out::First;
|
|
|
|
OutputSection *Out::Opd;
|
|
|
|
uint8_t *Out::OpdBuf;
|
|
|
|
PhdrEntry *Out::TlsPhdr;
|
|
|
|
OutputSection *Out::DebugInfo;
|
|
|
|
OutputSection *Out::ElfHeader;
|
|
|
|
OutputSection *Out::ProgramHeaders;
|
|
|
|
OutputSection *Out::PreinitArray;
|
|
|
|
OutputSection *Out::InitArray;
|
|
|
|
OutputSection *Out::FiniArray;
|
|
|
|
|
2017-06-14 07:26:31 +08:00
|
|
|
std::vector<OutputSection *> elf::OutputSections;
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
uint32_t OutputSection::getPhdrFlags() const {
|
2018-02-28 00:55:25 +08:00
|
|
|
uint32_t Ret = 0;
|
|
|
|
if (Config->EMachine != EM_ARM || !(Flags & SHF_ARM_PURECODE))
|
|
|
|
Ret |= PF_R;
|
2016-07-27 22:10:56 +08:00
|
|
|
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>
|
2017-02-24 23:07:30 +08:00
|
|
|
void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
|
2016-11-09 09:42:41 +08:00
|
|
|
Shdr->sh_entsize = Entsize;
|
2017-03-07 22:55:52 +08:00
|
|
|
Shdr->sh_addralign = Alignment;
|
2016-11-09 09:42:41 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
|
2017-07-28 03:22:43 +08:00
|
|
|
: BaseCommand(OutputSectionKind),
|
|
|
|
SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
|
2018-03-08 03:25:27 +08:00
|
|
|
/*Info*/ 0, /*Link*/ 0) {
|
2017-07-28 03:22:43 +08:00
|
|
|
Live = false;
|
|
|
|
}
|
2016-02-25 16:23:37 +08:00
|
|
|
|
2017-10-07 08:43:31 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSection::addSection(InputSection *IS) {
|
2017-10-07 08:58:19 +08:00
|
|
|
if (!Live) {
|
|
|
|
// If IS is the first section to be added to this section,
|
2018-02-28 00:55:25 +08:00
|
|
|
// initialize Type, Entsize and flags from IS.
|
2017-10-07 08:58:19 +08:00
|
|
|
Live = true;
|
|
|
|
Type = IS->Type;
|
2017-11-16 01:35:22 +08:00
|
|
|
Entsize = IS->Entsize;
|
2018-02-28 00:55:25 +08:00
|
|
|
Flags = IS->Flags;
|
2017-10-07 08:58:19 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, check if new type or flags are compatible with existing ones.
|
2018-03-08 23:06:58 +08:00
|
|
|
unsigned Mask = SHF_ALLOC | SHF_TLS | SHF_LINK_ORDER;
|
|
|
|
if ((Flags & Mask) != (IS->Flags & Mask))
|
2017-10-07 08:43:31 +08:00
|
|
|
error("incompatible section flags for " + Name + "\n>>> " + toString(IS) +
|
|
|
|
": 0x" + utohexstr(IS->Flags) + "\n>>> output section " + Name +
|
|
|
|
": 0x" + utohexstr(Flags));
|
|
|
|
|
|
|
|
if (Type != IS->Type) {
|
|
|
|
if (!canMergeToProgbits(Type) || !canMergeToProgbits(IS->Type))
|
|
|
|
error("section type mismatch for " + IS->Name + "\n>>> " +
|
|
|
|
toString(IS) + ": " +
|
|
|
|
getELFSectionTypeName(Config->EMachine, IS->Type) +
|
|
|
|
"\n>>> output section " + Name + ": " +
|
|
|
|
getELFSectionTypeName(Config->EMachine, Type));
|
|
|
|
Type = SHT_PROGBITS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IS->Parent = this;
|
2018-02-28 01:11:10 +08:00
|
|
|
uint64_t AndMask =
|
|
|
|
Config->EMachine == EM_ARM ? (uint64_t)SHF_ARM_PURECODE : 0;
|
2018-02-28 00:55:25 +08:00
|
|
|
uint64_t OrMask = ~AndMask;
|
|
|
|
uint64_t AndFlags = (Flags & IS->Flags) & AndMask;
|
|
|
|
uint64_t OrFlags = (Flags | IS->Flags) & OrMask;
|
|
|
|
Flags = AndFlags | OrFlags;
|
|
|
|
|
2017-10-07 08:58:34 +08:00
|
|
|
Alignment = std::max(Alignment, IS->Alignment);
|
[ELF] Reset OutputSection size prior to processing linker script commands
The size of an OutputSection is calculated early, to aid handling of compressed
debug sections. However, subsequent to this point, unused synthetic sections are
removed. In the event that an OutputSection, from which such an InputSection is
removed, is still required (e.g. because it has a symbol assignment), and no longer
has any InputSections, dot assignments, or BYTE()-family directives, the size
member is never updated when processing the commands. If the removed InputSection
had a non-zero size (such as a .got.plt section), the section ends up with the
wrong size in the output.
The fix is to reset the OutputSection size prior to processing the linker script
commands relating to that OutputSection. This ensures that the size is correct even
in the above situation.
Additionally, to reduce the risk of developers misusing OutputSection Size and
InputSection OutSecOff, they are set to simply the number of InputSections in an
OutputSection, and the corresponding index respectively. We cannot completely
stop using them, due to SHF_LINK_ORDER sections requiring them.
Compressed debug sections also require the full size. This is now calculated in
maybeCompress for these kinds of sections.
Reviewers: ruiu, rafael
Differential Revision: https://reviews.llvm.org/D38361
llvm-svn: 320472
2017-12-12 19:51:13 +08:00
|
|
|
IS->OutSecOff = Size++;
|
2017-05-08 18:18:12 +08:00
|
|
|
|
2017-03-01 12:04:23 +08:00
|
|
|
// If this section contains a table of fixed-size entries, sh_entsize
|
2017-11-16 01:35:22 +08:00
|
|
|
// holds the element size. If it contains elements of different size we
|
|
|
|
// set sh_entsize to 0.
|
|
|
|
if (Entsize != IS->Entsize)
|
|
|
|
Entsize = 0;
|
2017-07-28 03:22:43 +08:00
|
|
|
|
2017-10-07 08:43:31 +08:00
|
|
|
if (!IS->Assigned) {
|
|
|
|
IS->Assigned = true;
|
2017-10-11 09:50:56 +08:00
|
|
|
if (SectionCommands.empty() ||
|
|
|
|
!isa<InputSectionDescription>(SectionCommands.back()))
|
|
|
|
SectionCommands.push_back(make<InputSectionDescription>(""));
|
|
|
|
auto *ISD = cast<InputSectionDescription>(SectionCommands.back());
|
2017-10-07 08:43:31 +08:00
|
|
|
ISD->Sections.push_back(IS);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:20:08 +08:00
|
|
|
static void sortByOrder(MutableArrayRef<InputSection *> In,
|
|
|
|
std::function<int(InputSectionBase *S)> Order) {
|
2017-08-04 18:25:29 +08:00
|
|
|
typedef std::pair<int, InputSection *> Pair;
|
|
|
|
auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
|
|
|
|
|
|
|
|
std::vector<Pair> V;
|
|
|
|
for (InputSection *S : In)
|
|
|
|
V.push_back({Order(S), S});
|
|
|
|
std::stable_sort(V.begin(), V.end(), Comp);
|
|
|
|
|
|
|
|
for (size_t I = 0; I < V.size(); ++I)
|
|
|
|
In[I] = V[I].second;
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:40:58 +08:00
|
|
|
uint64_t elf::getHeaderSize() {
|
|
|
|
if (Config->OFormatBinary)
|
|
|
|
return 0;
|
|
|
|
return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
bool OutputSection::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == OutputSectionKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
|
2017-10-30 18:12:49 +08:00
|
|
|
assert(Live);
|
2018-02-02 03:30:15 +08:00
|
|
|
for (BaseCommand *B : SectionCommands)
|
|
|
|
if (auto *ISD = dyn_cast<InputSectionDescription>(B))
|
|
|
|
sortByOrder(ISD->Sections, Order);
|
2017-07-28 03:22:43 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compress section contents if this section contains debug info.
|
|
|
|
template <class ELFT> void OutputSection::maybeCompress() {
|
|
|
|
typedef typename ELFT::Chdr Elf_Chdr;
|
|
|
|
|
|
|
|
// Compress only DWARF debug sections.
|
|
|
|
if (!Config->CompressDebugSections || (Flags & SHF_ALLOC) ||
|
|
|
|
!Name.startswith(".debug_"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a section header.
|
|
|
|
ZDebugHeader.resize(sizeof(Elf_Chdr));
|
|
|
|
auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data());
|
|
|
|
Hdr->ch_type = ELFCOMPRESS_ZLIB;
|
|
|
|
Hdr->ch_size = Size;
|
|
|
|
Hdr->ch_addralign = Alignment;
|
|
|
|
|
|
|
|
// Write section contents to a temporary buffer and compress it.
|
|
|
|
std::vector<uint8_t> Buf(Size);
|
|
|
|
writeTo<ELFT>(Buf.data());
|
|
|
|
if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
|
|
|
|
fatal("compress failed: " + llvm::toString(std::move(E)));
|
|
|
|
|
|
|
|
// Update section headers.
|
|
|
|
Size = sizeof(Elf_Chdr) + CompressedData.size();
|
|
|
|
Flags |= SHF_COMPRESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
|
|
|
|
if (Size == 1)
|
|
|
|
*Buf = Data;
|
|
|
|
else if (Size == 2)
|
2018-03-21 02:07:25 +08:00
|
|
|
write16(Buf, Data);
|
2017-07-28 03:22:43 +08:00
|
|
|
else if (Size == 4)
|
2018-03-21 02:07:25 +08:00
|
|
|
write32(Buf, Data);
|
2017-07-28 03:22:43 +08:00
|
|
|
else if (Size == 8)
|
2018-03-21 02:07:25 +08:00
|
|
|
write64(Buf, Data);
|
2017-07-28 03:22:43 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("unsupported Size argument");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
|
|
|
|
if (Type == SHT_NOBITS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Loc = Buf;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (!CompressedData.empty()) {
|
|
|
|
memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
|
|
|
|
memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
|
|
|
|
CompressedData.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write leading padding.
|
2018-02-22 17:55:28 +08:00
|
|
|
std::vector<InputSection *> Sections = getInputSections(this);
|
2017-07-28 03:22:43 +08:00
|
|
|
uint32_t Filler = getFiller();
|
|
|
|
if (Filler)
|
|
|
|
fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler);
|
|
|
|
|
2017-10-11 13:13:16 +08:00
|
|
|
parallelForEachN(0, Sections.size(), [&](size_t I) {
|
2017-07-28 03:22:43 +08:00
|
|
|
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 + 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.
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *Base : SectionCommands)
|
2017-10-11 12:22:09 +08:00
|
|
|
if (auto *Data = dyn_cast<ByteCommand>(Base))
|
2017-07-28 03:22:43 +08:00
|
|
|
writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static void finalizeShtGroup(OutputSection *OS,
|
2017-12-12 21:38:51 +08:00
|
|
|
InputSection *Section) {
|
|
|
|
assert(Config->Relocatable);
|
2017-07-28 03:22:43 +08:00
|
|
|
|
|
|
|
// sh_link field for SHT_GROUP sections should contain the section index of
|
|
|
|
// the symbol table.
|
|
|
|
OS->Link = InX::SymTab->getParent()->SectionIndex;
|
|
|
|
|
|
|
|
// sh_info then contain index of an entry in symbol table section which
|
|
|
|
// provides signature of the section group.
|
2017-12-12 21:38:51 +08:00
|
|
|
ObjFile<ELFT> *Obj = Section->getFile<ELFT>();
|
2017-11-04 05:21:47 +08:00
|
|
|
ArrayRef<Symbol *> Symbols = Obj->getSymbols();
|
2017-12-12 21:38:51 +08:00
|
|
|
OS->Info = InX::SymTab->getSymbolIndex(Symbols[Section->Info]);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void OutputSection::finalize() {
|
2018-02-22 17:55:28 +08:00
|
|
|
if (Type == SHT_NOBITS)
|
|
|
|
for (BaseCommand *Base : SectionCommands)
|
|
|
|
if (isa<ByteCommand>(Base))
|
|
|
|
Type = SHT_PROGBITS;
|
|
|
|
|
|
|
|
std::vector<InputSection *> V = getInputSections(this);
|
|
|
|
InputSection *First = V.empty() ? nullptr : V[0];
|
2017-07-28 03:22:43 +08:00
|
|
|
|
2017-09-16 06:14:59 +08:00
|
|
|
if (Flags & SHF_LINK_ORDER) {
|
2017-07-28 03:22:43 +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-12-12 21:38:51 +08:00
|
|
|
if (auto *D = First->getLinkOrderDep())
|
2017-07-28 03:22:43 +08:00
|
|
|
Link = D->getParent()->SectionIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Type == SHT_GROUP) {
|
2017-12-12 21:38:51 +08:00
|
|
|
finalizeShtGroup<ELFT>(this, First);
|
2017-07-28 03:22:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isa<SyntheticSection>(First))
|
|
|
|
return;
|
|
|
|
|
|
|
|
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();
|
|
|
|
Info = S->getOutputSection()->SectionIndex;
|
|
|
|
Flags |= SHF_INFO_LINK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
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 OutputSection::sortCtorsDtors() {
|
2017-10-11 09:50:56 +08:00
|
|
|
assert(SectionCommands.size() == 1);
|
|
|
|
auto *ISD = cast<InputSectionDescription>(SectionCommands[0]);
|
2017-07-28 03:22:43 +08:00
|
|
|
std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
int elf::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;
|
|
|
|
}
|
|
|
|
|
2018-02-22 17:55:28 +08:00
|
|
|
std::vector<InputSection *> elf::getInputSections(OutputSection *OS) {
|
|
|
|
std::vector<InputSection *> Ret;
|
|
|
|
for (BaseCommand *Base : OS->SectionCommands)
|
|
|
|
if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
|
|
|
|
Ret.insert(Ret.end(), ISD->Sections.begin(), ISD->Sections.end());
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +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.
|
|
|
|
void OutputSection::sortInitFini() {
|
|
|
|
// Sort sections by priority.
|
|
|
|
sort([](InputSectionBase *S) { return getPriority(S->Name); });
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t OutputSection::getFiller() {
|
|
|
|
if (Filler)
|
|
|
|
return *Filler;
|
|
|
|
if (Flags & SHF_EXECINSTR)
|
|
|
|
return Target->TrapInstr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
|
|
|
|
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
|
|
|
|
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
|
|
|
|
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
|
2017-07-28 03:22:43 +08:00
|
|
|
|
|
|
|
template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
|
|
|
|
template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
|
|
|
|
template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
|
|
|
|
template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
|
|
|
|
|
|
|
|
template void OutputSection::maybeCompress<ELF32LE>();
|
|
|
|
template void OutputSection::maybeCompress<ELF32BE>();
|
|
|
|
template void OutputSection::maybeCompress<ELF64LE>();
|
|
|
|
template void OutputSection::maybeCompress<ELF64BE>();
|
|
|
|
|
|
|
|
template void OutputSection::finalize<ELF32LE>();
|
|
|
|
template void OutputSection::finalize<ELF32BE>();
|
|
|
|
template void OutputSection::finalize<ELF64LE>();
|
|
|
|
template void OutputSection::finalize<ELF64BE>();
|