[lld] 242316b - [ELF] Simplify createObjectFile/createLazyFile. NFC
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 22 01:26:19 PDT 2022
Author: Fangrui Song
Date: 2022-07-22T01:26:12-07:00
New Revision: 242316bc2719126ec76ac8535b4db0f24b7c500c
URL: https://github.com/llvm/llvm-project/commit/242316bc2719126ec76ac8535b4db0f24b7c500c
DIFF: https://github.com/llvm/llvm-project/commit/242316bc2719126ec76ac8535b4db0f24b7c500c.diff
LOG: [ELF] Simplify createObjectFile/createLazyFile. NFC
And avoid redundant identify_magic test.
Added:
Modified:
lld/ELF/Driver.cpp
lld/ELF/InputFiles.cpp
lld/ELF/InputFiles.h
lld/ELF/LTO.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 4c26cba1cb4f8..d4621c1affbc9 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -217,8 +217,12 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
return;
case file_magic::archive: {
if (inWholeArchive) {
- for (const auto &p : getArchiveMembers(mbref))
- files.push_back(createObjectFile(p.first, path, p.second));
+ for (const auto &p : getArchiveMembers(mbref)) {
+ if (isBitcode(p.first))
+ files.push_back(make<BitcodeFile>(p.first, path, p.second, false));
+ else
+ files.push_back(createObjFile(p.first, path));
+ }
return;
}
@@ -241,8 +245,10 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
InputFile::isInGroup = true;
for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
auto magic = identify_magic(p.first.getBuffer());
- if (magic == file_magic::bitcode || magic == file_magic::elf_relocatable)
- files.push_back(createLazyFile(p.first, path, p.second));
+ if (magic == file_magic::elf_relocatable)
+ files.push_back(createObjFile(p.first, path, true));
+ else if (magic == file_magic::bitcode)
+ files.push_back(make<BitcodeFile>(p.first, path, p.second, true));
else
warn(path + ": archive member '" + p.first.getBufferIdentifier() +
"' is neither ET_REL nor LLVM bitcode");
@@ -267,11 +273,10 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
make<SharedFile>(mbref, withLOption ? path::filename(path) : path));
return;
case file_magic::bitcode:
+ files.push_back(make<BitcodeFile>(mbref, "", 0, inLib));
+ break;
case file_magic::elf_relocatable:
- if (inLib)
- files.push_back(createLazyFile(mbref, "", 0));
- else
- files.push_back(createObjectFile(mbref));
+ files.push_back(createObjFile(mbref, "", inLib));
break;
default:
error(path + ": unknown file type");
@@ -1612,7 +1617,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
break;
case OPT_just_symbols:
if (Optional<MemoryBufferRef> mb = readFile(arg->getValue())) {
- files.push_back(createObjectFile(*mb));
+ files.push_back(createObjFile(*mb));
files.back()->justSymbols = true;
}
break;
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 1feddf0b4033e..b56dd1acfc43a 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1710,34 +1710,27 @@ void BinaryFile::parse() {
data.size(), 0, nullptr});
}
-InputFile *elf::createObjectFile(MemoryBufferRef mb, StringRef archiveName,
- uint64_t offsetInArchive) {
- if (isBitcode(mb))
- return make<BitcodeFile>(mb, archiveName, offsetInArchive, /*lazy=*/false);
-
+ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName,
+ bool lazy) {
+ ELFFileBase *f;
switch (getELFKind(mb, archiveName)) {
case ELF32LEKind:
- return make<ObjFile<ELF32LE>>(mb, archiveName);
+ f = make<ObjFile<ELF32LE>>(mb, archiveName);
+ break;
case ELF32BEKind:
- return make<ObjFile<ELF32BE>>(mb, archiveName);
+ f = make<ObjFile<ELF32BE>>(mb, archiveName);
+ break;
case ELF64LEKind:
- return make<ObjFile<ELF64LE>>(mb, archiveName);
+ f = make<ObjFile<ELF64LE>>(mb, archiveName);
+ break;
case ELF64BEKind:
- return make<ObjFile<ELF64BE>>(mb, archiveName);
+ f = make<ObjFile<ELF64BE>>(mb, archiveName);
+ break;
default:
llvm_unreachable("getELFKind");
}
-}
-
-InputFile *elf::createLazyFile(MemoryBufferRef mb, StringRef archiveName,
- uint64_t offsetInArchive) {
- if (isBitcode(mb))
- return make<BitcodeFile>(mb, archiveName, offsetInArchive, /*lazy=*/true);
-
- auto *file =
- cast<ELFFileBase>(createObjectFile(mb, archiveName, offsetInArchive));
- file->lazy = true;
- return file;
+ f->lazy = lazy;
+ return f;
}
template <class ELFT> void ObjFile<ELFT>::parseLazy() {
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index f89246eb645e8..02538f44de2c1 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -371,10 +371,8 @@ class BinaryFile : public InputFile {
void parse();
};
-InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName = "",
- uint64_t offsetInArchive = 0);
-InputFile *createLazyFile(MemoryBufferRef mb, StringRef archiveName,
- uint64_t offsetInArchive);
+ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "",
+ bool lazy = false);
inline bool isBitcode(MemoryBufferRef mb) {
return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode;
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 8c5001af3a91d..b8019bd7d2401 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -383,10 +383,10 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
std::vector<InputFile *> ret;
for (unsigned i = 0; i != maxTasks; ++i)
if (!buf[i].empty())
- ret.push_back(createObjectFile(MemoryBufferRef(buf[i], "lto.tmp")));
+ ret.push_back(createObjFile(MemoryBufferRef(buf[i], "lto.tmp")));
for (std::unique_ptr<MemoryBuffer> &file : files)
if (file)
- ret.push_back(createObjectFile(*file));
+ ret.push_back(createObjFile(*file));
return ret;
}
More information about the llvm-commits
mailing list