-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "../config/config_asm.h" | ||
|
||
// preplace32 is lazy | ||
#define preplace32(range, a, b) do { uintptr_t _ = preplace32_a(range, a); if(_) preplace32_b(range, _, a, b); } while(0) | ||
|
||
static void check_no_placeholders(prange_t pr) { | ||
for(uintptr_t addr = (uintptr_t)pr.start; addr + sizeof(uint32_t) <= (uintptr_t)pr.start + pr.size; addr++) { | ||
uint32_t val = *(uint32_t *)addr; | ||
if(val > CONFIG_MIN && val < CONFIG_MAX) { | ||
die("got %08x", val); | ||
} | ||
} | ||
} | ||
|
||
static uintptr_t preplace32_a(prange_t range, uint32_t a) { | ||
for(uintptr_t addr = (uintptr_t)range.start; addr + sizeof(uint32_t) <= (uintptr_t)range.start + range.size; addr++) { | ||
if(*(uint32_t *)addr == a) { | ||
return addr; | ||
} | ||
} | ||
//fprintf(stderr, "preplace32: warning: didn't find %08x anywhere\n", a); | ||
return 0; | ||
} | ||
|
||
static void preplace32_b(prange_t range, uintptr_t start, uint32_t a, uint32_t b) { | ||
for(uintptr_t addr = start; addr + sizeof(uint32_t) <= (uintptr_t)range.start + range.size; addr++) { | ||
if(*(uint32_t *)addr == a) { | ||
*(uint32_t *)addr = b; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <data/common.h> | ||
#include <data/binary.h> | ||
#include <assert.h> | ||
|
||
int main(int argc, char **argv) { | ||
struct binary binary; | ||
b_init(&binary); | ||
mode_t mode; | ||
prange_t kernel = load_file(argv[1], true, &mode); | ||
b_prange_load_macho(&binary, kernel, argv[1]); | ||
|
||
int patchfd = open(argv[2], O_RDONLY); | ||
if(patchfd == -1) { | ||
edie("could not open patchfd"); | ||
} | ||
|
||
while(1) { | ||
uint32_t name_len; | ||
ssize_t result = read(patchfd, &name_len, sizeof(name_len)); | ||
if(result == 0) break; | ||
assert(result == sizeof(name_len)); | ||
assert(name_len < 128); | ||
char *name = malloc(name_len + 1); | ||
assert(read(patchfd, name, name_len) == (ssize_t) name_len); | ||
name[name_len] = 0; | ||
|
||
addr_t addr; | ||
assert(read(patchfd, &addr, sizeof(addr)) == sizeof(addr)); | ||
uint32_t size; | ||
assert(read(patchfd, &size, sizeof(size)) == sizeof(size)); | ||
assert(size < 0x1000000); | ||
|
||
void *stuff = malloc(size); | ||
assert(read(patchfd, stuff, size) == (ssize_t) size); | ||
|
||
printf("%s\n", name); | ||
memcpy((char *) kernel.start + range_to_off_range((range_t) {&binary, addr, size}).start, stuff, size); | ||
|
||
free(name); | ||
free(stuff); | ||
} | ||
|
||
store_file(kernel, argv[3], mode); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters