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
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "Relocations.h"
|
2022-07-08 01:16:09 +08:00
|
|
|
#include "lld/Common/CommonLinkerContext.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2022-07-08 01:16:09 +08:00
|
|
|
#include "lld/Common/Memory.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
|
|
|
|
2022-02-08 13:53:34 +08:00
|
|
|
class InputFile;
|
2017-11-04 05:21:47 +08:00
|
|
|
class Symbol;
|
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-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;
|
|
|
|
|
2021-10-28 00:51:06 +08:00
|
|
|
// Returned by InputSectionBase::relsOrRelas. At least one member is empty.
|
|
|
|
template <class ELFT> struct RelsOrRelas {
|
|
|
|
ArrayRef<typename ELFT::Rel> rels;
|
|
|
|
ArrayRef<typename ELFT::Rela> relas;
|
|
|
|
bool areRelocsRel() const { return rels.size(); }
|
|
|
|
};
|
|
|
|
|
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:
|
2022-02-28 16:24:25 +08:00
|
|
|
enum Kind { Regular, Synthetic, EHFrame, Merge, 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
|
|
|
|
2020-11-10 01:55:09 +08:00
|
|
|
uint8_t 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.
|
|
|
|
|
2020-11-10 01:55:09 +08:00
|
|
|
uint8_t bss : 1;
|
2017-11-06 12:33:58 +08:00
|
|
|
|
2018-05-15 16:57:21 +08:00
|
|
|
// Set for sections that should not be folded by ICF.
|
2020-11-10 01:55:09 +08:00
|
|
|
uint8_t keepUnique : 1;
|
2018-05-15 16:57:21 +08:00
|
|
|
|
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.
|
2022-01-31 07:12:09 +08:00
|
|
|
uint8_t partition = 1;
|
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;
|
2021-11-29 11:50:33 +08:00
|
|
|
uint32_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:
|
2021-11-29 11:50:33 +08:00
|
|
|
constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
|
|
|
|
uint32_t entsize, uint32_t alignment, uint32_t type,
|
|
|
|
uint32_t info, uint32_t link)
|
2021-12-25 04:09:48 +08:00
|
|
|
: name(name), sectionKind(sectionKind), bss(false), keepUnique(false),
|
2022-01-31 07:12:09 +08:00
|
|
|
alignment(alignment), flags(flags), entsize(entsize), type(type),
|
|
|
|
link(link), info(info) {}
|
2017-03-09 06:36:28 +08:00
|
|
|
};
|
|
|
|
|
2022-07-08 01:16:09 +08:00
|
|
|
struct RISCVRelaxAux;
|
|
|
|
|
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; }
|
|
|
|
|
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;
|
|
|
|
|
2022-01-30 08:20:40 +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;
|
|
|
|
|
2021-11-29 11:50:33 +08:00
|
|
|
// Section index of the relocation section if exists.
|
|
|
|
uint32_t relSecIdx = 0;
|
|
|
|
|
2017-10-10 11:22:29 +08:00
|
|
|
template <class ELFT> ObjFile<ELFT> *getFile() const {
|
|
|
|
return cast_or_null<ObjFile<ELFT>>(file);
|
|
|
|
}
|
|
|
|
|
2022-07-08 01:16:09 +08:00
|
|
|
// Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
|
|
|
|
// indicate the number of bytes which is not counted in the size. This should
|
|
|
|
// be reset to zero after uses.
|
|
|
|
uint16_t bytesDropped = 0;
|
2020-04-07 21:48:18 +08:00
|
|
|
|
2020-11-10 01:55:09 +08:00
|
|
|
// Whether the section needs to be padded with a NOP filler due to
|
|
|
|
// deleteFallThruJmpInsn.
|
|
|
|
bool nopFiller = false;
|
|
|
|
|
2021-12-27 14:17:30 +08:00
|
|
|
void drop_back(unsigned num) {
|
|
|
|
assert(bytesDropped + num < 256);
|
|
|
|
bytesDropped += num;
|
|
|
|
}
|
2020-04-07 21:48:18 +08:00
|
|
|
|
|
|
|
void push_back(uint64_t num) {
|
|
|
|
assert(bytesDropped >= num);
|
|
|
|
bytesDropped -= num;
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:41:08 +08:00
|
|
|
mutable ArrayRef<uint8_t> rawData;
|
|
|
|
|
2020-04-07 21:48:18 +08:00
|
|
|
void trim() {
|
|
|
|
if (bytesDropped) {
|
|
|
|
rawData = rawData.drop_back(bytesDropped);
|
|
|
|
bytesDropped = 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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2021-10-28 00:51:06 +08:00
|
|
|
template <class ELFT> RelsOrRelas<ELFT> relsOrRelas() const;
|
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.
|
|
|
|
Defined *getEnclosingFunction(uint64_t offset);
|
|
|
|
|
2016-11-26 02:51:53 +08:00
|
|
|
// Returns a source location string. Used to construct an error message.
|
2022-01-19 09:33:58 +08:00
|
|
|
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);
|
2020-04-07 21:48:18 +08:00
|
|
|
static uint64_t getRelocTargetVA(const InputFile *File, RelType Type,
|
|
|
|
int64_t A, uint64_t P, const Symbol &Sym,
|
|
|
|
RelExpr Expr);
|
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.
|
2020-11-10 01:55:09 +08:00
|
|
|
SmallVector<Relocation, 0> relocations;
|
2020-04-07 21:48:18 +08:00
|
|
|
|
2022-07-08 01:16:09 +08:00
|
|
|
union {
|
|
|
|
// These are modifiers to jump instructions that are necessary when basic
|
|
|
|
// block sections are enabled. Basic block sections creates opportunities
|
|
|
|
// to relax jump instructions at basic block boundaries after reordering the
|
|
|
|
// basic blocks.
|
|
|
|
JumpInstrMod *jumpInstrMod = nullptr;
|
|
|
|
|
|
|
|
// Auxiliary information for RISC-V linker relaxation. RISC-V does not use
|
|
|
|
// jumpInstrMod.
|
|
|
|
RISCVRelaxAux *relaxAux;
|
|
|
|
};
|
2020-04-07 21:48:18 +08:00
|
|
|
|
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 {
|
2022-02-21 16:39:26 +08:00
|
|
|
size_t s = rawData.size();
|
2017-02-23 10:32:18 +08:00
|
|
|
assert(s % sizeof(T) == 0);
|
2022-02-21 16:39:26 +08:00
|
|
|
return llvm::makeArrayRef<T>((const T *)rawData.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:
|
2021-08-06 18:29:47 +08:00
|
|
|
template <typename ELFT>
|
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
|
|
|
void parseCompressedHeader();
|
|
|
|
void uncompress() const;
|
|
|
|
|
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 {
|
2022-01-30 16:10:52 +08:00
|
|
|
SectionPiece() = default;
|
2017-10-22 07:20:13 +08:00
|
|
|
SectionPiece(size_t off, uint32_t hash, bool live)
|
2021-12-17 13:17:02 +08:00
|
|
|
: inputOff(off), live(live), 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.
|
2021-12-17 13:07:39 +08:00
|
|
|
SmallVector<SectionPiece, 0> 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 =
|
2022-02-21 16:39:26 +08:00
|
|
|
(pieces.size() - 1 == i) ? rawData.size() : pieces[i + 1].inputOff;
|
|
|
|
return {toStringRef(rawData.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.
|
2022-03-29 01:02:35 +08:00
|
|
|
SectionPiece &getSectionPiece(uint64_t offset);
|
|
|
|
const SectionPiece &getSectionPiece(uint64_t offset) const {
|
2018-03-28 11:14:11 +08:00
|
|
|
return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
|
|
|
|
}
|
2016-07-21 21:32:37 +08:00
|
|
|
|
2022-07-31 08:42:08 +08:00
|
|
|
SyntheticSection *getParent() const {
|
|
|
|
return cast_or_null<SyntheticSection>(parent);
|
|
|
|
}
|
2017-02-03 21:06:18 +08:00
|
|
|
|
2016-05-24 00:55:43 +08:00
|
|
|
private:
|
2022-01-31 09:15:45 +08:00
|
|
|
void splitStrings(StringRef s, size_t size);
|
2016-11-26 23:15:11 +08:00
|
|
|
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) {}
|
|
|
|
|
2020-08-05 07:05:14 +08:00
|
|
|
ArrayRef<uint8_t> data() const {
|
2022-02-21 16:39:26 +08:00
|
|
|
return {sec->rawData.data() + this->inputOff, size};
|
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
|
|
|
}
|
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.
|
2022-08-01 07:16:10 +08:00
|
|
|
SmallVector<EhSectionPiece, 0> cies, fdes;
|
2017-06-01 04:17:44 +08:00
|
|
|
|
|
|
|
SyntheticSection *getParent() const;
|
2022-03-30 00:51:41 +08:00
|
|
|
uint64_t getParentOffset(uint64_t offset) 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
|
|
|
|
2022-02-28 16:16:45 +08:00
|
|
|
static bool classof(const SectionBase *s) {
|
|
|
|
return s->kind() == SectionBase::Regular ||
|
|
|
|
s->kind() == SectionBase::Synthetic;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-03-09 03:26:12 +08:00
|
|
|
OutputSection *getParent() const {
|
|
|
|
return reinterpret_cast<OutputSection *>(parent);
|
|
|
|
}
|
2017-06-01 04:17:44 +08:00
|
|
|
|
[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;
|
|
|
|
|
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);
|
|
|
|
|
2021-12-25 04:09:48 +08:00
|
|
|
// Points to the canonical section. If ICF folds two sections, repl pointer of
|
|
|
|
// one section points to the other.
|
|
|
|
InputSection *repl = this;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-12-27 14:17:30 +08:00
|
|
|
static_assert(sizeof(InputSection) <= 160, "InputSection is too big");
|
2020-11-10 01:55:09 +08:00
|
|
|
|
2022-07-31 08:42:08 +08:00
|
|
|
class SyntheticSection : public InputSection {
|
|
|
|
public:
|
|
|
|
SyntheticSection(uint64_t flags, uint32_t type, uint32_t alignment,
|
|
|
|
StringRef name)
|
|
|
|
: InputSection(nullptr, flags, type, alignment, {}, name,
|
|
|
|
InputSectionBase::Synthetic) {}
|
|
|
|
|
|
|
|
virtual ~SyntheticSection() = default;
|
|
|
|
virtual size_t getSize() const = 0;
|
|
|
|
virtual bool updateAllocSize() { return false; }
|
|
|
|
// If the section has the SHF_ALLOC flag and the size may be changed if
|
|
|
|
// thunks are added, update the section size.
|
|
|
|
virtual bool isNeeded() const { return true; }
|
|
|
|
virtual void finalizeContents() {}
|
|
|
|
virtual void writeTo(uint8_t *buf) = 0;
|
|
|
|
|
|
|
|
static bool classof(const SectionBase *sec) {
|
|
|
|
return sec->kind() == InputSectionBase::Synthetic;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-13 06:08:42 +08:00
|
|
|
inline bool isDebugSection(const InputSectionBase &sec) {
|
2020-11-13 01:59:43 +08:00
|
|
|
return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
|
2022-06-03 04:37:19 +08:00
|
|
|
sec.name.startswith(".debug");
|
2020-02-13 06:08:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-27 10:32:08 +08:00
|
|
|
// The list of all input sections.
|
2021-12-23 14:30:07 +08:00
|
|
|
extern SmallVector<InputSectionBase *, 0> inputSections;
|
2022-08-01 02:58:08 +08:00
|
|
|
extern SmallVector<EhInputSection *, 0> ehInputSections;
|
2018-08-10 01:59:56 +08:00
|
|
|
|
[ELF][PPC64] Suppress toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen
The current implementation assumes that R_PPC64_TOC16_HA is always followed
by R_PPC64_TOC16_LO_DS. This can break with R_PPC64_TOC16_LO:
// Load the address of the TOC entry, instead of the value stored at that address
addis 3, 2, .LC0@tloc@ha # R_PPC64_TOC16_HA
addi 3, 3, .LC0@tloc@l # R_PPC64_TOC16_LO
blr
which is used by boringssl's util/fipstools/delocate/delocate.go
https://github.com/google/boringssl/blob/master/crypto/fipsmodule/FIPS.md has some documentation.
In short, this tool converts an assembly file to avoid any potential relocations.
The distance to an input .toc is not a constant after linking, so it cannot use an `addis;ld` pair.
Instead, it jumps to a stub which loads the TOC entry address with `addis;addi`.
This patch checks the presence of R_PPC64_TOC16_LO and suppresses
toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen.
This approach is conservative and loses some relaxation opportunities but is easy to implement.
addis 3, 2, .LC0@toc@ha # no relaxation
addi 3, 3, .LC0@toc@l # no relaxation
li 9, 0
addis 4, 2, .LC0@toc@ha # can relax but suppressed
ld 4, .LC0@toc@l(4) # can relax but suppressed
Also note that interleaved R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS is
possible and this patch accounts for that.
addis 3, 2, .LC1@toc@ha # can relax
addis 4, 2, .LC2@toc@ha # can relax
ld 3, .LC1@toc@l(3) # can relax
ld 4, .LC2@toc@l(4) # can relax
Reviewed By: #powerpc, sfertile
Differential Revision: https://reviews.llvm.org/D78431
2020-04-18 14:08:11 +08:00
|
|
|
// The set of TOC entries (.toc + addend) for which we should not apply
|
|
|
|
// toc-indirect to toc-relative relaxation. const Symbol * refers to the
|
|
|
|
// STT_SECTION symbol associated to the .toc input section.
|
|
|
|
extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax;
|
|
|
|
|
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
|