X-Git-Url: https://git.saurik.com/ldid.git/blobdiff_plain/4374152f9caeb53be3e89388effce42be36e62f1..7b496abd3d9cbe500279e025860ed8a345d665b4:/ldid.cpp diff --git a/ldid.cpp b/ldid.cpp index 39c1d96..384b27c 100644 --- a/ldid.cpp +++ b/ldid.cpp @@ -31,9 +31,7 @@ extern "C" { #include #include -#include -#include -#include +#include struct fat_header { uint32_t magic; @@ -732,12 +730,14 @@ struct CodesignAllocation { uint32_t offset_; uint32_t size_; uint32_t alloc_; + uint32_t align_; - CodesignAllocation(FatMachHeader mach_header, size_t offset, size_t size, size_t alloc) : + CodesignAllocation(FatMachHeader mach_header, size_t offset, size_t size, size_t alloc, size_t align) : mach_header_(mach_header), offset_(offset), size_(size), - alloc_(alloc) + alloc_(alloc), + align_(align) { } }; @@ -1010,15 +1010,19 @@ int main(int argc, const char *argv[]) { size_t normal((size + 0x1000 - 1) / 0x1000); alloc = Align(alloc + (special + normal) * 0x14, 16); - offset = Align(offset, 4096); - allocations.push_back(CodesignAllocation(mach_header, offset, size, alloc)); + auto *fat_arch(mach_header.GetFatArch()); + uint32_t align(fat_arch == NULL ? 0 : source.Swap(fat_arch->align)); + offset = Align(offset, 1 << align); + + allocations.push_back(CodesignAllocation(mach_header, offset, size, alloc, align)); offset += size + alloc; + offset = Align(offset, 16); } } asprintf(&temp, "%s.%s.cs", dir, base); fclose(fopen(temp, "w+")); - truncate(temp, offset); + _syscall(truncate(temp, offset)); void *file(map(temp, 0, offset, NULL, false)); memset(file, 0, offset); @@ -1044,7 +1048,7 @@ int main(int argc, const char *argv[]) { fat_arch->cpusubtype = Swap(source->cpusubtype); fat_arch->offset = Swap(allocation.offset_); fat_arch->size = Swap(align + allocation.alloc_); - fat_arch->align = Swap(0xc); + fat_arch->align = Swap(allocation.align_); ++fat_arch; } @@ -1072,10 +1076,17 @@ int main(int argc, const char *argv[]) { signature->dataoff = mach_header.Swap(align); signature->datasize = mach_header.Swap(allocation.alloc_); - _foreach (segment, mach_header.GetSegments("__LINKEDIT")) - segment->filesize = mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff)); - _foreach (segment, mach_header.GetSegments64("__LINKEDIT")) - segment->filesize = mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff)); + _foreach (segment, mach_header.GetSegments("__LINKEDIT")) { + size_t size(mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff))); + segment->filesize = size; + segment->vmsize = Align(size, 0x1000); + } + + _foreach (segment, mach_header.GetSegments64("__LINKEDIT")) { + size_t size(mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff))); + segment->filesize = size; + segment->vmsize = Align(size, 0x1000); + } } } @@ -1086,6 +1097,8 @@ int main(int argc, const char *argv[]) { struct linkedit_data_command *signature(NULL); _foreach (mach_header, fat_header.GetMachHeaders()) { + struct encryption_info_command *encryption(NULL); + if (flag_A) { if (mach_header.GetCPUType() != flag_CPUType) continue; @@ -1132,6 +1145,8 @@ int main(int argc, const char *argv[]) { load_command->cmd = mach_header.Swap(LC_LOAD_DYLIB); else if (cmd == LC_CODE_SIGNATURE) signature = reinterpret_cast(load_command); + else if (cmd == LC_ENCRYPTION_INFO) + encryption = reinterpret_cast(load_command); else if (cmd == LC_UUID) { volatile struct uuid_command *uuid_command(reinterpret_cast(load_command)); @@ -1161,18 +1176,20 @@ int main(int argc, const char *argv[]) { dylib_command->dylib.timestamp = mach_header.Swap(timed); } - } else if (cmd == LC_ENCRYPTION_INFO) { - volatile struct encryption_info_command *encryption_info_command(reinterpret_cast(load_command)); + } + } - if (flag_D) - encryption_info_command->cryptid = mach_header.Swap(0); + if (flag_d) { + _assert(encryption != NULL); - if (flag_d) { - printf("cryptoff=0x%x\n", mach_header.Swap(encryption_info_command->cryptoff)); - printf("cryptsize=0x%x\n", mach_header.Swap(encryption_info_command->cryptsize)); - printf("cryptid=0x%x\n", mach_header.Swap(encryption_info_command->cryptid)); - } - } + printf("cryptoff=0x%x\n", mach_header.Swap(encryption->cryptoff)); + printf("cryptsize=0x%x\n", mach_header.Swap(encryption->cryptsize)); + printf("cryptid=0x%x\n", mach_header.Swap(encryption->cryptid)); + } + + if (flag_D) { + _assert(encryption != NULL); + encryption->cryptid = mach_header.Swap(0); } if (flag_e) {