r/linux • u/OneRaynyDay • May 24 '20
Analyzing The Simplest C++ Program
https://oneraynyday.github.io/dev/2020/05/03/Analyzing-The-Simplest-C++-Program/9
u/_TechFTW_ May 24 '20
Maybe crosspost this to r/hacking and r/programming, I'm sure they'd be interested.
7
4
May 24 '20
Idk what the cpp program there is but it's not the simplest. The simplest program is
21
u/OneRaynyDay May 24 '20
You actually raise a really good point! Do you need to define the main function? According to the c++ standards, you do need to define the main function. Now whether that main function is implicitly defined for you if you're missing one is up to the toolchain (clang, g++, msvc), but the final binary will be identical to one that is written with
int main() {}. Please refer to: https://en.cppreference.com/w/cpp/language/main_function.3
u/JonnyRobbie May 24 '20 edited May 24 '20
Unfortunately I don't know where I've read it, but there was a nice blogpost detailing "Smallest program that will crash", which went to a nice detail of the corner cases of C language.
EDIT: http://llbit.se/?p=1744
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:
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.
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
memmapthe 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/cloveistaken May 24 '20
Thanks for answering. I get the PhysAddr part, but still a bit confused about the Alignment part.
From what I understand, you are saying Offset is the physical position and VirtAddr is the loaded position. Then this must leads to (VirtAddr - Offset) being a multiple of PageSize already (says 4KB), how exactly does Align play a role here ? Can I change it per segment and what would happen ?
1
u/OneRaynyDay May 24 '20
Err, not quite. Offset is the location in the ELF file, not the executable. However, the ELF file gets mapped into the executable in a read-only segment most of the time. The alignment is just a requirement of the ABI, and it does not have to be a page-size alignment. The page-size alignment assumption you have may come from the LOAD segments but this is not true for all segments. You don't want to change alignment for LOAD because it's used for memmapping - you'll get in trouble because memmap will straight up die if you don't pass it an addr that is page-aligned.
Unsure if I made this more confusing for you...
1
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
5
5
u/noja47 May 24 '20
I know it has already been said, but i just have to say it. Very nice, clean and easy to follow writeup. Thank you for making this .
4
u/PaperCupsAhoy May 24 '20
Interesting read with the right level of depth. Anyone know of something similar to this but focusing on Python?
6
2
1
1
1
22
u/reini_urban May 24 '20 edited May 24 '20
Had to fix the linker loader confusion there. And it's not much about C++, 99% is about C.
The C++ part should talk about the class layout, vtable and virtual functions, the different calling convention (__thiscall, MSABI), typeinfo and such.