Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an "Algn" of 2**2 and 2**0 mean in the output of objdump?

Tags:

c

elf

objdump

What does this mean in below file? 2**2 and 2**0

$ objdump -h main.o

main.o:     file format elf32-i386

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         0000000b  00000000  00000000  00000034  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .data         00000000  00000000  00000000  00000040  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  00000000  00000000  00000040  2**2
                  ALLOC
  3 .note.GNU-stack 00000000  00000000  00000000  00000040  2**0
                  CONTENTS, READONLY, CODE
like image 260
qcmao Avatar asked Jan 12 '23 05:01

qcmao


1 Answers

I would assume that 2**2 means 22, or 4 byte alignment, while 2**0 means no (one byte) alignment.

This value comes from the sh_addralign field of the ELF section header. The ELF specification states (emphasis mine):

sh_addralign Some sections have address alignment constraints. For example, 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 0, modulo the value of sh_addralign. Currently, only 0 and positive integral powers of two are allowed. Values 0 and 1 mean the section has no alignment constraints.

As Ray Toal mentioned, since the alignment must be a power of two, it only makes sense that objdump would express this value as a power of two with the 2**x notation.

Note that in some languages, like Python and FORTRAN, ** is a power or exponentiation operator.


Looking at objdump.c, we see:

static void
dump_section_header (bfd *abfd, asection *section,
             void *ignored ATTRIBUTE_UNUSED)
{
  // ...
  printf ("  %08lx  2**%u", (unsigned long) section->filepos,
      bfd_get_section_alignment (abfd, section));

And in objdump.h:

#define bfd_get_section_alignment(bfd, ptr) ((ptr)->alignment_power + 0)

where the alignment_power member of bfd is:

/* The alignment requirement of the section, as an exponent of 2 -
   e.g., 3 aligns to 2^3 (or 8). */

unsigned int alignment_power;

like image 100
Jonathon Reinhart Avatar answered Feb 02 '23 12:02

Jonathon Reinhart