Author Topic: Have some linking errors with ld: "relocation truncated to fit"  (Read 9779 times)

Offline unicornx

  • Jr. Member
  • *
  • Posts: 2
Hello, I have some errors then linking with ld:

I am writing a simple 16-bit real-mode demo code to print some char on screen, and assemble with nasm:

Code: [Select]
[org 0x7c00]
mov ah, 0x0e

mov al, "1"
int 0x10
mov al, the_secret ; just want to demo a wrong usage
int 0x10

mov al, "2"
int 0x10
mov al, [the_secret] ; correct usage
int 0x10

jmp $ ; infinite loop

the_secret:
    db "X"

times 510-($-$$) db 0
dw 0xaa55

I use
Code: [Select]
nasm -f bin bootsect.asm -o bootsect.bin, and it's ok

Then I tried to use gdb to trace the code, so I tried following:

I removed first line
Code: [Select]
[org 0x7c00] due to nasm will report "error: unrecognised directive [org]"

So I use ld to assign the load address with "-Ttext=0x7c00", following are my commands:
Code: [Select]
$ nasm bootsect.asm -f elf -g -F dwarf -o bootsect.o
$ i386-elf-ld -Ttext=0x7c00 bootsect.o -o bootsect.elf

But ld reports error:
Code: [Select]
bootsect.o:bootsect.asm:6:(.text+0x7): relocation truncated to fit: R_386_8 against `.text'

Checked code and I see this code line causes the problem
Code: [Select]
mov al, the_secret

I know it is not good writing, but I would have to keep this code due to I just want to write a demo and I know it could pass build with "nasm -f bin". So my question is how to pass building with ld so I can use gdb to trace this demo? Thanks in adv.



Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: Have some linking errors with ld: "relocation truncated to fit"
« Reply #1 on: March 15, 2020, 06:21:55 AM »
"the_secret" is a memory address/offset, so will not fit in an 8-bit register. I'm surprised it assembled, sounds like a bug to me.
My graphics card database: www.gpuzoo.com

Offline unicornx

  • Jr. Member
  • *
  • Posts: 2
Re: Have some linking errors with ld: "relocation truncated to fit"
« Reply #2 on: March 15, 2020, 06:50:12 AM »
Do you mean it is a bug for nasm? I am using NASM version 2.11.08

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 224
  • Country: gb
    • GPUZoo
Re: Have some linking errors with ld: "relocation truncated to fit"
« Reply #3 on: March 15, 2020, 01:42:41 PM »
It sounds like a nasm bug if it assembles a 16/32/64 bit number into an 8-bit register. It shouldn't be possible.
My graphics card database: www.gpuzoo.com