LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2005, 04:06 PM   #1
juanbobo
Member
 
Registered: Mar 2005
Location: Chicago
Distribution: Gentoo AMD64
Posts: 365

Rep: Reputation: 30
how linking in linux works


Could someone explain how linking works in Linux? I am not asking how to create or use shared libraries, I am wondering how/when exactly the libraries are loaded and called.
 
Old 06-08-2005, 08:42 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
I'm not exactly certain I understand what you are asking for. If I understand correctly, you want to know when/how a library file is called?

Look through your systems files. Look for files with the .h extension (header files). Near the top of such files, you will see lines with #include <somefile.h>. That (at least in the C programming language) is the call to the compiler to "include" this file from some library when the program is compiled. In other words, that part is written into the compiled application, and only that part of the library. There is no call, per se, to the library. The included part is in the binary compiled application.
 
Old 06-08-2005, 09:23 PM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Not exactly -- the header file just specifies the interface to a particular file. The actual code of the library routines is in a library file (static, generally ending in .a followed optionally by a version number or dynamic ending in so with an optional version number). Most executables are linked dynamically, i.e. the code of the library routines is not inserted directly into the executable, as this would make the executable file very big and inefficient in other respects. Instead, the dynamic linker in Linux will load the library code from the appropriate library file if needed and insert it into the process address space at runtime (thus the system need only keep one copy of the library in memory in most cases).

Or do you mean links in the file system? If so, I wrote a long post about the internals of file system links about a month or two ago, which should be found in the search.
 
Old 06-08-2005, 09:48 PM   #4
juanbobo
Member
 
Registered: Mar 2005
Location: Chicago
Distribution: Gentoo AMD64
Posts: 365

Original Poster
Rep: Reputation: 30
Thanks guys, I should have been more specific. I had to check an old book on protected mode to remind me about descriptor tables and whatnot. I guess to know the workings any deeper I'll have to look at the linker code.

Last edited by juanbobo; 06-08-2005 at 10:10 PM.
 
Old 06-09-2005, 05:37 PM   #5
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
the linker puts only the name of the shared library in the executable code.
unless you use prelink i think which alters the executable to point to a preseeked table for the lib and its dependancies.
shared library is actually one single position independent object file and when the executable comes across it it includes (mapps) all of the code in the library and its dependancies not just what it needs.
then the various functions in the shared library may be mapped to different virtual addresses for each program (process) using that function and when and how all that actually gets into RAM is just kernel MM junk

basically all ld does is look for and find the library and all of the libraries dependancies i think
 
Old 06-09-2005, 10:08 PM   #6
juanbobo
Member
 
Registered: Mar 2005
Location: Chicago
Distribution: Gentoo AMD64
Posts: 365

Original Poster
Rep: Reputation: 30
Thank you very much foo_bar_foo, you answered my question.
 
Old 02-16-2006, 04:20 AM   #7
ravi
LQ Newbie
 
Registered: Jan 2006
Location: CHENNAI
Distribution: RH9(2.4.20-8)
Posts: 13

Rep: Reputation: 0
Strange problem accessing the stack

Hi people,
I am trying to play with shared libraries. I have written a shared library(libgcall) and I link it with another program written by me. In one module of the shared library I am trying to get the stack address as below:

Code:
int get_the_stack_addr()
{
	int *ptr,var;
	ptr = &var;
	printf("%p",ptr);
	/* Both ptr and var will be on the stack and ptr will contain address of var i.e. address of 		   stack
	*/
	return (1);
}
Now if I know it right, the shared library will share the same stack as that of the test progam. But the results are different. I get a stack address 0x4212ee20. The test program stack address is 0x8048918.

Now is the interesting part. If I declare any variable like I did below:
Code:
int get_the_stack_addr()
{
	int *ptr,var;
	int a;
	ptr = &var;
	printf("%p",ptr);
	/* Both ptr and var will be on the stack and ptr will contain address of var i.e. address of 		   stack
	*/
	return (1);
}
I get the same address of the test program stack(0x80489180).
There seems to be no logic or reason to it. Can anyone help?
Thanks in advance
 
Old 02-16-2006, 12:54 PM   #8
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-17-2006, 11:36 AM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
A function in a shared library uses the same stack as the function which calls it.

Code:
/* gcall.c */
#include "gcall.h"
#include <stdio.h>

int get_the_stack_addr(void)
{
	int *ptr, var;
	ptr = &var;
	printf("%p\n", ptr);
	return 1;
}
Code:
/* testlib.c */
#include "gcall.h"
#include <stdio.h>

int main()
{
	int a;
	printf("main sees stack at %p\n", &a);
	get_the_stack_addr();
	return 0;
}
Code:
12:10 aluser@alf:~/test/c/libstack$ make testlib
gcc -Wall  -c testlib.c
gcc -Wall  -fPIC -shared -c gcall.c
gcc -Wall  -shared -o libgcall.so gcall.o
gcc -Wall  -L. -Wl,-rpath,"`pwd`" -o testlib testlib.o -lgcall
12:10 aluser@alf:~/test/c/libstack$ ./testlib
main sees stack at 0xbfcce604
0xbfcce5cc
12:10 aluser@alf:~/test/c/libstack$ ./testlib
main sees stack at 0xbfdae2f4
0xbfdae2bc
12:10 aluser@alf:~/test/c/libstack$ ./testlib
main sees stack at 0xbfcf4ba4
0xbfcf4b6c
Each time the program runs, the stack starts at a slightly different place, but get_the_stack_addr() always sees a stack address just below that which main sees, as you'd expect for a downward growing stack.

The different stack starting points occur even for static executables; I suspect that my system is doing this in order to make certain exploits harder to run. Actually I'm waving my hands a bit here; it seems that it's actually the OS which sets up the stack pointer at the beginning, since I can eliminate _start() and still see this variation:

Code:
/* esp.s */
.data
someint:
        .int 4
.text
.global _start
_start:
        movl %esp, someint
        movl $4, %eax
        movl $1, %ebx
        movl $someint, %ecx
        movl $4, %edx
        int $0x80
        movl $1, %eax
        movl $0, %ebx
        int $0x80
This moves %esp into someint, then calls the write() syscall on &someint. You can get human readable output by piping through xxd (remember that it's little endian, so a05b96bf means bf965ba0):

Code:
12:32 aluser@alf:~/test/asm$ gcc -c esp.s
12:32 aluser@alf:~/test/asm$ ld -o esp esp.o
12:32 aluser@alf:~/test/asm$ ./esp|xxd -g4
0000000: a05b96bf                             .[..
12:32 aluser@alf:~/test/asm$ ./esp|xxd -g4
0000000: 406ba4bf                             @k..
12:32 aluser@alf:~/test/asm$ ./esp|xxd -g4
0000000: d02fd4bf                             ./..
So yeah.. conclusion is that linux varies the starting position of the stack, but that a function being in a shared library or not doesn't change what stack it uses. (The only "normal" way I know for a program to run functions on a different stack is inside signal handlers)

wow that was long.

Last edited by aluser; 02-17-2006 at 11:45 AM.
 
Old 02-17-2006, 03:09 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
To partially answer the OP, I too would like to have a decent source on linking in linux. What I've been able to gather for shared libraries is that your program gets an executable .plt section which contains stubs for all the shared lib functions you call; each jumps to an address which is stored in a writable secion called .got.plt (got stands for global offset table). It looks like there may be an additional indirection too, which has something to do with the fact that the offset table isn't necessarily loaded with all the shared lib addresses to begin with; sometimes it has to be loaded on demand (?)

The linker must do some of this at runtime, perhaps before _start() is even called (?)

I worked out some of this with objdump; see the -d, -s, and -j options. I googled for some of the symbols I came across with that. There must be some better way to get this information : )
 
Old 02-17-2006, 11:14 PM   #11
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
http://wiki.linuxquestions.org/wiki/...ands_and_Files
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking three Linux systems together... SonoranFun Linux - Networking 3 06-29-2005 05:49 AM
Linking with posix4 on Linux ? colmmagoo Linux - Software 0 08-05-2004 03:24 PM
Trouble linking with libmqueue on Linux 2.6.5 -mm illuminatedwax Programming 0 04-14-2004 07:55 AM
linking Windows/Delphi/Linux deadlock Programming 3 10-14-2003 05:09 PM
Linking Linux to a intranet webpage... Garfman Linux - Software 0 09-10-2001 06:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration