Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtual and physical addresses of sections in elf files

How does objdump compute the physical address (LMA) of elf sections? As far as I can tell, elf section headers only contain the virtual address (VMA) of sections [1].

Usually, VMA and LMA are the same. But for initialized data sections (.data), the VMA is the RAM location of the variables and LMA is the ROM location where the initial values are located. Crt0 is responsible for copying the initial values into RAM before main() is called. For example:

$ objdump -h my.elf
Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         0003c3d0  00080000  00080000  00010000  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  5 .data         000008d0  40000000  000d08d4  00060000  2**3
                  CONTENTS, ALLOC, LOAD, DATA

-Tom

[1] http://www.ouah.org/RevEng/x430.htm

like image 921
tomahawkins Avatar asked Jun 02 '11 18:06

tomahawkins


3 Answers

Find this about LMA: http://www-zeuthen.desy.de/dv/documentation/unixguide/infohtml/binutils/docs/ld/Basic-Script-Concepts.html#Basic-Script-Concepts

The important is following:

Every loadable or allocatable output section has two addresses. The first is the VMA, or virtual memory address. This is the address the section will have when the output file is run. The second is the LMA, or load memory address. This is the address at which the section will be loaded. In most cases the two addresses will be the same. An example of when they might be different is when a data section is loaded into ROM, and then copied into RAM when the program starts up (this technique is often used to initialize global variables in a ROM based system). In this case the ROM address would be the LMA, and the RAM address would be the VMA

like image 131
zu1000 Avatar answered Nov 20 '22 00:11

zu1000


The section header contains a single address. It looks to me like address in the section header is the VMA. The program headers contain the mapping of VMA to LMA.

For example, here's a snippet of what "objdump -x" shows for my elf file:

Program Header:
<a few lines removed>
    LOAD off    0x00000240 vaddr 0x00000048 paddr 0x0000018c align 2**0
         filesz 0x00000000 memsz 0x00000004 flags rw-

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
<a few lines removed>
  3 .bss          00000004  00000048  0000018c  00000240  2**1
                  ALLOC

So, .bss has a VMA of 0x48. If you look through the program headers, one entry has a "vaddr" of 0x48 and a paddr of 0x18c, which is the LMA.

like image 32
Andrew Bainbridge Avatar answered Nov 20 '22 00:11

Andrew Bainbridge


Physical address is an attribute of ELF file segment. ELF file section does not have such attribute. It is possible though to map sections to corresponding segment's memory.

The meaning of physical address is architecture dependent and may vary between different OSes and hardware platforms.

From this link:

p_paddr - On systems for which physical addressing is relevant, this member is reserved for the segment’s physical address. Because System V ignores physical addressing for application programs, this member has unspecified contents for executable files and shared objects.

It looks like your Crt0 makes some assumption about meaning of physical address located in ELF file. This assumption may be true for the particular system, but is not garanteed on another.

like image 1
Serge C Avatar answered Nov 20 '22 00:11

Serge C