Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Write an article for LinuxForums Today! Win Great Prizes!
I'm trying to figure out how dynamic libraries work in Linux(or at least the concept) and I'm having a difficult time of it so I created
this oversimplified piece of assembler code in hopes that it represents at least in principle the calling of a dynamic linked function.
Note: CPU - AMD athlon 64 bit
Now my question is, is this even close to how dynamic libraries work(the linux loader is simulated
by the statement movq $printit, %rax like I said oversimplified)?
I know this is a gross oversimplification but is it correct in spirit....Please see code and thanks for your input...Gerard4143
Note: there is no errors in the code...it will compile and run correctly.
Code:
.section .data
mydata: .ascii "hello world!\n"
.equ mylen, . - mydata
myvar: .quad 0
.section .bss
.section .text
.global _start
_start:
movq $printit, myvar /*simulated linux loader loading*/
/*function address into fix location*/
call tohere /*push instruction pointer*/
tohere: /* onto the stack*/
jmp tryit /*so we can return*/
movq $60, %rax /*exit routine*/
movq $0, %rdi
syscall
tryit:
call *myvar /*call the fix address*/
popq %rax /*pop the instruction pointer - return address*/
addq $2, %rax /*add two to it*/
jmp *%rax /*and jump*/
printit: /*a function with an arbitrary address*/
/*it isn't here but it could have been*/
pushq %rbp
movq %rsp, %rbp
movq $1, %rax
movq $1, %rdi
movq $mydata, %rsi
movq $mylen, %rdx
syscall
movq %rbp, %rsp
popq %rbp
ret
I've done absolutely no assember language coding since before Linux came along, but you can get a good start at understanding how dynamic libraries work by linking dynaimcally a C program which calls a function in a dynamic library specifically written by you for this test. You can observe the assembler language two ways:
by using the -S option while compiling; and
by running the dynamically linked program under strace.
I've looked briefly at dynamically linked UNIX code a few years back, and Linux code since then (too lazy to try it again tonight), and the generated code seems to have done an mmap() with the file that contains the library. If several programs do that with the same library, they'll share the same library code image, just as they'd share the same data if they weren't doing linking, but had some other purpose for mmap()ping a file without using PROT_WRITE.
That's just my foggy memory speaking, though. Try it! :)
__________________ --
Bill
Old age and treachery will overcome youth and skill.
Thanks for the reply Bill, your answers are a little beyond what I was looking for right now, maybe if I stated it another way...
Does the Linux loader assign dynamic addresses to known address spaces in an object file and are these known addresses accessed with jump tables within the object file?
Again thanks for your reply...I'll be investigating the information you provided as soon as I get this straightened out...Gerard4143
1. the linux loader assigns dynamic addresses into the GOT section so that the user process
can access these values in fixed address locations
2. the user process uses the PLT section as a jump table into the GOT addresses
Now I know this is a simplified version of the process but are my assumptions right? I can't pursue this
topic without a firm understanding of the basics so if anybody has any input...it will be appreciated...
thanks again for your time and effort Gerard4143
"Relocation is handled through an indirection mechanism called the Global Offset Table (GOT) and the Procedure Linkage Table (PLT).
These tables provide the addresses of external functions and data, which ld-linux.so loads during the relocation process. This means that
the code that requires the indirection (that is, uses the tables) needs no changes: only the tables require adjustment. Relocation can occur
immediately upon load or whenever a given function is needed. (See more on this difference later in Dynamic loading with Linux.)"
A great blurb but it really doesn't say anything specific...It doesn't answer my questions that I posted above.
That is the problem with this topic, its either to general or to technical to get any instruction from...not to worry, I'm stubborn I'll figure this out and post it...
I'm going to assume that my understanding of this process is correct and start exploring the mmap() function that bill eluded to...
Thanks again for all the help if I find a definite answer I'll post it...Gerard4143
now the C code:
Note I found the value of myvoid = 0x600fd8 from the above objdump
Code:
#include <stdio.h>
#include <stdlib.h>
int x = 5;
int getx()
{
x = 88;
}
void **myvoid = NULL;
int main(int argc, char**argv)
{
myvoid = (void**)0x600fd8; /*pointer to GOT which contains
a pointer to the global varable x*/
fprintf(stdout, "ptr->%p\n", *myvoid);
fprintf(stdout, "x->%p\n", &x);
getx();
return x;
}
and the result of running the code:
the global variable x has an address 0x601020
and we have a pointer in the GOT equal to 0x601020
Code:
ptr->0x601020
x->0x601020
So the GOT, I hope, is a place in memory to store pointers to global variables and functions
so that they can reside anywhere
and the C code with the address 0x 601008 extracted from
the objdump above
Code:
#include <stdio.h>
#include <stdlib.h>
int x = 5;
int getx()
{
x = 88;
}
void **myvoid = NULL;
int main(int argc, char**argv)
{
//the test for the global variable x
myvoid = (void**)0x600fd0; /*pointer to GOT which contains
a pointer to the global varable x*/
fprintf(stdout, "got ptr to x->%p\n", *myvoid);
fprintf(stdout, "x------------>%p\n", &x);
//the test for the fprintf
myvoid = (void**)0x601008; /*pointer to GOT which contains
a pointer to the global varable fprintf*/
fprintf(stdout, "fprintf------------>%p\n", fprintf);
fprintf(stdout, "got ptr to fprintf->%p\n", *myvoid);
getx();
return x;
}
and the answer
Code:
got ptr to x->0x601020
x------------>0x601020
fprintf------------>0x7f8bc3cc9ec0
got ptr to fprintf->0x7f8bc3cc9ec0
What does this all mean to me...well the call to the function fprintf is accomplished by the call to the <fprintf@plt>
which is a lauching point or jump to the got table where the address of fprintf resides...
Please, if this is incorrect let me know...Gerard4143
Open Source Security Myths Dispelled Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization subscribe
InformationWeek InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology. subscribe