flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Heap > MacOS libs, function pointers and PIC

Author
Thread Post new topic Reply to topic
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
MacOS libs, function pointers and PIC
Hi,
I'm posting this in heap because it concerns MacOS and NASM. I suppose I could have posted it in the Unix forum but I doubt anyone ever reads that.
It's about position-independent code. Say I have an executable, E, and a C library, L (in this case a MacOS .dylib). In my executable I say:

Code:

extern _my_func



In the C library I say:

Code:

void *my_func(...){...}



Now I want to pass a pointer to _my_func from my executable E back to my library L. Just passing _my_func doesn't seem to work and I think it is a PIC problem. It worked on Linux I think because I only had 4GB of RAM on my Linux machine? So the function pointer was within range? On MacOS with 16GB of RAM it crashes with SIG_ABRT Code 6 or something like that.
My workaround is to ask my library for a pointer to _my_func at runtime, this stops it crashing. Is my diagnosis correct and does anyone have any suggestions for something more elegant?
Post 07 Apr 2017, 19:29
View user's profile Send private message Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
Okay I'm thinking now this is more likely to be a relocations problem. But why would it affect MacOS and not Ubuntu?
Post 07 Apr 2017, 20:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15299
Location: Bigweld Industries
Getting pointers to functions depends upon how the code is linked. And it depends upon the code bitness. Is it 32-bit or 64-bit? What is your linker? What options did you use? What does the documentation say?
Post 07 Apr 2017, 20:55
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 889
Let me get this straight. You want to pass a pointer to the library's function from the executable back to the library? Why? Confused

Doesn't the library know about its own function already?
Post 08 Apr 2017, 15:54
View user's profile Send private message Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
Hiya,

I let this topic go cold as I was experimenting and also I don't like it when a question is answered with a barrage of questions. mr/mrs/miss revolution: the code is self-evidently 64-bit as it's a MacOS machine with 16GB of memory... I don't know the name of the linker - I just called gcc which i think on MacOS is a link to clang. So I guess, whatever linker clang uses? I didn't use any particuarly remarkable options, just:

gcc -o a.out array_multi.o -L . -L /usr/local/lib/ -lc -lm -lkoshka.core -lkoshka.mm

libkoshka.core and libkoshka.mm are libraries I wrote myself.

Furs:

I shouldn't have to justify myself, but it's an elegance thing. I'm writing a compiler. To make it future-proof, when the executable allocates an array, it passes a suitable constructor and destructor to libkoshka.core. In this case, the destructor is called "_bb_free_string" and is implemented in libkoshka.core because that library makes use of it. Sure, I could pass a message saying "this is a string, you know how to free it" but that's not as pretty,

Anyway, my current workaround is an asm version of _bb_free_string hard-coded into the executable. Which is duplication of effort and not very nice.

I just want to know what's special about MacOS that stops me passing function pointers around like I did with Linux and Windows.

PS Revolution, no-one reads the ****ing manual. That's what forums are for.
Post 05 May 2017, 09:11
View user's profile Send private message Reply with quote
YONG



Joined: 16 Mar 2005
Posts: 8000
Location: 22° 15' N | 114° 10' E

ProphetOfDoom wrote:
PS Revolution, no-one reads the ****ing manual. That's what forums are for.

First, "revolution" starts with a lower-case "r". Second, revolution is going to point you to his/her website, which contains lots of useful information. Third, pick a better adjective to describe the manuals: useless, difficult-to-read, boring, and so forth.

Wink
Post 06 May 2017, 02:25
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 889
I didn't say you have to justify yourself. I was asking because I wasn't sure I got your problem right in the first place. I still don't, but I'm not familiar with Mac OS (though I just assumed it would be a common workaround, not specific).
Post 06 May 2017, 14:57
View user's profile Send private message Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
Sorry Furs. You did correctly understand what I am trying to do in your original post. TBH, I don't know why the "elegance" thing is bothering me so much, as the C component of my language is a total train wreck anyway. I guess I just have this annoying "need to know why" thing that programmers have.
YONG: Have we even ruled out the possibility that revolution is an unusually intelligent mole (a small brown furry mammal which snuffles around in the ground looking for worms)? It would explain why his/her/its website is always down.
I didn't mean to swear AT revolution BTW, I was just referring to the traditional RTFM response.
If anyone could demystify the function pointer problem I would be very grateful.
Post 06 May 2017, 18:58
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 889
Can you show some code that demonstrates the problem? If it's a relocation issue, then it depends on the linker/FASM (generation of object files) and maybe I can find a workaround.

How do you "get" the function's address anyway? Maybe that's the problem.

For example, you mentioned it is 64-bit, and in 64-bit (at least Linux/Windows, dunno about Mac) names are not decorated with a leading underscore. Hence your "extern _my_func" is likely wrong (should be "extern my_func") and perhaps is a "weak symbol" and thus gets resolved to NULL on load? But I assume you can call it, so this is likely not the problem... Confused

(there are a lot of questions instead of a straight answer, but since I don't know specifics, nor anything about Mac, I can't really give you a precise answer Smile)
Post 06 May 2017, 20:30
View user's profile Send private message Reply with quote
YONG



Joined: 16 Mar 2005
Posts: 8000
Location: 22° 15' N | 114° 10' E

ProphetOfDoom wrote:
I didn't mean to swear AT revolution BTW, I was just referring to the traditional RTFM response.

I could not care less even if you did mean to swear at him/her. Laughing

Thanks for reminding me of the expression "RTFM". I have not come across it since my college days a long, long time ago.

Wink
Post 07 May 2017, 02:07
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15299
Location: Bigweld Industries

ProphetOfDoom wrote:
... the code is self-evidently 64-bit as it's a MacOS machine ...

That means nothing to me. But thanks for explaining that MacOS is horribly limited to 64-bit code only.

ProphetOfDoom wrote:
... with 16GB of memory...

Still doesn't mean anything actually. Lots of Windows boxes have 16GB memory and will happily run 32-bit code.

ProphetOfDoom wrote:
PS Revolution, no-one reads the ****ing manual. That's what forums are for.

That is self-evidently false. But in fact I stand by my response because it was clear to me that no one here would be able to answer your question. And sometimes even pointing out that there is a manual can be enough to prompt someone to realise that they could have answered their own question immediately instead of waiting for someone else on a forum to read the manual for them.
Post 07 May 2017, 13:28
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 889
I think it runs 32 bit code, but I don't know if it uses ELF or something proprietary as usual. Does Mac even have "weak symbols"?
Post 07 May 2017, 15:03
View user's profile Send private message Reply with quote
YONG



Joined: 16 Mar 2005
Posts: 8000
Location: 22° 15' N | 114° 10' E

revolution wrote:
... it was clear to me that no one here would be able to answer your question.

Maybe we can start a new sub-forum for discussions on Mac OS programming and let ProphetOfDoom be the mod.

Wink
Post 08 May 2017, 03:45
View user's profile Send private message Visit poster's website Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
Re: MacOS libs, function pointers and PIC
Hi Furs,
Thanks so much for your willingness to help (more than certain others on here). Problem now is I did my kludgy fix of having two init_string/free_string pairs - one for the executable and one for the dynamically linked library... And I forgot to save the broken version in git. So I'd actually have to rewrite the broken code from scratch. Which doesn't particularly thrill me... I might do it. Thanks anyway.
Post 31 May 2017, 07:48
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.