Author Topic: Library calls in NASM OS X.  (Read 8879 times)

nobody

  • Guest
Library calls in NASM OS X.
« on: October 15, 2008, 07:23:56 AM »
I was told by Apple that you aren't supposed to make syscalls in OS X, so I tried to changed my code to call the C standard library but I can't figure out how to make the stub for the linker. The docs are all for GAS and they say to make the stub like this:

.section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5
_printf_stub:
   .indirect_symbol _printf
   hlt ; hlt ; hlt ; hlt ; hlt

And the dynamic linker will replace the hlts with a jump to the function the first time you call the stub. I can't find any documentation for the Apple version of NASM,  and the docs for the regular version has all of two sentences about MachO. Is creating a stub for system functions currently possible or is it as yet unimplemented?

nobody

  • Guest
Re: Library calls in NASM OS X.
« Reply #1 on: October 15, 2008, 05:40:06 PM »
Take a look at the source of HLA to see how Randall handled this issue.

http://sourceforge.net/projects/hlav1/

Also, OS X *does* seem to support syscalls.  Look at the source to HLA StdLib to see how that is handled.

http://sourceforge.net/projects/hla-stdlib/

nobody

  • Guest
Re: Library calls in NASM OS X.
« Reply #2 on: October 15, 2008, 07:50:11 PM »
http://webster.cs.ucr.edu/AsmTools/HLA/HLADoc/HTMLDoc/UnixInstall.html
HLA translates to GAS on OS X, which is the only way I currently know how to do it, and while you can make syscalls in OS X apple doesn't support it and the syscalls are undocumented and constantly change. http://lists.apple.com/archives/darwin-kernel/2008/Apr/msg00125.html The only supported way is to use the syscall function in LibC, or the wrapper function for the syscall you want to make.

But thanks anyway. :)

nobody

  • Guest
Re: Library calls in NASM OS X.
« Reply #3 on: December 06, 2008, 05:55:20 AM »
I found out why I couldn't get NASM to make external function calls. It doesn't support the appropriate sections nor does it support any section commands; all it does compile the simplest possible Mach-O binary.

I also found better explanation of the syscall situation: "[...]the system call interface on Mac OS X is different from what you might be used to on DOS/Windows, Linux, or the other BSD flavors. System calls aren't considered a stable API on Mac OS X; instead, you always go through libSystem. That will ensure you're writing code that's portable from one release of the OS to the next."