2016-11-02 04:28:21 +08:00
|
|
|
//===- SyntheticSection.h ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-11-02 04:28:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-01-25 05:35:25 +08:00
|
|
|
//
|
|
|
|
// Synthetic sections represent chunks of linker-created data. If you
|
|
|
|
// need to create a chunk of data that to be included in some section
|
2017-02-27 10:32:29 +08:00
|
|
|
// in the result, you probably want to create that as a synthetic section.
|
2017-01-25 05:35:25 +08:00
|
|
|
//
|
|
|
|
// Synthetic sections are designed as input sections as opposed to
|
|
|
|
// output sections because we want to allow them to be manipulated
|
|
|
|
// using linker scripts just like other input sections from regular
|
|
|
|
// files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-11-02 04:28:21 +08:00
|
|
|
|
2019-05-16 08:45:50 +08:00
|
|
|
#ifndef LLD_ELF_SYNTHETIC_SECTIONS_H
|
|
|
|
#define LLD_ELF_SYNTHETIC_SECTIONS_H
|
2016-11-02 04:28:21 +08:00
|
|
|
|
2018-09-15 07:51:05 +08:00
|
|
|
#include "DWARF.h"
|
2017-02-24 06:06:28 +08:00
|
|
|
#include "EhFrame.h"
|
2016-11-02 04:28:21 +08:00
|
|
|
#include "InputSection.h"
|
2020-08-05 07:05:14 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2016-11-29 21:26:04 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2016-12-15 20:07:53 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2018-07-11 07:48:27 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-11-24 10:15:51 +08:00
|
|
|
#include <functional>
|
2016-11-02 04:28:21 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf {
|
2018-03-30 06:32:13 +08:00
|
|
|
class Defined;
|
2019-06-08 01:57:58 +08:00
|
|
|
struct PhdrEntry;
|
|
|
|
class SymbolTableBaseSection;
|
|
|
|
class VersionNeedBaseSection;
|
2016-11-02 04:28:21 +08:00
|
|
|
|
2017-02-27 10:56:02 +08:00
|
|
|
class SyntheticSection : public InputSection {
|
2016-11-10 17:48:29 +08:00
|
|
|
public:
|
2017-03-09 03:35:29 +08:00
|
|
|
SyntheticSection(uint64_t flags, uint32_t type, uint32_t alignment,
|
2016-11-10 17:48:29 +08:00
|
|
|
StringRef name)
|
2017-12-21 10:11:51 +08:00
|
|
|
: InputSection(nullptr, flags, type, alignment, {}, name,
|
2017-02-24 00:49:07 +08:00
|
|
|
InputSectionBase::Synthetic) {
|
2019-05-29 11:55:20 +08:00
|
|
|
markLive();
|
2016-11-11 21:03:58 +08:00
|
|
|
}
|
2016-11-10 17:48:29 +08:00
|
|
|
|
2016-11-22 09:31:32 +08:00
|
|
|
virtual ~SyntheticSection() = default;
|
2016-11-10 20:50:59 +08:00
|
|
|
virtual void writeTo(uint8_t *buf) = 0;
|
2016-11-22 12:17:12 +08:00
|
|
|
virtual size_t getSize() const = 0;
|
2017-02-27 11:07:41 +08:00
|
|
|
virtual void finalizeContents() {}
|
2017-03-08 22:06:24 +08:00
|
|
|
// If the section has the SHF_ALLOC flag and the size may be changed if
|
|
|
|
// thunks are added, update the section size.
|
2017-10-28 01:49:40 +08:00
|
|
|
virtual bool updateAllocSize() { return false; }
|
2019-04-01 16:16:08 +08:00
|
|
|
virtual bool isNeeded() const { return true; }
|
2016-11-10 17:48:29 +08:00
|
|
|
|
2017-06-01 04:17:44 +08:00
|
|
|
static bool classof(const SectionBase *d) {
|
2017-02-23 10:32:18 +08:00
|
|
|
return d->kind() == InputSectionBase::Synthetic;
|
2016-11-10 17:48:29 +08:00
|
|
|
}
|
2016-11-02 04:28:21 +08:00
|
|
|
};
|
|
|
|
|
2017-02-24 06:06:28 +08:00
|
|
|
struct CieRecord {
|
2017-09-20 05:31:57 +08:00
|
|
|
EhSectionPiece *cie = nullptr;
|
|
|
|
std::vector<EhSectionPiece *> fdes;
|
2017-02-24 06:06:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Section for .eh_frame.
|
2017-10-27 11:13:39 +08:00
|
|
|
class EhFrameSection final : public SyntheticSection {
|
2017-02-24 06:06:28 +08:00
|
|
|
public:
|
|
|
|
EhFrameSection();
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override { return !sections.empty(); }
|
2017-02-24 06:06:28 +08:00
|
|
|
size_t getSize() const override { return size; }
|
|
|
|
|
2019-05-24 05:30:30 +08:00
|
|
|
static bool classof(const SectionBase *d) {
|
|
|
|
return SyntheticSection::classof(d) && d->name == ".eh_frame";
|
|
|
|
}
|
|
|
|
|
2019-08-26 18:32:12 +08:00
|
|
|
void addSection(EhInputSection *sec);
|
2017-02-24 06:06:28 +08:00
|
|
|
|
2017-10-27 11:13:24 +08:00
|
|
|
std::vector<EhInputSection *> sections;
|
2017-02-24 06:06:28 +08:00
|
|
|
size_t numFdes = 0;
|
|
|
|
|
2017-10-27 11:13:24 +08:00
|
|
|
struct FdeData {
|
2018-07-21 04:27:42 +08:00
|
|
|
uint32_t pcRel;
|
|
|
|
uint32_t fdeVARel;
|
2017-10-27 11:13:24 +08:00
|
|
|
};
|
|
|
|
|
2017-10-27 11:14:24 +08:00
|
|
|
std::vector<FdeData> getFdeData() const;
|
2018-03-15 05:18:18 +08:00
|
|
|
ArrayRef<CieRecord *> getCieRecords() const { return cieRecords; }
|
2020-08-05 07:05:14 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void iterateFDEWithLSDA(llvm::function_ref<void(InputSection &)> fn);
|
2017-03-11 04:00:42 +08:00
|
|
|
|
2017-02-24 06:06:28 +08:00
|
|
|
private:
|
2018-04-28 04:19:28 +08:00
|
|
|
// This is used only when parsing EhInputSection. We keep it here to avoid
|
|
|
|
// allocating one for each EhInputSection.
|
|
|
|
llvm::DenseMap<size_t, CieRecord *> offsetToCie;
|
|
|
|
|
2017-02-24 06:06:28 +08:00
|
|
|
uint64_t size = 0;
|
2017-10-27 11:13:39 +08:00
|
|
|
|
|
|
|
template <class ELFT, class RelTy>
|
2019-08-26 18:32:12 +08:00
|
|
|
void addRecords(EhInputSection *s, llvm::ArrayRef<RelTy> rels);
|
2020-08-05 07:05:14 +08:00
|
|
|
template <class ELFT> void addSectionAux(EhInputSection *s);
|
|
|
|
template <class ELFT, class RelTy>
|
|
|
|
void iterateFDEWithLSDAAux(EhInputSection &sec, ArrayRef<RelTy> rels,
|
|
|
|
llvm::DenseSet<size_t> &ciesWithLSDA,
|
|
|
|
llvm::function_ref<void(InputSection &)> fn);
|
2017-02-24 06:06:28 +08:00
|
|
|
|
2017-10-27 11:13:39 +08:00
|
|
|
template <class ELFT, class RelTy>
|
2017-09-20 16:03:18 +08:00
|
|
|
CieRecord *addCie(EhSectionPiece &piece, ArrayRef<RelTy> rels);
|
2017-02-24 06:06:28 +08:00
|
|
|
|
2017-10-27 11:13:39 +08:00
|
|
|
template <class ELFT, class RelTy>
|
2020-08-05 07:05:14 +08:00
|
|
|
Defined *isFdeLive(EhSectionPiece &piece, ArrayRef<RelTy> rels);
|
2017-02-24 06:06:28 +08:00
|
|
|
|
2017-10-27 11:13:24 +08:00
|
|
|
uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const;
|
2017-02-24 06:06:28 +08:00
|
|
|
|
2017-09-20 05:31:57 +08:00
|
|
|
std::vector<CieRecord *> cieRecords;
|
2017-02-24 06:06:28 +08:00
|
|
|
|
|
|
|
// CIE records are uniquified by their contents and personality functions.
|
2017-11-04 05:21:47 +08:00
|
|
|
llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap;
|
2017-02-24 06:06:28 +08:00
|
|
|
};
|
|
|
|
|
2017-05-19 00:45:36 +08:00
|
|
|
class GotSection : public SyntheticSection {
|
2016-11-11 19:33:32 +08:00
|
|
|
public:
|
2017-05-19 00:45:36 +08:00
|
|
|
GotSection();
|
2016-11-11 19:33:32 +08:00
|
|
|
size_t getSize() const override { return size; }
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2017-05-19 00:45:36 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
2016-11-25 16:05:41 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
void addEntry(Symbol &sym);
|
|
|
|
bool addDynTlsEntry(Symbol &sym);
|
2016-11-17 05:01:02 +08:00
|
|
|
bool addTlsIndex();
|
2017-11-04 05:21:47 +08:00
|
|
|
uint64_t getGlobalDynAddr(const Symbol &b) const;
|
|
|
|
uint64_t getGlobalDynOffset(const Symbol &b) const;
|
2016-11-17 05:01:02 +08:00
|
|
|
|
2017-04-14 09:34:45 +08:00
|
|
|
uint64_t getTlsIndexVA() { return this->getVA() + tlsIndexOff; }
|
2016-11-17 05:01:02 +08:00
|
|
|
uint32_t getTlsIndexOff() const { return tlsIndexOff; }
|
|
|
|
|
|
|
|
// Flag to force GOT to be in output if we have relocations
|
|
|
|
// that relies on its address.
|
|
|
|
bool hasGotOffRel = false;
|
|
|
|
|
2017-05-12 07:26:03 +08:00
|
|
|
protected:
|
2016-11-29 11:45:36 +08:00
|
|
|
size_t numEntries = 0;
|
2016-11-17 05:01:02 +08:00
|
|
|
uint32_t tlsIndexOff = -1;
|
2017-04-14 09:34:45 +08:00
|
|
|
uint64_t size = 0;
|
2016-11-17 05:01:02 +08:00
|
|
|
};
|
|
|
|
|
2018-08-29 15:27:09 +08:00
|
|
|
// .note.GNU-stack section.
|
|
|
|
class GnuStackSection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
GnuStackSection()
|
|
|
|
: SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
|
|
|
|
void writeTo(uint8_t *buf) override {}
|
|
|
|
size_t getSize() const override { return 0; }
|
|
|
|
};
|
|
|
|
|
2019-06-05 11:04:46 +08:00
|
|
|
class GnuPropertySection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
GnuPropertySection();
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override;
|
|
|
|
};
|
|
|
|
|
2016-11-22 09:31:32 +08:00
|
|
|
// .note.gnu.build-id section.
|
2017-03-21 00:40:21 +08:00
|
|
|
class BuildIdSection : public SyntheticSection {
|
2016-11-22 09:31:32 +08:00
|
|
|
// First 16 bytes are a header.
|
|
|
|
static const unsigned headerSize = 16;
|
|
|
|
|
|
|
|
public:
|
2019-05-09 16:08:09 +08:00
|
|
|
const size_t hashSize;
|
2016-11-22 09:31:32 +08:00
|
|
|
BuildIdSection();
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override { return headerSize + hashSize; }
|
|
|
|
void writeBuildId(llvm::ArrayRef<uint8_t> buf);
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t *hashBuf;
|
|
|
|
};
|
|
|
|
|
2017-03-17 21:31:07 +08:00
|
|
|
// BssSection is used to reserve space for copy relocations and common symbols.
|
2017-03-24 08:15:57 +08:00
|
|
|
// We create three instances of this class for .bss, .bss.rel.ro and "COMMON",
|
|
|
|
// that are used for writable symbols, read-only symbols and common symbols,
|
|
|
|
// respectively.
|
2017-03-17 18:14:53 +08:00
|
|
|
class BssSection final : public SyntheticSection {
|
2017-02-09 18:27:57 +08:00
|
|
|
public:
|
2017-10-04 08:21:17 +08:00
|
|
|
BssSection(StringRef name, uint64_t size, uint32_t alignment);
|
2018-11-26 18:07:10 +08:00
|
|
|
void writeTo(uint8_t *) override {
|
|
|
|
llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section");
|
|
|
|
}
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override { return size != 0; }
|
2017-03-16 16:44:53 +08:00
|
|
|
size_t getSize() const override { return size; }
|
2017-03-17 18:14:53 +08:00
|
|
|
|
2017-11-06 12:33:58 +08:00
|
|
|
static bool classof(const SectionBase *s) { return s->bss; }
|
2017-10-04 08:21:17 +08:00
|
|
|
uint64_t size;
|
2017-02-09 18:27:57 +08:00
|
|
|
};
|
|
|
|
|
2017-03-21 00:44:28 +08:00
|
|
|
class MipsGotSection final : public SyntheticSection {
|
2016-11-17 05:01:02 +08:00
|
|
|
public:
|
|
|
|
MipsGotSection();
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override { return size; }
|
2017-10-28 01:49:40 +08:00
|
|
|
bool updateAllocSize() override;
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2018-06-11 15:24:31 +08:00
|
|
|
|
|
|
|
// Join separate GOTs built for each input file to generate
|
|
|
|
// primary and optional multiple secondary GOTs.
|
2019-03-06 11:07:57 +08:00
|
|
|
void build();
|
2018-06-11 15:24:31 +08:00
|
|
|
|
|
|
|
void addEntry(InputFile &file, Symbol &sym, int64_t addend, RelExpr expr);
|
|
|
|
void addDynTlsEntry(InputFile &file, Symbol &sym);
|
|
|
|
void addTlsIndex(InputFile &file);
|
|
|
|
|
2018-06-11 16:37:19 +08:00
|
|
|
uint64_t getPageEntryOffset(const InputFile *f, const Symbol &s,
|
2018-06-11 15:24:31 +08:00
|
|
|
int64_t addend) const;
|
2018-06-11 16:37:19 +08:00
|
|
|
uint64_t getSymEntryOffset(const InputFile *f, const Symbol &s,
|
2018-06-11 15:24:31 +08:00
|
|
|
int64_t addend) const;
|
2018-06-11 16:37:19 +08:00
|
|
|
uint64_t getGlobalDynOffset(const InputFile *f, const Symbol &s) const;
|
|
|
|
uint64_t getTlsIndexOffset(const InputFile *f) const;
|
2016-11-11 19:33:32 +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.
|
2017-11-04 05:21:47 +08:00
|
|
|
const Symbol *getFirstGlobalEntry() const;
|
2016-11-11 19:33:32 +08:00
|
|
|
|
|
|
|
// Returns the number of entries in the local part of GOT including
|
2016-11-18 05:49:14 +08:00
|
|
|
// the number of reserved entries.
|
|
|
|
unsigned getLocalEntriesNum() const;
|
2016-11-11 19:33:32 +08:00
|
|
|
|
2018-06-11 15:24:31 +08:00
|
|
|
// Return _gp value for primary GOT (nullptr) or particular input file.
|
|
|
|
uint64_t getGp(const InputFile *f = nullptr) const;
|
2016-11-24 06:22:16 +08:00
|
|
|
|
2016-11-11 19:33:32 +08:00
|
|
|
private:
|
2016-11-18 05:49:14 +08:00
|
|
|
// MIPS GOT consists of three parts: local, global and tls. Each part
|
|
|
|
// contains different types of entries. Here is a layout of GOT:
|
|
|
|
// - Header entries |
|
|
|
|
// - Page entries | Local part
|
|
|
|
// - Local entries (16-bit access) |
|
|
|
|
// - Local entries (32-bit access) |
|
|
|
|
// - Normal global entries || Global part
|
|
|
|
// - Reloc-only global entries ||
|
|
|
|
// - TLS entries ||| TLS part
|
|
|
|
//
|
|
|
|
// Header:
|
|
|
|
// Two entries hold predefined value 0x0 and 0x80000000.
|
|
|
|
// Page entries:
|
|
|
|
// These entries created by R_MIPS_GOT_PAGE relocation and R_MIPS_GOT16
|
|
|
|
// relocation against local symbols. They are initialized by higher 16-bit
|
|
|
|
// of the corresponding symbol's value. So each 64kb of address space
|
|
|
|
// requires a single GOT entry.
|
|
|
|
// Local entries (16-bit access):
|
|
|
|
// These entries created by GOT relocations against global non-preemptible
|
|
|
|
// symbols so dynamic linker is not necessary to resolve the symbol's
|
|
|
|
// values. "16-bit access" means that corresponding relocations address
|
|
|
|
// GOT using 16-bit index. Each unique Symbol-Addend pair has its own
|
|
|
|
// GOT entry.
|
|
|
|
// Local entries (32-bit access):
|
|
|
|
// These entries are the same as above but created by relocations which
|
|
|
|
// address GOT using 32-bit index (R_MIPS_GOT_HI16/LO16 etc).
|
|
|
|
// Normal global entries:
|
|
|
|
// These entries created by GOT relocations against preemptible global
|
|
|
|
// symbols. They need to be initialized by dynamic linker and they ordered
|
|
|
|
// exactly as the corresponding entries in the dynamic symbols table.
|
|
|
|
// Reloc-only global entries:
|
|
|
|
// These entries created for symbols that are referenced by dynamic
|
|
|
|
// relocations R_MIPS_REL32. These entries are not accessed with gp-relative
|
|
|
|
// addressing, but MIPS ABI requires that these entries be present in GOT.
|
|
|
|
// TLS entries:
|
|
|
|
// Entries created by TLS relocations.
|
2018-06-11 15:24:31 +08:00
|
|
|
//
|
|
|
|
// If the sum of local, global and tls entries is less than 64K only single
|
|
|
|
// got is enough. Otherwise, multi-got is created. Series of primary and
|
|
|
|
// multiple secondary GOTs have the following layout:
|
|
|
|
// - Primary GOT
|
|
|
|
// Header
|
|
|
|
// Local entries
|
|
|
|
// Global entries
|
|
|
|
// Relocation only entries
|
|
|
|
// TLS entries
|
|
|
|
//
|
|
|
|
// - Secondary GOT
|
|
|
|
// Local entries
|
|
|
|
// Global entries
|
|
|
|
// TLS entries
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// All GOT entries required by relocations from a single input file entirely
|
|
|
|
// belong to either primary or one of secondary GOTs. To reference GOT entries
|
|
|
|
// each GOT has its own _gp value points to the "middle" of the GOT.
|
|
|
|
// In the code this value loaded to the register which is used for GOT access.
|
|
|
|
//
|
|
|
|
// MIPS 32 function's prologue:
|
|
|
|
// lui v0,0x0
|
|
|
|
// 0: R_MIPS_HI16 _gp_disp
|
|
|
|
// addiu v0,v0,0
|
|
|
|
// 4: R_MIPS_LO16 _gp_disp
|
|
|
|
//
|
|
|
|
// MIPS 64:
|
|
|
|
// lui at,0x0
|
|
|
|
// 14: R_MIPS_GPREL16 main
|
|
|
|
//
|
|
|
|
// Dynamic linker does not know anything about secondary GOTs and cannot
|
|
|
|
// use a regular MIPS mechanism for GOT entries initialization. So we have
|
|
|
|
// to use an approach accepted by other architectures and create dynamic
|
|
|
|
// relocations R_MIPS_REL32 to initialize global entries (and local in case
|
|
|
|
// of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
|
|
|
|
// requires GOT entries and correspondingly ordered dynamic symbol table
|
|
|
|
// entries to deal with dynamic relocations. To handle this problem
|
|
|
|
// relocation-only section in the primary GOT contains entries for all
|
|
|
|
// symbols referenced in global parts of secondary GOTs. Although the sum
|
|
|
|
// of local and normal global entries of the primary got should be less
|
|
|
|
// than 64K, the size of the primary got (including relocation-only entries
|
|
|
|
// can be greater than 64K, because parts of the primary got that overflow
|
|
|
|
// the 64K limit are used only by the dynamic linker at dynamic link-time
|
|
|
|
// and not by 16-bit gp-relative addressing at run-time.
|
|
|
|
//
|
|
|
|
// For complete multi-GOT description see the following link
|
|
|
|
// https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
|
2016-11-18 05:49:14 +08:00
|
|
|
|
2016-11-29 18:23:50 +08:00
|
|
|
// Number of "Header" entries.
|
|
|
|
static const unsigned headerEntriesNum = 2;
|
2016-11-18 05:49:14 +08:00
|
|
|
|
2017-03-21 00:44:28 +08:00
|
|
|
uint64_t size = 0;
|
2018-06-11 15:24:31 +08:00
|
|
|
|
|
|
|
// Symbol and addend.
|
2019-04-01 08:11:24 +08:00
|
|
|
using GotEntry = std::pair<Symbol *, int64_t>;
|
2018-06-11 15:24:31 +08:00
|
|
|
|
|
|
|
struct FileGot {
|
|
|
|
InputFile *file = nullptr;
|
|
|
|
size_t startIndex = 0;
|
|
|
|
|
|
|
|
struct PageBlock {
|
2019-01-21 15:44:52 +08:00
|
|
|
size_t firstIndex;
|
|
|
|
size_t count;
|
|
|
|
PageBlock() : firstIndex(0), count(0) {}
|
2018-06-11 15:24:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Map output sections referenced by MIPS GOT relocations
|
|
|
|
// to the description (index/count) "page" entries allocated
|
|
|
|
// for this section.
|
|
|
|
llvm::SmallMapVector<const OutputSection *, PageBlock, 16> pagesMap;
|
|
|
|
// Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
|
|
|
|
llvm::MapVector<GotEntry, size_t> local16;
|
|
|
|
llvm::MapVector<GotEntry, size_t> local32;
|
|
|
|
llvm::MapVector<Symbol *, size_t> global;
|
|
|
|
llvm::MapVector<Symbol *, size_t> relocs;
|
|
|
|
llvm::MapVector<Symbol *, size_t> tls;
|
|
|
|
// Set of symbols referenced by dynamic TLS relocations.
|
|
|
|
llvm::MapVector<Symbol *, size_t> dynTlsSymbols;
|
|
|
|
|
|
|
|
// Total number of all entries.
|
|
|
|
size_t getEntriesNum() const;
|
|
|
|
// Number of "page" entries.
|
|
|
|
size_t getPageEntriesNum() const;
|
|
|
|
// Number of entries require 16-bit index to access.
|
|
|
|
size_t getIndexedEntriesNum() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Container of GOT created for each input file.
|
|
|
|
// After building a final series of GOTs this container
|
|
|
|
// holds primary and secondary GOT's.
|
|
|
|
std::vector<FileGot> gots;
|
|
|
|
|
|
|
|
// Return (and create if necessary) `FileGot`.
|
|
|
|
FileGot &getGot(InputFile &f);
|
|
|
|
|
|
|
|
// Try to merge two GOTs. In case of success the `Dst` contains
|
|
|
|
// result of merging and the function returns true. In case of
|
2020-04-02 00:21:08 +08:00
|
|
|
// overflow the `Dst` is unchanged and the function returns false.
|
2018-06-11 15:24:31 +08:00
|
|
|
bool tryMergeGots(FileGot & dst, FileGot & src, bool isPrimary);
|
2016-11-11 19:33:32 +08:00
|
|
|
};
|
|
|
|
|
2017-03-15 17:12:56 +08:00
|
|
|
class GotPltSection final : public SyntheticSection {
|
2016-11-10 17:48:29 +08:00
|
|
|
public:
|
|
|
|
GotPltSection();
|
2017-11-04 05:21:47 +08:00
|
|
|
void addEntry(Symbol &sym);
|
2016-11-10 17:48:29 +08:00
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2016-11-10 17:48:29 +08:00
|
|
|
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-26 07:46:19 +08:00
|
|
|
// Flag to force GotPlt to be in output if we have relocations
|
|
|
|
// that relies on its address.
|
|
|
|
bool hasGotPltOffRel = false;
|
|
|
|
|
2016-11-10 17:48:29 +08:00
|
|
|
private:
|
2017-11-04 05:21:47 +08:00
|
|
|
std::vector<const Symbol *> entries;
|
2016-11-10 17:48:29 +08:00
|
|
|
};
|
|
|
|
|
2017-02-09 18:56:15 +08:00
|
|
|
// The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
|
2016-12-08 20:58:55 +08:00
|
|
|
// Symbols that will be relocated by Target->IRelativeRel.
|
|
|
|
// On most Targets the IgotPltSection will immediately follow the GotPltSection
|
|
|
|
// on ARM the IgotPltSection will immediately follow the GotSection.
|
2017-03-15 17:12:56 +08:00
|
|
|
class IgotPltSection final : public SyntheticSection {
|
2016-12-08 20:58:55 +08:00
|
|
|
public:
|
|
|
|
IgotPltSection();
|
2017-11-04 05:21:47 +08:00
|
|
|
void addEntry(Symbol &sym);
|
2016-12-08 20:58:55 +08:00
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override { return !entries.empty(); }
|
2016-12-08 20:58:55 +08:00
|
|
|
|
|
|
|
private:
|
2017-11-04 05:21:47 +08:00
|
|
|
std::vector<const Symbol *> entries;
|
2016-12-08 20:58:55 +08:00
|
|
|
};
|
|
|
|
|
2017-03-15 17:32:36 +08:00
|
|
|
class StringTableSection final : public SyntheticSection {
|
2016-11-14 17:16:00 +08:00
|
|
|
public:
|
|
|
|
StringTableSection(StringRef name, bool dynamic);
|
|
|
|
unsigned addString(StringRef s, bool hashIt = true);
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override { return size; }
|
|
|
|
bool isDynamic() const { return dynamic; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const bool dynamic;
|
|
|
|
|
2017-03-15 17:32:36 +08:00
|
|
|
uint64_t size = 0;
|
2016-11-14 17:16:00 +08:00
|
|
|
|
|
|
|
llvm::DenseMap<StringRef, unsigned> stringMap;
|
|
|
|
std::vector<StringRef> strings;
|
|
|
|
};
|
|
|
|
|
2017-03-17 20:07:44 +08:00
|
|
|
class DynamicReloc {
|
2016-11-16 18:02:27 +08:00
|
|
|
public:
|
2018-02-11 02:14:34 +08:00
|
|
|
DynamicReloc(RelType type, const InputSectionBase *inputSec,
|
2017-11-04 05:21:47 +08:00
|
|
|
uint64_t offsetInSec, bool useSymVA, Symbol *sym, int64_t addend)
|
2016-11-16 18:02:27 +08:00
|
|
|
: type(type), sym(sym), inputSec(inputSec), offsetInSec(offsetInSec),
|
2018-06-11 15:24:31 +08:00
|
|
|
useSymVA(useSymVA), addend(addend), outputSec(nullptr) {}
|
|
|
|
// This constructor records dynamic relocation settings used by MIPS
|
|
|
|
// multi-GOT implementation. It's to relocate addresses of 64kb pages
|
|
|
|
// lie inside the output section.
|
|
|
|
DynamicReloc(RelType type, const InputSectionBase *inputSec,
|
|
|
|
uint64_t offsetInSec, const OutputSection *outputSec,
|
|
|
|
int64_t addend)
|
|
|
|
: type(type), sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec),
|
|
|
|
useSymVA(false), addend(addend), outputSec(outputSec) {}
|
2016-11-16 18:02:27 +08:00
|
|
|
|
2017-03-16 19:42:16 +08:00
|
|
|
uint64_t getOffset() const;
|
2019-06-08 01:57:58 +08:00
|
|
|
uint32_t getSymIndex(SymbolTableBaseSection *symTab) const;
|
2016-11-16 18:02:27 +08:00
|
|
|
|
2018-02-19 19:00:15 +08:00
|
|
|
// Computes the addend of the dynamic relocation. Note that this is not the
|
2019-07-16 13:50:45 +08:00
|
|
|
// same as the addend member variable as it also includes the symbol address
|
|
|
|
// if useSymVA is true.
|
2018-02-19 19:00:15 +08:00
|
|
|
int64_t computeAddend() const;
|
|
|
|
|
2018-02-11 02:14:34 +08:00
|
|
|
RelType type;
|
2016-11-16 18:02:27 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *sym;
|
2017-02-23 10:28:28 +08:00
|
|
|
const InputSectionBase *inputSec = nullptr;
|
2017-03-16 19:42:16 +08:00
|
|
|
uint64_t offsetInSec;
|
2018-02-19 19:00:15 +08:00
|
|
|
// If this member is true, the dynamic relocation will not be against the
|
|
|
|
// symbol but will instead be a relative relocation that simply adds the
|
|
|
|
// load address. This means we need to write the symbol virtual address
|
|
|
|
// plus the original addend as the final relocation addend.
|
2016-11-16 18:02:27 +08:00
|
|
|
bool useSymVA;
|
2017-02-16 08:12:34 +08:00
|
|
|
int64_t addend;
|
2018-06-11 15:24:31 +08:00
|
|
|
const OutputSection *outputSec;
|
2016-11-16 18:02:27 +08:00
|
|
|
};
|
|
|
|
|
2017-02-27 10:56:02 +08:00
|
|
|
template <class ELFT> class DynamicSection final : public SyntheticSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Dyn = typename ELFT::Dyn;
|
|
|
|
using Elf_Rel = typename ELFT::Rel;
|
|
|
|
using Elf_Rela = typename ELFT::Rela;
|
|
|
|
using Elf_Relr = typename ELFT::Relr;
|
|
|
|
using Elf_Shdr = typename ELFT::Shdr;
|
|
|
|
using Elf_Sym = typename ELFT::Sym;
|
2016-11-15 20:26:55 +08:00
|
|
|
|
2017-02-27 11:07:41 +08:00
|
|
|
// finalizeContents() fills this vector with the section contents.
|
2017-11-24 10:15:51 +08:00
|
|
|
std::vector<std::pair<int32_t, std::function<uint64_t()>>> entries;
|
2016-11-15 20:26:55 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
DynamicSection();
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2016-11-15 20:26:55 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override { return size; }
|
|
|
|
|
|
|
|
private:
|
2017-11-24 10:15:51 +08:00
|
|
|
void add(int32_t tag, std::function<uint64_t()> fn);
|
|
|
|
void addInt(int32_t tag, uint64_t val);
|
|
|
|
void addInSec(int32_t tag, InputSection *sec);
|
2018-04-13 16:15:01 +08:00
|
|
|
void addInSecRelative(int32_t tag, InputSection *sec);
|
2017-11-24 10:15:51 +08:00
|
|
|
void addOutSec(int32_t tag, OutputSection *sec);
|
|
|
|
void addSize(int32_t tag, OutputSection *sec);
|
|
|
|
void addSym(int32_t tag, Symbol *sym);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2017-04-14 09:34:45 +08:00
|
|
|
uint64_t size = 0;
|
2016-11-15 20:26:55 +08:00
|
|
|
};
|
|
|
|
|
2017-10-28 01:49:40 +08:00
|
|
|
class RelocationBaseSection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
RelocationBaseSection(StringRef name, uint32_t type, int32_t dynamicTag,
|
|
|
|
int32_t sizeDynamicTag);
|
2018-02-14 00:06:11 +08:00
|
|
|
void addReloc(RelType dynType, InputSectionBase *isec, uint64_t offsetInSec,
|
2018-02-14 00:03:52 +08:00
|
|
|
Symbol *sym);
|
2018-02-16 18:01:17 +08:00
|
|
|
// Add a dynamic relocation that might need an addend. This takes care of
|
|
|
|
// writing the addend to the output section if needed.
|
2018-02-14 00:06:11 +08:00
|
|
|
void addReloc(RelType dynType, InputSectionBase *inputSec,
|
2018-02-17 00:53:04 +08:00
|
|
|
uint64_t offsetInSec, Symbol *sym, int64_t addend, RelExpr expr,
|
|
|
|
RelType type);
|
2017-10-28 01:49:40 +08:00
|
|
|
void addReloc(const DynamicReloc &reloc);
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override { return !relocs.empty(); }
|
2017-10-28 01:49:40 +08:00
|
|
|
size_t getSize() const override { return relocs.size() * this->entsize; }
|
|
|
|
size_t getRelativeRelocCount() const { return numRelativeRelocs; }
|
|
|
|
void finalizeContents() override;
|
|
|
|
int32_t dynamicTag, sizeDynamicTag;
|
2019-06-08 01:57:58 +08:00
|
|
|
std::vector<DynamicReloc> relocs;
|
2017-10-28 01:49:40 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
size_t numRelativeRelocs = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class RelocationSection final : public RelocationBaseSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Rel = typename ELFT::Rel;
|
|
|
|
using Elf_Rela = typename ELFT::Rela;
|
2016-11-16 18:02:27 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
RelocationSection(StringRef name, bool sort);
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool sort;
|
2017-10-28 01:49:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class AndroidPackedRelocationSection final : public RelocationBaseSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Rel = typename ELFT::Rel;
|
|
|
|
using Elf_Rela = typename ELFT::Rela;
|
2017-10-28 01:49:40 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
AndroidPackedRelocationSection(StringRef name);
|
|
|
|
|
|
|
|
bool updateAllocSize() override;
|
|
|
|
size_t getSize() const override { return relocData.size(); }
|
|
|
|
void writeTo(uint8_t *buf) override {
|
|
|
|
memcpy(buf, relocData.data(), relocData.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmallVector<char, 0> relocData;
|
2016-11-16 18:02:27 +08:00
|
|
|
};
|
|
|
|
|
2018-07-10 04:08:55 +08:00
|
|
|
struct RelativeReloc {
|
|
|
|
uint64_t getOffset() const { return inputSec->getVA(offsetInSec); }
|
|
|
|
|
|
|
|
const InputSectionBase *inputSec;
|
|
|
|
uint64_t offsetInSec;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RelrBaseSection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
RelrBaseSection();
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override { return !relocs.empty(); }
|
2018-07-10 04:08:55 +08:00
|
|
|
std::vector<RelativeReloc> relocs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// RelrSection is used to encode offsets for relative relocations.
|
|
|
|
// Proposal for adding SHT_RELR sections to generic-abi is here:
|
|
|
|
// https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
|
|
|
|
// For more details, see the comment in RelrSection::updateAllocSize().
|
|
|
|
template <class ELFT> class RelrSection final : public RelrBaseSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Relr = typename ELFT::Relr;
|
2018-07-10 04:08:55 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
RelrSection();
|
|
|
|
|
|
|
|
bool updateAllocSize() override;
|
|
|
|
size_t getSize() const override { return relrRelocs.size() * this->entsize; }
|
|
|
|
void writeTo(uint8_t *buf) override {
|
|
|
|
memcpy(buf, relrRelocs.data(), getSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Elf_Relr> relrRelocs;
|
|
|
|
};
|
|
|
|
|
2016-11-18 14:44:18 +08:00
|
|
|
struct SymbolTableEntry {
|
2017-11-04 06:33:49 +08:00
|
|
|
Symbol *sym;
|
2016-11-18 14:44:18 +08:00
|
|
|
size_t strTabOffset;
|
|
|
|
};
|
|
|
|
|
2017-05-16 16:53:30 +08:00
|
|
|
class SymbolTableBaseSection : public SyntheticSection {
|
2016-11-17 17:16:34 +08:00
|
|
|
public:
|
2017-05-16 16:53:30 +08:00
|
|
|
SymbolTableBaseSection(StringTableSection &strTabSec);
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2017-05-16 16:53:30 +08:00
|
|
|
size_t getSize() const override { return getNumSymbols() * entsize; }
|
2017-11-04 08:31:04 +08:00
|
|
|
void addSymbol(Symbol *sym);
|
2017-01-23 22:07:23 +08:00
|
|
|
unsigned getNumSymbols() const { return symbols.size() + 1; }
|
2017-11-04 08:31:04 +08:00
|
|
|
size_t getSymbolIndex(Symbol *sym);
|
2016-11-17 17:16:34 +08:00
|
|
|
ArrayRef<SymbolTableEntry> getSymbols() const { return symbols; }
|
|
|
|
|
2017-05-16 16:53:30 +08:00
|
|
|
protected:
|
2018-08-10 15:24:18 +08:00
|
|
|
void sortSymTabSymbols();
|
|
|
|
|
2016-11-17 17:16:34 +08:00
|
|
|
// A vector of symbols and their string table offsets.
|
|
|
|
std::vector<SymbolTableEntry> symbols;
|
2017-01-23 22:07:23 +08:00
|
|
|
|
2017-03-15 17:32:36 +08:00
|
|
|
StringTableSection &strTabSec;
|
2017-09-27 17:08:53 +08:00
|
|
|
|
|
|
|
llvm::once_flag onceFlag;
|
2017-11-04 05:21:47 +08:00
|
|
|
llvm::DenseMap<Symbol *, size_t> symbolIndexMap;
|
2017-09-27 17:08:53 +08:00
|
|
|
llvm::DenseMap<OutputSection *, size_t> sectionIndexMap;
|
2016-11-17 17:16:34 +08:00
|
|
|
};
|
|
|
|
|
2017-05-16 16:53:30 +08:00
|
|
|
template <class ELFT>
|
|
|
|
class SymbolTableSection final : public SymbolTableBaseSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Sym = typename ELFT::Sym;
|
2017-05-16 16:53:30 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
SymbolTableSection(StringTableSection &strTabSec);
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
};
|
|
|
|
|
2018-07-30 20:39:54 +08:00
|
|
|
class SymtabShndxSection final : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
SymtabShndxSection();
|
|
|
|
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2018-07-30 20:39:54 +08:00
|
|
|
void finalizeContents() override;
|
|
|
|
};
|
|
|
|
|
2016-11-18 14:44:18 +08:00
|
|
|
// Outputs GNU Hash section. For detailed explanation see:
|
|
|
|
// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
|
2017-02-27 10:56:02 +08:00
|
|
|
class GnuHashTableSection final : public SyntheticSection {
|
2016-11-18 14:44:18 +08:00
|
|
|
public:
|
|
|
|
GnuHashTableSection();
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2016-11-18 14:44:18 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-02-27 11:10:06 +08:00
|
|
|
size_t getSize() const override { return size; }
|
2016-11-18 14:44:18 +08:00
|
|
|
|
|
|
|
// Adds symbols to the hash table.
|
|
|
|
// Sorts the input to satisfy GNU hash section requirements.
|
|
|
|
void addSymbols(std::vector<SymbolTableEntry> &symbols);
|
|
|
|
|
|
|
|
private:
|
2018-12-22 05:59:34 +08:00
|
|
|
// See the comment in writeBloomFilter.
|
|
|
|
enum { Shift2 = 26 };
|
2016-11-18 14:44:18 +08:00
|
|
|
|
2017-03-01 10:51:42 +08:00
|
|
|
void writeBloomFilter(uint8_t *buf);
|
2016-11-18 14:44:18 +08:00
|
|
|
void writeHashTable(uint8_t *buf);
|
|
|
|
|
2017-03-01 10:51:42 +08:00
|
|
|
struct Entry {
|
2017-11-04 08:31:04 +08:00
|
|
|
Symbol *sym;
|
2017-03-01 10:51:42 +08:00
|
|
|
size_t strTabOffset;
|
2016-11-18 14:44:18 +08:00
|
|
|
uint32_t hash;
|
2017-12-02 08:37:13 +08:00
|
|
|
uint32_t bucketIdx;
|
2016-11-18 14:44:18 +08:00
|
|
|
};
|
|
|
|
|
2017-03-01 10:51:42 +08:00
|
|
|
std::vector<Entry> symbols;
|
|
|
|
size_t maskWords;
|
2017-03-01 18:12:49 +08:00
|
|
|
size_t nBuckets = 0;
|
2017-03-01 10:51:42 +08:00
|
|
|
size_t size = 0;
|
2016-11-18 14:44:18 +08:00
|
|
|
};
|
|
|
|
|
2017-09-27 17:14:59 +08:00
|
|
|
class HashTableSection final : public SyntheticSection {
|
2016-11-18 17:06:47 +08:00
|
|
|
public:
|
|
|
|
HashTableSection();
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2016-11-18 17:06:47 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-02-27 11:10:06 +08:00
|
|
|
size_t getSize() const override { return size; }
|
2016-11-18 17:06:47 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
size_t size = 0;
|
|
|
|
};
|
|
|
|
|
2019-12-15 06:17:35 +08:00
|
|
|
// Used for PLT entries. It usually has a PLT header for lazy binding. Each PLT
|
|
|
|
// entry is associated with a JUMP_SLOT relocation, which may be resolved lazily
|
|
|
|
// at runtime.
|
2019-12-11 10:05:36 +08:00
|
|
|
//
|
|
|
|
// On PowerPC, this section contains lazy symbol resolvers. A branch instruction
|
|
|
|
// jumps to a PLT call stub, which will then jump to the target (BIND_NOW) or a
|
|
|
|
// lazy symbol resolver.
|
|
|
|
//
|
|
|
|
// On x86 when IBT is enabled, this section (.plt.sec) contains PLT call stubs.
|
|
|
|
// A call instruction jumps to a .plt.sec entry, which will then jump to the
|
|
|
|
// target (BIND_NOW) or a .plt entry.
|
2017-03-17 19:01:57 +08:00
|
|
|
class PltSection : public SyntheticSection {
|
2016-11-18 22:35:03 +08:00
|
|
|
public:
|
2019-12-15 06:17:35 +08:00
|
|
|
PltSection();
|
2016-12-08 20:58:55 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override;
|
2019-12-15 06:17:35 +08:00
|
|
|
bool isNeeded() const override;
|
2017-01-25 18:31:16 +08:00
|
|
|
void addSymbols();
|
2019-12-17 02:58:48 +08:00
|
|
|
void addEntry(Symbol &sym);
|
2019-12-11 10:05:36 +08:00
|
|
|
size_t getNumEntries() const { return entries.size(); }
|
2017-03-17 19:01:57 +08:00
|
|
|
|
[ELF][PPC32] Support canonical PLT
-fno-pie produces a pair of non-GOT-non-PLT relocations R_PPC_ADDR16_{HA,LO} (R_ABS) referencing external
functions.
```
lis 3, func@ha
la 3, func@l(3)
```
In a -no-pie/-pie link, if func is not defined in the executable, a canonical PLT entry (st_value>0, st_shndx=0) will be needed.
References to func in shared objects will be resolved to this address.
-fno-pie -pie should fail with "can't create dynamic relocation ... against ...", so we just need to think about -no-pie.
On x86, the PLT entry passes the JMP_SLOT offset to the rtld PLT resolver.
On x86-64: the PLT entry passes the JUMP_SLOT index to the rtld PLT resolver.
On ARM/AArch64: the PLT entry passes &.got.plt[n]. The PLT header passes &.got.plt[fixed-index]. The rtld PLT resolver can compute the JUMP_SLOT index from the two addresses.
For these targets, the canonical PLT entry can just reuse the regular PLT entry (in PltSection).
On PPC32: PltSection (.glink) consists of `b PLTresolve` instructions and `PLTresolve`. The rtld PLT resolver depends on r11 having been set up to the .plt (GotPltSection) entry.
On PPC64 ELFv2: PltSection (.glink) consists of `__glink_PLTresolve` and `bl __glink_PLTresolve`. The rtld PLT resolver depends on r12 having been set up to the .plt (GotPltSection) entry.
We cannot reuse a `b PLTresolve`/`bl __glink_PLTresolve` in PltSection as a canonical PLT entry. PPC64 ELFv2 avoids the problem by using TOC for any external reference, even in non-pic code, so the canonical PLT entry scenario should not happen in the first place.
For PPC32, we have to create a PLT call stub as the canonical PLT entry. The code sequence sets up r11.
Reviewed By: Bdragon28
Differential Revision: https://reviews.llvm.org/D73399
2020-01-25 09:49:59 +08:00
|
|
|
size_t headerSize;
|
2018-11-29 01:42:59 +08:00
|
|
|
|
2019-03-23 05:17:25 +08:00
|
|
|
std::vector<const Symbol *> entries;
|
2019-12-15 06:17:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Used for non-preemptible ifuncs. It does not have a header. Each entry is
|
|
|
|
// associated with an IRELATIVE relocation, which will be resolved eagerly at
|
2019-12-18 09:17:33 +08:00
|
|
|
// runtime. PltSection can only contain entries associated with JUMP_SLOT
|
2019-12-15 06:17:35 +08:00
|
|
|
// relocations, so IPLT entries are in a separate section.
|
|
|
|
class IpltSection final : public SyntheticSection {
|
|
|
|
std::vector<const Symbol *> entries;
|
|
|
|
|
|
|
|
public:
|
|
|
|
IpltSection();
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override;
|
|
|
|
bool isNeeded() const override { return !entries.empty(); }
|
|
|
|
void addSymbols();
|
|
|
|
void addEntry(Symbol &sym);
|
2016-12-08 20:58:55 +08:00
|
|
|
};
|
|
|
|
|
2020-02-29 09:22:29 +08:00
|
|
|
class PPC32GlinkSection : public PltSection {
|
|
|
|
public:
|
|
|
|
PPC32GlinkSection();
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override;
|
|
|
|
|
|
|
|
std::vector<const Symbol *> canonical_plts;
|
|
|
|
static constexpr size_t footerSize = 64;
|
|
|
|
};
|
|
|
|
|
2019-12-11 10:05:36 +08:00
|
|
|
// This is x86-only.
|
|
|
|
class IBTPltSection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
IBTPltSection();
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
size_t getSize() const override;
|
|
|
|
};
|
|
|
|
|
2018-07-11 07:48:27 +08:00
|
|
|
class GdbIndexSection final : public SyntheticSection {
|
|
|
|
public:
|
2017-09-25 05:45:35 +08:00
|
|
|
struct AddressEntry {
|
|
|
|
InputSection *section;
|
|
|
|
uint64_t lowAddress;
|
|
|
|
uint64_t highAddress;
|
|
|
|
uint32_t cuIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CuEntry {
|
|
|
|
uint64_t cuOffset;
|
|
|
|
uint64_t cuLength;
|
|
|
|
};
|
|
|
|
|
2018-11-14 04:25:51 +08:00
|
|
|
struct NameAttrEntry {
|
2017-09-25 09:42:57 +08:00
|
|
|
llvm::CachedHashStringRef name;
|
2018-11-14 04:25:51 +08:00
|
|
|
uint32_t cuIndexAndAttrs;
|
2017-09-25 05:45:35 +08:00
|
|
|
};
|
|
|
|
|
2018-07-11 07:48:27 +08:00
|
|
|
struct GdbChunk {
|
|
|
|
InputSection *sec;
|
|
|
|
std::vector<AddressEntry> addressAreas;
|
|
|
|
std::vector<CuEntry> compilationUnits;
|
|
|
|
};
|
2017-09-25 05:45:35 +08:00
|
|
|
|
2018-07-11 07:48:27 +08:00
|
|
|
struct GdbSymbol {
|
|
|
|
llvm::CachedHashStringRef name;
|
|
|
|
std::vector<uint32_t> cuVector;
|
|
|
|
uint32_t nameOff;
|
|
|
|
uint32_t cuVectorOff;
|
|
|
|
};
|
|
|
|
|
|
|
|
GdbIndexSection();
|
|
|
|
template <typename ELFT> static GdbIndexSection *create();
|
2016-11-21 17:24:43 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
2018-07-11 07:48:27 +08:00
|
|
|
size_t getSize() const override { return size; }
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2016-11-21 17:24:43 +08:00
|
|
|
|
2017-08-16 01:01:39 +08:00
|
|
|
private:
|
2018-07-11 07:48:27 +08:00
|
|
|
struct GdbIndexHeader {
|
|
|
|
llvm::support::ulittle32_t version;
|
|
|
|
llvm::support::ulittle32_t cuListOff;
|
|
|
|
llvm::support::ulittle32_t cuTypesOff;
|
|
|
|
llvm::support::ulittle32_t addressAreaOff;
|
|
|
|
llvm::support::ulittle32_t symtabOff;
|
|
|
|
llvm::support::ulittle32_t constantPoolOff;
|
2018-07-10 23:57:25 +08:00
|
|
|
};
|
|
|
|
|
2018-07-11 07:48:27 +08:00
|
|
|
void initOutputSize();
|
|
|
|
size_t computeSymtabSize() const;
|
2016-12-15 20:07:53 +08:00
|
|
|
|
2018-07-11 07:48:27 +08:00
|
|
|
// Each chunk contains information gathered from debug sections of a
|
|
|
|
// single object file.
|
|
|
|
std::vector<GdbChunk> chunks;
|
2016-12-15 17:08:13 +08:00
|
|
|
|
2018-07-11 07:48:27 +08:00
|
|
|
// A symbol table for this .gdb_index section.
|
|
|
|
std::vector<GdbSymbol> symbols;
|
2016-12-15 20:07:53 +08:00
|
|
|
|
2018-07-11 07:48:27 +08:00
|
|
|
size_t size;
|
2016-11-21 17:24:43 +08:00
|
|
|
};
|
|
|
|
|
2016-11-21 23:52:10 +08:00
|
|
|
// --eh-frame-hdr option tells linker to construct a header for all the
|
|
|
|
// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
|
|
|
|
// and also to a PT_GNU_EH_FRAME segment.
|
|
|
|
// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
|
|
|
|
// calling dl_iterate_phdr.
|
|
|
|
// This section contains a lookup table for quick binary search of FDEs.
|
|
|
|
// Detailed info about internals can be found in Ian Lance Taylor's blog:
|
|
|
|
// http://www.airs.com/blog/archives/460 (".eh_frame")
|
|
|
|
// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
|
2017-10-27 11:14:24 +08:00
|
|
|
class EhFrameHeader final : public SyntheticSection {
|
2016-11-21 23:52:10 +08:00
|
|
|
public:
|
|
|
|
EhFrameHeader();
|
2019-03-01 07:11:35 +08:00
|
|
|
void write();
|
2016-11-21 23:52:10 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2016-11-21 23:52:10 +08:00
|
|
|
};
|
|
|
|
|
2016-11-22 00:59:33 +08:00
|
|
|
// For more information about .gnu.version and .gnu.version_r see:
|
|
|
|
// https://www.akkadia.org/drepper/symbol-versioning
|
|
|
|
|
|
|
|
// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
|
|
|
|
// contain symbol version definitions. The number of entries in this section
|
|
|
|
// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
|
|
|
|
// The section shall contain an array of Elf_Verdef structures, optionally
|
|
|
|
// followed by an array of Elf_Verdaux structures.
|
2017-02-27 10:56:02 +08:00
|
|
|
class VersionDefinitionSection final : public SyntheticSection {
|
2016-11-22 00:59:33 +08:00
|
|
|
public:
|
|
|
|
VersionDefinitionSection();
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2016-11-22 00:59:33 +08:00
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
|
|
|
|
private:
|
2018-09-26 04:37:51 +08:00
|
|
|
enum { EntrySize = 28 };
|
2016-11-22 00:59:33 +08:00
|
|
|
void writeOne(uint8_t *buf, uint32_t index, StringRef name, size_t nameOff);
|
2019-06-08 01:57:58 +08:00
|
|
|
StringRef getFileDefName();
|
2016-11-22 00:59:33 +08:00
|
|
|
|
|
|
|
unsigned fileDefNameOff;
|
2019-06-08 01:57:58 +08:00
|
|
|
std::vector<unsigned> verDefNameOffs;
|
2016-11-22 00:59:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The .gnu.version section specifies the required version of each symbol in the
|
|
|
|
// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
|
|
|
|
// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
|
|
|
|
// identifier defined in the either .gnu.version_r or .gnu.version_d section.
|
|
|
|
// The values 0 and 1 are reserved. All other values are used for versions in
|
|
|
|
// the own object or in any of the dependencies.
|
2017-02-27 10:56:02 +08:00
|
|
|
class VersionTableSection final : public SyntheticSection {
|
2016-11-22 00:59:33 +08:00
|
|
|
public:
|
|
|
|
VersionTableSection();
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2016-11-22 00:59:33 +08:00
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2016-11-22 00:59:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The .gnu.version_r section defines the version identifiers used by
|
|
|
|
// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
|
|
|
|
// Elf_Verneed specifies the version requirements for a single DSO, and contains
|
|
|
|
// a reference to a linked list of Elf_Vernaux data structures which define the
|
|
|
|
// mapping from version identifiers to version names.
|
2019-03-06 11:07:48 +08:00
|
|
|
template <class ELFT>
|
2019-04-09 01:48:05 +08:00
|
|
|
class VersionNeedSection final : public SyntheticSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Verneed = typename ELFT::Verneed;
|
|
|
|
using Elf_Vernaux = typename ELFT::Vernaux;
|
2016-11-22 00:59:33 +08:00
|
|
|
|
2019-04-09 01:48:05 +08:00
|
|
|
struct Vernaux {
|
|
|
|
uint64_t hash;
|
|
|
|
uint32_t verneedIndex;
|
|
|
|
uint64_t nameStrTab;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Verneed {
|
|
|
|
uint64_t nameStrTab;
|
|
|
|
std::vector<Vernaux> vernauxs;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Verneed> verneeds;
|
2016-11-22 00:59:33 +08:00
|
|
|
|
|
|
|
public:
|
2019-04-09 01:48:05 +08:00
|
|
|
VersionNeedSection();
|
2017-02-27 11:07:41 +08:00
|
|
|
void finalizeContents() override;
|
2016-11-22 00:59:33 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
size_t getSize() const override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2016-11-22 00:59:33 +08:00
|
|
|
};
|
|
|
|
|
2017-02-03 21:06:18 +08:00
|
|
|
// MergeSyntheticSection is a class that allows us to put mergeable sections
|
|
|
|
// with different attributes in a single output sections. To do that
|
|
|
|
// we put them into MergeSyntheticSection synthetic input sections which are
|
|
|
|
// attached to regular output sections.
|
2017-09-26 08:54:24 +08:00
|
|
|
class MergeSyntheticSection : public SyntheticSection {
|
2017-02-03 21:06:18 +08:00
|
|
|
public:
|
2017-03-07 04:23:56 +08:00
|
|
|
void addSection(MergeInputSection *ms);
|
2018-03-28 11:20:18 +08:00
|
|
|
std::vector<MergeInputSection *> sections;
|
2017-02-03 21:06:18 +08:00
|
|
|
|
2017-09-26 08:54:24 +08:00
|
|
|
protected:
|
|
|
|
MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags,
|
2017-09-30 19:46:26 +08:00
|
|
|
uint32_t alignment)
|
|
|
|
: SyntheticSection(flags, type, alignment, name) {}
|
2017-09-26 08:54:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class MergeTailSection final : public MergeSyntheticSection {
|
|
|
|
public:
|
|
|
|
MergeTailSection(StringRef name, uint32_t type, uint64_t flags,
|
2017-09-30 19:46:26 +08:00
|
|
|
uint32_t alignment);
|
2017-09-26 08:54:24 +08:00
|
|
|
|
2017-09-30 19:46:26 +08:00
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-09-26 08:54:24 +08:00
|
|
|
void finalizeContents() override;
|
2017-09-30 19:46:26 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
llvm::StringTableBuilder builder;
|
2017-09-26 08:54:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class MergeNoTailSection final : public MergeSyntheticSection {
|
|
|
|
public:
|
|
|
|
MergeNoTailSection(StringRef name, uint32_t type, uint64_t flags,
|
|
|
|
uint32_t alignment)
|
|
|
|
: MergeSyntheticSection(name, type, flags, alignment) {}
|
|
|
|
|
2017-09-30 19:46:26 +08:00
|
|
|
size_t getSize() const override { return size; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-09-26 08:54:24 +08:00
|
|
|
void finalizeContents() override;
|
2017-09-30 19:46:26 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// We use the most significant bits of a hash as a shard ID.
|
|
|
|
// The reason why we don't want to use the least significant bits is
|
|
|
|
// because DenseMap also uses lower bits to determine a bucket ID.
|
|
|
|
// If we use lower bits, it significantly increases the probability of
|
|
|
|
// hash collisons.
|
|
|
|
size_t getShardId(uint32_t hash) {
|
2019-04-18 15:46:09 +08:00
|
|
|
assert((hash >> 31) == 0);
|
|
|
|
return hash >> (31 - llvm::countTrailingZeros(numShards));
|
2017-09-30 19:46:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Section size
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
// String table contents
|
|
|
|
constexpr static size_t numShards = 32;
|
|
|
|
std::vector<llvm::StringTableBuilder> shards;
|
|
|
|
size_t shardOffsets[numShards];
|
2017-02-03 21:06:18 +08:00
|
|
|
};
|
|
|
|
|
2016-11-22 11:57:06 +08:00
|
|
|
// .MIPS.abiflags section.
|
|
|
|
template <class ELFT>
|
2017-02-27 10:56:02 +08:00
|
|
|
class MipsAbiFlagsSection final : public SyntheticSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Mips_ABIFlags = llvm::object::Elf_Mips_ABIFlags<ELFT>;
|
2016-11-22 11:57:06 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static MipsAbiFlagsSection *create();
|
|
|
|
|
|
|
|
MipsAbiFlagsSection(Elf_Mips_ABIFlags flags);
|
|
|
|
size_t getSize() const override { return sizeof(Elf_Mips_ABIFlags); }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Elf_Mips_ABIFlags flags;
|
|
|
|
};
|
|
|
|
|
2016-11-22 12:13:09 +08:00
|
|
|
// .MIPS.options section.
|
2017-02-27 10:56:02 +08:00
|
|
|
template <class ELFT> class MipsOptionsSection final : public SyntheticSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Mips_Options = llvm::object::Elf_Mips_Options<ELFT>;
|
|
|
|
using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
|
2016-11-22 12:13:09 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static MipsOptionsSection *create();
|
|
|
|
|
|
|
|
MipsOptionsSection(Elf_Mips_RegInfo reginfo);
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
|
|
|
|
size_t getSize() const override {
|
|
|
|
return sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Elf_Mips_RegInfo reginfo;
|
|
|
|
};
|
|
|
|
|
2016-11-22 11:57:08 +08:00
|
|
|
// MIPS .reginfo section.
|
2017-02-27 10:56:02 +08:00
|
|
|
template <class ELFT> class MipsReginfoSection final : public SyntheticSection {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Mips_RegInfo = llvm::object::Elf_Mips_RegInfo<ELFT>;
|
2016-11-22 11:57:08 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static MipsReginfoSection *create();
|
|
|
|
|
|
|
|
MipsReginfoSection(Elf_Mips_RegInfo reginfo);
|
|
|
|
size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Elf_Mips_RegInfo reginfo;
|
|
|
|
};
|
|
|
|
|
2016-11-23 01:49:14 +08:00
|
|
|
// This is a MIPS specific section to hold a space within the data segment
|
|
|
|
// of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
|
|
|
|
// See "Dynamic section" in Chapter 5 in the following document:
|
|
|
|
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
2017-03-15 20:02:31 +08:00
|
|
|
class MipsRldMapSection : public SyntheticSection {
|
2016-11-23 01:49:14 +08:00
|
|
|
public:
|
2016-11-23 03:24:52 +08:00
|
|
|
MipsRldMapSection();
|
2017-03-18 07:29:01 +08:00
|
|
|
size_t getSize() const override { return config->wordsize; }
|
2017-05-25 06:04:32 +08:00
|
|
|
void writeTo(uint8_t *buf) override {}
|
2016-11-23 01:49:14 +08:00
|
|
|
};
|
|
|
|
|
2019-03-28 19:10:20 +08:00
|
|
|
// Representation of the combined .ARM.Exidx input sections. We process these
|
|
|
|
// as a SyntheticSection like .eh_frame as we need to merge duplicate entries
|
|
|
|
// and add terminating sentinel entries.
|
|
|
|
//
|
|
|
|
// The .ARM.exidx input sections after SHF_LINK_ORDER processing is done form
|
|
|
|
// a table that the unwinder can derive (Addresses are encoded as offsets from
|
|
|
|
// table):
|
|
|
|
// | Address of function | Unwind instructions for function |
|
|
|
|
// where the unwind instructions are either a small number of unwind or the
|
|
|
|
// special EXIDX_CANTUNWIND entry representing no unwinding information.
|
|
|
|
// When an exception is thrown from an address A, the unwinder searches the
|
|
|
|
// table for the closest table entry with Address of function <= A. This means
|
|
|
|
// that for two consecutive table entries:
|
|
|
|
// | A1 | U1 |
|
|
|
|
// | A2 | U2 |
|
|
|
|
// The range of addresses described by U1 is [A1, A2)
|
|
|
|
//
|
|
|
|
// There are two cases where we need a linker generated table entry to fixup
|
|
|
|
// the address ranges in the table
|
|
|
|
// Case 1:
|
|
|
|
// - A sentinel entry added with an address higher than all
|
|
|
|
// executable sections. This was needed to work around libunwind bug pr31091.
|
|
|
|
// - After address assignment we need to find the highest addressed executable
|
|
|
|
// section and use the limit of that section so that the unwinder never
|
|
|
|
// matches it.
|
|
|
|
// Case 2:
|
|
|
|
// - InputSections without a .ARM.exidx section (usually from Assembly)
|
|
|
|
// need a table entry so that they terminate the range of the previously
|
|
|
|
// function. This is pr40277.
|
|
|
|
//
|
|
|
|
// Instead of storing pointers to the .ARM.exidx InputSections from
|
|
|
|
// InputObjects, we store pointers to the executable sections that need
|
|
|
|
// .ARM.exidx sections. We can then use the dependentSections of these to
|
|
|
|
// either find the .ARM.exidx section or know that we need to generate one.
|
|
|
|
class ARMExidxSyntheticSection : public SyntheticSection {
|
2016-11-24 19:43:55 +08:00
|
|
|
public:
|
2019-03-28 19:10:20 +08:00
|
|
|
ARMExidxSyntheticSection();
|
2019-04-02 02:01:18 +08:00
|
|
|
|
|
|
|
// Add an input section to the ARMExidxSyntheticSection. Returns whether the
|
|
|
|
// section needs to be removed from the main input section list.
|
|
|
|
bool addSection(InputSection *isec);
|
|
|
|
|
2019-03-28 19:10:20 +08:00
|
|
|
size_t getSize() const override { return size; }
|
2016-11-24 19:43:55 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
2019-09-25 05:44:14 +08:00
|
|
|
bool isNeeded() const override;
|
2019-03-28 19:10:20 +08:00
|
|
|
// Sort and remove duplicate entries.
|
|
|
|
void finalizeContents() override;
|
|
|
|
InputSection *getLinkOrderDep() const;
|
2017-12-20 16:56:10 +08:00
|
|
|
|
2018-07-11 23:11:13 +08:00
|
|
|
static bool classof(const SectionBase *d);
|
|
|
|
|
2019-03-28 19:10:20 +08:00
|
|
|
// Links to the ARMExidxSections so we can transfer the relocations once the
|
|
|
|
// layout is known.
|
|
|
|
std::vector<InputSection *> exidxSections;
|
|
|
|
|
|
|
|
private:
|
2020-04-24 18:23:23 +08:00
|
|
|
size_t size = 0;
|
2019-03-28 19:10:20 +08:00
|
|
|
|
|
|
|
// Instead of storing pointers to the .ARM.exidx InputSections from
|
|
|
|
// InputObjects, we store pointers to the executable sections that need
|
|
|
|
// .ARM.exidx sections. We can then use the dependentSections of these to
|
|
|
|
// either find the .ARM.exidx section or know that we need to generate one.
|
|
|
|
std::vector<InputSection *> executableSections;
|
|
|
|
|
|
|
|
// The executable InputSection with the highest address to use for the
|
|
|
|
// sentinel. We store separately from ExecutableSections as merging of
|
|
|
|
// duplicate entries may mean this InputSection is removed from
|
|
|
|
// ExecutableSections.
|
|
|
|
InputSection *sentinel = nullptr;
|
2016-11-24 19:43:55 +08:00
|
|
|
};
|
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
// A container for one or more linker generated thunks. Instances of these
|
|
|
|
// thunks including ARM interworking and Mips LA25 PI to non-PI thunks.
|
2017-03-16 18:40:50 +08:00
|
|
|
class ThunkSection : public SyntheticSection {
|
2017-02-01 18:26:03 +08:00
|
|
|
public:
|
2019-07-16 13:50:45 +08:00
|
|
|
// ThunkSection in OS, with desired outSecOff of Off
|
2017-02-24 23:07:30 +08:00
|
|
|
ThunkSection(OutputSection *os, uint64_t off);
|
2017-02-01 18:26:03 +08:00
|
|
|
|
|
|
|
// Add a newly created Thunk to this container:
|
|
|
|
// Thunk is given offset from start of this InputSection
|
|
|
|
// Thunk defines a symbol in this InputSection that can be used as target
|
|
|
|
// of a relocation
|
2017-03-16 18:40:50 +08:00
|
|
|
void addThunk(Thunk *t);
|
2019-12-10 16:29:23 +08:00
|
|
|
size_t getSize() const override;
|
2017-02-01 18:26:03 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-02-24 00:49:07 +08:00
|
|
|
InputSection *getTargetInputSection() const;
|
2018-03-30 06:32:13 +08:00
|
|
|
bool assignOffsets();
|
2017-02-01 18:26:03 +08:00
|
|
|
|
2020-01-07 23:22:09 +08:00
|
|
|
// When true, round up reported size of section to 4 KiB. See comment
|
|
|
|
// in addThunkSection() for more details.
|
|
|
|
bool roundUpSizeForErrata = false;
|
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
private:
|
2018-03-29 05:33:31 +08:00
|
|
|
std::vector<Thunk *> thunks;
|
2017-02-01 18:26:03 +08:00
|
|
|
size_t size = 0;
|
|
|
|
};
|
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Used to compute outSecOff of .got2 in each object file. This is needed to
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
// synthesize PLT entries for PPC32 Secure PLT ABI.
|
|
|
|
class PPC32Got2Section final : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
PPC32Got2Section();
|
|
|
|
size_t getSize() const override { return 0; }
|
|
|
|
bool isNeeded() const override;
|
|
|
|
void finalizeContents() override;
|
|
|
|
void writeTo(uint8_t *buf) override {}
|
|
|
|
};
|
|
|
|
|
2018-11-15 01:56:43 +08:00
|
|
|
// This section is used to store the addresses of functions that are called
|
2019-10-29 09:41:38 +08:00
|
|
|
// in range-extending thunks on PowerPC64. When producing position dependent
|
2018-11-15 01:56:43 +08:00
|
|
|
// code the addresses are link-time constants and the table is written out to
|
2019-10-29 09:41:38 +08:00
|
|
|
// the binary. When producing position-dependent code the table is allocated and
|
2018-11-15 01:56:43 +08:00
|
|
|
// filled in by the dynamic linker.
|
|
|
|
class PPC64LongBranchTargetSection final : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
PPC64LongBranchTargetSection();
|
2019-12-03 06:20:42 +08:00
|
|
|
uint64_t getEntryVA(const Symbol *sym, int64_t addend);
|
|
|
|
llvm::Optional<uint32_t> addEntry(const Symbol *sym, int64_t addend);
|
2018-11-15 01:56:43 +08:00
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2019-04-01 16:16:08 +08:00
|
|
|
bool isNeeded() const override;
|
2018-11-15 01:56:43 +08:00
|
|
|
void finalizeContents() override { finalized = true; }
|
|
|
|
|
|
|
|
private:
|
2019-12-03 06:20:42 +08:00
|
|
|
std::vector<std::pair<const Symbol *, int64_t>> entries;
|
|
|
|
llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index;
|
2018-11-15 01:56:43 +08:00
|
|
|
bool finalized = false;
|
|
|
|
};
|
|
|
|
|
2019-06-08 01:57:58 +08:00
|
|
|
template <typename ELFT>
|
|
|
|
class PartitionElfHeaderSection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
PartitionElfHeaderSection();
|
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ELFT>
|
|
|
|
class PartitionProgramHeadersSection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
PartitionProgramHeadersSection();
|
|
|
|
size_t getSize() const override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PartitionIndexSection : public SyntheticSection {
|
|
|
|
public:
|
|
|
|
PartitionIndexSection();
|
|
|
|
size_t getSize() const override;
|
|
|
|
void finalizeContents() override;
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
};
|
|
|
|
|
2017-02-27 10:32:49 +08:00
|
|
|
InputSection *createInterpSection();
|
2017-12-21 09:21:59 +08:00
|
|
|
MergeInputSection *createCommentSection();
|
2019-09-24 19:48:31 +08:00
|
|
|
MergeSyntheticSection *createMergeSynthetic(StringRef name, uint32_t type,
|
|
|
|
uint64_t flags, uint32_t alignment);
|
2018-04-28 02:17:36 +08:00
|
|
|
template <class ELFT> void splitSections();
|
2017-05-16 18:11:36 +08:00
|
|
|
|
2019-06-08 01:57:58 +08:00
|
|
|
template <typename ELFT> void writeEhdr(uint8_t *buf, Partition &part);
|
|
|
|
template <typename ELFT> void writePhdrs(uint8_t *buf, Partition &part);
|
|
|
|
|
2018-03-30 06:32:13 +08:00
|
|
|
Defined *addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
|
|
|
|
uint64_t size, InputSectionBase §ion);
|
2016-11-08 22:42:34 +08:00
|
|
|
|
2019-04-09 01:48:05 +08:00
|
|
|
void addVerneed(Symbol *ss);
|
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
// Linker generated per-partition sections.
|
|
|
|
struct Partition {
|
|
|
|
StringRef name;
|
2019-06-08 01:57:58 +08:00
|
|
|
uint64_t nameStrTab;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-06-08 01:57:58 +08:00
|
|
|
SyntheticSection *elfHeader;
|
|
|
|
SyntheticSection *programHeaders;
|
|
|
|
std::vector<PhdrEntry *> phdrs;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-03-28 19:10:20 +08:00
|
|
|
ARMExidxSyntheticSection *armExidx;
|
2018-09-26 03:26:58 +08:00
|
|
|
BuildIdSection *buildId;
|
|
|
|
SyntheticSection *dynamic;
|
|
|
|
StringTableSection *dynStrTab;
|
|
|
|
SymbolTableBaseSection *dynSymTab;
|
2019-06-08 01:57:58 +08:00
|
|
|
EhFrameHeader *ehFrameHdr;
|
|
|
|
EhFrameSection *ehFrame;
|
2018-09-26 03:26:58 +08:00
|
|
|
GnuHashTableSection *gnuHashTab;
|
|
|
|
HashTableSection *hashTab;
|
2019-06-08 01:57:58 +08:00
|
|
|
RelocationBaseSection *relaDyn;
|
|
|
|
RelrBaseSection *relrDyn;
|
|
|
|
VersionDefinitionSection *verDef;
|
|
|
|
SyntheticSection *verNeed;
|
|
|
|
VersionTableSection *verSym;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-06-08 01:57:58 +08:00
|
|
|
unsigned getNumber() const { return this - &partitions[0] + 1; }
|
|
|
|
};
|
|
|
|
|
|
|
|
extern Partition *mainPart;
|
|
|
|
|
|
|
|
inline Partition &SectionBase::getPartition() const {
|
|
|
|
assert(isLive());
|
|
|
|
return partitions[partition - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linker generated sections which can be used as inputs and are not specific to
|
|
|
|
// a partition.
|
|
|
|
struct InStruct {
|
|
|
|
InputSection *armAttributes;
|
|
|
|
BssSection *bss;
|
|
|
|
BssSection *bssRelRo;
|
2018-09-26 03:26:58 +08:00
|
|
|
GotSection *got;
|
|
|
|
GotPltSection *gotPlt;
|
|
|
|
IgotPltSection *igotPlt;
|
2018-11-15 01:56:43 +08:00
|
|
|
PPC64LongBranchTargetSection *ppc64LongBranchTarget;
|
2018-09-26 03:26:58 +08:00
|
|
|
MipsGotSection *mipsGot;
|
|
|
|
MipsRldMapSection *mipsRldMap;
|
2019-06-08 01:57:58 +08:00
|
|
|
SyntheticSection *partEnd;
|
|
|
|
SyntheticSection *partIndex;
|
2018-09-26 03:26:58 +08:00
|
|
|
PltSection *plt;
|
2019-12-15 06:17:35 +08:00
|
|
|
IpltSection *iplt;
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
PPC32Got2Section *ppc32Got2;
|
2019-12-11 10:05:36 +08:00
|
|
|
IBTPltSection *ibtPlt;
|
2018-09-26 03:26:58 +08:00
|
|
|
RelocationBaseSection *relaPlt;
|
|
|
|
RelocationBaseSection *relaIplt;
|
|
|
|
StringTableSection *shStrTab;
|
|
|
|
StringTableSection *strTab;
|
|
|
|
SymbolTableBaseSection *symTab;
|
|
|
|
SymtabShndxSection *symTabShndx;
|
2017-03-15 23:29:29 +08:00
|
|
|
};
|
|
|
|
|
2018-09-26 03:26:58 +08:00
|
|
|
extern InStruct in;
|
|
|
|
|
2016-11-02 04:28:21 +08:00
|
|
|
} // namespace elf
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|