blob: 9655adcb63071e578a4a9dde20989d627babb43d [file] [log] [blame]
Nico Rieck1da45292013-04-10 23:28:17 +00001===============
2LLVM Extensions
3===============
4
5.. contents::
6 :local:
Nico Rieck1da45292013-04-10 23:28:17 +00007
8.. toctree::
9 :hidden:
10
11Introduction
12============
13
14This document describes extensions to tools and formats LLVM seeks compatibility
15with.
16
Tim Northover09376792013-08-14 15:27:20 +000017General Assembly Syntax
18===========================
19
20C99-style Hexadecimal Floating-point Constants
21----------------------------------------------
22
23LLVM's assemblers allow floating-point constants to be written in C99's
24hexadecimal format instead of decimal if desired.
25
26.. code-block:: gas
Benjamin Kramer24ab6b32013-08-14 16:18:47 +000027
Tim Northover09376792013-08-14 15:27:20 +000028 .section .data
29 .float 0x1c2.2ap3
30
Nico Rieck1da45292013-04-10 23:28:17 +000031Machine-specific Assembly Syntax
32================================
33
34X86/COFF-Dependent
35------------------
36
Nico Riecka37acf72013-07-06 12:13:10 +000037Relocations
38^^^^^^^^^^^
39
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +000040The following additional relocation types are supported:
Nico Rieck1da45292013-04-10 23:28:17 +000041
42**@IMGREL** (AT&T syntax only) generates an image-relative relocation that
43corresponds to the COFF relocation types ``IMAGE_REL_I386_DIR32NB`` (32-bit) or
44``IMAGE_REL_AMD64_ADDR32NB`` (64-bit).
45
Renato Golin124f2592016-07-20 12:16:38 +000046.. code-block:: text
Nico Rieck1da45292013-04-10 23:28:17 +000047
48 .text
49 fun:
50 mov foo@IMGREL(%ebx, %ecx, 4), %eax
51
52 .section .pdata
53 .long fun@IMGREL
54 .long (fun@imgrel + 0x3F)
55 .long $unwind$fun@imgrel
Nico Riecka37acf72013-07-06 12:13:10 +000056
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +000057**.secrel32** generates a relocation that corresponds to the COFF relocation
58types ``IMAGE_REL_I386_SECREL`` (32-bit) or ``IMAGE_REL_AMD64_SECREL`` (64-bit).
59
60**.secidx** relocation generates an index of the section that contains
61the target. It corresponds to the COFF relocation types
62``IMAGE_REL_I386_SECTION`` (32-bit) or ``IMAGE_REL_AMD64_SECTION`` (64-bit).
63
Aaron Ballmanb3c51512017-01-17 21:48:31 +000064.. code-block:: none
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +000065
66 .section .debug$S,"rn"
67 .long 4
68 .long 242
69 .long 40
Keno Fischerf7d84ee2017-01-02 03:00:19 +000070 .secrel32 _function_name + 0
Timur Iskhodzhanovc1fb2d62013-12-20 18:15:00 +000071 .secidx _function_name
72 ...
Nico Riecka37acf72013-07-06 12:13:10 +000073
74``.linkonce`` Directive
75^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76
77Syntax:
78
Rafael Espindola0766ae02014-06-06 19:26:12 +000079 ``.linkonce [ comdat type ]``
Nico Riecka37acf72013-07-06 12:13:10 +000080
81Supported COMDAT types:
82
83``discard``
84 Discards duplicate sections with the same COMDAT symbol. This is the default
85 if no type is specified.
86
87``one_only``
88 If the symbol is defined multiple times, the linker issues an error.
89
90``same_size``
91 Duplicates are discarded, but the linker issues an error if any have
92 different sizes.
93
94``same_contents``
95 Duplicates are discarded, but the linker issues an error if any duplicates
96 do not have exactly the same content.
97
Nico Riecka37acf72013-07-06 12:13:10 +000098``largest``
99 Links the largest section from among the duplicates.
100
101``newest``
102 Links the newest section from among the duplicates.
103
104
105.. code-block:: gas
106
107 .section .text$foo
108 .linkonce
109 ...
110
Rafael Espindola60ec3832013-11-19 19:52:52 +0000111``.section`` Directive
112^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
113
114MC supports passing the information in ``.linkonce`` at the end of
115``.section``. For example, these two codes are equivalent
116
117.. code-block:: gas
118
119 .section secName, "dr", discard, "Symbol1"
120 .globl Symbol1
121 Symbol1:
122 .long 1
123
124.. code-block:: gas
125
126 .section secName, "dr"
127 .linkonce discard
128 .globl Symbol1
129 Symbol1:
130 .long 1
131
Timur Iskhodzhanov18f666b2013-12-20 10:32:12 +0000132Note that in the combined form the COMDAT symbol is explicit. This
Nico Riecke52d2932014-02-15 06:02:36 +0000133extension exists to support multiple sections with the same name in
134different COMDATs:
Rafael Espindola60ec3832013-11-19 19:52:52 +0000135
136
137.. code-block:: gas
138
139 .section secName, "dr", discard, "Symbol1"
140 .globl Symbol1
141 Symbol1:
142 .long 1
143
144 .section secName, "dr", discard, "Symbol2"
145 .globl Symbol2
146 Symbol2:
147 .long 1
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000148
Rafael Espindola0766ae02014-06-06 19:26:12 +0000149In addition to the types allowed with ``.linkonce``, ``.section`` also accepts
150``associative``. The meaning is that the section is linked if a certain other
151COMDAT section is linked. This other section is indicated by the comdat symbol
152in this directive. It can be any symbol defined in the associated section, but
153is usually the associated section's comdat.
154
155 The following restrictions apply to the associated section:
156
157 1. It must be a COMDAT section.
158 2. It cannot be another associative COMDAT section.
159
160In the following example the symobl ``sym`` is the comdat symbol of ``.foo``
161and ``.bar`` is associated to ``.foo``.
162
163.. code-block:: gas
164
165 .section .foo,"bw",discard, "sym"
166 .section .bar,"rd",associative, "sym"
167
Reid Kleckner8ddf07c2016-09-15 15:11:49 +0000168MC supports these flags in the COFF ``.section`` directive:
169
170 - ``b``: BSS section (``IMAGE_SCN_CNT_INITIALIZED_DATA``)
171 - ``d``: Data section (``IMAGE_SCN_CNT_UNINITIALIZED_DATA``)
172 - ``n``: Section is not loaded (``IMAGE_SCN_LNK_REMOVE``)
173 - ``r``: Read-only
174 - ``s``: Shared section
175 - ``w``: Writable
176 - ``x``: Executable section
177 - ``y``: Not readable
178 - ``D``: Discardable (``IMAGE_SCN_MEM_DISCARDABLE``)
179
180These flags are all compatible with gas, with the exception of the ``D`` flag,
181which gnu as does not support. For gas compatibility, sections with a name
182starting with ".debug" are implicitly discardable.
183
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000184
Martin Storsjoc61ff3b2018-03-01 20:42:28 +0000185ARM64/COFF-Dependent
186--------------------
187
188Relocations
189^^^^^^^^^^^
190
191The following additional symbol variants are supported:
192
193**:secrel_lo12:** generates a relocation that corresponds to the COFF relocation
194types ``IMAGE_REL_ARM64_SECREL_LOW12A`` or ``IMAGE_REL_ARM64_SECREL_LOW12L``.
195
196**:secrel_hi12:** generates a relocation that corresponds to the COFF relocation
197type ``IMAGE_REL_ARM64_SECREL_HIGH12A``.
198
199.. code-block:: gas
200
201 add x0, x0, :secrel_hi12:symbol
202 ldr x0, [x0, :secrel_lo12:symbol]
203
204 add x1, x1, :secrel_hi12:symbol
205 add x1, x1, :secrel_lo12:symbol
206 ...
207
208
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000209ELF-Dependent
210-------------
211
212``.section`` Directive
213^^^^^^^^^^^^^^^^^^^^^^
214
215In order to support creating multiple sections with the same name and comdat,
216it is possible to add an unique number at the end of the ``.seciton`` directive.
217For example, the following code creates two sections named ``.text``.
218
219.. code-block:: gas
220
Rafael Espindola41e2b5c2015-04-06 16:34:41 +0000221 .section .text,"ax",@progbits,unique,1
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000222 nop
223
Rafael Espindola41e2b5c2015-04-06 16:34:41 +0000224 .section .text,"ax",@progbits,unique,2
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000225 nop
226
227
228The unique number is not present in the resulting object at all. It is just used
229in the assembler to differentiate the sections.
230
Evgeniy Stepanov12de7b22017-04-04 22:35:08 +0000231The 'o' flag is mapped to SHF_LINK_ORDER. If it is present, a symbol
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000232must be given that identifies the section to be placed is the
233.sh_link.
234
235.. code-block:: gas
236
237 .section .foo,"a",@progbits
238 .Ltmp:
Evgeniy Stepanov12de7b22017-04-04 22:35:08 +0000239 .section .bar,"ao",@progbits,.Ltmp
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000240
241which is equivalent to just
242
243.. code-block:: gas
244
245 .section .foo,"a",@progbits
Evgeniy Stepanov12de7b22017-04-04 22:35:08 +0000246 .section .bar,"ao",@progbits,.foo
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000247
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000248``.linker-options`` Section (linker options)
249^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
250
251In order to support passing linker options from the frontend to the linker, a
252special section of type ``SHT_LLVM_LINKER_OPTIONS`` (usually named
253``.linker-options`` though the name is not significant as it is identified by
254the type). The contents of this section is a simple pair-wise encoding of
Saleem Abdulrasool04434992018-01-31 00:16:23 +0000255directives for consideration by the linker. The strings are encoded as standard
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000256null-terminated UTF-8 strings. They are emitted inline to avoid having the
Saleem Abdulrasool04434992018-01-31 00:16:23 +0000257linker traverse the object file for retrieving the value. The linker is
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000258permitted to not honour the option and instead provide a warning/error to the
259user that the requested option was not honoured.
260
Saleem Abdulrasool04434992018-01-31 00:16:23 +0000261The section has type ``SHT_LLVM_LINKER_OPTIONS`` and has the ``SHF_EXCLUDE``
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000262flag to ensure that the section is treated as opaque by linkers which do not
263support the feature and will not be emitted into the final linked binary.
264
265This would be equivalent to the follow raw assembly:
266
267.. code-block:: gas
268
269 .section ".linker-options","e",@llvm_linker_options
270 .asciz "option 1"
271 .asciz "value 1"
272 .asciz "option 2"
273 .asciz "value 2"
274
Saleem Abdulrasool04434992018-01-31 00:16:23 +0000275The following directives are specified:
276
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000277 - lib
278
279 The parameter identifies a library to be linked against. The library will
280 be looked up in the default and any specified library search paths
281 (specified to this point).
282
Rui Ueyama79755dc2018-01-30 23:49:27 +0000283 - libpath
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000284
285 The paramter identifies an additional library search path to be considered
286 when looking up libraries after the inclusion of this option.
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000287
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000288``SHT_LLVM_CALL_GRAPH_PROFILE`` Section (Call Graph Profile)
289^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
290
291This section is used to pass a call graph profile to the linker which can be
292used to optimize the placement of sections. It contains a sequence of
293(from symbol, to symbol, weight) tuples.
294
295It shall have a type of ``SHT_LLVM_CALL_GRAPH_PROFILE`` (0x6fff4c02), shall
296have the ``SHF_EXCLUDE`` flag set, the ``sh_link`` member shall hold the section
297header index of the associated symbol table, and shall have a ``sh_entsize`` of
29816. It should be named ``.llvm.call-graph-profile``.
299
300The contents of the section shall be a sequence of ``Elf_CGProfile`` entries.
301
302.. code-block:: c
303
304 typedef struct {
305 Elf_Word cgp_from;
306 Elf_Word cgp_to;
307 Elf_Xword cgp_weight;
308 } Elf_CGProfile;
309
310cgp_from
311 The symbol index of the source of the edge.
312
313cgp_to
314 The symbol index of the destination of the edge.
315
316cgp_weight
317 The weight of the edge.
318
319This is represented in assembly as:
320
321.. code-block:: gas
322
323 .cg_profile from, to, 42
324
325``.cg_profile`` directives are processed at the end of the file. It is an error
326if either ``from`` or ``to`` are undefined temporary symbols. If either symbol
327is a temporary symbol, then the section symbol is used instead. If either
328symbol is undefined, then that symbol is defined as if ``.weak symbol`` has been
329written at the end of the file. This forces the symbol to show up in the symbol
330table.
331
Saleem Abdulrasool1b02b632018-06-19 16:47:31 +0000332CodeView-Dependent
333------------------
334
335``.cv_file`` Directive
336^^^^^^^^^^^^^^^^^^^^^^
337Syntax:
338 ``.cv_file`` *FileNumber FileName* [ *checksum* ] [ *checksumkind* ]
339
340``.cv_func_id`` Directive
341^^^^^^^^^^^^^^^^^^^^^^^^^
342Introduces a function ID that can be used with ``.cv_loc``.
343
344Syntax:
345 ``.cv_func_id`` *FunctionId*
346
347``.cv_inline_site_id`` Directive
348^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
349Introduces a function ID that can be used with ``.cv_loc``. Includes
350``inlined at`` source location information for use in the line table of the
351caller, whether the caller is a real function or another inlined call site.
352
353Syntax:
354 ``.cv_inline_site_id`` *FunctionId* ``within`` *Function* ``inlined_at`` *FileNumber Line* [ *Colomn* ]
355
356``.cv_loc`` Directive
357^^^^^^^^^^^^^^^^^^^^^
358The first number is a file number, must have been previously assigned with a
359``.file`` directive, the second number is the line number and optionally the
360third number is a column position (zero if not specified). The remaining
361optional items are ``.loc`` sub-directives.
362
363Syntax:
364 ``.cv_loc`` *FunctionId FileNumber* [ *Line* ] [ *Column* ] [ *prologue_end* ] [ ``is_stmt`` *value* ]
365
366``.cv_linetable`` Directive
367^^^^^^^^^^^^^^^^^^^^^^^^^^^
368Syntax:
369 ``.cv_linetable`` *FunctionId* ``,`` *FunctionStart* ``,`` *FunctionEnd*
370
371``.cv_inline_linetable`` Directive
372^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
373Syntax:
374 ``.cv_inline_linetable`` *PrimaryFunctionId* ``,`` *FileNumber Line FunctionStart FunctionEnd*
375
376``.cv_def_range`` Directive
377^^^^^^^^^^^^^^^^^^^^^^^^^^^
378The *GapStart* and *GapEnd* options may be repeated as needed.
379
380Syntax:
381 ``.cv_def_range`` *RangeStart RangeEnd* [ *GapStart GapEnd* ] ``,`` *bytes*
382
383``.cv_stringtable`` Directive
384^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
385
386``.cv_filechecksums`` Directive
387^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
388
389``.cv_filechecksumoffset`` Directive
390^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
391Syntax:
392 ``.cv_filechecksumoffset`` *FileNumber*
393
394``.cv_fpo_data`` Directive
395^^^^^^^^^^^^^^^^^^^^^^^^^^
396Syntax:
397 ``.cv_fpo_data`` *procsym*
398
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000399Target Specific Behaviour
400=========================
401
Peter Collingbourned763c4c2017-01-31 18:28:44 +0000402X86
403---
404
405Relocations
406^^^^^^^^^^^
407
408``@ABS8`` can be applied to symbols which appear as immediate operands to
409instructions that have an 8-bit immediate form for that operand. It causes
410the assembler to use the 8-bit form and an 8-bit relocation (e.g. ``R_386_8``
411or ``R_X86_64_8``) for the symbol.
412
413For example:
414
415.. code-block:: gas
416
417 cmpq $foo@ABS8, %rdi
418
419This causes the assembler to select the form of the 64-bit ``cmpq`` instruction
420that takes an 8-bit immediate operand that is sign extended to 64 bits, as
421opposed to ``cmpq $foo, %rdi`` which takes a 32-bit immediate operand. This
422is also not the same as ``cmpb $foo, %dil``, which is an 8-bit comparison.
423
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000424Windows on ARM
425--------------
426
427Stack Probe Emission
428^^^^^^^^^^^^^^^^^^^^
429
430The reference implementation (Microsoft Visual Studio 2012) emits stack probes
431in the following fashion:
432
433.. code-block:: gas
434
435 movw r4, #constant
436 bl __chkstk
437 sub.w sp, sp, r4
438
Alp Tokerbeaca192014-05-15 01:52:21 +0000439However, this has the limitation of 32 MiB (±16MiB). In order to accommodate
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000440larger binaries, LLVM supports the use of ``-mcode-model=large`` to allow a 4GiB
441range via a slight deviation. It will generate an indirect jump as follows:
442
443.. code-block:: gas
444
445 movw r4, #constant
446 movw r12, :lower16:__chkstk
447 movt r12, :upper16:__chkstk
448 blx r12
449 sub.w sp, sp, r4
450
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000451Variable Length Arrays
452^^^^^^^^^^^^^^^^^^^^^^
453
454The reference implementation (Microsoft Visual Studio 2012) does not permit the
455emission of Variable Length Arrays (VLAs).
456
457The Windows ARM Itanium ABI extends the base ABI by adding support for emitting
458a dynamic stack allocation. When emitting a variable stack allocation, a call
459to ``__chkstk`` is emitted unconditionally to ensure that guard pages are setup
460properly. The emission of this stack probe emission is handled similar to the
461standard stack probe emission.
462
463The MSVC environment does not emit code for VLAs currently.
464
Martin Storsjo2778fd02017-12-20 06:51:45 +0000465Windows on ARM64
466----------------
467
468Stack Probe Emission
469^^^^^^^^^^^^^^^^^^^^
470
471The reference implementation (Microsoft Visual Studio 2017) emits stack probes
472in the following fashion:
473
474.. code-block:: gas
475
476 mov x15, #constant
477 bl __chkstk
478 sub sp, sp, x15, lsl #4
479
480However, this has the limitation of 256 MiB (±128MiB). In order to accommodate
481larger binaries, LLVM supports the use of ``-mcode-model=large`` to allow a 8GiB
482(±4GiB) range via a slight deviation. It will generate an indirect jump as
483follows:
484
485.. code-block:: gas
486
487 mov x15, #constant
488 adrp x16, __chkstk
489 add x16, x16, :lo12:__chkstk
490 blr x16
491 sub sp, sp, x15, lsl #4
492