Linker error - Can't spot the cause

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
ThisMayWork
Member
Member
Posts: 65
Joined: Sat Mar 22, 2014 1:14 pm
Location: /bin

Linker error - Can't spot the cause

Post by ThisMayWork »

Stage3.asm

Code: Select all

bits	32				; 32 bit code

extern kernel_entry

jmp	Stage3				; jump to entry point

%include "stdio.inc"
%include "Fat12.inc"			; FAT12 driver. Kinda :)

msg db  0x0A, 0x0A, "                                                "
    db  0x0A, 0x0A, "                         THEMOS Kernel Executing", 0x0A, 0

Stage3:

	mov	ax, 0x10		; set data segments to data selector (0x10)
	mov	ds, ax
	mov	ss, ax
	mov	es, ax
	mov	esp, 90000h		; stack begins from 90000h

	call	ClrScr32
	mov	ebx, msg
	call	Puts32
	call kernel_entry
      
	cli
	hlt
Linker.ld (Modified version from Wiki)

Code: Select all

ENTRY("kernel_entry")
STARTUP(Stage3.o)
/* Tell where the various sections of the object files will be put in the final
   kernel image. */
SECTIONS
{
	/* Begin putting sections at 1 MiB, a conventional place for kernels to be
	   loaded at by the bootloader. */
	. = 1M;

	/* First put the multiboot header, as it is required to be put very early
	   early in the image or the bootloader won't recognize the file format.
	   Next we'll put the .text section. */
	.text BLOCK(4K) : ALIGN(4K)
	{
		*(.multiboot)
		*(.text)
	}

	/* Read-only data. */
	.rodata BLOCK(4K) : ALIGN(4K)
	{
		*(.rodata)
	}

	/* Read-write data (initialized) */
	.data BLOCK(4K) : ALIGN(4K)
	{
		*(.data)
	}

	/* Read-write data (uninitialized) and stack */
	.bss BLOCK(4K) : ALIGN(4K)
	{
		*(COMMON)
		*(.bss)
		*(.bootstrap_stack)
	}

	/* The compiler may produce other sections, by default it will put them in
	   a segment with the same name. Simply add stuff here as needed. */
}
Error(s):

Code: Select all

toolchain/gcc/bin/i686-pc-elf-ld -T linker.ld -o kernel.elf kernel.o
Stage3.o: In function `ClusterLBA':
stage3.asm:(.text+0x12e): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x134): relocation truncated to fit: R_386_16 against `.text'
Stage3.o: In function `LBACHS':
stage3.asm:(.text+0x13b): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x141): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x147): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x14b): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x14e): relocation truncated to fit: R_386_16 against `.text'
Stage3.o: In function `ReadSectors.SECTORLOOP':
stage3.asm:(.text+0x160): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x164): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x168): relocation truncated to fit: R_386_16 against `.text'
stage3.asm:(.text+0x16c): additional relocation overflows omitted from the output
Any ideas?

EDIT: I was accidentally including 16 bit code, which resulted in those mysterious errors.
"Programming is an art form that fights back."
-Kudzu
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Linker error - Can't spot the cause

Post by alexfru »

You have the answer in your "Edit:". You were mixing 16-bit and 32-bit code, which isn't something to be done carelessly. Instructions compiled as 16-bit code by default have 16-bit displacements (see memory operand encodings in your favorite x86 CPU manual) in them. And even if you extend the displacement to 32 bits (by way of the address size override prefix, 0x67), the segment limit used by the 16-bit code must be large enough (it'll still be 65535 in real/v86 mode, btw, you need protected mode for this to work or big real/unreal mode). So, if you try to stick a 32-bit addresses into a 16-bit displacement, you deserve to get either of:
- a warning
- a link error
- a runtime error
Post Reply