scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit eeaadb0d2ecae79301362f94813ece460eb9c06b
parent 81111ecaf950ecfa89c3869ce65bcc0991af712a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 27 Nov 2017 19:27:35 +0100

[objdump] First version of objdump

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

Diffstat:
objdump/Makefile | 23+++++++++++++++++++++++
objdump/main.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 124 insertions(+), 0 deletions(-)

diff --git a/objdump/Makefile b/objdump/Makefile @@ -0,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 diff --git a/objdump/main.c b/objdump/main.c @@ -0,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; +}