scc

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

commit 92ff27f9ece894a9fe6f426a97120a24d536457f
parent 40525604fca878404a8e7bec907162a4519515db
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 28 Nov 2017 08:48:36 +0000

[objdump] Create dump() function and use a.out as default file name

Diffstat:
Mobjdump/Makefile | 2+-
Mobjdump/main.c | 104+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
2 files changed, 63 insertions(+), 43 deletions(-)

diff --git a/objdump/Makefile b/objdump/Makefile @@ -11,7 +11,7 @@ all: objdump objdump: $(OBJ) $(LIBDIR)/libscc.a $(CC) $(SCC_LDFLAGS) $(OBJ) -lscc -o $@ -main.o: ../inc/scc.h ../inc/myro.h +main.o: ../inc/scc.h ../inc/myro.h ../inc/arg.h $(LIBDIR)/libscc.a: $(LIB-OBJ) cd $(LIBDIR) && $(MAKE) diff --git a/objdump/main.c b/objdump/main.c @@ -6,11 +6,13 @@ #include <stdlib.h> #include <string.h> +#include "../inc/arg.h" #include "../inc/scc.h" #include "../inc/myro.h" -char *strings; -size_t strsiz; +char *argv0; +static char *strings; +static size_t strsiz; static char * getstring(unsigned long off) @@ -176,57 +178,75 @@ exit_loop: return (ferror(fp)) ? -1 : 0; } -int -main(int argc, char *argv[]) +void +dump(char *fname) { FILE *fp; struct myrohdr hdr; - while (*++argv) { - free(strings); - strings = NULL; - - puts(*argv); + puts(fname); + if ((fp = fopen(fname, "rb")) == NULL) + goto wrong_file; + if (rdmyrohdr(fp, &hdr) < 0) + goto wrong_file; + if (hdr.strsize > SIZE_MAX) { + fprintf(stderr, + "objdump: %s: overflow in header\n", + fname, strerror(errno)); + goto close_file; + } + strsiz = hdr.strsize; - if ((fp = fopen(*argv, "rb")) == NULL) + if (strsiz > 0) { + strings = xmalloc(strsiz); + fread(strings, strsiz, 1, fp); + if (feof(fp)) goto wrong_file; - if (rdmyrohdr(fp, &hdr) < 0) - goto wrong_file; - if (hdr.strsize > SIZE_MAX) { - fprintf(stderr, - "objdump: %s: overflow in header\n", - *argv, strerror(errno)); - goto close_file; - } - strsiz = hdr.strsize; + } - if (strsiz > 0) { - strings = xmalloc(strsiz); - fread(strings, strsiz, 1, fp); - if (feof(fp)) - goto wrong_file; - } + printhdr(&hdr); + printstrings(&hdr); + if (printsections(&hdr, fp) < 0) + goto wrong_file; + if (printsymbols(&hdr, fp) < 0) + goto wrong_file; + if (printrelocs(&hdr, fp) < 0) + goto wrong_file; + if (printdata(&hdr, fp) < 0) + goto wrong_file; - printhdr(&hdr); - printstrings(&hdr); - if (printsections(&hdr, fp) < 0) - goto wrong_file; - if (printsymbols(&hdr, fp) < 0) - goto wrong_file; - if (printrelocs(&hdr, fp) < 0) - goto wrong_file; - if (printdata(&hdr, fp) < 0) - goto wrong_file; + goto close_file; - goto close_file; - wrong_file: - fprintf(stderr, - "objdump: %s: %s\n", - *argv, strerror(errno)); + fprintf(stderr, + "objdump: %s: %s\n", + fname, strerror(errno)); close_file: - if (fp) - fclose(fp); + if (fp) + fclose(fp); +} + +void +usage(void) +{ + fputs("usage: objdump file ...\n", stderr); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + ARGBEGIN { + default: + usage(); + } ARGEND + + if (argc == 1) + dump("a.out"); + else while (*++argv) { + free(strings); + strings = NULL; + dump(*argv); } return 0;