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

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.

6

u/OneRaynyDay May 24 '20

Hey, you're totally correct and thank you for the change. I'm a human and I make typos too so I appreciate you opening the PR :) it's merged now.

Unfortunately, I've spent my time learning C++ most of the time and it would be dubious for me to assume that the same file layout exists for C for the same program(although I later discovered this to be true, as the output executables were exactly the same size).

9

u/_TechFTW_ May 24 '20

Maybe crosspost this to r/hacking and r/programming, I'm sure they'd be interested.

7

u/OneRaynyDay May 24 '20

I did to /r/programming :) Thanks for the heads up!

4

u/[deleted] 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:

  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/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

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

5

u/Drejo May 24 '20

Amazing read...

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

u/UnicornMolestor May 24 '20

Very Nice very well written i learned a lot!

2

u/noooit May 24 '20

A very fine blog. I wish I were knowledgeable enough to write posts like these.

1

u/[deleted] May 24 '20

Nice, didn't analyze elf structure till now.

1

u/[deleted] May 24 '20

Great write up, thankyou!

1

u/[deleted] May 24 '20

[removed] — view removed comment

1

u/OneRaynyDay May 24 '20

I fixed that a few hours ago - can you check it again?