Results 1 to 6 of 6
Hi,
I have a question about shared objects and when mapping and linking is established in the following code...Well more of a verification.
getsetx.c - shared object source code
Code:
...
- 06-13-2010 #1
[SOLVED] Shared objects - mapping and linking
Hi,
I have a question about shared objects and when mapping and linking is established in the following code...Well more of a verification.
getsetx.c - shared object source code
getsetx.c compile linesCode:unsigned long x = 0; unsigned long getx(void) { return x; } void setx(unsigned long val) { x = val; }
gcc -fPIC -c getsetx.c
ld -shared -o getsetx.so getsetx.o
testit.c - Main executable source
testit.c compile lineCode:#include <stdio.h> #include <stdlib.h> #include <stdbool.h> extern unsigned long getx(void); extern void setx(unsigned long); int main(int argc, char**argv) { int choice = 0; unsigned long val = 0; while (true) { fputs("(0)exit (1)view (2)set->", stdout); fscanf(stdin, "%d", &choice); if (choice < 1) break; if (choice == 1) fprintf(stdout, "ans->%lu\n", getx()); if (choice == 2) { fputs("enter a value->", stdout); fscanf(stdin, "%lu", &val); setx(val); } } exit(EXIT_SUCCESS); }
gcc testit.c ./getsetx.so -Wall -ansi -pedantic -o testit
Now its my understanding when I execute ./testit, getsetx.so will get mapped into its address space at start up and testit will link any functions as they are needed...So is this correct, because this is what I'm experiencing...Thank-you, Gerard4143Make mine Arch Linux
- 06-14-2010 #2Linux User
- Join Date
- Jan 2006
- Posts
- 414
Umm, your compile lines look a bit out to me, and will result in the .so being build into the executable, rather than linked against as a share object...
Code:gcc -fPIC -c getsetx.c ld -shared -o libgetsetx.so getsetx.o -Wl,-soname,libgetsetx.so
Would result in it behaving correctly I think.Code:gcc testit.c -L/path/to/shared/object/parent/directory -lgetsetx -Wall -ansi -pedantic -o testit
Last edited by darkrose0510; 06-14-2010 at 05:47 AM.
- 06-14-2010 #3Linux Newbie
- Join Date
- Mar 2010
- Posts
- 121
I'm quite sure you can reference shared objects directly - using ./blah.so will cause the dynamic linker to look for blah.so in the current directory, which is one way you can bypass its normal search paths. You can compile the sample programs and run ldd on the resulting executable to check.Umm, your compile lines look a bit out to me, and will result in the .so being build into the executable, rather than linked against as a share object...
That's basically it - although for small shared libraries, the dynamic linker seems to load functions into memory at program load-time, as opposed to when they're needed (for example, you could probably delete or move getsetx.so after you start testit, and everything would be fine.Now its my understanding when I execute ./testit, getsetx.so will get mapped into its address space at start up and testit will link any functions as they are needed...So is this correct, because this is what I'm experiencing...
You can set LD_DEBUG=all in the environment to get the dynamic linker to print out what it's doing (or LD_DEBUG=help for debug options).
- 06-14-2010 #4
Thank-you for the replies...One comment. If the executable(testit) maps the shared object(getsetx.so) into its address space at start up, why would it matter if the shared object is moved or deleted after start-up.....testit has a copy(MAP_PRIVATE) at start up so any changes or deletions should go unnoticed.
Make mine Arch Linux
- 06-14-2010 #5Linux Newbie
- Join Date
- Mar 2010
- Posts
- 121
I thought that large object files (not sure how large) may be loaded on demand in order to save memory - however, looking into it a bit more, I can't find anything on t'web to really back that up, so I don't know where I got that from. Sometimes things like that just get into my head for no reason.
Either way, symbols are only resolved on demand (though you can change this by setting LD_BIND_NOW in the environment) even if the associated data is already in memory.
- 06-14-2010 #6


Reply With Quote
