1
2# RUN: ld64.lld -arch x86_64 -r %s -o %t -print_atoms | FileCheck %s \
3# RUN: && ld64.lld -arch x86_64 %t -r -print_atoms -o %t2  | FileCheck %s
4#
5# Test parsing and writing of x86_64 data relocations.
6#
7# The first step tests if the supplied mach-o file is parsed into the correct
8# set of references.  The second step verifies relocations can be round-tripped
9# by writing to a new .o file, then parsing that file which should result in
10# the same references.
11#
12#_foo:
13#  ret
14#
15#_bar:
16#  ret
17#
18#  .section __DATA,__custom
19#L1:
20#  .quad 0
21#
22#  .data
23#_d:
24#  .quad   _foo
25#  .quad   _foo+4
26#  .quad   _foo - .
27#  .quad   L1
28#  .quad   L1 + 2
29#  .quad   _foo - .
30#  .quad   _foo + 4 - .
31#  .quad   L1 - .
32#  .long   _foo - .
33#  .long   _foo + 4 - .
34#  .long   L1 - .
35#
36
37--- !mach-o
38arch:            x86_64
39file-type:       MH_OBJECT
40flags:           [  ]
41compat-version:  0.0
42current-version: 0.0
43has-UUID:        false
44OS:              unknown
45sections:
46  - segment:         __TEXT
47    section:         __text
48    type:            S_REGULAR
49    attributes:      [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ]
50    address:         0x0000000000000000
51    content:         [ 0xC3, 0xC3 ]
52  - segment:         __DATA
53    section:         __custom
54    type:            S_REGULAR
55    attributes:      [  ]
56    address:         0x0000000000000002
57    content:         [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]
58  - segment:         __DATA
59    section:         __data
60    type:            S_REGULAR
61    attributes:      [  ]
62    address:         0x000000000000000A
63    content:         [
64#  .quad   _foo
65# No addend is needed here as we are referencing _foo directly and that is
66# encoded entirely in the X86_64_RELOC_UNSIGNED
67                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68#  .quad   _foo+4
69# Addend of 4 is needed here as we are referencing _foo from the
70# X86_64_RELOC_UNSIGNED, then the addend gives us 4 more.
71                       0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72#  .quad   _foo - .
73# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
74# The subtractor references _d which is the first nonlocal label in this
75# section.  The unsigned references _foo.
76# Note the addend here is -16 because that is the offset from here back
77# to _d.
78                       0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
79#  .quad   . - _foo
80# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
81# The subtractor references _d which is the first nonlocal label in this
82# section.  The unsigned references _foo.
83# Note the addend here is -16 because that is the offset from here back
84# to _d.
85                       0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86#  .quad   L1
87# This is a X86_64_RELOC_UNSIGNED without extern set.
88# In this case, we encode the section number for L1 in the relocation, and
89# the addend here is the absolute address of the location in that section
90# we want to reference.
91                       0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92#  .quad   L1 + 2
93# This is a X86_64_RELOC_UNSIGNED without extern set.
94# In this case, we encode the section number for L1 in the relocation, and
95# the addend here is the absolute address of the location in that section
96# we want to reference.  We have a 4 because the section is at address 2
97# and we want an offset of 2 from there.
98                       0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99#  .quad   _foo - .
100# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
101# The subtractor references _d which is the first nonlocal label in this
102# section.  The unsigned references _foo.
103# Note the addend here is -40 because that is the offset from here back
104# to _d.
105                       0xD0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
106#  .quad   _foo + 4 - .
107# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
108# The subtractor references _d which is the first nonlocal label in this
109# section.  The unsigned references _foo.
110# Note the addend here is -52.  It would have been -56 because that
111# would take us from the address of this relocation back to _d.  But as
112# we also add 4 for the offset, we get -52.
113                       0xCC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
114#  .quad   L1 - .
115# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
116# The subtractor references _d which is the first nonlocal label in this
117# section.  The unsigned does not have extern set, so the relocation
118# number is the section number for L1.
119# Note the addend here is -62. Of that, -64 would be the offset from
120# this location from _d.  The remaining 2 is the absolute address
121# of L1.
122                       0xC2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
123#  .long   _foo - .
124# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
125# The subtractor references _d which is the first nonlocal label in this
126# section.  The unsigned references _foo.
127# Note the addend here is -72 because that is the offset from here back
128# to _d.
129                       0xB8, 0xFF, 0xFF, 0xFF,
130#  .long   . - _foo
131# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
132# The subtractor references _d which is the first nonlocal label in this
133# section.  The unsigned references _foo.
134# Note the addend here is -76 because that is the offset from here back
135# to _d.
136                       0xB4, 0xFF, 0xFF, 0xFF,
137#  .long   _foo + 4 - .
138# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
139# The subtractor references _d which is the first nonlocal label in this
140# section.  The unsigned references _foo.
141# Note the addend here is -76.  It would have been -80 because that
142# would take us from the address of this relocation back to _d.  But as
143# we also add 4 for the offset, we get -76.
144                       0xB4, 0xFF, 0xFF, 0xFF,
145#  .long   L1 - .
146# This is the pair X86_64_RELOC_SUBTRACTOR and X86_64_RELOC_UNSIGNED.
147# The subtractor references _d which is the first nonlocal label in this
148# section.  The unsigned does not have extern set, so the relocation
149# number is the section number for L1.
150# Note the addend here is -82. Of that, -84 would be the offset from
151# this location from _d.  The remaining 2 is the absolute address
152# of L1.
153                       0xAE, 0xFF, 0xFF, 0xFF ]
154    relocations:
155      - offset:          0x00000054
156        type:            X86_64_RELOC_SUBTRACTOR
157        length:          2
158        pc-rel:          false
159        extern:          true
160        symbol:          2
161      - offset:          0x00000054
162        type:            X86_64_RELOC_UNSIGNED
163        length:          2
164        pc-rel:          false
165        extern:          false
166        symbol:          2
167      - offset:          0x00000050
168        type:            X86_64_RELOC_SUBTRACTOR
169        length:          2
170        pc-rel:          false
171        extern:          true
172        symbol:          2
173      - offset:          0x00000050
174        type:            X86_64_RELOC_UNSIGNED
175        length:          2
176        pc-rel:          false
177        extern:          true
178        symbol:          0
179      - offset:          0x0000004C
180        type:            X86_64_RELOC_SUBTRACTOR
181        length:          2
182        pc-rel:          false
183        extern:          true
184        symbol:          0
185      - offset:          0x0000004C
186        type:            X86_64_RELOC_UNSIGNED
187        length:          2
188        pc-rel:          false
189        extern:          true
190        symbol:          2
191      - offset:          0x00000048
192        type:            X86_64_RELOC_SUBTRACTOR
193        length:          2
194        pc-rel:          false
195        extern:          true
196        symbol:          2
197      - offset:          0x00000048
198        type:            X86_64_RELOC_UNSIGNED
199        length:          2
200        pc-rel:          false
201        extern:          true
202        symbol:          0
203      - offset:          0x00000040
204        type:            X86_64_RELOC_SUBTRACTOR
205        length:          3
206        pc-rel:          false
207        extern:          true
208        symbol:          2
209      - offset:          0x00000040
210        type:            X86_64_RELOC_UNSIGNED
211        length:          3
212        pc-rel:          false
213        extern:          false
214        symbol:          2
215      - offset:          0x00000038
216        type:            X86_64_RELOC_SUBTRACTOR
217        length:          3
218        pc-rel:          false
219        extern:          true
220        symbol:          2
221      - offset:          0x00000038
222        type:            X86_64_RELOC_UNSIGNED
223        length:          3
224        pc-rel:          false
225        extern:          true
226        symbol:          0
227      - offset:          0x00000030
228        type:            X86_64_RELOC_SUBTRACTOR
229        length:          3
230        pc-rel:          false
231        extern:          true
232        symbol:          2
233      - offset:          0x00000030
234        type:            X86_64_RELOC_UNSIGNED
235        length:          3
236        pc-rel:          false
237        extern:          true
238        symbol:          0
239      - offset:          0x00000028
240        type:            X86_64_RELOC_UNSIGNED
241        length:          3
242        pc-rel:          false
243        extern:          false
244        symbol:          2
245      - offset:          0x00000020
246        type:            X86_64_RELOC_UNSIGNED
247        length:          3
248        pc-rel:          false
249        extern:          false
250        symbol:          2
251      - offset:          0x00000018
252        type:            X86_64_RELOC_SUBTRACTOR
253        length:          3
254        pc-rel:          false
255        extern:          true
256        symbol:          0
257      - offset:          0x00000018
258        type:            X86_64_RELOC_UNSIGNED
259        length:          3
260        pc-rel:          false
261        extern:          true
262        symbol:          2
263      - offset:          0x00000010
264        type:            X86_64_RELOC_SUBTRACTOR
265        length:          3
266        pc-rel:          false
267        extern:          true
268        symbol:          2
269      - offset:          0x00000010
270        type:            X86_64_RELOC_UNSIGNED
271        length:          3
272        pc-rel:          false
273        extern:          true
274        symbol:          0
275      - offset:          0x00000008
276        type:            X86_64_RELOC_UNSIGNED
277        length:          3
278        pc-rel:          false
279        extern:          true
280        symbol:          0
281      - offset:          0x00000000
282        type:            X86_64_RELOC_UNSIGNED
283        length:          3
284        pc-rel:          false
285        extern:          true
286        symbol:          0
287local-symbols:
288  - name:            _foo
289    type:            N_SECT
290    sect:            1
291    value:           0x0000000000000000
292  - name:            _bar
293    type:            N_SECT
294    sect:            1
295    value:           0x0000000000000001
296  - name:            _d
297    type:            N_SECT
298    sect:            3
299    value:           0x000000000000000A
300page-size:       0x00000000
301...
302
303
304# CHECK:defined-atoms:
305# CHECK:  - name:            _d
306# CHECK:    type:            data
307# CHECK:    content:         [ 00, 00, 00, 00, 00, 00, 00, 00, 04, 00, 00, 00,
308# CHECK:                       00, 00, 00, 00, F0, FF, FF, FF, FF, FF, FF, FF,
309# CHECK:                       18, 00, 00, 00, 00, 00, 00, 00, {{..}}, {{..}}, 00, 00,
310# CHECK:                       00, 00, 00, 00, {{..}}, {{..}}, 00, 00, 00, 00, 00, 00,
311# CHECK:                       D0, FF, FF, FF, FF, FF, FF, FF, CC, FF, FF, FF,
312# CHECK:                       FF, FF, FF, FF, {{..}}, {{..}}, {{..}}, {{..}}, {{..}}, {{..}}, {{..}}, {{..}},
313# CHECK:                       B8, FF, FF, FF, B4, FF, FF, FF, B4, FF, FF, FF,
314# CHECK:                       {{..}}, {{..}}, {{..}}, {{..}} ]
315# CHECK:    dead-strip:      never
316# CHECK:    references:
317# CHECK:      - kind:            pointer64
318# CHECK:        offset:          0
319# CHECK:        target:          _foo
320# CHECK:      - kind:            pointer64
321# CHECK:        offset:          8
322# CHECK:        target:          _foo
323# CHECK:        addend:          4
324# CHECK:      - kind:            delta64
325# CHECK:        offset:          16
326# CHECK:        target:          _foo
327# CHECK:      - kind:            negDelta64
328# CHECK:        offset:          24
329# CHECK:        target:          _foo
330# CHECK:      - kind:            pointer64Anon
331# CHECK:        offset:          32
332# CHECK:        target:          L003
333# CHECK:      - kind:            pointer64Anon
334# CHECK:        offset:          40
335# CHECK:        target:          L003
336# CHECK:        addend:          2
337# CHECK:      - kind:            delta64
338# CHECK:        offset:          48
339# CHECK:        target:          _foo
340# CHECK:      - kind:            delta64
341# CHECK:        offset:          56
342# CHECK:        target:          _foo
343# CHECK:        addend:          4
344# CHECK:      - kind:            delta64Anon
345# CHECK:        offset:          64
346# CHECK:        target:          L003
347# CHECK:      - kind:            delta32
348# CHECK:        offset:          72
349# CHECK:        target:          _foo
350# CHECK:      - kind:            negDelta32
351# CHECK:        offset:          76
352# CHECK:        target:          _foo
353# CHECK:      - kind:            delta32
354# CHECK:        offset:          80
355# CHECK:        target:          _foo
356# CHECK:        addend:          4
357# CHECK:      - kind:            delta32Anon
358# CHECK:        offset:          84
359# CHECK:        target:          L003
360# CHECK:  - name:            _foo
361# CHECK:    content:         [ C3 ]
362# CHECK:    dead-strip:      never
363# CHECK:  - name:            _bar
364# CHECK:    content:         [ C3 ]
365# CHECK:    dead-strip:      never
366# CHECK:  - ref-name:        L003
367# CHECK:    type:            unknown
368# CHECK:    content:         [ 00, 00, 00, 00, 00, 00, 00, 00 ]
369# CHECK:    section-choice:  custom-required
370# CHECK:    section-name:    '__DATA/__custom'
371# CHECK:    dead-strip:      never
372
373