smwikipedia
smwikipedia

Reputation: 64173

How to view the section layout info recorded in an ELF file?

Suppose the following linker script is used to layout an executable file.

SECTIONS
{
. = 0x10000;
.text : { *(.text) }
.data : { *(.data) }
.bss  : { *(.bss) }
}

OUTPUT_FORMAT(ELF)

My questions are:

  1. I think such layout info must be stored in the output ELF file so the loader can load the executable based on that info. Right?

  2. If 1 is true, how to view such layout info in a ELF? objdump?

Upvotes: 2

Views: 7041

Answers (3)

sherrellbc
sherrellbc

Reputation: 4853

You can deduce the information from the linker command script by taking a look at the output of the -S option in readelf.

$ readelf -S $(which ls)
There are 28 section headers, starting at offset 0x1f6f8:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000400238  00000238
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000400254  00000254
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             0000000000400274  00000274
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .gnu.hash         GNU_HASH         0000000000400298  00000298
       0000000000000104  0000000000000000   A       5     0     8
  [ 5] .dynsym           DYNSYM           00000000004003a0  000003a0
       0000000000000c48  0000000000000018   A       6     1     8
  [ 6] .dynstr           STRTAB           0000000000400fe8  00000fe8
       0000000000000582  0000000000000000   A       0     0     1
  [ 7] .gnu.version      VERSYM           000000000040156a  0000156a
...

If you want to know where this comes from, take a look at the default linker command scripts for your toolchain; this default is used if you did not explicitly create your own. I think there may be some inheritance between them as well, but I can't say for certain.

$ ls -l /usr/lib/ldscripts/ | grep elf
-rw-r--r-- 1 root root 9027 Mar 22 04:05 elf32_x86_64.x
-rw-r--r-- 1 root root 8880 Mar 22 04:05 elf32_x86_64.xbn
-rw-r--r-- 1 root root 8706 Mar 22 04:05 elf32_x86_64.xc
-rw-r--r-- 1 root root 9024 Mar 22 04:05 elf32_x86_64.xd
-rw-r--r-- 1 root root 8720 Mar 22 04:05 elf32_x86_64.xdc
-rw-r--r-- 1 root root 8680 Mar 22 04:05 elf32_x86_64.xdw
-rw-r--r-- 1 root root 9027 Mar 22 04:05 elf32_x86_64.xn
-rw-r--r-- 1 root root 5443 Mar 22 04:05 elf32_x86_64.xr
-rw-r--r-- 1 root root 8551 Mar 22 04:05 elf32_x86_64.xs
-rw-r--r-- 1 root root 8247 Mar 22 04:05 elf32_x86_64.xsc
-rw-r--r-- 1 root root 8207 Mar 22 04:05 elf32_x86_64.xsw
-rw-r--r-- 1 root root 5489 Mar 22 04:05 elf32_x86_64.xu
-rw-r--r-- 1 root root 8666 Mar 22 04:05 elf32_x86_64.xw
-rw-r--r-- 1 root root 8396 Mar 22 04:05 elf_i386.x
-rw-r--r-- 1 root root 8249 Mar 22 04:05 elf_i386.xbn
-rw-r--r-- 1 root root 8209 Mar 22 04:05 elf_i386.xc
-rw-r--r-- 1 root root 8389 Mar 22 04:05 elf_i386.xd
-rw-r--r-- 1 root root 8219 Mar 22 04:05 elf_i386.xdc
...

Upvotes: 2

Employed Russian
Employed Russian

Reputation: 213385

  1. I think such layout info must be stored in the output ELF file so the loader can load the executable based on that info. Right?

ELF stands for executable and linking format.

The .text, .data etc. are part of the linking format. While they usually are present in the fully-linked binary, they can be stripped.

The info that the loader needs to load the executable is stored as a table of segments (Elf{36,64}_Phdr[]).

  1. If 1 is true, how to view such layout info in a ELF? objdump?

You can examine segments (and, if the optional section table is present, the mapping of sections to segments) with readelf -Wl a.out.

Upvotes: 1

smwikipedia
smwikipedia

Reputation: 64173

I searched a bit and found something.

For 1. According ELF format on wikipedia, there are program headers that record the address layout info.

For 2. readelf is a promising tool.

Upvotes: 0

Related Questions