2015-09-22 05:38:08 +08:00
|
|
|
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_ELF_OUTPUT_SECTIONS_H
|
|
|
|
#define LLD_ELF_OUTPUT_SECTIONS_H
|
|
|
|
|
|
|
|
#include "lld/Core/LLVM.h"
|
|
|
|
|
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
2015-09-25 11:56:11 +08:00
|
|
|
#include "Config.h"
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
|
|
|
|
|
|
|
class SymbolBody;
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> class SymbolTable;
|
2015-09-22 05:38:08 +08:00
|
|
|
template <class ELFT> class SymbolTableSection;
|
|
|
|
template <bool Is64Bits> class StringTableSection;
|
|
|
|
template <class ELFT> class InputSection;
|
|
|
|
template <class ELFT> class OutputSection;
|
|
|
|
template <class ELFT> class ObjectFile;
|
|
|
|
template <class ELFT> class DefinedRegular;
|
2015-09-26 02:19:03 +08:00
|
|
|
template <class ELFT> class ELFSymbolBody;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-08 03:18:16 +08:00
|
|
|
typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename llvm::object::ELFFile<ELFT>::uintX_t
|
2015-10-13 04:28:22 +08:00
|
|
|
getLocalRelTarget(const ObjectFile<ELFT> &File,
|
|
|
|
const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Sym);
|
2015-10-15 02:42:16 +08:00
|
|
|
bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
|
2015-10-05 23:24:04 +08:00
|
|
|
template <class ELFT> bool includeInSymtab(const SymbolBody &B);
|
|
|
|
|
2015-09-23 07:38:23 +08:00
|
|
|
bool includeInDynamicSymtab(const SymbolBody &B);
|
2015-10-06 00:25:43 +08:00
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool shouldKeepInSymtab(
|
2015-10-10 03:25:07 +08:00
|
|
|
const ObjectFile<ELFT> &File, StringRef Name,
|
|
|
|
const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
|
2015-09-25 11:56:11 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// This represents a section in an output file.
|
|
|
|
// Different sub classes represent different types of sections. Some contain
|
|
|
|
// input sections, others are created by the linker.
|
|
|
|
// The writer creates multiple OutputSections and assign them unique,
|
2015-09-22 05:38:08 +08:00
|
|
|
// non-overlapping file offsets and VAs.
|
|
|
|
template <bool Is64Bits> class OutputSectionBase {
|
|
|
|
public:
|
|
|
|
typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
|
|
|
|
typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
|
|
|
|
llvm::ELF::Elf32_Shdr>::type HeaderT;
|
|
|
|
|
|
|
|
OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
|
|
|
|
void setVA(uintX_t VA) { Header.sh_addr = VA; }
|
|
|
|
uintX_t getVA() const { return Header.sh_addr; }
|
|
|
|
void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
|
|
|
|
template <llvm::object::endianness E>
|
|
|
|
void writeHeaderTo(typename llvm::object::ELFFile<
|
|
|
|
llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
|
|
|
|
StringRef getName() { return Name; }
|
|
|
|
void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
|
|
|
|
|
|
|
|
unsigned getSectionIndex() const { return SectionIndex; }
|
|
|
|
void setSectionIndex(unsigned I) { SectionIndex = I; }
|
|
|
|
|
|
|
|
// Returns the size of the section in the output file.
|
2015-10-03 03:37:55 +08:00
|
|
|
uintX_t getSize() const { return Header.sh_size; }
|
2015-09-22 05:38:08 +08:00
|
|
|
void setSize(uintX_t Val) { Header.sh_size = Val; }
|
|
|
|
uintX_t getFlags() { return Header.sh_flags; }
|
|
|
|
uintX_t getFileOff() { return Header.sh_offset; }
|
|
|
|
uintX_t getAlign() {
|
|
|
|
// The ELF spec states that a value of 0 means the section has no alignment
|
|
|
|
// constraits.
|
|
|
|
return std::max<uintX_t>(Header.sh_addralign, 1);
|
|
|
|
}
|
|
|
|
uint32_t getType() { return Header.sh_type; }
|
|
|
|
|
|
|
|
virtual void finalize() {}
|
|
|
|
virtual void writeTo(uint8_t *Buf) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
StringRef Name;
|
|
|
|
HeaderT Header;
|
|
|
|
unsigned SectionIndex;
|
|
|
|
~OutputSectionBase() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
|
|
|
|
typedef OutputSectionBase<ELFT::Is64Bits> Base;
|
|
|
|
typedef typename Base::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
2015-10-08 03:18:16 +08:00
|
|
|
GotSection();
|
2015-09-22 05:38:08 +08:00
|
|
|
void finalize() override {
|
2015-10-14 22:02:06 +08:00
|
|
|
this->Header.sh_size = Entries.size() * sizeof(uintX_t);
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
2015-10-07 07:56:53 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-09-22 05:38:08 +08:00
|
|
|
void addEntry(SymbolBody *Sym);
|
|
|
|
bool empty() const { return Entries.empty(); }
|
|
|
|
uintX_t getEntryAddr(const SymbolBody &B) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<const SymbolBody *> Entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
|
|
|
|
typedef OutputSectionBase<ELFT::Is64Bits> Base;
|
|
|
|
typedef typename Base::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
2015-10-08 03:18:16 +08:00
|
|
|
PltSection();
|
2015-10-09 05:51:31 +08:00
|
|
|
void finalize() override;
|
2015-09-22 05:38:08 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
void addEntry(SymbolBody *Sym);
|
|
|
|
bool empty() const { return Entries.empty(); }
|
|
|
|
uintX_t getEntryAddr(const SymbolBody &B) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<const SymbolBody *> Entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT> struct DynamicReloc {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
const InputSection<ELFT> &C;
|
|
|
|
const Elf_Rel &RI;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
|
|
|
|
public:
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
|
|
|
typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
|
2015-10-10 05:07:25 +08:00
|
|
|
SymbolTableSection(SymbolTable<ELFT> &Table,
|
2015-10-08 03:18:16 +08:00
|
|
|
StringTableSection<ELFT::Is64Bits> &StrTabSec);
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2015-10-08 00:58:54 +08:00
|
|
|
void finalize() override;
|
2015-09-22 05:38:08 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-10-08 00:58:54 +08:00
|
|
|
void addSymbol(StringRef Name, bool isLocal = false);
|
2015-09-23 09:50:53 +08:00
|
|
|
StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
|
2015-09-22 05:38:08 +08:00
|
|
|
unsigned getNumSymbols() const { return NumVisible + 1; }
|
|
|
|
|
|
|
|
private:
|
2015-09-30 08:32:10 +08:00
|
|
|
void writeLocalSymbols(uint8_t *&Buf);
|
|
|
|
void writeGlobalSymbols(uint8_t *&Buf);
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
SymbolTable<ELFT> &Table;
|
2015-09-22 05:38:08 +08:00
|
|
|
StringTableSection<ELFT::Is64Bits> &StrTabSec;
|
|
|
|
unsigned NumVisible = 0;
|
|
|
|
unsigned NumLocals = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
2015-10-06 05:09:37 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
public:
|
2015-10-14 00:59:30 +08:00
|
|
|
RelocationSection(bool IsRela);
|
2015-09-22 05:38:08 +08:00
|
|
|
void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
|
|
|
|
void finalize() override;
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
bool hasRelocs() const { return !Relocs.empty(); }
|
|
|
|
bool isRela() const { return IsRela; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<DynamicReloc<ELFT>> Relocs;
|
|
|
|
const bool IsRela;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
|
|
|
|
public:
|
|
|
|
typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
2015-10-08 03:18:16 +08:00
|
|
|
OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
|
2015-09-22 08:16:19 +08:00
|
|
|
void addSection(InputSection<ELFT> *C);
|
2015-09-22 05:38:08 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
|
|
|
|
private:
|
2015-09-22 08:16:19 +08:00
|
|
|
std::vector<InputSection<ELFT> *> Sections;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <bool Is64Bits>
|
|
|
|
class InterpSection final : public OutputSectionBase<Is64Bits> {
|
|
|
|
public:
|
|
|
|
InterpSection();
|
|
|
|
|
|
|
|
void writeTo(uint8_t *Buf);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <bool Is64Bits>
|
|
|
|
class StringTableSection final : public OutputSectionBase<Is64Bits> {
|
|
|
|
public:
|
|
|
|
typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
|
2015-09-26 01:19:10 +08:00
|
|
|
StringTableSection(bool Dynamic);
|
2015-09-22 05:38:08 +08:00
|
|
|
void add(StringRef S) { StrTabBuilder.add(S); }
|
|
|
|
size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
|
|
|
|
StringRef data() const { return StrTabBuilder.data(); }
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
|
|
|
|
void finalize() override {
|
|
|
|
StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
|
|
|
|
this->Header.sh_size = StrTabBuilder.data().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDynamic() const { return Dynamic; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const bool Dynamic;
|
|
|
|
llvm::StringTableBuilder StrTabBuilder;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
|
|
|
|
|
|
|
public:
|
2015-10-08 03:18:16 +08:00
|
|
|
HashTableSection();
|
2015-09-22 05:38:08 +08:00
|
|
|
void addSymbol(SymbolBody *S);
|
2015-10-08 00:58:54 +08:00
|
|
|
void finalize() override;
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t hash(StringRef Name) {
|
|
|
|
uint32_t H = 0;
|
|
|
|
for (char C : Name) {
|
|
|
|
H = (H << 4) + C;
|
|
|
|
uint32_t G = H & 0xf0000000;
|
|
|
|
if (G)
|
|
|
|
H ^= G >> 24;
|
|
|
|
H &= ~G;
|
|
|
|
}
|
|
|
|
return H;
|
|
|
|
}
|
|
|
|
std::vector<uint32_t> Hashes;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
|
|
|
|
typedef OutputSectionBase<ELFT::Is64Bits> Base;
|
|
|
|
typedef typename Base::HeaderT HeaderT;
|
2015-10-01 05:57:53 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-10-03 00:21:30 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
public:
|
2015-10-10 05:07:25 +08:00
|
|
|
DynamicSection(SymbolTable<ELFT> &SymTab);
|
2015-09-22 05:38:08 +08:00
|
|
|
void finalize() override;
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
|
2015-10-15 23:29:53 +08:00
|
|
|
OutputSectionBase<ELFT::Is64Bits> *PreInitArraySec = nullptr;
|
|
|
|
OutputSectionBase<ELFT::Is64Bits> *InitArraySec = nullptr;
|
|
|
|
OutputSectionBase<ELFT::Is64Bits> *FiniArraySec = nullptr;
|
2015-10-03 03:37:55 +08:00
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
private:
|
2015-10-10 05:07:25 +08:00
|
|
|
SymbolTable<ELFT> &SymTab;
|
2015-10-05 18:29:46 +08:00
|
|
|
const ELFSymbolBody<ELFT> *InitSym = nullptr;
|
|
|
|
const ELFSymbolBody<ELFT> *FiniSym = nullptr;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
2015-10-08 03:18:16 +08:00
|
|
|
|
|
|
|
// All output sections that are hadnled by the linker specially are
|
|
|
|
// globally accessible. Writer initializes them, so don't use them
|
|
|
|
// until Writer is initialized.
|
|
|
|
template <class ELFT> struct Out {
|
|
|
|
static DynamicSection<ELFT> *Dynamic;
|
|
|
|
static GotSection<ELFT> *Got;
|
|
|
|
static HashTableSection<ELFT> *HashTab;
|
|
|
|
static InterpSection<ELFT::Is64Bits> *Interp;
|
|
|
|
static OutputSection<ELFT> *Bss;
|
2015-10-15 23:29:53 +08:00
|
|
|
static OutputSectionBase<ELFT::Is64Bits> *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
|
|
|
static uint8_t *OpdBuf;
|
2015-10-08 03:18:16 +08:00
|
|
|
static PltSection<ELFT> *Plt;
|
|
|
|
static RelocationSection<ELFT> *RelaDyn;
|
|
|
|
static StringTableSection<ELFT::Is64Bits> *DynStrTab;
|
|
|
|
static StringTableSection<ELFT::Is64Bits> *StrTab;
|
|
|
|
static SymbolTableSection<ELFT> *DynSymTab;
|
|
|
|
static SymbolTableSection<ELFT> *SymTab;
|
|
|
|
};
|
2015-10-10 03:34:55 +08:00
|
|
|
|
|
|
|
template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
|
|
|
|
template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
|
|
|
|
template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
|
|
|
|
template <class ELFT> InterpSection<ELFT::Is64Bits> *Out<ELFT>::Interp;
|
|
|
|
template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
|
2015-10-15 23:29:53 +08:00
|
|
|
template <class ELFT> OutputSectionBase<ELFT::Is64Bits> *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
|
|
|
template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
|
2015-10-10 03:34:55 +08:00
|
|
|
template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
|
|
|
|
template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
|
|
|
|
template <class ELFT> StringTableSection<ELFT::Is64Bits> *Out<ELFT>::DynStrTab;
|
|
|
|
template <class ELFT> StringTableSection<ELFT::Is64Bits> *Out<ELFT>::StrTab;
|
|
|
|
template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
|
|
|
|
template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|