tkchia / newlib-ia16

Fork of IA-16 port of Newlib -- added small and medium model support
GNU General Public License v2.0
13 stars 4 forks source link

Seeking fails in medium model #14

Closed cspiegel closed 3 years ago

cspiegel commented 3 years ago

I'm using version 20201106 of build-ia16; I'm not sure if this is a newlib problem or otherwise, but I had to pick somewhere for the issue.

Consider the following:

#include <errno.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    FILE *fp = fopen("somefile", "rb");

    if (fp == NULL) {
        fprintf(stderr, "can't open\n");
        return 1;
    }

    if (fseek(fp, 0, SEEK_SET) == -1) {
        fprintf(stderr, "error: %s (%d)\n", strerror(errno), errno);
    }

    return 0;
}

When building with ia16-elf-gcc -mcmodel=small t.c this works as intended, producing no output. However, when I use -mcmodel=medium, I get the following:

error: Bad file number (6)

Reading the file still works fine in the medium model (e.g. getc(fp) returns the first byte of the file). It's just that seeking doesn't work. I've tried lseek() and fseeko() as well, so it appears to be an issue with the underlying seeking functonality, not just the standard C fseek().

tkchia commented 3 years ago

Hello @cspiegel,

My apologies. Yes, this is a bug in newlib-ia16 --- the function was getting its parameters from the wrong places in the stack. I will work on fixing this soon.

Thank you!