[lld] f2b0133 - [ELF] Move static nextGroupId isInGroup to LinkerDriver
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 6 17:38:47 PDT 2024
Author: Fangrui Song
Date: 2024-10-06T17:38:35-07:00
New Revision: f2b01338584c90f48dba1a937bf5b1da8dcedbd5
URL: https://github.com/llvm/llvm-project/commit/f2b01338584c90f48dba1a937bf5b1da8dcedbd5
DIFF: https://github.com/llvm/llvm-project/commit/f2b01338584c90f48dba1a937bf5b1da8dcedbd5.diff
LOG: [ELF] Move static nextGroupId isInGroup to LinkerDriver
Added:
Modified:
lld/ELF/Config.h
lld/ELF/Driver.cpp
lld/ELF/InputFiles.cpp
lld/ELF/InputFiles.h
lld/ELF/ScriptParser.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 1d113375143a51..f4a295a1371e07 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -175,6 +175,9 @@ class LinkerDriver {
std::vector<InputFile *> files;
public:
+ // See InputFile::groupId.
+ uint32_t nextGroupId;
+ bool isInGroup;
InputFile *armCmseImpLib = nullptr;
SmallVector<std::pair<StringRef, unsigned>, 0> archiveFiles;
};
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index c1132ff81f9bc1..4206a7fcc65926 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -56,6 +56,7 @@
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Remarks/HotnessThresholdParser.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/GlobPattern.h"
@@ -322,8 +323,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
//
// All files within the archive get the same group ID to allow mutual
// references for --warn-backrefs.
- bool saved = InputFile::isInGroup;
- InputFile::isInGroup = true;
+ SaveAndRestore saved(isInGroup, true);
for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
auto magic = identify_magic(p.first.getBuffer());
if (magic == file_magic::elf_relocatable) {
@@ -335,9 +335,8 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
warn(path + ": archive member '" + p.first.getBufferIdentifier() +
"' is neither ET_REL nor LLVM bitcode");
}
- InputFile::isInGroup = saved;
- if (!saved)
- ++InputFile::nextGroupId;
+ if (!saved.get())
+ ++nextGroupId;
return;
}
case file_magic::elf_shared_object: {
@@ -1934,7 +1933,8 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
// Iterate over argv to process input files and positional arguments.
std::optional<MemoryBufferRef> defaultScript;
- InputFile::isInGroup = false;
+ nextGroupId = 0;
+ isInGroup = false;
bool hasInput = false, hasScript = false;
for (auto *arg : args) {
switch (arg->getOption().getID()) {
@@ -2004,30 +2004,30 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
armCmseImpLib = createObjFile(*mb);
break;
case OPT_start_group:
- if (InputFile::isInGroup)
+ if (isInGroup)
error("nested --start-group");
- InputFile::isInGroup = true;
+ isInGroup = true;
break;
case OPT_end_group:
- if (!InputFile::isInGroup)
+ if (!isInGroup)
error("stray --end-group");
- InputFile::isInGroup = false;
- ++InputFile::nextGroupId;
+ isInGroup = false;
+ ++nextGroupId;
break;
case OPT_start_lib:
if (inLib)
error("nested --start-lib");
- if (InputFile::isInGroup)
+ if (isInGroup)
error("may not nest --start-lib in --start-group");
inLib = true;
- InputFile::isInGroup = true;
+ isInGroup = true;
break;
case OPT_end_lib:
if (!inLib)
error("stray --end-lib");
inLib = false;
- InputFile::isInGroup = false;
- ++InputFile::nextGroupId;
+ isInGroup = false;
+ ++nextGroupId;
break;
case OPT_push_state:
stack.emplace_back(ctx.arg.asNeeded, ctx.arg.isStatic, inWholeArchive);
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index dc107c2d0caede..d2ba5b2e4f8f8f 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -48,9 +48,6 @@ extern template void ObjFile<ELF32BE>::importCmseSymbols();
extern template void ObjFile<ELF64LE>::importCmseSymbols();
extern template void ObjFile<ELF64BE>::importCmseSymbols();
-bool InputFile::isInGroup;
-uint32_t InputFile::nextGroupId;
-
// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
std::string lld::toString(const InputFile *f) {
static std::mutex mu;
@@ -206,11 +203,11 @@ static void updateSupportedARMFeatures(Ctx &ctx,
}
InputFile::InputFile(Kind k, MemoryBufferRef m)
- : mb(m), groupId(nextGroupId), fileKind(k) {
+ : mb(m), groupId(ctx.driver.nextGroupId), fileKind(k) {
// All files within the same --{start,end}-group get the same group ID.
// Otherwise, a new file will get a new group ID.
- if (!isInGroup)
- ++nextGroupId;
+ if (!ctx.driver.isInGroup)
+ ++ctx.driver.nextGroupId;
}
std::optional<MemoryBufferRef> elf::readFile(Ctx &ctx, StringRef path) {
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index 4f78cdb60d9762..50fa882922c767 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -127,8 +127,6 @@ class InputFile {
// --{start,end}-lib get the same group ID. Otherwise, each file gets a new
// group ID. For more info, see checkDependency() in SymbolTable.cpp.
uint32_t groupId;
- static bool isInGroup;
- static uint32_t nextGroupId;
// If this is an architecture-specific file, the following members
// have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index cc0e8cef9763b0..3febcfb87da4ac 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -385,12 +385,10 @@ void ScriptParser::readExtern() {
}
void ScriptParser::readGroup() {
- bool orig = InputFile::isInGroup;
- InputFile::isInGroup = true;
+ SaveAndRestore saved(ctx.driver.isInGroup, true);
readInput();
- InputFile::isInGroup = orig;
- if (!orig)
- ++InputFile::nextGroupId;
+ if (!saved.get())
+ ++ctx.driver.nextGroupId;
}
void ScriptParser::readInclude() {
More information about the llvm-commits
mailing list