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"
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
#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;
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> class StringTableSection;
|
2015-11-12 03:54:14 +08:00
|
|
|
template <class ELFT> class EHInputSection;
|
2015-09-22 05:38:08 +08:00
|
|
|
template <class ELFT> class InputSection;
|
2015-11-12 03:54:14 +08:00
|
|
|
template <class ELFT> class InputSectionBase;
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT> class MergeInputSection;
|
2015-12-20 18:57:34 +08:00
|
|
|
template <class ELFT> class MipsReginfoInputSection;
|
2015-09-22 05:38:08 +08:00
|
|
|
template <class ELFT> class OutputSection;
|
|
|
|
template <class ELFT> class ObjectFile;
|
|
|
|
template <class ELFT> class DefinedRegular;
|
|
|
|
|
2015-12-21 18:00:12 +08:00
|
|
|
// Flag to force GOT to be in output if we have relocations
|
|
|
|
// that relies on its address.
|
|
|
|
extern bool HasGotOffRel;
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
template <class ELFT>
|
2015-10-20 04:24:44 +08:00
|
|
|
static inline typename llvm::object::ELFFile<ELFT>::uintX_t
|
|
|
|
getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static inline typename llvm::object::ELFFile<ELFT>::uintX_t
|
|
|
|
getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
|
|
|
|
return Rel.r_addend;
|
|
|
|
}
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-20 04:24:44 +08:00
|
|
|
typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
|
|
|
|
|
|
|
|
template <class ELFT, bool IsRela>
|
2015-09-22 05:38:08 +08:00
|
|
|
typename llvm::object::ELFFile<ELFT>::uintX_t
|
2015-10-13 04:28:22 +08:00
|
|
|
getLocalRelTarget(const ObjectFile<ELFT> &File,
|
2015-12-21 18:37:33 +08:00
|
|
|
const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
|
|
|
|
typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
|
2015-10-05 23:24:04 +08:00
|
|
|
|
2015-12-25 15:01:09 +08:00
|
|
|
bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
|
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.
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> class OutputSectionBase {
|
2015-09-22 05:38:08 +08:00
|
|
|
public:
|
2015-10-16 06:27:29 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
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; }
|
2015-10-16 06:27:29 +08:00
|
|
|
void writeHeaderTo(Elf_Shdr *SHdr);
|
2015-09-22 05:38:08 +08:00
|
|
|
StringRef getName() { return Name; }
|
|
|
|
|
2015-12-26 13:51:07 +08:00
|
|
|
virtual void addSection(InputSectionBase<ELFT> *C) {}
|
|
|
|
|
2015-10-16 04:55:22 +08:00
|
|
|
unsigned SectionIndex;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
// 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; }
|
2015-11-03 22:13:40 +08:00
|
|
|
void updateAlign(uintX_t Align) {
|
|
|
|
if (Align > Header.sh_addralign)
|
|
|
|
Header.sh_addralign = Align;
|
|
|
|
}
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
virtual void finalize() {}
|
|
|
|
virtual void writeTo(uint8_t *Buf) = 0;
|
2015-12-26 15:01:26 +08:00
|
|
|
virtual ~OutputSectionBase() = default;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
StringRef Name;
|
2015-10-16 06:27:29 +08:00
|
|
|
Elf_Shdr Header;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
|
|
|
|
typedef OutputSectionBase<ELFT> Base;
|
2015-09-22 05:38:08 +08:00
|
|
|
typedef typename Base::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
2015-10-08 03:18:16 +08:00
|
|
|
GotSection();
|
2015-11-06 15:43:03 +08:00
|
|
|
void finalize() override;
|
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);
|
2015-12-02 03:20:26 +08:00
|
|
|
bool addDynTlsEntry(SymbolBody *Sym);
|
2015-12-07 16:02:20 +08:00
|
|
|
bool addCurrentModuleTlsIndex();
|
2015-09-22 05:38:08 +08:00
|
|
|
bool empty() const { return Entries.empty(); }
|
|
|
|
uintX_t getEntryAddr(const SymbolBody &B) const;
|
2015-12-02 03:20:26 +08:00
|
|
|
uintX_t getGlobalDynAddr(const SymbolBody &B) const;
|
2015-12-02 17:58:20 +08:00
|
|
|
uintX_t getNumEntries() const { return Entries.size(); }
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2015-11-12 12:39:49 +08:00
|
|
|
// Returns the symbol which corresponds to the first entry of the global part
|
|
|
|
// of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
|
|
|
|
// table properties.
|
|
|
|
// Returns nullptr if the global part is empty.
|
|
|
|
const SymbolBody *getMipsFirstGlobalEntry() const;
|
|
|
|
|
|
|
|
// Returns the number of entries in the local part of GOT including
|
|
|
|
// the number of reserved entries. This method is MIPS-specific.
|
|
|
|
unsigned getMipsLocalEntriesNum() const;
|
|
|
|
|
2015-12-02 02:24:07 +08:00
|
|
|
uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
private:
|
|
|
|
std::vector<const SymbolBody *> Entries;
|
2015-12-02 02:24:07 +08:00
|
|
|
uint32_t LocalTlsIndexOff = -1;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
2015-10-20 16:54:27 +08:00
|
|
|
template <class ELFT>
|
|
|
|
class GotPltSection final : public OutputSectionBase<ELFT> {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GotPltSection();
|
|
|
|
void finalize() override;
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
void addEntry(SymbolBody *Sym);
|
|
|
|
bool empty() const;
|
|
|
|
uintX_t getEntryAddr(const SymbolBody &B) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<const SymbolBody *> Entries;
|
|
|
|
};
|
|
|
|
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
|
|
|
|
typedef OutputSectionBase<ELFT> Base;
|
2015-09-22 05:38:08 +08:00
|
|
|
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:
|
2015-11-26 06:15:01 +08:00
|
|
|
std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT> struct DynamicReloc {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
2015-11-13 08:28:34 +08:00
|
|
|
InputSectionBase<ELFT> *C;
|
|
|
|
const Elf_Rel *RI;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-16 06:27:29 +08:00
|
|
|
class SymbolTableSection final : public OutputSectionBase<ELFT> {
|
2015-09-22 05:38:08 +08:00
|
|
|
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;
|
2015-10-16 06:27:29 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-10-10 05:07:25 +08:00
|
|
|
SymbolTableSection(SymbolTable<ELFT> &Table,
|
2015-10-16 06:27:29 +08:00
|
|
|
StringTableSection<ELFT> &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-21 05:47:58 +08:00
|
|
|
void addLocalSymbol(StringRef Name);
|
|
|
|
void addSymbol(SymbolBody *Body);
|
2015-10-16 06:27:29 +08:00
|
|
|
StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
|
2015-09-22 05:38:08 +08:00
|
|
|
unsigned getNumSymbols() const { return NumVisible + 1; }
|
|
|
|
|
2015-10-28 15:05:56 +08:00
|
|
|
ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
|
2015-10-21 05:47:58 +08:00
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
private:
|
2015-09-30 08:32:10 +08:00
|
|
|
void writeLocalSymbols(uint8_t *&Buf);
|
2015-10-19 16:01:51 +08:00
|
|
|
void writeGlobalSymbols(uint8_t *Buf);
|
2015-09-30 08:32:10 +08:00
|
|
|
|
2015-10-21 04:52:14 +08:00
|
|
|
static uint8_t getSymbolBinding(SymbolBody *Body);
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
SymbolTable<ELFT> &Table;
|
2015-10-16 06:27:29 +08:00
|
|
|
StringTableSection<ELFT> &StrTabSec;
|
2015-10-28 15:05:56 +08:00
|
|
|
std::vector<SymbolBody *> Symbols;
|
2015-09-22 05:38:08 +08:00
|
|
|
unsigned NumVisible = 0;
|
|
|
|
unsigned NumLocals = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-16 06:27:29 +08:00
|
|
|
class RelocationSection final : public OutputSectionBase<ELFT> {
|
2015-09-22 05:38:08 +08:00
|
|
|
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-20 16:54:27 +08:00
|
|
|
RelocationSection(StringRef Name, bool IsRela);
|
2015-09-22 05:38:08 +08:00
|
|
|
void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
|
2015-11-26 06:15:01 +08:00
|
|
|
unsigned getRelocOffset();
|
2015-09-22 05:38:08 +08:00
|
|
|
void finalize() override;
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
bool hasRelocs() const { return !Relocs.empty(); }
|
|
|
|
bool isRela() const { return IsRela; }
|
|
|
|
|
2015-12-21 18:12:06 +08:00
|
|
|
bool Static = false;
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
private:
|
2015-12-01 01:49:19 +08:00
|
|
|
bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
|
|
|
|
Elf_Rel *N);
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
std::vector<DynamicReloc<ELFT>> Relocs;
|
|
|
|
const bool IsRela;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-16 06:27:29 +08:00
|
|
|
class OutputSection final : public OutputSectionBase<ELFT> {
|
2015-09-22 05:38:08 +08:00
|
|
|
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_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
2015-10-16 06:27:29 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-10-08 03:18:16 +08:00
|
|
|
OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
|
2015-12-26 13:51:07 +08:00
|
|
|
void addSection(InputSectionBase<ELFT> *C) override;
|
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
|
|
|
};
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT>
|
|
|
|
class MergeOutputSection final : public OutputSectionBase<ELFT> {
|
|
|
|
typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
|
|
|
|
|
2015-10-25 06:51:01 +08:00
|
|
|
bool shouldTailMerge() const;
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
public:
|
|
|
|
MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
|
2015-12-26 13:51:07 +08:00
|
|
|
void addSection(InputSectionBase<ELFT> *S) override;
|
2015-10-20 05:00:02 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-10-25 06:51:01 +08:00
|
|
|
unsigned getOffset(StringRef Val);
|
|
|
|
void finalize() override;
|
2015-10-20 05:00:02 +08:00
|
|
|
|
|
|
|
private:
|
2015-10-25 06:51:01 +08:00
|
|
|
llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
|
2015-10-20 05:00:02 +08:00
|
|
|
};
|
|
|
|
|
2015-11-12 03:54:14 +08:00
|
|
|
// FDE or CIE
|
|
|
|
template <class ELFT> struct EHRegion {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
EHRegion(EHInputSection<ELFT> *S, unsigned Index);
|
|
|
|
StringRef data() const;
|
|
|
|
EHInputSection<ELFT> *S;
|
|
|
|
unsigned Index;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT> struct Cie : public EHRegion<ELFT> {
|
|
|
|
Cie(EHInputSection<ELFT> *S, unsigned Index);
|
|
|
|
std::vector<EHRegion<ELFT>> Fdes;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class EHOutputSection final : public OutputSectionBase<ELFT> {
|
|
|
|
public:
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
|
|
|
EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
|
|
|
|
template <bool IsRela>
|
|
|
|
void addSectionAux(
|
|
|
|
EHInputSection<ELFT> *S,
|
|
|
|
llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
|
|
|
|
Rels);
|
|
|
|
|
2015-12-26 13:51:07 +08:00
|
|
|
void addSection(InputSectionBase<ELFT> *S) override;
|
2015-11-12 03:54:14 +08:00
|
|
|
|
|
|
|
private:
|
2015-12-17 17:23:40 +08:00
|
|
|
uintX_t readEntryLength(ArrayRef<uint8_t> D);
|
|
|
|
|
2015-11-12 03:54:14 +08:00
|
|
|
std::vector<EHInputSection<ELFT> *> Sections;
|
|
|
|
std::vector<Cie<ELFT>> Cies;
|
|
|
|
|
|
|
|
// Maps CIE content + personality to a index in Cies.
|
|
|
|
llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
|
|
|
|
};
|
|
|
|
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT>
|
|
|
|
class InterpSection final : public OutputSectionBase<ELFT> {
|
2015-09-22 05:38:08 +08:00
|
|
|
public:
|
|
|
|
InterpSection();
|
2015-11-04 10:11:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT>
|
|
|
|
class StringTableSection final : public OutputSectionBase<ELFT> {
|
2015-09-22 05:38:08 +08:00
|
|
|
public:
|
2015-10-16 06:27:29 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-10-21 01:21:35 +08:00
|
|
|
StringTableSection(StringRef Name, bool Dynamic);
|
2016-01-07 10:35:32 +08:00
|
|
|
void reserve(StringRef S);
|
|
|
|
size_t addString(StringRef S);
|
2015-09-22 05:38:08 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
2016-01-07 10:35:32 +08:00
|
|
|
size_t getSize() const { return Used + Reserved; }
|
|
|
|
void finalize() override { this->Header.sh_size = getSize(); }
|
2015-09-22 05:38:08 +08:00
|
|
|
bool isDynamic() const { return Dynamic; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const bool Dynamic;
|
2016-01-07 10:35:32 +08:00
|
|
|
std::vector<StringRef> Strings;
|
|
|
|
size_t Used = 1; // ELF string tables start with a NUL byte, so 1.
|
|
|
|
size_t Reserved = 0;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-16 06:27:29 +08:00
|
|
|
class HashTableSection final : public OutputSectionBase<ELFT> {
|
2015-09-22 05:38:08 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
|
|
|
|
|
|
|
public:
|
2015-10-08 03:18:16 +08:00
|
|
|
HashTableSection();
|
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
|
|
|
};
|
|
|
|
|
2015-10-22 16:21:35 +08:00
|
|
|
// Outputs GNU Hash section. For detailed explanation see:
|
|
|
|
// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
|
|
|
|
template <class ELFT>
|
|
|
|
class GnuHashTableSection final : public OutputSectionBase<ELFT> {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GnuHashTableSection();
|
|
|
|
void finalize() override;
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
|
2015-10-28 15:05:56 +08:00
|
|
|
// Adds symbols to the hash table.
|
|
|
|
// Sorts the input to satisfy GNU hash section requirements.
|
|
|
|
void addSymbols(std::vector<SymbolBody *> &Symbols);
|
2015-10-22 16:21:35 +08:00
|
|
|
|
|
|
|
private:
|
2015-10-28 15:05:56 +08:00
|
|
|
static unsigned calcNBuckets(unsigned NumHashed);
|
2015-10-22 16:21:35 +08:00
|
|
|
static unsigned calcMaskWords(unsigned NumHashed);
|
|
|
|
|
|
|
|
void writeHeader(uint8_t *&Buf);
|
|
|
|
void writeBloomFilter(uint8_t *&Buf);
|
|
|
|
void writeHashTable(uint8_t *Buf);
|
|
|
|
|
2015-10-28 15:05:56 +08:00
|
|
|
struct HashedSymbolData {
|
|
|
|
SymbolBody *Body;
|
|
|
|
uint32_t Hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<HashedSymbolData> HashedSymbols;
|
|
|
|
|
2015-10-22 16:21:35 +08:00
|
|
|
unsigned MaskWords;
|
|
|
|
unsigned NBuckets;
|
|
|
|
unsigned Shift2;
|
|
|
|
};
|
|
|
|
|
2015-09-22 05:38:08 +08:00
|
|
|
template <class ELFT>
|
2015-10-16 06:27:29 +08:00
|
|
|
class DynamicSection final : public OutputSectionBase<ELFT> {
|
|
|
|
typedef OutputSectionBase<ELFT> Base;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
|
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;
|
2015-10-16 06:27:29 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
2015-10-01 05:57:53 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
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-16 06:27:29 +08:00
|
|
|
OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
|
|
|
|
OutputSectionBase<ELFT> *InitArraySec = nullptr;
|
|
|
|
OutputSectionBase<ELFT> *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-12-22 04:59:29 +08:00
|
|
|
const SymbolBody *InitSym = nullptr;
|
|
|
|
const SymbolBody *FiniSym = nullptr;
|
2015-10-22 01:47:10 +08:00
|
|
|
uint32_t DtFlags = 0;
|
|
|
|
uint32_t DtFlags1 = 0;
|
2015-09-22 05:38:08 +08:00
|
|
|
};
|
2015-10-08 03:18:16 +08:00
|
|
|
|
2015-12-20 18:57:34 +08:00
|
|
|
template <class ELFT>
|
|
|
|
class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
|
|
|
|
typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MipsReginfoOutputSection();
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-12-26 13:51:07 +08:00
|
|
|
void addSection(InputSectionBase<ELFT> *S) override;
|
2015-12-20 18:57:34 +08:00
|
|
|
|
|
|
|
private:
|
2016-01-07 06:42:43 +08:00
|
|
|
uint32_t GprMask = 0;
|
2015-12-20 18:57:34 +08:00
|
|
|
};
|
|
|
|
|
2016-01-07 07:25:42 +08:00
|
|
|
inline uint64_t align(uint64_t Value, uint64_t Align) {
|
2016-01-07 07:34:12 +08:00
|
|
|
return llvm::RoundUpToAlignment(Value, Align);
|
2016-01-07 07:25:42 +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 {
|
2015-11-04 06:39:09 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-11-07 06:14:44 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
|
2015-10-08 03:18:16 +08:00
|
|
|
static DynamicSection<ELFT> *Dynamic;
|
2015-10-22 16:21:35 +08:00
|
|
|
static GnuHashTableSection<ELFT> *GnuHashTab;
|
2015-10-20 16:54:27 +08:00
|
|
|
static GotPltSection<ELFT> *GotPlt;
|
2015-10-08 03:18:16 +08:00
|
|
|
static GotSection<ELFT> *Got;
|
|
|
|
static HashTableSection<ELFT> *HashTab;
|
2015-10-16 06:27:29 +08:00
|
|
|
static InterpSection<ELFT> *Interp;
|
2015-10-08 03:18:16 +08:00
|
|
|
static OutputSection<ELFT> *Bss;
|
2015-11-12 12:39:49 +08:00
|
|
|
static OutputSection<ELFT> *MipsRldMap;
|
2015-10-16 06:27:29 +08:00
|
|
|
static OutputSectionBase<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
|
|
|
static uint8_t *OpdBuf;
|
2015-10-08 03:18:16 +08:00
|
|
|
static PltSection<ELFT> *Plt;
|
|
|
|
static RelocationSection<ELFT> *RelaDyn;
|
2015-10-20 16:54:27 +08:00
|
|
|
static RelocationSection<ELFT> *RelaPlt;
|
2015-10-16 06:27:29 +08:00
|
|
|
static StringTableSection<ELFT> *DynStrTab;
|
2015-10-21 01:21:35 +08:00
|
|
|
static StringTableSection<ELFT> *ShStrTab;
|
2015-10-16 06:27:29 +08:00
|
|
|
static StringTableSection<ELFT> *StrTab;
|
2015-10-08 03:18:16 +08:00
|
|
|
static SymbolTableSection<ELFT> *DynSymTab;
|
|
|
|
static SymbolTableSection<ELFT> *SymTab;
|
2015-11-07 06:14:44 +08:00
|
|
|
static Elf_Phdr *TlsPhdr;
|
2015-10-08 03:18:16 +08:00
|
|
|
};
|
2015-10-10 03:34:55 +08:00
|
|
|
|
|
|
|
template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
|
2015-10-22 16:21:35 +08:00
|
|
|
template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
|
2015-10-20 16:54:27 +08:00
|
|
|
template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
|
2015-10-10 03:34:55 +08:00
|
|
|
template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
|
|
|
|
template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
|
2015-11-04 06:01:20 +08:00
|
|
|
template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
|
2015-11-12 12:39:49 +08:00
|
|
|
template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> OutputSectionBase<ELFT> *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;
|
2015-10-20 16:54:27 +08:00
|
|
|
template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
|
2015-10-21 01:21:35 +08:00
|
|
|
template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
|
2015-10-10 03:34:55 +08:00
|
|
|
template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
|
|
|
|
template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
|
2015-11-07 06:14:44 +08:00
|
|
|
template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
|
2015-11-04 10:11:57 +08:00
|
|
|
|
|
|
|
} // namespace elf2
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif // LLD_ELF_OUTPUT_SECTIONS_H
|