Flags in objdump output of object file

scdmb picture scdmb · Jun 25, 2012 · Viewed 8.3k times · Source

There is this output of objdump on some object file:

$ 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

What do these flags CONTENTS, ALLOC, LOAD and so on mean?

Answer

Hristo Iliev picture Hristo Iliev · Jun 25, 2012

What you see is the interpretation of the combination of ELF segment flags, section type and section flags for each section in the object file.

  • LOAD means that the section resides in a loadable segment, i.e. its content could be read from the file into memory when a process is created

Section flags are well documented in the Chapter 4 of the System V Application Binary Interface, although under slightly different names from what objdump shows.

  • CODE means that the section contains executable code; it is indicated by the SHF_EXECINSTR flag in the section header
  • DATA means that the section is not executable but is writable, indicated by the presence of the SHF_WRITE flag
  • READONLY means that the section is neither executable nor writtable and should be placed in read-only memory pages
  • ALLOC means that the section occupies memory, e.g. memory pages are actually allocated to hold the section content when a process is created, indicated by the SHF_ALLOC flag. Some sections, e.g. those containing debug information, are not read into memory during normal program execution and are not marked as ALLOC to save memory.

Sections of type SHT_PROGBITS have corresponding content in the file and are shown as CONTENTS. Some sections does not have corresponding content in the file, e.g. the .bss section, which is of type SHT_NOBITS.

The .text section contains the program executable code. It is show as CONTENTS since it is of type SHT_PROGBITS. Memory should be reserved for this section since it is ALLOC and its contents should be loaded from the file since it is placed in a LOAD-able segment. Program code is generally non-modifiable and hence the section is placed in read-only memory. It contains instructions that are to be executed and hence the CODE flag.

Initialised variables with static storage class go into the .data section. Their initial values are stored in the file and read from there as the process is created. In C/C++ these are global variables, static local variables and C++ static member variables that are initialised appropriately, e.g. static int a = 10;. Fortran places initialised SAVE-d variables and COMMON blocks, which are given intiial value with a block DATA statement there.

The .bss section (historic name, abbreviation from Block Started by Symbol) is the most simple one. It holds uninitialised variables with static storage class. It is a section of type SHT_NOBITS and takes no space in the file. Memory is ALLOC-ated for it but nothing is read from the file to prepopulate the memory - it just stays all zeroes as delivered by the kernel memory allocator.

Constants usually go into the .rodata section (not present in your example), which looks like .data but is not marked as writable and is thus shown as READONLY.