scc

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

commit e44b4230eef30c9458ca2ba1818dfb9837d6a017
parent de4cba8d93311300fadf3cbe165b44090520d4d7
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 22 Aug 2019 09:25:14 +0100

[libmach] Remove target getsyms and getsect

These function are only called from objread(), so
it is better to join these functions in only one file
instead of having 3 different files.

Diffstat:
Msrc/libmach/coff32/Makefile | 2--
Dsrc/libmach/coff32/coff32getsect.c | 78------------------------------------------------------------------------------
Dsrc/libmach/coff32/coff32getsyms.c | 91-------------------------------------------------------------------------------
Msrc/libmach/coff32/coff32read.c | 172++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/libmach/libmach.h | 3---
Msrc/libmach/objread.c | 54++----------------------------------------------------
6 files changed, 172 insertions(+), 228 deletions(-)

diff --git a/src/libmach/coff32/Makefile b/src/libmach/coff32/Makefile @@ -12,8 +12,6 @@ OBJS = coff32del.o \ coff32getindex.o \ coff32setidx.o \ coff32getidx.o \ - coff32getsect.o \ - coff32getsyms.o \ coff32sync.o \ all: $(OBJS) diff --git a/src/libmach/coff32/coff32getsect.c b/src/libmach/coff32/coff32getsect.c @@ -1,78 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -#include <scc/mach.h> - -#include "../libmach.h" -#include "coff32.h" - -int -coff32getsect(Obj *obj) -{ - int i; - unsigned sflags, type; - unsigned long flags; - FILHDR *hdr; - struct coff32 *coff; - SCNHDR *scn; - Objsect *secs, *sp; - - coff = obj->data; - hdr = &coff->hdr; - - secs = malloc(sizeof(Objsect) * hdr->f_nscns); - if (!secs) - return -1; - - for (i = 0; i < hdr->f_nscns; i++) { - sp = &secs[i]; - sp->next = (i < hdr->f_nscns-1) ? &secs[i+1] : NULL; - scn = &coff->scns[i]; - flags = scn->s_flags; - - if (flags & STYP_TEXT) { - type = 'T'; - sflags = SALLOC | SRELOC | SLOAD | SEXEC | SREAD; - if (flags & STYP_NOLOAD) - sflags |= SSHARED; - } else if (flags & STYP_DATA) { - type = 'D'; - sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD; - if (flags & STYP_NOLOAD) - sflags |= SSHARED; - } else if (flags & STYP_BSS) { - type = 'B'; - sflags = SALLOC | SREAD | SWRITE; - } else if (flags & STYP_INFO) { - type = 'N'; - sflags = 0; - } else if (flags & STYP_LIB) { - type = 'T'; - sflags = SRELOC; - } else if (flags & STYP_DSECT) { - type = 'D'; - sflags = SRELOC; - } else if (flags & STYP_PAD) { - type = 'D'; - sflags = SLOAD; - } else { - type = 'D'; /* We assume that STYP_REG is data */ - sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD; - } - - if (flags & STYP_NOLOAD) - sflags &= ~SLOAD; - - sp->name = scn->s_name; - sp->id = i; - sp->seek = scn->s_scnptr; - sp->size = scn->s_size; - sp->type = type; - sp->flags = sflags; - sp->align = 4; /* TODO: Check how align is defined in coff */ - } - obj->secs = secs; - obj->nsecs = i; - - return 1; -} diff --git a/src/libmach/coff32/coff32getsyms.c b/src/libmach/coff32/coff32getsyms.c @@ -1,91 +0,0 @@ -#include <stdio.h> -#include <ctype.h> - -#include <scc/mach.h> - -#include "../libmach.h" -#include "coff32.h" - -static int -typeof(Coff32 *coff, SYMENT *ent) -{ - int c; - SCNHDR *scn; - long flags; - - switch (ent->n_scnum) { - case N_DEBUG: - c = 'N'; - break; - case N_ABS: - c = 'a'; - break; - case N_UNDEF: - c = (ent->n_value != 0) ? 'C' : 'U'; - break; - default: - if (ent->n_scnum > coff->hdr.f_nscns) - return -1; - scn = &coff->scns[ent->n_scnum-1]; - flags = scn->s_flags; - if (flags & STYP_TEXT) - c = 't'; - else if (flags & STYP_DATA) - c = 'd'; - else if (flags & STYP_BSS) - c = 'b'; - else - c = '?'; - break; - } - - if (ent->n_sclass == C_EXT) - c = toupper(c); - - return c; -} - -static char * -symname(Coff32 *coff, SYMENT *ent) -{ - long off; - - if (ent->n_zeroes != 0) - return ent->n_name; - - off = ent->n_offset; - if (off >= coff->strsiz) - return NULL; - return &coff->strtbl[off]; -} - -int -coff32getsyms(Obj *obj) -{ - int t; - long i; - char *s; - Objsym *sym; - SYMENT *ent; - Coff32 *coff = obj->data; - - for (i = 0; i < coff->hdr.f_nsyms; i += ent->n_numaux + 1) { - ent = &coff->ents[i]; - - if ((t = typeof(coff, ent)) < 0) - return -1; - - if ((s = symname(coff, ent)) == NULL) - return -1; - - if ((sym = objlookup(obj, s, 1)) == NULL) - return -1; - - sym->type = t; - sym->value = ent->n_value; - sym->size = (sym->type == 'C') ? ent->n_value : 0; - sym->sect = ent->n_scnum-1; - } - - return i; -} diff --git a/src/libmach/coff32/coff32read.c b/src/libmach/coff32/coff32read.c @@ -1,4 +1,5 @@ #include <assert.h> +#include <ctype.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -336,8 +337,8 @@ readaout(Obj *obj, FILE *fp) return 1; } -int -coff32read(Obj *obj, FILE *fp) +static int +readfile(Obj *obj, FILE *fp) { /* TODO: Add validation of the different fields */ if (fgetpos(fp, &obj->pos)) @@ -362,3 +363,170 @@ error: objfree(obj, TARGETDEL); return -1; } + +static int +convsect(Obj *obj) +{ + int i; + unsigned sflags, type; + unsigned long flags; + FILHDR *hdr; + struct coff32 *coff; + SCNHDR *scn; + Objsect *secs, *sp; + + coff = obj->data; + hdr = &coff->hdr; + + secs = malloc(sizeof(Objsect) * hdr->f_nscns); + if (!secs) + return -1; + + for (i = 0; i < hdr->f_nscns; i++) { + sp = &secs[i]; + sp->next = (i < hdr->f_nscns-1) ? &secs[i+1] : NULL; + scn = &coff->scns[i]; + flags = scn->s_flags; + + if (flags & STYP_TEXT) { + type = 'T'; + sflags = SALLOC | SRELOC | SLOAD | SEXEC | SREAD; + if (flags & STYP_NOLOAD) + sflags |= SSHARED; + } else if (flags & STYP_DATA) { + type = 'D'; + sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD; + if (flags & STYP_NOLOAD) + sflags |= SSHARED; + } else if (flags & STYP_BSS) { + type = 'B'; + sflags = SALLOC | SREAD | SWRITE; + } else if (flags & STYP_INFO) { + type = 'N'; + sflags = 0; + } else if (flags & STYP_LIB) { + type = 'T'; + sflags = SRELOC; + } else if (flags & STYP_DSECT) { + type = 'D'; + sflags = SRELOC; + } else if (flags & STYP_PAD) { + type = 'D'; + sflags = SLOAD; + } else { + type = 'D'; /* We assume that STYP_REG is data */ + sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD; + } + + if (flags & STYP_NOLOAD) + sflags &= ~SLOAD; + + sp->name = scn->s_name; + sp->id = i; + sp->seek = scn->s_scnptr; + sp->size = scn->s_size; + sp->type = type; + sp->flags = sflags; + sp->align = 4; /* TODO: Check how align is defined in coff */ + } + obj->secs = secs; + obj->nsecs = i; + + return 1; +} + +static int +typeof(Coff32 *coff, SYMENT *ent) +{ + int c; + SCNHDR *scn; + long flags; + + switch (ent->n_scnum) { + case N_DEBUG: + c = 'N'; + break; + case N_ABS: + c = 'a'; + break; + case N_UNDEF: + c = (ent->n_value != 0) ? 'C' : 'U'; + break; + default: + if (ent->n_scnum > coff->hdr.f_nscns) + return -1; + scn = &coff->scns[ent->n_scnum-1]; + flags = scn->s_flags; + if (flags & STYP_TEXT) + c = 't'; + else if (flags & STYP_DATA) + c = 'd'; + else if (flags & STYP_BSS) + c = 'b'; + else + c = '?'; + break; + } + + if (ent->n_sclass == C_EXT) + c = toupper(c); + + return c; +} + +static char * +symname(Coff32 *coff, SYMENT *ent) +{ + long off; + + if (ent->n_zeroes != 0) + return ent->n_name; + + off = ent->n_offset; + if (off >= coff->strsiz) + return NULL; + return &coff->strtbl[off]; +} + +static int +convsyms(Obj *obj) +{ + int t; + long i; + char *s; + Objsym *sym; + SYMENT *ent; + Coff32 *coff = obj->data; + + for (i = 0; i < coff->hdr.f_nsyms; i += ent->n_numaux + 1) { + ent = &coff->ents[i]; + + if ((t = typeof(coff, ent)) < 0) + return -1; + + if ((s = symname(coff, ent)) == NULL) + return -1; + + if ((sym = objlookup(obj, s, 1)) == NULL) + return -1; + + sym->type = t; + sym->value = ent->n_value; + sym->size = (sym->type == 'C') ? ent->n_value : 0; + sym->sect = ent->n_scnum-1; + } + + return i; +} + +int +coff32read(Obj *obj, FILE *fp) +{ + if (readfile(obj, fp) < 0) + return -1; + if (convsect(obj) < 0) + return -1; + if (convsyms(obj) < 0) + return -1; + return 0; +} diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h @@ -49,8 +49,5 @@ extern long coff32setidx(int order, long nsyms, Objsymdef *head, FILE *fp); extern int coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp); extern int coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp); -extern int coff32getsect(Obj *obj); - extern char *coff32namidx(void); -extern int coff32getsyms(Obj *obj); extern int coff32sync(Obj *obj); diff --git a/src/libmach/objread.c b/src/libmach/objread.c @@ -8,40 +8,8 @@ static int (*freadv[])(Obj *, FILE *) = { [COFF32] = coff32read, }; -static int (*fsymsv[])(Obj *) = { - [COFF32] = coff32getsyms, -}; - -static int (*fsectsv[])(Obj *) = { - [COFF32] = coff32getsect, -}; - -static int -getsects(Obj *obj) -{ - int fmt; - - fmt = FORMAT(obj->type); - if (fmt >= NFORMATS) - return -1; - - return (*fsectsv[fmt])(obj); -} - -static int -getsyms(Obj *obj) -{ - int fmt; - - fmt = FORMAT(obj->type); - if (fmt >= NFORMATS) - return -1; - - return (*fsymsv[fmt])(obj); -} - -static int -readfile(Obj *obj, FILE *fp) +int +objread(Obj *obj, FILE *fp) { int fmt; @@ -55,21 +23,3 @@ readfile(Obj *obj, FILE *fp) return 0; } - -int -objread(Obj *obj, FILE *fp) -{ - objfree(obj, TARGETDEL | GENERICDEL); - - if (readfile(obj, fp) < 0) - goto err; - if (getsyms(obj) < 0) - goto err; - if (getsects(obj) < 0) - goto err; - return 0; - -err: - objfree(obj, TARGETDEL | GENERICDEL); - return -1; -}