shithub: scc

Download patch

ref: 4fc7dfcc3f4ca59064142c3c830b2720914b5e8c
parent: 068af381fcb0e5b312fbc0c6da9ae1115f91b7c9
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Nov 27 14:27:35 EST 2017

[objdump] First version of objdump

This version only prints the header of the file and the code
is awful.

diff: cannot open b/objdump//null: file does not exist: 'b/objdump//null'
--- /dev/null
+++ b/objdump/Makefile
@@ -1,0 +1,23 @@
+.POSIX:
+
+LIBDIR    = ../lib/scc
+include ../config.mk
+include $(LIBDIR)/libdep.mk
+
+OBJ       = main.o 
+
+all: objdump
+
+objdump: $(OBJ) $(LIBDIR)/libscc.a
+	$(CC) $(SCC_LDFLAGS) $(OBJ) -lscc -o $@
+
+main.o: ../inc/scc.h ../inc/myro.h
+
+$(LIBDIR)/libscc.a: $(LIB-OBJ)
+	cd $(LIBDIR) && $(MAKE)
+
+dep:
+clean:
+	rm -f objdump *.o
+
+distclean: clean
--- /dev/null
+++ b/objdump/main.c
@@ -1,0 +1,101 @@
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../inc/scc.h"
+#include "../inc/myro.h"
+
+char *strings;
+size_t strsiz;
+
+static char *
+getstring(unsigned long off)
+{
+	size_t n;
+
+	
+	if (off < SIZE_MAX) {
+		for (n = off; n < strsiz && strings[n]; ++n)
+			;
+		if (n != strsiz)
+			return &strings[off];
+	}
+	fprintf(stderr, "objdump: wrong string offset %lu\n", off);
+	return "";
+}
+
+static void
+printhdr(struct myrohdr *hdr)
+{
+	printf("header:\n"
+	       "\tmagic: %02x%02x%02x%02x \"%4.4s\"\n"
+	       "\tformat: %lu (\"%s\")\n"
+	       "\tentry: %llu\n"
+	       "\tstring table size: %llu\n"
+	       "\tsection table size: %llu\n"
+	       "\tsymbol table size: %llu\n"
+	       "\trelocation table size: %llu\n",
+	       hdr->magic[0], hdr->magic[1],
+	       hdr->magic[2], hdr->magic[3],
+	       hdr->magic,
+	       hdr->format, getstring(hdr->format),
+	       hdr->entry,
+	       hdr->strsize,
+	       hdr->secsize,
+	       hdr->symsize,
+	       hdr->relsize);
+}
+
+int
+main(int argc, char *argv[])
+{
+	FILE *fp;
+	struct myrohdr hdr;
+
+	while (*++argv) {
+		free(strings);
+		strings = NULL;
+
+		puts(*argv);
+
+		if ((fp = fopen(*argv, "rb")) == NULL)
+			goto wrong_file;
+		if (rdmyrohdr(fp, &hdr) < 0)
+			goto wrong_file;
+		if (hdr.strsize > SIZE_MAX)
+			goto overflow;
+		strsiz = hdr.strsize;
+
+		if (strsiz > 0) {
+			strings = xmalloc(strsiz);
+			fread(strings, strsiz, 1, fp);
+			if (feof(fp))
+				goto wrong_file;
+		}
+
+		printhdr(&hdr);
+
+
+		goto close_file;
+		
+wrong_file:
+		fprintf(stderr,
+			"objdump: %s: %s\n",
+			*argv, strerror(errno));
+		goto close_file;
+
+overflow:
+		fprintf(stderr,
+			"objdump: %s: overflow in header\n",
+			*argv, strerror(errno));
+
+close_file:
+		if (fp)
+			fclose(fp);
+	}
+
+	return 0;
+}