Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is function main() inside a shared object (.so) taken care of by the linker?

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?

like image 801
silverframemonitor Avatar asked Mar 21 '12 15:03

silverframemonitor


People also ask

Can shared library have main?

No, the implementation of C library of gcc has an entry point not a main symbol.

How do shared objects work?

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.

What is a shared object in Linux?

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.

How do I view the contents of a .so file?

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.


2 Answers

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.

like image 102
R.. GitHub STOP HELPING ICE Avatar answered Sep 19 '22 00:09

R.. GitHub STOP HELPING ICE


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.

like image 39
Alok Save Avatar answered Sep 22 '22 00:09

Alok Save