4 ms·
For a long time, I thought that the ELF shared library model was the obviously correct thing, but over the past few years, I've come to realize that Windows got
by quotemstr 14y ago
For a long time, I thought that the ELF shared library model was the obviously correct thing, but over the past few years, I've come to realize that Windows got shared libraries right after all.
On Windows, all inter-module dependencies are (module, symbol) pairs (written "module!symbol"), not just bare symbol names as on ELF systems. That is, modA!fun1 calling modB!fun2 can exist in the same process as a modC!fun1 calling modD!fun2. The Windows model doesn't permit global interposition, but that's a good thing: the lack of interposition support permits the optimizations Macieira mentions, but "hooking" is still possible through a variety of mechanisms, which usually involve either overwriting import and export tables or the replacement of function preambles with jumps to trampolines.
Regardless of the performance considerations, I think the Windows DLL approach is the more robust and conceptually lighter one. That the Windows approach is also faster is simply a beneficial side effect.
- saurik 14y agoMach-O, the executable format used by Darwin (and hence Mac OS X) also has that feature, where it is called "two-level namespaces".
- quotemstr 14y agoSure. Windows has one additional feature that dyld lacks, however: activation contexts[1]. You can have in the same process modAv1!foo and modAv2!foo even if modAv1 comes from c:\foo\moda.dll and modAv2 comes from c:\bar\moda.dll. (Note that the module filenames are identical.) [1] http://msdn.microsoft.com/en-us/library/windows/desktop/aa374153%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/windows/desktop/aa37...
- saurik 14y agoMach-O's two-level namespace does not work based on the one-level filename of the library on disk: it works based on the canonical full path stored in the LC_ID_DYLIB load command of the library; these names should contain the version number of the library, and so provides the functionality that you seem to be referencing (although I am very rusty at Windows and never knew how things worked at this particular level, so I'm working off of the short paragraph you just linked). As an example: /usr/lib/libreadline.dylib is actually /usr/lib/libedit.2.dylib /System/Library/Frameworks/Python.framework/Python is actually /System/Library/Frameworks/Python.framework/Versions/2.6/Python You can thereby have symbols linked from both /System/Library/Frameworks/Python.framework/Versions/2.5/Python and /System/Library/Frameworks/Python.framework/Versions/2.6/Python. "(Note that the module filenames are identical.)"
- quotemstr 14y agoOh, cool. Thanks! My OS X is a bit rusty.
- FooBarWidget 14y agoWhile ELF has its problems, I wouldn't call Windows's approach 'right', by a long shot. * Writing libraries is a big hassle in Windows because you have to explicitly define all exportable symbols. * DLLs have all kinds of strange boundary rules. C++ exceptions cannot pass DLL boundaries. Heap memory allocated by one DLL cannot always be freed by another. Etc etc.
- shin_lao 14y agoThe first problem is actually not a problem but one of the reasons DLLs are better: private by default reduce linking problems. The second problem has got nothing to do with DLLs and everything to do with the fact that there is no C++ ABI. As for memory allocation, it's not really a good idea to allocate memory in a library and free it in another, is it?
- npsimons 14y agoAs for memory allocation, it's not really a good idea to allocate memory in a library and free it in another, is it? Ah, I guess I'll have to stop using strdup() then.
- shin_lao 14y agostrdup(), part of the libc, allocates memory with a function from the libc (malloc), therefore you are allocating and freeing memory with functions within the same library. However, yes, for security reasons strdup should be avoided.
- pjmlp 14y ago> Writing libraries is a big hassle in Windows because you have to explicitly define all exportable symbols. Just like in Aix, unless it is now changed (my experience goes back to 2001). > C++ exceptions cannot pass DLL boundaries. C++ restrictions have to do with lack of C++ ABI. You have in same issue in ELF systems, when mixing compilers. > Heap memory allocated by one DLL cannot always be freed by another. This is a good thing. Object handles belong to the module that created them, and it is a good way to ensure a clean way to use accessor functions to deal with data.