This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
645 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#include <elf.h> | ||
#include <link.h> | ||
|
||
#include <fstream> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "logger/logger.h" | ||
#include "macro.h" | ||
|
||
namespace hook { | ||
|
||
class HOOK_API CachedSymbolTable { | ||
public: | ||
struct StringRefIterator { | ||
StringRefIterator(const char *str); | ||
|
||
StringRefIterator &operator++() &; | ||
StringRefIterator operator++(int) &; | ||
|
||
adt::StringRef operator*(); | ||
|
||
bool operator==(const StringRefIterator &other) const; | ||
bool operator!=(const StringRefIterator &other) const; | ||
const void *data() const; | ||
|
||
private: | ||
const char *str_; | ||
}; | ||
|
||
template <typename T> | ||
struct OwnerBuf { | ||
OwnerBuf() = default; | ||
OwnerBuf(const OwnerBuf &other) = delete; | ||
OwnerBuf &operator=(const OwnerBuf &other) = delete; | ||
|
||
OwnerBuf(OwnerBuf &&other) { *this = std::move(other); } | ||
OwnerBuf &operator=(OwnerBuf &&other) { | ||
std::swap(buf_, other.buf_); | ||
std::swap(size_, other.size_); | ||
return *this; | ||
} | ||
|
||
static OwnerBuf alloc(size_t size) { | ||
OwnerBuf buf; | ||
buf.buf_ = reinterpret_cast<T *>(malloc(size * sizeof(T))); | ||
buf.size_ = size; | ||
return buf; | ||
} | ||
|
||
T &operator[](size_t index) { | ||
assert(index < size_); | ||
assert(buf_); | ||
return buf_[index]; | ||
} | ||
|
||
T *data() const { return buf_; } | ||
|
||
size_t size() const { return size_; } | ||
|
||
std::tuple<T *, size_t> release() { | ||
auto result = std::make_tuple(buf_, size_); | ||
buf_ = 0; | ||
size_ = 0; | ||
return result; | ||
} | ||
|
||
template <typename N> | ||
friend class OwnerBuf; | ||
|
||
template <typename N> | ||
OwnerBuf<N> cast() { | ||
OwnerBuf<N> result; | ||
auto [buf, size] = release(); | ||
result.buf_ = reinterpret_cast<N *>(buf); | ||
result.size_ = size * sizeof(T) / sizeof(N); | ||
return result; | ||
} | ||
|
||
~OwnerBuf() { | ||
if (buf_) { | ||
free(buf_); | ||
#ifndef NDEBUG | ||
buf_ = nullptr; | ||
size_ = 0; | ||
#endif | ||
} | ||
} | ||
|
||
private: | ||
T *buf_ = nullptr; | ||
size_t size_ = 0; | ||
}; | ||
|
||
StringRefIterator strtab_begin(const char *str) const; | ||
|
||
StringRefIterator strtab_end(const char *str) const; | ||
|
||
std::tuple<StringRefIterator, StringRefIterator> strtab_range( | ||
const char *str, size_t size) const { | ||
return std::make_tuple(strtab_begin(str), strtab_begin(str + size)); | ||
} | ||
|
||
CachedSymbolTable(const std::string &name, const void *base_address, | ||
const std::vector<std::string> §ion_names = {}); | ||
|
||
void move_to_section_header(); | ||
|
||
void move_to_section_header(size_t index); | ||
|
||
adt::StringRef getSectionName(size_t index) const; | ||
size_t find_section(adt::StringRef name) const; | ||
|
||
void load_symbol_table(); | ||
void parse_section_header(); | ||
void parse_named_section(); | ||
|
||
template <typename T> | ||
OwnerBuf<T> load_section_data(adt::StringRef name) { | ||
auto buf = load_section_data(name); | ||
return buf.cast<T>(); | ||
} | ||
|
||
OwnerBuf<char> load_section_data(adt::StringRef name); | ||
|
||
const std::string &lookUpSymbol(const void *func) const; | ||
|
||
const std::unordered_map<size_t, std::string> &getSymbolTable() const { | ||
return symbol_table; | ||
} | ||
|
||
size_t min_addrtess() const { return min_address_; } | ||
size_t max_addrtess() const { return max_address_; } | ||
|
||
const std::vector<ElfW(Shdr)> §ions() const { return sections_; } | ||
|
||
private: | ||
std::string libName; | ||
std::ifstream ifs; | ||
ElfW(Ehdr) elf_header; | ||
std::vector<char> section_header_str; | ||
std::vector<ElfW(Shdr)> sections_; | ||
std::unordered_map<size_t, std::string> symbol_table; | ||
const void *base_address; | ||
size_t min_address_ = -1; | ||
size_t max_address_ = 0; | ||
std::vector<std::string> section_names; | ||
}; | ||
|
||
CachedSymbolTable *createSymbolTable(const std::string &lib, | ||
const void *address); | ||
|
||
CachedSymbolTable *getSymbolTable(const std::string &lib); | ||
|
||
} // namespace hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.