Skip to content

Commit 312fd39

Browse files
committed
Revert some of the previous changes
1 parent 25f3b01 commit 312fd39

File tree

4 files changed

+60
-76
lines changed

4 files changed

+60
-76
lines changed

lld/COFF/Driver.cpp

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,30 +198,6 @@ static bool compatibleMachineType(COFFLinkerContext &ctx, MachineTypes mt) {
198198
}
199199

200200
void LinkerDriver::addFile(InputFile *file) {
201-
// We need to process the machine type early on, so that APIs such as
202-
// `mangle()` won't fail.
203-
MachineTypes mt = file->getMachineType();
204-
// The ARM64EC target must be explicitly specified and cannot be inferred.
205-
if (mt == ARM64EC &&
206-
(ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN ||
207-
(ctx.config.machineInferred &&
208-
(ctx.config.machine == ARM64 || ctx.config.machine == AMD64)))) {
209-
Err(ctx) << toString(file)
210-
<< ": machine type arm64ec is ambiguous and cannot be "
211-
"inferred, use /machine:arm64ec or /machine:arm64x";
212-
return;
213-
}
214-
if (!compatibleMachineType(ctx, mt)) {
215-
Err(ctx) << toString(file) << ": machine type " << machineToStr(mt)
216-
<< " conflicts with " << machineToStr(ctx.config.machine);
217-
return;
218-
}
219-
if (ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN &&
220-
mt != IMAGE_FILE_MACHINE_UNKNOWN) {
221-
ctx.config.machineInferred = true;
222-
setMachine(mt);
223-
}
224-
225201
Log(ctx) << "Reading " << toString(file);
226202
if (file->lazy) {
227203
if (auto *f = dyn_cast<BitcodeFile>(file))
@@ -244,6 +220,28 @@ void LinkerDriver::addFile(InputFile *file) {
244220
}
245221
}
246222

223+
MachineTypes mt = file->getMachineType();
224+
// The ARM64EC target must be explicitly specified and cannot be inferred.
225+
if (mt == ARM64EC &&
226+
(ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN ||
227+
(ctx.config.machineInferred &&
228+
(ctx.config.machine == ARM64 || ctx.config.machine == AMD64)))) {
229+
Err(ctx) << toString(file)
230+
<< ": machine type arm64ec is ambiguous and cannot be "
231+
"inferred, use /machine:arm64ec or /machine:arm64x";
232+
return;
233+
}
234+
if (!compatibleMachineType(ctx, mt)) {
235+
Err(ctx) << toString(file) << ": machine type " << machineToStr(mt)
236+
<< " conflicts with " << machineToStr(ctx.config.machine);
237+
return;
238+
}
239+
if (ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN &&
240+
mt != IMAGE_FILE_MACHINE_UNKNOWN) {
241+
ctx.config.machineInferred = true;
242+
setMachine(mt);
243+
}
244+
247245
parseDirectives(file);
248246
}
249247

lld/COFF/InputFiles.cpp

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ static bool fixupDllMain(COFFLinkerContext &ctx, llvm::object::Archive *file,
142142
}
143143

144144
ArchiveFile::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

150147
void 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.
243230
void ArchiveFile::addMember(const Archive::Symbol &sym) {
244231
const Archive::Child &c =

lld/COFF/InputFiles.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ class ArchiveFile : public InputFile {
121121
explicit ArchiveFile(COFFLinkerContext &ctx, MemoryBufferRef m);
122122
static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
123123
void parse() override;
124-
MachineTypes getMachineType() const override;
125124

126125
// Enqueues an archive member load for the given symbol. If we've already
127126
// enqueued a load for the same archive member, this function does nothing,

lld/test/COFF/implib-machine.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
# RUN: llvm-mc -triple x86_64-windows-msvc %t.dir/test.s -filetype=obj -o %t.dir/test64.obj
77

88
# RUN: not lld-link -dll -noentry -out:%t32.dll %t.dir/test32.obj %t.dir/test64.lib 2>&1 | FileCheck --check-prefix=ERR32 %s
9-
# ERR32: error: {{.*[/\\]}}test64.lib: machine type x64 conflicts with x86
9+
# ERR32: error: test64.lib(test.dll): machine type x64 conflicts with x86
1010

1111
# RUN: not lld-link -dll -noentry -out:%t64.dll %t.dir/test64.obj %t.dir/test32.lib 2>&1 | FileCheck --check-prefix=ERR64 %s
12-
# ERR64: error: {{.*[/\\]}}test32.lib: machine type x86 conflicts with x64
12+
# ERR64: error: test32.lib(test.dll): machine type x86 conflicts with x64
1313

1414
#--- test.s
1515
.def @feat.00;

0 commit comments

Comments
 (0)