Wrong addresses being pushed on the stack

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.
Davidm44
Posts: 13
Joined: Sun Dec 26, 2010 10:52 am

Wrong addresses being pushed on the stack

Post by Davidm44 »

Title kind of sums it up.
Details:

I have a basic bootloader that loads the kernel, the kernel ( not really a kernel.. ) is just supposed to output a few letter to the screen. The problem is it doesn't push the right addresses onto the stack for the function to use.

The kernel is basically a linked object file compiled in C and an object file assembled in NASM.

The weird thing is, it will only fail if pointers are involved ( i.e. character arrays ), when I call the function to print using a single character, rather than an array, it works fine.

Anyone know whats going on? o_O

Note: I'm new to all this OS development stuff, don't go too hard on me..
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Wrong addresses being pushed on the stack

Post by Chandra »

Davidm44 wrote:Title kind of sums it up.
Details:

I have a basic bootloader that loads the kernel, the kernel ( not really a kernel.. ) is just supposed to output a few letter to the screen. The problem is it doesn't push the right addresses onto the stack for the function to use.

The kernel is basically a linked object file compiled in C and an object file assembled in NASM.

The weird thing is, it will only fail if pointers are involved ( i.e. character arrays ), when I call the function to print using a single character, rather than an array, it works fine.

Anyone know whats going on? o_O

Note: I'm new to all this OS development stuff, don't go too hard on me..
No enough information provided to answer your question.

Still, assuming you are in Real Mode, I guess there's a problem with setup of segment registers.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
Davidm44
Posts: 13
Joined: Sun Dec 26, 2010 10:52 am

Re: Wrong addresses being pushed on the stack

Post by Davidm44 »

Chandra wrote:
Davidm44 wrote:Title kind of sums it up.
Details:

I have a basic bootloader that loads the kernel, the kernel ( not really a kernel.. ) is just supposed to output a few letter to the screen. The problem is it doesn't push the right addresses onto the stack for the function to use.

The kernel is basically a linked object file compiled in C and an object file assembled in NASM.

The weird thing is, it will only fail if pointers are involved ( i.e. character arrays ), when I call the function to print using a single character, rather than an array, it works fine.

Anyone know whats going on? o_O

Note: I'm new to all this OS development stuff, don't go too hard on me..
No enough information provided to answer your question.

Still, assuming you are in Real Mode, I guess there's a problem with setup of segment registers.
Sorry about that.

I'm in protected mode at the moment. What other information would I need to provide for a definite answer? Or atleast a push in the right direction. I'm debugging but I'm not sure what to look for..
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Wrong addresses being pushed on the stack

Post by DavidCooper »

You could try pasting a relevant chunk of your source code into a post (then highlight it and click on the "Code" button higher up the screen). Make sure it's well commented so that people don't have to work hard to follow it.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Wrong addresses being pushed on the stack

Post by Chandra »

DavidCooper wrote:You could try pasting a relevant chunk of your source code into a post
and the linker script as well.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
Davidm44
Posts: 13
Joined: Sun Dec 26, 2010 10:52 am

Re: Wrong addresses being pushed on the stack

Post by Davidm44 »

Sorry for the late reply, I wasn't on my usual computer, I didn't have my files with me.

Okay here you go, it's pretty much the basic of the basics at the moment until I continue with my readings on OS development.

Here's the portion of the bootloader that is jumping to the kernel.

Code: Select all

[bits 32]



enter_32:



	mov ax,10h

	mov ds,ax

	mov ss,ax

	mov esp,090000h



	call 0x08:0x1000
Here is the assembly portion of the kernel ( jumps to an externel function in my C object file )

Code: Select all

[bits 32]

global main
extern kmain

section .text
main:

	call kmain

	hlt
And here's the C function I'm jumping to.

Code: Select all

unsigned char *D = "TA";

extern int kmain()
{
	unsigned char* vidMem = (unsigned char*)0xB8000;

	vidMem[0] = *D;
	vidMem[1] = 0x0E;

	return 0;
}

There isn't any function to print yet because I'm having troubles with the global variables at the moment. But pointers don't seem to be working, regardless.

Here's how I'm putting it all together. I'm using gcc and NASM.

Code: Select all

nasm -f elf bootmain.asm

gcc -ffreestanding -m32 -c -o main.o main.c

ld -Ttext 0x1000 -melf_i386 --oformat elf32-i386 -o kernel.o bootmain.o main.o

objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
Thanks.
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: Wrong addresses being pushed on the stack

Post by Tosi »

I'm assuming you have a GDT set up with a 32-bit privileged code selector of 0x08 and a data one of 0x10, as the code seems to imply.

Code: Select all

 mov esp,090000h
How do you know it's safe to put your stack at 0x00090000? There might be important data structures there you will want later. It's better to reserve memory in your kernel's BSS section for the stack. And yes, it should work with a flat binary file.

Code: Select all

gcc -ffreestanding -m32 -c -o main.o main.c
Since it appears you are not using a cross compiler, you may be linking in things that you don't want, among other problems. It is recommended to use one for OS development.

Code: Select all

ld -Ttext 0x1000 -melf_i386 --oformat elf32-i386 -o kernel.o bootmain.o main.o
You are not setting the addresses of your .data, .bss, and .rodata sections at least. Either specify that on the command line, or use a linker script.
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: Wrong addresses being pushed on the stack

Post by Combuster »

call 0x08:0x1000
So you are calling what's supposed to be the start of your text section. Do you have any idea what's there? (and how did you verify that?) Why is ES left undefined?

Do you have any experience with the bochs debugger? You can use that to see if your code is actually loaded where you expect it to be, in the way you expect it to be.
"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 ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Wrong addresses being pushed on the stack

Post by Solar »

Have you checked that your string actually ends up in the binary?

I have only skimmed the thread, but this looks like an old classic (.rodata not getting linked into the binary).
Every good solution is obvious once you've found it.
Davidm44
Posts: 13
Joined: Sun Dec 26, 2010 10:52 am

Re: Wrong addresses being pushed on the stack

Post by Davidm44 »

Combuster wrote:
call 0x08:0x1000
So you are calling what's supposed to be the start of your text section. Do you have any idea what's there? (and how did you verify that?) Why is ES left undefined?

Do you have any experience with the bochs debugger? You can use that to see if your code is actually loaded where you expect it to be, in the way you expect it to be.
The call to kmain is in my text section, see above. I have used bochs debugger, I found where my string is, its underneath the main function ( I would assume thats where it should be, since thats my only function at the moment ).

I gave the ld command -Tdata 0x1000 but I get the same results.

When I use the linker script provided from osdev's wiki, exact same results. I'm completely stuck.
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: Wrong addresses being pushed on the stack

Post by Tosi »

Are you sure the bootloader loads your kernel correctly at 0x1000 (which is a bad place for protected mode anyway)?
Davidm44
Posts: 13
Joined: Sun Dec 26, 2010 10:52 am

Re: Wrong addresses being pushed on the stack

Post by Davidm44 »

Tosi wrote:Are you sure the bootloader loads your kernel correctly at 0x1000 (which is a bad place for protected mode anyway)?
Yes I'm sure, before attempting to make a kernel in C I tried doing some basic printing using assembly, and it worked fine. I would assume it's because I can declare the data section and text section in the code itself, but I'm not sure...
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Wrong addresses being pushed on the stack

Post by b.zaar »

Davidm44 wrote:The call to kmain is in my text section, see above. I have used bochs debugger, I found where my string is, its underneath the main function ( I would assume thats where it should be, since thats my only function at the moment ).

I gave the ld command -Tdata 0x1000 but I get the same results.

When I use the linker script provided from osdev's wiki, exact same results. I'm completely stuck.
When you say the string is underneath your main function do you mean before main in memory? I haven't used Bochs debugger so I don't know how the output looks.
If your string is before your main then you'd be jumping into your data section not your code.
Use -Map with LD to see the order of your sections in the compiled kernel.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: Wrong addresses being pushed on the stack

Post by Tosi »

The ELF format uses the .rodata section for strings and other read-only data, make sure you include that section in your final executable.
Davidm44
Posts: 13
Joined: Sun Dec 26, 2010 10:52 am

Re: Wrong addresses being pushed on the stack

Post by Davidm44 »

Tosi wrote:The ELF format uses the .rodata section for strings and other read-only data, make sure you include that section in your final executable.
Add it while linking or add it in the code itself..?

Also, Idk if I mentioned it, but I can access my variables if it isn't a character array, like if I do:

Code: Select all

char singleChar = 'A';
If I use a character array, stops working..
Post Reply