cannot find a way to link kernel

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
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

cannot find a way to link kernel

Post 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
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
sandras
Member
Member
Posts: 181
Joined: Thu Nov 03, 2011 9:30 am

Re: cannot find a way to link kernel

Post 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?
https://github.com/shimanauskas
Slava Ukraini!
Šalin rankas nuo laisvo žodžio!
User avatar
xenos
Member
Member
Posts: 1144
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
GitHub: https://github.com/xenos1984
Contact:

Re: cannot find a way to link kernel

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: cannot find a way to link kernel

Post 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'
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: cannot find a way to link kernel

Post 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.
User avatar
xenos
Member
Member
Posts: 1144
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
GitHub: https://github.com/xenos1984
Contact:

Re: cannot find a way to link kernel

Post 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?
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: cannot find a way to link kernel

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply