2015-07-25 05:03:07 +08:00
|
|
|
//===- Writer.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-05 06:48:30 +08:00
|
|
|
#include "Writer.h"
|
2015-08-06 07:51:50 +08:00
|
|
|
#include "Config.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
#include "OutputSections.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
#include "Target.h"
|
2015-08-12 08:00:24 +08:00
|
|
|
|
2015-10-12 01:44:22 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2015-10-13 04:51:48 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2015-08-06 07:24:46 +08:00
|
|
|
namespace {
|
2015-09-09 03:43:27 +08:00
|
|
|
|
2015-10-11 06:34:30 +08:00
|
|
|
static uint32_t toPhdrFlags(uint64_t Flags) {
|
2015-09-18 03:58:07 +08:00
|
|
|
uint32_t Ret = PF_R;
|
|
|
|
if (Flags & SHF_WRITE)
|
|
|
|
Ret |= PF_W;
|
|
|
|
if (Flags & SHF_EXECINSTR)
|
|
|
|
Ret |= PF_X;
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2015-08-06 07:24:46 +08:00
|
|
|
// The writer writes a SymbolTable result to a file.
|
|
|
|
template <class ELFT> class Writer {
|
|
|
|
public:
|
2015-09-09 05:57:31 +08:00
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-09-17 04:45:57 +08:00
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
2015-09-16 23:54:15 +08:00
|
|
|
typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
2015-10-10 05:07:25 +08:00
|
|
|
Writer(SymbolTable<ELFT> &S) : Symtab(S) {}
|
2015-08-06 07:24:46 +08:00
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
2015-10-09 07:49:30 +08:00
|
|
|
void copyLocalSymbols();
|
2015-08-06 07:24:46 +08:00
|
|
|
void createSections();
|
2015-09-17 22:02:10 +08:00
|
|
|
template <bool isRela>
|
2015-09-22 01:47:00 +08:00
|
|
|
void scanRelocs(const InputSection<ELFT> &C,
|
2015-09-17 22:02:10 +08:00
|
|
|
iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels);
|
2015-09-22 01:47:00 +08:00
|
|
|
void scanRelocs(const InputSection<ELFT> &C);
|
2015-08-06 07:24:46 +08:00
|
|
|
void assignAddresses();
|
|
|
|
void openFile(StringRef OutputPath);
|
|
|
|
void writeHeader();
|
|
|
|
void writeSections();
|
2015-09-12 02:49:42 +08:00
|
|
|
bool needsInterpSection() const {
|
2015-10-08 05:25:39 +08:00
|
|
|
return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty();
|
2015-09-12 02:49:42 +08:00
|
|
|
}
|
2015-10-07 04:39:09 +08:00
|
|
|
bool isOutputDynamic() const {
|
2015-10-08 05:25:39 +08:00
|
|
|
return !Symtab.getSharedFiles().empty() || Config->Shared;
|
2015-09-12 06:42:45 +08:00
|
|
|
}
|
2015-10-09 06:43:55 +08:00
|
|
|
uintX_t getVAStart() const { return Config->Shared ? 0 : Target->getVAStart(); }
|
2015-08-06 07:24:46 +08:00
|
|
|
|
|
|
|
std::unique_ptr<llvm::FileOutputBuffer> Buffer;
|
2015-09-18 03:58:07 +08:00
|
|
|
|
2015-08-14 06:21:37 +08:00
|
|
|
llvm::SpecificBumpPtrAllocator<OutputSection<ELFT>> CAlloc;
|
2015-08-14 06:14:37 +08:00
|
|
|
std::vector<OutputSectionBase<ELFT::Is64Bits> *> OutputSections;
|
2015-09-09 01:39:39 +08:00
|
|
|
unsigned getNumSections() const { return OutputSections.size() + 1; }
|
2015-08-06 07:24:46 +08:00
|
|
|
|
2015-10-12 03:45:58 +08:00
|
|
|
void setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags, uintX_t FileOff,
|
2015-10-11 06:34:30 +08:00
|
|
|
uintX_t VA, uintX_t Align);
|
2015-10-12 03:45:58 +08:00
|
|
|
void copyPhdr(Elf_Phdr *PH, OutputSectionBase<ELFT::Is64Bits> *From);
|
2015-10-11 06:34:30 +08:00
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
SymbolTable<ELFT> &Symtab;
|
2015-10-11 07:59:57 +08:00
|
|
|
std::vector<Elf_Phdr> Phdrs;
|
2015-09-18 03:58:07 +08:00
|
|
|
|
2015-08-12 07:14:13 +08:00
|
|
|
uintX_t FileSize;
|
2015-08-06 07:24:46 +08:00
|
|
|
uintX_t SectionHeaderOff;
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> void lld::elf2::writeResult(SymbolTable<ELFT> *Symtab) {
|
2015-10-08 03:18:16 +08:00
|
|
|
// Initialize output sections that are handled by Writer specially.
|
|
|
|
// Don't reorder because the order of initialization matters.
|
|
|
|
InterpSection<ELFT::Is64Bits> Interp;
|
|
|
|
Out<ELFT>::Interp = &Interp;
|
|
|
|
StringTableSection<ELFT::Is64Bits> StrTab(false);
|
|
|
|
Out<ELFT>::StrTab = &StrTab;
|
|
|
|
StringTableSection<ELFT::Is64Bits> DynStrTab(true);
|
|
|
|
Out<ELFT>::DynStrTab = &DynStrTab;
|
|
|
|
OutputSection<ELFT> Bss(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
|
|
|
|
Out<ELFT>::Bss = &Bss;
|
|
|
|
GotSection<ELFT> Got;
|
|
|
|
Out<ELFT>::Got = &Got;
|
|
|
|
PltSection<ELFT> Plt;
|
|
|
|
Out<ELFT>::Plt = &Plt;
|
|
|
|
SymbolTableSection<ELFT> SymTab(*Symtab, *Out<ELFT>::StrTab);
|
|
|
|
Out<ELFT>::SymTab = &SymTab;
|
|
|
|
SymbolTableSection<ELFT> DynSymTab(*Symtab, *Out<ELFT>::DynStrTab);
|
|
|
|
Out<ELFT>::DynSymTab = &DynSymTab;
|
|
|
|
HashTableSection<ELFT> HashTab;
|
|
|
|
Out<ELFT>::HashTab = &HashTab;
|
2015-10-14 00:59:30 +08:00
|
|
|
RelocationSection<ELFT> RelaDyn(Symtab->shouldUseRela());
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::RelaDyn = &RelaDyn;
|
|
|
|
DynamicSection<ELFT> Dynamic(*Symtab);
|
|
|
|
Out<ELFT>::Dynamic = &Dynamic;
|
|
|
|
|
2015-10-08 05:25:39 +08:00
|
|
|
Writer<ELFT>(*Symtab).run();
|
2015-10-08 03:18:16 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// The main function of the writer.
|
2015-08-06 07:24:46 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::run() {
|
2015-10-09 07:49:30 +08:00
|
|
|
if (!Config->DiscardAll)
|
|
|
|
copyLocalSymbols();
|
2015-07-25 05:03:07 +08:00
|
|
|
createSections();
|
|
|
|
assignAddresses();
|
2015-08-06 07:51:50 +08:00
|
|
|
openFile(Config->OutputFile);
|
2015-07-25 05:03:07 +08:00
|
|
|
writeHeader();
|
|
|
|
writeSections();
|
|
|
|
error(Buffer->commit());
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:04:50 +08:00
|
|
|
namespace {
|
|
|
|
template <bool Is64Bits> struct SectionKey {
|
|
|
|
typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
|
|
|
|
StringRef Name;
|
2015-10-03 05:13:19 +08:00
|
|
|
uint32_t Type;
|
|
|
|
uintX_t Flags;
|
2015-08-14 01:04:50 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
namespace llvm {
|
|
|
|
template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> {
|
|
|
|
static SectionKey<Is64Bits> getEmptyKey() {
|
|
|
|
return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
|
|
|
|
}
|
|
|
|
static SectionKey<Is64Bits> getTombstoneKey() {
|
|
|
|
return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0,
|
|
|
|
0};
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(const SectionKey<Is64Bits> &Val) {
|
2015-10-03 05:13:19 +08:00
|
|
|
return hash_combine(Val.Name, Val.Type, Val.Flags);
|
2015-08-14 01:04:50 +08:00
|
|
|
}
|
|
|
|
static bool isEqual(const SectionKey<Is64Bits> &LHS,
|
|
|
|
const SectionKey<Is64Bits> &RHS) {
|
|
|
|
return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
|
2015-10-03 05:13:19 +08:00
|
|
|
LHS.Type == RHS.Type && LHS.Flags == RHS.Flags;
|
2015-08-14 01:04:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:54:15 +08:00
|
|
|
// The reason we have to do this early scan is as follows
|
|
|
|
// * To mmap the output file, we need to know the size
|
|
|
|
// * For that, we need to know how many dynamic relocs we will have.
|
|
|
|
// It might be possible to avoid this by outputting the file with write:
|
|
|
|
// * Write the allocated output sections, computing addresses.
|
|
|
|
// * Apply relocations, recording which ones require a dynamic reloc.
|
|
|
|
// * Write the dynamic relocations.
|
|
|
|
// * Write the rest of the file.
|
2015-09-17 22:02:10 +08:00
|
|
|
template <class ELFT>
|
|
|
|
template <bool isRela>
|
|
|
|
void Writer<ELFT>::scanRelocs(
|
2015-09-22 01:47:00 +08:00
|
|
|
const InputSection<ELFT> &C,
|
2015-09-17 22:02:10 +08:00
|
|
|
iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) {
|
|
|
|
typedef Elf_Rel_Impl<ELFT, isRela> RelType;
|
|
|
|
const ObjectFile<ELFT> &File = *C.getFile();
|
2015-09-24 23:11:50 +08:00
|
|
|
bool IsMips64EL = File.getObj().isMips64EL();
|
2015-09-17 22:02:10 +08:00
|
|
|
for (const RelType &RI : Rels) {
|
|
|
|
uint32_t SymIndex = RI.getSymbol(IsMips64EL);
|
2015-09-18 22:40:19 +08:00
|
|
|
SymbolBody *Body = File.getSymbolBody(SymIndex);
|
2015-09-21 23:11:29 +08:00
|
|
|
uint32_t Type = RI.getType(IsMips64EL);
|
2015-10-12 04:59:12 +08:00
|
|
|
|
|
|
|
// Set "used" bit for --as-needed.
|
|
|
|
if (Body && Body->isUndefined() && !Body->isWeak())
|
|
|
|
if (auto *S = dyn_cast<SharedSymbol<ELFT>>(Body->repl()))
|
|
|
|
S->File->IsUsed = true;
|
|
|
|
|
|
|
|
if (Body)
|
|
|
|
Body = Body->repl();
|
2015-10-06 03:30:12 +08:00
|
|
|
if (Body) {
|
|
|
|
if (Target->relocNeedsPlt(Type, *Body)) {
|
|
|
|
if (Body->isInPlt())
|
|
|
|
continue;
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Plt->addEntry(Body);
|
2015-10-14 00:59:30 +08:00
|
|
|
}
|
|
|
|
if (Target->relocNeedsGot(Type, *Body)) {
|
2015-10-06 03:30:12 +08:00
|
|
|
if (Body->isInGot())
|
|
|
|
continue;
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Got->addEntry(Body);
|
2015-10-06 03:30:12 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-12 04:59:12 +08:00
|
|
|
|
|
|
|
bool CBP = canBePreempted(Body);
|
|
|
|
if (!CBP && (!Config->Shared || Target->isRelRelative(Type)))
|
|
|
|
continue;
|
|
|
|
if (CBP)
|
2015-10-07 07:56:53 +08:00
|
|
|
Body->setUsedInDynamicReloc();
|
2015-10-14 00:59:30 +08:00
|
|
|
Out<ELFT>::RelaDyn->addReloc({C, RI});
|
2015-09-17 22:02:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:54:15 +08:00
|
|
|
template <class ELFT>
|
2015-09-22 01:47:00 +08:00
|
|
|
void Writer<ELFT>::scanRelocs(const InputSection<ELFT> &C) {
|
2015-09-24 23:11:50 +08:00
|
|
|
ObjectFile<ELFT> *File = C.getFile();
|
|
|
|
ELFFile<ELFT> &EObj = File->getObj();
|
2015-09-16 23:54:15 +08:00
|
|
|
|
|
|
|
if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const Elf_Shdr *RelSec : C.RelocSections) {
|
2015-09-17 22:02:10 +08:00
|
|
|
if (RelSec->sh_type == SHT_RELA)
|
2015-09-24 23:11:50 +08:00
|
|
|
scanRelocs(C, EObj.relas(RelSec));
|
2015-09-17 22:02:10 +08:00
|
|
|
else
|
2015-09-24 23:11:50 +08:00
|
|
|
scanRelocs(C, EObj.rels(RelSec));
|
2015-09-16 23:54:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 22:37:01 +08:00
|
|
|
template <class ELFT>
|
2015-10-10 05:07:25 +08:00
|
|
|
static void reportUndefined(const SymbolTable<ELFT> &S, const SymbolBody &Sym) {
|
2015-09-23 22:37:01 +08:00
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
|
|
|
|
2015-10-02 04:14:45 +08:00
|
|
|
if (Config->Shared && !Config->NoUndefined)
|
2015-10-02 01:24:24 +08:00
|
|
|
return;
|
|
|
|
|
2015-09-23 22:37:01 +08:00
|
|
|
const Elf_Sym &SymE = cast<ELFSymbolBody<ELFT>>(Sym).Sym;
|
2015-10-12 09:55:32 +08:00
|
|
|
ELFFileBase<ELFT> *SymFile = nullptr;
|
2015-09-23 22:37:01 +08:00
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &File : S.getObjectFiles()) {
|
|
|
|
Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable());
|
2015-09-23 22:37:01 +08:00
|
|
|
if (&SymE > Syms.begin() && &SymE < Syms.end())
|
2015-10-10 05:07:25 +08:00
|
|
|
SymFile = File.get();
|
2015-09-23 22:37:01 +08:00
|
|
|
}
|
2015-09-26 03:24:57 +08:00
|
|
|
|
|
|
|
std::string Message = "undefined symbol: " + Sym.getName().str();
|
2015-09-23 22:37:01 +08:00
|
|
|
if (SymFile)
|
2015-09-26 03:24:57 +08:00
|
|
|
Message += " in " + SymFile->getName().str();
|
|
|
|
if (Config->NoInhibitExec)
|
|
|
|
warning(Message);
|
2015-09-23 22:37:01 +08:00
|
|
|
else
|
2015-09-26 03:24:57 +08:00
|
|
|
error(Message);
|
2015-09-23 22:37:01 +08:00
|
|
|
}
|
|
|
|
|
2015-10-09 07:49:30 +08:00
|
|
|
// Local symbols are not in the linker's symbol table. This function scans
|
|
|
|
// each object file's symbol table to copy local symbols to the output.
|
|
|
|
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
|
2015-10-10 05:07:25 +08:00
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
|
|
|
|
for (const Elf_Sym &Sym : F->getLocalSymbols()) {
|
|
|
|
ErrorOr<StringRef> SymNameOrErr = Sym.getName(F->getStringTable());
|
2015-10-09 07:49:30 +08:00
|
|
|
error(SymNameOrErr);
|
|
|
|
StringRef SymName = *SymNameOrErr;
|
2015-10-10 05:07:25 +08:00
|
|
|
if (!shouldKeepInSymtab<ELFT>(*F, SymName, Sym))
|
2015-10-10 03:25:07 +08:00
|
|
|
continue;
|
|
|
|
Out<ELFT>::SymTab->addSymbol(SymName, true);
|
2015-10-09 07:49:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 04:51:48 +08:00
|
|
|
// PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
|
|
|
|
// we would like to make sure appear is a specific order to maximize their
|
|
|
|
// coverage by a single signed 16-bit offset from the TOC base pointer.
|
|
|
|
// Conversely, the special .tocbss section should be first among all SHT_NOBITS
|
|
|
|
// sections. This will put it next to the loaded special PPC64 sections (and,
|
|
|
|
// thus, within reach of the TOC base pointer).
|
|
|
|
static int getPPC64SectionRank(StringRef SectionName) {
|
|
|
|
return StringSwitch<int>(SectionName)
|
|
|
|
.Case(".tocbss", 0)
|
|
|
|
.Case(".branch_lt", 2)
|
|
|
|
.Case(".toc", 3)
|
|
|
|
.Case(".toc1", 4)
|
|
|
|
.Case(".opd", 5)
|
|
|
|
.Default(1);
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:49:30 +08:00
|
|
|
// Output section ordering is determined by this function.
|
|
|
|
template <class ELFT>
|
|
|
|
static bool compareOutputSections(OutputSectionBase<ELFT::Is64Bits> *A,
|
|
|
|
OutputSectionBase<ELFT::Is64Bits> *B) {
|
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
|
|
|
|
uintX_t AFlags = A->getFlags();
|
|
|
|
uintX_t BFlags = B->getFlags();
|
|
|
|
|
|
|
|
// Allocatable sections go first to reduce the total PT_LOAD size and
|
|
|
|
// so debug info doesn't change addresses in actual code.
|
|
|
|
bool AIsAlloc = AFlags & SHF_ALLOC;
|
|
|
|
bool BIsAlloc = BFlags & SHF_ALLOC;
|
|
|
|
if (AIsAlloc != BIsAlloc)
|
|
|
|
return AIsAlloc;
|
|
|
|
|
|
|
|
// We don't have any special requirements for the relative order of
|
|
|
|
// two non allocatable sections.
|
|
|
|
if (!AIsAlloc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We want the read only sections first so that they go in the PT_LOAD
|
|
|
|
// covering the program headers at the start of the file.
|
|
|
|
bool AIsWritable = AFlags & SHF_WRITE;
|
|
|
|
bool BIsWritable = BFlags & SHF_WRITE;
|
|
|
|
if (AIsWritable != BIsWritable)
|
|
|
|
return BIsWritable;
|
|
|
|
|
|
|
|
// For a corresponding reason, put non exec sections first (the program
|
|
|
|
// header PT_LOAD is not executable).
|
|
|
|
bool AIsExec = AFlags & SHF_EXECINSTR;
|
|
|
|
bool BIsExec = BFlags & SHF_EXECINSTR;
|
|
|
|
if (AIsExec != BIsExec)
|
|
|
|
return BIsExec;
|
|
|
|
|
|
|
|
// If we got here we know that both A and B and in the same PT_LOAD.
|
|
|
|
// The last requirement we have is to put nobits section last. The
|
|
|
|
// reason is that the only thing the dynamic linker will see about
|
|
|
|
// them is a p_memsz that is larger than p_filesz. Seeing that it
|
|
|
|
// zeros the end of the PT_LOAD, so that has to correspond to the
|
|
|
|
// nobits sections.
|
2015-10-13 05:14:03 +08:00
|
|
|
if (A->getType() != B->getType())
|
|
|
|
return A->getType() != SHT_NOBITS && B->getType() == SHT_NOBITS;
|
2015-10-13 04:51:48 +08:00
|
|
|
|
|
|
|
return getPPC64SectionRank(A->getName()) < getPPC64SectionRank(B->getName());
|
2015-10-09 07:49:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Until this function is called, common symbols do not belong to any section.
|
|
|
|
// This function adds them to end of BSS section.
|
|
|
|
template <class ELFT>
|
|
|
|
static void addCommonSymbols(std::vector<DefinedCommon<ELFT> *> &Syms) {
|
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
|
|
|
|
// Sort the common symbols by alignment as an heuristic to pack them better.
|
|
|
|
std::stable_sort(
|
|
|
|
Syms.begin(), Syms.end(),
|
|
|
|
[](const DefinedCommon<ELFT> *A, const DefinedCommon<ELFT> *B) {
|
|
|
|
return A->MaxAlignment > B->MaxAlignment;
|
|
|
|
});
|
|
|
|
|
|
|
|
uintX_t Off = Out<ELFT>::Bss->getSize();
|
|
|
|
for (DefinedCommon<ELFT> *C : Syms) {
|
|
|
|
const Elf_Sym &Sym = C->Sym;
|
|
|
|
uintX_t Align = C->MaxAlignment;
|
|
|
|
Off = RoundUpToAlignment(Off, Align);
|
|
|
|
C->OffsetInBSS = Off;
|
|
|
|
Off += Sym.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out<ELFT>::Bss->setSize(Off);
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Create output section objects and add them to OutputSections.
|
|
|
|
template <class ELFT> void Writer<ELFT>::createSections() {
|
2015-10-11 07:25:39 +08:00
|
|
|
// .interp needs to be on the first page in the output file.
|
|
|
|
if (needsInterpSection())
|
|
|
|
OutputSections.push_back(Out<ELFT>::Interp);
|
|
|
|
|
2015-08-14 06:21:37 +08:00
|
|
|
SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map;
|
2015-09-24 02:25:05 +08:00
|
|
|
|
2015-10-08 03:18:16 +08:00
|
|
|
OutputSections.push_back(Out<ELFT>::Bss);
|
|
|
|
Map[{Out<ELFT>::Bss->getName(), Out<ELFT>::Bss->getType(),
|
|
|
|
Out<ELFT>::Bss->getFlags()}] = Out<ELFT>::Bss;
|
2015-09-24 02:25:05 +08:00
|
|
|
|
2015-10-07 10:51:44 +08:00
|
|
|
// Declare linker generated symbols.
|
|
|
|
// This must be done before the relocation scan to make sure we can correctly
|
|
|
|
// decide if a dynamic relocation is needed or not.
|
|
|
|
// FIXME: Make this more declarative.
|
|
|
|
for (StringRef Name :
|
|
|
|
{"__preinit_array_start", "__preinit_array_end", "__init_array_start",
|
|
|
|
"__init_array_end", "__fini_array_start", "__fini_array_end"})
|
2015-10-10 05:07:25 +08:00
|
|
|
Symtab.addIgnoredSym(Name);
|
2015-10-07 10:51:44 +08:00
|
|
|
|
|
|
|
// __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
|
|
|
|
// static linking the linker is required to optimize away any references to
|
|
|
|
// __tls_get_addr, so it's not defined anywhere. Create a hidden definition
|
|
|
|
// to avoid the undefined symbol error.
|
|
|
|
if (!isOutputDynamic())
|
2015-10-10 05:07:25 +08:00
|
|
|
Symtab.addIgnoredSym("__tls_get_addr");
|
2015-10-07 10:51:44 +08:00
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab.getObjectFiles()) {
|
|
|
|
for (InputSection<ELFT> *C : F->getSections()) {
|
2015-10-10 03:25:07 +08:00
|
|
|
if (!C || C == &InputSection<ELFT>::Discarded)
|
2015-09-16 23:54:15 +08:00
|
|
|
continue;
|
|
|
|
const Elf_Shdr *H = C->getSectionHdr();
|
2015-10-10 03:25:07 +08:00
|
|
|
uintX_t OutFlags = H->sh_flags & ~SHF_GROUP;
|
|
|
|
SectionKey<ELFT::Is64Bits> Key{C->getSectionName(), H->sh_type, OutFlags};
|
2015-09-25 23:08:44 +08:00
|
|
|
OutputSection<ELFT> *&Sec = Map[Key];
|
|
|
|
if (!Sec) {
|
2015-10-08 03:18:16 +08:00
|
|
|
Sec = new (CAlloc.Allocate())
|
|
|
|
OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
|
2015-09-25 23:08:44 +08:00
|
|
|
OutputSections.push_back(Sec);
|
|
|
|
}
|
2015-09-22 08:16:19 +08:00
|
|
|
Sec->addSection(C);
|
2015-09-16 23:54:15 +08:00
|
|
|
scanRelocs(*C);
|
|
|
|
}
|
2015-09-01 08:16:38 +08:00
|
|
|
}
|
|
|
|
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Dynamic->PreInitArraySec =
|
2015-10-03 03:37:55 +08:00
|
|
|
Map.lookup({".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC});
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Dynamic->InitArraySec =
|
2015-10-03 03:37:55 +08:00
|
|
|
Map.lookup({".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC});
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Dynamic->FiniArraySec =
|
2015-10-03 03:37:55 +08:00
|
|
|
Map.lookup({".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC});
|
|
|
|
|
2015-10-08 05:25:39 +08:00
|
|
|
auto AddStartEnd = [&](StringRef Start, StringRef End,
|
|
|
|
OutputSection<ELFT> *OS) {
|
2015-10-06 09:16:17 +08:00
|
|
|
if (OS) {
|
2015-10-10 05:07:25 +08:00
|
|
|
Symtab.addSyntheticSym(Start, *OS, 0);
|
|
|
|
Symtab.addSyntheticSym(End, *OS, OS->getSize());
|
2015-10-06 09:16:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-07 03:57:05 +08:00
|
|
|
AddStartEnd("__preinit_array_start", "__preinit_array_end",
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Dynamic->PreInitArraySec);
|
2015-10-07 03:57:05 +08:00
|
|
|
AddStartEnd("__init_array_start", "__init_array_end",
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Dynamic->InitArraySec);
|
2015-10-07 03:57:05 +08:00
|
|
|
AddStartEnd("__fini_array_start", "__fini_array_end",
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::Dynamic->FiniArraySec);
|
2015-09-26 02:56:53 +08:00
|
|
|
|
2015-09-23 07:38:23 +08:00
|
|
|
// FIXME: Try to avoid the extra walk over all global symbols.
|
|
|
|
std::vector<DefinedCommon<ELFT> *> CommonSymbols;
|
|
|
|
for (auto &P : Symtab.getSymbols()) {
|
|
|
|
StringRef Name = P.first;
|
|
|
|
SymbolBody *Body = P.second->Body;
|
2015-10-05 17:43:57 +08:00
|
|
|
if (auto *U = dyn_cast<Undefined<ELFT>>(Body)) {
|
|
|
|
if (!U->isWeak() && !U->canKeepUndefined())
|
|
|
|
reportUndefined<ELFT>(Symtab, *Body);
|
|
|
|
}
|
2015-09-23 07:38:23 +08:00
|
|
|
|
|
|
|
if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
|
|
|
|
CommonSymbols.push_back(C);
|
2015-10-05 23:24:04 +08:00
|
|
|
if (!includeInSymtab<ELFT>(*Body))
|
2015-09-23 07:38:23 +08:00
|
|
|
continue;
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::SymTab->addSymbol(Name);
|
2015-09-23 07:38:23 +08:00
|
|
|
|
2015-10-13 07:39:23 +08:00
|
|
|
if (isOutputDynamic() && includeInDynamicSymtab(*Body))
|
2015-10-08 03:18:16 +08:00
|
|
|
Out<ELFT>::HashTab->addSymbol(Body);
|
2015-09-23 07:38:23 +08:00
|
|
|
}
|
2015-10-09 07:49:30 +08:00
|
|
|
addCommonSymbols(CommonSymbols);
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-08 03:18:16 +08:00
|
|
|
OutputSections.push_back(Out<ELFT>::SymTab);
|
2015-10-13 07:39:23 +08:00
|
|
|
if (isOutputDynamic()) {
|
2015-10-08 03:18:16 +08:00
|
|
|
OutputSections.push_back(Out<ELFT>::DynSymTab);
|
|
|
|
OutputSections.push_back(Out<ELFT>::HashTab);
|
|
|
|
OutputSections.push_back(Out<ELFT>::Dynamic);
|
|
|
|
OutputSections.push_back(Out<ELFT>::DynStrTab);
|
|
|
|
if (Out<ELFT>::RelaDyn->hasRelocs())
|
|
|
|
OutputSections.push_back(Out<ELFT>::RelaDyn);
|
2015-09-09 23:33:08 +08:00
|
|
|
}
|
2015-10-08 03:18:16 +08:00
|
|
|
if (!Out<ELFT>::Got->empty())
|
|
|
|
OutputSections.push_back(Out<ELFT>::Got);
|
|
|
|
if (!Out<ELFT>::Plt->empty())
|
|
|
|
OutputSections.push_back(Out<ELFT>::Plt);
|
2015-09-09 03:43:27 +08:00
|
|
|
|
2015-10-09 07:49:30 +08:00
|
|
|
std::stable_sort(OutputSections.begin(), OutputSections.end(),
|
|
|
|
compareOutputSections<ELFT>);
|
2015-09-25 11:48:25 +08:00
|
|
|
|
2015-10-02 05:15:02 +08:00
|
|
|
// Always put StrTabSec last so that no section names are added to it after
|
|
|
|
// it's finalized.
|
2015-10-08 03:18:16 +08:00
|
|
|
OutputSections.push_back(Out<ELFT>::StrTab);
|
2015-10-02 05:15:02 +08:00
|
|
|
|
2015-09-09 02:08:57 +08:00
|
|
|
for (unsigned I = 0, N = OutputSections.size(); I < N; ++I)
|
|
|
|
OutputSections[I]->setSectionIndex(I + 1);
|
2015-10-02 05:15:02 +08:00
|
|
|
|
2015-10-08 03:18:16 +08:00
|
|
|
// Fill the DynStrTab early.
|
|
|
|
Out<ELFT>::Dynamic->finalize();
|
2015-10-10 00:03:53 +08:00
|
|
|
|
|
|
|
// Fix each section's header (e.g. sh_size, sh_link, etc.)
|
|
|
|
for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
|
|
|
|
Out<ELFT>::StrTab->add(Sec->getName());
|
|
|
|
Sec->finalize();
|
|
|
|
}
|
[ELF2/PPC64] Resolve local-call relocations using the correct function-descriptor values
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
2015-10-13 07:16:53 +08:00
|
|
|
|
|
|
|
// If we have a .opd section (used under PPC64 for function descriptors),
|
|
|
|
// store a pointer to it here so that we can use it later when processing
|
|
|
|
// relocations.
|
|
|
|
Out<ELFT>::Opd = Map.lookup({".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC});
|
2015-08-13 23:23:46 +08:00
|
|
|
}
|
|
|
|
|
2015-09-10 04:48:09 +08:00
|
|
|
template <class ELFT>
|
2015-10-11 06:34:30 +08:00
|
|
|
static bool needsPhdr(OutputSectionBase<ELFT::Is64Bits> *Sec) {
|
2015-09-18 03:58:07 +08:00
|
|
|
return Sec->getFlags() & SHF_ALLOC;
|
2015-09-10 04:48:09 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Visits all sections to assign incremental, non-overlapping RVAs and
|
|
|
|
// file offsets.
|
|
|
|
template <class ELFT> void Writer<ELFT>::assignAddresses() {
|
2015-09-18 03:58:07 +08:00
|
|
|
assert(!OutputSections.empty() && "No output sections to layout!");
|
2015-10-11 06:34:30 +08:00
|
|
|
uintX_t VA = getVAStart() + sizeof(Elf_Ehdr);
|
|
|
|
uintX_t FileOff = sizeof(Elf_Ehdr);
|
2015-09-10 04:48:09 +08:00
|
|
|
|
2015-10-11 06:34:30 +08:00
|
|
|
// Reserve space for Phdrs.
|
2015-10-11 07:25:39 +08:00
|
|
|
int NumPhdrs = 2; // 2 for PhdrPhdr and FileHeaderPhdr
|
|
|
|
if (needsInterpSection())
|
|
|
|
++NumPhdrs;
|
2015-10-13 07:39:23 +08:00
|
|
|
if (isOutputDynamic())
|
2015-10-11 07:25:39 +08:00
|
|
|
++NumPhdrs;
|
|
|
|
uintX_t Last = PF_R;
|
|
|
|
for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
|
|
|
|
if (!Sec->getSize() || !needsPhdr<ELFT>(Sec))
|
|
|
|
continue;
|
|
|
|
uintX_t Flags = toPhdrFlags(Sec->getFlags());
|
|
|
|
if (Last != Flags) {
|
|
|
|
Last = Flags;
|
|
|
|
++NumPhdrs;
|
|
|
|
}
|
|
|
|
}
|
2015-10-12 01:44:22 +08:00
|
|
|
|
|
|
|
// Reserve space needed for the program header so that the array
|
|
|
|
// will never be resized.
|
2015-10-11 07:59:57 +08:00
|
|
|
Phdrs.reserve(NumPhdrs);
|
|
|
|
|
|
|
|
// The first Phdr entry is PT_PHDR which describes the program header itself.
|
|
|
|
Phdrs.emplace_back();
|
|
|
|
Elf_Phdr *PhdrPhdr = &Phdrs.back();
|
2015-10-12 03:45:58 +08:00
|
|
|
setPhdr(PhdrPhdr, PT_PHDR, PF_R, FileOff, VA, /*Align=*/8);
|
2015-10-11 07:59:57 +08:00
|
|
|
|
2015-10-11 07:25:39 +08:00
|
|
|
FileOff += sizeof(Elf_Phdr) * NumPhdrs;
|
|
|
|
VA += sizeof(Elf_Phdr) * NumPhdrs;
|
2015-08-13 23:23:46 +08:00
|
|
|
|
2015-10-11 07:59:57 +08:00
|
|
|
Elf_Phdr *Interp = nullptr;
|
|
|
|
if (needsInterpSection()) {
|
|
|
|
Phdrs.emplace_back();
|
|
|
|
Interp = &Phdrs.back();
|
|
|
|
}
|
2015-09-12 02:49:42 +08:00
|
|
|
|
2015-10-11 06:34:30 +08:00
|
|
|
// Create a Phdr for the file header.
|
2015-10-11 07:59:57 +08:00
|
|
|
Phdrs.emplace_back();
|
|
|
|
Elf_Phdr *FileHeader = &Phdrs.back();
|
2015-10-12 03:45:58 +08:00
|
|
|
setPhdr(FileHeader, PT_LOAD, PF_R, 0, getVAStart(), Target->getPageSize());
|
2015-09-10 23:41:34 +08:00
|
|
|
|
2015-10-12 01:44:22 +08:00
|
|
|
SmallPtrSet<Elf_Phdr *, 8> Closed;
|
2015-08-14 06:14:37 +08:00
|
|
|
for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
|
2015-09-18 03:58:07 +08:00
|
|
|
if (Sec->getSize()) {
|
2015-10-11 06:34:30 +08:00
|
|
|
uintX_t Flags = toPhdrFlags(Sec->getFlags());
|
2015-10-11 07:59:57 +08:00
|
|
|
Elf_Phdr *Last = &Phdrs.back();
|
2015-10-11 06:34:30 +08:00
|
|
|
if (Last->p_flags != Flags || !needsPhdr<ELFT>(Sec)) {
|
|
|
|
// Flags changed. End current Phdr and potentially create a new one.
|
|
|
|
if (Closed.insert(Last).second) {
|
|
|
|
Last->p_filesz = FileOff - Last->p_offset;
|
|
|
|
Last->p_memsz = VA - Last->p_vaddr;
|
2015-09-18 03:58:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-11 06:34:30 +08:00
|
|
|
if (needsPhdr<ELFT>(Sec)) {
|
2015-10-09 06:23:54 +08:00
|
|
|
VA = RoundUpToAlignment(VA, Target->getPageSize());
|
|
|
|
FileOff = RoundUpToAlignment(FileOff, Target->getPageSize());
|
2015-10-11 07:59:57 +08:00
|
|
|
Phdrs.emplace_back();
|
|
|
|
Elf_Phdr *PH = &Phdrs.back();
|
2015-10-12 03:45:58 +08:00
|
|
|
setPhdr(PH, PT_LOAD, Flags, FileOff, VA, Target->getPageSize());
|
2015-09-18 03:58:07 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 04:48:09 +08:00
|
|
|
}
|
|
|
|
|
2015-08-14 04:24:18 +08:00
|
|
|
uintX_t Align = Sec->getAlign();
|
|
|
|
uintX_t Size = Sec->getSize();
|
2015-08-13 23:31:17 +08:00
|
|
|
if (Sec->getFlags() & SHF_ALLOC) {
|
2015-09-15 03:00:35 +08:00
|
|
|
VA = RoundUpToAlignment(VA, Align);
|
2015-08-13 23:31:17 +08:00
|
|
|
Sec->setVA(VA);
|
2015-09-15 03:00:35 +08:00
|
|
|
VA += Size;
|
2015-08-13 23:31:17 +08:00
|
|
|
}
|
2015-09-15 03:00:35 +08:00
|
|
|
FileOff = RoundUpToAlignment(FileOff, Align);
|
2015-07-25 05:03:07 +08:00
|
|
|
Sec->setFileOffset(FileOff);
|
2015-09-01 04:23:57 +08:00
|
|
|
if (Sec->getType() != SHT_NOBITS)
|
2015-09-15 03:00:35 +08:00
|
|
|
FileOff += Size;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-08-12 08:00:24 +08:00
|
|
|
|
2015-10-11 07:59:57 +08:00
|
|
|
if (Interp) {
|
|
|
|
Interp->p_type = PT_INTERP;
|
2015-10-12 03:45:58 +08:00
|
|
|
copyPhdr(Interp, Out<ELFT>::Interp);
|
2015-10-11 06:34:30 +08:00
|
|
|
}
|
2015-10-13 07:39:23 +08:00
|
|
|
if (isOutputDynamic()) {
|
2015-10-11 07:59:57 +08:00
|
|
|
Phdrs.emplace_back();
|
|
|
|
Elf_Phdr *PH = &Phdrs.back();
|
|
|
|
PH->p_type = PT_DYNAMIC;
|
2015-10-12 03:45:58 +08:00
|
|
|
copyPhdr(PH, Out<ELFT>::Dynamic);
|
2015-10-11 06:34:30 +08:00
|
|
|
}
|
2015-08-12 09:45:28 +08:00
|
|
|
|
2015-10-09 23:31:49 +08:00
|
|
|
// Fix up the first entry's size.
|
2015-10-11 07:59:57 +08:00
|
|
|
PhdrPhdr->p_filesz = sizeof(Elf_Phdr) * Phdrs.size();
|
|
|
|
PhdrPhdr->p_memsz = sizeof(Elf_Phdr) * Phdrs.size();
|
2015-10-09 23:31:49 +08:00
|
|
|
|
2015-10-11 07:25:39 +08:00
|
|
|
// If nothing was merged into the file header PT_LOAD, set the size correctly.
|
2015-10-11 07:59:57 +08:00
|
|
|
if (FileHeader->p_filesz == Target->getPageSize()) {
|
2015-10-11 07:25:39 +08:00
|
|
|
uint64_t Size = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Phdrs.size();
|
2015-10-11 07:59:57 +08:00
|
|
|
FileHeader->p_filesz = Size;
|
|
|
|
FileHeader->p_memsz = Size;
|
2015-10-11 07:25:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-29 08:30:10 +08:00
|
|
|
// Add space for section headers.
|
2015-10-11 06:34:30 +08:00
|
|
|
FileOff = RoundUpToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4);
|
2015-07-29 08:30:10 +08:00
|
|
|
SectionHeaderOff = FileOff;
|
2015-09-09 05:57:31 +08:00
|
|
|
FileOff += getNumSections() * sizeof(Elf_Shdr);
|
2015-09-10 04:48:09 +08:00
|
|
|
FileSize = FileOff;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void Writer<ELFT>::writeHeader() {
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
2015-09-09 05:57:31 +08:00
|
|
|
auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
|
2015-07-25 05:03:07 +08:00
|
|
|
EHdr->e_ident[EI_MAG0] = 0x7F;
|
|
|
|
EHdr->e_ident[EI_MAG1] = 0x45;
|
|
|
|
EHdr->e_ident[EI_MAG2] = 0x4C;
|
|
|
|
EHdr->e_ident[EI_MAG3] = 0x46;
|
2015-08-05 23:08:40 +08:00
|
|
|
EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
|
|
|
|
EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little
|
|
|
|
? ELFDATA2LSB
|
|
|
|
: ELFDATA2MSB;
|
2015-07-25 05:03:07 +08:00
|
|
|
EHdr->e_ident[EI_VERSION] = EV_CURRENT;
|
2015-09-25 09:59:13 +08:00
|
|
|
|
2015-10-12 19:52:31 +08:00
|
|
|
auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
|
2015-09-25 09:59:13 +08:00
|
|
|
EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-09 06:55:28 +08:00
|
|
|
// FIXME: Generalize the segment construction similar to how we create
|
|
|
|
// output sections.
|
|
|
|
|
2015-09-12 06:42:45 +08:00
|
|
|
EHdr->e_type = Config->Shared ? ET_DYN : ET_EXEC;
|
2015-09-04 04:03:54 +08:00
|
|
|
EHdr->e_machine = FirstObj.getEMachine();
|
2015-07-25 05:03:07 +08:00
|
|
|
EHdr->e_version = EV_CURRENT;
|
2015-10-10 05:12:40 +08:00
|
|
|
if (Config->EntrySym)
|
|
|
|
if (auto *E = dyn_cast<ELFSymbolBody<ELFT>>(Config->EntrySym->repl()))
|
|
|
|
EHdr->e_entry = getSymVA<ELFT>(*E);
|
2015-10-11 06:36:36 +08:00
|
|
|
EHdr->e_phoff = sizeof(Elf_Ehdr);
|
2015-07-29 08:30:10 +08:00
|
|
|
EHdr->e_shoff = SectionHeaderOff;
|
2015-09-09 05:57:31 +08:00
|
|
|
EHdr->e_ehsize = sizeof(Elf_Ehdr);
|
|
|
|
EHdr->e_phentsize = sizeof(Elf_Phdr);
|
2015-10-11 06:34:30 +08:00
|
|
|
EHdr->e_phnum = Phdrs.size();
|
2015-09-09 05:57:31 +08:00
|
|
|
EHdr->e_shentsize = sizeof(Elf_Shdr);
|
2015-09-09 01:39:39 +08:00
|
|
|
EHdr->e_shnum = getNumSections();
|
2015-10-08 03:18:16 +08:00
|
|
|
EHdr->e_shstrndx = Out<ELFT>::StrTab->getSectionIndex();
|
2015-10-11 08:10:36 +08:00
|
|
|
memcpy(Buf + EHdr->e_phoff, &Phdrs[0], Phdrs.size() * sizeof(Phdrs[0]));
|
2015-09-09 06:55:28 +08:00
|
|
|
|
2015-09-09 05:57:31 +08:00
|
|
|
auto SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
|
2015-07-29 08:30:10 +08:00
|
|
|
// First entry is null.
|
|
|
|
++SHdrs;
|
2015-08-14 06:14:37 +08:00
|
|
|
for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
|
2015-10-08 03:18:16 +08:00
|
|
|
Sec->setNameOffset(Out<ELFT>::StrTab->getFileOff(Sec->getName()));
|
2015-08-14 02:37:23 +08:00
|
|
|
Sec->template writeHeaderTo<ELFT::TargetEndianness>(SHdrs++);
|
2015-08-12 08:00:24 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
|
2015-08-13 08:31:46 +08:00
|
|
|
ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
|
|
|
FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable);
|
|
|
|
error(BufferOrErr, Twine("failed to open ") + Path);
|
|
|
|
Buffer = std::move(*BufferOrErr);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write section contents to a mmap'ed file.
|
|
|
|
template <class ELFT> void Writer<ELFT>::writeSections() {
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
[ELF2/PPC64] Resolve local-call relocations using the correct function-descriptor values
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
2015-10-13 07:16:53 +08:00
|
|
|
|
|
|
|
// PPC64 needs to process relocations in the .opd section before processing
|
|
|
|
// relocations in code-containing sections.
|
2015-10-13 22:45:51 +08:00
|
|
|
if (OutputSectionBase<ELFT::Is64Bits> *Sec = Out<ELFT>::Opd) {
|
|
|
|
Out<ELFT>::OpdBuf = Buf + Sec->getFileOff();
|
|
|
|
Sec->writeTo(Buf + Sec->getFileOff());
|
|
|
|
}
|
[ELF2/PPC64] Resolve local-call relocations using the correct function-descriptor values
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
2015-10-13 07:16:53 +08:00
|
|
|
|
2015-08-14 06:14:37 +08:00
|
|
|
for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections)
|
2015-10-13 22:45:51 +08:00
|
|
|
if (Sec != Out<ELFT>::Opd)
|
[ELF2/PPC64] Resolve local-call relocations using the correct function-descriptor values
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
2015-10-13 07:16:53 +08:00
|
|
|
Sec->writeTo(Buf + Sec->getFileOff());
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-10-10 05:07:25 +08:00
|
|
|
|
2015-10-11 06:34:30 +08:00
|
|
|
template <class ELFT>
|
2015-10-12 03:45:58 +08:00
|
|
|
void Writer<ELFT>::setPhdr(Elf_Phdr *PH, uint32_t Type, uint32_t Flags,
|
2015-10-11 06:34:30 +08:00
|
|
|
uintX_t FileOff, uintX_t VA, uintX_t Align) {
|
|
|
|
PH->p_type = Type;
|
|
|
|
PH->p_flags = Flags;
|
|
|
|
PH->p_offset = FileOff;
|
|
|
|
PH->p_vaddr = VA;
|
|
|
|
PH->p_paddr = VA;
|
|
|
|
PH->p_align = Align;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-12 03:45:58 +08:00
|
|
|
void Writer<ELFT>::copyPhdr(Elf_Phdr *PH,
|
2015-10-11 06:34:30 +08:00
|
|
|
OutputSectionBase<ELFT::Is64Bits> *From) {
|
|
|
|
PH->p_flags = toPhdrFlags(From->getFlags());
|
|
|
|
PH->p_offset = From->getFileOff();
|
|
|
|
PH->p_vaddr = From->getVA();
|
|
|
|
PH->p_paddr = From->getVA();
|
|
|
|
PH->p_filesz = From->getSize();
|
|
|
|
PH->p_memsz = From->getSize();
|
|
|
|
PH->p_align = From->getAlign();
|
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template void lld::elf2::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab);
|
|
|
|
template void lld::elf2::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab);
|
|
|
|
template void lld::elf2::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab);
|
|
|
|
template void lld::elf2::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab);
|