+ const char *path(file.c_str());
+ const char *base = strrchr(path, '/');
+ char *temp(NULL), *dir;
+
+ if (base != NULL)
+ dir = strndup_(path, base++ - path + 1);
+ else {
+ dir = strdup("");
+ base = path;
+ }
+
+ if (flag_r) {
+ uint32_t clip(0); {
+ FatHeader fat_header(Map(path));
+ _foreach (mach_header, fat_header.GetMachHeaders()) {
+ if (flag_A) {
+ if (mach_header.GetCPUType() != flag_CPUType)
+ continue;
+ if (mach_header.GetCPUSubtype() != flag_CPUSubtype)
+ continue;
+ }
+
+ mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK);
+
+ uint32_t size(_not(uint32_t)); {
+ _foreach (load_command, mach_header.GetLoadCommands()) {
+ switch (mach_header.Swap(load_command->cmd)) {
+ case LC_CODE_SIGNATURE: {
+ struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
+ memset(reinterpret_cast<uint8_t *>(mach_header.GetBase()) + mach_header.Swap(signature->dataoff), 0, mach_header.Swap(signature->datasize));
+ memset(signature, 0, sizeof(struct linkedit_data_command));
+
+ mach_header->ncmds = mach_header.Swap(mach_header.Swap(mach_header->ncmds) - 1);
+ mach_header->sizeofcmds = mach_header.Swap(uint32_t(mach_header.Swap(mach_header->sizeofcmds) - sizeof(struct linkedit_data_command)));
+ } break;
+
+ case LC_SYMTAB: {
+ struct symtab_command *symtab = reinterpret_cast<struct symtab_command *>(load_command);
+ size = mach_header.Swap(symtab->stroff) + mach_header.Swap(symtab->strsize);
+ } break;
+ }
+ }
+ }
+
+ _assert(size != _not(uint32_t));
+
+ _foreach (segment, const_cast<FatMachHeader &>(mach_header).GetSegments("__LINKEDIT")) {
+ segment->filesize -= mach_header.GetSize() - size;
+
+ if (fat_arch *fat_arch = mach_header.GetFatArch()) {
+ fat_arch->size = fat_header.Swap(size);
+ clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size);
+ } else
+ clip = std::max(clip, size);
+ }
+
+ _foreach (segment, const_cast<FatMachHeader &>(mach_header).GetSegments64("__LINKEDIT")) {
+ segment->filesize -= mach_header.GetSize() - size;
+
+ if (fat_arch *fat_arch = mach_header.GetFatArch()) {
+ fat_arch->size = fat_header.Swap(size);
+ clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size);
+ } else
+ clip = std::max(clip, size);
+ }
+ }
+ }
+
+ if (clip != 0)
+ _syscall(truncate(path, clip));
+ }
+
+ if (flag_S) {
+ asprintf(&temp, "%s.%s.cs", dir, base);
+ const char *allocate = getenv("CODESIGN_ALLOCATE");
+ if (allocate == NULL)
+ allocate = "codesign_allocate";
+
+ std::vector<CodesignAllocation> allocations; {
+ FatHeader fat_header(Map(path));
+ _foreach (mach_header, fat_header.GetMachHeaders()) {
+ if (flag_A) {
+ if (mach_header.GetCPUType() != flag_CPUType)
+ continue;
+ if (mach_header.GetCPUSubtype() != flag_CPUSubtype)
+ continue;
+ }
+
+ mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK);
+
+ size_t size(_not(size_t)); {
+ _foreach (load_command, mach_header.GetLoadCommands()) {
+ uint32_t cmd(mach_header.Swap(load_command->cmd));
+ if (cmd == LC_CODE_SIGNATURE) {
+ struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
+ size = mach_header.Swap(signature->dataoff);
+ _assert(size < mach_header.GetSize());
+ break;
+ }
+ }
+
+ if (size == _not(size_t))
+ size = mach_header.GetSize();
+ }
+
+ allocations.push_back(CodesignAllocation(mach_header.GetCPUType(), mach_header.GetCPUSubtype(), size));
+ }
+ }
+
+ if (!allocations.empty()) {
+
+ pid_t pid = fork();
+ _syscall(pid);
+ if (pid == 0) {
+ // XXX: this leaks memory, but it doesn't really matter
+ std::vector<const char *> args;
+ char *arg;
+
+ args.push_back(allocate);
+
+ args.push_back("-i");
+ args.push_back(path);
+
+ _foreach (allocation, allocations) {
+ args.push_back("-A");
+
+ asprintf(&arg, "%u", allocation.type_);
+ args.push_back(arg);
+
+ asprintf(&arg, "%u", allocation.subtype_);
+ args.push_back(arg);
+
+ size_t alloc(0);
+ alloc += sizeof(struct SuperBlob);
+ uint32_t special(0);
+
+ special = std::max(special, CSSLOT_CODEDIRECTORY);
+ alloc += sizeof(struct BlobIndex);
+ alloc += sizeof(struct CodeDirectory);
+ alloc += strlen(base) + 1;
+
+ special = std::max(special, CSSLOT_REQUIREMENTS);
+ alloc += sizeof(struct BlobIndex);
+ alloc += 0xc;
+
+ if (xmld != NULL) {
+ special = std::max(special, CSSLOT_ENTITLEMENTS);
+ alloc += sizeof(struct BlobIndex);
+ alloc += sizeof(struct Blob);
+ alloc += xmls;
+ }
+
+ size_t normal((allocation.size_ + 0x1000 - 1) / 0x1000);
+ alloc += (special + normal) * 0x14;
+
+ alloc += 15;
+ alloc /= 16;
+ alloc *= 16;
+
+ asprintf(&arg, "%zu", alloc);
+ args.push_back(arg);
+ }
+
+ args.push_back("-o");
+ args.push_back(temp);
+
+ args.push_back(NULL);
+
+ if (false) {
+ printf("run:");
+ _foreach (arg, args)
+ printf(" %s", arg);
+ printf("\n");
+ }
+
+ execvp(allocate, (char **) &args[0]);
+ _assert(false);
+ }
+
+ int status;
+ _syscall(waitpid(pid, &status, 0));
+ _assert(WIFEXITED(status));
+ _assert(WEXITSTATUS(status) == 0);
+
+ }
+ }