From a2f13a9c473aaacc105bd1eb5e1d88386b934a56 Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Mon, 16 Dec 2024 20:05:25 +0100 Subject: [PATCH] [LLD][COFF] Create COFFObjectFile instance when constructing ObjFile This change moves the creation of COFFObjectFile to the construction of ObjFile, instead of delaying it until parsing. --- lld/COFF/Driver.cpp | 6 +++--- lld/COFF/InputFiles.cpp | 35 ++++++++++++++++++----------------- lld/COFF/InputFiles.h | 6 ++++-- lld/COFF/LTO.cpp | 2 +- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index 0705f1c1be999..b588086a3c98b 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -230,7 +230,7 @@ void LinkerDriver::addBuffer(std::unique_ptr mb, break; case file_magic::coff_object: case file_magic::coff_import_library: - ctx.symtab.addFile(make(ctx, mbref, lazy)); + ctx.symtab.addFile(ObjFile::create(ctx, mbref, lazy)); break; case file_magic::pdb: ctx.symtab.addFile(make(ctx, mbref)); @@ -313,7 +313,7 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName, InputFile *obj; if (magic == file_magic::coff_object) { - obj = make(ctx, mb); + obj = ObjFile::create(ctx, mb); } else if (magic == file_magic::bitcode) { obj = make(ctx, mb, parentName, offsetInArchive, /*lazy=*/false); @@ -1390,7 +1390,7 @@ void LinkerDriver::convertResources() { return; } ObjFile *f = - make(ctx, convertResToCOFF(resources, resourceObjFiles)); + ObjFile::create(ctx, convertResToCOFF(resources, resourceObjFiles)); ctx.symtab.addFile(f); f->includeResourceChunks(); } diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp index 9ff9346ed598b..6ce075be798e4 100644 --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -162,13 +162,26 @@ lld::coff::getArchiveMembers(COFFLinkerContext &ctx, Archive *file) { return v; } -ObjFile::ObjFile(COFFLinkerContext &ctx, MemoryBufferRef m, bool lazy) - : InputFile(ctx.symtab, ObjectKind, m, lazy) {} +ObjFile::ObjFile(SymbolTable &symtab, COFFObjectFile *coffObj, bool lazy) + : InputFile(symtab, ObjectKind, coffObj->getMemoryBufferRef(), lazy), + coffObj(coffObj) {} + +ObjFile *ObjFile::create(COFFLinkerContext &ctx, MemoryBufferRef m, bool lazy) { + // Parse a memory buffer as a COFF file. + Expected> bin = createBinary(m); + if (!bin) + Fatal(ctx) << "Could not parse " << m.getBufferIdentifier(); + + auto *obj = dyn_cast(bin->get()); + if (!obj) + Fatal(ctx) << m.getBufferIdentifier() << " is not a COFF file"; + + bin->release(); + return make(ctx.symtab, obj, lazy); +} void ObjFile::parseLazy() { // Native object file. - std::unique_ptr coffObjPtr = CHECK(createBinary(mb), this); - COFFObjectFile *coffObj = cast(coffObjPtr.get()); uint32_t numSymbols = coffObj->getNumberOfSymbols(); for (uint32_t i = 0; i < numSymbols; ++i) { COFFSymbolRef coffSym = check(coffObj->getSymbol(i)); @@ -219,16 +232,6 @@ void ObjFile::initializeECThunks() { } void ObjFile::parse() { - // Parse a memory buffer as a COFF file. - std::unique_ptr bin = CHECK(createBinary(mb), this); - - if (auto *obj = dyn_cast(bin.get())) { - bin.release(); - coffObj.reset(obj); - } else { - Fatal(symtab.ctx) << toString(this) << " is not a COFF file"; - } - // Read section and symbol tables. initializeChunks(); initializeSymbols(); @@ -807,9 +810,7 @@ std::optional ObjFile::createDefined( } MachineTypes ObjFile::getMachineType() const { - if (coffObj) - return static_cast(coffObj->getMachine()); - return IMAGE_FILE_MACHINE_UNKNOWN; + return static_cast(coffObj->getMachine()); } ArrayRef ObjFile::getDebugSection(StringRef secName) { diff --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h index fd2e409ada30f..956e4dd8bc4cf 100644 --- a/lld/COFF/InputFiles.h +++ b/lld/COFF/InputFiles.h @@ -136,8 +136,10 @@ class ArchiveFile : public InputFile { // .obj or .o file. This may be a member of an archive file. class ObjFile : public InputFile { public: - explicit ObjFile(COFFLinkerContext &ctx, MemoryBufferRef m, - bool lazy = false); + static ObjFile *create(COFFLinkerContext &ctx, MemoryBufferRef mb, + bool lazy = false); + explicit ObjFile(SymbolTable &symtab, COFFObjectFile *coffObj, bool lazy); + static bool classof(const InputFile *f) { return f->kind() == ObjectKind; } void parse() override; void parseLazy(); diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp index d7e003ce3e2d8..a8cecb39ac614 100644 --- a/lld/COFF/LTO.cpp +++ b/lld/COFF/LTO.cpp @@ -259,7 +259,7 @@ std::vector BitcodeCompiler::compile() { if (llvm::is_contained(ctx.config.saveTempsArgs, "prelink") || emitASM) saveBuffer(buf[i].second, ltoObjName); if (!emitASM) - ret.push_back(make(ctx, MemoryBufferRef(objBuf, ltoObjName))); + ret.push_back(ObjFile::create(ctx, MemoryBufferRef(objBuf, ltoObjName))); } return ret;