shithub: scc

Download patch

ref: 1ebd02c28ba4d5969e349942e2ec7334ca13d195
parent: 99bb172e34500445d1969971eb77bff53ac9cb6e
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Mar 14 04:08:18 EDT 2018

[ld/coff32] Add support for coff32 probe()

This function checks if a file is a coff32 object file.

--- a/ld/Makefile
+++ b/ld/Makefile
@@ -26,6 +26,10 @@
 distclean: clean
 
 #deps
+coff32.c: ../inc/coff32/filehdr.h
+coff32.c: ../inc/coff32/scnhdr.h
+coff32.c: ../inc/coff32/syms.h
+coff32.c: ../inc/scc.h
 coff32.c: ld.h
 formats.c: ld.h
 main.c: ../inc/arg.h
--- a/ld/coff32.c
+++ b/ld/coff32.c
@@ -1,22 +1,52 @@
 static char sccsid[] = "@(#) ./ld/coff32.c";
 
+#include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
+#include "../inc/coff32/filehdr.h"
+#include "../inc/coff32/scnhdr.h"
+#include "../inc/coff32/syms.h"
+#include "../inc/scc.h"
 #include "ld.h"
 
 static int
-probe(char *fname, char *member, FILE *fp)
+pass1(char *fname, char *member, FILE *fp)
 {
 }
 
 static int
-pass1(char *fname, char *member, FILE *fp)
+pass2(char *fname, char *member, FILE *fp)
 {
 }
 
 static int
-pass2(char *fname, char *member, FILE *fp)
+probe(char *fname, char *member, FILE *fp)
 {
+	int c;
+	int c1, c2;
+	fpos_t pos;
+	unsigned short magic;
+
+	fgetpos(fp, &pos);
+	c1 = getc(fp);
+	c2 = getc(fp);
+	fsetpos(fp, &pos);
+
+	if (ferror(fp))
+		die("nm: %s: %s", fname, strerror(errno));
+
+	if (c1 == EOF || c2 == EOF)
+		return 0;
+	magic = c1 | c2 << 8;
+
+	switch (magic) {
+	case COFF_Z80MAGIC:
+		return 1;
+	default:
+		return 0;
+	}
 }
 
 struct objfile coff32 = {