16

What does the ALIGN keyword do in linker scripts? I read many tutorials about linker scripts but I cant understand what really ALIGN do. Can any one explain it simply. Thanks!

Jayanga Kaushalya
  • 2,462
  • 5
  • 34
  • 56

3 Answers3

24

A typical usage is

. = ALIGN(8);

This means: insert padding bytes until current location becomes aligned on 8-byte boundary. That is:

while ((current_location & 7) != 0)
  *current_location++ = padding_value;
Employed Russian
  • 164,132
  • 27
  • 242
  • 306
  • 1
    Thanks for explaining it in such a simple fashion. For the life of me I couldn't find a place that explains it just that simple – TopchetoEU Jan 19 '21 at 05:36
3

The ALIGN() instructions tell the linker that section(bss, text) should be this much aligned.

For a typical idea, you can take a look here (4.6.3 "Output Section Description")

e.g.

    //.data is aligned by word size on the 32-bit architecture and direct it to the data section
For a 32-bit machine, it typically needs to be word aligned 

        .data : ALIGN(4) 
        {
           *(.data*)
        } > data_sdram
dav23r
  • 86
  • 2
  • 7
ixnisarg
  • 77
  • 1
  • 8
1

. = ALIGN(8)

Corresponds to the following (working link script example using operators):

data = .;

. = ((data + 0x8 - 1) & ~(0x8 - 1)) - data;
ria
  • 152
  • 1
  • 9