NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: edgar2705 on November 18, 2012, 07:46:28 PM

Title: Have some linking errors with ld: "relocation truncated to fit"
Post by: edgar2705 on November 18, 2012, 07:46:28 PM
I have some errors then linking with ld:
Code: [Select]
lr3nix.asm:(.text+0x1): relocation truncated to fit: R_X86_64_8 against `.data'
lr3nix.asm:(.text+0x3): relocation truncated to fit: R_X86_64_8 against `.data'
lr3nix.asm:(.text+0x10): relocation truncated to fit: R_X86_64_8 against `.data'
lr3nix.asm:(.text+0x12): relocation truncated to fit: R_X86_64_8 against `.data'
Nasm doesn't show errors.
My version of nasm: 2.09.10
Version of ld: GNU ld (GNU Binutils for Ubuntu) 2.22
I compile:
Code: [Select]
nasm -f elf64 lr3nix.asm
Source code of my prog:
Code: [Select]
section .data
a: db 43
b: db -6
c: db -20
d: db 47
hex: db '0123456789ABCDEF'
msg: db 'Result: ',0x0
msgL: db $-msg

section .bss
res: resb 1
out_str resb 4

section .text

%define STDOUT 1

%macro write 2
mov rax, 4
mov rbx, STDOUT
mov rcx, %1
mov rdx, %2
int 0x80
%endmacro

global _start
_start:
mov al, a
mov bl, c
or al, bl
not al
mov [res], al
mov al, b
mov bl, d
or al, bl
and [res], al

res2hex:
xor rsi, rsi
xor rax, rax
mov al, [res]
shr rax, 4
mov rsi, rax
add rsi, [hex+rsi]
mov rax, rsi
mov [out_str], al
xor rsi, rsi
xor rax, rax
add rax, 0x0F
mov rsi, rax
add rsi, [hex+rsi]
mov rax, rsi
mov [out_str+1], al
mov byte [out_str+2], 0xA
mov byte [out_str+3], 0x0
print_res:
write msg, msgL
write out_str, 4

quit:
mov rax, 1
mov rbx, 0
int 0x80
Title: Re: Have some linking errors with ld: "relocation truncated to fit"
Post by: TightCoderEx on November 18, 2012, 08:27:46 PM
Your trying to load a 64 bit value into AL & BL.  Should be

Code: [Select]
     mov    al, [a]
     mov    bl, [c]

This takes the 8 bit value pointed to by a & c into al & bl.
Title: Re: Have some linking errors with ld: "relocation truncated to fit"
Post by: Frank Kotler on November 18, 2012, 08:57:39 PM
Code: [Select]
mov al, a
The root of your problem is that this attempts to put the address of the variable "a" into al. You surely want:
Code: [Select]
        mov al, [a] ; "[contents]" of a
(and similar instances)

Using the int 80h interface, and the 32-bit sys call numbers isn't really "right" in 64-bit code, but I'm told it still works, so you may want to leave it as is for now. Poke around the "examples" section and see if you can find an example of 64-bit code. Things are different in 64-bit!

Best,
Frank

Title: Re: Have some linking errors with ld: "relocation truncated to fit"
Post by: edgar2705 on November 19, 2012, 07:47:26 PM
Thank you all for your advices !
I rewrite my prog to true 64-bit (I found some usable links in 'examples' section), but have strange error. I convert hex to char and result must be 0x10 or 16 in integer, but convertion to char takes me 0x20. I think I'm wrong to use the offset in "hex" variable.
Code: [Select]
section .data
a: db 43
b: db -6
c: db -20
d: db 47
hex: db '0123456789ABCDEF'
hexl:       dq $-hexl
msg: db 'Result: ',0x0
msgL dq $-msg

section .bss
res: resb 1
out_str resb 4

section .text

%define STDOUT 1

%macro write 2
mov rax, 1
mov rdi, STDOUT
mov rsi, %1
mov rdx, %2
syscall
%endmacro

global _start
_start:
mov al, [a]
mov bl, [c]
or al, bl
not al
mov [res], al
mov al, [b]
mov bl, [d]
or al, bl
and [res], al

res2hex:
xor rax, rax
mov al, [res]
shr rax, 4
mov rsi, hex
add al, [rsi+rax] ; here
mov [out_str], al
xor rax, rax
mov al, [res]
and rax, 0xF
mov rsi, hex
add al, [rsi+rax] ; and here
mov [out_str+1], al
mov [out_str+2], byte 0xA
mov [out_str+3], byte 0x0
print_res:
write msg, [msgL]
write out_str, qword 4

quit:
mov rax, 60
mov rdi, 0
syscall
I have no idea where is the error...  :-\
Title: Re: Have some linking errors with ld: "relocation truncated to fit"
Post by: Frank Kotler on November 19, 2012, 08:29:42 PM
I'd have to convert this back to 32-bit to be able to actually try it. At first glance...
Code: [Select]
add al, [rsi + rax]
... looks suspicious to me. The lines you've got commented "here" "and here". I think you'd want a plain "mov" here (and here). As I understand it, "rax" is an offset into your "hex table" in "rsi". I think you'd want to replace this value, not add to it... See if that helps...

Best,
Frank

Title: Re: Have some linking errors with ld: "relocation truncated to fit"
Post by: edgar2705 on November 19, 2012, 08:36:20 PM
Thank you very much ! This is my stupid mistake, I replace add to mov and now everything is ok.  :)