summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@felloff.net>2022-04-23 18:27:31 +0000
committercinap_lenrek <cinap_lenrek@felloff.net>2022-04-23 18:27:31 +0000
commite0fdebf39443a01e4cd4378099cc59001719df18 (patch)
tree6c8cbf3feca761b79c3f38b9088f553a6c5bab75
parent70e2b4f7ec6b225ae69a6e76dd025e1be0e0e8b1 (diff)
aux/aout2uimage: fix text section size rounding (for data section alignment)
the a.out header is not considered part of the image and should not be loaded. it also has no contribution to the rounding of the text section size.
-rw-r--r--sys/src/cmd/aux/aout2uimage.c34
1 files changed, 17 insertions, 17 deletions
diff --git a/sys/src/cmd/aux/aout2uimage.c b/sys/src/cmd/aux/aout2uimage.c
index e8a9e63b7..c557eadfa 100644
--- a/sys/src/cmd/aux/aout2uimage.c
+++ b/sys/src/cmd/aux/aout2uimage.c
@@ -100,9 +100,10 @@ main(int argc, char **argv)
{
Fhdr fhdr;
u64int kzero;
+ ulong rtext;
uchar header[64];
char *ofile, *iname;
- int n, arch;
+ int arch;
kzero = 0xF0000000;
ofile = nil;
@@ -116,7 +117,10 @@ main(int argc, char **argv)
infd = open(argv[0], OREAD);
if(infd < 0) sysfatal("infd: %r");
if(crackhdr(infd, &fhdr) == 0) sysfatal("crackhdr: %r");
-
+ if((uint)mach->mtype >= nelem(archtab) || archtab[mach->mtype] == 0)
+ sysfatal("archloch");
+ arch = archtab[mach->mtype];
+ assert(sizeof(buf) >= mach->pgsize);
iname = strrchr(argv[0], '/');
if(iname != nil)
iname++;
@@ -127,29 +131,25 @@ main(int argc, char **argv)
if(outfd < 0) sysfatal("create: %r");
tab = mkcrctab(0xEDB88320);
- assert(sizeof(buf) >= mach->pgsize);
- seek(infd, 0, 0);
+ seek(infd, fhdr.hdrsz, 0); /* a.out header not part of the image */
seek(outfd, sizeof(header), 0);
dcrc = 0;
-
- copy(fhdr.hdrsz + fhdr.txtsz);
- n = -(fhdr.hdrsz + fhdr.txtsz) & mach->pgsize - 1;
- if(n > 0){
- memset(buf, 0, n);
- if(write(outfd, buf, n) < 0) sysfatal("write: %r");
- dcrc = blockcrc(tab, dcrc, buf, n);
+ copy(fhdr.txtsz);
+
+ /* round text out to page boundary (see rebootcmd()) */
+ rtext = ((fhdr.entry + fhdr.txtsz + mach->pgsize-1) & -mach->pgsize) - fhdr.entry;
+ if(rtext > fhdr.txtsz){
+ memset(buf, 0, rtext - fhdr.txtsz);
+ if(write(outfd, buf, rtext - fhdr.txtsz) < 0) sysfatal("write: %r");
+ dcrc = blockcrc(tab, dcrc, buf, rtext - fhdr.txtsz);
}
copy(fhdr.datsz);
- if((uint)mach->mtype >= nelem(archtab) || archtab[mach->mtype] == 0)
- sysfatal("archloch");
- arch = archtab[mach->mtype];
-
memset(header, 0, sizeof(header));
put(&header[0], 0x27051956); /* magic */
put(&header[8], time(0)); /* time */
- put(&header[12], -(-(fhdr.hdrsz + fhdr.txtsz) & -mach->pgsize) + fhdr.datsz); /* image size */
- put(&header[16], fhdr.txtaddr - fhdr.hdrsz - kzero); /* load address */
+ put(&header[12], rtext + fhdr.datsz); /* image size */
+ put(&header[16], fhdr.txtaddr - kzero); /* load address */
put(&header[20], fhdr.entry - kzero); /* entry point */
put(&header[24], dcrc); /* data crc */
header[28] = 23; /* os = plan 9 */