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"
|
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
|
|
|
|
|
|
|
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-11-12 03:54:14 +08:00
|
|
|
enum Kind { Regular, EHFrame, Merge };
|
2015-10-20 05:00:02 +08:00
|
|
|
Kind SectionKind;
|
|
|
|
|
|
|
|
InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
|
|
|
|
Kind SectionKind);
|
|
|
|
OutputSectionBase<ELFT> *OutSec = nullptr;
|
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.
|
|
|
|
// Live bit makes sense only when Config->GcSections is true.
|
|
|
|
bool isLive() const { return !Config->GcSections || Live; }
|
|
|
|
bool Live = false;
|
|
|
|
|
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
|
|
|
|
2015-10-20 05:00:02 +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-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// The writer sets and uses the addresses.
|
2015-09-05 08:25:33 +08:00
|
|
|
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);
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
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.
|
|
|
|
InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel);
|
|
|
|
InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel);
|
2015-11-11 18:18:52 +08:00
|
|
|
|
|
|
|
template <bool isRela>
|
|
|
|
void relocate(uint8_t *Buf, uint8_t *BufEnd,
|
|
|
|
llvm::iterator_range<
|
2015-11-12 03:54:14 +08:00
|
|
|
const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels);
|
2015-10-20 05:00:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT>
|
|
|
|
InputSectionBase<ELFT>::Discarded(nullptr, nullptr,
|
|
|
|
InputSectionBase<ELFT>::Regular);
|
|
|
|
|
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);
|
|
|
|
std::vector<std::pair<uintX_t, uintX_t>> Offsets;
|
|
|
|
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> {
|
|
|
|
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.
|
|
|
|
SmallVector<const Elf_Shdr *, 1> RelocSections;
|
|
|
|
|
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);
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace elf2
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|