Skip to content

Commit 64be2e2

Browse files
committed
[LLD][COFF] Validate import library machine type.
1 parent 4068e72 commit 64be2e2

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,13 @@ void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
987987
ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
988988
: InputFile(ctx, ImportKind, m), live(!ctx.config.doGC), thunkLive(live) {}
989989

990+
MachineTypes ImportFile::getMachineType() const {
991+
uint16_t machine =
992+
reinterpret_cast<const coff_import_header *>(mb.getBufferStart())
993+
->Machine;
994+
return MachineTypes(machine);
995+
}
996+
990997
void ImportFile::parse() {
991998
const auto *hdr =
992999
reinterpret_cast<const coff_import_header *>(mb.getBufferStart());

lld/COFF/InputFiles.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ class ImportFile : public InputFile {
344344
explicit ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m);
345345

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

348349
Symbol *impSym = nullptr;
349350
Symbol *thunkSym = nullptr;

lld/test/COFF/implib-machine.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# REQUIRES: x86
2+
# RUN: split-file %s %t.dir
3+
# RUN: llvm-lib -machine:i386 -out:%t.dir/test32.lib -def:%t.dir/test32.def
4+
# RUN: llvm-lib -machine:amd64 -out:%t.dir/test64.lib -def:%t.dir/test64.def
5+
# RUN: llvm-mc -triple i686-windows-msvc %t.dir/test.s -filetype=obj -o %t.dir/test32.obj
6+
# RUN: llvm-mc -triple x86_64-windows-msvc %t.dir/test.s -filetype=obj -o %t.dir/test64.obj
7+
8+
# 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: test.dll: machine type x64 conflicts with x86
10+
11+
# 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: test.dll: machine type x86 conflicts with x64
13+
14+
#--- test.s
15+
.def @feat.00;
16+
.scl 3;
17+
.type 0;
18+
.endef
19+
.globl @feat.00
20+
@feat.00 = 1
21+
.data
22+
.rva __imp__test
23+
24+
#--- test32.def
25+
NAME test.dll
26+
EXPORTS
27+
test DATA
28+
29+
#--- test64.def
30+
NAME test.dll
31+
EXPORTS
32+
_test DATA

0 commit comments

Comments
 (0)