0

I'm completing the final assignment for a compilers course and right now the deal is to translate some intermediate representation into x86_64 assembly source code and then build an executable through gcc by running

gcc output.s -o output

This executable should work properly. The issue is that I just can't get my code past GCC when it comes to (at least) one particular instruction. This is it:

mov L0, %rbx

Where L0 is a label.

The whole test file is as follows:


.text

.section .rodata

.text

.globl main

main:
        // rbss for now
    add $0, %rsp
    mov %rsp, %rsi
        // register spill area
    add $0, %rsp
    mov %rsp, %rdi
        // store rax => rsp
    mov %rax, %rcx
    mov %rcx, ( %rsp )
        // subI rsp, 4 => rsp
    mov %rsp, %rcx
    sub $4, %rcx
    mov %rcx, %rsp
        // lea L0 => rbx
    mov L0, %rbx
        // store rbp => rsp
    mov %rbp, %rcx
    mov %rcx, ( %rsp )
        // subI rsp, 4 => rsp
    mov %rsp, %rcx
    sub $4, %rcx
    mov %rcx, %rsp
        // store rbx => rsp
    mov %rbx, %rcx
    mov %rcx, ( %rsp )
        // subI rsp, 4 => rsp
    mov %rsp, %rcx
    sub $4, %rcx
    mov %rcx, %rsp
        // jumpI => Lmain
    jmp Lmain
        // L0 : halt
L0:
    hlt
        // Lmain : nop
Lmain:
        // addI rsp, 0 => rbp
    mov %rsp, %rcx
    add $0, %rcx
    mov %rcx, %rbp
        // subI rsp, 0 => rsp
    mov %rsp, %rcx
    sub $0, %rcx
    mov %rcx, %rsp
        // addI rbp, 8 => rsp
    mov %rbp, %rcx
    add $8, %rcx
    mov %rcx, %rsp
        // loadAI rbp, 4 => rbp
    mov %rdi, %rbx
    add %rbp, %rbx
    add $4, %rbx
    mov ( %rbx ), %rcx
    mov %rcx, %rbp
        // jump => rbp
    mov %rbp, %rbx
    jmp *%rbx

Is there anything inherently mistaken about using mov this way? I'm not using call/ret semantics since the translation must be carried out directly from ILOC (a toy/education purpose) intermediate code. When I try to run the aforementioned command I get some variation of:

/usr/bin/ld: /tmp/cccAoFmz.o: relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status

Could you guys help me to get a grasp of what's actually going on? I'm quite new to x86 programming and that's my first time with this kind of application. The whole assignment is done, my only issue is getting it to a working state (So no, huahuahua, I'm not getting you guys to do my homework :D).

Is there another way to get what I'm trying to achieve? Is my approach incorrect? I'm out of ideas right now.

Thank you so much :) Best,

Garren
  • 13
  • 4
  • That's a load, not an LEA or mov-immediate of the address. And you used a 32-bit-sign-extended addressing mode instead of RIP-relative. See the linked duplicates. – Peter Cordes Oct 16 '22 at 02:15

0 Answers0