void printBinInfo(const char* path, const char* arch, const char* opt) { MachO* macho = MachO::readFile(path, arch, true); if (!macho) throw std::runtime_error("Load failed"); OpMode opmode = getOpMode(opt); std::cout << "Platform: " << macho->platform() << std::endl; switch (opmode) { case ModeDylibs: { std::cout << "Dylibs:\n"; for (const char* name : macho->dylibs()) { std::cout << "\t" << name << std::endl; } break; } case ModeSymbols: { std::cout << "Symbols:\n"; for (const MachO::Symbol& s : macho->symbols()) { if (!s.addr) continue; std::cout << '\t' << s.name << " at " << "[0x" << std::hex << std::setfill('0'); if (macho->is64()) std::cout << std::setw(16); else std::cout << std::setw(8); std::cout << s.addr << std::setw(0) << std::dec << ']' << std::endl; } break; } case ModeExports: { std::cout << "Exports:\n"; for (const MachO::Export* e : macho->exports()) { std::cout << '\t' << e->name << " at " << "[0x" << std::hex << std::setfill('0'); if (macho->is64()) std::cout << std::setw(16); else std::cout << std::setw(8); std::cout << e->addr << std::setw(0) << "] (flag: " << e->flag << ")" << std::dec << std::endl; } break; } case ModeBinds: { std::cout << "Binds:\n"; for (const MachO::Bind* b : macho->binds()) { std::cout << '\t' << b->name << " at " << "[0x" << std::hex << std::setfill('0'); if (macho->is64()) std::cout << std::setw(16); else std::cout << std::setw(8); std::cout << b->vmaddr << std::setw(0) << "] "; if (!b->is_classic) std::cout << "(addend: " << b->addend; else std::cout << "(value: " << b->value; std::cout << ", type: " << int(b->type) << ", ordinal: " << int(b->ordinal) << ")"; if (b->is_weak) std::cout << 'W'; if (b->is_classic) std::cout << 'C'; if (b->is_lazy) std::cout << 'L'; std::cout << std::dec << std::endl; } break; } case ModeSegments: { std::cout << "Segments:\n" << std::hex; printSegments(macho->segments64()); printSegments(macho->segments()); break; } case ModeRebases: { std::cout << "Rebases:\n"; for (MachO::Rebase* r : macho->rebases()) { std::cout << "\t at [0x" << std::hex << std::setfill('0'); if (macho->is64()) std::cout << std::setw(16); else std::cout << std::setw(8); std::cout << r->vmaddr << std::dec << std::setw(0) << "]\n"; } break; } case ModeRelocations: { std::cout << "External relocations:\n"; for (MachO::Relocation* r : macho->relocations()) { std::cout << '\t' << r->name << " at " << "[0x" << std::hex << std::setfill('0'); if (macho->is64()) std::cout << std::setw(16); else std::cout << std::setw(8); std::cout << r->addr << std::setw(0) << "] "; if (r->pcrel) std::cout << "PC-REL"; std::cout << std::endl; } break; } } delete macho; }
int main( int argc, char* argv[]) { // create some points in the plane Point p1(1,2), p2(4,2); Point p3(2,1), p4(4,3); Point p5(2,2), p6(6,2); Point p7(1,4), p8(2,3); Point p9(3,2), p10(5,0); // create some segments Segment s1(p1, p2); Segment s2(p3, p4); Segment s3(p5, p6); Segment s4(p7, p8); Segment s5(p9, p10); // test with predicate functions echo("*****************************************************************"); echo("Segment 1 and 2, they should intersect at point (3,2)"); printSegments(s1, s2); testForIntersectionAndPrint(s1, s2); doIntersectionAndPrint(s1, s2); newline(); echo("Segment 1 and 3, they should intersect at segment (2,2), (4,2)"); printSegments(s1, s3); testForIntersectionAndPrint(s1, s3); doIntersectionAndPrint(s1, s3); newline(); echo("Segment 1 and 4, they should not intersect"); printSegments(s1, s4); testForIntersectionAndPrint(s1, s4); doIntersectionAndPrint(s1, s4); newline(); echo("Segment 2 and 5, they should intersect at point (3,2)"); printSegments(s2, s5); testForIntersectionAndPrint(s2, s5); doIntersectionAndPrint(s2, s5); newline(); echo("Segment 2 and 2, they should intersect at segment (2,1), (4,3)"); printSegments(s2, s2); testForIntersectionAndPrint(s2, s2); doIntersectionAndPrint(s2, s2); newline(); // test with non-predicate functions echo("*****************************************************************"); echo("Segment 1 and 2, they should intersect at point (3,2)"); printSegments(s1, s2); testForIntersectionAndPrint2(s1, s2); doIntersectionAndPrint2(s1, s2); newline(); echo("Segment 1 and 3, they should intersect at segment (2,2), (4,2)"); printSegments(s1, s3); testForIntersectionAndPrint2(s1, s3); doIntersectionAndPrint2(s1, s3); newline(); echo("Segment 1 and 4, they should not intersect"); printSegments(s1, s4); testForIntersectionAndPrint2(s1, s4); doIntersectionAndPrint2(s1, s4); newline(); echo("Segment 2 and 5, they should intersect at point (3,2)"); printSegments(s2, s5); testForIntersectionAndPrint2(s2, s5); doIntersectionAndPrint2(s2, s5); newline(); echo("Segment 2 and 2, they should intersect at segment (2,1), (4,3)"); printSegments(s2, s2); testForIntersectionAndPrint2(s2, s2); doIntersectionAndPrint2(s2, s2); newline(); return 0; }