shithub: scc

Download patch

ref: 7edf451bf7accdac3edb62dbba35e0713072a9f0
parent: 263d99100408f467fbe6a06dad26e11789c9d603
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Feb 10 14:02:43 EST 2019

[libmach] Add namindex()

This function returns the name of the index file for the
object file format.

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -75,6 +75,7 @@
 extern int objwrite(Obj *obj, FILE *fp);
 extern long setindex(int type, long nsyms, Objsymdef *def, FILE *fp);
 extern long getindex(int type, long *nsyms, Objsymdef **def, FILE *fp);
+extern char *namindex(int type);
 
 /* TODO */
 extern int objload(Obj *obj, Obj *to);
--- a/src/cmd/ld/main.c
+++ b/src/cmd/ld/main.c
@@ -275,8 +275,8 @@
 static int
 newmember(FILE *fp, char *name, void *data)
 {
-	int *nmemb = data;
 	int t;
+	int *nmemb = data;
 
 	membname = data;
 
@@ -285,18 +285,17 @@
 		return 0;
 	}
 
-	/* TODO: This name depends of the format */
-	if (*nmemb++ == 0 && !strncmp(name, "/", SARNAM)) {
-		loadlib(fp);
-		return 0;
+	if (*nmemb++ == 0) {
+		if(!strncmp(name, namindex(bintype), SARNAM)) {
+			loadlib(fp);
+			return 0;
+		}
 	}
 
 	if ((t = objtype(fp, NULL)) == -1)
 		return 1;
 
-	if (bintype == -1) {
-		bintype = t;
-	} else if (bintype != t) {
+	if (bintype != t) {
 		error("wrong object file format");
 		return 1;
 	}
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -198,7 +198,7 @@
 merge(FILE *to, struct fprop *prop, FILE *lib, FILE *idx)
 {
 	int c;
-	char mtime[13];
+	char *index, mtime[13];
 	struct ar_hdr first;
 
 	rewind(lib);
@@ -208,8 +208,8 @@
 	if (fread(&first, sizeof(first), 1, lib) != 1)
 		return 0;
 
-	/* TODO: This name depends of the format */
-	if (!strncmp(first.ar_name, "/", SARNAM))
+	index = namindex(artype);
+	if (!strncmp(first.ar_name, index, SARNAM))
 		fseek(lib, atol(first.ar_size), SEEK_CUR);
 
 	fwrite(ARMAG, SARMAG, 1, to);
@@ -217,7 +217,7 @@
         strftime(mtime, sizeof(mtime), "%s", gmtime(&prop->time));
         fprintf(to,
                 "%-16.16s%-12s%-6u%-6u%-8lo%-10ld`\n",
-                "/",      /* TODO: This name depends of the format */
+                index,
                 mtime,
                 prop->uid,
                 prop->gid,
--- a/src/libmach/.gitignore
+++ b/src/libmach/.gitignore
@@ -8,3 +8,4 @@
 write.c
 getidx.c
 setidx.c
+namidx.c
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -16,6 +16,7 @@
        objstrip.o \
        getindex.o \
        setindex.o \
+       namindex.o \
        forsym.o \
        forsect.o \
        formember.o \
@@ -33,6 +34,7 @@
        write.o \
        getidx.o \
        setidx.o \
+       namidx.o \
 
 
 DIRS = coff32
@@ -39,6 +41,7 @@
 
 TBLS = setidx.c \
        getidx.c \
+       namidx.c \
        new.c \
        read.c \
        del.c \
--- a/src/libmach/coff32/Makefile
+++ b/src/libmach/coff32/Makefile
@@ -12,6 +12,7 @@
        coff32getindex.o \
        coff32setidx.o \
        coff32getidx.o \
+       coff32namidx.o \
 
 all: $(OBJS)
 
--- /dev/null
+++ b/src/libmach/coff32/coff32namidx.c
@@ -1,0 +1,9 @@
+#include <stdio.h>
+
+#include <scc/mach.h>
+
+char *
+coff32namidx(void)
+{
+	return "/";
+}
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -36,6 +36,7 @@
 typedef int (*writefun_t)(Obj *obj, FILE *fp);
 typedef long (*setidxfun_t)(int, long, Objsymdef *, FILE *);
 typedef int (*getidxfun_t)(int t, long *n, Objsymdef **def, FILE *fp);
+typedef char *(*namidxfun_t)(void);
 
 /* common functions */
 extern int pack(int order, unsigned char *dst, char *fmt, ...);
@@ -57,3 +58,5 @@
 
 extern int coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp);
 extern int coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp);
+
+extern char *coff32namidx(void);
--- /dev/null
+++ b/src/libmach/namindex.c
@@ -1,0 +1,20 @@
+#include <stdio.h>
+
+#include <scc/mach.h>
+
+#include "libmach.h"
+
+extern namidxfun_t namidxv[];
+
+char *
+namindex(int type)
+{
+	int fmt;
+	namidxfun_t fn;
+
+	fmt = FORMAT(type);
+	if (fmt >= NFORMATS)
+		return NULL;
+	fn = namidxv[fmt];
+	return (*fn)();
+}