0

I'm a student currently taking a computer structures course and I have an assignement for writing in aseembly and C. I've found an issue I don't quite understand. We were given a makefile for running the program, and when connecting to our school's servers it works as intended. The issue arises when trying to use the same makefile on my personal machine. One of the assembly files (and likely more as I continue working) utilizes a .rodata section. Attempting to use the makefile as it was given results in the following error:

gcc -g -o a.out main.o run_main.o func_select.o pstring.o
/usr/bin/ld: func_select.o: relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
make: *** [makefile:2: a.out] Error 1

attempting to add one of the following -fpie -fPIE or -no-pie in between -o and a.out results in the following error

gcc -g -o -fPIE a.out main.o run_main.o func_select.o pstring.o
/usr/bin/ld: cannot find a.out: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [makefile:2: a.out] Error 1

I'm sorry if this is considered a silly question however I can't figuere it out.

The expectation was for the makefile to create all of the .o files and a.out, however the a.out file never works.

In addition writing the following: gcc -g -fPIE -o a.out main.o run_main.o func_select.o pstring.o still creates an error, specifically

gcc -g -fPIE -o a.out main.o run_main.o func_select.o pstring.o
/usr/bin/ld: func_select.o: relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
make: *** [makefile:2: a.out] Error 1

someone
  • 19
  • 3
  • 2
    That's a misleading suggestion in the error message, it assumes you are using the compiler. Of course those flags have no effect on hand written code. You did not show your program but presumably you have not written it in a position independent way. If you nevertheless want to link it, you should counter-intuitively use `-no-pie` – Jester Dec 23 '22 at 18:56
  • That seems to have dont it! I must have failed to try `-no-pie` in that position, now I just have a run-time error for the output file lol. Thank you so much! – someone Dec 23 '22 at 19:11

1 Answers1

1

As per man gcc:

gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-Wpedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] [@file] infile...

-o requires outfile parameter. This should work:

gcc -g -fPIE -o a.out main.o run_main.o func_select.o pstring.o

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
  • It didn't work for some reason, still says to recompile with -fPIE – someone Dec 23 '22 at 19:00
  • Have you recompiled your object files before running the command I provided? If no: `rm *.o`, and rebuild all from scratch. Otherwise - provide us with exact error message. – Andrejs Cainikovs Dec 23 '22 at 19:04
  • I have done that. Could the other object file commands require the `-fPIE` flag? – someone Dec 23 '22 at 19:07
  • 3
    You need to write position independent assembly or use `-no-pie` to link. No compiler flags will fix your code. – Jester Dec 23 '22 at 19:10