Skip to content

[LLD][COFF] Validate import library machine type. #102738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,13 @@ void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
: InputFile(ctx, ImportKind, m), live(!ctx.config.doGC), thunkLive(live) {}

MachineTypes ImportFile::getMachineType() const {
uint16_t machine =
reinterpret_cast<const coff_import_header *>(mb.getBufferStart())
->Machine;
return MachineTypes(machine);
}

void ImportFile::parse() {
const auto *hdr =
reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
Expand Down
1 change: 1 addition & 0 deletions lld/COFF/InputFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ class ImportFile : public InputFile {
explicit ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m);

static bool classof(const InputFile *f) { return f->kind() == ImportKind; }
MachineTypes getMachineType() const override;

Symbol *impSym = nullptr;
Symbol *thunkSym = nullptr;
Expand Down
32 changes: 32 additions & 0 deletions lld/test/COFF/implib-machine.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# REQUIRES: x86
# RUN: split-file %s %t.dir
# RUN: llvm-lib -machine:i386 -out:%t.dir/test32.lib -def:%t.dir/test32.def
# RUN: llvm-lib -machine:amd64 -out:%t.dir/test64.lib -def:%t.dir/test64.def
# RUN: llvm-mc -triple i686-windows-msvc %t.dir/test.s -filetype=obj -o %t.dir/test32.obj
# RUN: llvm-mc -triple x86_64-windows-msvc %t.dir/test.s -filetype=obj -o %t.dir/test64.obj

# RUN: not lld-link -dll -noentry -out:%t32.dll %t.dir/test32.obj %t.dir/test64.lib 2>&1 | FileCheck --check-prefix=ERR32 %s
# ERR32: error: test.dll: machine type x64 conflicts with x86
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The machine type is on "test64.lib", so shouldn't that be used in the error message instead? Otherwise it can get pretty confusing for a user to track down where "test.dll" comes from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The machine type is a property of import file, which is what's printed here. Import file is a member or an archive ("test64.lib" in this case) and I agree that it would be more informative to print that information. That's what we do for other member types, import files are an exception in this case. It's easy to change, I will create a PR.


# RUN: not lld-link -dll -noentry -out:%t64.dll %t.dir/test64.obj %t.dir/test32.lib 2>&1 | FileCheck --check-prefix=ERR64 %s
# ERR64: error: test.dll: machine type x86 conflicts with x64

#--- test.s
.def @feat.00;
.scl 3;
.type 0;
.endef
.globl @feat.00
@feat.00 = 1
.data
.rva __imp__test

#--- test32.def
NAME test.dll
EXPORTS
test DATA

#--- test64.def
NAME test.dll
EXPORTS
_test DATA
Loading