1# REQUIRES: x86
2
3# RUN: echo -e ".global variable\n.global DllMainCRTStartup\n.text\nDllMainCRTStartup:\nret\n.data\nvariable:\n.long 42" > %t-lib.s
4# RUN: llvm-mc -triple=x86_64-windows-gnu %t-lib.s -filetype=obj -o %t-lib.obj
5# RUN: lld-link -out:%t-lib.dll -dll -entry:DllMainCRTStartup %t-lib.obj -lldmingw -implib:%t-lib.lib
6
7# RUN: llvm-mc -triple=x86_64-windows-gnu %s -defsym listptrs=1 -filetype=obj -o %t.obj
8# RUN: lld-link -lldmingw -out:%t.exe -entry:main %t.obj %t-lib.lib -verbose
9
10# RUN: llvm-readobj --coff-imports %t.exe | FileCheck -check-prefix=IMPORTS %s
11# RUN: llvm-objdump -d %t.exe | FileCheck --check-prefix=DISASM %s
12# RUN: llvm-objdump -s %t.exe | FileCheck --check-prefix=CONTENTS %s
13
14## Check that we can autoimport these variables with pseudo relocs disabled.
15# RUN: llvm-mc -triple=x86_64-windows-gnu %s -defsym listptrs=0 -filetype=obj -o %t.noptrs.obj
16# RUN: lld-link -lldmingw -runtime-pseudo-reloc:no -out:%t.exe -entry:main %t.noptrs.obj %t-lib.lib
17
18## Check that we can't autoimport them with autoimport disabled.
19# RUN: not lld-link -lldmingw -auto-import:no -out:%t.exe -entry:main %t.noptrs.obj %t-lib.lib 2>&1 | FileCheck --check-prefix=NO-AUTOIMPORT %s
20
21# IMPORTS: Import {
22# IMPORTS-NEXT: Name: autoimport-refptr.s.tmp-lib.dll
23# IMPORTS-NEXT: ImportLookupTableRVA: 0x2050
24# IMPORTS-NEXT: ImportAddressTableRVA: 0x2060
25# IMPORTS-NEXT: Symbol: variable (0)
26# IMPORTS-NEXT: }
27
28# DISASM: Disassembly of section .text:
29# DISASM-EMPTY:
30# DISASM: <.text>:
31# Relative offset at 0x1002 pointing at the IAT at 0x2060
32# DISASM: 140001000:      48 8b 05 59 10 00 00    movq    4185(%rip), %rax
33# DISASM: 140001007:      8b 00   movl    (%rax), %eax
34# Relative offset at 0x100b pointing at the .refptr.localvar stub at
35# 0x2000
36# DISASM: 140001009:      48 8b 0d f0 0f 00 00    movq    4080(%rip), %rcx
37# DISASM: 140001010:      03 01   addl    (%rcx), %eax
38# DISASM: 140001012:      c3      retq
39
40# relocs: pointing at an empty list of runtime pseudo relocs.
41# localvar: 42
42# CONTENTS: Contents of section .data:
43# CONTENTS:  140003000 08200040 01000000 08200040 01000000
44# CONTENTS:  140003010 2a000000
45
46# NO-AUTOIMPORT: error: undefined symbol: variable
47
48    .global main
49    .global localvar
50    .text
51main:
52    movq .refptr.variable(%rip), %rax
53    movl (%rax), %eax
54    movq .refptr.localvar(%rip), %rcx
55    addl (%rcx), %eax
56    ret
57
58    .data
59.if listptrs==1
60relocs:
61    .quad __RUNTIME_PSEUDO_RELOC_LIST__
62    .quad __RUNTIME_PSEUDO_RELOC_LIST_END__
63.endif
64localvar:
65    .int 42
66
67# Normally the compiler wouldn't emit a stub for a variable that is
68# emitted in the same translation unit.
69    .section .rdata$.refptr.localvar,"dr",discard,.refptr.localvar
70    .global .refptr.localvar
71.refptr.localvar:
72    .quad localvar
73
74    .section .rdata$.refptr.variable,"dr",discard,.refptr.variable
75    .global .refptr.variable
76.refptr.variable:
77    .quad variable
78