47 changes: 47 additions & 0 deletions lld/test/MinGW/Inputs/imagebase-i386.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_I386
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 16
SectionData: 31C0C3666666662E0F1F84000000000031C0C3666666662E0F1F840000000000B800000000C3
symbols:
- Name: .text
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 38
NumberOfRelocations: 1
NumberOfLinenumbers: 0
CheckSum: 3189961473
Number: 1
- Name: _mainCRTStartup
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: _main
Value: 16
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: _func
Value: 32
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: __image_base__
Value: 0
SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...
47 changes: 47 additions & 0 deletions lld/test/MinGW/Inputs/imagebase-x86_64.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 16
SectionData: 31C0C3666666662E0F1F84000000000031C0C3666666662E0F1F840000000000488D0500000000C3
symbols:
- Name: .text
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 40
NumberOfRelocations: 1
NumberOfLinenumbers: 0
CheckSum: 3930888477
Number: 1
- Name: mainCRTStartup
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: main
Value: 16
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: func
Value: 32
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: __image_base__
Value: 0
SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...
52 changes: 52 additions & 0 deletions lld/test/MinGW/output.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# RUN: yaml2obj < %p/../COFF/Inputs/ret42.yaml > %t.obj

# RUN: rm -f a.exe a.dll

# RUN: ld.lld -m i386pep --entry main %t.obj
# RUN: llvm-readobj a.exe | FileCheck %s

# RUN: ld.lld -m i386pep -shared --entry main %t.obj
# RUN: llvm-readobj a.dll | FileCheck %s

# RUN: ld.lld -m i386pep -e main %t.obj -o %t.exe
# RUN: llvm-readobj %t.exe | FileCheck %s
CHECK: File:

# RUN: ld.lld -m i386pep --entry main %t.obj -o %t.exe --subsystem console
# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix CHECK-CONSOLE
CHECK-CONSOLE: Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI (0x3)

# RUN: ld.lld -m i386pep --entry main %t.obj -o %t.exe --subsystem windows
# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix CHECK-WINDOWS
CHECK-WINDOWS: Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI (0x2)

# RUN: ld.lld -m i386pep --entry main %t.obj -o %t.exe --stack 4194304,8192
# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix CHECK-STACK
CHECK-STACK: SizeOfStackReserve: 4194304
CHECK-STACK: SizeOfStackCommit: 8192

# RUN: yaml2obj < %p/Inputs/imagebase-i386.yaml > %t.obj
# RUN: ld.lld -m i386pe %t.obj -o %t.exe
# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix CHECK-I386
CHECK-I386: Machine: IMAGE_FILE_MACHINE_I386

# RUN: yaml2obj < %p/Inputs/imagebase-x86_64.yaml > %t.obj
# RUN: ld.lld -m i386pep %t.obj -o %t.exe
# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix CHECK-AMD64
CHECK-AMD64: Machine: IMAGE_FILE_MACHINE_AMD64

# RUN: yaml2obj < %p/Inputs/imagebase-arm.yaml > %t.obj
# RUN: ld.lld -m thumb2pe %t.obj -o %t.exe
# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix CHECK-ARMNT
CHECK-ARMNT: Machine: IMAGE_FILE_MACHINE_ARMNT

# RUN: yaml2obj < %p/Inputs/imagebase-aarch64.yaml > %t.obj
# RUN: ld.lld -m arm64pe %t.obj -o %t.exe
# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix CHECK-ARM64
CHECK-ARM64: Machine: IMAGE_FILE_MACHINE_ARM64

# RUN: yaml2obj < %p/../COFF/Inputs/export.yaml > %t.obj
# RUN: ld.lld -m i386pep --shared %t.obj -o %t.dll --out-implib %t.lib
# RUN: llvm-readobj %t.lib | FileCheck %s -check-prefix CHECK-IMPLIB
CHECK-IMPLIB: Symbol: __imp_exportfn3
CHECK-IMPLIB: Symbol: exportfn3
1 change: 1 addition & 0 deletions lld/tools/lld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_link_libraries(lld
lldDriver
lldCOFF
lldELF
lldMinGW
)

install(TARGETS lld
Expand Down
12 changes: 12 additions & 0 deletions lld/tools/lld/lld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ static Flavor getFlavor(StringRef S) {
.Default(Invalid);
}

static bool isPETarget(const std::vector<const char *> &V) {
for (auto It = V.begin(); It + 1 != V.end(); ++It) {
if (StringRef(*It) != "-m")
continue;
StringRef S = *(It + 1);
return S == "i386pe" || S == "i386pep" || S == "thumb2pe" || S == "arm64pe";
}
return false;
}

static Flavor parseProgname(StringRef Progname) {
#if __APPLE__
// Use Darwin driver for "ld" on Darwin.
Expand Down Expand Up @@ -101,6 +111,8 @@ int main(int Argc, const char **Argv) {
std::vector<const char *> Args(Argv, Argv + Argc);
switch (parseFlavor(Args)) {
case Gnu:
if (isPETarget(Args))
return !mingw::link(Args);
return !elf::link(Args, true);
case WinLink:
return !coff::link(Args);
Expand Down