dynamic linking
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > dynamic linking




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    dynamic linking  
sam_cit@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-29-07 12:18 PM

Hi Everyone,

dll is used for library which are linked to programs dynamically
and it seems a stub is inserted in the application which takes care of
a loading of the library dynamically into the memory and linking the
same.

However, when i have a program with a reference to a function in a
library, how does the linker know if a dynamic link was meant for a
reference or if a static link was meant. Because if it is a static
link, the linker has to report an error saying function missing etc.

Thanks in advance!!!






[ Post a follow-up to this message ]



    Re: dynamic linking  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-29-07 12:18 PM

On Apr 29, 12:50 am, sam_...@yahoo.co.in wrote:

>     dll is used for library which are linked to programs dynamically
> and it seems a stub is inserted in the application which takes care of
> a loading of the library dynamically into the memory and linking the
> same.

The stub contains the symbols that the dynamically-linked library
provides to the program.

>    However, when i have a program with a reference to a function in a
> library, how does the linker know if a dynamic link was meant for a
> reference or if a static link was meant. Because if it is a static
> link, the linker has to report an error saying function missing etc.

If it finds the symbol in the stub, then the real function is in the
dynamic link library. If it finds the symbol elsewhere, then it links
to it elsewhere. The purpose of the stub is to provide the symbols.

DS






[ Post a follow-up to this message ]



    Re: dynamic linking  
sam_cit@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-29-07 12:18 PM

On Apr 29, 2:07 pm, David Schwartz <dav...@webmaster.com> wrote:
> On Apr 29, 12:50 am, sam_...@yahoo.co.in wrote:
> 
>
> The stub contains the symbols that the dynamically-linked library
> provides to the program.
> 
>
> If it finds the symbol in the stub, then the real function is in the
> dynamic link library. If it finds the symbol elsewhere, then it links
> to it elsewhere. The purpose of the stub is to provide the symbols.
>
> DS

Ok you seem to indicate that the symbol that the stub provides helps
the linker to conclude on the type of library ( static or dynamic
library), however, who inserts the stub? is the programmer's
responsibility? or does the compiler includes the stub?

If the compiler includes the stub, on what basis does it do? what if
my dll is not ready by the time application is being developed and
build?

Thanks in advance!!!






[ Post a follow-up to this message ]



    Re: dynamic linking  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-29-07 12:18 PM

On Apr 29, 3:34 am, sam_...@yahoo.co.in wrote:

>  Ok you seem to indicate that the symbol that the stub provides helps
> the linker to conclude on the type of library ( static or dynamic
> library), however, who inserts the stub? is the programmer's
> responsibility? or does the compiler includes the stub?

I thought the specific case we were dealing with is where you linked
to a stub. If you don't link to a stub, then it doesn't work this way.

> If the compiler includes the stub, on what basis does it do? what if
> my dll is not ready by the time application is being developed and
> build?

The DLL is not needed to make the stub, only the interface is. If the
interface is not ready, you can't write a program that will later use
the DLL anyway.

Stubs are not the only way to access a DLL, you can also do it
'manually'. Basically, a stub just automates the process of loading a
DLL, finding the appropriate symbol, and calling it.

Think of a stub as a piece of code that does two things:

1) It loads the library when the program starts.

2) It creates wrapper functions for every symbol in the library's
interface that calls the appropriate function in the library. (This
may be done another way, but it has this logical effect.)

Only the name of the library (to do part 1) and its interface (to do
part 2) are needed to create the stub.

DS






[ Post a follow-up to this message ]



    Re: dynamic linking  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-29-07 06:17 PM

sam_cit@yahoo.co.in wrote:
> Hi Everyone,
>
>     dll is used for library which are linked to programs dynamically
> and it seems a stub is inserted in the application

You are stating an assumption that a stub is used, but I think you
are really asking about dynamic linking in general.

I don't think it is usually done with a stub.  There are two common
ways to do this, as far as I know (though there may be more that I
don't know of).

The first way is to use a jump table, which is just an array of function
pointers.  Every function that has been linked dynamically would have
an entry in the jump table.  When the executable is loaded, the dynamic
libraries would be loaded into memory and the jump table would be filled
in with the address of each of the functions.  As a function is called,
it would simply read the address out of the jump table and jump to the
address.  This would take it to the code in the dynamic library.
(Returning to the caller is handled because the address is already on
the stack.)

The second method is to build a list of call sites.  That is, in the
client of library foo, there will be a bunch of calls to foo().  Each
call to foo() will have a spot for foo()'s address inside an opcode
or somewhere else embedded within the code.  (The address may be
PC relative or may be absolute.)  You can make a list of call sites
(and the addresses where they store the address of foo()) and build
that list into the executable.  When the executable is loaded, the
dynamic library will be loaded into memory.  Then, the address of
foo() is known, and you go through the list of call sites and modify
the code so that all the call sites have the real address of foo().
(For relative addresses, you have to do some subtraction to find the
correct address to insert.)  In a sense, you are actually not finishing
the code generation until runtime.  You write a fake address into the
code until you know the real one, then fill it in later.

I believe the second method is more popular because it results in
more efficient function calls because there is no overhead of looking
things up in a jump table.

>    However, when i have a program with a reference to a function in a
> library, how does the linker know if a dynamic link was meant for a
> reference or if a static link was meant.

It knows because when you link against the library, it reads the
format of the library file and sees something in the format of the
file that tells it what type of library you are linking against.

> Because if it is a static
> link, the linker has to report an error saying function missing etc.

The linker will also report an error if the function is missing
for dynamic libraries, not just for static libraries!  If the
function is missing, the linker does not know how to call the
function because it does not know the arguments it takes and so on.

It would certainly be possible to build a system where the linker
does not do any verification of calls to functions in dynamic
libraries at link time.  You could just include the function's
name and not verify its existence (or verify the arguments), but
that would be error-prone.  I am uncertain, but I believe the
linker on a Unix system will verify both of these.

- Logan





[ Post a follow-up to this message ]



    Re: dynamic linking  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-01-07 12:22 PM

On 29 Apr, 08:50, sam_...@yahoo.co.in wrote:

>     dll is used for library which are linked to programs dynamically
> and it seems a stub is inserted in the application which takes care of
> a loading of the library dynamically into the memory and linking the
> same.
>
>    However, when i have a program with a reference to a function in a
> library, how does the linker know if a dynamic link was meant for a
> reference or if a static link was meant. Because if it is a static
> link, the linker has to report an error saying function missing etc.

It often helps starting looking for answers at wikipedia:
http://en.wikipedia.org/wiki/Shared_libraries
In section "External links" on that page you can find wealth of in-
depth information.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:29 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register