libmach: Initialize coff file header in objnew() - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 41f4e8a8414c3db705b3c730f933e8a980110f91
 (DIR) parent cb8145813e5414d66e2416feb0026f3d70ae9abb
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
       Date:   Sun, 10 Mar 2024 16:30:58 +0100
       
       libmach: Initialize coff file header in objnew()
       
       This is needed if we want to write a new object file instead
       of just reading an already created object file.
       
       Diffstat:
         M src/libmach/coff32/coff32.h         |       3 ++-
         M src/libmach/coff32/coff32archs.c    |       4 ++--
         M src/libmach/coff32/coff32new.c      |      15 ++++++++++++++-
         M src/libmach/elf64/elf64.h           |       2 +-
         M src/libmach/elf64/elf64new.c        |       2 +-
         M src/libmach/libmach.h               |       2 +-
         M src/libmach/newobj.c                |       2 +-
       
       7 files changed, 22 insertions(+), 8 deletions(-)
       ---
 (DIR) diff --git a/src/libmach/coff32/coff32.h b/src/libmach/coff32/coff32.h
       @@ -22,9 +22,10 @@ struct arch {
                char *name;
                unsigned char magic[2];
                int type;
       +        int flags;
        };
        
       -extern int coff32new(Obj *);
       +extern int coff32new(Obj *, int);
        extern int coff32read(Obj *, FILE *);
        extern int coff32setidx(long, char **, long *, FILE *);
        extern int coff32getidx(long *, char ***, long **, FILE *);
 (DIR) diff --git a/src/libmach/coff32/coff32archs.c b/src/libmach/coff32/coff32archs.c
       @@ -6,7 +6,7 @@
        #include "coff32.h"
        
        struct arch coff32archs[] = {
       -        "coff32-i386", "\x4c\x01", OBJ(COFF32, ARCH386, LITTLE_ENDIAN),
       -        "coff32-z80", "\x5a\x80", OBJ(COFF32, ARCHZ80, LITTLE_ENDIAN),
       +        {"coff32-i386", "\x4c\x01", OBJ(COFF32, ARCH386, LITTLE_ENDIAN), 0x0105},
       +        {"coff32-z80", "\x5a\x80", OBJ(COFF32, ARCHZ80, LITTLE_ENDIAN), 0x3105},
                NULL,
        };
 (DIR) diff --git a/src/libmach/coff32/coff32new.c b/src/libmach/coff32/coff32new.c
       @@ -2,17 +2,30 @@
        #include <stdlib.h>
        
        #include <scc/mach.h>
       +#include <scc/scc.h>
        
        #include "../libmach.h"
        #include "coff32.h"
        
        int
       -coff32new(Obj *obj)
       +coff32new(Obj *obj, int type)
        {
       +        struct arch *p;
                struct coff32 *coff;
       +        FILHDR *hdr;
       +
       +        for (p = coff32archs; p->name && p->type != type; ++p)
       +                ;
       +        if (!p->name)
       +                return -1;
        
                if ((coff = calloc(1, sizeof(*coff))) == NULL)
                        return -1;
       +
       +        hdr = &coff->hdr;
       +        hdr->f_magic = p->magic[1] << 8 | p->magic[0];
       +        hdr->f_flags = p->flags;
       +
                obj->data = coff;
                obj->index = "/";
                return 0;
 (DIR) diff --git a/src/libmach/elf64/elf64.h b/src/libmach/elf64/elf64.h
       @@ -42,7 +42,7 @@ struct arch {
                int type;
        };
        
       -extern int elf64new(Obj *);
       +extern int elf64new(Obj *, int);
        extern int elf64read(Obj *, FILE *);
        extern int elf64setidx(long, char **, long *, FILE *);
        extern int elf64getidx(long *, char ***, long **, FILE *);
 (DIR) diff --git a/src/libmach/elf64/elf64new.c b/src/libmach/elf64/elf64new.c
       @@ -7,7 +7,7 @@
        #include "elf64.h"
        
        int
       -elf64new(Obj *obj)
       +elf64new(Obj *obj, int type)
        {
                struct elf64 *elf;
        
 (DIR) diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h
       @@ -36,7 +36,7 @@ struct objops {
                int (*type)(char *);
                int (*probe)(unsigned char *, char **);
        
       -        int (*new)(Obj *);
       +        int (*new)(Obj *, int);
                void (*del)(Obj *);
        
                int (*read)(Obj *, FILE *);
 (DIR) diff --git a/src/libmach/newobj.c b/src/libmach/newobj.c
       @@ -25,7 +25,7 @@ newobj(int type)
                obj->type = type;
                obj->ops = objops[fmt];
                obj->next = NULL;
       -        if ((*obj->ops->new)(obj) < 0) {
       +        if ((*obj->ops->new)(obj, type) < 0) {
                        free(obj);
                        return NULL;
                }