How the alignment of .data and .bss is determined

The alignment of .data and .bss is sometimes 4 bytes, and sometimes 32 bytes. Example 1. According to the last column below, the alignment of bss and data is 32 bytes.

bash-3.00$ readelf --sections libmodel.so There are 39 section headers, starting at offset 0x1908a63c: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al ... [25] .data PROGBITS 01e221e0 1e211e0 26ca54 00 WA 0 0 32 [26] .bss NOBITS 0208ec40 208dc34 374178 00 WA 0 0 32 ... 

Example 2: according to the result below, alignment of os.data and .bss is 4 bytes

 bash-3.00$ readelf --sections ./a.out There are 28 section headers, starting at offset 0x78c: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al ... [22] .data PROGBITS 0804956c 00056c 000034 00 WA 0 0 4 [23] .bss NOBITS 080495a0 0005a0 000004 00 WA 0 0 4 ... 

What determines alignment for .bss and .data? Why is it sometimes 4 bytes and in other cases 32 bytes?

+6
source share
4 answers

I think that these different alignment values ​​were determined using the appropriate ld scripts.

  • For libmodel.so : section .data align=16, section .bss align=16
  • For a.out : section .data align=4, section .bss align=4
0
source

Why is it sometimes 4 bytes and in other cases 32 bytes?

As soon as the executable file is loaded into memory, it is addressed through the virtual addresses of the process. Alignment restrictions relate to virtual addressing. For example, if you look at the elf man page, check out the description for sh_addralign. It is for this reason that various elf objects set different alignment requirements. You can experiment by changing the source for a.out to turn on double and see if the alignment changes.

Note. This only applies to memory alignment. There is an alignment restriction for the actual file on disk. What for? I think this will help simplify the mapping of data files with inline structures after reading the file. Others can correct me if I am wrong here.

Update: I would like to clarify one problem. Alignment of virtual addresses is necessary only because of the depth of access to the basic memory access set by the chipset. Thus, the same program compiled for different architectures can lead to restrictions on the difference.

0
source

There is a really good book that will give you an excellent understanding of not only your question, but also everything related to it.

Book website here

Chapter n. 7 is the one you are looking for.

I don’t know if you need a direct answer or a more complex one, but I hope this helps.

0
source

For bss, alignment is determined using the size of the data types, if it is not aligned, then the linker first aligns the address of the variable where the bss start can be placed.

0
source

Source: https://habr.com/ru/post/951539/


All Articles