How to debug

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
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

How to debug

Post 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.
Last edited by Jezze on Sat Jan 28, 2012 10:07 am, edited 1 time in total.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How to debug

Post 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.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: How to debug

Post 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);

    }

}
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
Post Reply