Having trouble with header fiels

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.
HitmanYesman
Member
Member
Posts: 47
Joined: Fri Apr 23, 2010 8:27 am

Having trouble with header fiels

Post 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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Having trouble with header fiels

Post 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?
montrom
Member
Member
Posts: 86
Joined: Thu May 13, 2010 1:45 pm

Re: Having trouble with header fiels

Post by montrom »

Shot in the dark. Replace < with " when using relative paths.
Visit the Montrom user page for more info.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: Having trouble with header fiels

Post 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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
HitmanYesman
Member
Member
Posts: 47
Joined: Fri Apr 23, 2010 8:27 am

Re: Having trouble with header fiels

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Having trouble with header fiels

Post 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.
Every good solution is obvious once you've found it.
HitmanYesman
Member
Member
Posts: 47
Joined: Fri Apr 23, 2010 8:27 am

Re: Having trouble with header fiels

Post 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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Having trouble with header fiels

Post by Gigasoft »

Maybe something's wrong in kernel.bin? The calls having wrong operands or the functions from the other files not being there?
montrom
Member
Member
Posts: 86
Joined: Thu May 13, 2010 1:45 pm

Re: Having trouble with header fiels

Post by montrom »

Question. Does it matter if you are using i586-elf-ld and not also i586-elf-gcc?
Visit the Montrom user page for more info.
User avatar
JackScott
Member
Member
Posts: 1032
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Mastodon: https://aus.social/@jackscottau
GitHub: https://github.com/JackScottAU
Contact:

Re: Having trouble with header fiels

Post 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.
HitmanYesman
Member
Member
Posts: 47
Joined: Fri Apr 23, 2010 8:27 am

Re: Having trouble with header fiels

Post 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.
montrom
Member
Member
Posts: 86
Joined: Thu May 13, 2010 1:45 pm

Re: Having trouble with header fiels

Post 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?
Visit the Montrom user page for more info.
HitmanYesman
Member
Member
Posts: 47
Joined: Fri Apr 23, 2010 8:27 am

Re: Having trouble with header fiels

Post by HitmanYesman »

montrom: Nope, didn't seem to work... keeps rebooting.
montrom
Member
Member
Posts: 86
Joined: Thu May 13, 2010 1:45 pm

Re: Having trouble with header fiels

Post by montrom »

So, it at least compiled now?
Visit the Montrom user page for more info.
HitmanYesman
Member
Member
Posts: 47
Joined: Fri Apr 23, 2010 8:27 am

Re: Having trouble with header fiels

Post by HitmanYesman »

Huh? It was always compiling. It was just that it kept faulting and rebooting.
Post Reply