anotherCode245
anotherCode245

Reputation: 703

Using simple linker script gives "relocation truncated to fit: R_X86_64_32S"

I'm trying to compile this simple program with a custom linker script on an amd-64 linux machine

// main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    printf("Hello World!\n");
    int* x = malloc(sizeof(int));
    while(1) {}
    return 0;
}

and

// script.ld 
SECTIONS
{
    . = 0x100000000;
    .text : { *(.text) }
    .data : { *(.data) }
    .bss : { *(.bss) }
}

but if I do gcc -T script.ld main.c -o main, I get a lot of errors like

/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x12): relocation truncated to fit: R_X86_64_32S against symbol `__libc_csu_fini' defined in .text section in /usr/lib/x86_64-linux-gnu/libc_nonshared.a(elf-init.oS)

Upvotes: 0

Views: 1318

Answers (1)

Employed Russian
Employed Russian

Reputation: 213375

You are trying to link main at 0x100000000, which is above 4GB boundary, but GCC (and your libc) are not set up for -mcmodel=large.

The default is -mcmodel=medium, which implies that the program is linked into the lower 2GBs of address space.

You would need to build both main.c and crt1.o with -mcmodel=large in order to link your executable at 0x100000000.

Or just use -fPIE and -pie and your executable will load at arbitrary (but usually well above 4GB boundary) address.

Upvotes: 1

Related Questions