4

I was recently trying to compile the "Find a Maximum Value" program from the book "Programming from the Ground Up". Since I'm using Windows, I compile the assembly file with Cygwin. However, I get the following errors:

/tmp/ccuamKmO.o:fake:(.text+0xc): relocation truncated to fit: R_X86_64_32S against `.data'
/tmp/ccuamKmO.o:fake:(.text+0x1d): relocation truncated to fit: R_X86_64_32S against `.data'
collect2: error: ld returned 1 exit status


It's probably some silly error and I really can't recognise it. This is the code for the program:

.section .data
    data_items: 
        .long 3, 67, 34, 222, 45, 75, 54, 34, 44, 33, 22, 11, 66, 0

.section .text
    .globl main

main:
    jmp find_largest
  ret_find_largest:

    ret

/* 
 * %edi - Holds the index of the item being examined
 * %ebx - Largest item found
 * %eax - Current item
 */
find_largest:
    movl $0, %edi
    movl data_items(,%edi,4), %eax /* load eax with first item */
    movl %eax, %ebx

  start_loop:
    cmpl $0, %eax
    je loop_exit
    incl %edi
    movl data_items(,%edi,4), %eax
    cmpl %ebx, %eax
    jle start_loop
    movl %eax, %ebx
    jmp start_loop

  loop_exit:
    jmp ret_find_largest
jrhetf4xb
  • 161
  • 3
  • 3
  • 10

4 Answers4

3

It seems your assembler code is intended for 32-bit machine but you're assembling for x64. Try adding -m32 on the command line.

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109
  • Compiling with "gcc -m32 cmp.S -o cmp" gives me a huge list of linker errors like these: /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-cygwin/4.8.2//libgcc_s.dll.a when searching for -lgcc_s – jrhetf4xb Aug 25 '14 at 19:58
  • 3
    I believe there's a linker option you need to specify as well. Something along the lines of `-mi386pe`. To pass it from GCC you can use `-Wl,-mi386pe`. – Drew McGowen Aug 26 '14 at 08:13
  • 1
    @DrewMcGowen Thanks, the option I found is -Wl,-mi386pep. I get the same two errors like in the first post but with some new: /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../lib/libcygwin.a(libcmain.o): In function `main': /usr/src/debug/cygwin-1.7.25-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain' /usr/src/debug/cygwin-1.7.25-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain' – jrhetf4xb Aug 26 '14 at 09:48
  • OK so it seems that this line of code causes the relocation error: movl data_items(,%edi,4), %eax. But shouldn't it be a valid line of code? – jrhetf4xb Aug 26 '14 at 17:30
  • I'm still stuck with this and I'm looking for a solution... Can anybody provide a bit of help? – jrhetf4xb Aug 29 '14 at 16:17
2

I faces with this problem. Just try to compile with gcc -no-pie file.s -o file It worked fine for me. Then ./file in order to run executable

Jasurbek Nabijonov
  • 1,607
  • 3
  • 24
  • 37
  • 2
    Please don't add the [same answer to multiple questions](http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-ad??d-a-duplicate-answer-to-several-questions). Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question – Manfred Radlwimmer Mar 22 '17 at 14:15
1

It seems to be a Windows error. I had the same one and couldn‘t figure out how to solve it. Trying the exact same thing on linux worked.

For x64 and with nasm:

nasm -f elf64 -o <asm_name>.o <asm_name>.asm

If you’re using a c-program calling it, follow this with:

gcc -m64 -o <c_name> <c_name>.c <asm_name>.asm

You can finally call this with ./<c_name>

I know, this is no real solution to your problem, but at least a way it would work.

leo
  • 11
  • 1
  • 1
    It sounds more like you have a question than an answer. If you get R_X86_64_32S relocation errors on Windows, you'll also get them on Linux if your gcc is configured with `-pie` as the default (like on most recent distros). [Avoiding 32-bit absolute addressing](https://stackoverflow.com/q/50205129) is the obvious solution here, or make a non-PIE executable on Windows. (Assuming you actually have 64-bit code in the first place, unlike in this question where the 32-bit addressing mode is a clue that it's intended for 32-bit mode.). – Peter Cordes Jun 14 '18 at 08:00
-1

When I had this problem the answer was simple, I was trying to compile wrong.

Once I created the object files: gcc -g -O -c main.c functions.c

I could compile the program: gcc main.o functions.o -o prog

Sorry, I don't know assembly so I don't know if this is applicable to the OP. Hopefully this helps someone though.

J.Sunderland
  • 50
  • 1
  • 3
  • 1
    No, this is not useful at all. Compiling with the default `-m64` would give this error with this code for many setups, because it's written for 32-bit mode. And if you had actual 64-bit code that gave this error, this answer isn't helpful. Also, you don't need to compile to `.o` separately, you can just `gcc -g -O3 -o prog *.c` – Peter Cordes May 16 '19 at 18:08