Any practical exercise to understand how a program is loaded into memory and executed

I'm wondering what happened before main () is called, for example loading an executable into memory, dynamically loading a shared library. Do you have any suggestions for understanding these things through practical exercises?

The tools that I know and use now include:

  • strace
  • disassembles
  • readelf
  • / Proc / pid / card

NOTES: I know a large book of linkers and downloaders , but practical exercises can teach me better than reading a book.

+6
c linux mips elf loader
source share
5 answers
  • The ld.so man page documents several environment variables that can be configured to either configure dynamic linking or additional information.

eg.

LD_DEBUG=all cat </dev/null 
  • You can easily get the source code for each part involved - the Linux kernel, the dynamic linker, the C library, the startup code (crt0.o or similar). You can start by looking at the code and making experimental changes.
+3
source share

If you want to check how the binary package is packaged and its different sections, I think the best program is objdump ,

Select any executable file and execute:

 objdump -S <executable> > myfile.S 

Another good exercise would be:

  • Creates a program using an external library
  • Compile the program using static binding
  • Run the program
  • Rename the library file and check if the program is running
  • Compile a program using a shared library
  • Rename the library and check if the program is running

This will answer some of your questions about what happens under the curtains and how.

+3
source share

I found two interesting links (at least for Linux) and a little shorter than the mentioned book (Linkers and Loaders)

+2
source share

When I took the OS class in college, we used Nachos . This is not the operating system itself, but a kind of "simulation" of the operating system that runs in user space. It is written in C ++, and you can cross-compile executables that Nachos can load and run. You can play with the system call interface and in the general experiment as much as you want using the code.

We ran it in the Solaris lab, and I had problems running it and running it on Linux on my personal machine, but it can be a fun toy if you want to delve into some kind of code.

+1
source share

I understand that this is probably a lot for what you are looking for, but writing your own assembler and linker would be very educated. I did this when I was in college and loved him. It took me 120 hours of work, as I recall, to get it to work for the basic things that I wanted to do. I think this project more than anything else made me sure that a career in programming was for me.

+1
source share

All Articles