Skip to content

Commit b000c38

Browse files
committed
elf/macho: modernize Binary::print
1 parent a8b11cf commit b000c38

File tree

2 files changed

+216
-130
lines changed

2 files changed

+216
-130
lines changed

src/ELF/Binary.cpp

Lines changed: 69 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,145 +3312,103 @@ bool Binary::remove_version_requirement(const std::string& libname) {
33123312
}
33133313

33143314
std::ostream& Binary::print(std::ostream& os) const {
3315+
using namespace fmt;
3316+
os << "Header {\n" << indent(LIEF::to_string(header()), 2) << "}\n";
33153317

3316-
os << "Header" << '\n';
3317-
os << "======" << '\n';
3318-
3319-
os << header();
3320-
os << '\n';
3321-
3322-
3323-
os << "Sections" << '\n';
3324-
os << "========" << '\n';
3325-
for (const Section& section : sections()) {
3326-
os << section << '\n';
3318+
{
3319+
const auto secs = sections();
3320+
if (!secs.empty()) {
3321+
os << fmt::format("Sections (#{})\n", secs.size());
3322+
for (size_t i = 0; i < secs.size(); ++i) {
3323+
os << fmt::format(" Section #{:02} {{\n", i)
3324+
<< indent(LIEF::to_string(secs[i]), 4) << " }\n";
3325+
}
3326+
}
33273327
}
3328-
os << '\n';
3329-
33303328

3331-
os << "Segments" << '\n';
3332-
os << "========" << '\n';
3333-
for (const Segment& segment : segments()) {
3334-
os << segment << '\n';
3329+
{
3330+
const auto segs = segments();
3331+
if (!segs.empty()) {
3332+
os << fmt::format("Segments (#{})\n", segs.size());
3333+
for (size_t i = 0; i < segs.size(); ++i) {
3334+
os << fmt::format(" Segment #{:02} {{\n", i)
3335+
<< indent(LIEF::to_string(segs[i]), 4) << " }\n";
3336+
}
3337+
}
33353338
}
33363339

3337-
os << '\n';
3338-
3339-
3340-
os << "Dynamic entries" << '\n';
3341-
os << "===============" << '\n';
3342-
3343-
for (const DynamicEntry& entry : dynamic_entries()) {
3344-
os << entry << '\n';
3340+
if (auto entries = dynamic_entries(); !entries.empty()) {
3341+
os << fmt::format("Dynamic Entries (#{})\n", entries.size());
3342+
for (const DynamicEntry& entry : entries) {
3343+
os << " " << entry << '\n';
3344+
}
33453345
}
33463346

3347-
os << '\n';
3348-
3349-
3350-
os << "Dynamic symbols" << '\n';
3351-
os << "===============" << '\n';
3352-
3353-
for (const Symbol& symbol : dynamic_symbols()) {
3354-
os << symbol << '\n';
3347+
if (auto syms = dynamic_symbols(); !syms.empty()) {
3348+
os << fmt::format("Dynamic Symbols (#{})\n", syms.size());
3349+
for (const Symbol& symbol : syms) {
3350+
os << " " << symbol << '\n';
3351+
}
33553352
}
33563353

3357-
os << '\n';
3358-
3359-
3360-
os << "Symtab symbols" << '\n';
3361-
os << "==============" << '\n';
3362-
3363-
for (const Symbol& symbol : symtab_symbols()) {
3364-
os << symbol << '\n';
3354+
if (auto syms = symtab_symbols(); !syms.empty()) {
3355+
os << fmt::format("Symtab Symbols (#{})\n", syms.size());
3356+
for (const Symbol& symbol : syms) {
3357+
os << " " << symbol << '\n';
3358+
}
33653359
}
33663360

3367-
os << '\n';
3368-
3369-
3370-
os << "Symbol versions" << '\n';
3371-
os << "===============" << '\n';
3372-
3373-
for (const SymbolVersion& sv : symbols_version()) {
3374-
os << sv << '\n';
3361+
if (auto vers = symbols_version(); !vers.empty()) {
3362+
os << fmt::format("Symbol Versions (#{})\n", vers.size());
3363+
for (const SymbolVersion& sv : vers) {
3364+
os << " " << sv << '\n';
3365+
}
33753366
}
33763367

3377-
os << '\n';
3378-
3379-
3380-
os << "Symbol versions definition" << '\n';
3381-
os << "==========================" << '\n';
3382-
3383-
for (const SymbolVersionDefinition& svd : symbols_version_definition()) {
3384-
os << svd << '\n';
3368+
if (auto defs = symbols_version_definition(); !defs.empty()) {
3369+
os << fmt::format("Symbol Version Definitions (#{})\n", defs.size());
3370+
for (const SymbolVersionDefinition& svd : defs) {
3371+
os << indent(LIEF::to_string(svd), 2);
3372+
}
33853373
}
33863374

3387-
os << '\n';
3388-
3389-
3390-
os << "Symbol version requirement" << '\n';
3391-
os << "==========================" << '\n';
3392-
3393-
for (const SymbolVersionRequirement& svr : symbols_version_requirement()) {
3394-
os << svr << '\n';
3375+
if (auto reqs = symbols_version_requirement(); !reqs.empty()) {
3376+
os << fmt::format("Symbol Version Requirements (#{})\n", reqs.size());
3377+
for (const SymbolVersionRequirement& svr : reqs) {
3378+
os << indent(LIEF::to_string(svr), 2);
3379+
}
33953380
}
33963381

3397-
os << '\n';
3398-
3399-
3400-
os << "Dynamic relocations" << '\n';
3401-
os << "===================" << '\n';
3402-
3403-
for (const Relocation& relocation : dynamic_relocations()) {
3404-
os << relocation << '\n';
3382+
if (auto relocs = dynamic_relocations(); !relocs.empty()) {
3383+
os << fmt::format("Dynamic Relocations (#{})\n", relocs.size());
3384+
for (const Relocation& relocation : relocs) {
3385+
os << " " << relocation << '\n';
3386+
}
34053387
}
34063388

3407-
os << '\n';
3408-
3409-
3410-
os << ".plt.got relocations" << '\n';
3411-
os << "====================" << '\n';
3412-
3413-
for (const Relocation& relocation : pltgot_relocations()) {
3414-
os << relocation << '\n';
3389+
if (auto relocs = pltgot_relocations(); !relocs.empty()) {
3390+
os << fmt::format(".plt.got Relocations (#{})\n", relocs.size());
3391+
for (const Relocation& relocation : relocs) {
3392+
os << " " << relocation << '\n';
3393+
}
34153394
}
34163395

3417-
os << '\n';
3418-
3419-
if (notes().size() > 0) {
3420-
os << "Notes" << '\n';
3421-
os << "=====" << '\n';
3422-
3423-
it_const_notes notes = this->notes();
3424-
for (size_t i = 0; i < notes.size(); ++i) {
3425-
std::string title = "Note #" + std::to_string(i);
3426-
os << title << '\n';
3427-
os << std::string(title.size(), '-') << '\n';
3428-
os << notes[i] << '\n';
3396+
if (auto n = this->notes(); !n.empty()) {
3397+
os << fmt::format("Notes (#{})\n", n.size());
3398+
for (size_t i = 0; i < n.size(); ++i) {
3399+
os << fmt::format(" Note #{:02} {{\n", i)
3400+
<< indent(LIEF::to_string(n[i]), 4) << " }\n";
34293401
}
3430-
os << '\n';
34313402
}
34323403

3433-
os << '\n';
3434-
if (use_gnu_hash()) {
3435-
os << "GNU Hash Table" << '\n';
3436-
os << "==============" << '\n';
3437-
3438-
os << gnu_hash() << '\n';
3439-
3440-
os << '\n';
3404+
if (const GnuHash* gnu = gnu_hash()) {
3405+
os << "GNU Hash Table {\n" << indent(LIEF::to_string(*gnu), 2) << "}\n";
34413406
}
34423407

3443-
3444-
if (use_sysv_hash()) {
3445-
os << "SYSV Hash Table" << '\n';
3446-
os << "===============" << '\n';
3447-
3448-
os << sysv_hash() << '\n';
3449-
3450-
os << '\n';
3408+
if (const SysvHash* sysv = sysv_hash()) {
3409+
os << "SYSV Hash Table {\n" << indent(LIEF::to_string(*sysv), 2) << "}\n";
34513410
}
34523411

3453-
34543412
return os;
34553413
}
34563414

src/MachO/Binary.cpp

Lines changed: 147 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,36 +2644,164 @@ bool Binary::can_cache_segment(const SegmentCommand& segment) {
26442644
Binary::~Binary() = default;
26452645

26462646
std::ostream& Binary::print(std::ostream& os) const {
2647-
os << "Header" << '\n';
2648-
os << "======" << '\n';
2647+
using namespace fmt;
2648+
os << "Header {\n" << indent(LIEF::to_string(header()), 2) << "}\n";
26492649

2650-
os << header();
2651-
os << '\n';
2650+
if (auto cmds = commands(); !cmds.empty()) {
2651+
os << fmt::format("Commands (#{})\n", cmds.size());
2652+
for (size_t i = 0; i < cmds.size(); ++i) {
2653+
os << fmt::format(" Command #{:02} {{\n", i)
2654+
<< indent(LIEF::to_string(cmds[i]), 4) << " }\n";
2655+
}
2656+
}
26522657

2658+
{
2659+
const auto segs = segments();
2660+
if (!segs.empty()) {
2661+
os << fmt::format("Segments (#{})\n", segs.size());
2662+
for (size_t i = 0; i < segs.size(); ++i) {
2663+
os << fmt::format(" Segment #{:02} {{\n", i)
2664+
<< indent(LIEF::to_string(segs[i]), 4) << " }\n";
2665+
}
2666+
}
2667+
}
26532668

2654-
os << "Commands" << '\n';
2655-
os << "========" << '\n';
2656-
for (const LoadCommand& cmd : commands()) {
2657-
os << cmd << '\n';
2669+
{
2670+
const auto secs = sections();
2671+
if (!secs.empty()) {
2672+
os << fmt::format("Sections (#{})\n", secs.size());
2673+
for (size_t i = 0; i < secs.size(); ++i) {
2674+
os << fmt::format(" Section #{:02} {{\n", i)
2675+
<< indent(LIEF::to_string(secs[i]), 4) << " }\n";
2676+
}
2677+
}
26582678
}
26592679

2660-
os << '\n';
2680+
if (auto libs = libraries(); !libs.empty()) {
2681+
os << fmt::format("Libraries (#{})\n", libs.size());
2682+
for (const DylibCommand& lib : libs) {
2683+
os << " " << lib << '\n';
2684+
}
2685+
}
26612686

2662-
os << "Sections" << '\n';
2663-
os << "========" << '\n';
2664-
for (const Section& section : sections()) {
2665-
os << section << '\n';
2687+
if (auto syms = symbols(); !syms.empty()) {
2688+
os << fmt::format("Symbols (#{})\n", syms.size());
2689+
for (const Symbol& symbol : syms) {
2690+
os << " " << symbol << '\n';
2691+
}
2692+
}
2693+
2694+
if (auto syms = exported_symbols(); !syms.empty()) {
2695+
os << fmt::format("Exported Symbols (#{})\n", syms.size());
2696+
for (const Symbol& symbol : syms) {
2697+
os << " " << symbol << '\n';
2698+
}
2699+
}
2700+
2701+
if (auto syms = imported_symbols(); !syms.empty()) {
2702+
os << fmt::format("Imported Symbols (#{})\n", syms.size());
2703+
for (const Symbol& symbol : syms) {
2704+
os << " " << symbol << '\n';
2705+
}
2706+
}
2707+
2708+
if (auto relocs = relocations(); !relocs.empty()) {
2709+
os << fmt::format("Relocations (#{})\n", relocs.size());
2710+
for (const Relocation& R : relocs) {
2711+
os << " " << R << '\n';
2712+
}
2713+
}
2714+
2715+
if (auto paths = rpaths(); !paths.empty()) {
2716+
os << fmt::format("RPaths (#{})\n", paths.size());
2717+
for (const RPathCommand& rp : paths) {
2718+
os << " " << rp << '\n';
2719+
}
2720+
}
2721+
2722+
if (auto n = notes(); !n.empty()) {
2723+
os << fmt::format("Notes (#{})\n", n.size());
2724+
for (size_t i = 0; i < n.size(); ++i) {
2725+
os << fmt::format(" Note #{:02} {{\n", i)
2726+
<< indent(LIEF::to_string(n[i]), 4) << " }\n";
2727+
}
2728+
}
2729+
2730+
if (auto fsets = filesets(); !fsets.empty()) {
2731+
os << fmt::format("Filesets (#{})\n", fsets.size());
2732+
for (const Binary& fs : fsets) {
2733+
os << indent(LIEF::to_string(fs), 2);
2734+
}
2735+
}
2736+
2737+
if (const UUIDCommand* cmd = uuid()) {
2738+
os << "UUID {\n" << indent(LIEF::to_string(*cmd), 2) << "}\n";
26662739
}
26672740

2668-
os << '\n';
2741+
if (const MainCommand* cmd = main_command()) {
2742+
os << "Main Command {\n" << indent(LIEF::to_string(*cmd), 2) << "}\n";
2743+
}
2744+
2745+
if (const DylinkerCommand* cmd = dylinker()) {
2746+
os << "Dylinker {\n" << indent(LIEF::to_string(*cmd), 2) << "}\n";
2747+
}
2748+
2749+
if (const DyldInfo* info = dyld_info()) {
2750+
os << "Dyld Info {\n" << indent(LIEF::to_string(*info), 2) << "}\n";
2751+
}
2752+
2753+
if (const FunctionStarts* fs = function_starts()) {
2754+
os << "Function Starts {\n" << indent(LIEF::to_string(*fs), 2) << "}\n";
2755+
}
2756+
2757+
if (const SourceVersion* sv = source_version()) {
2758+
os << "Source Version {\n" << indent(LIEF::to_string(*sv), 2) << "}\n";
2759+
}
2760+
2761+
if (const BuildVersion* bv = build_version()) {
2762+
os << "Build Version {\n" << indent(LIEF::to_string(*bv), 2) << "}\n";
2763+
}
2764+
2765+
if (const VersionMin* vm = version_min()) {
2766+
os << "Version Min {\n" << indent(LIEF::to_string(*vm), 2) << "}\n";
2767+
}
2768+
2769+
if (const CodeSignature* cs = code_signature()) {
2770+
os << "Code Signature {\n" << indent(LIEF::to_string(*cs), 2) << "}\n";
2771+
}
2772+
2773+
if (const CodeSignatureDir* csd = code_signature_dir()) {
2774+
os << "Code Signature Dir {\n" << indent(LIEF::to_string(*csd), 2) << "}\n";
2775+
}
2776+
2777+
if (const DataInCode* dic = data_in_code()) {
2778+
os << "Data In Code {\n" << indent(LIEF::to_string(*dic), 2) << "}\n";
2779+
}
2780+
2781+
if (const DyldChainedFixups* dcf = dyld_chained_fixups()) {
2782+
os << "Dyld Chained Fixups {\n" << indent(LIEF::to_string(*dcf), 2) << "}\n";
2783+
}
2784+
2785+
if (const DyldExportsTrie* det = dyld_exports_trie()) {
2786+
os << "Dyld Exports Trie {\n" << indent(LIEF::to_string(*det), 2) << "}\n";
2787+
}
2788+
2789+
if (const EncryptionInfo* ei = encryption_info()) {
2790+
os << "Encryption Info {\n" << indent(LIEF::to_string(*ei), 2) << "}\n";
2791+
}
2792+
2793+
if (const TwoLevelHints* tlh = two_level_hints()) {
2794+
os << "Two Level Hints {\n" << indent(LIEF::to_string(*tlh), 2) << "}\n";
2795+
}
2796+
2797+
if (const SubFramework* sf = sub_framework()) {
2798+
os << "Sub Framework {\n" << indent(LIEF::to_string(*sf), 2) << "}\n";
2799+
}
26692800

2670-
os << "Symbols" << '\n';
2671-
os << "=======" << '\n';
2672-
for (const Symbol& symbol : symbols()) {
2673-
os << symbol << '\n';
2801+
if (const DyldEnvironment* de = dyld_environment()) {
2802+
os << "Dyld Environment {\n" << indent(LIEF::to_string(*de), 2) << "}\n";
26742803
}
26752804

2676-
os << '\n';
26772805
return os;
26782806
}
26792807

0 commit comments

Comments
 (0)