Skip to content

Commit

Permalink
[ELF] Remove ctx indirection. NFC
Browse files Browse the repository at this point in the history
Add LLVM_LIBRARY_VISIBILITY to remove unneeded GOT and unique_ptr
indirection. We can move other global variables into ctx without
indirection concern. In the long term we may consider passing Ctx
as a parameter to various functions and eliminate global state as
much as possible and then remove `Ctx::reset`.
  • Loading branch information
MaskRay committed Oct 1, 2022
1 parent a623a4c commit 34fa860
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 111 deletions.
2 changes: 1 addition & 1 deletion lld/ELF/AArch64ErrataFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ void AArch64Err843419Patcher::init() {
};

// Collect mapping symbols for every executable InputSection.
for (ELFFileBase *file : ctx->objectFiles) {
for (ELFFileBase *file : ctx.objectFiles) {
for (Symbol *b : file->getLocalSymbols()) {
auto *def = dyn_cast<Defined>(b);
if (!def)
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/ARMErrataFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void ARMErr657417Patcher::init() {
};

// Collect mapping symbols for every executable InputSection.
for (ELFFileBase *file : ctx->objectFiles) {
for (ELFFileBase *file : ctx.objectFiles) {
for (Symbol *s : file->getLocalSymbols()) {
auto *def = dyn_cast<Defined>(s);
if (!def)
Expand Down
16 changes: 8 additions & 8 deletions lld/ELF/Arch/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ static uint32_t getEFlags(InputFile *file) {
}

uint32_t AMDGPU::calcEFlagsV3() const {
uint32_t ret = getEFlags(ctx->objectFiles[0]);
uint32_t ret = getEFlags(ctx.objectFiles[0]);

// Verify that all input files have the same e_flags.
for (InputFile *f : makeArrayRef(ctx->objectFiles).slice(1)) {
for (InputFile *f : makeArrayRef(ctx.objectFiles).slice(1)) {
if (ret == getEFlags(f))
continue;
error("incompatible e_flags: " + toString(f));
Expand All @@ -61,15 +61,15 @@ uint32_t AMDGPU::calcEFlagsV3() const {
}

uint32_t AMDGPU::calcEFlagsV4() const {
uint32_t retMach = getEFlags(ctx->objectFiles[0]) & EF_AMDGPU_MACH;
uint32_t retMach = getEFlags(ctx.objectFiles[0]) & EF_AMDGPU_MACH;
uint32_t retXnack =
getEFlags(ctx->objectFiles[0]) & EF_AMDGPU_FEATURE_XNACK_V4;
getEFlags(ctx.objectFiles[0]) & EF_AMDGPU_FEATURE_XNACK_V4;
uint32_t retSramEcc =
getEFlags(ctx->objectFiles[0]) & EF_AMDGPU_FEATURE_SRAMECC_V4;
getEFlags(ctx.objectFiles[0]) & EF_AMDGPU_FEATURE_SRAMECC_V4;

// Verify that all input files have compatible e_flags (same mach, all
// features in the same category are either ANY, ANY and ON, or ANY and OFF).
for (InputFile *f : makeArrayRef(ctx->objectFiles).slice(1)) {
for (InputFile *f : makeArrayRef(ctx.objectFiles).slice(1)) {
if (retMach != (getEFlags(f) & EF_AMDGPU_MACH)) {
error("incompatible mach: " + toString(f));
return 0;
Expand Down Expand Up @@ -106,10 +106,10 @@ uint32_t AMDGPU::calcEFlagsV4() const {
}

uint32_t AMDGPU::calcEFlags() const {
if (ctx->objectFiles.empty())
if (ctx.objectFiles.empty())
return 0;

uint8_t abiVersion = cast<ObjFile<ELF64LE>>(ctx->objectFiles[0])
uint8_t abiVersion = cast<ObjFile<ELF64LE>>(ctx.objectFiles[0])
->getObj()
.getHeader()
.e_ident[EI_ABIVERSION];
Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/Arch/AVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ static uint32_t getEFlags(InputFile *file) {
}

uint32_t AVR::calcEFlags() const {
assert(!ctx->objectFiles.empty());
assert(!ctx.objectFiles.empty());

uint32_t flags = getEFlags(ctx->objectFiles[0]);
uint32_t flags = getEFlags(ctx.objectFiles[0]);
bool hasLinkRelaxFlag = flags & EF_AVR_LINKRELAX_PREPARED;

for (InputFile *f : makeArrayRef(ctx->objectFiles).slice(1)) {
for (InputFile *f : makeArrayRef(ctx.objectFiles).slice(1)) {
uint32_t objFlags = getEFlags(f);
if ((objFlags & EF_AVR_ARCH_MASK) != (flags & EF_AVR_ARCH_MASK))
error(toString(f) +
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/Arch/Hexagon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ Hexagon::Hexagon() {
}

uint32_t Hexagon::calcEFlags() const {
assert(!ctx->objectFiles.empty());
assert(!ctx.objectFiles.empty());

// The architecture revision must always be equal to or greater than
// greatest revision in the list of inputs.
uint32_t ret = 0;
for (InputFile *f : ctx->objectFiles) {
for (InputFile *f : ctx.objectFiles) {
uint32_t eflags = cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags;
if (eflags > ret)
ret = eflags;
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Arch/MipsArchTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ static uint32_t getArchFlags(ArrayRef<FileFlags> files) {

template <class ELFT> uint32_t elf::calcMipsEFlags() {
std::vector<FileFlags> v;
for (InputFile *f : ctx->objectFiles)
for (InputFile *f : ctx.objectFiles)
v.push_back({f, cast<ObjFile<ELFT>>(f)->getObj().getHeader().e_flags});
if (v.empty()) {
// If we don't have any input files, we'll have to rely on the information
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Arch/PPC64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ static uint32_t getEFlags(InputFile *file) {
// This file implements v2 ABI. This function makes sure that all
// object files have v2 or an unspecified version as an ABI version.
uint32_t PPC64::calcEFlags() const {
for (InputFile *f : ctx->objectFiles) {
for (InputFile *f : ctx.objectFiles) {
uint32_t flag = getEFlags(f);
if (flag == 1)
error(toString(f) + ": ABI version 1 is not supported");
Expand Down
10 changes: 5 additions & 5 deletions lld/ELF/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ static uint32_t getEFlags(InputFile *f) {
uint32_t RISCV::calcEFlags() const {
// If there are only binary input files (from -b binary), use a
// value of 0 for the ELF header flags.
if (ctx->objectFiles.empty())
if (ctx.objectFiles.empty())
return 0;

uint32_t target = getEFlags(ctx->objectFiles.front());
uint32_t target = getEFlags(ctx.objectFiles.front());

for (InputFile *f : ctx->objectFiles) {
for (InputFile *f : ctx.objectFiles) {
uint32_t eflags = getEFlags(f);
if (eflags & EF_RISCV_RVC)
target |= EF_RISCV_RVC;
Expand All @@ -142,7 +142,7 @@ uint32_t RISCV::calcEFlags() const {
error(
toString(f) +
": cannot link object files with different floating-point ABI from " +
toString(ctx->objectFiles[0]));
toString(ctx.objectFiles[0]));

if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE))
error(toString(f) +
Expand Down Expand Up @@ -524,7 +524,7 @@ static void initSymbolAnchors() {
}
// Store anchors (st_value and st_value+st_size) for symbols relative to text
// sections.
for (InputFile *file : ctx->objectFiles)
for (InputFile *file : ctx.objectFiles)
for (Symbol *sym : file->getSymbols()) {
auto *d = dyn_cast<Defined>(sym);
if (!d || d->file != file)
Expand Down
13 changes: 7 additions & 6 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,6 @@ struct Ctx {
// Symbols in a non-prevailing COMDAT group which should be changed to an
// Undefined.
SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;
// True if SHT_LLVM_SYMPART is used.
std::atomic<bool> hasSympart{false};
// True if we need to reserve two .got entries for local-dynamic TLS model.
std::atomic<bool> needsTlsLd{false};
// A tuple of (reference, extractedFile, sym). Used by --why-extract=.
SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0>
whyExtractRecords;
Expand All @@ -407,10 +403,15 @@ struct Ctx {
llvm::DenseMap<const Symbol *,
std::pair<const InputFile *, const InputFile *>>
backwardReferences;
// True if SHT_LLVM_SYMPART is used.
std::atomic<bool> hasSympart{false};
// True if we need to reserve two .got entries for local-dynamic TLS model.
std::atomic<bool> needsTlsLd{false};

void reset();
};

// The only instance of Ctx struct.
extern std::unique_ptr<Ctx> ctx;
LLVM_LIBRARY_VISIBILITY extern Ctx ctx;

// The first two elements of versionDefinitions represent VER_NDX_LOCAL and
// VER_NDX_GLOBAL. This helper returns other elements.
Expand Down
Loading

0 comments on commit 34fa860

Please sign in to comment.