2015-09-22 08:01:39 +08:00
|
|
|
//===- InputSection.h -------------------------------------------*- C++ -*-===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
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
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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;
|
2019-06-08 01:57:58 +08:00
|
|
|
struct Partition;
|
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
|
|
|
|
2019-06-08 01:57:58 +08:00
|
|
|
extern std::vector<Partition> partitions;
|
|
|
|
|
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
|
|
|
|
2019-09-24 19:48:46 +08:00
|
|
|
// The next two bit fields are only used by InputSectionBase, but we
|
2017-03-09 06:36:28 +08:00
|
|
|
// put them here so the struct packs better.
|
|
|
|
|
2017-11-06 12:33:58 +08:00
|
|
|
unsigned bss : 1;
|
|
|
|
|
2018-05-15 16:57:21 +08:00
|
|
|
// Set for sections that should not be folded by ICF.
|
|
|
|
unsigned keepUnique : 1;
|
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
// The 1-indexed partition that this section is assigned to by the garbage
|
|
|
|
// collector, or 0 if this section is dead. Normally there is only one
|
|
|
|
// partition, so this will either be 0 or 1.
|
|
|
|
uint8_t partition;
|
2019-06-08 01:57:58 +08:00
|
|
|
elf::Partition &getPartition() const;
|
2019-05-29 11:55:20 +08:00
|
|
|
|
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
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
bool isLive() const { return partition != 0; }
|
|
|
|
void markLive() { partition = 1; }
|
|
|
|
void markDead() { partition = 0; }
|
|
|
|
|
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)
|
2019-09-24 19:48:46 +08:00
|
|
|
: name(name), repl(this), sectionKind(sectionKind), bss(false),
|
|
|
|
keepUnique(false), partition(0), 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; }
|
|
|
|
|
2019-03-08 02:48:12 +08:00
|
|
|
// Relocations that refer to this section.
|
|
|
|
unsigned numRelocations : 31;
|
|
|
|
unsigned areRelocsRela : 1;
|
|
|
|
const void *firstRelocation = nullptr;
|
|
|
|
|
2018-07-18 07:16:02 +08:00
|
|
|
// The file which contains this section. Its dynamic type is always
|
2017-10-10 11:22:29 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
ArrayRef<uint8_t> data() const {
|
2019-03-13 04:32:30 +08:00
|
|
|
if (uncompressedSize >= 0)
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
uncompress();
|
|
|
|
return rawData;
|
|
|
|
}
|
|
|
|
|
2017-10-10 11:22:29 +08:00
|
|
|
uint64_t getOffsetInFile() const;
|
|
|
|
|
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
|
|
|
|
2019-11-19 13:56:58 +08:00
|
|
|
// The next member in the section group if this section is in a group. This is
|
|
|
|
// used by --gc-sections.
|
|
|
|
InputSectionBase *nextInSectionGroup = nullptr;
|
|
|
|
|
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
|
|
|
|
2018-07-18 07:16:02 +08:00
|
|
|
// Get the function symbol that encloses this offset from within the
|
|
|
|
// section.
|
|
|
|
template <class ELFT>
|
|
|
|
Defined *getEnclosingFunction(uint64_t offset);
|
|
|
|
|
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;
|
|
|
|
|
2018-07-18 07:16:02 +08:00
|
|
|
// A function compiled with -fsplit-stack calling a function
|
|
|
|
// compiled without -fsplit-stack needs its prologue adjusted. Find
|
|
|
|
// such functions and adjust their prologues. This is very similar
|
|
|
|
// to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
|
|
|
|
// information.
|
|
|
|
template <typename ELFT>
|
|
|
|
void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
|
|
|
|
|
|
|
|
|
2017-02-23 10:32:18 +08:00
|
|
|
template <typename T> llvm::ArrayRef<T> getDataAs() const {
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
size_t s = data().size();
|
2017-02-23 10:32:18 +08:00
|
|
|
assert(s % sizeof(T) == 0);
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
|
2017-02-23 10:32:18 +08:00
|
|
|
}
|
2017-08-17 08:27:55 +08:00
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
protected:
|
|
|
|
void parseCompressedHeader();
|
|
|
|
void uncompress() const;
|
|
|
|
|
|
|
|
mutable ArrayRef<uint8_t> rawData;
|
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// This field stores the uncompressed size of the compressed data in rawData,
|
|
|
|
// or -1 if rawData is not compressed (either because the section wasn't
|
2019-03-13 04:32:30 +08:00
|
|
|
// compressed in the first place, or because we ended up uncompressing it).
|
|
|
|
// Since the feature is not used often, this is usually -1.
|
|
|
|
mutable int64_t uncompressedSize = -1;
|
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)
|
2019-04-18 15:46:09 +08:00
|
|
|
: inputOff(off), live(live || !config->gcSections), hash(hash >> 1) {}
|
2017-10-22 07:20:13 +08:00
|
|
|
|
|
|
|
uint32_t inputOff;
|
2019-04-18 15:46:09 +08:00
|
|
|
uint32_t live : 1;
|
|
|
|
uint32_t hash : 31;
|
|
|
|
uint64_t outputOff = 0;
|
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;
|
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 =
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
(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) {}
|
|
|
|
|
Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.
In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.
This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.
If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.
This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.
Differential Revision: https://reviews.llvm.org/D52917
llvm-svn: 343979
2018-10-09 00:58:59 +08:00
|
|
|
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);
|
|
|
|
|
2018-05-23 09:58:43 +08:00
|
|
|
InputSectionBase *getRelocatedSection() const;
|
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 eqClass[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;
|
2018-08-10 01:59:56 +08:00
|
|
|
|
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
|