Autor Thema: keine Nutzung von Globalen Variablen möglich.  (Gelesen 2106 mal)

james

  • Beiträge: 2
    • Profil anzeigen
Gespeichert
« am: 27. February 2012, 11:39 »
Guten Tag

Sobald ich mein System über den GRUB starte, kann ich keine globalen Variablen nutzen. (qemu -fda floppy.img)
Das heisst wenn ich im globalen Bereich z.B eine char* deklariere & definiere, gibt die printf funktion nichts aus, wenn ich diese Variable als Parameter übergebe.
wenn ich aber denn Kernel mit qemu direkt starte, dann kann ich die globalen Variablen problemlos nutzen. (qemu -kernel kernel.bin)
Muss ich irgendwo noch explizit für den globalen Bereich speicher reservieren?


Die relevanten Files:

loader.asm
Zitat
global loader                           ; making entry point visible to linker
 
extern kernel                            ; kmain is defined in kmain.cpp
 
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS     equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
section .text
 
align 4
    dd MAGIC
    dd FLAGS
    dd CHECKSUM
 
; reserve initial kernel stack space
STACKSIZE equ 0x200000                    ; that's 2MB
;STACKSIZE equ 0xFFFF
 
loader:
    mov  esp, stack + STACKSIZE         ; set up the stack
    push eax                            ; Multiboot magic number
    push ebx                            ; Multiboot info structure
 
    call kernel                          ; call kernel proper
 
    cli
.hang:
    hlt                                 ; halt machine should kernel return
    jmp  .hang
 
section .bss
 
align 4
stack:
    resb STACKSIZE                      ; reserve 16k stack on a doubleword boundary
kernel.c
Zitat
#include "global.h"
#include "video.h"
#include "keyboard.h"
#include "commandline.h"
#include "utils.h"

void kernel( void* mbd, unsigned int magic )
{

 
   if ( magic != 0x2BADB002 )
   {
      /* Something went not according to specs. Print an error */
      /* message and halt, but do *not* rely on the multiboot */
      /* data structure. */
      printf("GRUB-Magic-Number ist nicht korrekt!",10);
   }
 
   /* You could either use multiboot.h */
   /* (http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#multiboot_002eh) */
   /* or do your offsets yourself. The following is merely an example. */
   //char * boot_loader_name =(char*) ((long*)mbd)[16];

   while((inportb(0x64) & 0x2)) {}
    outportb(0x60, 0xF4);
   

   clear_screen();
   printf("Welcome to TinyOS 0.1 !", 0);
   changeColor(GREEN_TEXT,0x0);
   update_cursor(1,0);
}

loader.ld
Zitat
ENTRY (loader)

SECTIONS
{
    . = 0x00100000;

    .text ALIGN (0x1000) :
    {
        *(.text)
    }

    .rodata ALIGN (0x1000) :
    {
        *(.rodata*)
    }

    .data ALIGN (0x1000) :
    {
        *(.data)
    }

    .bss :
    {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
comple_scriph.sh
Zitat
nasm -f elf -o loader.o loader.asm
gcc -m32 -o kernel.o -c kernel.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
gcc -m32 -o clear_screen.o -c clear_screen.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
gcc -m32 -o change_color.o -c change_color.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
gcc -m32 -o printf.o -c printf.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
gcc -m32 -o cursor.o -c cursor.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
gcc -m32 -o utils.o -c utils.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
gcc -m32 -o keyboard.o -c keyboard.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
gcc -m32 -o commandline.o -c commandline.c -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs
ld -m elf_i386 -T loader.ld -o kernel.bin loader.o kernel.o clear_screen.o change_color.o printf.o cursor.o utils.o keyboard.o commandline.o
cat  stage1 stage2 pad kernel.bin > floppy.img


Danke im Voraus.


Freundliche Grüsse
James

« Letzte Änderung: 27. February 2012, 11:42 von james »

Sannaj

  • Beiträge: 103
    • Profil anzeigen
Gespeichert
« Antwort #1 am: 27. February 2012, 16:51 »
Kann es sein, dass dein Programm mit den Adressen durcheinander kommt. Das passiert, wenn du absolute Adressen benutzt, dein Kernel aber nicht an die selbe Stelle geladen wird.

james

  • Beiträge: 2
    • Profil anzeigen
Gespeichert
« Antwort #2 am: 02. March 2012, 08:40 »
Kann es sein, dass dein Programm mit den Adressen durcheinander kommt. Das passiert, wenn du absolute Adressen benutzt, dein Kernel aber nicht an die selbe Stelle geladen wird.

Es lag am Linkerscript. habe es jetzt ergänzt und nun klappt alles problemlos. Vielen Dank aber trotzdem.
Zitat
ENTRY (loader)

SECTIONS
{
    .text 0x00100000 :
    {
   code = .;
        *(.text)
   . = ALIGN(4096);
    }
    .data :
    {
   data = .; _data = .; __data = .;
   *(.rodata)
        *(.data)
   . = ALIGN(4096);
    }

    .bss :
    {
   bss = .; _bss = .; __bss = .;
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
   . = ALIGN(4096);
    }
    end = .; _end = .; __end =.;
}


Freundliche Grüsse
James

 

Einloggen