summaryrefslogtreecommitdiff
path: root/tools/gbafix/gbafix.c
diff options
context:
space:
mode:
authorPikalaxALT <pikalaxalt@gmail.com>2019-06-29 11:41:26 -0400
committerPikalaxALT <pikalaxalt@gmail.com>2019-06-29 11:41:26 -0400
commitf9d8678db43c533cf08c84ae4ebc85738ff113ea (patch)
treeb8b2e4eb19d0a0eec5af0ac0ec73b3c90ebdbebf /tools/gbafix/gbafix.c
parent4b759da71413f3a2b63cb1c220fba4da490bf3d2 (diff)
Additional fixes
Rearrange gbafix to not modify file in the event of failure Fix bug where Dma3Manager may get stuck when compiled with gcc-9.1.0
Diffstat (limited to 'tools/gbafix/gbafix.c')
-rw-r--r--tools/gbafix/gbafix.c41
1 files changed, 24 insertions, 17 deletions
diff --git a/tools/gbafix/gbafix.c b/tools/gbafix/gbafix.c
index b4ad15a88..179305b1b 100644
--- a/tools/gbafix/gbafix.c
+++ b/tools/gbafix/gbafix.c
@@ -138,6 +138,7 @@ int main(int argc, char *argv[])
char *argfile = 0;
FILE *infile;
int silent = 0;
+ int schedule_pad = 0;
int size,bit;
@@ -172,28 +173,27 @@ int main(int argc, char *argv[])
return -1;
}
+ uint32_t sh_offset = 0;
+
// read file
infile = fopen(argfile, "r+b");
if (!infile) { fprintf(stderr, "Error opening input file!\n"); return -1; }
- fseek(infile, 0, SEEK_SET);
+ fseek(infile, sh_offset, SEEK_SET);
fread(&header, sizeof(header), 1, infile);
// elf check
- uint32_t sh_offset = 0;
+ Elf32_Shdr secHeader;
if (memcmp(&header, ELFMAG, 4) == 0) {
Elf32_Ehdr *elfHeader = (Elf32_Ehdr *)&header;
fseek(infile, elfHeader->e_shoff, SEEK_SET);
int i;
for (i = 0; i < elfHeader->e_shnum; i++) {
- Elf32_Shdr secHeader;
fread(&secHeader, sizeof(Elf32_Shdr), 1, infile);
- if (secHeader.sh_type == SHT_PROGBITS && secHeader.sh_addr == elfHeader->e_entry) {
- fseek(infile, secHeader.sh_offset, SEEK_SET);
- sh_offset = secHeader.sh_offset;
- break;
- }
+ if (secHeader.sh_type == SHT_PROGBITS && secHeader.sh_addr == elfHeader->e_entry) break;
}
if (i == elfHeader->e_shnum) { fprintf(stderr, "Error finding entry point!\n"); return 1; }
+ fseek(infile, secHeader.sh_offset, SEEK_SET);
+ sh_offset = secHeader.sh_offset;
fread(&header, sizeof(header), 1, infile);
}
@@ -211,15 +211,7 @@ int main(int argc, char *argv[])
{
case 'p': // pad
{
- fseek(infile, 0, SEEK_END);
- size = ftell(infile);
- for (bit=31; bit>=0; bit--) if (size & (1<<bit)) break;
- if (size != (1<<bit))
- {
- int todo = (1<<(bit+1)) - size;
- while (todo--) fputc(0xFF, infile);
- }
- fseek(infile, 0, SEEK_SET);
+ schedule_pad = 1;
break;
}
@@ -299,6 +291,21 @@ int main(int argc, char *argv[])
header.complement = HeaderComplement();
//header.checksum = checksum_without_header + HeaderChecksum();
+ if (schedule_pad) {
+ if (sh_offset != 0) {
+ fprintf(stderr, "Warning: Cannot safely pad an ELF\n");
+ } else {
+ fseek(infile, 0, SEEK_END);
+ size = ftell(infile);
+ for (bit=31; bit>=0; bit--) if (size & (1<<bit)) break;
+ if (size != (1<<bit))
+ {
+ int todo = (1<<(bit+1)) - size;
+ while (todo--) fputc(0xFF, infile);
+ }
+ }
+ }
+
fseek(infile, sh_offset, SEEK_SET);
fwrite(&header, sizeof(header), 1, infile);
fclose(infile);