r/linux • • May 24 '20

Analyzing The Simplest C++ Program

https://oneraynyday.github.io/dev/2020/05/03/Analyzing-The-Simplest-C++-Program/
235 Upvotes

21 comments sorted by

View all comments

2

u/cloveistaken May 24 '20

In the program header section I think you didn't explain every fields so I have a couple of questions:

  • What are Offset and Align here ?

  • What is the difference between VirtAddr and PhysAddr and when they are different in value ?

1

u/OneRaynyDay May 24 '20

I didn't explain those fields because it would've taken a while and I didn't think it was as important as the purpose the segments served. Lucky for you, I can answer your questions:

  1. Offset is the location in the ELF file where the segment is defined. Align, just like data alignment(by DWORD or something else), is usually a power of 2 which states that the (VirtAddr - Offset) is divisible by Align.

  2. That's a good question. I'm going to assume you know what virtual memory is. In the libc source code, virt addr is used to memmap the segment into memory at that address (w/ alignment and offset in the calculation). I don't think the modern libc code even uses physaddr anymore which makes me assume it's an entity reserved for when there's no memory virtualization, which probably is the case for embedded applications but I'm not an embedded systems programmer ¯_(ツ)_/¯

Hope that helps bro

1

u/[deleted] May 24 '20

yes, the physaddr field is used for e.g. when e.g. a bootloader loads a kernel binary in ELF format, but on an OS with no direct access to physical addresses it is completely ignored. you can actually write anything into it