Page 1 of 1

ASM bug: Relocation truncatted to fit R_386_16

Posted: Wed Jan 23, 2008 9:27 pm
by junkoi
I am compiling the below small code with GNU AS on Linux. The compile command is:

Code: Select all

gcc -nostdlib a.s
And it reports the error "relocation truncatted to fit R_386_16 against .text". And the problem caused by the line:

Code: Select all

movw ax, $ok_msg
Anybody please tell me how to fix this?

(The code must be in 16bit because I want to run it in Real-mode in x86)

Thanks,
Jun

Code: Select all

/* a.s */
.text
.global _start

_start:
        .code16
        movw $ok_msg, %ax   /* <=== WHY THIS CAUSES A BUG? */
        ret

ok_msg: .string "OK\n"

Re: ASM bug: Relocation truncatted to fit R_386_16

Posted: Wed Jan 23, 2008 10:07 pm
by Zenith
Now, I'm not that experienced in linux (main dev environment is cygwin on windows), but there are a few problems in what you're doing...

Code: Select all

gcc -nostdlib a.s
Since you're on linux, I'm presuming you're compiling it to ELF (since there's no compiler flag saying otherwise). Correct me if I'm wrong, but does ELF (or if it does, then GAS) support 16-bit relocation? Maybe try flat binary instead... (but it all depends on how you plan to run this program in real mode)

I don't think -nostdlib is enough to prevent linking other unwanted (not to mention 32-bit) stuff in when compiling to real mode...

Code: Select all

.global _start

_start:
        .code16
        movw $ok_msg, %ax   /* <=== WHY THIS CAUSES A BUG? */
        ret 
Maybe putting the ".code16" thing at the beginning could help...

And don't forget that once you fix this issue, real mode segment addressing and offsets can mess up everything (since you're not running at 0x0000:0x0000 unless in V86 mode, i think)

Good luck :wink:
Julian

Re: ASM bug: Relocation truncatted to fit R_386_16

Posted: Thu Jan 24, 2008 12:45 am
by junkoi
karekare0 wrote:Now, I'm not that experienced in linux (main dev environment is cygwin on windows), but there are a few problems in what you're doing...

Code: Select all

gcc -nostdlib a.s
Since you're on linux, I'm presuming you're compiling it to ELF (since there's no compiler flag saying otherwise). Correct me if I'm wrong, but does ELF (or if it does, then GAS) support 16-bit relocation? Maybe try flat binary instead... (but it all depends on how you plan to run this program in real mode)

I don't think -nostdlib is enough to prevent linking other unwanted (not to mention 32-bit) stuff in when compiling to real mode...
I use -nostdlib with 32bit ASM code, and it works well. So I think that is fine.

Code: Select all

.global _start

_start:
        .code16
        movw $ok_msg, %ax   /* <=== WHY THIS CAUSES A BUG? */
        ret 
Maybe putting the ".code16" thing at the beginning could help...
Unfortunately this trick doesn't help.

This silly bug makes me bang my head in the keyboard for few days already. Very frustrated and down now !!!

Thanks,
Jun

Re: ASM bug: Relocation truncatted to fit R_386_16

Posted: Thu Jan 24, 2008 1:28 am
by Solar
junkoi wrote:I use -nostdlib with 32bit ASM code, and it works well.
To be expected, because the things that get linked in are natively in 32bit mode. The problem arises when you try to compile 16-bit ASM into an userspace executable - because when it tries to link in the 32-bit runtime bits, it dies.

At least, that is what I am guessing here, since GAS is perfectly capable of generating real-mode flat binary code - GRUB is written in GAS, after all.

Try not to go through the GCC frontend, but call 'as' directly. Check 'info as' for command line options that might help. Check out the GRUB sources for how they did it.