4

i'm writing a project for university in Assembly x86 (32 bit) with AT&T syntax. During the course we compiled our source files with gnu gas and the command ld. Now i'm using GCC because i want to make an assembly function and call it from C program.

But when i started to compile with GCC i got these strange warnings, about linking and relocation.

/usr/bin/ld: obj/telemetry.o: warning: relocation in read-only section `.text'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE

Here is a small example of file which cause the warnings. Warnings happen only when i try to access/use the static variables declared in .data

.file "telemetry.s"

.data 
test: .string "Example"
 
.text
.globl telemetry
.type telemetry, @function
telemetry:
    pushl %ebp
    movl %esp, %ebp
    pushl %ebx

    leal test, %ebx # if i comment this line warnings wont happen

    popl %ebx
    popl %ebp
    ret

I get the warnings only with the gcc version which i'm currently using (11.2.0), on Ubuntu 22.04.01. Here's the commands:

gcc -g -m32 -c src/telemetry.s -o obj/telemetry.o
gcc -g -m32 -c src/main.c -o obj/main.o
gcc -g -m32 obj/telemetry.o obj/main.o -o bin/telemetry

If I assemble it with GAS and link with ld manually, I don't get the warnings.

An older version of gcc (7.5.0) also produces no warning (in a docker container Ubuntu 18.04).

So this could be a bug/problem of the current version of gcc, or what changed since then? Am I doing something wrong?

Michele Viviani
  • 175
  • 1
  • 7
  • 2
    Compile with GCC using `-no-pie` option – Michael Petch Aug 08 '22 at 09:02
  • 1
    @Someprogrammerdude yes, if i generate the obj file of the .s file with as, and then use gcc i get the same warnings – Michele Viviani Aug 08 '22 at 09:06
  • 5
    Using absolute addresses in a PIE requires runtime fixups of your `.text` section (done by CRT startup code) when the kernel picks an address to load this at. 32-bit mode doesn't have RIP-relative addressing, otherwise you'd use `lea test(%rip), %ebx` (or more likely `%rbx`). This is less efficient (and is a dirty page of memory not backed by the executable on disk), but does work in 32-bit code, that's why it's a warning not an error. (Unlike [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427)) – Peter Cordes Aug 08 '22 at 09:06
  • 2
    @Someprogrammerdude: It's linking this into a PIE that's the problem, not assembling. GCC doesn't pass `as` any extra options that make it treat relocations differently. But modern distros configure GCC to pass `-pie` to `ld` by default, and compile with `-fPIE` so they use the usual inefficient techniques for 32-bit PC-relative addressing of data. – Peter Cordes Aug 08 '22 at 09:07
  • Separate from the relocation warning, your `ret` is going to jump to the caller's EBX, since you push it right before `ret`. – Peter Cordes Aug 08 '22 at 09:11
  • 1
    Your comment and the link clarified me a lot of things that i studied but never seen all together in a real example, I got the point. Thank you. – Michele Viviani Aug 08 '22 at 10:52
  • 64-bit version of the same question: [Understanding a NASM warning](https://stackoverflow.com/a/76654374) - but not a duplicate because 32-bit mode doesn't have RIP-relative addressing. – Peter Cordes Jul 10 '23 at 13:47

0 Answers0