Mac OSX ld report 32-bit RIP relative reference out of range error for Absolute symbol

1

I'm trying to combine objcopy with clang toolchain. Because objcopy of binutils 2.25 generates broken Mach-O object file, I edit generated object file using my shell script.

$ objcopy-comp.sh -I binary -O mach-o-x86-64 test test.o
$ nm test.o
000000000000000b D _binary_test_end
000000000000000b A _binary_test_size
0000000000000000 D _binary_test_start

However, link against a C code fails with this error message.

$ clang main.c test.o
ld: 32-bit RIP relative reference out of range (-4294971146 max is +/-4GB): 
  from _main (0x100000EA0) to _binary_test_size (0x0000000B) 
  in '_main' from main.o for architecture x86_64

(Newlines are inserted for readbility)

Here is main.c.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern const unsigned char binary_test_start[];
extern const unsigned char binary_test_end[];
extern const unsigned char binary_test_size[];

int main(int argc, char *argv[])
{
  size_t len = binary_test_end - binary_test_start;
  char *data = calloc(len + 1, sizeof(char));
  memcpy(data, binary_test_start, len);
  data[len] = 0;
  printf("%s %ld %d\n", data, len, (int)binary_test_size);
  return 0;
}

According to nlist document,

N_ABS (0x2)—The symbol is absolute. The linker does not change the value of an absolute symbol.

but the error message suggests that linker does try to change the value.

How to protect Absolute value from linker?

macos
linker
asked on Stack Overflow Jul 17, 2015 by 12651654123zzz • edited Jul 18, 2015 by 12651654123zzz

1 Answer

0

The Mach-OABI utilizes relative addressing with x86_64. The compiler interprets the address you've used as 32-bit which is out range, nor will it be absolute. Try compiling your code as i386 only and you might have a better chance of success.

Specifically how you're changing the symbol types is unknown since you haven't shown the commands you've used with objcopy.

answered on Stack Overflow Jul 18, 2015 by l'L'l

User contributions licensed under CC BY-SA 3.0