static const pid_t _gen_child() { uint8_t buf[PAGE_SIZE]; mem_assign(buf, PAGE_SIZE, TRAP, TRAP_SZ); uint8_t *elf; const size_t elf_sz = gen_elf(&elf, options.start, (uint8_t *)buf, PAGE_SIZE); const int exe_fd = write_exe(elf, elf_sz, options.savefile); free(elf); const pid_t tracee = fork(); if (tracee < 0) { perror("fork"); exit(EXIT_FAILURE); } else if (tracee == 0) { ptrace_child(exe_fd); abort(); } // Parent close(exe_fd); return tracee; }
int main(int argc, char *argv[]) { int ret = 0; if (argc != 5) { printf( "ARM ELF relocation fixer\n" "Usage: %s inputelf inputbinary outputfile /path/to/startup.bin\n" ,argv[0]); return 0; } int fd = open(argv[1],O_RDONLY); if (fd < 0) { printf("%s: Unable to open \"%s\"\n", argv[0], argv[1]); goto error; } FILE *startup = NULL, *inbin = NULL, *out = NULL; if ( !(inbin = fopen(argv[2],"rb")) ) { printf("%s: Unable to open \"%s\"\n", argv[0], argv[2]); goto error; } if ( !(out = fopen(argv[3],"wb")) ) { printf("%s: Unable to open \"%s\"\n", argv[0], argv[3]); goto error; } if ( !(startup = fopen(argv[4],"rb")) ) { printf("%s: Unable to open \"%s\"\n", argv[0], argv[4]); goto error; } uint32_t count; uint32_t * offsets = elf_get_offsets(fd, &count); if (!offsets) { printf("%s: Unable to parse ELF file \"%s\"\n", argv[0], argv[1]); goto error; } write_startup(startup, out); write_offsets(offsets, count, out); write_exe(inbin, out); free(offsets); goto success; error: ret = -1; success: if (fd >= 0) close(fd); fclose(startup); fclose(out); fclose(inbin); return ret; }