shithub: doll

Download patch

ref: 62286e5ef46f9b8cbfbab10ec64b2cb97e35070b
parent: 62edc48ee53375b461c77e1a33b7d649ad3ba3ca
author: Tevo <[email protected]>
date: Sun Dec 6 23:01:44 EST 2020

Verbosity, stupid bug

--- a/main.c
+++ b/main.c
@@ -3,7 +3,7 @@
 #include <bio.h>
 #include <mach.h>
 
-int keepgoing;
+int verbosity, keepgoing;
 char *out = "out.dol";
 
 typedef struct Dolhdr Dolhdr;
@@ -38,7 +38,23 @@
 	va_end(args);
 }
 
+#pragma varargck argpos error 1
 void
+verbose(int l, char *fmt, ...)
+{
+	va_list args;
+
+	if(verbosity < l)
+		return;
+
+	va_start(args, fmt);
+	vfprint(1, fmt, args);
+	va_end(args);
+}
+
+#define verbose(lvl, ...) if(verbosity < (lvl)) {} else print(__VA_ARGS__);
+
+void
 usage(void)
 {
 	fprint(2, "usage: %s [-k] [-o out] exec\n", argv0);
@@ -60,6 +76,9 @@
 	case 'k':
 		keepgoing++;
 		break;
+	case 'v':
+		verbosity++;
+		break;
 	default:
 		usage();
 	} ARGEND;
@@ -75,6 +94,8 @@
 
 	crackhdr(ifd, &fhdr);
 
+	verbose(1, "executable is %s\n", fhdr.name);
+
 	if(fhdr.type != FPOWER && fhdr.type != FPOWERB)
 		error("not a PowerPC executable (%d)", fhdr.type);
 
@@ -86,21 +107,46 @@
 	if(fhdr.txtaddr < 0x80003F00 || fhdr.txtaddr > 0x81330000)
 		error("text outside standard executable area (0x%08lX)", (ulong)fhdr.txtaddr);
 
+	verbose(2,
+		"TEXT section 0:\n"
+		"address: 0x%08ulX\n"
+		"offset (file): 0x%08ulX\n"
+		"size: 0x%08ulX bytes\n\n",
+		beswal(dol.txtaddr[0]), beswal(dol.txtoff[0]), beswal(dol.txtsz[0])
+	);
+
 	dol.dataddr[0] = beswal(fhdr.dataddr);
 	dol.datoff[0] = beswal(fhdr.datoff);
 	dol.datsz[0] = beswal(fhdr.datsz);
 
+	verbose(2,
+		"DATA section 0:\n"
+		"address: 0x%08ulX\n"
+		"offset (file): 0x%08ulX\n"
+		"size: 0x%08ulX bytes\n\n",
+		beswal(dol.dataddr[0]), beswal(dol.datoff[0]), beswal(dol.datsz[0])
+	);
+
 	dol.bssaddr = fhdr.dataddr + fhdr.datsz;	/* FIXME? */
 	dol.bsssz = fhdr.bsssz;
 
-	dol.entry = fhdr.entry;
+	verbose(2,
+		"BSS segment:\n"
+		"address: 0x%08ulX\n"
+		"size: 0x%08ulX bytes\n\n",
+		beswal(dol.bssaddr), beswal(dol.bsssz)
+	);
 
+	dol.entry = beswal(fhdr.entry);
+
+	verbose(1, "entry point: 0x%08ulX\n", beswal(dol.entry));
+
 	/* TODO validate addresses */
 
 	if(write(ofd, &dol, sizeof(dol)) != sizeof(dol))
 		sysfatal("write: %r");
 
-	seek(ifd, fhdr.hdrsz, 0);
+	seek(ifd, fhdr.hdrsz+1, 0);
 	while(read(ifd, buf, sizeof(buf)) > 0)
 		if(write(ofd, buf, sizeof(buf)) != sizeof(buf))
 			sysfatal("write: %r");