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"
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "Relocations.h"
|
2016-07-09 00:10:27 +08:00
|
|
|
#include "Thunks.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2016-12-19 11:14:16 +08:00
|
|
|
#include "llvm/ADT/CachedHashString.h"
|
2016-05-24 00:55:43 +08:00
|
|
|
#include "llvm/ADT/DenseSet.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
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
class Symbol;
|
2016-10-06 02:40:00 +08:00
|
|
|
struct SectionPiece;
|
2016-04-15 22:41:56 +08:00
|
|
|
|
2017-11-06 12:35:31 +08:00
|
|
|
class Defined;
|
2017-03-07 05:17:18 +08:00
|
|
|
class SyntheticSection;
|
2017-03-07 04:23:56 +08:00
|
|
|
class MergeSyntheticSection;
|
2017-07-27 06:13:32 +08:00
|
|
|
template <class ELFT> class ObjFile;
|
2017-02-24 23:07:30 +08:00
|
|
|
class OutputSection;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
// This is the base class of all sections that lld handles. Some are sections in
|
|
|
|
// input files, some are sections in the produced output file and some exist
|
|
|
|
// just as a convenience for implementing special ways of combining some
|
|
|
|
// sections.
|
|
|
|
class SectionBase {
|
2016-09-01 17:55:57 +08:00
|
|
|
public:
|
2017-03-09 06:36:28 +08:00
|
|
|
enum Kind { Regular, EHFrame, Merge, Synthetic, Output };
|
2016-09-01 17:55:57 +08:00
|
|
|
|
2016-09-08 20:33:41 +08:00
|
|
|
Kind kind() const { return (Kind)SectionKind; }
|
2016-09-12 21:13:53 +08:00
|
|
|
|
2017-02-23 10:32:18 +08:00
|
|
|
StringRef Name;
|
2016-10-26 08:54:03 +08:00
|
|
|
|
2017-12-14 06:59:23 +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.
|
|
|
|
SectionBase *Repl;
|
|
|
|
|
2017-02-23 10:32:18 +08:00
|
|
|
unsigned SectionKind : 3;
|
2016-09-01 17:55:57 +08:00
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
// The next two bit fields are only used by InputSectionBase, but we
|
|
|
|
// put them here so the struct packs better.
|
|
|
|
|
2017-02-23 10:32:18 +08:00
|
|
|
// The garbage collector sets sections' Live bits.
|
|
|
|
// If GC is disabled, all sections are considered live by default.
|
2017-10-08 09:55:29 +08:00
|
|
|
unsigned Live : 1;
|
2017-02-23 10:32:18 +08:00
|
|
|
|
2017-11-06 12:33:58 +08:00
|
|
|
unsigned Bss : 1;
|
|
|
|
|
2016-10-26 08:54:03 +08:00
|
|
|
// These corresponds to the fields in Elf_Shdr.
|
2017-10-08 09:55:29 +08:00
|
|
|
uint32_t Alignment;
|
2017-02-23 10:28:28 +08:00
|
|
|
uint64_t Flags;
|
|
|
|
uint64_t Entsize;
|
2016-10-26 08:54:03 +08:00
|
|
|
uint32_t Type;
|
|
|
|
uint32_t Link;
|
|
|
|
uint32_t Info;
|
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
OutputSection *getOutputSection();
|
|
|
|
const OutputSection *getOutputSection() const {
|
|
|
|
return const_cast<SectionBase *>(this)->getOutputSection();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate an offset in the input section to an offset in the output
|
|
|
|
// section.
|
|
|
|
uint64_t getOffset(uint64_t Offset) const;
|
|
|
|
|
2018-04-14 00:07:27 +08:00
|
|
|
uint64_t getVA(uint64_t Offset = 0) const;
|
2018-03-24 08:35:11 +08:00
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
protected:
|
|
|
|
SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags,
|
|
|
|
uint64_t Entsize, uint64_t Alignment, uint32_t Type,
|
|
|
|
uint32_t Info, uint32_t Link)
|
2017-12-14 06:59:23 +08:00
|
|
|
: Name(Name), Repl(this), SectionKind(SectionKind), Live(false),
|
|
|
|
Bss(false), Alignment(Alignment), Flags(Flags), Entsize(Entsize),
|
|
|
|
Type(Type), Link(Link), Info(Info) {}
|
2017-03-09 06:36:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// This corresponds to a section of an input file.
|
|
|
|
class InputSectionBase : public SectionBase {
|
|
|
|
public:
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT>
|
2017-12-21 10:03:39 +08:00
|
|
|
InputSectionBase(ObjFile<ELFT> &File, const typename ELFT::Shdr &Header,
|
2016-09-08 22:06:08 +08:00
|
|
|
StringRef Name, Kind SectionKind);
|
2017-02-23 10:28:28 +08:00
|
|
|
|
|
|
|
InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type,
|
|
|
|
uint64_t Entsize, uint32_t Link, uint32_t Info,
|
2017-03-09 03:35:29 +08:00
|
|
|
uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name,
|
2016-10-26 08:54:03 +08:00
|
|
|
Kind SectionKind);
|
2017-06-01 04:17:44 +08:00
|
|
|
|
2017-10-10 11:22:29 +08:00
|
|
|
static bool classof(const SectionBase *S) { return S->kind() != Output; }
|
|
|
|
|
|
|
|
// The file which contains this section. It's dynamic type is always
|
|
|
|
// ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
|
|
|
|
// its static type.
|
|
|
|
InputFile *File;
|
|
|
|
|
|
|
|
template <class ELFT> ObjFile<ELFT> *getFile() const {
|
|
|
|
return cast_or_null<ObjFile<ELFT>>(File);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> Data;
|
|
|
|
uint64_t getOffsetInFile() const;
|
|
|
|
|
2017-10-30 07:32:23 +08:00
|
|
|
// True if this section has already been placed to a linker script
|
|
|
|
// output section. This is needed because, in a linker script, you
|
|
|
|
// can refer to the same section more than once. For example, in
|
|
|
|
// the following linker script,
|
|
|
|
//
|
|
|
|
// .foo : { *(.text) }
|
|
|
|
// .bar : { *(.text) }
|
|
|
|
//
|
|
|
|
// .foo takes all .text sections, and .bar becomes empty. To achieve
|
|
|
|
// this, we need to memorize whether a section has been placed or
|
|
|
|
// not for each input section.
|
|
|
|
bool Assigned = false;
|
|
|
|
|
2017-06-01 04:17:44 +08:00
|
|
|
// Input sections are part of an output section. Special sections
|
|
|
|
// like .eh_frame and merge sections are first combined into a
|
|
|
|
// synthetic section that is then added to an output section. In all
|
|
|
|
// cases this points one level up.
|
|
|
|
SectionBase *Parent = nullptr;
|
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-11-10 22:53:24 +08:00
|
|
|
// Relocations that refer to this section.
|
2017-02-23 10:28:28 +08:00
|
|
|
const void *FirstRelocation = nullptr;
|
2016-11-10 22:53:24 +08:00
|
|
|
unsigned NumRelocations : 31;
|
|
|
|
unsigned AreRelocsRela : 1;
|
2017-10-10 11:22:29 +08:00
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
|
2016-11-10 22:53:24 +08:00
|
|
|
assert(!AreRelocsRela);
|
2017-02-23 10:28:28 +08:00
|
|
|
return llvm::makeArrayRef(
|
|
|
|
static_cast<const typename ELFT::Rel *>(FirstRelocation),
|
|
|
|
NumRelocations);
|
2016-11-10 22:53:24 +08:00
|
|
|
}
|
2017-10-10 11:22:29 +08:00
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
|
2016-11-10 22:53:24 +08:00
|
|
|
assert(AreRelocsRela);
|
2017-02-23 10:28:28 +08:00
|
|
|
return llvm::makeArrayRef(
|
|
|
|
static_cast<const typename ELFT::Rela *>(FirstRelocation),
|
|
|
|
NumRelocations);
|
2016-11-10 22:53:24 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 03:34:05 +08:00
|
|
|
// InputSections that are dependent on us (reverse dependency for GC)
|
2017-10-11 12:01:13 +08:00
|
|
|
llvm::TinyPtrVector<InputSection *> DependentSections;
|
2017-02-18 03:34:05 +08:00
|
|
|
|
2016-11-08 22:47:16 +08:00
|
|
|
// Returns the size of this section (even if this is a common or BSS.)
|
2017-03-08 23:44:30 +08:00
|
|
|
size_t getSize() const;
|
2016-11-08 22:47:16 +08:00
|
|
|
|
2017-06-01 03:09:52 +08:00
|
|
|
InputSection *getLinkOrderDep() const;
|
2015-11-12 00:50:37 +08:00
|
|
|
|
2017-10-10 11:40:57 +08:00
|
|
|
// Compilers emit zlib-compressed debug sections if the -gz option
|
|
|
|
// is given. This function checks if this section is compressed, and
|
|
|
|
// if so, decompress in memory.
|
2018-02-13 05:56:14 +08:00
|
|
|
void maybeDecompress();
|
2016-06-24 19:18:44 +08:00
|
|
|
|
2016-11-26 02:51:53 +08:00
|
|
|
// Returns a source location string. Used to construct an error message.
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> std::string getLocation(uint64_t Offset);
|
2017-11-04 05:21:47 +08:00
|
|
|
std::string getSrcMsg(const Symbol &Sym, uint64_t Offset);
|
2017-10-27 11:13:54 +08:00
|
|
|
std::string getObjMsg(uint64_t Offset);
|
2016-11-26 02:51:53 +08:00
|
|
|
|
2017-10-10 11:40:57 +08:00
|
|
|
// Each section knows how to relocate itself. These functions apply
|
|
|
|
// relocations, assuming that Buf points to this section's copy in
|
|
|
|
// the mmap'ed output buffer.
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd);
|
2017-05-19 00:45:36 +08:00
|
|
|
void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd);
|
2017-02-23 10:32:18 +08:00
|
|
|
|
2017-10-10 11:40:57 +08:00
|
|
|
// The native ELF reloc data type is not very convenient to handle.
|
|
|
|
// So we convert ELF reloc records to our own records in Relocations.cpp.
|
|
|
|
// This vector contains such "cooked" relocations.
|
2017-02-23 10:32:18 +08:00
|
|
|
std::vector<Relocation> Relocations;
|
|
|
|
|
|
|
|
template <typename T> llvm::ArrayRef<T> getDataAs() const {
|
|
|
|
size_t S = Data.size();
|
|
|
|
assert(S % sizeof(T) == 0);
|
|
|
|
return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
|
|
|
|
}
|
2017-08-17 08:27:55 +08:00
|
|
|
|
|
|
|
private:
|
2018-02-13 05:56:14 +08:00
|
|
|
// A pointer that owns decompressed data if a section is compressed by zlib.
|
2017-08-17 08:27:55 +08:00
|
|
|
// Since the feature is not used often, this is usually a nullptr.
|
2018-02-13 05:56:14 +08:00
|
|
|
std::unique_ptr<char[]> DecompressBuf;
|
2015-10-20 05:00:02 +08:00
|
|
|
};
|
|
|
|
|
2016-05-22 08:13:04 +08:00
|
|
|
// SectionPiece represents a piece of splittable section contents.
|
2016-10-20 18:55:58 +08:00
|
|
|
// We allocate a lot of these and binary search on them. This means that they
|
|
|
|
// have to be as compact as possible, which is why we don't store the size (can
|
2017-10-25 05:44:43 +08:00
|
|
|
// be found by looking at the next one).
|
2016-05-22 08:13:04 +08:00
|
|
|
struct SectionPiece {
|
2017-10-22 07:20:13 +08:00
|
|
|
SectionPiece(size_t Off, uint32_t Hash, bool Live)
|
2018-04-05 23:56:04 +08:00
|
|
|
: InputOff(Off), Hash(Hash), OutputOff(0),
|
2017-10-22 07:20:13 +08:00
|
|
|
Live(Live || !Config->GcSections) {}
|
|
|
|
|
|
|
|
uint32_t InputOff;
|
|
|
|
uint32_t Hash;
|
2017-10-24 03:31:31 +08:00
|
|
|
int64_t OutputOff : 63;
|
2017-10-22 07:20:13 +08:00
|
|
|
uint64_t Live : 1;
|
2016-05-22 08:13:04 +08:00
|
|
|
};
|
2017-10-22 07:20:13 +08:00
|
|
|
|
|
|
|
static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
|
2016-05-22 08:13:04 +08:00
|
|
|
|
2015-10-20 05:00:02 +08:00
|
|
|
// This corresponds to a SHF_MERGE section of an input file.
|
2017-03-07 04:23:56 +08:00
|
|
|
class MergeInputSection : public InputSectionBase {
|
2015-10-20 05:00:02 +08:00
|
|
|
public:
|
2017-03-07 04:23:56 +08:00
|
|
|
template <class ELFT>
|
2017-12-21 10:03:39 +08:00
|
|
|
MergeInputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header,
|
2016-09-08 22:06:08 +08:00
|
|
|
StringRef Name);
|
2017-12-21 09:21:59 +08:00
|
|
|
MergeInputSection(uint64_t Flags, uint32_t Type, uint64_t Entsize,
|
|
|
|
ArrayRef<uint8_t> Data, StringRef Name);
|
|
|
|
|
2017-10-02 07:46:31 +08:00
|
|
|
static bool classof(const SectionBase *S) { return S->kind() == Merge; }
|
2016-05-24 00:55:43 +08:00
|
|
|
void splitIntoPieces();
|
|
|
|
|
2018-04-20 00:05:07 +08:00
|
|
|
// Translate an offset in the input section to an offset in the parent
|
|
|
|
// MergeSyntheticSection.
|
|
|
|
uint64_t getParentOffset(uint64_t Offset) const;
|
2016-05-24 00:55:43 +08:00
|
|
|
|
2016-07-21 21:32:37 +08:00
|
|
|
// Splittable sections are handled as a sequence of data
|
|
|
|
// rather than a single large blob of data.
|
|
|
|
std::vector<SectionPiece> Pieces;
|
2018-04-04 05:38:18 +08:00
|
|
|
llvm::DenseMap<uint32_t, uint32_t> OffsetMap;
|
2016-12-06 10:19:30 +08:00
|
|
|
|
|
|
|
// Returns I'th piece's data. This function is very hot when
|
|
|
|
// string merging is enabled, so we want to inline.
|
|
|
|
LLVM_ATTRIBUTE_ALWAYS_INLINE
|
|
|
|
llvm::CachedHashStringRef getData(size_t I) const {
|
|
|
|
size_t Begin = Pieces[I].InputOff;
|
2017-10-22 07:20:13 +08:00
|
|
|
size_t End =
|
|
|
|
(Pieces.size() - 1 == I) ? Data.size() : Pieces[I + 1].InputOff;
|
|
|
|
return {toStringRef(Data.slice(Begin, End - Begin)), Pieces[I].Hash};
|
2016-12-06 10:19:30 +08:00
|
|
|
}
|
2016-07-21 21:32:37 +08:00
|
|
|
|
|
|
|
// Returns the SectionPiece at a given input section offset.
|
2017-03-07 04:23:56 +08:00
|
|
|
SectionPiece *getSectionPiece(uint64_t Offset);
|
2018-03-28 11:14:11 +08:00
|
|
|
const SectionPiece *getSectionPiece(uint64_t Offset) const {
|
|
|
|
return const_cast<MergeInputSection *>(this)->getSectionPiece(Offset);
|
|
|
|
}
|
2016-07-21 21:32:37 +08:00
|
|
|
|
2017-06-01 04:17:44 +08:00
|
|
|
SyntheticSection *getParent() const;
|
2017-02-03 21:06:18 +08:00
|
|
|
|
2016-05-24 00:55:43 +08:00
|
|
|
private:
|
2016-11-26 23:15:11 +08:00
|
|
|
void splitStrings(ArrayRef<uint8_t> A, size_t Size);
|
|
|
|
void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
|
2015-10-20 05:00:02 +08:00
|
|
|
};
|
|
|
|
|
2017-09-19 07:07:21 +08:00
|
|
|
struct EhSectionPiece {
|
2017-09-20 16:03:18 +08:00
|
|
|
EhSectionPiece(size_t Off, InputSectionBase *Sec, uint32_t Size,
|
|
|
|
unsigned FirstRelocation)
|
|
|
|
: InputOff(Off), Sec(Sec), Size(Size), FirstRelocation(FirstRelocation) {}
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> data() { return {Sec->Data.data() + this->InputOff, Size}; }
|
2017-09-19 07:07:21 +08:00
|
|
|
|
2017-09-20 16:03:18 +08:00
|
|
|
size_t InputOff;
|
|
|
|
ssize_t OutputOff = -1;
|
|
|
|
InputSectionBase *Sec;
|
2017-09-19 07:07:21 +08:00
|
|
|
uint32_t Size;
|
2017-09-20 16:03:18 +08:00
|
|
|
unsigned FirstRelocation;
|
2016-07-22 04:18:30 +08:00
|
|
|
};
|
|
|
|
|
2015-11-12 03:54:14 +08:00
|
|
|
// This corresponds to a .eh_frame section of an input file.
|
2017-03-07 05:17:18 +08:00
|
|
|
class EhInputSection : public InputSectionBase {
|
2015-11-12 03:54:14 +08:00
|
|
|
public:
|
2017-03-07 05:17:18 +08:00
|
|
|
template <class ELFT>
|
2017-12-21 10:03:39 +08:00
|
|
|
EhInputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header,
|
2017-03-07 05:17:18 +08:00
|
|
|
StringRef Name);
|
2017-10-02 07:46:31 +08:00
|
|
|
static bool classof(const SectionBase *S) { return S->kind() == EHFrame; }
|
2017-03-07 05:17:18 +08:00
|
|
|
template <class ELFT> void split();
|
|
|
|
template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels);
|
2015-11-12 03:54:14 +08:00
|
|
|
|
2016-07-21 21:32:37 +08:00
|
|
|
// Splittable sections are handled as a sequence of data
|
|
|
|
// rather than a single large blob of data.
|
2016-07-22 04:18:30 +08:00
|
|
|
std::vector<EhSectionPiece> Pieces;
|
2017-06-01 04:17:44 +08:00
|
|
|
|
|
|
|
SyntheticSection *getParent() const;
|
2015-11-12 03:54:14 +08:00
|
|
|
};
|
|
|
|
|
2017-02-24 21:06:59 +08:00
|
|
|
// This is a section that is added directly to an output section
|
|
|
|
// instead of needing special combination via a synthetic section. This
|
|
|
|
// includes all input sections with the exceptions of SHF_MERGE and
|
|
|
|
// .eh_frame. It also includes the synthetic sections themselves.
|
2017-02-24 00:49:07 +08:00
|
|
|
class InputSection : public InputSectionBase {
|
2015-10-20 05:00:02 +08:00
|
|
|
public:
|
2017-12-21 10:11:51 +08:00
|
|
|
InputSection(InputFile *F, uint64_t Flags, uint32_t Type, uint32_t Alignment,
|
2017-02-23 10:32:18 +08:00
|
|
|
ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular);
|
2017-02-24 00:49:07 +08:00
|
|
|
template <class ELFT>
|
2017-12-21 10:03:39 +08:00
|
|
|
InputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header,
|
2017-02-24 00:49:07 +08:00
|
|
|
StringRef Name);
|
2015-10-20 05:00:02 +08:00
|
|
|
|
|
|
|
// Write this section to a mmap'ed file, assuming Buf is pointing to
|
|
|
|
// beginning of the output section.
|
2017-02-24 00:49:07 +08:00
|
|
|
template <class ELFT> void writeTo(uint8_t *Buf);
|
2015-10-20 05:00:02 +08:00
|
|
|
|
2018-04-20 02:00:46 +08:00
|
|
|
uint64_t getOffset(uint64_t Offset) const { return OutSecOff + Offset; }
|
|
|
|
|
2017-06-01 04:17:44 +08:00
|
|
|
OutputSection *getParent() const;
|
|
|
|
|
[ELF] Reset OutputSection size prior to processing linker script commands
The size of an OutputSection is calculated early, to aid handling of compressed
debug sections. However, subsequent to this point, unused synthetic sections are
removed. In the event that an OutputSection, from which such an InputSection is
removed, is still required (e.g. because it has a symbol assignment), and no longer
has any InputSections, dot assignments, or BYTE()-family directives, the size
member is never updated when processing the commands. If the removed InputSection
had a non-zero size (such as a .got.plt section), the section ends up with the
wrong size in the output.
The fix is to reset the OutputSection size prior to processing the linker script
commands relating to that OutputSection. This ensures that the size is correct even
in the above situation.
Additionally, to reduce the risk of developers misusing OutputSection Size and
InputSection OutSecOff, they are set to simply the number of InputSections in an
OutputSection, and the corresponding index respectively. We cannot completely
stop using them, due to SHF_LINK_ORDER sections requiring them.
Compressed debug sections also require the full size. This is now calculated in
maybeCompress for these kinds of sections.
Reviewers: ruiu, rafael
Differential Revision: https://reviews.llvm.org/D38361
llvm-svn: 320472
2017-12-12 19:51:13 +08:00
|
|
|
// This variable has two usages. Initially, it represents an index in the
|
|
|
|
// OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
|
|
|
|
// sections. After assignAddresses is called, it represents the offset from
|
|
|
|
// the beginning of the output section this section was assigned to.
|
2017-12-13 01:37:01 +08:00
|
|
|
uint64_t OutSecOff = 0;
|
|
|
|
|
|
|
|
static bool classof(const SectionBase *S);
|
|
|
|
|
|
|
|
InputSectionBase *getRelocatedSection();
|
2016-02-25 16:23:37 +08:00
|
|
|
|
2017-02-24 00:49:07 +08:00
|
|
|
template <class ELFT, class RelTy>
|
2016-04-29 02:42:04 +08:00
|
|
|
void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
|
|
|
|
|
2016-11-20 10:39:59 +08:00
|
|
|
// Used by ICF.
|
2016-12-06 02:11:35 +08:00
|
|
|
uint32_t Class[2] = {0, 0};
|
2016-02-26 02:43:51 +08:00
|
|
|
|
|
|
|
// Called by ICF to merge two input sections.
|
2017-02-24 00:49:07 +08:00
|
|
|
void replace(InputSection *Other);
|
2016-02-26 02:43:51 +08:00
|
|
|
|
2017-12-13 09:39:35 +08:00
|
|
|
static InputSection Discarded;
|
|
|
|
|
2016-11-20 10:39:59 +08:00
|
|
|
private:
|
2017-02-24 00:49:07 +08:00
|
|
|
template <class ELFT, class RelTy>
|
2016-11-20 10:39:59 +08:00
|
|
|
void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
|
2017-05-29 16:37:50 +08:00
|
|
|
|
2017-06-09 11:19:08 +08:00
|
|
|
template <class ELFT> void copyShtGroup(uint8_t *Buf);
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
2017-02-27 10:32:08 +08:00
|
|
|
// The list of all input sections.
|
|
|
|
extern std::vector<InputSectionBase *> InputSections;
|
2016-02-28 08:25:54 +08:00
|
|
|
} // namespace elf
|
2017-01-06 18:04:08 +08:00
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
std::string toString(const elf::InputSectionBase *);
|
2015-07-25 05:03:07 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|