Skip to content

[LLD][COFF] Create COFFObjectFile instance when constructing ObjFile (NFC) #120144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> mb,
break;
case file_magic::coff_object:
case file_magic::coff_import_library:
ctx.symtab.addFile(make<ObjFile>(ctx, mbref, lazy));
ctx.symtab.addFile(ObjFile::create(ctx, mbref, lazy));
break;
case file_magic::pdb:
ctx.symtab.addFile(make<PDBInputFile>(ctx, mbref));
Expand Down Expand Up @@ -313,7 +313,7 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName,

InputFile *obj;
if (magic == file_magic::coff_object) {
obj = make<ObjFile>(ctx, mb);
obj = ObjFile::create(ctx, mb);
} else if (magic == file_magic::bitcode) {
obj =
make<BitcodeFile>(ctx, mb, parentName, offsetInArchive, /*lazy=*/false);
Expand Down Expand Up @@ -1390,7 +1390,7 @@ void LinkerDriver::convertResources() {
return;
}
ObjFile *f =
make<ObjFile>(ctx, convertResToCOFF(resources, resourceObjFiles));
ObjFile::create(ctx, convertResToCOFF(resources, resourceObjFiles));
ctx.symtab.addFile(f);
f->includeResourceChunks();
}
Expand Down
35 changes: 18 additions & 17 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<Binary>> bin = createBinary(m);
if (!bin)
Fatal(ctx) << "Could not parse " << m.getBufferIdentifier();

auto *obj = dyn_cast<COFFObjectFile>(bin->get());
if (!obj)
Fatal(ctx) << m.getBufferIdentifier() << " is not a COFF file";

bin->release();
return make<ObjFile>(ctx.symtab, obj, lazy);
}

void ObjFile::parseLazy() {
// Native object file.
std::unique_ptr<Binary> coffObjPtr = CHECK(createBinary(mb), this);
COFFObjectFile *coffObj = cast<COFFObjectFile>(coffObjPtr.get());
uint32_t numSymbols = coffObj->getNumberOfSymbols();
for (uint32_t i = 0; i < numSymbols; ++i) {
COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
Expand Down Expand Up @@ -219,16 +232,6 @@ void ObjFile::initializeECThunks() {
}

void ObjFile::parse() {
// Parse a memory buffer as a COFF file.
std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);

if (auto *obj = dyn_cast<COFFObjectFile>(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();
Expand Down Expand Up @@ -807,9 +810,7 @@ std::optional<Symbol *> ObjFile::createDefined(
}

MachineTypes ObjFile::getMachineType() const {
if (coffObj)
return static_cast<MachineTypes>(coffObj->getMachine());
return IMAGE_FILE_MACHINE_UNKNOWN;
return static_cast<MachineTypes>(coffObj->getMachine());
}

ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
Expand Down
6 changes: 4 additions & 2 deletions lld/COFF/InputFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion lld/COFF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
if (llvm::is_contained(ctx.config.saveTempsArgs, "prelink") || emitASM)
saveBuffer(buf[i].second, ltoObjName);
if (!emitASM)
ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName)));
ret.push_back(ObjFile::create(ctx, MemoryBufferRef(objBuf, ltoObjName)));
}

return ret;
Expand Down
Loading