this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Easier to use support for FAT bineries in motool

+69 -13
+69 -13
src/motool/motool.cpp
··· 35 35 void printBinInfo(const char* path, const char* arch, const char* opt); 36 36 OpMode getOpMode(const char* opt); 37 37 std::string protString(int prot); 38 + void processArchArgument(std::string& fileName, std::string& arch); 39 + std::string autoSelectArchitecture(const std::map<std::string, fat_arch>& archs); 40 + 41 + const char* g_archPrio[] = 42 + #ifdef __x86_64__ 43 + { "x86-64", "x86", nullptr } 44 + #elif defined(__i386__) 45 + { "x86", "x86-64", nullptr } 46 + #elif defined(__ppc__) 47 + { "ppc", nullptr } 48 + #elif defined(__ppc64__) 49 + { "ppc64", nullptr } 50 + #elif defined (__arm__) 51 + { "arm", nullptr } 52 + #endif 53 + ; 38 54 39 55 int main(int argc, char** argv) 40 56 { 41 57 if (argc < 2) 42 58 { 43 59 std::cerr << "Mach Object File Format (Mach-O) Tool\n"; 44 - std::cerr << "Usage: " << argv[0] << " <file> [arch] [option]\n"; 60 + std::cerr << "Usage: " << argv[0] << " <file>[:arch] [option]\n"; 45 61 std::cerr << "\nOptions:\n" 46 62 "\t-d --dylibs\tList dylibs (default)\n" 47 63 "\t-s --symbols\tList symbols\n" 48 64 "\t-e --exports\tList exports\n" 49 65 "\t-b --binds\tList binds\n" 50 66 "\t-g --segments\tList segments and sections\n" 67 + "\t-f --fat\tList FAT Mach-O architectures\n" 51 68 "\n"; 52 69 return 1; 53 70 } 54 71 55 72 try 56 73 { 57 - int fd = ::open(argv[1], O_RDONLY); 74 + std::string fileName = argv[1]; 75 + std::string arch; 76 + 77 + processArchArgument(fileName, arch); 78 + 79 + int fd = ::open(fileName.c_str(), O_RDONLY); 58 80 if (fd == -1) 59 81 throw std::runtime_error("Cannot open file"); 60 82 61 - std::cout << "Processing file: " << argv[1] << std::endl; 83 + std::cout << "Processing file: " << fileName << std::endl; 62 84 63 85 std::map<std::string, fat_arch> archs; 64 86 if (FatMachO::readFatInfo(fd, &archs)) 65 87 { 66 - if (argc >= 3) 67 - { 68 - const char* opt = 0; 69 - if (argc >= 4) 70 - opt = argv[3]; 71 - printBinInfo(argv[1], argv[2], opt); 72 - } 73 - else 88 + if (archs.empty()) 89 + throw std::runtime_error("FAT Mach-O file contains no subfiles!"); 90 + if (arch.empty()) 91 + arch = autoSelectArchitecture(archs); 92 + 93 + std::cout << "Selected architecture: " << arch << std::endl; 94 + 95 + if (argc >= 3 && ( !strcmp(argv[2], "-f") || !strcmp(argv[2], "--fat"))) 74 96 { 75 97 // print fat info 76 98 std::cout << "Fat file architectures:\n"; ··· 79 101 { 80 102 std::cout << "\t" << it->first << std::endl; 81 103 } 82 - std::cout << "\nPass the architecture name as the second argument to get more info on that subfile.\n"; 104 + } 105 + else 106 + { 107 + const char* opt = 0; 108 + if (argc >= 3) 109 + opt = argv[2]; 110 + printBinInfo(fileName.c_str(), arch.c_str(), opt); 83 111 } 84 112 } 85 113 else ··· 87 115 const char* opt = 0; 88 116 if (argc >= 3) 89 117 opt = argv[2]; 90 - printBinInfo(argv[1], 0, opt); 118 + printBinInfo(fileName.c_str(), 0, opt); 91 119 } 92 120 93 121 ::close(fd); ··· 239 267 return rv; 240 268 } 241 269 270 + void processArchArgument(std::string& fileName, std::string& arch) 271 + { 272 + size_t p = fileName.find(':'); 273 + if (p != std::string::npos) 274 + { 275 + arch = fileName.substr(p+1); 276 + fileName.resize(p); 277 + } 278 + } 279 + 280 + std::string autoSelectArchitecture(const std::map<std::string, fat_arch>& archs) 281 + { 282 + std::string choice; 283 + for (int i = 0; g_archPrio[i] != nullptr; i++) 284 + { 285 + if (archs.find(g_archPrio[i]) != archs.end()) 286 + { 287 + choice = g_archPrio[i]; 288 + break; 289 + } 290 + } 291 + 292 + if (choice.empty()) 293 + choice = archs.begin()->first; 294 + 295 + return choice; 296 + } 297 +