URI:
       libmach: Unify coff32getidx and elfgetidx - scc - simple c99 compiler
  HTML git clone git://git.simple-cc.org/scc
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit a4c40d95dec037a875469cddc7987c8d3783c86a
   DIR parent 260689938154916eebc6d7002c81f001a8771ab3
  HTML Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Fri,  8 May 2026 19:44:40 +0200
       
       libmach: Unify coff32getidx and elfgetidx
       
       Diffstat:
         M src/libmach/Makefile                |       1 +
         M src/libmach/coff32/Makefile         |       2 --
         D src/libmach/coff32/coff32getidx.c   |      13 -------------
         D src/libmach/coff32/coff32xgetidx.c  |      73 -------------------------------
         A src/libmach/getidx32.c              |      72 +++++++++++++++++++++++++++++++
         M src/libmach/getindex.c              |      17 +++++++++++++----
         M src/libmach/libmach.h               |       2 +-
       
       7 files changed, 87 insertions(+), 93 deletions(-)
       ---
   DIR diff --git a/src/libmach/Makefile b/src/libmach/Makefile
       @@ -17,6 +17,7 @@ OBJS =\
                delobj.o\
                findsec.o\
                findseg.o\
       +        getidx32.o\
                getindex.o\
                getsec.o\
                getsym.o\
   DIR diff --git a/src/libmach/coff32/Makefile b/src/libmach/coff32/Makefile
       @@ -6,7 +6,6 @@ include $(PROJECTDIR)/scripts/rules.mk
        OBJS =\
                coff32archs.o\
                coff32del.o\
       -        coff32getidx.o\
                coff32getsec.o\
                coff32getsym.o\
                coff32loadmap.o\
       @@ -20,7 +19,6 @@ OBJS =\
                coff32strip.o\
                coff32type.o\
                coff32write.o\
       -        coff32xgetidx.o\
        
        COFFHDRS =\
                $(INCDIR)/bits/scc/coff32/aouthdr.h\
   DIR diff --git a/src/libmach/coff32/coff32getidx.c b/src/libmach/coff32/coff32getidx.c
       @@ -1,13 +0,0 @@
       -#include <stdio.h>
       -
       -#include <scc/mach.h>
       -#include <scc/coff32.h>
       -
       -#include "../libmach.h"
       -#include "fun.h"
       -
       -int
       -coff32getidx(long *nsyms, char ***namep, long **offsp, FILE *fp)
       -{
       -        return coff32xgetidx(BIG_ENDIAN, nsyms, namep, offsp, fp);
       -}
   DIR diff --git a/src/libmach/coff32/coff32xgetidx.c b/src/libmach/coff32/coff32xgetidx.c
       @@ -1,73 +0,0 @@
       -#include <stdio.h>
       -#include <stdlib.h>
       -#include <string.h>
       -
       -#include <scc/mach.h>
       -#include <scc/cstd.h>
       -#include <scc/coff32.h>
       -
       -#include "../libmach.h"
       -#include "fun.h"
       -
       -int
       -coff32xgetidx(int order, long *nsyms, char ***namep, long **offsp, FILE *fp)
       -{
       -        long i, n;
       -        long *offs;
       -        char **names;
       -        unsigned char buf[EXTIDENTSIZ+1];
       -
       -        if (fread(buf, 4, 1, fp) != 1)
       -                return -1;
       -        unpack(order, buf, "l", &n);
       -
       -        if (n <= 0)
       -                return -1;
       -
       -        if ((names = calloc(sizeof(char *), n)) == NULL)
       -                return -1;
       -
       -        if ((offs = calloc(sizeof(long), n)) == NULL)
       -                goto err1;
       -
       -        for (i = 0; i < n; i++) {
       -                fread(buf, 4, 1, fp);
       -                unpack(order, buf, "l", offs[i]);
       -        }
       -
       -        for (i = 0; i < n; i++) {
       -                int j, c;
       -                char *s;
       -
       -                for (j = 0; j < EXTIDENTSIZ; j++) {
       -                        if ((c = getc(fp)) == EOF || c == '\0')
       -                                break;
       -                        buf[j] = c;
       -                }
       -                if (c != '\0')
       -                        goto err2;
       -                buf[j] = '\0';
       -
       -                if ((s = malloc(j)) == NULL)
       -                        goto err2;
       -                memcpy(s, buf, j);
       -                names[i]= s;
       -        }
       -
       -        if (ferror(fp))
       -                goto err2;
       -
       -        *offsp = offs;
       -        *namep = names;
       -        *nsyms = n;
       -
       -        return 0;
       -
       -err2:
       -        free(offs);
       -err1:
       -        for (i = 0; i < n; i++)
       -                free(names[i]);
       -        free(*names);
       -        return -1;
       -}
   DIR diff --git a/src/libmach/getidx32.c b/src/libmach/getidx32.c
       @@ -0,0 +1,72 @@
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <string.h>
       +
       +#include <scc/mach.h>
       +#include <scc/cstd.h>
       +#include <scc/coff32.h>
       +
       +#include "libmach.h"
       +
       +int
       +getidx32(int order, long *nsyms, char ***namep, long **offsp, FILE *fp)
       +{
       +        long i, n;
       +        long *offs;
       +        char **names;
       +        unsigned char buf[EXTIDENTSIZ+1];
       +
       +        if (fread(buf, 4, 1, fp) != 1)
       +                return -1;
       +        unpack(order, buf, "l", &n);
       +
       +        if (n <= 0)
       +                return -1;
       +
       +        if ((names = calloc(sizeof(char *), n)) == NULL)
       +                return -1;
       +
       +        if ((offs = calloc(sizeof(long), n)) == NULL)
       +                goto err1;
       +
       +        for (i = 0; i < n; i++) {
       +                fread(buf, 4, 1, fp);
       +                unpack(order, buf, "l", offs[i]);
       +        }
       +
       +        for (i = 0; i < n; i++) {
       +                int j, c;
       +                char *s;
       +
       +                for (j = 0; j < EXTIDENTSIZ; j++) {
       +                        if ((c = getc(fp)) == EOF || c == '\0')
       +                                break;
       +                        buf[j] = c;
       +                }
       +                if (c != '\0')
       +                        goto err2;
       +                buf[j] = '\0';
       +
       +                if ((s = malloc(j)) == NULL)
       +                        goto err2;
       +                memcpy(s, buf, j);
       +                names[i]= s;
       +        }
       +
       +        if (ferror(fp))
       +                goto err2;
       +
       +        *offsp = offs;
       +        *namep = names;
       +        *nsyms = n;
       +
       +        return 0;
       +
       +err2:
       +        free(offs);
       +err1:
       +        for (i = 0; i < n; i++)
       +                free(names[i]);
       +        free(*names);
       +        return -1;
       +}
   DIR diff --git a/src/libmach/getindex.c b/src/libmach/getindex.c
       @@ -8,9 +8,13 @@
        #include "elf/fun.h"
        #include "coff32/fun.h"
        
       -static int (*ops[NFORMATS])(long *, char ***, long **, FILE *) = {
       -        [COFF32] = coff32getidx,
       -};
       +static int (*ops[NFORMATS])(int, long *, char ***, long **, FILE *);
       +
       +static int
       +getidx(int type, long *nsyms, char ***namep, long **offsp, FILE *fp)
       +{
       +        return getidx32(BIG_ENDIAN, nsyms, namep, offsp, fp);
       +}
        
        int
        getindex(int type, long *nsyms, char ***names, long **offs, FILE *fp)
       @@ -23,6 +27,11 @@ getindex(int type, long *nsyms, char ***names, long **offs, FILE *fp)
                        return -1;
                }
        
       -        return (*ops[fmt])(nsyms, names, offs, fp);
       +        return (*ops[fmt])(type, nsyms, names, offs, fp);
        }
        
       +static int (*ops[NFORMATS])(int, long *, char ***, long **, FILE *) = {
       +        [COFF32] = getidx,
       +        [ELF] = getidx,
       +};
       +
   DIR diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h
       @@ -1,7 +1,7 @@
        #ifdef stdin
        int copysec(Mapsec *, FILE *);
        int setidx32(int, long, char *[], long [], FILE *);
       -int setidx64(int, long, char *[], long [], FILE *);
       +int getidx32(int, long *, char ***, long **, FILE *);
        #endif
        
        /* common functions */