Suppose I have
an object file (source.o
) without function main
.
a shared object (libmain.so
) with function main
.
How will the linker take care of the entry point when both are linked dynamically to create binary source.bin
?
No, the implementation of C library of gcc has an entry point not a main symbol.
A shared object is an indivisible unit that is generated from one or more relocatable objects. Shared objects can be bound with dynamic executables to form a runable process. As their name implies, shared objects can be shared by more than one application.
so (short for "shared object"). Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system.
Instead, they're just placed in an appropriate folder and used automatically by other programs via Linux's dynamic link loader. However, you might be able to read the SO file as a text file by opening it in a text editor like Leafpad, gedit, KWrite, or Geany if you're on Linux, or Notepad++ on Windows.
Whenever you have a dynamic linked program, there are several "entry points" involved. The first is in entry point of the dynamic linker itself, e.g. /lib/ld-linux.so.2
(on Linux/x86) or similar. The dynamic linker runs first, resolving all symbol names to their definitions (regardless of whether the definitions are in the main program or a library), then passes execution to the second entry point in the main program binary. This is not main
but part of the "C runtime" (thus crt.o
and similar names) that takes care of some pre-main
stuff (like C++ ctors, setting up the pointer to the environment variables, and constructing the right arguments for main
). This code ends with (the equivalent of) exit(main(argc, argv));
which will use the relocated (by the dynamic linker) addresses of both exit
and main
.
main()
is just like any other function however in hosted envrionment it is treated as the logical entry point of an c/c++ program. So there is nothing special with regards to linking of main()
as an function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With