COFF format, ELF format of an executable in linux

COFF format, ELF format of an executable in linux

Post by hunso » Tue, 13 Apr 1999 04:00:00



In order to analyze the coff format of an executable in linux platform,
I wrote following code.  The executable of the following program does
not behaves what I expected, and I realized that the main cause of
this is the fact that the output format is the ELF format, instead
of COFF format.  So I'd like to ask you to inform me the 'good option
of gcc' using which I can order the gcc compiler to produce an COFF
format not an ELF format.  Remember that I am working at a linux system.

================
#include <stdio.h>
#include <stdlib.h>
#include <a.out.h>
#include <unistd.h>
#include <fcntl.h>

struct exec     execb;

void
dump_00(void *given, int len)
{
        char    buff[128], *p;
        unsigned char   *data;
        int     i, cnt;

        data = (unsigned char *) given;
        for (i=0, cnt = 0,  p = buff; i < len;) {
                p += sprintf(p, "%02x ", data[i]);
                if ((++i % 16) == 0) {
                        p[0] = 0;
                        printf("%p : %s\n", data + cnt * 16, buff);
                        p = buff;
                        cnt++;
                        continue;
                }
                if ((i % 4) == 0)
                        p += sprintf(p, " ");
        }
        return;

Quote:}

int
get_header(int fd)
{
        if (sizeof(struct exec) != read(fd, &execb, sizeof(struct
exec)))
                return -1;
        dump_00(&execb, sizeof(struct exec));
        printf("%08x\n", N_MAGIC(execb));
        if (N_BADMAG(execb))
                return -2;
        return 0;

Quote:}

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

        if (argc < 2) {
                fprintf(stderr, "You should provide the name of an
executable");
                return 0;
        }

        fd = open(argv[1], 0);
        if (fd < 0)
                return -1;
        ret = get_header(fd);
        if (ret)
                printf("ERR: ret = %d\n", ret);

        return 0;

Quote:}

 
 
 

COFF format, ELF format of an executable in linux

Post by Ken Pizzi » Wed, 14 Apr 1999 04:00:00



>In order to analyze the coff format of an executable in linux platform,
>I wrote following code.  The executable of the following program does
>not behaves what I expected, and I realized that the main cause of
>this is the fact that the output format is the ELF format, instead
>of COFF format.  So I'd like to ask you to inform me the 'good option
>of gcc' using which I can order the gcc compiler to produce an COFF
>format not an ELF format.  Remember that I am working at a linux system.

gcc itself doesn't know anything about binary file formats --- it
defers this to the as and ld programs of the binutils package.
First you need to check to see if the binutils installed on your
system have COFF support built-in (it is unusual on a Linux system):
  ld --help | grep supported

If you don't find COFF listed, then you will need to get the
binutils source and do a custom build.  (Actually, I don't know
for sure whether the binutils package _does_ have a way to
support COFF on Linux systems.)

Then look into the --format and --oformat options of ld.
AFAIK, "as" does not have a mechanism for choosing alternate
binary formats for its output, so you will either need to
(if you can) build a custom version of "as" which producess
COFF output, or use ld to make a conversion for you.

                --Ken Pizzini