@@ -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
168183void 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
221234void 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
809812MachineTypes 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
815816ArrayRef<uint8_t > ObjFile::getDebugSection (StringRef secName) {
0 commit comments