scc

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

commit 3143f19d9908c9efa390a9dde8aca7c409e50cc0
parent 2f004084c3f30490182c9e5292d57980659063ad
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  8 Feb 2019 20:39:41 +0000

[libmach/coff32] Implement coff32strip

Diffstat:
Msrc/cmd/strip.c | 26+++++++++++---------------
Msrc/libmach/coff32/coff32read.c | 2+-
Msrc/libmach/coff32/coff32strip.c | 23++++++++++++++++++++---
Msrc/libmach/coff32/coff32write.c | 12+++++++++++-
4 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/src/cmd/strip.c b/src/cmd/strip.c @@ -29,16 +29,14 @@ static void strip(char *fname) { int type; - FILE *fp, *tmp; + FILE *fp; Obj *obj; errno = 0; filename = fname; - fp = fopen(fname, "rb"); - tmp = tmpfile(); - if (!fp || !tmp) - goto err; + if ((fp = fopen(fname, "rb")) == NULL) + goto err1; if ((type = objtype(fp, NULL)) < 0) { error("file format not recognized"); @@ -52,21 +50,22 @@ strip(char *fname) error("file corrupted"); goto err3; } + fclose(fp); + fp = NULL; + objstrip(obj); - if (objwrite(obj, tmp) < 0) { + if ((fp = fopen(fname, "wb")) == NULL) + goto err1; + + if (objwrite(obj, fp) < 0) { error("error writing output"); goto err3; } fclose(fp); - fp = NULL; - - if (remove(fname) || rename("strip.tmp", fname)) { - error(strerror(errno)); - goto err3; - } objdel(obj); + return; err3: @@ -75,9 +74,6 @@ err2: if (fp) fclose(fp); err1: - fclose(tmp); - remove("strip.tmp"); -err: if (errno) error(strerror(errno)); diff --git a/src/libmach/coff32/coff32read.c b/src/libmach/coff32/coff32read.c @@ -262,7 +262,7 @@ readreloc(Obj *obj, FILE *fp) for (i = 0; i < hdr->f_nscns; i++) { scn = &coff->scns[i]; - if (scn->s_nlnno == 0) + if (scn->s_nrelloc == 0) continue; if (!objpos(obj, fp, scn->s_relptr)) diff --git a/src/libmach/coff32/coff32strip.c b/src/libmach/coff32/coff32strip.c @@ -9,12 +9,29 @@ void coff32strip(Obj *obj) { - struct coff32 *coff = obj->data; + int i; FILHDR *hdr; + SCNHDR *scn; + struct coff32 *coff = obj->data; hdr = &coff->hdr; - free(coff->ents); - coff->ents = NULL; + for (i = 0; i < hdr->f_nscns; i++) { + scn = &coff->scns[i]; + scn->s_nrelloc = 0; + scn->s_relptr = 0; + scn->s_nlnno = 0; + scn->s_lnnoptr = 0; + } + hdr->f_nsyms = 0; hdr->f_symptr = 0; + hdr->f_flags |= F_RELFLG | F_LMNO | F_SYMS; + + free(coff->ents); + free(coff->rels); + free(coff->lines); + + coff->ents = NULL; + coff->rels = NULL; + coff->lines = NULL; } diff --git a/src/libmach/coff32/coff32write.c b/src/libmach/coff32/coff32write.c @@ -166,6 +166,10 @@ writeents(Obj *obj, FILE *fp) coff = obj->data; hdr = &coff->hdr; + + if (!coff->ents) + return 1; + strtbl = NULL; strsiz = 0; @@ -212,7 +216,7 @@ writestr(Obj *obj, FILE *fp) fwrite(buf, 4, 1, fp); fwrite(coff->strtbl, coff->strsiz, 1, fp); - return ferror(fp); + return ferror(fp) == 0; } static int @@ -245,6 +249,9 @@ writereloc(Obj *obj, FILE *fp) coff = obj->data; hdr = &coff->hdr; + if (!coff->rels) + return 1; + for (i = 0; i < hdr->f_nscns; i++) { rp = coff->rels[i]; if (!rp) @@ -275,6 +282,9 @@ writelines(Obj *obj, FILE *fp) coff = obj->data; hdr = &coff->hdr; + if (!coff->lines) + return 1; + for (i = 0; i < hdr->f_nscns; i++) { lp = coff->lines[i]; if (!lp)