Page 1 of 1

cannot find a way to link kernel

Posted: Sat Apr 07, 2012 1:09 pm
by BASICFreak
before I get started here is the codes:

start.asm

Code: Select all

bits 16					; we are still in real mode
; we are loaded at linear address 0x10000
jmp main				; jump to main
main:
;set graphincs mode 320x200 256 colors
mov ah, 00
mov al, 13
int 10h

;enterPMode
cli
xor ax, ax
mov ds, ax
lgdt [toc]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 08h:clean_up

bits 32
global kernel
extern _k_main

clean_up:
	mov ax, 10h
	mov ds, ax
	mov ss, ax
	mov esp, 090000h
kernel:
	call _k_main
	cli
	hlt

gdt_data:
	dd 0
	dd 0
	dw 0FFFFh
	dw 0
	db 0
	db 10011010b
	db 11001111b
	db 0
	dw 0FFFFh
	dw 0
	db 0
	db 10010010b
	db 11001111b
	db 0
end_of_gdt:

toc:
	dw end_of_gdt - gdt_data -1
	dd gdt_data
graphics.h

Code: Select all

char *vga = 0xA0000;
void pset(int x, int y, char color)
{
	vga[(y * 320) + x] = color;
}
main.c

Code: Select all

#include "graphics.h"

k_main()
{
	int xx = 0;
	int yy = 0;
	for (xx = 0; xx < 320; xx++)
	{
		for (yy = 0; yy < 200; yy++)
		{
			pset(xx, yy, 17);
		}
	}
}
I am using nasm and GCC at the moment.

I'm running into the problem where LD cannot link 16-BIT instructions.
Does anyone know any tricks or another linker that I can use.

I cannot put the video and PMode loader in my bootsector wich is already pushing 508 bytes

Re: cannot find a way to link kernel

Posted: Sat Apr 07, 2012 1:47 pm
by sandras
BASICFreak wrote: graphics.h

Code: Select all

char *vga = 0xA0000;
void pset(int x, int y, char color)
{
	vga[(y * 320) + x] = color;
}
Code in a header file?

Re: cannot find a way to link kernel

Posted: Sat Apr 07, 2012 1:49 pm
by xenos
It would be helpful if you could provide the commands you use to compile, assemble and link your code, as well as the error message you obtain.

Re: cannot find a way to link kernel

Posted: Sat Apr 07, 2012 1:59 pm
by BASICFreak
link.ld

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(kernel)
phys = 0x0010000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
nasm -f aout -o start.o .\src\start.asm
gcc -c .\src\main.c -o main.o
ld -T link.ld -o kernel.bin start.o main.o

when linking i get this error
start.o:start.o:(.text+0x18): relocation truncated to fit: 16 against `.text'

Re: cannot find a way to link kernel

Posted: Sat Apr 07, 2012 2:02 pm
by Rudster816
You shouldn't add the leading underscore in the NASM external definitions.

The only way to link 16 bit code is to make sure that no relocations are generated for that code. You can see an example of how to do this in the Linux source with the SMP trampoline. The relevant source file would be arch/x86/kernel/trampoline_32.S.

What you should really do is write a proper boot loader, or use someone else's. You shouldn't be going from the boot sector straight to the kernel, there just isn't enough room to do everything that a proper boot loader should do.

Re: cannot find a way to link kernel

Posted: Sat Apr 07, 2012 3:32 pm
by xenos
BASICFreak wrote:start.o:start.o:(.text+0x18): relocation truncated to fit: 16 against `.text'
If you disassemble the file start.o, which instruction do you find at address 0x18 of the .text segment?

Re: cannot find a way to link kernel

Posted: Sun Apr 08, 2012 3:44 pm
by Combuster
XenOS wrote:If you disassemble the file start.o, which instruction do you find at address 0x18 of the .text segment?
LGDT [toc] and the far jump are both capable of causing that error, and it doesn't quite matter as both are absolute addresses over 1M.
The only way to link 16 bit code is to make sure that no relocations are generated for that code.
That is not strictly true:

LD is very well capable of linking 16-bit relocations. The problem arises when code is mixed between 16 and 32-bit code. The 32-bit code is typically linked against its physical location, the 16 bit code might be, but it might also take real mode segmentation into account. At any rate, real mode has no sane access to addresses over 1M so trying to link your code to that address is most likely wrong.

The only way you can get a construction like this to work is by making one side strictly position-independent so it can't find anything to complain about during linking, or you separate the 16 and 32 bit code and link both to their own correct offsets. You can do it in a single run of LD or you can use one of many custom methods of combining two separately linked binaries into one.