The alignment directives align the current location in the file to a specified boundary.
Syntax
.balign
num_bytes
[, fill_value
]
.balignl
num_bytes
[, fill_value
]
.balignw
num_bytes
[, fill_value
]
.p2align
exponent
[, fill_value
]
.p2alignl
exponent
[, fill_value
]
.p2alignw
exponent
[, fill_value
]
.align
exponent
[, fill_value
]
Description
num_bytes
-
This specifies the number of bytes that must be aligned
to. This must be a power of 2.
exponent
-
This specifies the alignment boundary as an exponent. The actual alignment
boundary is 2exponent
.
fill_value
-
The value to fill any inserted padding bytes with. This
value is optional.
Operation
The alignment directives align the current location in the file to a specified
boundary. The unused space between the previous and the new current location are
filled with:
- Copies of
fill_value
, if it is
specified. The width of fill_value
can be
controlled with the w
and l
suffixes, see
below.
- NOP instructions appropriate to the current instruction set, if all the
following conditions are specified:
- The
fill_value
argument is not
specified.
- The
w
or l
suffix is not
specified.
- The alignment directive follows an instruction.
- Zeroes otherwise.
The .balign
directive takes an absolute number of bytes as
its first argument, and the .p2align
directive
takes a power of 2. For example, the following directives align the current location
to the next multiple of 16 bytes:
.balign 16
.p2align 4
.align 4
The w
and l
suffixes modify the width of
the padding value that will be inserted.
- By default, the
fill_value
is a 1-byte
value.
- If the
w
suffix is specified, the
fill_value
is a 2-byte value.
- If the
l
suffix is specified, the
fill_value
is a 4-byte value.
If either of these suffixes are specified, the padding values are emitted as data
(defaulting to a value of zero), even if following an instruction.
The .align
directive is an alias for
.p2align
, but it does not accept the w
and l
suffixes.
Alignment is relative to the start of the section in which the directive occurs. If
the current alignment of the section is lower than the alignment requested by the
directive, the alignment of the section will be increased.
Usage
Use the alignment directives to ensure that your data and code are aligned to appropriate boundaries. This is typically required in the following circumstances:
- In T32 code, the ADR instruction and the PC-relative version of the LDR
instruction can only reference addresses that are 4-byte aligned, but a label
within T32 code might only be 2-byte aligned. Use
.balign 4
to ensure 4-byte alignment of an address within T32
code.
- Use alignment directives to take advantage of caches on some Arm
processors. For example, many processors have an instruction cache with 16-byte
lines. Use
.p2align 4
or .balign 16
to align function entry points on
16-byte boundaries to maximize the efficiency of the cache.
Examples
Aligning a constant pool value to a 4-byte boundary in T32 code:
get_val:
ldr r0, value
adds r0, #1
bx lr
// The above code is 6 bytes in size.
// Therefore the data defined by the .word directive below must be manually aligned
// to a 4-byte boundary to be able to use the LDR instruction.
.p2align 2
value:
.word 42
Ensuring that the entry points to functions are on 16-byte boundaries, to
better utilize caches:
.p2align 4
.type func1, "function"
func1:
// code
.p2align 4
.type func2, "function"
func2:
// code
Note:
In both of the examples above, it is important that the directive comes before the
label that is to be aligned. If the label came first, then it would point at the
padding bytes, and not the function or data it is intended to point to.