Bad string pointer after link

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
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Bad string pointer after link

Post by gedd »

Hello

I'm writing a os (not very original) and i have a boot sector wich work fine,
a kernel loader wich work fine, a kernel wich work fine but ... it dosn't see my string.

here is my kernel :

Code: Select all

void ADNos_main(){
	char *video = ( char *)0xB8000;
	const char *message = "ADN.os first boot ! :)";
	const long size = 80*25;
	long loop;

	//clear the screen
	for (loop=0; loop<size; loop++) {
		*video++ = 0;
		*video++ = 0x1;
	}

	// test first character 
	video = (char *)0xB8000;
	int i = 0;
	char c = message[0];
	if(c=='A')
	{
		*(video+160) = 'O';
		*(video+161) = 0xf;	
		*(video+162) = 'K';
		*(video+163) = 0xf;
	}else
	{
		*(video+160) = 'K';
		*(video+161) = 0xf;	
		*(video+162) = 'O';
		*(video+163) = 0xf;
	}

	// dysplay message
	while(c!='\0')
	{
		*video++ = c;
		*video++ = 0xf;	
		c = message[i++];
	}
	while(1);
}
her is my build command:

Code: Select all

gcc -c  ./kernel/kernel.c -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -o ./bin/kernel.o
ld -i -e ADNos_main -Ttext 0x100000 -o ./bin/kernel.tmp ./bin/kernel.o -M ./bin/kernel.tmp  > kernel.map
objcopy  -R .note -R .comment -R .indent -R .stab -R .stabstr-S -O binary ./bin/kernel.tmp ./bin/kernel.bin
The screen is cleared
The test for the first character is KO
and the message is never displayed

The command "ndisasm ./bin/kernel.bin -u"
give :

Code: Select all

00000000  53                push ebx
00000001  BA00800B00        mov edx,0xb8000
00000006  BB00000000        mov ebx,0x0
0000000B  B8CF070000        mov eax,0x7cf
00000010  C60200            mov byte [edx],0x0
00000013  42                inc edx
00000014  C60201            mov byte [edx],0x1
00000017  42                inc edx
00000018  48                dec eax
00000019  79F5              jns 0x10
0000001B  BA00800B00        mov edx,0xb8000
00000020  B900000000        mov ecx,0x0
00000025  0FB603            movzx eax,byte [ebx]
00000028  3C41              cmp al,0x41
0000002A  751E              jnz 0x4a
0000002C  C605A0800B004F    mov byte [0xb80a0],0x4f
00000033  C605A1800B000F    mov byte [0xb80a1],0xf
0000003A  C605A2800B004B    mov byte [0xb80a2],0x4b
00000041  C605A3800B000F    mov byte [0xb80a3],0xf
00000048  EB1C              jmp short 0x66
0000004A  C682A00000004B    mov byte [edx+0xa0],0x4b
00000051  C682A10000000F    mov byte [edx+0xa1],0xf
00000058  C682A20000004F    mov byte [edx+0xa2],0x4f
0000005F  C682A30000000F    mov byte [edx+0xa3],0xf
00000066  84C0              test al,al
00000068  7412              jz 0x7c
0000006A  8802              mov [edx],al
0000006C  42                inc edx
0000006D  C6020F            mov byte [edx],0xf
00000070  42                inc edx
00000071  89C8              mov eax,ecx
00000073  41                inc ecx
00000074  0FB60418          movzx eax,byte [eax+ebx]
00000078  84C0              test al,al
0000007A  75EE              jnz 0x6a
0000007C  EBFE              jmp short 0x7c
0000007E  90                nop
0000007F  90                nop
00000080  41                inc ecx
00000081  44                inc esp
00000082  4E                dec esi
00000083  2E6F              cs outsd
00000085  7320              jnc 0xa7
00000087  666972737420      imul si,[edx+0x73],word 0x2074
0000008D  626F6F            bound ebp,[edi+0x6f]
00000090  7420              jz 0xb2
00000092  2120              and [eax],esp
00000094  3A29              cmp ch,[ecx]
00000096  0000              add [eax],al
00000098  0000              add [eax],al
0000009A  0000              add [eax],al
0000009C  0000              add [eax],al
0000009E  0000              add [eax],al
It's exactly my kernel code (strange no ?) with my message at 0x80
ebx seems to the pointer to 'message':
00000025 0FB603 movzx eax,byte [ebx] --> char c = message[0];
and
00000074 0FB60418 movzx eax,byte [eax+ebx] --> c = message[i++];

I dont know why the message pointer seems to be bad
Someone could help me ?
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Post by gedd »

I have forgotten this detail
My development platform is Windows + cygwin with gcc & nasm
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:

Post by Combuster »

from the list of frequently asked questions:

have you got a .rodata in your linker script?
"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 ]
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Post by gedd »

In fact i have no linker script i use LD and OBJCOPY

Code: Select all

gcc -c  ./kernel/kernel.c -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -o ./bin/kernel.o 
ld -i -e ADNos_main -Ttext 0x100000 -o ./bin/kernel.tmp ./bin/kernel.o -M ./bin/kernel.tmp  > kernel.map 
objcopy  -R .note -R .comment -R .indent -R .stab -R .stabstr-S -O binary ./bin/kernel.tmp ./bin/kernel.bin 
And if you watch the disasm code you will see at 0x80 the string
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Now's a great time to start using a linker script, it makes life much easier later on ;).

All you need is to change -Ttext 0x100000 to -T <linker script> and you're set.
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Post by gedd »

Ok it's done but i have the save result:

The kernel code is the same

the new build command:

Code: Select all

gcc -c  ./kernel/kernel.c -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -o ./bin/kernel.o
ld -i -T ./kernel/kernel.lds -o ./bin/kernel.tmp ./bin/kernel.o -M ./bin/kernel.tmp  > kernel.map
objcopy -O binary ./bin/kernel.tmp ./bin/kernel.bin
here is the ld script:

Code: Select all

OUTPUT_FORMAT("pe-i386") 
ENTRY("ADNos_main")
SECTIONS 
{ 
    . = 0x100000;
    .text :
    { 
        code = .; 
        *(.text) 
        text_end = .;
    }
    .rodata : 
    { 
        rodata = text_end; 
        *(.rodata)
        rodata_end  = .;      
    }    
    .rdata : 
    { 
        rdata = rodata_end; 
        *(.rodata)
        rdata_end  = .;      
    } 
    .data : 
    { 
        data = rdata_end; 
        *(.data) 
        data_end = .;
    } 
    .bss :  
    { 
        bss = data_end; 
        *(.bss) 
        bss__end = .;
    } 
    end = .; 
            /DISCARD/ :{
                *(.note*)
                *(.indent)
                *(.comment)
                *(.stab)
                *(.stabstr)
        }
} 
The disasm code is also the same

any idea ?
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Post by gedd »

Problem solved

I have compiled a Cross compiler with cygwin [Binutils 2.18 & gcc 3.4.4]
and it's work !
Post Reply