Skip to content

Commit 43b1334

Browse files
authored
[ELF] Add internal InputFile (llvm#78944)
Based on https://reviews.llvm.org/D45375 . Introduce a new InputFile kind `InternalKind`, use it for * `ctx.internalFile`: for linker-defined symbols and some synthesized `Undefined` * `createInternalFile`: for symbol assignments and --defsym I picked "internal" instead of "synthetic" to avoid confusion with SyntheticSection. Currently a symbol's file is one of: nullptr, ObjKind, SharedKind, BitcodeKind, BinaryKind. Now it's non-null (I plan to add an `assert(file)` to Symbol::Symbol and change `toString(const InputFile *)` separately). Debugging and error reporting gets improved. The immediate user-facing difference is more descriptive "File" column in the --cref output. This patch may unlock further simplification. Currently each symbol assignment gets its own `createInternalFile(cmd->location)`. Two symbol assignments in a linker script do not share the same file. Making the file the same would be nice, but would require non trivial code.
1 parent a859df3 commit 43b1334

20 files changed

+75
-45
lines changed

lld/ELF/Arch/ARM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,9 +1381,9 @@ template <typename ELFT> void elf::writeARMCmseImportLib() {
13811381
// Copy the secure gateway entry symbols to the import library symbol table.
13821382
for (auto &p : symtab.cmseSymMap) {
13831383
Defined *d = cast<Defined>(p.second.sym);
1384-
impSymTab->addSymbol(makeDefined(nullptr, d->getName(), d->computeBinding(),
1385-
/*stOther=*/0, STT_FUNC, d->getVA(),
1386-
d->getSize(), nullptr));
1384+
impSymTab->addSymbol(makeDefined(
1385+
ctx.internalFile, d->getName(), d->computeBinding(),
1386+
/*stOther=*/0, STT_FUNC, d->getVA(), d->getSize(), nullptr));
13871387
}
13881388

13891389
size_t idx = 0;

lld/ELF/Arch/Mips.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,11 @@ template <class ELFT> bool elf::isMipsPIC(const Defined *sym) {
771771
if (!sym->section)
772772
return false;
773773

774-
ObjFile<ELFT> *file =
775-
cast<InputSectionBase>(sym->section)->template getFile<ELFT>();
776-
if (!file)
774+
InputFile *file = cast<InputSectionBase>(sym->section)->file;
775+
if (!file || file->isInternal())
777776
return false;
778777

779-
return file->getObj().getHeader().e_flags & EF_MIPS_PIC;
778+
return cast<ObjFile<ELFT>>(file)->getObj().getHeader().e_flags & EF_MIPS_PIC;
780779
}
781780

782781
template <class ELFT> TargetInfo *elf::getMipsTargetInfo() {

lld/ELF/Arch/PPC64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static bool addOptional(StringRef name, uint64_t value,
253253
Symbol *sym = symtab.find(name);
254254
if (!sym || sym->isDefined())
255255
return false;
256-
sym->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN,
256+
sym->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, STV_HIDDEN,
257257
STT_FUNC, value,
258258
/*size=*/0, /*section=*/nullptr});
259259
defined.push_back(cast<Defined>(sym));

lld/ELF/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ struct Ctx {
473473
std::pair<const InputFile *, const InputFile *>>
474474
backwardReferences;
475475
llvm::SmallSet<llvm::StringRef, 0> auxiliaryFiles;
476+
// InputFile for linker created symbols with no source location.
477+
InputFile *internalFile;
476478
// True if SHT_LLVM_SYMPART is used.
477479
std::atomic<bool> hasSympart{false};
478480
// True if there are TLS IE relocations. Set DF_STATIC_TLS if -shared.

lld/ELF/Driver.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void Ctx::reset() {
105105
whyExtractRecords.clear();
106106
backwardReferences.clear();
107107
auxiliaryFiles.clear();
108+
internalFile = nullptr;
108109
hasSympart.store(false, std::memory_order_relaxed);
109110
hasTlsIe.store(false, std::memory_order_relaxed);
110111
needsTlsLd.store(false, std::memory_order_relaxed);
@@ -2337,7 +2338,8 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
23372338

23382339
static Symbol *addUnusedUndefined(StringRef name,
23392340
uint8_t binding = STB_GLOBAL) {
2340-
return symtab.addSymbol(Undefined{nullptr, name, binding, STV_DEFAULT, 0});
2341+
return symtab.addSymbol(
2342+
Undefined{ctx.internalFile, name, binding, STV_DEFAULT, 0});
23412343
}
23422344

23432345
static void markBuffersAsDontNeed(bool skipLinkedOutput) {
@@ -2696,6 +2698,8 @@ void LinkerDriver::link(opt::InputArgList &args) {
26962698
for (auto *arg : args.filtered(OPT_trace_symbol))
26972699
symtab.insert(arg->getValue())->traced = true;
26982700

2701+
ctx.internalFile = createInternalFile("<internal>");
2702+
26992703
// Handle -u/--undefined before input files. If both a.a and b.so define foo,
27002704
// -u foo a.a b.so will extract a.a.
27012705
for (StringRef name : config->undefined)

lld/ELF/InputFiles.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,10 @@ void BinaryFile::parse() {
17851785
nullptr});
17861786
}
17871787

1788+
InputFile *elf::createInternalFile(StringRef name) {
1789+
return make<InputFile>(InputFile::InternalKind, MemoryBufferRef("", name));
1790+
}
1791+
17881792
ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName,
17891793
bool lazy) {
17901794
ELFFileBase *f;

lld/ELF/InputFiles.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,17 @@ class InputFile {
6363
SharedKind,
6464
BitcodeKind,
6565
BinaryKind,
66+
InternalKind,
6667
};
6768

69+
InputFile(Kind k, MemoryBufferRef m);
6870
Kind kind() const { return fileKind; }
6971

7072
bool isElf() const {
7173
Kind k = kind();
7274
return k == ObjKind || k == SharedKind;
7375
}
76+
bool isInternal() const { return kind() == InternalKind; }
7477

7578
StringRef getName() const { return mb.getBufferIdentifier(); }
7679
MemoryBufferRef mb;
@@ -151,9 +154,6 @@ class InputFile {
151154
// R_PPC64_TLSLD. Disable TLS relaxation to avoid bad code generation.
152155
bool ppc64DisableTLSRelax = false;
153156

154-
protected:
155-
InputFile(Kind k, MemoryBufferRef m);
156-
157157
public:
158158
// If not empty, this stores the name of the archive containing this file.
159159
// We use this string for creating error messages.
@@ -380,6 +380,7 @@ class BinaryFile : public InputFile {
380380
void parse();
381381
};
382382

383+
InputFile *createInternalFile(StringRef name);
383384
ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "",
384385
bool lazy = false);
385386

lld/ELF/InputSection.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ InputSection *InputSectionBase::getLinkOrderDep() const {
245245
// Find a symbol that encloses a given location.
246246
Defined *InputSectionBase::getEnclosingSymbol(uint64_t offset,
247247
uint8_t type) const {
248+
if (file->isInternal())
249+
return nullptr;
248250
for (Symbol *b : file->getSymbols())
249251
if (Defined *d = dyn_cast<Defined>(b))
250252
if (d->section == this && d->value <= offset &&
@@ -344,7 +346,7 @@ template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) {
344346
}
345347

346348
InputSectionBase *InputSection::getRelocatedSection() const {
347-
if (!file || (type != SHT_RELA && type != SHT_REL))
349+
if (!file || file->isInternal() || (type != SHT_RELA && type != SHT_REL))
348350
return nullptr;
349351
ArrayRef<InputSectionBase *> sections = file->getSections();
350352
return sections[info];

lld/ELF/InputSection.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLD_ELF_INPUT_SECTION_H
1010
#define LLD_ELF_INPUT_SECTION_H
1111

12+
#include "Config.h"
1213
#include "Relocations.h"
1314
#include "lld/Common/CommonLinkerContext.h"
1415
#include "lld/Common/LLVM.h"
@@ -413,7 +414,7 @@ class SyntheticSection : public InputSection {
413414
public:
414415
SyntheticSection(uint64_t flags, uint32_t type, uint32_t addralign,
415416
StringRef name)
416-
: InputSection(nullptr, flags, type, addralign, {}, name,
417+
: InputSection(ctx.internalFile, flags, type, addralign, {}, name,
417418
InputSectionBase::Synthetic) {}
418419

419420
virtual ~SyntheticSection() = default;

lld/ELF/LTO.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,12 @@ void BitcodeCompiler::add(BitcodeFile &f) {
256256
// Symbol section is always null for bitcode symbols, hence the check
257257
// for isElf(). Skip linker script defined symbols as well: they have
258258
// no File defined.
259-
!(dr->section == nullptr && (!sym->file || sym->file->isElf()));
259+
!(dr->section == nullptr &&
260+
(sym->file->isInternal() || sym->file->isElf()));
260261

261262
if (r.Prevailing)
262-
Undefined(nullptr, StringRef(), STB_GLOBAL, STV_DEFAULT, sym->type)
263+
Undefined(ctx.internalFile, StringRef(), STB_GLOBAL, STV_DEFAULT,
264+
sym->type)
263265
.overwrite(*sym);
264266

265267
// We tell LTO to not apply interprocedural optimization for wrapped

0 commit comments

Comments
 (0)