Skip to content

Commit

Permalink
[ELF] Use SmallVector for many SyntheticSections. NFC
Browse files Browse the repository at this point in the history
This decreases struct sizes and usually decreases the lld executable
size (39KiB for my x86-64 executable) (unless in some cases smaller
SmallVector leads to more inlining, e.g. StringTableBuilder).
For --gdb-index, there may be memory usage saving.
  • Loading branch information
MaskRay committed Dec 18, 2021
1 parent 212e6c9 commit 552d844
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
15 changes: 8 additions & 7 deletions lld/ELF/SyntheticSections.cpp
Expand Up @@ -2427,10 +2427,10 @@ static uint32_t hashGnu(StringRef name) {
// Add symbols to this symbol hash table. Note that this function
// destructively sort a given vector -- which is needed because
// GNU-style hash table places some sorting requirements.
void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &v) {
void GnuHashTableSection::addSymbols(SmallVectorImpl<SymbolTableEntry> &v) {
// We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
// its type correctly.
std::vector<SymbolTableEntry>::iterator mid =
auto mid =
std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) {
return !s.sym->isDefined() || s.sym->partition != partition;
});
Expand Down Expand Up @@ -2716,16 +2716,17 @@ void GdbIndexSection::initOutputSize() {
}
}

static std::vector<GdbIndexSection::CuEntry> readCuList(DWARFContext &dwarf) {
std::vector<GdbIndexSection::CuEntry> ret;
static SmallVector<GdbIndexSection::CuEntry, 0>
readCuList(DWARFContext &dwarf) {
SmallVector<GdbIndexSection::CuEntry, 0> ret;
for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units())
ret.push_back({cu->getOffset(), cu->getLength() + 4});
return ret;
}

static std::vector<GdbIndexSection::AddressEntry>
static SmallVector<GdbIndexSection::AddressEntry, 0>
readAddressAreas(DWARFContext &dwarf, InputSection *sec) {
std::vector<GdbIndexSection::AddressEntry> ret;
SmallVector<GdbIndexSection::AddressEntry, 0> ret;

uint32_t cuIdx = 0;
for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) {
Expand Down Expand Up @@ -2758,7 +2759,7 @@ readAddressAreas(DWARFContext &dwarf, InputSection *sec) {
template <class ELFT>
static std::vector<GdbIndexSection::NameAttrEntry>
readPubNamesAndTypes(const LLDDwarfObj<ELFT> &obj,
const std::vector<GdbIndexSection::CuEntry> &cus) {
const SmallVectorImpl<GdbIndexSection::CuEntry> &cus) {
const LLDDWARFSection &pubNames = obj.getGnuPubnamesSection();
const LLDDWARFSection &pubTypes = obj.getGnuPubtypesSection();

Expand Down
48 changes: 24 additions & 24 deletions lld/ELF/SyntheticSections.h
Expand Up @@ -61,7 +61,7 @@ class SyntheticSection : public InputSection {

struct CieRecord {
EhSectionPiece *cie = nullptr;
std::vector<EhSectionPiece *> fdes;
SmallVector<EhSectionPiece *, 0> fdes;
};

// Section for .eh_frame.
Expand All @@ -79,7 +79,7 @@ class EhFrameSection final : public SyntheticSection {

void addSection(EhInputSection *sec);

std::vector<EhInputSection *> sections;
SmallVector<EhInputSection *, 0> sections;
size_t numFdes = 0;

struct FdeData {
Expand Down Expand Up @@ -115,7 +115,7 @@ class EhFrameSection final : public SyntheticSection {

uint64_t getFdePc(uint8_t *buf, size_t off, uint8_t enc) const;

std::vector<CieRecord *> cieRecords;
SmallVector<CieRecord *, 0> cieRecords;

// CIE records are uniquified by their contents and personality functions.
llvm::DenseMap<std::pair<ArrayRef<uint8_t>, Symbol *>, CieRecord *> cieMap;
Expand Down Expand Up @@ -387,7 +387,7 @@ class GotPltSection final : public SyntheticSection {
bool hasGotPltOffRel = false;

private:
std::vector<const Symbol *> entries;
SmallVector<const Symbol *, 0> entries;
};

// The IgotPltSection is a Got associated with the PltSection for GNU Ifunc
Expand All @@ -403,7 +403,7 @@ class IgotPltSection final : public SyntheticSection {
bool isNeeded() const override { return !entries.empty(); }

private:
std::vector<const Symbol *> entries;
SmallVector<const Symbol *, 0> entries;
};

class StringTableSection final : public SyntheticSection {
Expand All @@ -420,7 +420,7 @@ class StringTableSection final : public SyntheticSection {
uint64_t size = 0;

llvm::DenseMap<StringRef, unsigned> stringMap;
std::vector<StringRef> strings;
SmallVector<StringRef, 0> strings;
};

class DynamicReloc {
Expand Down Expand Up @@ -540,7 +540,7 @@ class RelocationBaseSection : public SyntheticSection {
d->type == llvm::ELF::SHT_RELR);
}
int32_t dynamicTag, sizeDynamicTag;
std::vector<DynamicReloc> relocs;
SmallVector<DynamicReloc, 0> relocs;

protected:
size_t numRelativeRelocs = 0;
Expand Down Expand Up @@ -588,7 +588,7 @@ class RelrBaseSection : public SyntheticSection {
public:
RelrBaseSection();
bool isNeeded() const override { return !relocs.empty(); }
std::vector<RelativeReloc> relocs;
SmallVector<RelativeReloc, 0> relocs;
};

// RelrSection is used to encode offsets for relative relocations.
Expand All @@ -608,7 +608,7 @@ template <class ELFT> class RelrSection final : public RelrBaseSection {
}

private:
std::vector<Elf_Relr> relrRelocs;
SmallVector<Elf_Relr, 0> relrRelocs;
};

struct SymbolTableEntry {
Expand All @@ -630,7 +630,7 @@ class SymbolTableBaseSection : public SyntheticSection {
void sortSymTabSymbols();

// A vector of symbols and their string table offsets.
std::vector<SymbolTableEntry> symbols;
SmallVector<SymbolTableEntry, 0> symbols;

StringTableSection &strTabSec;

Expand Down Expand Up @@ -669,7 +669,7 @@ class GnuHashTableSection final : public SyntheticSection {

// Adds symbols to the hash table.
// Sorts the input to satisfy GNU hash section requirements.
void addSymbols(std::vector<SymbolTableEntry> &symbols);
void addSymbols(llvm::SmallVectorImpl<SymbolTableEntry> &symbols);

private:
// See the comment in writeBloomFilter.
Expand All @@ -682,7 +682,7 @@ class GnuHashTableSection final : public SyntheticSection {
uint32_t bucketIdx;
};

std::vector<Entry> symbols;
SmallVector<Entry, 0> symbols;
size_t maskWords;
size_t nBuckets = 0;
size_t size = 0;
Expand Down Expand Up @@ -722,15 +722,15 @@ class PltSection : public SyntheticSection {

size_t headerSize;

std::vector<const Symbol *> entries;
SmallVector<const Symbol *, 0> entries;
};

// Used for non-preemptible ifuncs. It does not have a header. Each entry is
// associated with an IRELATIVE relocation, which will be resolved eagerly at
// runtime. PltSection can only contain entries associated with JUMP_SLOT
// relocations, so IPLT entries are in a separate section.
class IpltSection final : public SyntheticSection {
std::vector<const Symbol *> entries;
SmallVector<const Symbol *, 0> entries;

public:
IpltSection();
Expand All @@ -747,7 +747,7 @@ class PPC32GlinkSection : public PltSection {
void writeTo(uint8_t *buf) override;
size_t getSize() const override;

std::vector<const Symbol *> canonical_plts;
SmallVector<const Symbol *, 0> canonical_plts;
static constexpr size_t footerSize = 64;
};

Expand Down Expand Up @@ -780,13 +780,13 @@ class GdbIndexSection final : public SyntheticSection {

struct GdbChunk {
InputSection *sec;
std::vector<AddressEntry> addressAreas;
std::vector<CuEntry> compilationUnits;
SmallVector<AddressEntry, 0> addressAreas;
SmallVector<CuEntry, 0> compilationUnits;
};

struct GdbSymbol {
llvm::CachedHashStringRef name;
std::vector<uint32_t> cuVector;
SmallVector<uint32_t, 0> cuVector;
uint32_t nameOff;
uint32_t cuVectorOff;
};
Expand Down Expand Up @@ -859,7 +859,7 @@ class VersionDefinitionSection final : public SyntheticSection {
StringRef getFileDefName();

unsigned fileDefNameOff;
std::vector<unsigned> verDefNameOffs;
SmallVector<unsigned, 0> verDefNameOffs;
};

// The .gnu.version section specifies the required version of each symbol in the
Expand Down Expand Up @@ -898,7 +898,7 @@ class VersionNeedSection final : public SyntheticSection {
std::vector<Vernaux> vernauxs;
};

std::vector<Verneed> verneeds;
SmallVector<Verneed, 0> verneeds;

public:
VersionNeedSection();
Expand All @@ -915,7 +915,7 @@ class VersionNeedSection final : public SyntheticSection {
class MergeSyntheticSection : public SyntheticSection {
public:
void addSection(MergeInputSection *ms);
std::vector<MergeInputSection *> sections;
SmallVector<MergeInputSection *, 0> sections;

protected:
MergeSyntheticSection(StringRef name, uint32_t type, uint64_t flags,
Expand Down Expand Up @@ -962,7 +962,7 @@ class MergeNoTailSection final : public MergeSyntheticSection {

// String table contents
constexpr static size_t numShards = 32;
std::vector<llvm::StringTableBuilder> shards;
SmallVector<llvm::StringTableBuilder, 0> shards;
size_t shardOffsets[numShards];
};

Expand Down Expand Up @@ -1120,7 +1120,7 @@ class ThunkSection : public SyntheticSection {
bool roundUpSizeForErrata = false;

private:
std::vector<Thunk *> thunks;
SmallVector<Thunk *, 0> thunks;
size_t size = 0;
};

Expand Down Expand Up @@ -1151,7 +1151,7 @@ class PPC64LongBranchTargetSection final : public SyntheticSection {
void finalizeContents() override { finalized = true; }

private:
std::vector<std::pair<const Symbol *, int64_t>> entries;
SmallVector<std::pair<const Symbol *, int64_t>, 0> entries;
llvm::DenseMap<std::pair<const Symbol *, int64_t>, uint32_t> entry_index;
bool finalized = false;
};
Expand Down

0 comments on commit 552d844

Please sign in to comment.