2015-09-22 08:01:39 +08:00
|
|
|
//===- InputSection.h -------------------------------------------*- C++ -*-===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-22 08:01:39 +08:00
|
|
|
#ifndef LLD_ELF_INPUT_SECTION_H
|
|
|
|
#define LLD_ELF_INPUT_SECTION_H
|
2015-07-25 05:03:07 +08:00
|
|
|
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
#include "Config.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
2016-02-23 11:34:37 +08:00
|
|
|
#include "llvm/ADT/TinyPtrVector.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
namespace lld {
|
2016-02-28 08:25:54 +08:00
|
|
|
namespace elf {
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-02-26 02:43:51 +08:00
|
|
|
template <class ELFT> class ICF;
|
2015-07-25 05:03:07 +08:00
|
|
|
template <class ELFT> class ObjectFile;
|
2015-08-25 04:06:32 +08:00
|
|
|
template <class ELFT> class OutputSection;
|
2015-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> class OutputSectionBase;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// This corresponds to a section of an input file.
|
2015-10-20 05:00:02 +08:00
|
|
|
template <class ELFT> class InputSectionBase {
|
|
|
|
protected:
|
2015-10-28 05:51:13 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
2015-09-15 20:43:09 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
2015-09-22 06:01:00 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-08-25 03:28:31 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-10-20 05:00:02 +08:00
|
|
|
const Elf_Shdr *Header;
|
|
|
|
|
|
|
|
// The file this section is from.
|
|
|
|
ObjectFile<ELFT> *File;
|
2015-08-14 06:21:37 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
public:
|
2015-12-20 18:57:34 +08:00
|
|
|
enum Kind { Regular, EHFrame, Merge, MipsReginfo };
|
2015-10-20 05:00:02 +08:00
|
|
|
Kind SectionKind;
|
|
|
|
|
|
|
|
InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
|
|
|
|
Kind SectionKind);
|
|
|
|
OutputSectionBase<ELFT> *OutSec = nullptr;
|
2016-02-26 02:49:09 +08:00
|
|
|
uint32_t Align;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
// Used for garbage collection.
|
2016-02-26 02:49:09 +08:00
|
|
|
bool Live;
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
|
2016-02-26 02:43:51 +08:00
|
|
|
// This pointer points to the "real" instance of this instance.
|
|
|
|
// Usually Repl == this. However, if ICF merges two sections,
|
|
|
|
// Repl pointer of one section points to another section. So,
|
|
|
|
// if you need to get a pointer to this instance, do not use
|
|
|
|
// this but instead this->Repl.
|
|
|
|
InputSectionBase<ELFT> *Repl;
|
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// Returns the size of this section (even if this is a common or BSS.)
|
2015-08-14 06:21:37 +08:00
|
|
|
size_t getSize() const { return Header->sh_size; }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-02-25 02:33:35 +08:00
|
|
|
static InputSectionBase<ELFT> *Discarded;
|
2015-08-14 06:21:37 +08:00
|
|
|
|
|
|
|
StringRef getSectionName() const;
|
|
|
|
const Elf_Shdr *getSectionHdr() const { return Header; }
|
2015-09-24 23:11:50 +08:00
|
|
|
ObjectFile<ELFT> *getFile() const { return File; }
|
2015-10-24 03:55:11 +08:00
|
|
|
uintX_t getOffset(const Elf_Sym &Sym);
|
2015-11-12 00:50:37 +08:00
|
|
|
|
|
|
|
// Translate an offset in the input section to an offset in the output
|
|
|
|
// section.
|
|
|
|
uintX_t getOffset(uintX_t Offset);
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
ArrayRef<uint8_t> getSectionData() const;
|
2015-10-28 05:51:13 +08:00
|
|
|
|
|
|
|
// Returns a section that Rel is pointing to. Used by the garbage collector.
|
2016-02-24 08:23:13 +08:00
|
|
|
InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel) const;
|
|
|
|
InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel) const;
|
2015-11-11 18:18:52 +08:00
|
|
|
|
|
|
|
template <bool isRela>
|
2015-12-02 05:24:45 +08:00
|
|
|
using RelIteratorRange =
|
|
|
|
llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, isRela> *>;
|
|
|
|
|
|
|
|
template <bool isRela>
|
|
|
|
void relocate(uint8_t *Buf, uint8_t *BufEnd, RelIteratorRange<isRela> Rels);
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <bool isRela>
|
2015-12-13 14:49:08 +08:00
|
|
|
uint8_t *findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex, uint32_t Type,
|
2015-12-02 05:24:45 +08:00
|
|
|
RelIteratorRange<isRela> Rels);
|
2015-10-20 05:00:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-02-25 02:33:35 +08:00
|
|
|
InputSectionBase<ELFT> *
|
|
|
|
InputSectionBase<ELFT>::Discarded = (InputSectionBase<ELFT> *)-1ULL;
|
2015-10-20 05:00:02 +08:00
|
|
|
|
2016-01-06 11:16:23 +08:00
|
|
|
// Usually sections are copied to the output as atomic chunks of data,
|
|
|
|
// but some special types of sections are split into small pieces of data
|
|
|
|
// and each piece is copied to a different place in the output.
|
|
|
|
// This class represents such special sections.
|
2015-11-12 03:54:14 +08:00
|
|
|
template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
|
|
|
|
typename InputSectionBase<ELFT>::Kind SectionKind);
|
2016-01-06 11:16:23 +08:00
|
|
|
|
|
|
|
// For each piece of data, we maintain the offsets in the input section and
|
|
|
|
// in the output section. The latter may be -1 if it is not assigned yet.
|
2015-11-12 03:54:14 +08:00
|
|
|
std::vector<std::pair<uintX_t, uintX_t>> Offsets;
|
2016-01-06 11:16:23 +08:00
|
|
|
|
2015-11-12 03:54:14 +08:00
|
|
|
std::pair<std::pair<uintX_t, uintX_t> *, uintX_t>
|
|
|
|
getRangeAndSize(uintX_t Offset);
|
|
|
|
};
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
// This corresponds to a SHF_MERGE section of an input file.
|
2015-11-12 03:54:14 +08:00
|
|
|
template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
|
2015-10-20 05:00:02 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
|
|
|
|
static bool classof(const InputSectionBase<ELFT> *S);
|
2015-10-25 06:51:01 +08:00
|
|
|
// Translate an offset in the input section to an offset in the output
|
|
|
|
// section.
|
2015-10-24 03:55:11 +08:00
|
|
|
uintX_t getOffset(uintX_t Offset);
|
2015-10-20 05:00:02 +08:00
|
|
|
};
|
|
|
|
|
2015-11-12 03:54:14 +08:00
|
|
|
// This corresponds to a .eh_frame section of an input file.
|
|
|
|
template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
|
|
|
|
public:
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
|
|
|
|
static bool classof(const InputSectionBase<ELFT> *S);
|
|
|
|
|
|
|
|
// Translate an offset in the input section to an offset in the output
|
|
|
|
// section.
|
|
|
|
uintX_t getOffset(uintX_t Offset);
|
|
|
|
|
|
|
|
// Relocation section that refer to this one.
|
|
|
|
const Elf_Shdr *RelocSection = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
// This corresponds to a non SHF_MERGE section of an input file.
|
|
|
|
template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
|
2016-02-26 02:43:51 +08:00
|
|
|
friend ICF<ELFT>;
|
2015-10-20 05:00:02 +08:00
|
|
|
typedef InputSectionBase<ELFT> Base;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
|
|
|
|
|
|
|
|
// Write this section to a mmap'ed file, assuming Buf is pointing to
|
|
|
|
// beginning of the output section.
|
|
|
|
void writeTo(uint8_t *Buf);
|
|
|
|
|
2015-08-28 07:15:56 +08:00
|
|
|
// Relocation sections that refer to this one.
|
2016-02-23 11:34:37 +08:00
|
|
|
llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
|
2015-08-28 07:15:56 +08:00
|
|
|
|
2015-10-15 05:00:23 +08:00
|
|
|
// The offset from beginning of the output sections this section was assigned
|
|
|
|
// to. The writer sets a value.
|
2015-10-15 09:58:40 +08:00
|
|
|
uint64_t OutSecOff = 0;
|
2015-10-15 05:00:23 +08:00
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
static bool classof(const InputSectionBase<ELFT> *S);
|
2016-02-25 16:23:37 +08:00
|
|
|
|
|
|
|
InputSectionBase<ELFT> *getRelocatedSection();
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <bool isRela>
|
|
|
|
using RelIteratorRange =
|
|
|
|
llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, isRela> *>;
|
|
|
|
|
|
|
|
template <bool isRela>
|
|
|
|
void copyRelocations(uint8_t *Buf, RelIteratorRange<isRela> Rels);
|
2016-02-26 02:43:51 +08:00
|
|
|
|
|
|
|
// Called by ICF to merge two input sections.
|
|
|
|
void replace(InputSection<ELFT> *Other);
|
|
|
|
|
|
|
|
// Used by ICF.
|
|
|
|
uint64_t GroupId = 0;
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
2015-12-20 18:57:34 +08:00
|
|
|
// MIPS .reginfo section provides information on the registers used by the code
|
|
|
|
// in the object file. Linker should collect this information and write a single
|
|
|
|
// .reginfo section in the output file. The output section contains a union of
|
|
|
|
// used registers masks taken from input .reginfo sections and final value
|
|
|
|
// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
|
|
|
|
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
|
|
|
template <class ELFT>
|
|
|
|
class MipsReginfoInputSection : public InputSectionBase<ELFT> {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
|
|
|
|
public:
|
2016-01-07 06:42:43 +08:00
|
|
|
MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
|
2015-12-20 18:57:34 +08:00
|
|
|
static bool classof(const InputSectionBase<ELFT> *S);
|
2016-01-07 06:42:43 +08:00
|
|
|
|
|
|
|
const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo;
|
2015-12-20 18:57:34 +08:00
|
|
|
};
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
} // namespace elf
|
2015-07-25 05:03:07 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|