Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to make calls to C library functions on Mac

257 views
Skip to first unread message

climber.cui

unread,
Sep 15, 2008, 3:16:57 PM9/15/08
to
Hi all,
I need to make the assembly code developed on Linux portable to Mac
OSX. And the assembly code contains plenty of calls to C library
functions, such as printf(), time(), srand(), etc.
The code won't work on Mac OSX. 'gcc' complains that those symbols
(C function names) can not be recognized.
I did compiled some C code on Mac OSX, using :
gcc -S code.c
to obtain the assembly code produced by gcc for Mac OSX. However,
the generated asm code is not readable, and I do not understand it.
Does anyone know how to invoke C library functions on Mac Osx ? Or
there are some resource to check on?
Thanks..

cheers,

tony

Frank Kotler

unread,
Sep 15, 2008, 5:09:45 PM9/15/08
to

I don't "do Mac", so I'm guessing...

Note carefully what symbols gcc claims it can't find. Is there an
underscore on "_main", "_printf", etc.? Linux doesn't use 'em - Macho
probably (?) does. Nasm will add an underscore to all global/extern
names if you add "--PREFIX_" to the command line. (I assume Nasm, since
you can't read gcc's "-S" output... there are other possibilities) Easy
to try, anyway...

In your earlier post, you mention having to use "-lpthread" (or not). In
some cases (not sure when), you might also have to use
"-L/lib/libpthread.so", or some such.

As I said, I'm guessing... but those are a couple of things that might
cause the problem you're seeing...

I think Apple's got an "Xcode" (or "Xcoders"?) package. You've probably
got that. Any docs/examples that would help? If you find the "trick" to
this, pass it on!

Best,
Frank

(here's an int 80h example - not quite the same as what you're trying to
do - I don't know if it actually works or not!)

; nasm -f macho hw.asm
; ld -o hw hw.o (???)

global _start

section .rodata align=16 data
msg db "hello macho", 10
msg_len equ $ - msg


section .text
_start:

push msg_len
push msg
push 1
mov eax, 4 ; __NR_write
call sys_call

push 0
mov eax, 1
call sys_call

sys_call:
int 80h
ret

climber.cui

unread,
Sep 15, 2008, 9:07:33 PM9/15/08
to
> Note carefully what symbols gcc claims it can't find. Is there an
> underscore on "_main", "_printf", etc.? Linux doesn't use 'em - Macho
> probably (?) does. Nasm will add an underscore to all global/extern
> names if you add "--PREFIX_" to the command line. (I assume Nasm, since
> you can't read gcc's "-S" output... there are other possibilities) Easy
> to try, anyway...
>
> In your earlier post, you mention having to use "-lpthread" (or not). In
> some cases (not sure when), you might also have to use
> "-L/lib/libpthread.so", or some such.

Thanks for the suggestions.
On linux, I don't need to place an underscore before 'main', 'printf',
'exit' etc. And on Mac, gcc will complain that those are undefined
symbols. So I add underscore, like in this piece of code:

.globl _main

_main:
pushl $termination_msg
call _printf
addl $4 , %esp
pushl $0
call _exit

Now, gcc will complain this:
/usr/bin/ld: /var/tmp//ccYKUF9r.o has external relocation entries in
non-writable section (__TEXT,__text) for symbols:
_exit
_printf

What does this mean then? How to call these external C functions in
assembly program on Mac?
Any ideas?

cheers,
tony

Ben

unread,
Sep 15, 2008, 11:52:23 PM9/15/08
to

Here's an article with a couple tips on assembly in OS X:
http://www.zathras.de/angelweb/blog-intel-assembler-on-mac-os-x.htm

Check out the section near the bottom - "Calling a system function". It
looks like you need to add some code to import functions like printf, etc.

Ben

Daniel Molina Wegener

unread,
Sep 19, 2008, 3:26:57 PM9/19/08
to
climber.cui <spam...@crayne.org>
on Monday 15 September 2008 15:16
wrote in comp.lang.asm.x86:


> Hi all,
> I need to make the assembly code developed on Linux portable to Mac
> OSX. And the assembly code contains plenty of calls to C library
> functions, such as printf(), time(), srand(), etc.

You don't need to call them from assembly code, OS X it's UNIX
certified operating system, then you may get the most of the C99
and POSIX interfaces working on your Mac... try installing
developer tools, such as X Code.

> The code won't work on Mac OSX. 'gcc' complains that those symbols
> (C function names) can not be recognized.
> I did compiled some C code on Mac OSX, using :
> gcc -S code.c
> to obtain the assembly code produced by gcc for Mac OSX. However,
> the generated asm code is not readable, and I do not understand it.
> Does anyone know how to invoke C library functions on Mac Osx ? Or
> there are some resource to check on?
> Thanks..
>
> cheers,
>
> tony

Regards,
--
.O. | Daniel Molina Wegener | FreeBSD & Linux
..O | dmw [at] coder [dot] cl | Open Standards
OOO | http://coder.cl/ | FOSS Developer

0 new messages