Page 1 of 1

How to debug

Posted: Sat Jan 28, 2012 10:00 am
by Jezze
Ok, this is kinda embarrasing but I just don't know.

I'm trying to run programs compiled by my own toolchain including newlib and ran into a weird issue.

This is my test program:

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{

    write(STDOUT_FILENO, "test1\n", 6);

//    FILE *out = fopen("stdout", "w");

    fputs("test2\n", stdout);

//    FILE *out = fopen("stdout", "w");

    return 0;

}
If I do not uncomment any of the comments my program will work and print test1 and test2 but if I either uncomment the first or the second (obviously not both) it will only print test1 before page fault giving me a very low memory address, either 0x17e4 or 0x88. Especially notice that uncommenting the second line would still only print test1.

Any hints on what could be wrong? I know I'm not zeroing bss on program loading, which could be a start but I don't see why that would matter in this case.

Re: How to debug

Posted: Sat Jan 28, 2012 10:06 am
by bluemoon
Jezze wrote:I know I'm not zeroing bss on program loading, which could be a start but I don't see why that would matter in this case.
It's because newlib require you to zero bss. By not doing so you probably left some internal data structure to be random.

Re: How to debug

Posted: Sat Jan 28, 2012 10:39 am
by Jezze
Ok I think I zeroed the bss now, but the problem still exist.

I added this code which probably should be enough:

Code: Select all

void elf_zero_bss(void *address)
{

    struct elf_header *header = get_header(address);

    if (!header)
        return;

    struct elf_section_header *sheader = address + header->shoffset;

    unsigned int i;

    for (i = 0; i < header->shnum; i++)
    {

        if (sheader[i].type == 8)
            memory_clear(address + sheader[i].offset, sheader[i].size);

    }

}