Dynamic linking (DLL/SO); how is it done?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
drunkenfox
Member
Member
Posts: 46
Joined: Tue Mar 13, 2012 10:46 pm

Dynamic linking (DLL/SO); how is it done?

Post by drunkenfox »

I know that the memory manager on most OS's map each process on it's own separate physical addresses, and then gives them the "same" virtual address. But how does this work with dynamic libraries? Does it map the libraries into one physical address, and then remap parts of processes that utilize the library with the same address?
;goodbye OS, hello BIOS
mov eax, FFFFFFF0h
jmp eax
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Re: Dynamic linking (DLL/SO); how is it done?

Post by mallard »

Dynamic libraries are loaded into the same address space as the program that uses them. The "dynamic linker" then modifies the references in the program code so that they point to the library.

Libraries are (usually) "position independent", meaning they can be loaded at any address, with the linker performing any adjustments needed (relocations). They often also have a "preferred" address which they will be loaded at if possible (i.e. that address doesn't conflict with the program itself or any other libraries). When loaded at the preferred address, the relocations aren't necessary.

Additionally, if two (or more) programs load the same library at the same address, the library's code (and some data; usually the 'text' and 'rodata' sections of the binary) can be shared between the programs as long as there is either a guarantee that the library doesn't modify what's ahared (usually by having the memory marked 'read only'), or appropriate measures to ensure that any modifications don't affect other programs (e.g. a "copy-on-write" strategy).
Image
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Dynamic linking (DLL/SO); how is it done?

Post by alexfru »

The parts that aren't going to change (e.g. code and constant data) can exist as a single instance/copy in the physical memory and be available through page translation at the same or different virtual addresses in different processes/virtual address spaces.

The parts that are going to change (data, heap) can either exists as multiple copies in physical memory from the beginning or start out as a single copy in the physical memory and then become separate private copies upon modification (ever heard of copy-on-write?). The virtual addresses, again, can be either the same or different.

Whether or not a library appears at the same virtual address in different processes/virtual address spaces is affected by several things:
- whether the library is compiled as position-independent code or as relocatable
- whether the region of the virtual address space is available, whether or not it's occupied by the main program module or other libraries
Post Reply