shithub: scc

Download patch

ref: 1055827d79c5371b019dfeec0fc91f2c5c4ebafd
parent: d4565f079cfff9680439f8b0c2b82d7a01167498
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Feb 8 15:39:41 EST 2019

[libmach/coff32] Implement coff32strip

--- a/src/cmd/strip.c
+++ b/src/cmd/strip.c
@@ -29,16 +29,14 @@
 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 @@
 		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 @@
 	if (fp)
 		fclose(fp);
 err1:
-	fclose(tmp);
-	remove("strip.tmp");
-err:
 	if (errno)
 		error(strerror(errno));
 
--- a/src/libmach/coff32/coff32read.c
+++ b/src/libmach/coff32/coff32read.c
@@ -262,7 +262,7 @@
 
 	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))
--- 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;
 }
--- a/src/libmach/coff32/coff32write.c
+++ b/src/libmach/coff32/coff32write.c
@@ -166,6 +166,10 @@
 
 	coff  = obj->data;
 	hdr = &coff->hdr;
+
+	if (!coff->ents)
+		return 1;
+
 	strtbl = NULL;
 	strsiz = 0;
 
@@ -212,7 +216,7 @@
 	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 @@
 	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)
@@ -274,6 +281,9 @@
 
 	coff  = obj->data;
 	hdr = &coff->hdr;
+
+	if (!coff->lines)
+		return 1;
 
         for (i = 0; i < hdr->f_nscns; i++) {
 		lp = coff->lines[i];