@@ -142,15 +142,15 @@ static bool fixupDllMain(COFFLinkerContext &ctx, llvm::object::Archive *file,
142142}
143143
144144ArchiveFile::ArchiveFile (COFFLinkerContext &ctx, MemoryBufferRef m)
145- : InputFile(ctx.symtab, ArchiveKind, m) {
146- // Parse a MemoryBufferRef as an archive file.
147- file = CHECK (Archive::create (mb), this );
148- }
145+ : InputFile(ctx.symtab, ArchiveKind, m) {}
149146
150147void ArchiveFile::parse () {
151148 COFFLinkerContext &ctx = symtab.ctx ;
152149 SymbolTable *archiveSymtab = &symtab;
153150
151+ // Parse a MemoryBufferRef as an archive file.
152+ file = CHECK (Archive::create (mb), this );
153+
154154 // Try to read symbols from ECSYMBOLS section on ARM64EC.
155155 if (ctx.symtab .isEC ()) {
156156 iterator_range<Archive::symbol_iterator> symbols =
@@ -166,9 +166,39 @@ void ArchiveFile::parse() {
166166 // be either a native-only ARM64 or x86_64 archive. Check the machine type
167167 // of the object containing a symbol to determine which symbol table to
168168 // use.
169- MachineTypes machine = getMachineType ();
170- if (machine != IMAGE_FILE_MACHINE_UNKNOWN)
169+ Archive::symbol_iterator sym = file->symbol_begin ();
170+ if (sym != file->symbol_end ()) {
171+ MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN;
172+ Archive::Child child =
173+ CHECK (sym->getMember (),
174+ file->getFileName () +
175+ " : could not get the buffer for a child of the archive" );
176+ MemoryBufferRef mb = CHECK (
177+ child.getMemoryBufferRef (),
178+ file->getFileName () +
179+ " : could not get the buffer for a child buffer of the archive" );
180+ switch (identify_magic (mb.getBuffer ())) {
181+ case file_magic::coff_object: {
182+ std::unique_ptr<COFFObjectFile> obj =
183+ CHECK (COFFObjectFile::create (mb),
184+ check (child.getName ()) + " :" + " : not a valid COFF file" );
185+ machine = MachineTypes (obj->getMachine ());
186+ break ;
187+ }
188+ case file_magic::coff_import_library:
189+ machine = MachineTypes (COFFImportFile (mb).getMachine ());
190+ break ;
191+ case file_magic::bitcode: {
192+ std::unique_ptr<lto::InputFile> obj =
193+ check (lto::InputFile::create (mb));
194+ machine = BitcodeFile::getMachineType (obj.get ());
195+ break ;
196+ }
197+ default :
198+ break ;
199+ }
171200 archiveSymtab = &ctx.getSymtab (machine);
201+ }
172202 }
173203 }
174204
@@ -196,49 +226,6 @@ void ArchiveFile::parse() {
196226 }
197227}
198228
199- MachineTypes ArchiveFile::getMachineType () const {
200- if (!file)
201- return IMAGE_FILE_MACHINE_UNKNOWN;
202- if (file->isEmpty ())
203- return IMAGE_FILE_MACHINE_UNKNOWN;
204- Archive::symbol_iterator sym = file->symbol_begin ();
205- if (sym != file->symbol_end ()) {
206- Expected<Archive::Child> child = sym->getMember ();
207- if (!child) {
208- consumeError (child.takeError ());
209- return IMAGE_FILE_MACHINE_UNKNOWN;
210- }
211- Expected<MemoryBufferRef> mb = child->getMemoryBufferRef ();
212- if (!mb) {
213- consumeError (mb.takeError ());
214- return IMAGE_FILE_MACHINE_UNKNOWN;
215- }
216- switch (identify_magic (mb->getBuffer ())) {
217- case file_magic::coff_object: {
218- Expected<std::unique_ptr<COFFObjectFile>> obj =
219- COFFObjectFile::create (*mb);
220- if (!obj) {
221- consumeError (obj.takeError ());
222- return IMAGE_FILE_MACHINE_UNKNOWN;
223- }
224- return MachineTypes ((*obj)->getMachine ());
225- break ;
226- }
227- case file_magic::coff_import_library:
228- return MachineTypes (COFFImportFile (*mb).getMachine ());
229- break ;
230- case file_magic::bitcode: {
231- std::unique_ptr<lto::InputFile> obj = check (lto::InputFile::create (*mb));
232- return BitcodeFile::getMachineType (obj.get ());
233- break ;
234- }
235- default :
236- break ;
237- }
238- }
239- return IMAGE_FILE_MACHINE_UNKNOWN;
240- }
241-
242229// Returns a buffer pointing to a member file containing a given symbol.
243230void ArchiveFile::addMember (const Archive::Symbol &sym) {
244231 const Archive::Child &c =
0 commit comments