I'm trying to compile a toy example as a homework problem. I've been given a small snippet of code, but it won't compile on my home machine (Ubuntu 17, gcc 7.2.0). The exact same code compiles on school machines (Ubuntu 16, gcc 5.4.1).
When I run make, I get the following error:
/usr/bin/ld: age.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Compiling with the -fPIC flag does not fix the problem.
Here is the sample assembly code:
.string1:
.string "Please enter your name: "
.string2:
.string "Thank you %s!\n"
.string3:
.string "Please enter your year of birth: "
.globl get_age
get_age:
push %rbx
push %rbp # callee saved registers
# local variables:
leaq -8(%rsp), %rsp # - endptr
leaq -24(%rsp), %rsp # - name_str[24]
leaq -24(%rsp), %rsp # - year_of_birth[24]
mov %rsp, %rbp
movq $.string1, %rdi
xorl %eax, %eax
call printf # printf("Please enter your name: ");
leaq 24(%rbp), %rdi
call gets # gets(name_str);
ret
This code is called from a simple main function:
#include <stdio.h>
long get_age(void);
void main () {
long age;
age = get_age();
printf("You are at least %ld years old.\n", age);
return;
printf("You are at least %ld years old and you look like it too.\n", age);
}
Here is the makefile I'm using. Note, the failure occurs when gcc -o x age.o main.o is called:
all: x
x: main.o age.o
gcc -o x age.o main.o
main.o: main.s
gcc -c main.s
age.o: age.s
gcc -g -c age.s
main.s: main.c
gcc -O2 -S main.c
clean:
rm -f x *.o main.s
As previously mentioned, the -fPIC flag does not seem to make any difference (I've tried adding it to every stage of the makefile). Any ideas?