Author Topic: _ functions in MacOS X  (Read 12419 times)

Offline dhazeghi

  • Jr. Member
  • *
  • Posts: 3
_ functions in MacOS X
« on: December 13, 2010, 10:00:34 PM »
Hello all,

I'm trying to move some of my code over from nasm in Linux to MacOS X.  I have a simple mixed C/assembly program.  The problem is that the C version is prepending _ to the symbols, so the link fails.  I can manually fix this by renaming my assembly routines, but this is not ideal.  Here's a simplified testcase which works fine under Linux.  Thanks for any suggestions,

# nasm -f macho test1.asm
# gcc -m32 -c test.c
# gcc -m32 test.o test1.o
Undefined symbols:
  "_asmFunc", referenced from:
      _main in test.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

test.c:
extern int asmFunc(int);

int main(int argc, char *argv[])
{ return asmFunc(1); }

test1.asm:
SECTION .text
global asmFunc

asmFunc:
    mov eax, [ebp + 4]
    ret

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: _ functions in MacOS X
« Reply #1 on: December 13, 2010, 10:54:29 PM »
# nasm -f macho --prefix _ test1.asm

This prepends an underscore to any symbol declared global or extern. Should fix it.

Best,
Frank


Offline dhazeghi

  • Jr. Member
  • *
  • Posts: 3
Re: _ functions in MacOS X
« Reply #2 on: December 14, 2010, 06:28:45 AM »
Thanks Frank!  That did the trick.

Now on to figuring out why calls to C functions almost alway (but not always) segfault.  Must be some bizarre alignment convention or something...

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: _ functions in MacOS X
« Reply #3 on: December 14, 2010, 02:52:09 PM »
The Mac OS X ABI for IA-32 explains everything.

Offline dhazeghi

  • Jr. Member
  • *
  • Posts: 3
Re: _ functions in MacOS X
« Reply #4 on: December 15, 2010, 08:29:05 AM »
Yes, thanks for the ABI doc.  Looks like it was the 16-byte alignment rule that was throwing me off, particularly because of the extra 4-bytes that the call instruction automatically inserts.

I wasn't able to find a similar ABI doc for x86-64 OS X, but I assume it must not be too different from the Linux one.

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: _ functions in MacOS X
« Reply #5 on: December 15, 2010, 04:55:11 PM »