Search code examples
assemblyx86gnu-assembleratt

relocation truncated to fit: R_386_8 against '.rodata'


I got this error when trying to compile a simple string case-swap function in x86 AT&T assembly.

I tried looking around at other questions with this error, but none was similar enough to help in my case. Most were dealing in things in different libraries and files, which is not the case here.

The error:

$ gcc -m32 -g main.c test.s -o Test4
/tmp/ccQ49nKM.o: In function `scLOOP':
/home/nikolay/Dropbox/comporg/ex3/solution/test.s:24:(.text+0x14): relocation truncated to fit: R_386_8 against `.rodata'
collect2: error: ld returned 1 exit status

The code:

    .section .rodata
.align      4
error:      .string "invalid input!\n"  # error message.
a:      .byte   97          #a in ascii.

    .text

    .global swapCase        #swaps the cases of the string.
    .type   swapCase, @function
swapCase:
    pushl   %ebp            #save old FP
    movl    %esp, %ebp      #set new FP

    movl    8(%ebp), %eax       #get pointer to pstring
    movb    (%eax), %cl     #get length of pstring
    addl    $1, %eax        #move pointer to string itself

    scLTEST:            #loop condition.
    cmpb    $0, %cl         #compare length with 0
    je  scDONE          #if length is zero, goto done
    jmp scLOOP          #else: goto loop

    scLOOP:
    cmpb    $a, (%eax)      #compares first byte of string with a='a'.
    jl  scTOLOWER       #byte is uppercase.
    subb    $32, (%eax)     #byte is lowercase, change to uppercase.
    jmp scINC           #increment pointer.
    scTOLOWER:
    addb    $32, (%eax)     #change to lowercase.
    jmp scINC           #increment pointer.

    scINC:
    addl    $1, %eax        #increment %eax to next byte
    decb    %cl         #decrement counter
    jmp scLTEST         #end current loop iteration

    scDONE:
    movl    8(%ebp), %eax       #set return value to pointer to pstring.
    movl    %ebp, %esp      #restore old stack pointer
    popl    %ebp            #restore old FP
    ret             #return to caller function

Solution

  • cmpb $a, (%eax) causes the error. I guess you didn't want to compare the memory address of a with the value of (eax). BTW: a memory-memory comparation is not possible in x86. I guess you wanted to compare the byte at (eax) with the immediate ASCII character 'a' (cmpb $97, (%eax)) You might replace

    a:      .byte   97          #a in ascii.
    

    by

    .equ a, 97