static library and dynamic library

E

Even

Hi all,

As far as I know, relative address will be assigned by program at link
time.Right? Ok, here we go.

So if we use a static library, it will give the program an relative
address(i mean the function address) at link time.
Also the code of the library will load into the program. Go on.

Now if we use a dynamic library, what it will give the program?
A symbolic? or Address ?

if it is a symbolic, when does the symbolic change to an address? and
How does it load its code to the program?


Thanks
Even
 
J

jacob navia

Even said:
Hi all,

As far as I know, relative address will be assigned by program at link
time.Right? Ok, here we go.

So if we use a static library, it will give the program an relative
address(i mean the function address) at link time.
Also the code of the library will load into the program. Go on.

Now if we use a dynamic library, what it will give the program?
A symbolic? or Address ?

if it is a symbolic, when does the symbolic change to an address? and
How does it load its code to the program?


Thanks
Even

Under the windows system, dynamic link libraries are
relocated to some address at load time. The linker
produces a relocation table that allows loading the
library at any address
 
R

Richard Bos

Even said:
As far as I know, relative address will be assigned by program at link
time.Right?

Or wrong. Or pale blue with dots. All this is entirely system-dependent.
It may even be (but possibly isn't) compiler-dependent. You'll have to
ask this question in a newsgroup that specialises in your OS (somewhere
in microsoft.public.*, by the looks of it), and there you'll probably
need to specify which compiler you're using as well.

Richard
 
C

cr88192

Even said:
Hi all,

As far as I know, relative address will be assigned by program at link
time.Right? Ok, here we go.

'relative' is not a good term here.

So if we use a static library, it will give the program an relative
address(i mean the function address) at link time.
Also the code of the library will load into the program. Go on.

yeah, at link time, all the code and data from the library goes into the
binary, which is part of why using lots of static libs tends to produce
large binaries...

the code in the app is linked directly to the code in the library, just the
same as with local code.

Now if we use a dynamic library, what it will give the program?
A symbolic? or Address ?

it depends.

if it is a symbolic, when does the symbolic change to an address? and
How does it load its code to the program?

this depends on the OS.

note that there may be errors here, all this is not something I have looked
into in too much detail.


on linux and friends, it will produce a binary that is like "oh well, I am
not fully linked, load these libraries on loading". so, the loader will also
grab the libraries, and resolve the addresses to the addresses in the
library (internally, this will look about the same as if the app had used a
static library).

the exact mechanics of this process are not something I will really go into.

note that in linux, these libs tend to use position-independent-code, which
is fun, since the same library pages can be shared between different
processes, and located at different locations in each (GOT magic...). in
windows, however, DLLs are typically fixed-address to allow sharing
(relocating DLLs is also possible, but they end up having their own pages in
each process...).


now, I think, DLLs are a little different.
as far as I understand it, with DLLs you end up with a number of proxy
functions, which jump into the function through a jump table.

so, at load time, the app is loaded, and either the loader (or part of the
app's runtime, or at least, I think this is how it worked at one time) sees
that a DLL is needed, so it loads the DLL and fills in the various slots in
the table.

in the past, this was done via import libraries (a special tool was used to
make a static library from the DLL), but (at least for GNU-ld), all this
magic is done by the linker itself.

but, none of this is anything I have really looked into in too much
detail...
 
E

Even

Thanks for your all reply, especially cr88192.

That is, in linux different processes share the the same code(Dlls),
but in window they copy the code to their space.

Now we have a global variable(g_var) in Dlls, in linux if one process
change the *g_var*, and does it effect another process?

by the way, what's the different between 'load time' and 'running
time'. Does 'OS' control 'load time'?
 
C

cr88192

Even said:
Thanks for your all reply, especially cr88192.

That is, in linux different processes share the the same code(Dlls),
but in window they copy the code to their space.

linux usually shares code. this is because shared-objects tend to be
compiled with position-independent-code.

windows tries to share pages, but does not if the hard-coded address can't
be used (many DLLs are pre-relocated to be loaded at certain addresses...).
Now we have a global variable(g_var) in Dlls, in linux if one process
change the *g_var*, and does it effect another process?

probably, linux likely handles shared libraries similar to, fork(), namely
that any shared pages are copy-on-write.
by the way, what's the different between 'load time' and 'running
time'. Does 'OS' control 'load time'?

load time is, when the app is being loaded. runtime generally means
"whenever the app so feels like it".
yes, usually load-time is OS controlled.


or such...
 
M

Mark McIntyre

linux usually shares code. this is because shared-objects tend to be
compiled with position-independent-code.

Would you please stop answering this highly-offtopic question in CLC?
The OP will start to think they can get all their programming
questions answered here, and next time it may be even more wildly
offtopic
windows tries to share pages, but does not if the hard-coded address can't
be used (many DLLs are pre-relocated to be loaded at certain addresses...).

And please don't say "well ,where /can/ you ask about windows _and_
linux at the same time" because thats not relevant. I've no idea where
you ask about bondage _and_ buddism at the same time, but that doesn't
make it topical in CLC ....

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top