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.
This commit is contained in:
cinap_lenrek 2022-04-23 18:27:31 +00:00 committed by xfnw
parent ddabc765ca
commit 36b0897a42

View file

@ -100,9 +100,10 @@ main(int argc, char **argv)
{ {
Fhdr fhdr; Fhdr fhdr;
u64int kzero; u64int kzero;
ulong rtext;
uchar header[64]; uchar header[64];
char *ofile, *iname; char *ofile, *iname;
int n, arch; int arch;
kzero = 0xF0000000; kzero = 0xF0000000;
ofile = nil; ofile = nil;
@ -116,7 +117,10 @@ main(int argc, char **argv)
infd = open(argv[0], OREAD); infd = open(argv[0], OREAD);
if(infd < 0) sysfatal("infd: %r"); if(infd < 0) sysfatal("infd: %r");
if(crackhdr(infd, &fhdr) == 0) sysfatal("crackhdr: %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], '/'); iname = strrchr(argv[0], '/');
if(iname != nil) if(iname != nil)
iname++; iname++;
@ -127,29 +131,25 @@ main(int argc, char **argv)
if(outfd < 0) sysfatal("create: %r"); if(outfd < 0) sysfatal("create: %r");
tab = mkcrctab(0xEDB88320); tab = mkcrctab(0xEDB88320);
assert(sizeof(buf) >= mach->pgsize); seek(infd, fhdr.hdrsz, 0); /* a.out header not part of the image */
seek(infd, 0, 0);
seek(outfd, sizeof(header), 0); seek(outfd, sizeof(header), 0);
dcrc = 0; dcrc = 0;
copy(fhdr.txtsz);
copy(fhdr.hdrsz + fhdr.txtsz);
n = -(fhdr.hdrsz + fhdr.txtsz) & mach->pgsize - 1; /* round text out to page boundary (see rebootcmd()) */
if(n > 0){ rtext = ((fhdr.entry + fhdr.txtsz + mach->pgsize-1) & -mach->pgsize) - fhdr.entry;
memset(buf, 0, n); if(rtext > fhdr.txtsz){
if(write(outfd, buf, n) < 0) sysfatal("write: %r"); memset(buf, 0, rtext - fhdr.txtsz);
dcrc = blockcrc(tab, dcrc, buf, n); if(write(outfd, buf, rtext - fhdr.txtsz) < 0) sysfatal("write: %r");
dcrc = blockcrc(tab, dcrc, buf, rtext - fhdr.txtsz);
} }
copy(fhdr.datsz); copy(fhdr.datsz);
if((uint)mach->mtype >= nelem(archtab) || archtab[mach->mtype] == 0)
sysfatal("archloch");
arch = archtab[mach->mtype];
memset(header, 0, sizeof(header)); memset(header, 0, sizeof(header));
put(&header[0], 0x27051956); /* magic */ put(&header[0], 0x27051956); /* magic */
put(&header[8], time(0)); /* time */ put(&header[8], time(0)); /* time */
put(&header[12], -(-(fhdr.hdrsz + fhdr.txtsz) & -mach->pgsize) + fhdr.datsz); /* image size */ put(&header[12], rtext + fhdr.datsz); /* image size */
put(&header[16], fhdr.txtaddr - fhdr.hdrsz - kzero); /* load address */ put(&header[16], fhdr.txtaddr - kzero); /* load address */
put(&header[20], fhdr.entry - kzero); /* entry point */ put(&header[20], fhdr.entry - kzero); /* entry point */
put(&header[24], dcrc); /* data crc */ put(&header[24], dcrc); /* data crc */
header[28] = 23; /* os = plan 9 */ header[28] = 23; /* os = plan 9 */