/* personal notes of renzo diomedi */



~ 00011100 ~

LIBRARY will be linked to the program in the linking phase.

There are 2 types of libraries:

STATIC with extension ".a" (.lib in windows)

SHARED or Dynamic with extension ".so" (.dll in windows)



Instead of including each separate function object file separately on the command line, the GNU C com- piler enables you to combine all of the object files into a single archive file. When you compile the main C program, all you need to include is the single object archive file.
The compiler can choose the proper object file required out of the archive file at compile time.
The archive file can be used to compile any program that uses any of the functions contained within the archive file.
This archive is referred to as a library file.
The library file contains object files for many functions. Functions are often grouped together by applica- tion type or function type into a library file. Multiple library files can be used within a single application project.
This type of library file is called static, because the object code contained in the library file is compiled into the main program from the compiler. Once the function object code is compiled into the executable code, the library file is not needed for the executable program to run. However, this means that every copy of the program contains the code for the functions within it.
Shared Libraries can help programs share object file code among themselves to con- serve memory requirements.
A shared library is a file containing object code that several files may use simultaneously while executing. When a program is link edited with a shared library, the library code that defines the program's external references is not copied into the program's object file.


$ ar -r libexa.a _nanosleep.o cpuidf.o screen.o

to display the files that are contained in the library:

$ ar t lexa.a

_nanosleep.o
cpuidf.o
screen.o


to create an index for the library to help speed up the compilation when other programs must link with the library. The ranlib program is used to create the index for the library. The index is placed inside of the library file.

$ ranlib lexa.a

nm program is used to display symbols on object files:

$ nm -s lexa.a

Archive index:
_start in _nanosleep.o
output in cpuidf.o
screen in screen.o

_nanosleep.o:
0000000000000010 a len
0000000000000008 t loop1
0000000000000008 d output
0000000000000018 d output_end
0000000000000000 b rem
0000000000000000 T _start
0000000000000000 d timespec

cpuidf.o:
0000000000000000 t cpuidf
000000000000000d C output
U _start

screen.o:
0000000000000010 d datasize
0000000000000000 T screen
0000000000000000 t _start
0000000000000000 d testdata





$ ls -al squari // linked squarint.o only

-rwxrwxr-x 1 rnz rnz 16640 feb 11 19:26 squari // 16640 bytes

$ ar r lexa.a _nanosleep.o cpuidf.o screen.o squarint.o

$ gcc -o squari squari.c lexa.a

$ ls -al squari

-rwxrwxr-x 1 rnz rnz 16640 feb 14 18:46 squari // linked lexa.a 16640 bytes ALSO!





Unlike Static libraries, the object code in Shared libraries is not compiled into the executable programs

squari.c ........ squarint.s ........ squaroot.s

as -o squaroot.o squaroot.s

squarint.o ........ squaroot.o





gcc -shared -o libsquari.so squarint.o squaroot.o libsquari.so





cs.c ____ gcc -S cf.c ----> cf.s







To transfer control of a program to a function you need to use the Stack

Stack needs an INDIRECT Addressing (it uses a Pointer to a Memory location instead directly same memory location)

here below is an example concerning IA32 First indirect address of SP is copied in BP. Then inserting (push) new values, SP increases.



Pushing the registers onto the stack before the function code, and Popping them off when the function is ready to return to the calling program













prog.c ........ screen.s ........ screen.o ........ screen


gcc -fpie -o prog prog.c screen


prog



objdump -D prog > disassembled_prog



disassembled_prog




squari.c ........ squarint.s


gcc -fPIE -o squari squari.c squarint.s


squari



objdump -D squari > squaridump



squaridump








~ back ~