update logic for manual loader

This commit is contained in:
nganhkhoa 2023-06-05 15:54:22 +07:00
parent 3a30f12019
commit 5a8a83f846
2 changed files with 17 additions and 6 deletions

View File

@ -642,7 +642,7 @@ void build_cache(struct libcache& cache, void* main) {
char *name = dyld_get_image_name_func(i); char *name = dyld_get_image_name_func(i);
bootstrap_libcache_item(&cache.libs[i], header, name); bootstrap_libcache_item(&cache.libs[i], header, name);
cache.libs[i].hash = calculate_libname_hash(&cache, name); cache.libs[i].hash = calculate_libname_hash(&cache, name);
printf("%p %s\n", header, name); // printf("%p %s\n", header, name);
} }
} }
@ -709,6 +709,7 @@ void fix(struct libcache& cache) {
// think of a way to get what binary to fix // think of a way to get what binary to fix
// so we can iterate through them // so we can iterate through them
struct libcache_item* libfixing = get_libcache_with_name(&cache, "./out/a"); struct libcache_item* libfixing = get_libcache_with_name(&cache, "./out/a");
// print_macho_summary(libfixing->header);
for (int i = 0; i < nimports; i++) { for (int i = 0; i < nimports; i++) {
struct imported_symbol symbol = imported_table[i]; struct imported_symbol symbol = imported_table[i];
uint64_t fix_at = symbol.offset + libfixing->segment[symbol.segment_i]; uint64_t fix_at = symbol.offset + libfixing->segment[symbol.segment_i];
@ -716,13 +717,13 @@ void fix(struct libcache& cache) {
int need_rw_fix = true; int need_rw_fix = true;
for (int j = 0; j < npage_rw_fixed; j++) { for (int j = 0; j < npage_rw_fixed; j++) {
if (page_rw_fixed[j] <= fix_at && if (page_rw_fixed[j] <= fix_at &&
page_rw_fixed[j] + 0x4000 > fix_at) { page_rw_fixed[j] + 0x1000 > fix_at) {
need_rw_fix = false; need_rw_fix = false;
} }
} }
if (need_rw_fix) { if (need_rw_fix) {
uint64_t start_page = fix_at - (fix_at % 0x4000); uint64_t start_page = fix_at - (fix_at % 0x1000);
vm_protect_func(mach_task_self_func(), start_page, 0x4000, 0, vm_protect_func(mach_task_self_func(), start_page, 0x1000, 0,
VM_PROT_READ | VM_PROT_WRITE); VM_PROT_READ | VM_PROT_WRITE);
page_rw_fixed[npage_rw_fixed++] = start_page; page_rw_fixed[npage_rw_fixed++] = start_page;
printf("modify page starts at 0x%llx to RW\n", start_page); printf("modify page starts at 0x%llx to RW\n", start_page);

View File

@ -1,11 +1,19 @@
set -ex # set -ex
VERSION=11 VERSION=${1:-14}
OUT=./out OUT=./out
LOGIC=2 LOGIC=2
mkdir -p $OUT mkdir -p $OUT
echo "using mach-o version $VERSION"
if [[ $VERSION -ge 14 ]]
then
echo "Resulting binary uses MODERN symbol resolver"
else
echo "Resulting binary uses LEGACY symbol resolver"
fi
if [[ $LOGIC -eq 0 ]] if [[ $LOGIC -eq 0 ]]
then then
@ -38,6 +46,8 @@ clang++ -mmacosx-version-min=$VERSION -o $OUT/a -L"./out" -lb a.cc
# build libb with symbols extracted from a # build libb with symbols extracted from a
clang++ -mmacosx-version-min=$VERSION -o $OUT/libb.dylib -shared -Wl,-reexport_library out/libc.dylib b.cc clang++ -mmacosx-version-min=$VERSION -o $OUT/libb.dylib -shared -Wl,-reexport_library out/libc.dylib b.cc
out/a
else else
# dummy test build # dummy test build