diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 3f5b00b383b0..e7abc411f2b6 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -98,7 +98,7 @@ void LinkerDriver::link(ArrayRef ArgsArr) { Symtab.reportRemainingUndefines(); // Write the result. - ObjectFileBase &FirstObj = *Symtab.getObjectFiles()[0]; + ObjectFileBase &FirstObj = *Symtab.getFirstObject(); switch (FirstObj.getELFKind()) { case ELF32LEKind: writeResult(&Symtab); diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp index 2d1b5c8f782a..5e9a1c9ffa83 100644 --- a/lld/ELF/SymbolTable.cpp +++ b/lld/ELF/SymbolTable.cpp @@ -33,10 +33,9 @@ template void SymbolTable::init() { } void SymbolTable::addObject(ObjectFileBase *File) { - if (!ObjectFiles.empty()) { - ObjectFileBase &Old = *ObjectFiles[0]; - if (!Old.isCompatibleWith(*File)) - error(Twine(Old.getName() + " is incompatible with " + File->getName())); + if (const ObjectFileBase *Old = getFirstObject()) { + if (!Old->isCompatibleWith(*File)) + error(Twine(Old->getName() + " is incompatible with " + File->getName())); } else { switch (File->getELFKind()) { case ELF32LEKind: diff --git a/lld/ELF/SymbolTable.h b/lld/ELF/SymbolTable.h index b4e50aa6438d..84fbe00be2b7 100644 --- a/lld/ELF/SymbolTable.h +++ b/lld/ELF/SymbolTable.h @@ -34,6 +34,12 @@ public: void addFile(std::unique_ptr File); + ObjectFileBase *getFirstObject() const { + if (!ObjectFiles.empty()) + return ObjectFiles[0].get(); + return nullptr; + } + // Print an error message on undefined symbols. void reportRemainingUndefines(); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 970d6cdb9bcb..f49874295bb3 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -543,7 +543,7 @@ template void Writer::writeHeader() { EHdr->e_type = ET_EXEC; const SymbolTable &Symtab = SymTable.getSymTable(); - auto &FirstObj = cast>(*Symtab.getObjectFiles()[0]); + auto &FirstObj = cast>(*Symtab.getFirstObject()); EHdr->e_machine = FirstObj.getObj()->getHeader()->e_machine; EHdr->e_version = EV_CURRENT; EHdr->e_entry = 0x401000;