Question

I am curious about the things happend before main() is called , such like load the executable into memory , dynamic load of shared library. Do you have any suggestions how to understand these things by a hand-on exercise?

The tools amd things I know of ,and using now, includes:

  • strace
  • disassemble
  • readelf
  • /proc/pid/map

NOTES: I know the great book linkers and loaders, but hands-on exercise may teach me better than reading the book.

Was it helpful?

Solution

  • The ld.so man page documents several environment variables that may be set to either tweak the dynamic linking process or provide additional details.

e.g.

LD_DEBUG=all cat </dev/null
  • You can easily obtain the source code for each and every piece involved - Linux kernel, dynamic linker, C library, startup code (crt0.o or similar). You could start by studying the code and making experimental modifications.

OTHER TIPS

If you want to check how a binary is packed and the different sections of it I think the best program out there is objdump.

Choose whatever executable and do:


objdump -S <executable>  > myfile.S

Another good exercise will be:

  • Creates a program that use an external library
  • Compile the program using static linking
  • Run the program
  • Rename the library file and check if the program runs
  • Compile the program using shared library
  • Rename the library and check if the program runs

That will answer some of your questions about what is happening under the curtains and how.

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

When I took an OS class in college, we used Nachos. It's not an operating system per se, but a sort of operating system "simulation" that runs in user space. It's written in C++ and you can cross-compile executables which Nachos can then load and run. You can play with the system call interface and in general experiment as much as you like by twiddling with the code.

We ran it in a Solaris lab, and I had some trouble getting it up and running on Linux on my personal machine, but it might be a fun toy if you're willing to dig into some code.

I realize that it's probably a lot for what you're looking for but writing your own assembler and linker would be very educational. I did it when I was in college and loved it. It took maybe 120 hours of work as I recall to make it work for the basic things I wanted it to do. I think this project more than anything else made me certain that a career in programing was for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top