Page 1 of 2

Having trouble with header fiels

Posted: Sat May 22, 2010 8:18 pm
by HitmanYesman
This is what is happening: I call a function (any function) and the CPU faults and reboots (on Bochs). Before I had everything in the same file, but decided to move into headers. I have one header file for every c file meaning screen.h/screen.c. All functions create a fault so I know it's not the code inside the function bodies (also have used for loops). Now for example inside screen.h I have a function declaration as: extern void cls(); (surrounded by #ifndef/#define/#endif). All header files are inside an include folder so I add the option: -I./include/kernel when compiling. Now in screen.c, I first include screen.h and then add my function code, like void cls() {...}. In kmain.c I include the screen.h file and I call the function cls. It faults and reboots.

Here is the code if you didn't understand what I'm saying (These are only the relevant parts of the files):

kmain.c:

Code: Select all

#include <../include/kernel/screen.h>

void kmain(void* mbd, unsigned int magic)
{
	cls();
        ....
}

screen.h:

Code: Select all

#ifndef SCREEN_H
#define SCREEN_H

#include <../include/kernel/common.h>

extern void cls();

#endif

screen.c:

Code: Select all

#include <../include/kernel/screen.h>

uint16_t *video = (uint16_t *) 0xB8000;
uint8_t xpos = 0;
uint8_t ypos = 0;

void cls()
{
	uint8_t aByte = (0 << 4) | (15 & 0x0F);
	uint16_t blank = 0x20 | (aByte << 8);
	
	uint32_t i;
	
	for (i = 0; i < 80*25; i++)
	{
		video[i] = blank;
	}
	
	xpos = 0;
	ypos = 0;
}

Please help me to see what I'm doing wrong. Also here's how I'm compiling: (I'm using a Makefile)

Code: Select all

CSOURCES=kernel.c screen.c
SOURCES=loader.o kernel.o screen.o
CFLAGS=-nostdlib -nostdinc -fno-builtin -I../include/
LDFLAGS=-Tlinker.ld

all:
	nasm -f elf -o loader.o loader.s
	gcc -o kernel.o -c kernel.c $(CFLAGS)
	gcc -o screen.o -c screen.c $(CFLAGS)
	i586-elf-ld $(LDFLAGS) -o kernel.bin $(SOURCES)
	cat kpad kernel.bin > floppy.img
I then move floppy.img into the bochs directory and start bochs.

linker.ld

Code: Select all

ENTRY (loader)

SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

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

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

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
Thanks for taking the time to look at this.

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 8:36 pm
by Gigasoft
Have you checked the disassembly to see if there is anything that shouldn't be there, such as references to default libraries? What's in the kpad file?

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 8:38 pm
by montrom
Shot in the dark. Replace < with " when using relative paths.

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 8:44 pm
by piranha
Are you sure it even makes it into kmain()?

(and the usual...are interrupts disabled?)

Did it work before you split everything into different files?

-JL

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 9:37 pm
by HitmanYesman
gigasoft: Doesn't seem to be anything that shouldn't be in it (might've missed something, so going over it again). As for kpad, I'm using GRUB and stage1/stage2 and a 750 byte pad are in there. It's to insure that the kernel starts at 0x100000.

montrom: Nope, made no difference.

piranha: Yes, I'm sure it does. I can insert a for loop right before the call to cls and it sits there. In my GRUB loader file I disable interrupts before calling kmain. Yes, when all the functions were in the kmain.c, it worked fine.

Anymore suggestions? Thanks for helping.

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 10:04 pm
by Solar
I assume you have a copy of the previous code in your version control software, or as a file backup.

You do still have it, don't you? :twisted:

I'd go back to that version, and then repeat the process of moving things into seperate files, one small step and one function at a time.

Without having a look at the whole source, it's a bit tricky to guess what's wrong. I assume something with the stack setup went kaputt, i.e. splitting the functions across multiple files wasn't the only thing you changed.

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 10:31 pm
by HitmanYesman
That's the thing though. I did go back to it and just tried to move cls over to a new file. I even made duplicates and just scratched everything except the GRUB loader, kmain (with just calling cls), and the screen.h/screen.c files.

I don't want everything to be in one file... so I need to figure this out.

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 10:42 pm
by Gigasoft
Maybe something's wrong in kernel.bin? The calls having wrong operands or the functions from the other files not being there?

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 11:07 pm
by montrom
Question. Does it matter if you are using i586-elf-ld and not also i586-elf-gcc?

Re: Having trouble with header fiels

Posted: Sat May 22, 2010 11:46 pm
by JackScott
It depends on a large number of variables, but the short answer is yes, it probably does. If you've got a cross compiler, use all of it's tools.

Re: Having trouble with header fiels

Posted: Sun May 23, 2010 12:55 am
by HitmanYesman
Tried using i586-elf-gcc. I get a bunch of weird errors such as: .type pseudo-opused outside of .def/.endef. When I have no lines of code outside of the ifndef.

Re: Having trouble with header fiels

Posted: Sun May 23, 2010 5:21 am
by montrom
Try stripping out the following from your header:

Code: Select all

#ifndef SCREEN_H
#define SCREEN_H
#endif
And, does cls() need extern?

Re: Having trouble with header fiels

Posted: Sun May 23, 2010 5:23 am
by HitmanYesman
montrom: Nope, didn't seem to work... keeps rebooting.

Re: Having trouble with header fiels

Posted: Sun May 23, 2010 5:25 am
by montrom
So, it at least compiled now?

Re: Having trouble with header fiels

Posted: Sun May 23, 2010 5:29 am
by HitmanYesman
Huh? It was always compiling. It was just that it kept faulting and rebooting.