+
+ std::vector<segment_command *> GetSegments(const char *segment_name) const {
+ std::vector<struct segment_command *> segment_commands;
+
+ _foreach (load_command, GetLoadCommands()) {
+ if (Swap(load_command->cmd) == LC_SEGMENT) {
+ segment_command *segment_command = reinterpret_cast<struct segment_command *>(load_command);
+ if (strncmp(segment_command->segname, segment_name, 16) == 0)
+ segment_commands.push_back(segment_command);
+ }
+ }
+
+ return segment_commands;
+ }
+
+ std::vector<segment_command_64 *> GetSegments64(const char *segment_name) const {
+ std::vector<struct segment_command_64 *> segment_commands;
+
+ _foreach (load_command, GetLoadCommands()) {
+ if (Swap(load_command->cmd) == LC_SEGMENT_64) {
+ segment_command_64 *segment_command = reinterpret_cast<struct segment_command_64 *>(load_command);
+ if (strncmp(segment_command->segname, segment_name, 16) == 0)
+ segment_commands.push_back(segment_command);
+ }
+ }
+
+ return segment_commands;
+ }
+
+ std::vector<section *> GetSections(const char *segment_name, const char *section_name) const {
+ std::vector<section *> sections;
+
+ _foreach (segment, GetSegments(segment_name)) {
+ section *section = (struct section *) (segment + 1);
+
+ uint32_t sect;
+ for (sect = 0; sect != Swap(segment->nsects); ++sect) {
+ if (strncmp(section->sectname, section_name, 16) == 0)
+ sections.push_back(section);
+ ++section;
+ }
+ }
+
+ return sections;
+ }
+
+ template <typename Target_>
+ Pointer<Target_> GetPointer(uint32_t address, const char *segment_name = NULL) const {
+ load_command *load_command = (struct load_command *) (mach_header_ + 1);
+ uint32_t cmd;
+
+ for (cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) {
+ if (Swap(load_command->cmd) == LC_SEGMENT) {
+ segment_command *segment_command = (struct segment_command *) load_command;
+ if (segment_name != NULL && strncmp(segment_command->segname, segment_name, 16) != 0)
+ goto next_command;
+
+ section *sections = (struct section *) (segment_command + 1);
+
+ uint32_t sect;
+ for (sect = 0; sect != Swap(segment_command->nsects); ++sect) {
+ section *section = §ions[sect];
+ //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
+ if (address >= Swap(section->addr) && address < Swap(section->addr) + Swap(section->size)) {
+ //printf("0x%.8x %s\n", address, segment_command->segname);
+ return Pointer<Target_>(this, reinterpret_cast<Target_ *>(address - Swap(section->addr) + Swap(section->offset) + (char *) mach_header_));
+ }
+ }
+ }
+
+ next_command:
+ load_command = (struct load_command *) ((char *) load_command + Swap(load_command->cmdsize));
+ }
+
+ return Pointer<Target_>(this);
+ }
+
+ template <typename Target_>
+ Pointer<Target_> GetOffset(uint32_t offset) {
+ return Pointer<Target_>(this, reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_));
+ }
+};
+
+class FatMachHeader :
+ public MachHeader
+{
+ private:
+ fat_arch *fat_arch_;
+
+ public:
+ FatMachHeader(void *base, size_t size, fat_arch *fat_arch) :
+ MachHeader(base, size),
+ fat_arch_(fat_arch)
+ {
+ }
+
+ fat_arch *GetFatArch() const {
+ return fat_arch_;
+ }
+};
+
+class FatHeader :
+ public Data
+{
+ private:
+ fat_header *fat_header_;
+ std::vector<FatMachHeader> mach_headers_;
+
+ public:
+ FatHeader(void *base, size_t size) :
+ Data(base, size)
+ {
+ fat_header_ = reinterpret_cast<struct fat_header *>(base);
+
+ if (Swap(fat_header_->magic) == FAT_CIGAM) {
+ swapped_ = !swapped_;
+ goto fat;
+ } else if (Swap(fat_header_->magic) != FAT_MAGIC) {
+ fat_header_ = NULL;
+ mach_headers_.push_back(FatMachHeader(base, size, NULL));
+ } else fat: {
+ size_t fat_narch = Swap(fat_header_->nfat_arch);
+ fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header_ + 1);
+ size_t arch;
+ for (arch = 0; arch != fat_narch; ++arch) {
+ uint32_t arch_offset = Swap(fat_arch->offset);
+ uint32_t arch_size = Swap(fat_arch->size);
+ mach_headers_.push_back(FatMachHeader((uint8_t *) base + arch_offset, arch_size, fat_arch));
+ ++fat_arch;
+ }
+ }
+ }
+
+ std::vector<FatMachHeader> &GetMachHeaders() {
+ return mach_headers_;
+ }
+
+ bool IsFat() const {
+ return fat_header_ != NULL;
+ }
+
+ struct fat_header *operator ->() const {
+ return fat_header_;
+ }
+
+ operator struct fat_header *() const {
+ return fat_header_;
+ }