Linking problems..

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.
jrfritz

Linking problems..

Post by jrfritz »

I've gotten GRUB to compile on linux...

now I can't get it to load my kernel!

I set it to load kernel32.sys...it says it's a invalid format...what's the default format for grub compiled on linux? And how would I link my kernel to that format?
nullify

Re:Linking problems..

Post by nullify »

What command do you use to link your kernel?
eL JeDi

Re:Linking problems..

Post by eL JeDi »

Hi Tom,


R u using an ELF kernel? or have you added the GRUBmultiboot header to the kernel?

If not, here is:

---- from http://osdev.neopages.net/tutorials/grub.php --

ELF kernels
-----------
GRUB understands the ELF file format directly. If your kernel
is ELF, you can use the simple Multiboot header shown here:

MULTIBOOT_PAGE_ALIGN equ 1<<0
MULTIBOOT_MEMORY_INFO equ 1<<1

MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

; The Multiboot header (in NASM syntax)
align 4
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd CHECKSUM

Put this near the beginning of your kernel startup code, then
build your kernel. After the kernel is built, you can use the
GRUB "mbchk" utility to test if the kernel complies with
Multiboot.

Take a look to the web, 'cos it explain well how to use GRUB with yout own kernel


hope it helps
jrfritz

Re:Linking problems..

Post by jrfritz »

I tested that...the util says no multi boot header...here is the code:

; * kernelasm.asm : FritzOS C++ Kernel Assembly Routines and Second Stage Loader
; Copyright (C) 2002 Tom Fritz
;
; * This program is a part of the FritzOS kernel, and may be freely
; * copied under the terms of the GNU General Public License (GPL),
; * version 2, or at your option any later version.
;
; For more info, look at the COPYING file.
;

; GRUB stuff

MULTIBOOT_PAGE_ALIGN   equ 1<<0
MULTIBOOT_MEMORY_INFO   equ 1<<1
MULTIBOOT_AOUT_KLUDGE   equ 1<<16
MULTIBOOT_HEADER_MAGIC   equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS   equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
MULTIBOOT_CHECKSUM   equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)


; Tell NASM that this is 32-bit PMode code
[BITS 32]
... etc...

and here is my linking file:
SECTIONS
{
.text 0x100000 : {
code = .; _code = .; __code = .;
*(.text)
. = ALIGN(4096);
}
.data : {
data = .; _data = .; __data = .;
*(.data)
. = ALIGN(4096);
}
.bss :
{
bss = .; _bss = .; __bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .; _end = .; __end = .;
}

and my linking command:
ld --oformat elf32-i386 -Tlink.ld -o ../source/temp/kernel32.sys ../source/object/kernelasm.o ../source/object/kernel32.o ../source/object/idt.o ../source/object/error.o ../source/object/fstdio.o ../source/object/x86.o ../source/object/string.o ../source/object/keys.o ../source/object/irq.o
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Linking problems..

Post by df »

make sure your multiboot header is at te begining of your code. so when you link your objects you need an asm stub and jumps to your c code.

when you link your stuff, make sure this is the first obect in your link file list, to keep it at the front.

eg: this is mine

Code: Select all

%define d dword
%define w word 
%define b byte 

[bits 32]
[section .text]

global start
global _main

extern edata
extern eend
extern _hi_mem_start
extern _stage1_main

start:
      lgdt   [start_gdt]
      jmp      dword 8:lgdt_fixup

alignb 4, db 0x0
lgdt_fixup:
      mov   edx, 0x10
      mov   ds,dx
      mov   es,dx
      mov   fs,dx
      mov   gs,dx
      mov   ss,dx

;; multiboot info in eax/ebx
      mov   [0x600],eax
      mov   [0x604],ebx   
   
      mov   esp,0x10000-4
      mov   ebp,esp
      
      push   dword 0
      popfl
      
      xor   eax,eax      
      mov edi,edata      
      mov   ecx,eend 
      sub   ecx,edi
      shr   ecx,2
      inc   ecx
      cld
      rep stosd      
      
      add   edi,0xFFF
      and   edi,0xFFFFF000
      
      ; end of memory, now take 8kb for init stack      
      add   edi,8192
      mov   [_hi_mem_start],edi
      
      ; setup new esp
      lea   esp,[edi-4]
      jmp   _stage1_main


      alignb 4, db 0x0
start_gdt:
      dw   23
      dd   start_gdt
      dw    0

      dd   0x0000ffff
      dd   0x00CF9A00

      dd   0x0000ffff
      dd   0x00CF9200
         

;//
;// put our GRUB bootloader header code here
;//

align 4, db 0x0
multiboot_header:
   dd   0x1BADB002               ;## magic
   dd   0x00000003               ;## flags[16]
   dd   0-(0x1BADB002 + 0x00000003)   ;## zero crc of fields 1+2
and this is my linker script

Code: Select all


ENTRY(start)
SECTIONS {
  .text 0x100000 : {
    *(.text)
    _etext = .;
    etext = _etext;
  }
  .data : {
    *(.data)
    _edata = .;
    edata = _edata;
  }
  .bss : {
    *(.bss)
    *(COMMON)
    _end = .;
    eend = _end;
  }
}
-- Stu --
jrfritz

Re:Linking problems..

Post by jrfritz »

where is edata and eend?
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Linking problems..

Post by df »

edata+eend are created by the linker script which specify the boundaries of DATA + BSS (so you can clean out BSS to zero and calc end of kernel size).
-- Stu --
Skywalker

Re:Linking problems..

Post by Skywalker »

Sorry form ay ignorance,


But DF, why is the multiboot header at the end of the file? In stead of you rellocate it in the begginnig of the code.. the header need to be at the begginnig of the compilied file, is it? ???

I don't undertand any the linking script, where can I get some tutorial? And sorry for the ignorance ???
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Linking problems..

Post by df »

Skywalker wrote: But DF, why is the multiboot header at the end of the file? In stead of you rellocate it in the begginnig of the code.. the header need to be at the begginnig of the compilied file, is it? ???

I don't undertand any the linking script, where can I get some tutorial? And sorry for the ignorance ???
i'm not sure I follow...

the multiboot header must be within the first.. 4kb or 8kb of your kernel, grub only looks that far into your file to find the header. so you must link your start code (that has your header in it + jump to c function) before any other code.

I use a linker script because thats the only way I can calculate where my data segment and my bss segment are and how big they are.

i need to know this since the BSS has uninitialsed variables in it, so we must zero the BSS memory area.
-- Stu --
Skywalker

Re:Linking problems..

Post by Skywalker »

Thanks

But what is the BSS memory zone? and what use we can give it? and why zero the bss?
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Linking problems..

Post by df »

here is an example c program

Code: Select all

#include "blah.h"

int MyGlobalVariable;

void main(void)
{
    MyGlobalVariable=3;
}
when we declare MyGlobalVariable, its uninitialsed (we dont give it any number UNTIL we get into our function).
since we dont give it anything, it does not really take up any _physical space_ inside your program on disk.

we set it all to zero, this is for checking NULL on pointers etc, and is correct practice to zero the bss.
-- Stu --
Skywalker

Re:Linking problems..

Post by Skywalker »

Thanks DF.

Can you tell any place where i can get some tutorial about the script linker? 'cos i don't undertand any thing about.
Skywalker

Re:Linking problems..

Post by Skywalker »

Thanks DF.

Can you tell me any place where i can get some tutorials about the linking script? I don't undertand nothing and i've seen its interesting.
jrfritz

Re:Linking problems..

Post by jrfritz »

This is where I was taught about linker scripts...download the file and read the info:

http://osdev.neopages.net/tutorials/basickernel.php
jrfritz

Re:Linking problems..

Post by jrfritz »

GRUB now says my kernel is too big to load or something...my text is at 1 meg...what's the default loading address and how do I change it? I can get grub to compile...
Post Reply