2015-09-22 00:01:39 +00:00
|
|
|
//===- InputSection.h -------------------------------------------*- C++ -*-===//
|
2015-07-24 21:03:07 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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-24 21:03:07 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-22 00:01:39 +00:00
|
|
|
#ifndef LLD_ELF_INPUT_SECTION_H
|
|
|
|
#define LLD_ELF_INPUT_SECTION_H
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2024-01-22 09:09:46 -08:00
|
|
|
#include "Config.h"
|
2016-05-24 20:24:43 +00:00
|
|
|
#include "Relocations.h"
|
2022-07-07 10:16:09 -07:00
|
|
|
#include "lld/Common/CommonLinkerContext.h"
|
2017-10-02 21:00:41 +00:00
|
|
|
#include "lld/Common/LLVM.h"
|
2022-07-07 10:16:09 -07:00
|
|
|
#include "lld/Common/Memory.h"
|
2016-12-19 03:14:16 +00:00
|
|
|
#include "llvm/ADT/CachedHashString.h"
|
2016-05-23 16:55:43 +00:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2023-06-17 13:18:23 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-02-23 03:34:37 +00:00
|
|
|
#include "llvm/ADT/TinyPtrVector.h"
|
2015-07-24 21:03:07 +00:00
|
|
|
#include "llvm/Object/ELF.h"
|
2022-10-02 13:23:52 -07:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2015-07-24 21:03:07 +00:00
|
|
|
|
|
|
|
namespace lld {
|
2016-02-28 00:25:54 +00:00
|
|
|
namespace elf {
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2022-02-07 21:53:34 -08:00
|
|
|
class InputFile;
|
2017-11-03 21:21:47 +00:00
|
|
|
class Symbol;
|
2016-04-15 14:41:56 +00:00
|
|
|
|
2017-11-06 04:35:31 +00:00
|
|
|
class Defined;
|
2019-06-07 17:57:58 +00:00
|
|
|
struct Partition;
|
2017-03-06 21:17:18 +00:00
|
|
|
class SyntheticSection;
|
2017-07-26 22:13:32 +00:00
|
|
|
template <class ELFT> class ObjFile;
|
2017-02-24 15:07:30 +00:00
|
|
|
class OutputSection;
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2025-01-03 09:38:04 -08:00
|
|
|
// Returned by InputSectionBase::relsOrRelas. At least two members are empty.
|
2021-10-27 09:51:06 -07:00
|
|
|
template <class ELFT> struct RelsOrRelas {
|
2024-07-27 10:55:17 -07:00
|
|
|
Relocs<typename ELFT::Rel> rels;
|
|
|
|
Relocs<typename ELFT::Rela> relas;
|
2024-08-01 10:22:03 -07:00
|
|
|
Relocs<typename ELFT::Crel> crels;
|
2021-10-27 09:51:06 -07:00
|
|
|
bool areRelocsRel() const { return rels.size(); }
|
2024-08-01 10:22:03 -07:00
|
|
|
bool areRelocsCrel() const { return crels.size(); }
|
2021-10-27 09:51:06 -07:00
|
|
|
};
|
|
|
|
|
2024-07-27 10:55:17 -07:00
|
|
|
#define invokeOnRelocs(sec, f, ...) \
|
|
|
|
{ \
|
|
|
|
const RelsOrRelas<ELFT> rs = (sec).template relsOrRelas<ELFT>(); \
|
2024-08-01 10:22:03 -07:00
|
|
|
if (rs.areRelocsCrel()) \
|
|
|
|
f(__VA_ARGS__, rs.crels); \
|
|
|
|
else if (rs.areRelocsRel()) \
|
2024-07-27 10:55:17 -07:00
|
|
|
f(__VA_ARGS__, rs.rels); \
|
|
|
|
else \
|
|
|
|
f(__VA_ARGS__, rs.relas); \
|
|
|
|
}
|
|
|
|
|
2017-03-08 22:36:28 +00: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 09:55:57 +00:00
|
|
|
public:
|
2024-11-23 16:34:21 -08:00
|
|
|
enum Kind : uint8_t {
|
|
|
|
Regular,
|
|
|
|
Synthetic,
|
|
|
|
Spill,
|
|
|
|
EHFrame,
|
|
|
|
Merge,
|
|
|
|
Output,
|
|
|
|
Class,
|
|
|
|
};
|
2018-05-15 08:57:21 +00:00
|
|
|
|
2024-11-23 16:34:21 -08:00
|
|
|
Kind kind() const { return sectionKind; }
|
2024-10-10 22:15:10 -07:00
|
|
|
|
|
|
|
// The file which contains this section. For InputSectionBase, its dynamic
|
|
|
|
// type is usually ObjFile<ELFT>, but may be an InputFile of InternalKind
|
|
|
|
// (for a synthetic section).
|
|
|
|
InputFile *file;
|
|
|
|
|
2022-11-20 21:23:30 +00:00
|
|
|
StringRef name;
|
|
|
|
|
2019-05-29 03:55:20 +00: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.
|
2024-10-15 22:58:07 -07:00
|
|
|
elf::Partition &getPartition(Ctx &) const;
|
2019-05-29 03:55:20 +00:00
|
|
|
|
2016-10-26 00:54:03 +00:00
|
|
|
// These corresponds to the fields in Elf_Shdr.
|
2017-02-23 02:28:28 +00:00
|
|
|
uint64_t flags;
|
2024-11-23 16:34:21 -08:00
|
|
|
uint32_t type;
|
2016-10-26 00:54:03 +00:00
|
|
|
uint32_t link;
|
|
|
|
uint32_t info;
|
2024-11-23 14:22:24 -08:00
|
|
|
uint32_t addralign;
|
|
|
|
uint32_t entsize;
|
2016-10-26 00:54:03 +00:00
|
|
|
|
2024-11-23 16:34:21 -08:00
|
|
|
Kind sectionKind;
|
|
|
|
uint8_t partition = 1;
|
|
|
|
|
|
|
|
// The next two bit fields are only used by InputSectionBase, but we
|
|
|
|
// put them here so the struct packs better.
|
|
|
|
|
2024-10-10 22:15:10 -07:00
|
|
|
Ctx &getCtx() const;
|
2017-03-08 22:36:28 +00: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-13 16:07:27 +00:00
|
|
|
uint64_t getVA(uint64_t offset = 0) const;
|
2018-03-24 00:35:11 +00:00
|
|
|
|
2019-05-29 03:55:20 +00:00
|
|
|
bool isLive() const { return partition != 0; }
|
|
|
|
void markLive() { partition = 1; }
|
|
|
|
void markDead() { partition = 0; }
|
|
|
|
|
2017-03-08 22:36:28 +00:00
|
|
|
protected:
|
2024-10-10 22:15:10 -07:00
|
|
|
constexpr SectionBase(Kind sectionKind, InputFile *file, StringRef name,
|
2024-11-23 14:22:24 -08:00
|
|
|
uint32_t type, uint64_t flags, uint32_t link,
|
|
|
|
uint32_t info, uint32_t addralign, uint32_t entsize)
|
2024-11-23 16:34:21 -08:00
|
|
|
: file(file), name(name), flags(flags), type(type), link(link),
|
|
|
|
info(info), addralign(addralign), entsize(entsize),
|
|
|
|
sectionKind(sectionKind) {}
|
2017-03-08 22:36:28 +00:00
|
|
|
};
|
|
|
|
|
2024-02-06 09:09:13 +08:00
|
|
|
struct SymbolAnchor {
|
|
|
|
uint64_t offset;
|
|
|
|
Defined *d;
|
|
|
|
bool end; // true for the anchor of st_value+st_size
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RelaxAux {
|
|
|
|
// This records symbol start and end offsets which will be adjusted according
|
|
|
|
// to the nearest relocDeltas element.
|
|
|
|
SmallVector<SymbolAnchor, 0> anchors;
|
|
|
|
// For relocations[i], the actual offset is
|
|
|
|
// r_offset - (i ? relocDeltas[i-1] : 0).
|
|
|
|
std::unique_ptr<uint32_t[]> relocDeltas;
|
|
|
|
// For relocations[i], the actual type is relocTypes[i].
|
|
|
|
std::unique_ptr<RelType[]> relocTypes;
|
|
|
|
SmallVector<uint32_t, 0> writes;
|
|
|
|
};
|
2022-07-07 10:16:09 -07:00
|
|
|
|
2017-03-08 22:36:28 +00:00
|
|
|
// This corresponds to a section of an input file.
|
|
|
|
class InputSectionBase : public SectionBase {
|
|
|
|
public:
|
2024-11-29 13:00:36 -08:00
|
|
|
struct ObjMsg {
|
|
|
|
const InputSectionBase *sec;
|
|
|
|
uint64_t offset;
|
|
|
|
};
|
2024-11-29 17:18:22 -08:00
|
|
|
struct SrcMsg {
|
|
|
|
const InputSectionBase &sec;
|
|
|
|
const Symbol &sym;
|
|
|
|
uint64_t offset;
|
|
|
|
};
|
2024-11-29 13:00:36 -08:00
|
|
|
|
2017-02-23 02:28:28 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
|
2016-09-08 14:06:08 +00:00
|
|
|
StringRef name, Kind sectionKind);
|
2017-02-23 02:28:28 +00:00
|
|
|
|
2024-11-23 14:22:24 -08:00
|
|
|
InputSectionBase(InputFile *file, StringRef name, uint32_t type,
|
|
|
|
uint64_t flags, uint32_t link, uint32_t info,
|
|
|
|
uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
|
2016-10-26 00:54:03 +00:00
|
|
|
Kind sectionKind);
|
2017-05-31 20:17:44 +00:00
|
|
|
|
2024-08-05 13:06:45 -07:00
|
|
|
static bool classof(const SectionBase *s) {
|
|
|
|
return s->kind() != Output && s->kind() != Class;
|
|
|
|
}
|
2017-10-10 03:22:29 +00:00
|
|
|
|
2024-11-23 16:34:21 -08:00
|
|
|
LLVM_PREFERRED_TYPE(bool)
|
|
|
|
uint8_t bss : 1;
|
|
|
|
|
|
|
|
// Whether this section is SHT_CREL and has been decoded to RELA by
|
|
|
|
// relsOrRelas.
|
|
|
|
LLVM_PREFERRED_TYPE(bool)
|
|
|
|
uint8_t decodedCrel : 1;
|
|
|
|
|
|
|
|
// Set for sections that should not be folded by ICF.
|
|
|
|
LLVM_PREFERRED_TYPE(bool)
|
|
|
|
uint8_t keepUnique : 1;
|
|
|
|
|
|
|
|
// Whether the section needs to be padded with a NOP filler due to
|
|
|
|
// deleteFallThruJmpInsn.
|
|
|
|
LLVM_PREFERRED_TYPE(bool)
|
|
|
|
uint8_t nopFiller : 1;
|
|
|
|
|
|
|
|
mutable bool compressed = false;
|
|
|
|
|
2022-01-29 16: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-28 19:50:33 -08:00
|
|
|
// Section index of the relocation section if exists.
|
|
|
|
uint32_t relSecIdx = 0;
|
|
|
|
|
2024-01-26 10:43:02 -08:00
|
|
|
// Getter when the dynamic type is ObjFile<ELFT>.
|
2017-10-10 03:22:29 +00:00
|
|
|
template <class ELFT> ObjFile<ELFT> *getFile() const {
|
2024-01-26 10:43:02 -08:00
|
|
|
return cast<ObjFile<ELFT>>(file);
|
2017-10-10 03:22:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 10:16:09 -07: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.
|
2023-05-16 13:35:35 -07:00
|
|
|
uint32_t bytesDropped = 0;
|
2020-04-07 06:48:18 -07:00
|
|
|
|
2021-12-26 22:17:30 -08:00
|
|
|
void drop_back(unsigned num) {
|
|
|
|
assert(bytesDropped + num < 256);
|
|
|
|
bytesDropped += num;
|
|
|
|
}
|
2020-04-07 06:48:18 -07:00
|
|
|
|
|
|
|
void push_back(uint64_t num) {
|
|
|
|
assert(bytesDropped >= num);
|
|
|
|
bytesDropped -= num;
|
|
|
|
}
|
|
|
|
|
2022-11-20 23:22:32 +00:00
|
|
|
mutable const uint8_t *content_;
|
|
|
|
uint64_t size;
|
2022-05-09 20:41:08 -07:00
|
|
|
|
2020-04-07 06:48:18 -07:00
|
|
|
void trim() {
|
|
|
|
if (bytesDropped) {
|
2022-11-20 23:22:32 +00:00
|
|
|
size -= bytesDropped;
|
2020-04-07 06:48:18 -07:00
|
|
|
bytesDropped = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 23:22:32 +00:00
|
|
|
ArrayRef<uint8_t> content() const {
|
|
|
|
return ArrayRef<uint8_t>(content_, size);
|
|
|
|
}
|
2022-11-20 22:43:22 +00:00
|
|
|
ArrayRef<uint8_t> contentMaybeDecompress() const {
|
2022-11-20 21:56:13 +00:00
|
|
|
if (compressed)
|
2022-09-09 10:18:46 -07:00
|
|
|
decompress();
|
2022-11-20 23:22:32 +00:00
|
|
|
return content();
|
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-08 16:58:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 21: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;
|
|
|
|
|
2024-08-01 10:22:03 -07:00
|
|
|
template <class ELFT>
|
|
|
|
RelsOrRelas<ELFT> relsOrRelas(bool supportsCrel = true) const;
|
2016-11-10 14:53:24 +00:00
|
|
|
|
2017-02-17 19:34:05 +00:00
|
|
|
// InputSections that are dependent on us (reverse dependency for GC)
|
2017-10-11 04:01:13 +00:00
|
|
|
llvm::TinyPtrVector<InputSection *> dependentSections;
|
2017-02-17 19:34:05 +00:00
|
|
|
|
2016-11-08 14:47:16 +00:00
|
|
|
// Returns the size of this section (even if this is a common or BSS.)
|
2017-03-08 15:44:30 +00:00
|
|
|
size_t getSize() const;
|
2016-11-08 14:47:16 +00:00
|
|
|
|
2017-05-31 19:09:52 +00:00
|
|
|
InputSection *getLinkOrderDep() const;
|
2015-11-11 16:50:37 +00:00
|
|
|
|
2023-11-03 13:43:28 -07:00
|
|
|
// Get a symbol that encloses this offset from within the section. If type is
|
|
|
|
// not zero, return a symbol with the specified type.
|
2023-11-16 20:24:14 -08:00
|
|
|
Defined *getEnclosingSymbol(uint64_t offset, uint8_t type = 0) const;
|
|
|
|
Defined *getEnclosingFunction(uint64_t offset) const {
|
2023-11-03 13:43:28 -07:00
|
|
|
return getEnclosingSymbol(offset, llvm::ELF::STT_FUNC);
|
|
|
|
}
|
2018-07-17 23:16:02 +00:00
|
|
|
|
2016-11-25 18:51:53 +00:00
|
|
|
// Returns a source location string. Used to construct an error message.
|
2023-11-16 20:24:14 -08:00
|
|
|
std::string getLocation(uint64_t offset) const;
|
2024-11-29 13:00:36 -08:00
|
|
|
ObjMsg getObjMsg(uint64_t offset) const { return {this, offset}; }
|
2024-11-29 17:18:22 -08:00
|
|
|
SrcMsg getSrcMsg(const Symbol &sym, uint64_t offset) const {
|
|
|
|
return {*this, sym, offset};
|
|
|
|
}
|
2016-11-25 18:51:53 +00:00
|
|
|
|
2017-10-10 03:40:57 +00: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.
|
2024-10-11 20:39:53 -07:00
|
|
|
template <class ELFT> void relocate(Ctx &, uint8_t *buf, uint8_t *bufEnd);
|
2024-10-06 16:34:09 -07:00
|
|
|
uint64_t getRelocTargetVA(Ctx &, const Relocation &r, uint64_t p) const;
|
2017-02-23 02:32:18 +00:00
|
|
|
|
2017-10-10 03:40:57 +00: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-09 09:55:09 -08:00
|
|
|
SmallVector<Relocation, 0> relocations;
|
2020-04-07 06:48:18 -07:00
|
|
|
|
2022-11-21 04:12:03 +00:00
|
|
|
void addReloc(const Relocation &r) { relocations.push_back(r); }
|
|
|
|
MutableArrayRef<Relocation> relocs() { return relocations; }
|
|
|
|
ArrayRef<Relocation> relocs() const { return relocations; }
|
|
|
|
|
2022-07-07 10:16:09 -07: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;
|
|
|
|
|
2024-02-06 09:09:13 +08:00
|
|
|
// Auxiliary information for RISC-V and LoongArch linker relaxation.
|
|
|
|
// They do not use jumpInstrMod.
|
|
|
|
RelaxAux *relaxAux;
|
2022-11-20 23:22:32 +00:00
|
|
|
|
|
|
|
// The compressed content size when `compressed` is true.
|
|
|
|
size_t compressedSize;
|
2022-07-07 10:16:09 -07:00
|
|
|
};
|
2020-04-07 06:48:18 -07:00
|
|
|
|
2018-07-17 23:16:02 +00: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>
|
2024-10-11 20:39:53 -07:00
|
|
|
void adjustSplitStackFunctionPrologues(Ctx &, uint8_t *buf, uint8_t *end);
|
2018-07-17 23:16:02 +00:00
|
|
|
|
2017-02-23 02:32:18 +00:00
|
|
|
template <typename T> llvm::ArrayRef<T> getDataAs() const {
|
2022-11-20 22:43:22 +00:00
|
|
|
size_t s = content().size();
|
2017-02-23 02:32:18 +00:00
|
|
|
assert(s % sizeof(T) == 0);
|
2023-01-09 18:11:07 +01:00
|
|
|
return llvm::ArrayRef<T>((const T *)content().data(), s / sizeof(T));
|
2017-02-23 02:32:18 +00:00
|
|
|
}
|
2017-08-17 00:27:55 +00: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-08 16:58:59 +00:00
|
|
|
protected:
|
2024-11-14 22:50:53 -08:00
|
|
|
template <typename ELFT> void parseCompressedHeader(Ctx &);
|
2022-09-09 10:18:46 -07:00
|
|
|
void decompress() const;
|
2015-10-19 21:00:02 +00:00
|
|
|
};
|
|
|
|
|
2016-05-22 00:13:04 +00:00
|
|
|
// SectionPiece represents a piece of splittable section contents.
|
2016-10-20 10:55:58 +00: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-24 21:44:43 +00:00
|
|
|
// be found by looking at the next one).
|
2016-05-22 00:13:04 +00:00
|
|
|
struct SectionPiece {
|
2022-01-30 00:10:52 -08:00
|
|
|
SectionPiece() = default;
|
2017-10-21 23:20:13 +00:00
|
|
|
SectionPiece(size_t off, uint32_t hash, bool live)
|
2021-12-16 21:17:02 -08:00
|
|
|
: inputOff(off), live(live), hash(hash >> 1) {}
|
2017-10-21 23:20:13 +00:00
|
|
|
|
|
|
|
uint32_t inputOff;
|
2024-07-05 13:48:08 +01:00
|
|
|
LLVM_PREFERRED_TYPE(bool)
|
2019-04-18 07:46:09 +00:00
|
|
|
uint32_t live : 1;
|
|
|
|
uint32_t hash : 31;
|
|
|
|
uint64_t outputOff = 0;
|
2016-05-22 00:13:04 +00:00
|
|
|
};
|
2017-10-21 23:20:13 +00:00
|
|
|
|
|
|
|
static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
|
2016-05-22 00:13:04 +00:00
|
|
|
|
2015-10-19 21:00:02 +00:00
|
|
|
// This corresponds to a SHF_MERGE section of an input file.
|
2017-03-06 20:23:56 +00:00
|
|
|
class MergeInputSection : public InputSectionBase {
|
2015-10-19 21:00:02 +00:00
|
|
|
public:
|
2017-03-06 20:23:56 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
|
2016-09-08 14:06:08 +00:00
|
|
|
StringRef name);
|
2024-11-23 14:22:24 -08:00
|
|
|
MergeInputSection(Ctx &, StringRef name, uint32_t type, uint64_t flags,
|
|
|
|
uint64_t entsize, ArrayRef<uint8_t> data);
|
2017-12-21 01:21:59 +00:00
|
|
|
|
2017-10-01 23:46:31 +00:00
|
|
|
static bool classof(const SectionBase *s) { return s->kind() == Merge; }
|
2016-05-23 16:55:43 +00:00
|
|
|
void splitIntoPieces();
|
|
|
|
|
2018-04-19 16:05:07 +00:00
|
|
|
// Translate an offset in the input section to an offset in the parent
|
|
|
|
// MergeSyntheticSection.
|
|
|
|
uint64_t getParentOffset(uint64_t offset) const;
|
2016-05-23 16:55:43 +00:00
|
|
|
|
2016-07-21 13:32:37 +00:00
|
|
|
// Splittable sections are handled as a sequence of data
|
|
|
|
// rather than a single large blob of data.
|
2021-12-16 21:07:39 -08:00
|
|
|
SmallVector<SectionPiece, 0> pieces;
|
2016-12-06 02:19:30 +00: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-21 23:20:13 +00:00
|
|
|
size_t end =
|
2022-11-20 22:43:22 +00:00
|
|
|
(pieces.size() - 1 == i) ? content().size() : pieces[i + 1].inputOff;
|
|
|
|
return {toStringRef(content().slice(begin, end - begin)), pieces[i].hash};
|
2016-12-06 02:19:30 +00:00
|
|
|
}
|
2016-07-21 13:32:37 +00:00
|
|
|
|
|
|
|
// Returns the SectionPiece at a given input section offset.
|
2022-03-28 10:02:35 -07:00
|
|
|
SectionPiece &getSectionPiece(uint64_t offset);
|
|
|
|
const SectionPiece &getSectionPiece(uint64_t offset) const {
|
2018-03-28 03:14:11 +00:00
|
|
|
return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
|
|
|
|
}
|
2016-07-21 13:32:37 +00:00
|
|
|
|
2022-07-30 17:42:08 -07:00
|
|
|
SyntheticSection *getParent() const {
|
|
|
|
return cast_or_null<SyntheticSection>(parent);
|
|
|
|
}
|
2017-02-03 13:06:18 +00:00
|
|
|
|
2016-05-23 16:55:43 +00:00
|
|
|
private:
|
2022-01-30 17:15:45 -08:00
|
|
|
void splitStrings(StringRef s, size_t size);
|
2016-11-26 15:15:11 +00:00
|
|
|
void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
|
2015-10-19 21:00:02 +00:00
|
|
|
};
|
|
|
|
|
2017-09-18 23:07:21 +00:00
|
|
|
struct EhSectionPiece {
|
2017-09-20 08:03:18 +00:00
|
|
|
EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
|
|
|
|
unsigned firstRelocation)
|
|
|
|
: inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
|
|
|
|
|
2020-08-04 16:05:14 -07:00
|
|
|
ArrayRef<uint8_t> data() const {
|
2022-11-20 22:43:22 +00:00
|
|
|
return {sec->content().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-08 16:58:59 +00:00
|
|
|
}
|
2017-09-18 23:07:21 +00:00
|
|
|
|
2017-09-20 08:03:18 +00:00
|
|
|
size_t inputOff;
|
|
|
|
ssize_t outputOff = -1;
|
|
|
|
InputSectionBase *sec;
|
2017-09-18 23:07:21 +00:00
|
|
|
uint32_t size;
|
2017-09-20 08:03:18 +00:00
|
|
|
unsigned firstRelocation;
|
2016-07-21 20:18:30 +00:00
|
|
|
};
|
|
|
|
|
2015-11-11 19:54:14 +00:00
|
|
|
// This corresponds to a .eh_frame section of an input file.
|
2017-03-06 21:17:18 +00:00
|
|
|
class EhInputSection : public InputSectionBase {
|
2015-11-11 19:54:14 +00:00
|
|
|
public:
|
2017-03-06 21:17:18 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
|
2017-03-06 21:17:18 +00:00
|
|
|
StringRef name);
|
2017-10-01 23:46:31 +00:00
|
|
|
static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
|
2017-03-06 21:17:18 +00:00
|
|
|
template <class ELFT> void split();
|
|
|
|
template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
|
2015-11-11 19:54:14 +00:00
|
|
|
|
2016-07-21 13:32:37 +00:00
|
|
|
// Splittable sections are handled as a sequence of data
|
|
|
|
// rather than a single large blob of data.
|
2022-07-31 16:16:10 -07:00
|
|
|
SmallVector<EhSectionPiece, 0> cies, fdes;
|
2017-05-31 20:17:44 +00:00
|
|
|
|
|
|
|
SyntheticSection *getParent() const;
|
2022-03-29 09:51:41 -07:00
|
|
|
uint64_t getParentOffset(uint64_t offset) const;
|
2015-11-11 19:54:14 +00:00
|
|
|
};
|
|
|
|
|
2017-02-24 13:06:59 +00: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-23 16:49:07 +00:00
|
|
|
class InputSection : public InputSectionBase {
|
2015-10-19 21:00:02 +00:00
|
|
|
public:
|
2024-11-23 14:22:24 -08:00
|
|
|
InputSection(InputFile *f, StringRef name, uint32_t type, uint64_t flags,
|
|
|
|
uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
|
|
|
|
Kind k = Regular);
|
2017-02-23 16:49:07 +00:00
|
|
|
template <class ELFT>
|
2017-12-21 02:03:39 +00:00
|
|
|
InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
|
2017-02-23 16:49:07 +00:00
|
|
|
StringRef name);
|
2015-10-19 21:00:02 +00:00
|
|
|
|
2022-02-28 00:16:45 -08:00
|
|
|
static bool classof(const SectionBase *s) {
|
|
|
|
return s->kind() == SectionBase::Regular ||
|
Reland: [LLD] Implement --enable-non-contiguous-regions (#90007)
When enabled, input sections that would otherwise overflow a memory
region are instead spilled to the next matching output section.
This feature parallels the one in GNU LD, but there are some differences
from its documented behavior:
- /DISCARD/ only matches previously-unmatched sections (i.e., the flag
does not affect it).
- If a section fails to fit at any of its matches, the link fails
instead of discarding the section.
- The flag --enable-non-contiguous-regions-warnings is not implemented,
as it exists to warn about such occurrences.
The implementation places stubs at possible spill locations, and
replaces them with the original input section when effecting spills.
Spilling decisions occur after address assignment. Sections are spilled
in reverse order of assignment, with each spill naively decreasing the
size of the affected memory regions. This continues until the memory
regions are brought back under size. Spilling anything causes another
pass of address assignment, and this continues to fixed point.
Spilling after rather than during assignment allows the algorithm to
consider the size effects of unspillable input sections that appear
later in the assignment. Otherwise, such sections (e.g. thunks) may
force an overflow, even if spilling something earlier could have avoided
it.
A few notable feature interactions occur:
- Stubs affect alignment, ONLY_IF_RO, etc, broadly as if a copy of the
input section were actually placed there.
- SHF_MERGE synthetic sections use the spill list of their first
contained input section (the one that gives the section its name).
- ICF occurs oblivious to spill sections; spill lists for merged-away
sections become inert and are removed after assignment.
- SHF_LINK_ORDER and .ARM.exidx are ordered according to the final
section ordering, after all spilling has completed.
- INSERT BEFORE/AFTER and OVERWRITE_SECTIONS are explicitly disallowed.
2024-05-13 12:30:50 -05:00
|
|
|
s->kind() == SectionBase::Synthetic ||
|
|
|
|
s->kind() == SectionBase::Spill;
|
2022-02-28 00:16:45 -08:00
|
|
|
}
|
|
|
|
|
2015-10-19 21:00:02 +00:00
|
|
|
// Write this section to a mmap'ed file, assuming Buf is pointing to
|
|
|
|
// beginning of the output section.
|
2024-10-11 20:39:53 -07:00
|
|
|
template <class ELFT> void writeTo(Ctx &, uint8_t *buf);
|
2015-10-19 21:00:02 +00:00
|
|
|
|
2022-03-08 11:26:12 -08:00
|
|
|
OutputSection *getParent() const {
|
|
|
|
return reinterpret_cast<OutputSection *>(parent);
|
|
|
|
}
|
2017-05-31 20:17:44 +00: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 11:51:13 +00: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-12 17:37:01 +00:00
|
|
|
uint64_t outSecOff = 0;
|
|
|
|
|
2018-05-23 01:58:43 +00:00
|
|
|
InputSectionBase *getRelocatedSection() const;
|
2016-02-25 08:23:37 +00:00
|
|
|
|
2017-02-23 16:49:07 +00:00
|
|
|
template <class ELFT, class RelTy>
|
2024-10-11 20:39:53 -07:00
|
|
|
void relocateNonAlloc(Ctx &, uint8_t *buf, Relocs<RelTy> rels);
|
2016-04-28 18:42:04 +00:00
|
|
|
|
2021-12-24 12: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 02:39:59 +00:00
|
|
|
// Used by ICF.
|
2016-12-05 18:11:35 +00:00
|
|
|
uint32_t eqClass[2] = {0, 0};
|
2016-02-25 18:43:51 +00:00
|
|
|
|
|
|
|
// Called by ICF to merge two input sections.
|
2017-02-23 16:49:07 +00:00
|
|
|
void replace(InputSection *other);
|
2016-02-25 18:43:51 +00:00
|
|
|
|
2017-12-13 01:39:35 +00:00
|
|
|
static InputSection discarded;
|
|
|
|
|
2016-11-20 02:39:59 +00:00
|
|
|
private:
|
2024-10-11 20:39:53 -07:00
|
|
|
template <class ELFT, class RelTy> void copyRelocations(Ctx &, uint8_t *buf);
|
2023-09-09 10:24:16 +02:00
|
|
|
|
|
|
|
template <class ELFT, class RelTy, class RelIt>
|
2024-10-11 20:39:53 -07:00
|
|
|
void copyRelocations(Ctx &, uint8_t *buf, llvm::iterator_range<RelIt> rels);
|
2017-05-29 08:37:50 +00:00
|
|
|
|
2017-06-09 03:19:08 +00:00
|
|
|
template <class ELFT> void copyShtGroup(uint8_t *buf);
|
2015-07-24 21:03:07 +00:00
|
|
|
};
|
|
|
|
|
Reland: [LLD] Implement --enable-non-contiguous-regions (#90007)
When enabled, input sections that would otherwise overflow a memory
region are instead spilled to the next matching output section.
This feature parallels the one in GNU LD, but there are some differences
from its documented behavior:
- /DISCARD/ only matches previously-unmatched sections (i.e., the flag
does not affect it).
- If a section fails to fit at any of its matches, the link fails
instead of discarding the section.
- The flag --enable-non-contiguous-regions-warnings is not implemented,
as it exists to warn about such occurrences.
The implementation places stubs at possible spill locations, and
replaces them with the original input section when effecting spills.
Spilling decisions occur after address assignment. Sections are spilled
in reverse order of assignment, with each spill naively decreasing the
size of the affected memory regions. This continues until the memory
regions are brought back under size. Spilling anything causes another
pass of address assignment, and this continues to fixed point.
Spilling after rather than during assignment allows the algorithm to
consider the size effects of unspillable input sections that appear
later in the assignment. Otherwise, such sections (e.g. thunks) may
force an overflow, even if spilling something earlier could have avoided
it.
A few notable feature interactions occur:
- Stubs affect alignment, ONLY_IF_RO, etc, broadly as if a copy of the
input section were actually placed there.
- SHF_MERGE synthetic sections use the spill list of their first
contained input section (the one that gives the section its name).
- ICF occurs oblivious to spill sections; spill lists for merged-away
sections become inert and are removed after assignment.
- SHF_LINK_ORDER and .ARM.exidx are ordered according to the final
section ordering, after all spilling has completed.
- INSERT BEFORE/AFTER and OVERWRITE_SECTIONS are explicitly disallowed.
2024-05-13 12:30:50 -05:00
|
|
|
// A marker for a potential spill location for another input section. This
|
|
|
|
// broadly acts as if it were the original section until address assignment.
|
|
|
|
// Then it is either replaced with the real input section or removed.
|
|
|
|
class PotentialSpillSection : public InputSection {
|
|
|
|
public:
|
|
|
|
// The containing input section description; used to quickly replace this stub
|
|
|
|
// with the actual section.
|
|
|
|
InputSectionDescription *isd;
|
|
|
|
|
|
|
|
// Next potential spill location for the same source input section.
|
|
|
|
PotentialSpillSection *next = nullptr;
|
|
|
|
|
|
|
|
PotentialSpillSection(const InputSectionBase &source,
|
|
|
|
InputSectionDescription &isd);
|
|
|
|
|
|
|
|
static bool classof(const SectionBase *sec) {
|
|
|
|
return sec->kind() == InputSectionBase::Spill;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-11-23 21:42:22 -08:00
|
|
|
#ifndef _WIN32
|
2024-11-23 16:34:21 -08:00
|
|
|
static_assert(sizeof(InputSection) <= 152, "InputSection is too big");
|
2024-11-23 21:42:22 -08:00
|
|
|
#endif
|
2020-11-09 09:55:09 -08:00
|
|
|
|
2022-07-30 17:42:08 -07:00
|
|
|
class SyntheticSection : public InputSection {
|
|
|
|
public:
|
2024-10-10 23:28:25 -07:00
|
|
|
Ctx &ctx;
|
2024-11-23 15:16:35 -08:00
|
|
|
SyntheticSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags,
|
|
|
|
uint32_t addralign)
|
2024-11-23 14:22:24 -08:00
|
|
|
: InputSection(ctx.internalFile, name, type, flags, addralign,
|
|
|
|
/*entsize=*/0, {}, InputSectionBase::Synthetic),
|
2024-10-10 23:28:25 -07:00
|
|
|
ctx(ctx) {}
|
2022-07-30 17:42:08 -07:00
|
|
|
|
|
|
|
virtual ~SyntheticSection() = default;
|
2024-10-10 23:43:21 -07:00
|
|
|
virtual size_t getSize() const = 0;
|
2024-10-06 17:23:16 -07:00
|
|
|
virtual bool updateAllocSize(Ctx &) { return false; }
|
2022-07-30 17:42:08 -07:00
|
|
|
// If the section has the SHF_ALLOC flag and the size may be changed if
|
|
|
|
// thunks are added, update the section size.
|
2024-10-10 23:43:21 -07:00
|
|
|
virtual bool isNeeded() const { return true; }
|
|
|
|
virtual void finalizeContents() {}
|
|
|
|
virtual void writeTo(uint8_t *buf) = 0;
|
2022-07-30 17:42:08 -07:00
|
|
|
|
|
|
|
static bool classof(const SectionBase *sec) {
|
|
|
|
return sec->kind() == InputSectionBase::Synthetic;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-20 09:58:56 -07:00
|
|
|
inline bool isStaticRelSecType(uint32_t type) {
|
2024-08-01 10:22:03 -07:00
|
|
|
return type == llvm::ELF::SHT_RELA || type == llvm::ELF::SHT_CREL ||
|
|
|
|
type == llvm::ELF::SHT_REL;
|
2024-03-20 09:58:56 -07:00
|
|
|
}
|
|
|
|
|
2020-02-12 14:08:42 -08:00
|
|
|
inline bool isDebugSection(const InputSectionBase &sec) {
|
2020-11-12 09:59:43 -08:00
|
|
|
return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
|
2023-06-05 14:36:19 -07:00
|
|
|
sec.name.starts_with(".debug");
|
2020-02-12 14:08:42 -08:00
|
|
|
}
|
2024-11-06 21:17:26 -08:00
|
|
|
|
2024-11-16 11:58:10 -08:00
|
|
|
std::string toStr(elf::Ctx &, const elf::InputSectionBase *);
|
2024-11-06 21:17:26 -08:00
|
|
|
const ELFSyncStream &operator<<(const ELFSyncStream &,
|
|
|
|
const InputSectionBase *);
|
2024-11-29 13:00:36 -08:00
|
|
|
const ELFSyncStream &operator<<(const ELFSyncStream &,
|
|
|
|
InputSectionBase::ObjMsg &&);
|
2024-11-29 17:18:22 -08:00
|
|
|
const ELFSyncStream &operator<<(const ELFSyncStream &,
|
|
|
|
InputSectionBase::SrcMsg &&);
|
2016-02-28 00:25:54 +00:00
|
|
|
} // namespace elf
|
2015-07-24 21:03:07 +00:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|