OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 6:23 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Wrong addresses being pushed on the stack
PostPosted: Tue Feb 08, 2011 6:53 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Tue Feb 08, 2011 8:13 pm 
Offline
Member
Member
User avatar

Joined: Sat Jul 17, 2010 12:45 am
Posts: 487
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 !


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Tue Feb 08, 2011 8:23 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Tue Feb 08, 2011 9:35 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2010 4:53 pm
Posts: 1150
Location: Scotland
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


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Tue Feb 08, 2011 11:53 pm 
Offline
Member
Member
User avatar

Joined: Sat Jul 17, 2010 12:45 am
Posts: 487
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 !


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Wed Feb 09, 2011 2:36 pm 
Offline

Joined: Sun Dec 26, 2010 10:52 am
Posts: 13
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:
[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:
[bits 32]

global main
extern kmain

section .text
main:

   call kmain

   hlt


And here's the C function I'm jumping to.
Code:
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:
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.


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Wed Feb 09, 2011 4:58 pm 
Offline
Member
Member

Joined: Tue Jun 15, 2010 9:27 am
Posts: 255
Location: Flyover State, United States
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:
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:
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:
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.

_________________
Getting_Started on the wiki
x86 technical information
Interrupt Jump Table
Real Programmers Don't Use Pascal
My open-source projects


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Thu Feb 10, 2011 2:59 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Quote:
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 ]


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Thu Feb 10, 2011 4:06 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Thu Feb 10, 2011 2:20 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Thu Feb 10, 2011 5:14 pm 
Offline
Member
Member

Joined: Tue Jun 15, 2010 9:27 am
Posts: 255
Location: Flyover State, United States
Are you sure the bootloader loads your kernel correctly at 0x1000 (which is a bad place for protected mode anyway)?

_________________
Getting_Started on the wiki
x86 technical information
Interrupt Jump Table
Real Programmers Don't Use Pascal
My open-source projects


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Thu Feb 10, 2011 7:29 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Thu Feb 10, 2011 10:35 pm 
Offline
Member
Member
User avatar

Joined: Wed May 21, 2008 4:33 am
Posts: 294
Location: Mars MTC +6:00
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


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Fri Feb 11, 2011 4:34 pm 
Offline
Member
Member

Joined: Tue Jun 15, 2010 9:27 am
Posts: 255
Location: Flyover State, United States
The ELF format uses the .rodata section for strings and other read-only data, make sure you include that section in your final executable.

_________________
Getting_Started on the wiki
x86 technical information
Interrupt Jump Table
Real Programmers Don't Use Pascal
My open-source projects


Top
 Profile  
 
 Post subject: Re: Wrong addresses being pushed on the stack
PostPosted: Fri Feb 11, 2011 7:19 pm 
Offline

Joined: Sun Dec 26, 2010 10:52 am
Posts: 13
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:
char singleChar = 'A';


If I use a character array, stops working..


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group