scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 40276ee4198f7553c815e0e785f2f1062077e2b8
parent 2a6afc8ceb4f54bc6ddf805a9482f915c15f4dee
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 20 Jan 2025 17:42:41 +0100

objdump: Add flag specific functions

As we are expected to print many different flags, having a dedicated
function will reduce the number of print functions, as they are going
to be very similar.

Diffstat:
Msrc/cmd/scc-objdump/coff32.c | 62++++++++++++++++++++++++++++++--------------------------------
Msrc/cmd/scc-objdump/main.c | 93+++++++++++++++++++++++++++++++++++--------------------------------------------
Msrc/cmd/scc-objdump/objdump.h | 13+++++++++++--
3 files changed, 82 insertions(+), 86 deletions(-)

diff --git a/src/cmd/scc-objdump/coff32.c b/src/cmd/scc-objdump/coff32.c @@ -109,33 +109,42 @@ coff32scns(Obj *obj) } } -unsigned -coff32fhdr(Obj *obj, unsigned long long *start) +void +coff32fhdr(Obj *obj, unsigned long long *start, Flags *f) { - unsigned flags, r; + unsigned flags; struct coff32 *coff = obj->data; FILHDR *hdr = &coff->hdr; AOUTHDR *aout = &coff->aout; - if (pflag) { - printf("FILEHDR:\n" - "\tf_magic: %#x\n" - "\tf_nscns: %u\n" - "\tf_timdat: %ld\n" - "\tf_symptr: %ld\n" - "\tf_nsyms: %ld\n" - "\tf_opthdr: %u\n" - "\tf_flags: 0x%04x\n\n", - hdr->f_magic, - hdr->f_nscns, - hdr->f_timdat, - hdr->f_symptr, - hdr->f_nsyms, - hdr->f_opthdr, - hdr->f_flags); - } + flags = hdr->f_flags; + setflag(f, (flags & F_RELFLG) == 0, HAS_RELOC); + setflag(f, (flags & F_LMNO) == 0, HAS_LINENO); + setflag(f, (flags & F_LSYMS) == 0, HAS_LOCALS); + setflag(f, hdr->f_nsyms > 0, HAS_SYMS); + setflag(f, flags & F_EXEC, EXEC_P); + setflag(f, flags & F_EXEC, D_PAGED); + + if (!pflag) + return; - if (pflag && hdr->f_opthdr > 0) { + printf("FILEHDR:\n" + "\tf_magic: %#x\n" + "\tf_nscns: %u\n" + "\tf_timdat: %ld\n" + "\tf_symptr: %ld\n" + "\tf_nsyms: %ld\n" + "\tf_opthdr: %u\n" + "\tf_flags: 0x%04x\n\n", + hdr->f_magic, + hdr->f_nscns, + hdr->f_timdat, + hdr->f_symptr, + hdr->f_nsyms, + hdr->f_opthdr, + hdr->f_flags); + + if (hdr->f_opthdr > 0) { printf("AOUTHDR:\n" "\tmagic: %x\n" "\tvstamp: %x\n" @@ -155,15 +164,4 @@ coff32fhdr(Obj *obj, unsigned long long *start) aout->data_start); *start = aout->entry; } - - r = 0; - flags = hdr->f_flags; - setflag(&r, (flags & F_RELFLG) == 0, HAS_RELOC); - setflag(&r, (flags & F_LMNO) == 0, HAS_LINENO); - setflag(&r, (flags & F_LSYMS) == 0, HAS_LOCALS); - setflag(&r, hdr->f_nsyms > 0, HAS_SYMS); - setflag(&r, flags & F_EXEC, EXEC_P); - setflag(&r, flags & F_EXEC, D_PAGED); - - return r; } diff --git a/src/cmd/scc-objdump/main.c b/src/cmd/scc-objdump/main.c @@ -37,33 +37,25 @@ error(char *fmt, ...) } void -setflag(unsigned *r, int cond, int flag) +setflag(Flags *f, int cond, int flag) { if (cond) - *r |= 1 << flag; + f->flags |= 1 << flag; } -static void -printfileflags(unsigned flags) +void +printflags(Flags *f) { - int first, i; - static const char *text[] = { - [HAS_RELOC] = "HAS_RELOC", - [EXEC_P] = "EXEC_P", - [HAS_LINENO] = "HAS_LINENO", - [HAS_DEBUG] = "HAS_DEBUG", - [HAS_SYMS] = "HAS_SYMS", - [HAS_LOCALS] = "HAS_LOCALS", - [D_PAGED] = "D_PAGED", - }; + int i, first; + unsigned long flags = f->flags; first = 1; - for (i = 0; i < NR_FILE_FLAGS; i++) { + for (i = 0; i < f->nr; i++) { if (flags & 1) { if (!first) fputs(", ", stdout); first = 0; - fputs(text[i], stdout); + fputs(f->text[i], stdout); } flags >>= 1; } @@ -74,56 +66,39 @@ printfileflags(unsigned flags) static void dumpfhdr(Obj *obj, char *fmt) { - unsigned f; unsigned long long start = 0; + static Flags f = { + .nr = NR_FILE_FLAGS, + .text = { + [HAS_RELOC] = "HAS_RELOC", + [EXEC_P] = "EXEC_P", + [HAS_LINENO] = "HAS_LINENO", + [HAS_DEBUG] = "HAS_DEBUG", + [HAS_SYMS] = "HAS_SYMS", + [HAS_LOCALS] = "HAS_LOCALS", + [D_PAGED] = "D_PAGED", + } + }; printf("architecture: %s, flags: 0x%08x\n", strchr(fmt, '-') + 1, obj->type); + f.flags = 0; + switch (FORMAT(obj->type)) { case COFF32: - f = coff32fhdr(obj, &start); + coff32fhdr(obj, &start, &f); break; default: error("unknown fhdr binary format"); return; } - printfileflags(f); + printflags(&f); printf("start address 0x%08llx\n", start); } -static void -printsecflags(unsigned flags) -{ - int first, i; - static const char *text[] = { - [SEC_HAS_CONTENTS] = "CONTENTS", - [SEC_ALLOC] = "ALLOC", - [SEC_LOAD] = "LOAD", - [SEC_RELOC] = "RELOC", - [SEC_READONLY] = "READONLY", - [SEC_CODE] = "CODE", - [SEC_DATA] = "DATA", - [SEC_DEBUGGING] = "DEBUGGING", - }; - - first = 1; - fputs(" ", stdout); - for (i = 0; i < NR_SEC_FLAGS; i++) { - if (flags & 1) { - if (!first) - fputs(", ", stdout); - first = 0; - fputs(text[i], stdout); - } - flags >>= 1; - } - - putchar('\n'); -} - static int logb2(unsigned val) { @@ -157,8 +132,21 @@ static void dumpscns(Obj *obj) { int i, debug; - unsigned f, flags; + unsigned flags; Section sec; + static Flags f = { + .nr = NR_SEC_FLAGS, + .text = { + [SEC_HAS_CONTENTS] = "CONTENTS", + [SEC_ALLOC] = "ALLOC", + [SEC_LOAD] = "LOAD", + [SEC_RELOC] = "RELOC", + [SEC_READONLY] = "READONLY", + [SEC_CODE] = "CODE", + [SEC_DATA] = "DATA", + [SEC_DEBUGGING] = "DEBUGGING", + } + }; puts("Sections:"); puts("Idx Name Size VMA LMA File off Algn"); @@ -175,7 +163,7 @@ dumpscns(Obj *obj) sec.offset, logb2(sec.align)); - f = 0; + f.flags = 0; flags = sec.flags; debug = sec.type == 'N'; setflag(&f, flags & SALLOC, SEC_ALLOC); @@ -186,7 +174,8 @@ dumpscns(Obj *obj) setflag(&f, (flags & (SEXEC|SLOAD)) == SLOAD, SEC_DATA); setflag(&f, debug, SEC_DEBUGGING); setflag(&f, (flags & SALLOC) && sec.size > 0, SEC_HAS_CONTENTS); - printsecflags(f); + fputs(" ", stdout); + printflags(&f); } if (!pflag) diff --git a/src/cmd/scc-objdump/objdump.h b/src/cmd/scc-objdump/objdump.h @@ -1,3 +1,5 @@ +typedef struct flags Flags; + enum file_flags { HAS_RELOC, EXEC_P, @@ -21,14 +23,21 @@ enum sec_flags { NR_SEC_FLAGS, }; +struct flags { + int nr; + unsigned long flags; + char *text[]; +}; + /* coff32.c */ extern void coff32syms(Obj *); extern void coff32scns(Obj *); -extern unsigned coff32fhdr(Obj *, unsigned long long *); +extern void coff32fhdr(Obj *, unsigned long long *, Flags *); /* main.c */ extern void error(char *, ...); -extern void setflag(unsigned *, int, int); +extern void setflag(Flags *, int, int); +extern void printflags(Flags *); extern int selected(char *); /* globals */