jww
jww

Reputation: 102306

How to create Mach-O symbol aliases in NASM?

I have an assembly file that exports two functions. The function names are NASM_RDRAND_GenerateBlock and NASM_RDSEED_GenerateBlock. The symbols lack the leading underscore decoration. The file is assembled with NASM.

In the C/C++ code, the symbol names are declared as extern "C" to avoid mangling. Linking under Linux and Cygwin works as expected. Linking under OS X fails with:

...
Undefined symbols for architecture x86_64:
  "_NASM_RDRAND_GenerateBlock", referenced from:
      RDRAND::GenerateBlock(unsigned char*, unsigned long) in libcryptopp.a(rdrand.o)
  "_NASM_RDSEED_GenerateBlock", referenced from:
      RDSEED::GenerateBlock(unsigned char*, unsigned long) in libcryptopp.a(rdrand.o)
ld: symbol(s) not found for architecture x86_64

I want to create an alias such that _NASM_RDRAND_GenerateBlock = NASM_RDRAND_GenerateBlock so Apple's linker can link to the symbol. Microsoft's PE format and MASM allow it, but I'm not sure if Apple and Mach-O have similar facilities.

My first question is, does the Mach-O file format support symbol aliases?

If so, then my second question is, how do I tell NASM to create the alias? Or what do I need to do in NASM to create a symbol alias?


Here are the command used to build the object files:

nasm -f macho32 rdrand.S -DX86 -g -o rdrand-x86.o
nasm -f macho64 rdrand.S -DX64 -g -o rdrand-x64.o

Here is the assembler file.

Upvotes: 1

Views: 496

Answers (1)

l'L'l
l'L'l

Reputation: 47189

In macOS you can use the type N_INDR to alias another symbol.

From /System/Library/Frameworks/Kernel.framework/Versions/A/Headers/mach-o/nlist.h:

If the type is N_INDR then the symbol is defined to be the same as another symbol. In this case the n_value field is an index into the string table of the other symbol's name. When the other symbol is defined then they both take on the defined type and value.

There's also a tool indr, which you can compile that might help in the case of using assembly:

https://opensource.apple.com/source/cctools/cctools-895/misc/indr.c

Upvotes: 1

Related Questions