1# RUN: ld64.lld -arch x86_64 -r -print_atoms %s -o %t | FileCheck %s \
2# RUN:  && ld64.lld -arch x86_64 -r -print_atoms %t -o %t2 | FileCheck %s
3#
4# Test parsing and writing of x86_64 text relocations.
5#
6# The first step tests if the supplied mach-o file is parsed into the correct
7# set of references.  The second step verifies relocations can be round-tripped
8# by writing to a new .o file, then parsing that file which should result in
9# the same references.
10#
11#_test:
12#  call  _foo
13#  call  _foo+4
14#  movq  _foo@GOTPCREL(%rip), %rax
15#  pushq _foo@GOTPCREL(%rip)
16#  movl  _foo(%rip), %eax
17#  movl  _foo+4(%rip), %eax
18#  movb  $0x12, _foo(%rip)
19#  movw  $0x1234, _foo(%rip)
20#  movl  $0x12345678, _foo(%rip)
21#  movl  L2(%rip), %eax
22#  movb  $0x12, L2(%rip)
23#  movw  $0x1234, L2(%rip)
24#  movl  $0x12345678, L2(%rip)
25#
26#  .data
27#L2: .long 0
28
29
30--- !mach-o
31arch:            x86_64
32file-type:       MH_OBJECT
33flags:           [  ]
34sections:
35  - segment:         __TEXT
36    section:         __text
37    type:            S_REGULAR
38    attributes:      [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ]
39    address:         0x0000000000000000
40    content:         [ 0xE8, 0x00, 0x00, 0x00, 0x00, 0xE8, 0x04, 0x00,
41                       0x00, 0x00, 0x48, 0x8B, 0x05, 0x04, 0x00, 0x00,
42                       0x00, 0xFF, 0x35, 0x04, 0x00, 0x00, 0x00, 0x8B,
43                       0x05, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x05, 0x04,
44                       0x00, 0x00, 0x00, 0xC6, 0x05, 0xFF, 0xFF, 0xFF,
45                       0xFF, 0x12, 0x66, 0xC7, 0x05, 0xFE, 0xFF, 0xFF,
46                       0xFF, 0x34, 0x12, 0xC7, 0x05, 0xFC, 0xFF, 0xFF,
47                       0xFF, 0x78, 0x56, 0x34, 0x12, 0x8B, 0x05, 0x1A,
48                       0x00, 0x00, 0x00, 0xc6, 0x05, 0x13, 0x00, 0x00,
49		       0x00, 0x12, 0x66, 0xc7, 0x05, 0x0a, 0x00, 0x00,
50		       0x00, 0x34, 0x12, 0xc7, 0x05, 0x00, 0x00, 0x00,
51		       0x00, 0x78, 0x56, 0x34, 0x12 ]
52    relocations:
53      - offset:          0x00000055
54        type:            X86_64_RELOC_SIGNED_4
55        length:          2
56        pc-rel:          true
57        extern:          false
58        symbol:          2
59      - offset:          0x0000004d
60        type:            X86_64_RELOC_SIGNED_2
61        length:          2
62        pc-rel:          true
63        extern:          false
64        symbol:          2
65      - offset:          0x00000045
66        type:            X86_64_RELOC_SIGNED_1
67        length:          2
68        pc-rel:          true
69        extern:          false
70        symbol:          2
71      - offset:          0x0000003F
72        type:            X86_64_RELOC_SIGNED
73        length:          2
74        pc-rel:          true
75        extern:          false
76        symbol:          2
77      - offset:          0x00000035
78        type:            X86_64_RELOC_SIGNED_4
79        length:          2
80        pc-rel:          true
81        extern:          true
82        symbol:          1
83      - offset:          0x0000002D
84        type:            X86_64_RELOC_SIGNED_2
85        length:          2
86        pc-rel:          true
87        extern:          true
88        symbol:          1
89      - offset:          0x00000025
90        type:            X86_64_RELOC_SIGNED_1
91        length:          2
92        pc-rel:          true
93        extern:          true
94        symbol:          1
95      - offset:          0x0000001F
96        type:            X86_64_RELOC_SIGNED
97        length:          2
98        pc-rel:          true
99        extern:          true
100        symbol:          1
101      - offset:          0x00000019
102        type:            X86_64_RELOC_SIGNED
103        length:          2
104        pc-rel:          true
105        extern:          true
106        symbol:          1
107      - offset:          0x00000013
108        type:            X86_64_RELOC_GOT
109        length:          2
110        pc-rel:          true
111        extern:          true
112        symbol:          1
113      - offset:          0x0000000D
114        type:            X86_64_RELOC_GOT_LOAD
115        length:          2
116        pc-rel:          true
117        extern:          true
118        symbol:          1
119      - offset:          0x00000006
120        type:            X86_64_RELOC_BRANCH
121        length:          2
122        pc-rel:          true
123        extern:          true
124        symbol:          1
125      - offset:          0x00000001
126        type:            X86_64_RELOC_BRANCH
127        length:          2
128        pc-rel:          true
129        extern:          true
130        symbol:          1
131  - segment:         __DATA
132    section:         __data
133    type:            S_REGULAR
134    attributes:      [  ]
135    address:         0x000000000000005D
136    content:         [ 0x00, 0x00, 0x00, 0x00 ]
137local-symbols:
138  - name:            _test
139    type:            N_SECT
140    sect:            1
141    value:           0x0000000000000000
142undefined-symbols:
143  - name:            _foo
144    type:            N_UNDF
145    scope:           [ N_EXT ]
146    value:           0x0000000000000000
147...
148
149# CHECK: defined-atoms:
150# CHECK:   - ref-name:        [[LABEL:L[0-9]+]]
151# CHECK:     type:            data
152# CHECK:     content:         [ 00, 00, 00, 00 ]
153# CHECK:  - name:            _test
154# CHECK:    references:
155# CHECK:      - kind:            branch32
156# CHECK:        offset:          1
157# CHECK:        target:          _foo
158# CHECK:      - kind:            branch32
159# CHECK:        offset:          6
160# CHECK:        target:          _foo
161# CHECK:        addend:          4
162# CHECK:      - kind:            ripRel32GotLoad
163# CHECK:        offset:          13
164# CHECK:        target:          _foo
165# CHECK:        addend:          4
166# CHECK:      - kind:            ripRel32Got
167# CHECK:        offset:          19
168# CHECK:        target:          _foo
169# CHECK:        addend:          4
170# CHECK:      - kind:            ripRel32
171# CHECK:        offset:          25
172# CHECK:        target:          _foo
173# CHECK:      - kind:            ripRel32
174# CHECK:        offset:          31
175# CHECK:        target:          _foo
176# CHECK:        addend:          4
177# CHECK:      - kind:            ripRel32Minus1
178# CHECK:        offset:          37
179# CHECK:        target:          _foo
180# CHECK-NOT:    addend:
181# CHECK:      - kind:            ripRel32Minus2
182# CHECK:        offset:          45
183# CHECK:        target:          _foo
184# CHECK-NOT:    addend:
185# CHECK:      - kind:            ripRel32Minus4
186# CHECK:        offset:          53
187# CHECK:        target:          _foo
188# CHECK-NOT:    addend:
189# CHECK:      - kind:            ripRel32Anon
190# CHECK:        offset:          63
191# CHECK:        target:          [[LABEL]]
192# CHECK-NOT:    addend:
193# CHECK:      - kind:            ripRel32Minus1Anon
194# CHECK:        offset:          69
195# CHECK:        target:          [[LABEL]]
196# CHECK-NOT:    addend:
197# CHECK:      - kind:            ripRel32Minus2Anon
198# CHECK:        offset:          77
199# CHECK:        target:          [[LABEL]]
200# CHECK-NOT:    addend:
201# CHECK:      - kind:            ripRel32Minus4Anon
202# CHECK:        offset:          85
203# CHECK:        target:          [[LABEL]]
204# CHECK-NOT:    addend:
205