简体   繁体   中英

How can the offset of a segment Off not divisible by the alignment Al in an ELF file?

[Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
[ 1] .text             PROGBITS        00000000 000034 00002a 00  AX  0   0  4

As above,the segment begin from 0x34 address, but its Al is 4,so it can't be divided by 2**4.

I mean : 0x34 % 16 != 0.So I want to ask why .text segment's address doesn't begin from Integer times of 16.

The section header struct looks like this:

typedef struct {
   uint32_t   sh_name;
   uint32_t   sh_type;
   uint32_t   sh_flags;
   Elf32_Addr sh_addr;
   Elf32_Off  sh_offset;
   uint32_t   sh_size;
   uint32_t   sh_link;
   uint32_t   sh_info;
   uint32_t   sh_addralign;
   uint32_t   sh_entsize;
} Elf32_Shdr;

So what you see under the Al column is sh_addralign . Let's look at the description of that member from the elf manpage :

sh_addralign
             Some sections have address alignment constraints.  If a
             section holds a doubleword, the system must ensure
             doubleword alignment for the entire section.  That is, the
             value of sh_addr must be congruent to zero, modulo the
             value of sh_addralign.  Only zero and positive integral
             powers of two are allowed.  Values of zero or one mean the
             section has no alignment constraints.

TL;DR : The alignment constraint shown in the Al column is for Addr (which is aligned in your case since it's zero), not for Off . In other words, it's an alignment constraint for the address where the image is loaded in memory, not for where it's stored in the ELF file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM