Illegal Instruction while using zlib in uCLinux environment

358 Views Asked by At

This may be completely irrelevant or very simple question.

I'm trying to write a very simple application that uses the libz library functions to do compression. It should run in uCLinux environment on a NIOS CPU. My system runs busybox and busybox provides all the regular gzip, gunzip functionalities. But they are built into busybox and as far as I can tell not using the dynamic libz library. Here is the code:

{
    printf("Hello World\n");
    printf("Zlib: %s\n", zlibVersion());
    gzFile file = gzopen ("/tmp/s2.log.gz", "wb");
    if (! file) {
        fprintf (stderr, "gzopen failed: %s.\n", strerror (errno));
        exit (-1);
    }

    printf("%d\n", __LINE__);
    {
        unsigned char buffer[LENGTH] = "Hello world";
        int bytes_read = gzwrite (file, buffer, LENGTH - 1);
        if (bytes_read < LENGTH - 1) {
            int err;
            const char * error_string;
            error_string = gzerror (file, & err);
            if (err) {
                fprintf (stderr, "Error: %s.\n", error_string);
                exit (-1);
            }
        }
    }
    printf("%d\n", __LINE__);
    printf("%d\n", gzclose (file));
    return 0;
}

It is partially lifted from zlib example. The problem is that on the last line - gzclose - when the compressed buffer actually flushed to the file, I get illegal instruction exception. Anyone has any idea why it may be happening?

Here is the backtrace from GDB of the failure:

#0  0x2aad9efc in order.3344 () from ./uClinux/uClinux-dist/staging/usr/lib/libz.so.1
#1  0x2aad21c8 in _tr_flush_block () from ./uClinux/uClinux-dist/staging/usr/lib/libz.so.1
#2  0x2aace694 in deflate_slow () from ./uClinux/uClinux-dist/staging/usr/lib/libz.so.1
#3  0x2aacec9c in deflate () from ./uClinux/uClinux-dist/staging/usr/lib/libz.so.1
#4  0x2aacb5d0 in gzclose () from ./uClinux/uClinux-dist/staging/usr/lib/libz.so.1
#5  0x0000193c in main () at main.c:49

Update: I've linked the libz.a statically, but the same error occurred.

1

There are 1 best solutions below

0
On

Found the cause of the problem.

The original zlib uses -O3 optimization flag. It is possible that either my NIOSII CPU or the compiler is not up to the task to create properly running code. Once I've relaxed the optimization flag the error went away.