4

I'm using NASM Assembler under linux when I use the command "nasm -f elf -l hello.lst hello.asm" it works fine but when linking with gcc "gcc -o hello hello.o" it generates an error :

hello.asm:(.text+0x4): relocation truncated to fit: R_386_16 against `.data' collect2: ld returned 1 exit status

I learned that this is some problem with gcc and 16-bit

any solutions ?

Vanddel
  • 1,094
  • 3
  • 13
  • 32

2 Answers2

3

Linux doesn't even have a 16-bit subsystem - even if you got gcc to link it, you still couldn't execute it! Why don't you just use .bits 32?

Fabian Giesen
  • 3,231
  • 16
  • 16
  • 2
    If you need to write 16-bit code for homework, I recommend doing so in DOSBox and using a DOS linker (NASM had regular DOS releases at some point, not sure if it still does). I'm not aware of *any* port of GCC / binutils to x86 16-bit, no matter the OS (closest thing is DJGPP, but that's still 32-bit and requires a DOS extender). – Fabian Giesen Dec 06 '10 at 06:43
0

I faced the same situation a few years ago and again this year.

Two ways to deal with it are:

  1. Write your own 16-bit friendly linker (you need to parse .rel.text section at least).
  2. Use a base symbol (or symbols) to calculate the absolute addresses manually (the .rel sections are still created, but can be removed). However, this method is very error prone.

I always end up doing 2, but I should probably make time to write a linker (or patching GNU ld...).

bummi
  • 27,123
  • 14
  • 62
  • 101
Ismael Luceno
  • 2,055
  • 15
  • 26