[fpc-pascal] External library name

Jonas Maebe jonas at freepascal.org
Sun Jan 5 10:43:39 CET 2025


On 05/01/2025 07:15, Hairy Pixels via fpc-pascal wrote:
> On Jan 5, 2025 at 11:53:36 AM, Adriaan van Os via fpc-pascal 
> <fpc-pascal at lists.freepascal.org 
> <mailto:fpc-pascal at lists.freepascal.org>> wrote:
>> This is a habit from the WIndows and Delphi world. In a one-levell 
>> namespace on MacOS, the dll name
>> is superflouous. Note that ObjC symbols are always global ,which 
>> implies a one-level namespace.
> 
> what do you mean a one level namespace?
> 
> I thought the idea of external names is that you could resolve name 
> conflicts. Like if you had “exit” declared in 2 libraries can they both 
> be linked together in one program using the external name? I’m curious 
> about this though because I’ve never seen a way to do that with a 
> linker, all you can ever do is specify libraries then the linker does 
> all the work and if there are duplicates you get an error, end of story.

That is exactly what namespaces mean.

On Windows, every DLL has its own namespace. I.o.w., the external 
symbol's mangled name is in part determined by the DLL it gets imported 
from, and in part by the symbol name itself.

Most Unix platforms use a single-level namespace, where the name of the 
library is irrelevant and all external symbols' names only depend on the 
symbol name. If multiple libraries export the same symbol, generally the 
one of the last one loaded gets used (when manually looking up a symbol 
address at run time, you have more options). Of course, programming 
language themselves can still use namespaces themselves (e.g. Pascal 
units and C++ namespaces).

That said, Darwin also uses two-level namespaces: 
https://blog.darlinghq.org/2018/07/mach-o-linking-and-loading-tricks.html#two-level-symbol-namespace 
. However, it does not do this through name mangling. The linker simply 
encodes in the generated binary in which library/framework it found the 
symbol at link time. Then, at run time, the dynamic linker will only 
look for that symbol in that specific library (unless you force a 
flat/single namespace through an environment variable).

However, as Adriaan also mentioned, an exception to this is Objective-C, 
which has its own name-based resolution logic. And having the same 
Objective-C class defined in multiple files, or having multiple 
categories adding the same method to the same class, will result in a 
random one getting picked at run time. And that results in real-world 
problems: 
https://steamcommunity.com/app/335620/eventcomments/4843147760499908525?snr=1_2108_9__2107&ctp=2#c4840896974229538203


Jonas


More information about the fpc-pascal mailing list