elf文件解析测试(cpp 版)

relocations.cpp

#include <iostream>
#include <inttypes.h> // PRIx64 
#include "../elf_parser.hpp"

void print_relocations(std::vector<elf_parser::relocation_t> &relocations);

int main(int argc, char* argv[]) {
    char usage_banner[] = "usage: ./sections [<executable>]\n";
    if(argc < 2) {
        std::cerr << usage_banner;
        return -1;
    }

    std::string program((std::string)argv[1]);
    elf_parser::Elf_parser elf_parser(program);

    std::vector<elf_parser::relocation_t> relocs = elf_parser.get_relocations();
    print_relocations(relocs);
    return 0;
}

void print_relocations(std::vector<elf_parser::relocation_t> &relocations) {
    printf("  [*] %-16s %-16s %-16s %-16s %s\n", "Offset", "Info", "Type", "Sym. Value", "Sym. Name (section)");
    printf("  [*] %-16s\n", "Calculated PLT Address");
    for (auto &rel : relocations) {
        printf("%016" PRIx64 "  %016" PRIx64 " %-16s %016" PRIx64 " %-16s (%s)\n",
            rel.relocation_offset,
            rel.relocation_info,
            rel.relocation_type.c_str(),
            rel.relocation_symbol_value,
            rel.relocation_symbol_name.c_str(),
            rel.relocation_section_name.c_str());
        printf("%016" PRIx64 "\n", rel.relocation_plt_address);
    }
}

sections.cpp

#include <iostream>
#include <inttypes.h> // PRIx64 
#include "../elf_parser.hpp"

void print_sections(std::vector<elf_parser::section_t> &sections);

int main(int argc, char* argv[]) {
    char usage_banner[] = "usage: ./sections [<executable>]\n";
    if(argc < 2) {
        std::cerr << usage_banner;
        return -1;
    }

    std::string program((std::string)argv[1]);
    elf_parser::Elf_parser elf_parser(program);

    std::vector<elf_parser::section_t> secs = elf_parser.get_sections();
    print_sections(secs);
    return 0;
}

void print_sections(std::vector<elf_parser::section_t> &sections) {
    printf("  [Nr] %-16s %-16s %-16s %s\n", "Name", "Type", "Address", "Offset");
    printf("       %-16s %-16s %5s\n",
                    "Size", "EntSize", "Align");

    for (auto &section : sections) {
        printf("  [%2d] %-16s %-16s %016" PRIx64 " %08" PRIx64 "\n", 
            section.section_index,
            section.section_name.c_str(),
            section.section_type.c_str(),
            section.section_addr, 
            section.section_offset);

        printf("       %016zx %016" PRIx64 " %5" PRIu64 "\n",
            section.section_size, 
            section.section_ent_size,
            section.section_addr_align);
    }
}

segments.cpp

#include <iostream>
#include <inttypes.h> // PRIx64 
#include "../elf_parser.hpp"

void print_segments(std::vector<elf_parser::segment_t> &segments);

int main(int argc, char* argv[]) {
    char usage_banner[] = "usage: ./sections [<executable>]\n";
    if(argc < 2) {
        std::cerr << usage_banner;
        return -1;
    }

    std::string program((std::string)argv[1]);
    elf_parser::Elf_parser elf_parser(program);

    std::vector<elf_parser::segment_t> segs = elf_parser.get_segments();
    print_segments(segs);
    return 0;
}

void print_segments(std::vector<elf_parser::segment_t> &segments) {
    printf("  %-16s  %-16s   %-16s   %s\n", "Type", "Offset", "VirtAddr", "PhysAddr");
    printf("  %-16s  %-16s   %-16s  %6s %5s\n", " ", "FileSiz", "MemSiz", "Flags", "Align");

    for (auto &segment : segments) {
        printf("   %-16s 0x%016" PRIx64 " 0x%016" PRIx64 " 0x%016" PRIx64 "\n",
                segment.segment_type.c_str(), 
                segment.segment_offset,
                segment.segment_virtaddr, 
                segment.segment_physaddr);

        printf("   %-16s 0x%016" PRIx64 " 0x%016" PRIx64 " %-5s %-5" PRIx64 "\n", "",
                segment.segment_filesize, 
                segment.segment_memsize,
                segment.segment_flags.c_str(), 
                segment.segment_align);
    }
}

symbols.cpp

#include <iostream>
#include <inttypes.h> // PRIx64 
#include "../elf_parser.hpp"

void print_symbols(std::vector<elf_parser::symbol_t> &symbols);

int main(int argc, char* argv[]) {
    char usage_banner[] = "usage: ./sections [<executable>]\n";
    if(argc < 2) {
        std::cerr << usage_banner;
        return -1;
    }

    std::string program((std::string)argv[1]);
    elf_parser::Elf_parser elf_parser(program);

    std::vector<elf_parser::symbol_t> syms = elf_parser.get_symbols();
    print_symbols(syms);
    return 0;
}

void print_symbols(std::vector<elf_parser::symbol_t> &symbols) {
    printf("Num:    Value  Size Type    Bind   Vis      Ndx Name\n");
    for (auto &symbol : symbols) {
        printf("%-3d: %08" PRIx64 "  %-4d %-8s %-7s %-9s %-3s %s(%s)\n",
                symbol.symbol_num, 
                symbol.symbol_value,
                symbol.symbol_size, 
                symbol.symbol_type.c_str(),
                symbol.symbol_bind.c_str(),
                symbol.symbol_visibility.c_str(),
                symbol.symbol_index.c_str(),
                symbol.symbol_name.c_str(),
                symbol.symbol_section.c_str());     
    }
}

转载于:https://blog.51cto.com/haidragon/2134235

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值