summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2010-11-16 07:35:45 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2010-11-16 07:35:45 (GMT)
commit0b74bc0b4b38b5d326a791007d18ec57f3ad027a (patch)
tree91addea0ba87ef89fa17a4b73b327dacc187042c
parentd943752a36805ecc12115712a0eefea42814a143 (diff)
downloadabc8000-old-0b74bc0b4b38b5d326a791007d18ec57f3ad027a.zip
abc8000-old-0b74bc0b4b38b5d326a791007d18ec57f3ad027a.tar.gz
abc8000-old-0b74bc0b4b38b5d326a791007d18ec57f3ad027a.tar.bz2
abc8000-old-0b74bc0b4b38b5d326a791007d18ec57f3ad027a.tar.xz
sysrom, exec: Correctly pad executables
Padding is (mostly) optional, but make sure that whatever we do actually matches the values in the header...
-rw-r--r--data/sysrom/exec.c8
-rwxr-xr-xdata/sysrom/genrel.pl15
2 files changed, 17 insertions, 6 deletions
diff --git a/data/sysrom/exec.c b/data/sysrom/exec.c
index f5d94a0..b0419db 100644
--- a/data/sysrom/exec.c
+++ b/data/sysrom/exec.c
@@ -48,7 +48,7 @@ int exec_file(const char *filename, void *addr)
fr = f_open(&file, filename, FA_READ|FA_OPEN_EXISTING);
if (fr != FR_OK)
- return fr;
+ return -1;
fr = f_read(&file, &hdr, sizeof hdr, &bytesread);
if (fr != FR_OK || bytesread != sizeof hdr)
@@ -88,6 +88,7 @@ int exec_file(const char *filename, void *addr)
uint32_t rel = *relp++;
if (rel + 4 > hdr.totalsize || (rel & 1))
goto err;
+
*(size_t *)((char *)addr + rel) += (size_t)addr;
}
}
@@ -105,7 +106,7 @@ int exec_file(const char *filename, void *addr)
err:
f_close(&file);
- return fr;
+ return -1;
}
/*
@@ -123,5 +124,6 @@ void disk_boot(void)
f_mount(0, &sd_fs);
printf("Running boot file...\n");
- exec_file(boot_filename, (void *)BOOT_ADDR);
+ if (exec_file(boot_filename, (void *)BOOT_ADDR))
+ printf("Boot file execution failed!\n");
}
diff --git a/data/sysrom/genrel.pl b/data/sysrom/genrel.pl
index e6be78d..1843ead 100755
--- a/data/sysrom/genrel.pl
+++ b/data/sysrom/genrel.pl
@@ -12,8 +12,6 @@ open(BINFILE, '<', $binfile)
or die "$0: $binfile: $!\n";
open(SHRFILE, '<', $shrfile)
or die "$0: $shrfile: $!\n";
-open(OUT, '>' , $out)
- or die "$0: $out: $!\n";
$binsize = (stat(BINFILE))[7];
@@ -90,9 +88,12 @@ while (defined($line = <SHRFILE>)) {
}
$addr = $base + $offset;
- if ($addr+3 >= $totalsize) {
+ if ($addr + 3 > $totalsize) {
die "$0: relocation outside the file\n";
+ } elsif ($addr & 1) {
+ die "$0: relocation at an odd address\n";
}
+
$relocs .= pack("N", $base + $offset);
$nrelocs++;
}
@@ -103,6 +104,9 @@ close(SHRFILE);
# Write executable header
#
+open(OUT, '>' , $out)
+ or die "$0: $out: $!\n";
+
# Pad the sizes to a multiple of 32
$binsize = ($binsize + 31) & ~31;
$totalsize = ($totalsize + 31) & ~31;
@@ -130,6 +134,11 @@ while (read(BINFILE, $bindata, 4096) > 0) {
close(BINFILE);
#
+# Add padding if necessary
+#
+print OUT "\0" x ($binsize - $raw_binsize);
+
+#
# Output the relocations
#
print OUT $relocs;