85 lines
2.7 KiB
C++
85 lines
2.7 KiB
C++
//
|
|
// Created by selim on 27.11.22.
|
|
//
|
|
|
|
#include "Reader.h"
|
|
#include "elfio/elfio_dump.hpp"
|
|
|
|
#include <fstream>
|
|
#include <elf.h>
|
|
|
|
namespace Dart {
|
|
|
|
Reader::Reader(const fs::path& path) {
|
|
if(!fs::exists(path)) {
|
|
throw std::runtime_error("File does not exists");
|
|
}
|
|
|
|
if(!_elfio.load(path)) {
|
|
throw std::runtime_error("Failed to load ELF file");
|
|
}
|
|
|
|
auto size = fs::file_size(path);
|
|
_file = std::make_unique<std::byte[]>(size);
|
|
|
|
std::ifstream stream(path, std::ios::binary);
|
|
stream.read(reinterpret_cast<char*>(_file.get()), static_cast<std::streamsize>(size));
|
|
|
|
if(stream.bad()) {
|
|
throw std::runtime_error("Error reading ELF file");
|
|
}
|
|
}
|
|
|
|
void Reader::dumpInfo() const {
|
|
ELFIO::dump::header( std::cout, _elfio );
|
|
ELFIO::dump::section_headers( std::cout, _elfio );
|
|
ELFIO::dump::segment_headers( std::cout, _elfio );
|
|
ELFIO::dump::symbol_tables( std::cout, _elfio );
|
|
ELFIO::dump::notes( std::cout, _elfio );
|
|
ELFIO::dump::modinfo( std::cout, _elfio );
|
|
ELFIO::dump::dynamic_tags( std::cout, _elfio );
|
|
ELFIO::dump::section_datas( std::cout, _elfio );
|
|
ELFIO::dump::segment_datas( std::cout, _elfio );
|
|
}
|
|
|
|
const std::byte *Reader::getSnapshotPtr(DartSnapshot snapshot) const {
|
|
auto snapshotName = getSnapshotName(snapshot);
|
|
|
|
for(const auto& section: _elfio.sections) {
|
|
const ELFIO::symbol_section_accessor symbols(_elfio, section.get());
|
|
for(size_t i = 0; i < symbols.get_symbols_num(); ++i) {
|
|
std::string name;
|
|
Elf64_Addr address;
|
|
ELFIO::Elf_Xword size;
|
|
ELFIO::Elf_Half section_index;
|
|
unsigned char bind, type, other;
|
|
symbols.get_symbol(i, name, address, size, bind, type, section_index, other );
|
|
|
|
if(name == snapshotName) {
|
|
return _file.get() + address;
|
|
}
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
std::string Reader::getSnapshotName(DartSnapshot snapshot) {
|
|
switch (snapshot) {
|
|
case DartSnapshot::VM_INSTRUCTIONS: return "_kDartVmSnapshotInstructions";
|
|
case DartSnapshot::VM_DATA: return "_kDartVmSnapshotData";
|
|
case DartSnapshot::ISOLATE_INSTRUCTIONS: return "_kDartIsolateSnapshotInstructions";
|
|
case DartSnapshot::ISOLATE_DATA: return "_kDartIsolateSnapshotData";
|
|
case DartSnapshot::BUILD_ID: return "_kDartSnapshotBuildId";
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
Snapshot Reader::getSnapshot(DartSnapshot snapshot) const {
|
|
auto ptr = getSnapshotPtr(snapshot);
|
|
return Snapshot(ptr);
|
|
}
|
|
|
|
}
|