Author Topic: OSX NASM Position independent code  (Read 6313 times)

Offline maxlovic

  • New Member
  • Posts: 1
OSX NASM Position independent code
« on: April 05, 2012, 10:53:23 AM »
Hi.
 
Recently I've got a task to build a project under MACOS. Struggling many differences, I finaly got to an end, and everything seems to work, but one issue bothers me.
 
The project has assembly (NASM) code like this:
 
Code: [Select]
SECTION .rodata align=16
 
ALIGN 16
const_w05: times 8 dw 5
 

SECTION .text
 
ALIGN16
_some_function_sse2:
...
pmullw      xmm1,   [rel const_w05]
...
ret

Now whe it comes to linking, I get the following error:
ld: absolute addressing (perhaps -mdynamic-no-pic) used in (_some_function_sse2) from ... not allowed in slidable image. Use '-read_only_relocs suppress' to enable text relocs
 
So reading lot of information on this, I had nothing bug put the suggested option -read_only_relocs suppress. The project was built and worked.
But I'm afraid it isn't correct and may not work if the shared object loads in different memory location.

I've uploaded a sample project here:
http://www.box.com/s/9e8f0f0b3ed6bbf22534

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: OSX NASM Position independent code
« Reply #1 on: April 07, 2012, 09:05:25 PM »
I don't know much about 64-bit code, and less about OSX. I would think that if you say "rel", that would make your code inherently position-independent.

However... you seem to be actually assembling as "-f macho32", not "-f macho64", in which case "rel" would not be valid (although Nasm does not complain! Bug?). As "-f macho64", "rel" is okay, but defining rbp as ebp fails. So I'm a little confused just what you're doing.

If it "appears to work", that's a good sign. Is there some way you can force your .so to load at a "non-preferred" location to test it? Linux won't relocate a .so at all (AFAIK), so I don't know what macho is "supposed" to do here.

Sorry I can't help more. Good luck with it!

Best,
Frank