Skip to content

Commit 9c8214f

Browse files
authored
[LLD][COFF] Create COFFObjectFile instance when constructing ObjFile (NFC) (#120144)
This change moves the creation of COFFObjectFile to the construction of ObjFile, instead of delaying it until parsing.
1 parent 659dbb6 commit 9c8214f

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

lld/COFF/Driver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> mb,
229229
break;
230230
case file_magic::coff_object:
231231
case file_magic::coff_import_library:
232-
ctx.symtab.addFile(make<ObjFile>(ctx, mbref, lazy));
232+
ctx.symtab.addFile(ObjFile::create(ctx, mbref, lazy));
233233
break;
234234
case file_magic::pdb:
235235
ctx.symtab.addFile(make<PDBInputFile>(ctx, mbref));
@@ -312,7 +312,7 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName,
312312

313313
InputFile *obj;
314314
if (magic == file_magic::coff_object) {
315-
obj = make<ObjFile>(ctx, mb);
315+
obj = ObjFile::create(ctx, mb);
316316
} else if (magic == file_magic::bitcode) {
317317
obj =
318318
make<BitcodeFile>(ctx, mb, parentName, offsetInArchive, /*lazy=*/false);
@@ -1389,7 +1389,7 @@ void LinkerDriver::convertResources() {
13891389
return;
13901390
}
13911391
ObjFile *f =
1392-
make<ObjFile>(ctx, convertResToCOFF(resources, resourceObjFiles));
1392+
ObjFile::create(ctx, convertResToCOFF(resources, resourceObjFiles));
13931393
ctx.symtab.addFile(f);
13941394
f->includeResourceChunks();
13951395
}

lld/COFF/InputFiles.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,26 @@ lld::coff::getArchiveMembers(COFFLinkerContext &ctx, Archive *file) {
162162
return v;
163163
}
164164

165-
ObjFile::ObjFile(COFFLinkerContext &ctx, MemoryBufferRef m, bool lazy)
166-
: InputFile(ctx.symtab, ObjectKind, m, lazy) {}
165+
ObjFile::ObjFile(SymbolTable &symtab, COFFObjectFile *coffObj, bool lazy)
166+
: InputFile(symtab, ObjectKind, coffObj->getMemoryBufferRef(), lazy),
167+
coffObj(coffObj) {}
168+
169+
ObjFile *ObjFile::create(COFFLinkerContext &ctx, MemoryBufferRef m, bool lazy) {
170+
// Parse a memory buffer as a COFF file.
171+
Expected<std::unique_ptr<Binary>> bin = createBinary(m);
172+
if (!bin)
173+
Fatal(ctx) << "Could not parse " << m.getBufferIdentifier();
174+
175+
auto *obj = dyn_cast<COFFObjectFile>(bin->get());
176+
if (!obj)
177+
Fatal(ctx) << m.getBufferIdentifier() << " is not a COFF file";
178+
179+
bin->release();
180+
return make<ObjFile>(ctx.symtab, obj, lazy);
181+
}
167182

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

221234
void ObjFile::parse() {
222-
// Parse a memory buffer as a COFF file.
223-
std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
224-
225-
if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
226-
bin.release();
227-
coffObj.reset(obj);
228-
} else {
229-
Fatal(symtab.ctx) << toString(this) << " is not a COFF file";
230-
}
231-
232235
// Read section and symbol tables.
233236
initializeChunks();
234237
initializeSymbols();
@@ -807,9 +810,7 @@ std::optional<Symbol *> ObjFile::createDefined(
807810
}
808811

809812
MachineTypes ObjFile::getMachineType() const {
810-
if (coffObj)
811-
return static_cast<MachineTypes>(coffObj->getMachine());
812-
return IMAGE_FILE_MACHINE_UNKNOWN;
813+
return static_cast<MachineTypes>(coffObj->getMachine());
813814
}
814815

815816
ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {

lld/COFF/InputFiles.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ class ArchiveFile : public InputFile {
136136
// .obj or .o file. This may be a member of an archive file.
137137
class ObjFile : public InputFile {
138138
public:
139-
explicit ObjFile(COFFLinkerContext &ctx, MemoryBufferRef m,
140-
bool lazy = false);
139+
static ObjFile *create(COFFLinkerContext &ctx, MemoryBufferRef mb,
140+
bool lazy = false);
141+
explicit ObjFile(SymbolTable &symtab, COFFObjectFile *coffObj, bool lazy);
142+
141143
static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
142144
void parse() override;
143145
void parseLazy();

lld/COFF/LTO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
259259
if (llvm::is_contained(ctx.config.saveTempsArgs, "prelink") || emitASM)
260260
saveBuffer(buf[i].second, ltoObjName);
261261
if (!emitASM)
262-
ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName)));
262+
ret.push_back(ObjFile::create(ctx, MemoryBufferRef(objBuf, ltoObjName)));
263263
}
264264

265265
return ret;

0 commit comments

Comments
 (0)