Page 1 of 1

C char array always empty

Posted: Tue Jun 30, 2015 11:14 pm
by naper01
I'm coding a 32/64 bit kernel. I'm having a strange issue that when I try to pass a const char* , char * or char [] to a function, the array is always empty.

For example,

I have a kmain that do gdt, idt, isrs, irq, video and timer initialization then when I write :

Code: Select all

test_func("test");
test_func(char * string) code for debugging :

Code: Select all

void test_func(char * string)
{
    if(string[0] == '\0'){
        putch('n');
    }else{
        putch('o');
    }
}
I'm always having 'n' on the screen .

and if I test :

Code: Select all

if (string == NULL){
       putch('n');
 }else{
       putch('o');
       putch(string[0]);
 }
I'm getting o without the first char of string

for linking and compiling , I'm using

Code: Select all

ld -m i386linux -T linker/link.ld  -nostdlib  -o kern.my ../obj/start.o \
                      ../obj/main.o \
                      ../obj/scrn.o \
                       ../obj/gdt.o \
                       ../obj/idt.o \
                      ../obj/isrs.o \
                       ../obj/irq.o \
                     ../obj/timer.o \
                        ../obj/io.o \
                     ../obj/yshell.o \
                        ../obj/kb.o
I'm testing out kern.my using Qemu i386

[Linker script]

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
I used Qemu along with GDB , my info registers :

Code: Select all

eax            0x1  1
ecx            0x0  0
edx            0x3d5    981
ebx            0x9500   38144
esp            0x104fe0 0x104fe0
ebp            0x0  0x0
esi            0x0  0
edi            0x108000 1081344
eip            0x100356 0x100356
eflags         0x6  [ PF ]
cs             0x8  8
ss             0x10 16
ds             0x10 16
es             0x10 16
fs             0x10 16
gs             0x10 16

Re: C char array always empty

Posted: Tue Jun 30, 2015 11:32 pm
by Antti
I am almost sure that your "const char strings" are not included in the binary image. At first please verify this.

Re: C char array always empty

Posted: Wed Jul 01, 2015 3:06 am
by Candy
Try replacing *(.rodata) with *(.rodata*)

Re: C char array always empty

Posted: Wed Jul 01, 2015 9:39 am
by naper01
Thank you *(.rodata*) fixed the issue