Files
calculet-npu-research-archive/source/baseline-expanded/calrt-sdk-0.7.6/usr/local/include/calrt/elf_parser.hpp
T

82 lines
2.3 KiB
C++

#pragma once
#include <cstring>
#include <cstdint>
#include <vector>
#include <map>
namespace calc_efl
{
enum class LoadFlag : uint32_t
{
DO_NOT_LOAD = 0,
BY_SECTION = 1,
BY_PT_LOAD
};
struct Parsed_Elf_Section_s
{
char name[64];
uint32_t type;
uint64_t size; // size after padding if in need
uint64_t address;
uint64_t offset = 0; // indicate the raw efficient data index in image
std::vector<uint8_t> data;
};
// struct Parsed_shared_ph_s
// {
// uint32_t p_type;
// uint32_t p_flgas;
// uint64_t p_vaddr;
// uint64_t p_filesz;
// };
struct Parsed_Elf_s
{
uint16_t id;
uint16_t e_type;
uint64_t e_entry;
uint64_t progBitSize = 0; // only record max progbit type siz (cross all elf under the same model)
uint64_t f_addr = 0; // indicate the first valid section address. For relocation usage
char path[256];
char postfix[128]; // if is same chip ccu
// std::map<uint64_t, Parsed_shared_ph_s> phs;
std::map<uint64_t, Parsed_Elf_Section_s> sections; // [address, section]
bool operator ==(const Parsed_Elf_s &other) const
{
bool isSamePost = std::strcmp(postfix, other.postfix) == 0;
bool isSamePath = std::strcmp(path, other.path) == 0;
return (isSamePost && isSamePath);
}
};
/**
* @brief intelligent parse method
*
* @param path ELF path
* @return std::pair<uint64_t, std::vector<uint8_t>> entry addr and datas
*/
std::pair<uint64_t, std::vector<uint8_t>> ParseElf_Intell(const char *path);
/**
* @brief naively parse metho
*
* @param path elf path
* @param isRT is it for calrt purpose (default: false)
* @return Parsed_Elf_s
*/
Parsed_Elf_s ParseElf_Naive(const char *path, LoadFlag lFlag = LoadFlag::BY_SECTION);
Parsed_Elf_s ParseElf_by_PT_LOAD(const char *path);
/**
* @brief relocate elf
*
* @param elf_mirror data struct get from ParseElf
* @param path elf file path
* @param loadBase new allocated address
*/
void RelocateSection(uint8_t *imageElf, const char *path, uint64_t loadBase, const Parsed_Elf_s &parsed_elf);
}