diff --git a/lld/COFF/Config.h b/lld/COFF/Config.h index ae718d3f7032..1572e49c3a59 100644 --- a/lld/COFF/Config.h +++ b/lld/COFF/Config.h @@ -105,7 +105,7 @@ struct Configuration { // Used for /alternatename. std::map AlternateNames; - uint64_t ImageBase = 0x140000000U; + uint64_t ImageBase = -1; uint64_t StackReserve = 1024 * 1024; uint64_t StackCommit = 4096; uint64_t HeapReserve = 1024 * 1024; diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index 47fc54fb91ed..02bfdc9c4dc0 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -244,6 +244,12 @@ WindowsSubsystem LinkerDriver::inferSubsystem() { return IMAGE_SUBSYSTEM_UNKNOWN; } +static uint64_t getDefaultImageBase() { + if (Config->is64()) + return Config->DLL ? 0x180000000 : 0x140000000; + return Config->DLL ? 0x10000000 : 0x400000; +} + bool LinkerDriver::link(llvm::ArrayRef ArgsArr) { // Needed for LTO. llvm::InitializeAllTargetInfos(); @@ -311,7 +317,6 @@ bool LinkerDriver::link(llvm::ArrayRef ArgsArr) { // Handle /dll if (Args.hasArg(OPT_dll)) { Config->DLL = true; - Config->ImageBase = 0x180000000U; Config->ManifestID = 2; } @@ -602,6 +607,10 @@ bool LinkerDriver::link(llvm::ArrayRef ArgsArr) { } } + // Set default image base if /base is not given. + if (Config->ImageBase == uint64_t(-1)) + Config->ImageBase = getDefaultImageBase(); + Symtab.addRelative(mangle("__ImageBase"), 0); if (Config->MachineType == IMAGE_FILE_MACHINE_I386) { Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0); diff --git a/lld/test/COFF/armnt-blx23t.test b/lld/test/COFF/armnt-blx23t.test index cf358a2a436b..51016b0befb3 100644 --- a/lld/test/COFF/armnt-blx23t.test +++ b/lld/test/COFF/armnt-blx23t.test @@ -1,7 +1,6 @@ # RUN: yaml2obj < %s > %t.obj # RUN: llvm-objdump -d %t.obj | FileCheck %s -check-prefix BEFORE -# RUN: lld -flavor link2 /base:0x400000 /entry:function \ -# RUN: /subsystem:console /out:%t.exe %t.obj +# RUN: lld -flavor link2 /entry:function /subsystem:console /out:%t.exe %t.obj # RUN: llvm-objdump -d %t.exe | FileCheck %s -check-prefix AFTER # BEFORE: Disassembly of section .text: diff --git a/lld/test/COFF/armnt-branch24t.test b/lld/test/COFF/armnt-branch24t.test index f893ea4e82e7..97e3055b3c3d 100644 --- a/lld/test/COFF/armnt-branch24t.test +++ b/lld/test/COFF/armnt-branch24t.test @@ -1,7 +1,6 @@ # RUN: yaml2obj < %s > %t.obj # RUN: llvm-objdump -d %t.obj | FileCheck %s -check-prefix BEFORE -# RUN: lld -flavor link2 /base:0x400000 /entry:function \ -# RUN: /subsystem:console /out:%t.exe %t.obj +# RUN: lld -flavor link2 /entry:function /subsystem:console /out:%t.exe %t.obj # RUN: llvm-objdump -d %t.exe | FileCheck %s -check-prefix AFTER # BEFORE: Disassembly of section .text: diff --git a/lld/test/COFF/armnt-imports.test b/lld/test/COFF/armnt-imports.test index 4432eea34880..01d9ebc6d19d 100644 --- a/lld/test/COFF/armnt-imports.test +++ b/lld/test/COFF/armnt-imports.test @@ -1,6 +1,5 @@ # RUN: yaml2obj < %s > %t.obj -# RUN: lld -flavor link /out:%t.exe /base:0x400000 /subsystem:console \ -# RUN: %t.obj %p/Inputs/library.lib +# RUN: lld -flavor link /out:%t.exe /subsystem:console %t.obj %p/Inputs/library.lib # RUN: llvm-readobj -coff-imports %t.exe | FileCheck %s # CHECK: Import { diff --git a/lld/test/COFF/armnt-mov32t-exec.test b/lld/test/COFF/armnt-mov32t-exec.test index 8caf436a6757..ea3605ea823f 100644 --- a/lld/test/COFF/armnt-mov32t-exec.test +++ b/lld/test/COFF/armnt-mov32t-exec.test @@ -1,6 +1,6 @@ # RUN: yaml2obj < %s > %t.obj # RUN: llvm-objdump -d %t.obj | FileCheck %s -check-prefix BEFORE -# RUN: lld -flavor link /base:0x400000 /out:%t.exe /subsystem:console /entry:get_function %t.obj +# RUN: lld -flavor link /out:%t.exe /subsystem:console /entry:get_function %t.obj # RUN: llvm-objdump -d %t.exe | FileCheck %s -check-prefix AFTER # BEFORE: Disassembly of section .text: diff --git a/lld/test/COFF/armnt-movt32t.test b/lld/test/COFF/armnt-movt32t.test index 0bec67ff8085..a1db0addd868 100644 --- a/lld/test/COFF/armnt-movt32t.test +++ b/lld/test/COFF/armnt-movt32t.test @@ -1,6 +1,6 @@ # RUN: yaml2obj < %s > %t.obj # RUN: llvm-objdump -d %t.obj | FileCheck %s -check-prefix BEFORE -# RUN: lld -flavor link2 /base:0x400000 /entry:get_buffer /subsystem:console /out:%t.exe %t.obj +# RUN: lld -flavor link2 /entry:get_buffer /subsystem:console /out:%t.exe %t.obj # RUN: llvm-objdump -d %t.exe | FileCheck %s -check-prefix AFTER # BEFORE: Disassembly of section .text: diff --git a/lld/test/COFF/delayimports32.test b/lld/test/COFF/delayimports32.test index 062620f33499..f634114e3e0e 100644 --- a/lld/test/COFF/delayimports32.test +++ b/lld/test/COFF/delayimports32.test @@ -19,11 +19,11 @@ IMPORT-NEXT: BoundDelayImportTable: 0x0 IMPORT-NEXT: UnloadDelayImportTable: 0x0 IMPORT-NEXT: Import { IMPORT-NEXT: Symbol: ExitProcess (0) -IMPORT-NEXT: Address: 0x40002029 +IMPORT-NEXT: Address: 0x402029 IMPORT-NEXT: } IMPORT-NEXT: Import { IMPORT-NEXT: Symbol: MessageBoxA (0) -IMPORT-NEXT: Address: 0x1 +IMPORT-NEXT: Address: 0x0 IMPORT-NEXT: } IMPORT-NEXT: } @@ -70,16 +70,16 @@ BASEREL-NEXT: Address: 0x2046 BASEREL-NEXT: } BASEREL-NEXT: ] -DISASM: 202b: 68 20 10 00 40 pushl $1073745952 -DISASM-NEXT: 2030: 68 00 30 00 40 pushl $1073754112 +DISASM: 202b: 68 20 10 40 00 pushl $4198432 +DISASM-NEXT: 2030: 68 00 30 40 00 pushl $4206592 DISASM-NEXT: 2035: e8 c6 ff ff ff calll -58 <_main@0> DISASM-NEXT: 203a: 5a popl %edx DISASM-NEXT: 203b: 59 popl %ecx DISASM-NEXT: 203c: ff e0 jmpl *%eax DISASM-NEXT: 203e: 51 pushl %ecx DISASM-NEXT: 203f: 52 pushl %edx -DISASM-NEXT: 2040: 68 28 10 00 40 pushl $1073745960 -DISASM-NEXT: 2045: 68 00 30 00 40 pushl $1073754112 +DISASM-NEXT: 2040: 68 28 10 40 00 pushl $4198440 +DISASM-NEXT: 2045: 68 00 30 40 00 pushl $4206592 DISASM-NEXT: 204a: e8 b1 ff ff ff calll -79 <_main@0> DISASM-NEXT: 204f: 5a popl %edx DISASM-NEXT: 2050: 59 popl %ecx diff --git a/lld/test/COFF/hello32.test b/lld/test/COFF/hello32.test index 52f6c7192b0a..3f483409ff45 100644 --- a/lld/test/COFF/hello32.test +++ b/lld/test/COFF/hello32.test @@ -29,7 +29,7 @@ HEADER-NEXT: SizeOfUninitializedData: 0 HEADER-NEXT: AddressOfEntryPoint: 0x2000 HEADER-NEXT: BaseOfCode: 0x2000 HEADER-NEXT: BaseOfData: 0x0 -HEADER-NEXT: ImageBase: 0x40000000 +HEADER-NEXT: ImageBase: 0x400000 HEADER-NEXT: SectionAlignment: 4096 HEADER-NEXT: FileAlignment: 512 HEADER-NEXT: MajorOperatingSystemVersion: 6 diff --git a/lld/test/COFF/locally-imported32.test b/lld/test/COFF/locally-imported32.test index 8510a7c009d2..6dbd531d315b 100644 --- a/lld/test/COFF/locally-imported32.test +++ b/lld/test/COFF/locally-imported32.test @@ -6,7 +6,7 @@ # CHECK-NEXT: 1000 00200000 # CHECK: Contents of section .rdata: -# CHECK-NEXT: 2000 04100040 +# CHECK-NEXT: 2000 04104000 --- header: