Position-independent Code - Windows DLLs

Windows DLLs

Microsoft Windows DLLs are not shared libraries in the Unix sense and do not use position independent code. This means they cannot have their routines overridden by previously loaded DLLs and require small tricks for sharing selected global data. Code has to be relocated after it has been loaded from disk, making it potentially non-shareable between processes; sharing mostly occurs on disk.

To alleviate this limitation, almost all Windows system DLLs are pre-mapped at different fixed addresses in such a way that there is no conflict. It is not necessary to relocate the libraries before using them and memory can be shared. Even pre-mapped DLLs still contain information which allows them to be loaded at arbitrary addresses if necessary.

A sharing technique Windows calls "memory mapping" is sometimes able to allow multiple processes to share an instance of a DLL loaded into memory. However, the reality is that Windows is not always able to share one instance of a DLL loaded by multiple processes. Windows requires each compiled program to know where in its address space each DLL will be accessed — there is no support for position independence.

A DLL specifies its desired base address when it is created, called its RVA (Visual C++ defaults to an offset of 0x10000000). However, if multiple DLLs have the same desired base address, a program cannot relocate them all to that base offset and must specify new offsets when linking. When the Windows loader loads an executable into memory for execution, it checks to see if each DLL has already been loaded with the offset used when the executable was created (not the DLL). If the DLL is not already loaded with that offset, it is relocated to the base requested by the executable. Note that this will provide sharing across multiple processes of the same executable (e.g. if started in different accounts via Fast User Switching), but not necessarily across different programs that link to the same DLL.

Other platforms such as Mac OS X and Linux now support forms of prebinding as well. For Mac OS X the system is called prebinding. Under Linux, the system used is implemented via a program called prelink. This is vastly different from memory mapping.

In the Itanium version of Microsoft Windows, the system has been altered to use position-independent code, however X64 versions still use standard relocation techniques alongside position independent methods and thus cannot be considered true "position independent" libraries.

Read more about this topic:  Position-independent Code

Famous quotes containing the word windows:

    In winter we lead a more inward life. Our hearts are warm and cheery, like cottages under drifts, whose windows and doors are half concealed, but from whose chimneys the smoke cheerfully ascends.... We enjoy now, not an Oriental, but a Boreal leisure, around warm stoves and fireplaces, and watch the shadow of motes in the sunbeams.
    Henry David Thoreau (1817–1862)