[Solved] LoadLibrary('test.dylib') appears to work but not GetProcedureAddress lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- Ozz_Nixon · 2016-10-09 19:27 Example works perfect in a Linux SSH session... same compiler v2.6.4 on Darwin, appears to load dylib fine, but never returns GetProcedureAddress. nm test.dylib | grep 'T ' shows the exported method names. I have tried upper, lower, propercase, prefixed "_", etc. nada. However same code runs perfectly on my Linux box. Attached is like 50th revision of trying to get this to work - experimented with cdecl, stdcall, etc. also. Attachment: buildunit (shell script to compile paramstr(paramcount)) sdl.pp  (test dll source - trying to access MODERNPASCALLINKHOOK (in any case)) loaddll.pp (source to host that tries to load and getproc MODERNPASCALLINKHOOK and fails w/o error) * code works perfecly on Linux 64bit, darwin i386 no luck. Free Pascal Compiler version 2.6.4 [2014/02/26] for i386 uname -a Darwin IDS-MacBook-Pro.local 15.5.0 Darwin Kernel Version 15.5.0: Tue Apr 19 18:36:36 PDT 2016; root:xnu-3248.50.21~8/RELEASE_X86_64 x86_64 ---------------------------------------------------------------------- tk · 2016-10-09 23:32 Some time ago I had similar problems but solved them. I assume you've already read this my post: http://forum.lazarus.freepascal.org/index.php/topic,32751.0.html There cdecl solved the issue, although it was strange error. I don't have access to OS X anymore but try to use shorter name. SDLTestHook you've exported works? ---------------------------------------------------------------------- Thaddy · 2016-10-10 05:47 Quote from: tk on October 10, 2016, 01:32:55 amThere cdecl solved the issue, although it was strange error. Not strange, but as Jonas explained a name mangling issue and a register issue because stdcall and cdecl are very different. In this case, if it is not the cdecl issue I suspect either a case sensitivity issue in the names or there may be a genuine bug. External procedure names are case sensitive! But you should  upgrade to a supported FPC version (3.0.0)  and check if that fixes the issue. Nobody is going to try and replicate the issue if it won't get fixed in 2.6.4. anyway. ---------------------------------------------------------------------- tk · 2016-10-10 07:32 QuoteNot strange, but as Jonas explained a name mangling issue and a register issue because stdcall and cdecl are very different. Don't remember that anymore exactly, but the error was really strange. The names were ok when viewed with nm etc. Yes cdecl and stdcall are different but that does not explain why (in my above case) localizationLoad worked and LocalizationLoad not (at least while linking the lib). In my opinion the issue was definitely an XCode bug. And in this case I expect something similar. ---------------------------------------------------------------------- Jonas Maebe · 2016-10-10 08:19 Quote from: tk on October 10, 2016, 09:32:21 amYes cdecl and stdcall are different but that does not explain why (in my above case) localizationLoad worked and LocalizationLoad not (at least while linking the lib). In my opinion the issue was definitely an XCode bug. And in this case I expect something similar. It's not an Xcode bug. Symbols that start with uppercase 'L' are defined by Apple as local labels, and hence cannot be exported. In fact, in principle all global symbol names that do not start with '_' are reserved for system use on Mac OS X/OS X/macOS/... If you use "cdecl", FPC ensures that your function completely adheres to all ABI requirements (naming, parameter passing, ...). If you don't, then you are on your own (although a warning would be appropriate). ---------------------------------------------------------------------- Jonas Maebe · 2016-10-10 08:43 Quote from: ozznixon on October 09, 2016, 09:27:52 pmExample works perfect in a Linux SSH session... same compiler v2.6.4 on Darwin, appears to load dylib fine, but never returns GetProcedureAddress. nm test.dylib | grep 'T ' shows the exported method names. I have tried upper, lower, propercase, prefixed "_", etc. nada. However same code runs perfectly on my Linux box. 1) you should use TLibHandle instead of longint for your library handle (otherwise your code will fail quickly when compiled for 64 bit): http://www.freepascal.org/docs-html/rtl/dynlibs/loadlibrary.html 2) it's cleaner to use a pointer instead of an int64 to store the result of getprocedureaddress 3)  the way to get an error from the dynlibs routines is using http://www.freepascal.org/docs-html/rtl/dynlibs/getloaderrorstr.html . Note that using "writeln('xyz', GetLoadErrorStr)" (or even GetLastOSError) is always unsafe, because the "write('xyz')" part may result in a system call, in which case your GetLoadErrorStr/GetLastOSError will give you the error code of that "write" rather than of whatever operation you just executed. In this case it's fine for Linux and OS X, but e.g. on Windows even in this case it would mess up tings 4) the dynlibs unit makes use of libdl on Unix platforms. The libdl function to look up a symbol name is "dlsym". From "man dlsym" on OS X: Quote     The symbol name passed to dlsym() is the name used in C source code.  For      example to find the address of function foo(), you would pass "foo" as      the symbol name.  This is unlike the older dyld APIs which required a      leading underscore.  If you looking up a C++ symbol, you need to use the      mangled C++ symbol name. If you declare a function foo() in C, on OS X the symbol name for that function will be _foo (see my previous post for why). If you declare a function as "cdecl" in FPC, FPC will do the same with the symbol name. If you want to manually hack around that with explicit names, then you have to export the name of the function as '_ModernPascalLinkHook' (or '_MODERNPASCALLINKHOOK') while passing ModernPascalLinkHook (resp. MODERNPASCALLINKHOOK) —i.e., "the name used in C source code"— to getprocedureaddress. ---------------------------------------------------------------------- tk · 2016-10-10 09:45 Quote from: Jonas Maebe on October 10, 2016, 10:19:53 amSymbols that start with uppercase 'L' are defined by Apple as local labels, and hence cannot be exported. In fact, in principle all global symbol names that do not start with '_' are reserved for system use on Mac OS X/OS X/macOS/... If you use "cdecl", FPC ensures that your function completely adheres to all ABI requirements (naming, parameter passing, ...). If you don't, then you are on your own (although a warning would be appropriate). Now I see, was not said in my thread. Warning should be there for sure, at least for those of us (me inclusive) not familiar in every detail with OSX playing rules, but just trying to port an app to OSX. A link to those ABI rules (could not find now quickly on google) would be IMO desirable as well (here or better on OSX wiki). ---------------------------------------------------------------------- Ozz_Nixon · 2016-10-10 22:40 I appreciate everyone's feedback, I have tried all of the suggestions (most already)... I did incorporate the proper error string and now I get: # ./loaddll libsdl.dylib LoadErrStr: OS Error? : 0 DLL handle: 1052144 Nil handle: 0 LoadErrStr: dlsym(0x100df0, SDLTestHook): symbol not found OS Error? : 0 Hook Addr : 0 LoadErrStr: dlsym(0x100df0, MP2linkhook): symbol not found OS Error? : 0 HOOK Addr : 0 # nm libsdl.dylib | grep 'T ' 00000770 T MP2linkhook 000006f0 T SDLTestHook *** then I forced everything lowercase...and leading underscore to force no name mangle anywhere, loaddll still shows me: LoadErrStr: OS Error? : 0 DLL handle: 1052144 Nil handle: 0 LoadErrStr: dlsym(0x100df0, _sdltesthook): symbol not found OS Error? : 0 Hook Addr : 0 LoadErrStr: dlsym(0x100df0, _mp2linkhook): symbol not found OS Error? : 0 HOOK Addr : 0 and nm still shows: 00000770 T _mp2linkhook 000006f0 T _sdltesthook they are being exported as I am trying load them. Lastly, the note on going to 3.0 is impossible - too many changes from 2.6.4 to 3.0 that our project cannot absorb the man hours adjusting and fixing. lastly, Xcode reports: Version 7.3.1 (7D1014) No idea what else to share/try... is there a way to ask the DLL what is exported after loadlibrary? I could live with talking via index, or whatever - have a deadline haunting me now ;-) Ozz ---------------------------------------------------------------------- Jonas Maebe · 2016-10-11 06:40 Maybe I should have been more explicit in my point 4) above. What it boils down to is that the dlsym API, which is used by the dynlibs unit, prepends and underscore to any name you pass it. E.g., if your symbol name in the library is '_foo', you have to pass 'foo' (without the underscore) to getprocedureaddress(). If the symbol name in your library is 'foo', then it is impossible to dynamically look up its address with getprocedureaddress() because the underlying API will always prepend an underscore to whatever you pass in as symbol name. ---------------------------------------------------------------------- Ozz_Nixon · 2016-10-11 15:28 Thanks Jonas - as always you have the answer! library code has "_" to the exports... the loader does not, and now: LoadErrStr: OS Error? : 0 DLL handle: 1052144 Nil handle: 0 LoadErrStr: OS Error? : 0 Hook Addr : 4490992 LoadErrStr: OS Error? : 0 HOOK Addr : 4491120