Problem with char array in gcc n fc7

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.
Post Reply
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Problem with char array in gcc n fc7

Post by Raven »

Hi
Please have a look at my test kernel code loaded at 0x10000
I have switched to protected mode, set up GDT,IDT and this code runs at ring0

Code: Select all

void print(char);
int main()
{
char * msg="HE";
char * vidmem=(char *) 0xb8000;

struct try
{char x;};

struct try a;

a.x='W';
print(a.x); // prints correctly
vidmem[2] = *msg; // junk is printed instead of H
vidmem[3]= 5;

me:goto me;
}

void print(char txt)
{
char * vidmem=(char *) 0xb8000;
vidmem[0] = txt;
vidmem[1]= 5;
}
Compilation script:
gcc -c kernel.c
ld -o kernel -Ttext 0x10000 -e main kernel.o
objcopy -R .note -R .comment -S -O binary kernel kernel.000

OS:
Fedora Core 7

Emulator:
Qemu

One more thing, i am using qemu to boot hard disk via
qemu -M pc -snapshot -kernel-kqemu -boot c -hda /dev/sda
I then select my os entry in GRUB to boot my second stage bootloader that loads kernel.
BUT when i recompile my kernel and put kernel.000 in my partition qemu fails to reflect changes unless i restart by pc.

Please help me out
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Problem with char array in gcc n fc7

Post by neon »

GCC places strings in a read only data section (.rdata or .rodata) of the binary. Insure you have defined these sections in your linker script.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Problem with char array in gcc n fc7

Post by JohnnyTheDon »

Mounted partitions don't immediatly write any changes to disk, they cache them until convenient. You need to run 'sync' or unmount the partition to flush the changes to disk.

And using your hard disk (or any other physical disk) in an emulator is a really bad idea. Make a disk image and put grub on it. You'll be able to mount it and use it like you're using your own HD now, but it doesn't risk damanging your disk or filesystem.
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Problem with char array in gcc n fc7

Post by Raven »

Thanks a lot everybody!
My linker script goes like this

Code: Select all

SECTIONS
{
  . = 0x10000;
  .text : { *(.text) }
  .rodata : { *(.rodata) } 
  .data : { *(.data) }
  .bss : { *(.bss) }
}
My compilation is like this

Code: Select all

gcc -c kernel.c 
ld -o kernel -TLinker.ld -e main -N kernel.o --verbose
objcopy -R .note -R .comment -S -O binary kernel kernel.000
My ndisasm shows this

Code: Select all

ndisasm kernel.000 -u -o 0x10000
00010000  8D4C2404          lea ecx,[esp+0x4]
00010004  83E4F0            and esp,byte -0x10
00010007  FF71FC            push dword [ecx-0x4]
0001000A  55                push ebp
0001000B  89E5              mov ebp,esp
0001000D  51                push ecx
0001000E  83EC10            sub esp,byte +0x10
00010011  C745F400800B00    mov dword [ebp-0xc],0xb8000
00010018  C745F82C000100    mov dword [ebp-0x8],0x1002c
0001001F  8B45F8            mov eax,[ebp-0x8]
00010022  0FB610            movzx edx,byte [eax]
00010025  8B45F4            mov eax,[ebp-0xc]
00010028  8810              mov [eax],dl
0001002A  EBFE              jmp short 0x1002a
0001002C  48                dec eax
0001002D  45                inc ebp
0001002E  00                db 0x00
My objdump shows this

Code: Select all

objdump --disassemble-all kernel

kernel:     file format elf32-i386

Disassembly of section .text:

00010000 <main>:
   10000:       8d 4c 24 04             lea    0x4(%esp),%ecx
   10004:       83 e4 f0                and    $0xfffffff0,%esp
   10007:       ff 71 fc                pushl  0xfffffffc(%ecx)
   1000a:       55                      push   %ebp
   1000b:       89 e5                   mov    %esp,%ebp
   1000d:       51                      push   %ecx
   1000e:       83 ec 10                sub    $0x10,%esp
   10011:       c7 45 f4 00 80 0b 00    movl   $0xb8000,0xfffffff4(%ebp)
   10018:       c7 45 f8 2c 00 01 00    movl   $0x1002c,0xfffffff8(%ebp)
   1001f:       8b 45 f8                mov    0xfffffff8(%ebp),%eax
   10022:       0f b6 10                movzbl (%eax),%edx
   10025:       8b 45 f4                mov    0xfffffff4(%ebp),%eax
   10028:       88 10                   mov    %dl,(%eax)
   1002a:       eb fe                   jmp    1002a <main+0x2a>
Disassembly of section .rodata:

0001002c <.rodata>:
   1002c:       48                      dec    %eax
   1002d:       45                      inc    %ebp
        ...
Disassembly of section .comment:

00000000 <.comment>:
   0:   00 47 43                add    %al,0x43(%edi)
   3:   43                      inc    %ebx
   4:   3a 20                   cmp    (%eax),%ah
   6:   28 47 4e                sub    %al,0x4e(%edi)
   9:   55                      push   %ebp
   a:   29 20                   sub    %esp,(%eax)
   c:   34 2e                   xor    $0x2e,%al
   e:   31 2e                   xor    %ebp,(%esi)
  10:   32 20                   xor    (%eax),%ah
  12:   32 30                   xor    (%eax),%dh
  14:   30 37                   xor    %dh,(%edi)
  16:   30 35 30 32 20 28       xor    %dh,0x28203230
  1c:   52                      push   %edx
  1d:   65 64 20 48 61          and    %cl,%fs:%gs:0x61(%eax)
  22:   74 20                   je     44 <main-0xffbc>
  24:   34 2e                   xor    $0x2e,%al
  26:   31 2e                   xor    %ebp,(%esi)
  28:   32 2d 31 32 29 00       xor    0x293231,%ch
I know everybody out there has done it before and hence fault lies with me, so please help me.

One more think I tried unmounting and then remounting and even tried sync and manual mounting like this
mount /dev/sda4 /mnt/CodeName/ -o sync
But still the changes are not reflected by qemu! #-o
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Problem with char array in gcc n fc7

Post by JohnnyTheDon »

You need to use a disk image. And what do you have in grub's menu.lst?
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Problem with char array in gcc n fc7

Post by Raven »

Well, 3 entries :

title Fedora Core 7 - base (2.6.21-1.3194.fc7)
root (hd0,1)
kernel /vmlinuz-2.6.21-1.3194.fc7 ro root=LABEL=/ rhgb quiet
initrd /initrd-2.6.21-1.3194.fc7.img
title Windows
rootnoverify (hd0,0)
chainloader +1
title MyOS
rootnoverify (hd0,3)
chainloader +1

Further, I checked some tutorial to compile flat binaries. It seems something is wrong with my Second Stage Bootloader.
User avatar
Steve the Pirate
Member
Member
Posts: 152
Joined: Fri Dec 15, 2006 7:01 am
Location: Brisbane, Australia
Contact:

Re: Problem with char array in gcc n fc7

Post by Steve the Pirate »

So your OS does boot and run properly if you restart? If so, I'd say that going with a disk image instead of a real disk might be the best way, like Johnny said.
My Site | My Blog
Symmetry - My operating system.
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Problem with char array in gcc n fc7

Post by Raven »

Thanks a lot everybody but due to some unavoidable reasons i cannot switch to Disk Images, I sorted the problem by keeping buffers always full to virtually stop cache of my small kernel.
BUT the real problem is still there............what is wrong with my Kernel or linker script/
Please help
Post Reply