1# RUN: ld64.lld.darwinold -arch i386 -r -print_atoms %s -o %t  | FileCheck %s \
2# RUN: && ld64.lld.darwinold -arch i386 -r -print_atoms %t -o %t2  | FileCheck %s
3#
4# Test parsing and writing of x86 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#  .text
12#_test:
13#  	call _undef
14#  	call _undef+2
15#  	call _foo
16#  	call _foo+2
17#   callw _undef
18#  	callw _foo
19#  	callw _foo+2
20#L1:
21#  	movl	_undef, %eax
22#   movl	_x, %eax
23#   movl	_x+4, %eax
24#  	movl	_x-L1(%eax), %eax
25#  	movl	_x+4-L1(%eax), %eax
26#
27#_foo:
28#    ret
29#
30#  .data
31#_x:
32#  .long _undef
33#  .long _undef+7
34#  .long _foo
35#  .long _foo+3
36#  .long _test - .
37#  .long _test+3 - .
38#
39
40--- !mach-o
41arch:            x86
42file-type:       MH_OBJECT
43flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
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:         [ 0xE8, 0xFB, 0xFF, 0xFF, 0xFF, 0xE8, 0xF8, 0xFF,
52                       0xFF, 0xFF, 0xE8, 0x2C, 0x00, 0x00, 0x00, 0xE8,
53                       0x29, 0x00, 0x00, 0x00, 0x66, 0xE8, 0xE8, 0xFF,
54                       0x66, 0xE8, 0x1F, 0x00, 0x66, 0xE8, 0x1D, 0x00,
55                       0xA1, 0x00, 0x00, 0x00, 0x00, 0xA1, 0x3C, 0x00,
56                       0x00, 0x00, 0xA1, 0x40, 0x00, 0x00, 0x00, 0x8B,
57                       0x80, 0x1C, 0x00, 0x00, 0x00, 0x8B, 0x80, 0x20,
58                       0x00, 0x00, 0x00, 0xC3 ]
59    relocations:
60      - offset:          0x00000037
61        scattered:       true
62        type:            GENERIC_RELOC_LOCAL_SECTDIFF
63        length:          2
64        pc-rel:          false
65        value:           0x0000003C
66      - offset:          0x00000000
67        scattered:       true
68        type:            GENERIC_RELOC_PAIR
69        length:          2
70        pc-rel:          false
71        value:           0x00000020
72      - offset:          0x00000031
73        scattered:       true
74        type:            GENERIC_RELOC_LOCAL_SECTDIFF
75        length:          2
76        pc-rel:          false
77        value:           0x0000003C
78      - offset:          0x00000000
79        scattered:       true
80        type:            GENERIC_RELOC_PAIR
81        length:          2
82        pc-rel:          false
83        value:           0x00000020
84      - offset:          0x0000002B
85        scattered:       true
86        type:            GENERIC_RELOC_VANILLA
87        length:          2
88        pc-rel:          false
89        value:           0x0000003C
90      - offset:          0x00000026
91        type:            GENERIC_RELOC_VANILLA
92        length:          2
93        pc-rel:          false
94        extern:          false
95        symbol:          2
96      - offset:          0x00000021
97        type:            GENERIC_RELOC_VANILLA
98        length:          2
99        pc-rel:          false
100        extern:          true
101        symbol:          3
102      - offset:          0x0000001E
103        scattered:       true
104        type:            GENERIC_RELOC_VANILLA
105        length:          1
106        pc-rel:          true
107        value:           0x0000003B
108      - offset:          0x0000001A
109        type:            GENERIC_RELOC_VANILLA
110        length:          1
111        pc-rel:          true
112        extern:          false
113        symbol:          1
114      - offset:          0x00000016
115        type:            GENERIC_RELOC_VANILLA
116        length:          1
117        pc-rel:          true
118        extern:          true
119        symbol:          3
120      - offset:          0x00000010
121        scattered:       true
122        type:            GENERIC_RELOC_VANILLA
123        length:          2
124        pc-rel:          true
125        value:           0x0000003B
126      - offset:          0x0000000B
127        type:            GENERIC_RELOC_VANILLA
128        length:          2
129        pc-rel:          true
130        extern:          false
131        symbol:          1
132      - offset:          0x00000006
133        type:            GENERIC_RELOC_VANILLA
134        length:          2
135        pc-rel:          true
136        extern:          true
137        symbol:          3
138      - offset:          0x00000001
139        type:            GENERIC_RELOC_VANILLA
140        length:          2
141        pc-rel:          true
142        extern:          true
143        symbol:          3
144  - segment:         __DATA
145    section:         __data
146    type:            S_REGULAR
147    attributes:      [  ]
148    address:         0x000000000000003C
149    content:         [ 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
150                       0x3B, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00,
151                       0xB4, 0xFF, 0xFF, 0xFF, 0xB3, 0xFF, 0xFF, 0xFF ]
152    relocations:
153      - offset:          0x00000014
154        scattered:       true
155        type:            GENERIC_RELOC_LOCAL_SECTDIFF
156        length:          2
157        pc-rel:          false
158        value:           0x00000000
159      - offset:          0x00000000
160        scattered:       true
161        type:            GENERIC_RELOC_PAIR
162        length:          2
163        pc-rel:          false
164        value:           0x00000050
165      - offset:          0x00000010
166        scattered:       true
167        type:            GENERIC_RELOC_LOCAL_SECTDIFF
168        length:          2
169        pc-rel:          false
170        value:           0x00000000
171      - offset:          0x00000000
172        scattered:       true
173        type:            GENERIC_RELOC_PAIR
174        length:          2
175        pc-rel:          false
176        value:           0x0000004C
177      - offset:          0x0000000C
178        scattered:       true
179        type:            GENERIC_RELOC_VANILLA
180        length:          2
181        pc-rel:          false
182        value:           0x0000003B
183      - offset:          0x00000008
184        type:            GENERIC_RELOC_VANILLA
185        length:          2
186        pc-rel:          false
187        extern:          false
188        symbol:          1
189      - offset:          0x00000004
190        type:            GENERIC_RELOC_VANILLA
191        length:          2
192        pc-rel:          false
193        extern:          true
194        symbol:          3
195      - offset:          0x00000000
196        type:            GENERIC_RELOC_VANILLA
197        length:          2
198        pc-rel:          false
199        extern:          true
200        symbol:          3
201local-symbols:
202  - name:            _test
203    type:            N_SECT
204    sect:            1
205    value:           0x0000000000000000
206  - name:            _foo
207    type:            N_SECT
208    sect:            1
209    value:           0x000000000000003B
210  - name:            _x
211    type:            N_SECT
212    sect:            2
213    value:           0x000000000000003C
214undefined-symbols:
215  - name:            _undef
216    type:            N_UNDF
217    scope:           [ N_EXT ]
218    value:           0x0000000000000000
219...
220
221# CHECK: defined-atoms:
222# CHECK:   - name:            _x
223# CHECK:     type:            data
224# CHECK:     references:
225# CHECK:       - kind:            pointer32
226# CHECK:         offset:          0
227# CHECK:         target:          _undef
228# CHECK-NOT:     addend:
229# CHECK:       - kind:            pointer32
230# CHECK:         offset:          4
231# CHECK:         target:          _undef
232# CHECK:         addend:          7
233# CHECK:       - kind:            pointer32
234# CHECK:         offset:          8
235# CHECK:         target:          _foo
236# CHECK-NOT:     addend:
237# CHECK:       - kind:            pointer32
238# CHECK:         offset:          12
239# CHECK:         target:          _foo
240# CHECK:         addend:          3
241# CHECK:       - kind:            delta32
242# CHECK:         offset:          16
243# CHECK:         target:          _test
244# CHECK:       - kind:            delta32
245# CHECK:         offset:          20
246# CHECK:         target:          _test
247# CHECK:         addend:          3
248# CHECK:  - name:            _test
249# CHECK:    references:
250# CHECK:      - kind:            branch32
251# CHECK:        offset:          1
252# CHECK:        target:          _undef
253# CHECK-NOT:    addend:
254# CHECK:      - kind:            branch32
255# CHECK:        offset:          6
256# CHECK:        target:          _undef
257# CHECK:        addend:          2
258# CHECK:      - kind:            branch32
259# CHECK:        offset:          11
260# CHECK:        target:          _foo
261# CHECK-NOT:    addend:
262# CHECK:      - kind:            branch32
263# CHECK:        offset:          16
264# CHECK:        target:          _foo
265# CHECK:        addend:          2
266# CHECK:      - kind:            branch16
267# CHECK:        offset:          22
268# CHECK:        target:          _undef
269# CHECK-NOT:    addend:
270# CHECK:      - kind:            branch16
271# CHECK:        offset:          26
272# CHECK:        target:          _foo
273# CHECK-NOT:    addend:
274# CHECK:      - kind:            branch16
275# CHECK:        offset:          30
276# CHECK:        target:          _foo
277# CHECK:        addend:          2
278# CHECK:      - kind:            abs32
279# CHECK:        offset:          33
280# CHECK:        target:          _undef
281# CHECK:      - kind:            abs32
282# CHECK:        offset:          38
283# CHECK:        target:          _x
284# CHECK:      - kind:            abs32
285# CHECK:        offset:          43
286# CHECK:        target:          _x
287# CHECK:        addend:          4
288# CHECK:      - kind:            funcRel32
289# CHECK:        offset:          49
290# CHECK:        target:          _x
291# CHECK:        addend:          -32
292# CHECK:      - kind:            funcRel32
293# CHECK:        offset:          55
294# CHECK:        target:          _x
295# CHECK:        addend:          -28
296
297