HFMU / HaskellFmu

The library package to include in order to develop an FMU with Haskell
Other
1 stars 0 forks source link

Buld static link library #6

Closed CThuleHansen closed 5 years ago

CThuleHansen commented 5 years ago

Figure out how to build library properly. Loading does not work when using RTLD_FIRST flag with dlopen on mac. This seems to be an error because: The haskell dll, call it lib1, does not contain the symbols. Instead, a dll loaded by lib1, call it lib2, does. So RTLD_FIRST makes these symbols not available.Loading does not work when using RTLD_FIRST flag with dlopen on mac.

Update: It that foregn export functions from the example project works, but not the one from haskellFMU, i.e. the imported project. How do I get these listed?

This link describes some arguments to be passed to ghc from cabal new-build https://cabal.readthedocs.io/en/latest/installing-packages.html?highlight=static#cmdoption-setup-configure-enable-static

P14 of FMI spec: https://svn.modelica.org/fmi/branches/public/specifications/v2.0/FMI_for_ModelExchange_and_CoSimulation_v2.0.pdf

RTLD_FIRST? https://github.com/opensource-apple/dyld/blob/3f928f32597888c5eac6003b9199d972d49857b5/doc/man/man3/dlopen.3

Does anyone know what the RTLD_FIRST flag to dlopen mean? I cannot load my dll when it is used, but I can when it is not used
Xkernel has left IRC (Quit: Leaving)
SecretFireMobile has left IRC (Ping timeout: 240 seconds)
theWhisper_ has left IRC (Read error: Connection reset by peer)
rpjsf has joined (~bar@104-1-93-74.lightspeed.sntcca.sbcglobal.net)
regreg has left IRC (Ping timeout: 240 seconds)
siiky
trcc: did you check dlopen's manpage? it looks like it doesnt exist here (linux) but there are some hits on google
trcc
siiky: I found this: http://www-it.desy.de/cgi-bin/man-cgi?dlopen+3
trcc
But it does not make me any smarter
theWhisper_ has joined (~quassel@unaffiliated/thewhisper/x-7482734)
siiky
but is that manpage relevant in your system?
trcc
not at all, but best explanation I could find
siiky
what about your system's manpage? maybe its not a thing there
siiky
also, that manpage is from 2005, dunno how much things have changed, but beware
jcob has left IRC (Ping timeout: 240 seconds)
trcc
really difficult to find anything from darwin
dfice has left IRC (Ping timeout: 245 seconds)
trcc
siiky: this is the closest I could find: http://www.polarhome.com/service/man/?qf=dlopen&af=0&tf=2&of=Darwin, which is also listed several other places. But I have no idea what it means, in order for me to comile my dll correctly.
o11c
trcc: Mac OS's linker is *really* weird from a Linux/BSD point of view
regreg has joined (~regreg@85.121.54.224)
noobineer has left IRC (Quit: Leaving)
o11c
trcc: it's as if it mangles the name of the library that `ld` found, into *every* symbol
o11c
which breaks all sorts of things
trcc
oh god
o11c
I *think* that RTLD_FIRST makes dlsym() work similar to the way `ld`-based linking works
o11c
(note, it's not "real" mangling, just conceptually similar)
dj_pi has left IRC (Ping timeout: 246 seconds)
o11c
tbh I would just turn on flat namespaces and be done with it
o11c
but that probably wouldn't play nice with system libraries
trcc
o11c: Could you elaborate a bit more? How should I change my dll?
jcob has joined (~jcob@152.7.255.202)
fstd_ has joined (~fstd@unaffiliated/fisted)
o11c
trcc: what actual problem are you having?
lgmonezi
Hello people! How are you doing? I'd like the opinion of yours.. I am a programmer of other languages, mainly Java. I want to learn C deeply, and for that purpose I've got a number of books. My ideia is to read from the olders to the new ones, to learn the beginning of C89 and gradually learn what has changed in C99 and C11. I would like to start (as I've already have) with the K&R book. But I've read a couple of articles that discourage the reading
lgmonezi
of this book, because theoretically it is "wrong" to the new standards. What do you think?
siiky
searching for darwin RTLD_FIRST i found https://github.com/opensource-apple/dyld/blob/3f928f32597888c5eac6003b9199d972d49857b5/doc/man/man3/dlopen.3 but im not sure how relevant it is
siiky
trcc: ^
o11c
lgmonezi: unlike C++ or Java, there are no *fundamental* changes between versions
trcc
o11c: when I do: dlopen(dllpath, RTLD_LAZY | RTLD_LOCAL) then it works. But when I do: dlopen(dllpath, RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST) I get Segmentation fault: 11
o11c
lgmonezi: just read a book based on C11
Doddlin has joined (~anonymous@82-183-62-138.customers.ownit.se)
o11c
trcc: is there a reason you need RTLD_LOCAL?
trcc
o11c: I am not really in control of the application loading my dll, so I cannot change it. The application loading my dll uses lazy, local and first
o11c
trcc: anyway, that sounds like the symbol you're dlsym'ing is actually in some *other* library, not that one
gelignite has left IRC (Quit: Good fight, good night!)
oldlaptop
lgmonezi: so long as you realize it describes an old version of the C standard, I see no reason not to read K&R.
i1nfusion has left IRC (Remote host closed the connection)
noobineer has joined (~noobineer@2601:401:8280:32f0:4c52:2710:e1b4:c8cb)
GenteelBen has left IRC (Quit: Leaving)
trcc
o11c: yes, so it seems like my library, call it lib1, loads another library, call it lib2, and lib2 contains the function.
trcc
but lib1 should contain the function
porky11 has joined (~porky11@p200300868A0F677AB6D5BDFFFE20A28D.dip0.t-ipconnect.de)
rpjsf
lgmonezi: would you mind posting links to the articles that discourage reading K&R? I'm very curious as to their arguments
oldlaptop
nothing, or very little anyway, in it has been made wrong by newer standards (as C has not changed nearly as much as most other languages in the past 30 years(
trcc
thanks siiky
DTZUZU has joined (~DTZUZU@S0106bcd16584b0aa.vs.shawcable.net)
lgmonezi
I see
tuxd00d has left IRC (Quit: tuxd00d)
o11c
trcc: so you need to wrap the symbol and call the original
trcc
o11c: sorry, the original?
rpjsf
lgmonezi: and yeah FWIW I agree wholeheartedly with oldlaptop, I believe K&R to be the best programming-language book ever written
o11c
trcc: this is pretty easy if your library `dlopen`s lib2
trcc
my library is written in haskell using their FFI, sooo :(
lgmonezi
I'm glad to know, because K&R seems really interesting. It's a short book, but very through
o11c
trcc: basically, in lib1: lib2_handle = dlopen(lib2); void foo() { dlsym(lib2_handle, "foo")(); }
oldlaptop
lgmonezi: the main changes over the years have been the addition of new features and relaxation of certain restrictions
trcc
ahh
rpjsf
lgmonezi: that's the beauty of it, its brevity yet breadth (which therefore speaks to the beauty of C in its brevity and power as well!)
o11c
trcc: but I think there *might* be a way to install interceptor functions using weird things like __real_ and __wrapper_
o11c
trcc: the ld(1) man page might mention this
nanoz has left IRC (Ping timeout: 240 seconds)
trcc
o11c: thank you for the clue. I will definitely try ti investigate... Maybe there is some way of compiling their symbols together?
o11c
trcc: yeah, that'd be easier
oldlaptop
(//comments, mixing declarations and code, VLAs, the _Bool type and stdbool.h, and anonymous unions spring to my mind, most of which was C99)
o11c
trcc: instead of letting lib2.so be built, leave it as lib2.pic.a
lgmonezi
<rpjsf> Unfortunately I don't have the link anymore. I'll try to find it
o11c
be sure to use --(  -fwhole-archive  --)
trcc
thanks o11c
rpjsf
oldlaptop lgmonezi: yeah but all that stuff is syntactic sugar. the language's foundations haven't changed enough to make K&R irrelevant, not at all.
oldlaptop
_Bool goes beyond syntactic sugar
rpjsf
lgmonezi: no worries! like I said, I was just curious :) thanks!
oldlaptop
as do VLAs
rpjsf
oldlaptop: true, sorry, I over-generalized
o11c
trcc: https://github.com/o11c/crab/blob/master/Makefile#L36
CThuleHansen commented 5 years ago

Current approach: Wrap each function. See this post on reddit: https://www.reddit.com/r/haskell/comments/apeoev/haskell_ffi_exporting_symbols_from_builddepends/

CThuleHansen commented 5 years ago

Closed in favor of #7