Virtual and physical section addresses in elf files

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

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

$ 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

+6
source share
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 following is important:

Each loaded or allocated output section has two addresses. The first is a VMA or virtual memory address. This is the address that the section will have when running the output file. The second is the LMA or boot memory address. This is the address to which the section will be loaded. In most cases, both addresses will be the same. An example of when they can be different is that a data section is loaded into ROM and then copied to RAM when the program starts (this method is often used to initialize global variables in a ROM-based system). In this case, the ROM address will be LMA, and the RAM address will be VMA

+6
source

The section header contains one address. It seems to me that the address in the section header is VMA. The program headers contain a mapping of VMA to LMA.

For example, here is 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 are viewing program headers, one entry has "vaddr" 0x48 and paddr 0x18c, which is LMA.

+4
source

The physical address is an attribute of the ELF file segment. There is no such attribute in an ELF element. It is possible, however, to map partitions to the corresponding segment memory.

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

From this link :

p_paddr - about systems for which physical addressing matters, this member is reserved for the segments of the Physical address. Because system V ignores physical addressing for applications, this member has undefined contents for executable files and shared objects.

It looks like your Crt0 is making some assumptions about the value of the physical address found in the ELF file. This assumption may be true for a particular system, but is not guaranteed on another.

0
source

Source: https://habr.com/ru/post/889645/


All Articles