小标
2019-05-08
来源 :
阅读 1835
评论 0
摘要:本文主要向大家介绍了C/C++知识点之elf文件解析测试(cpp 版),通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之elf文件解析测试(cpp 版),通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

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> §ions);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> §ions) { printf("" [Nr] %-16s %-16s %-16s %s\n"", ""Name"", ""Type"", ""Address"", ""Offset""); printf("" %-16s %-16s %5s\n"", ""Size"", ""EntSize"", ""Align""); for (auto §ion : 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());
}
}本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号