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
12 changed files
with
352 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#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_; | ||
}; | ||
|
||
StringRefIterator strtab_begin(const char *str) const; | ||
|
||
StringRefIterator strtab_end(const char *str) const; | ||
|
||
CachedSymbolTable(const std::string &name, const void *base_address); | ||
|
||
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(); | ||
void parse_named_section(); | ||
|
||
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_; } | ||
|
||
private: | ||
std::string libName; | ||
std::ifstream ifs; | ||
ElfW(Ehdr) elf_header; | ||
std::vector<char> section_header_str; | ||
std::vector<ElfW(Shdr)> sections; | ||
std::vector<std::string> strtab; | ||
std::unordered_map<size_t, std::string> symbol_table; | ||
const void *base_address; | ||
size_t min_address_ = -1; | ||
size_t max_address_ = 0; | ||
}; | ||
|
||
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
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
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,182 @@ | ||
#include "elf_parser.h" | ||
namespace hook { | ||
|
||
CachedSymbolTable::StringRefIterator::StringRefIterator(const char *str) | ||
: str_(str) {} | ||
|
||
CachedSymbolTable::StringRefIterator & | ||
CachedSymbolTable::StringRefIterator::operator++() & { | ||
size_t len = strlen(str_); | ||
++len; | ||
str_ += len; | ||
return *this; | ||
} | ||
|
||
CachedSymbolTable::StringRefIterator | ||
CachedSymbolTable::StringRefIterator::operator++(int) & { | ||
auto ret = StringRefIterator(str_); | ||
++*this; | ||
return ret; | ||
} | ||
|
||
adt::StringRef CachedSymbolTable::StringRefIterator::operator*() { | ||
return adt::StringRef(str_); | ||
} | ||
|
||
bool CachedSymbolTable::StringRefIterator::operator==( | ||
const StringRefIterator &other) const { | ||
return str_ == other.str_; | ||
} | ||
bool CachedSymbolTable::StringRefIterator::operator!=( | ||
const StringRefIterator &other) const { | ||
return !(*this == other); | ||
} | ||
|
||
const void *CachedSymbolTable::StringRefIterator::data() const { return str_; } | ||
|
||
CachedSymbolTable::CachedSymbolTable(const std::string &name, | ||
const void *base_address) | ||
: libName(name), ifs(name), base_address(base_address) { | ||
MLOG(DEBUG, INFO) << name << " base address:" << base_address; | ||
ifs.read(reinterpret_cast<char *>(&elf_header), sizeof(elf_header)); | ||
parse_named_section(); | ||
parse_section(); | ||
load_symbol_table(); | ||
for (size_t i = 0; i < sections.size(); ++i) { | ||
MLOG(DEBUG, INFO) << "found section:" << i | ||
<< " name:" << getSectionName(i); | ||
} | ||
} | ||
|
||
CachedSymbolTable::StringRefIterator CachedSymbolTable::strtab_begin( | ||
const char *str) const { | ||
return CachedSymbolTable::StringRefIterator(str); | ||
} | ||
|
||
CachedSymbolTable::StringRefIterator CachedSymbolTable::strtab_end( | ||
const char *str) const { | ||
return CachedSymbolTable::StringRefIterator(str); | ||
} | ||
|
||
void CachedSymbolTable::move_to_section_header() { | ||
ifs.seekg(elf_header.e_shoff, std::ios::beg); | ||
} | ||
|
||
void CachedSymbolTable::move_to_section_header(size_t index) { | ||
MLOG(DEBUG, INFO) << "move to:" << index; | ||
move_to_section_header(); | ||
size_t shstr_h_oft = sizeof(ElfW(Shdr)) * index; | ||
ifs.seekg(shstr_h_oft, std::ios::cur); | ||
} | ||
|
||
adt::StringRef CachedSymbolTable::getSectionName(size_t index) const { | ||
return adt::StringRef(§ion_header_str.at(sections.at(index).sh_name)); | ||
} | ||
|
||
size_t CachedSymbolTable::find_section(adt::StringRef name) const { | ||
size_t index = 0; | ||
for (; index < sections.size(); index++) { | ||
if (getSectionName(index) == name) { | ||
break; | ||
} | ||
} | ||
return index; | ||
} | ||
|
||
void CachedSymbolTable::load_symbol_table() { | ||
size_t symtab_h_index = find_section(".symtab"); | ||
if (symtab_h_index >= sections.size()) { | ||
LOG(WARN) << "can't found secton: .symtab"; | ||
return; | ||
} | ||
ifs.seekg(sections[symtab_h_index].sh_offset, std::ios::beg); | ||
std::vector<ElfW(Sym)> symbol_tb(sections[symtab_h_index].sh_size / | ||
sizeof(symbol_tb[0])); | ||
ifs.read(reinterpret_cast<char *>(symbol_tb.data()), | ||
sections[symtab_h_index].sh_size); | ||
|
||
size_t strtab_h_index = find_section(".strtab"); | ||
if (strtab_h_index >= sections.size()) { | ||
LOG(WARN) << "can't found secton: .strtab"; | ||
return; | ||
} | ||
ifs.seekg(sections[strtab_h_index].sh_offset, std::ios::beg); | ||
std::vector<char> buf(sections[strtab_h_index].sh_size); | ||
ifs.read(buf.data(), buf.size()); | ||
|
||
auto strtab_begin_iter = strtab_begin(buf.data()); | ||
auto strtab_end_iter = strtab_end(buf.data() + buf.size()); | ||
for (StringRefIterator iter = strtab_begin_iter; iter != strtab_end_iter; | ||
++iter) { | ||
strtab.push_back((*iter).str()); | ||
} | ||
for (size_t i = 0; i < symbol_tb.size() / sizeof(ElfW(Sym)); ++i) { | ||
if (strtab.size() <= symbol_tb[i].st_name) { | ||
LOG(WARN) << "symbol_tb[" << i << "].st_name(" | ||
<< symbol_tb[i].st_name | ||
<< ") over strtab size:" << strtab.size(); | ||
} | ||
symbol_table.emplace(symbol_tb[i].st_value, | ||
strtab[symbol_tb[i].st_name]); | ||
if (symbol_tb[i].st_value > max_address_) { | ||
max_address_ = symbol_tb[i].st_value; | ||
} | ||
if (symbol_tb[i].st_value < min_address_) { | ||
min_address_ = symbol_tb[i].st_value; | ||
} | ||
} | ||
MLOG(DEBUG, INFO) << libName << "\naddress range:" << min_address_ << "~" | ||
<< max_address_; | ||
std::vector<std::string>().swap(strtab); | ||
} | ||
void CachedSymbolTable::parse_section() { | ||
CHECK_EQ(sizeof(ElfW(Shdr)), elf_header.e_shentsize); | ||
move_to_section_header(); | ||
sections.resize(elf_header.e_shnum); | ||
MLOG(DEBUG, INFO) << "elf_header.e_shnum:" << elf_header.e_shnum; | ||
ifs.read(reinterpret_cast<char *>(sections.data()), | ||
sections.size() * sizeof(sections[0])); | ||
} | ||
void CachedSymbolTable::parse_named_section() { | ||
move_to_section_header(elf_header.e_shstrndx); | ||
|
||
ElfW(Shdr) shstr_h; | ||
ifs.read(reinterpret_cast<char *>(&shstr_h), sizeof(shstr_h)); | ||
ifs.seekg(shstr_h.sh_offset, std::ios::beg); | ||
|
||
section_header_str.resize(shstr_h.sh_size); | ||
ifs.read(section_header_str.data(), section_header_str.size()); | ||
} | ||
|
||
const std::string &CachedSymbolTable::lookUpSymbol(const void *func) const { | ||
auto offset = reinterpret_cast<const char *>(func) - | ||
reinterpret_cast<const char *>(base_address); | ||
MLOG(DEBUG, INFO) << "lookup address:" << offset; | ||
auto iter = symbol_table.find(offset); | ||
if (iter == symbol_table.end()) { | ||
LOG(WARN) << libName | ||
<< "\nnot find launch_async symbol offset:" << offset | ||
<< " base address:" << base_address | ||
<< " func address:" << func << " range(" << min_address_ | ||
<< "~" << max_address_; | ||
} else { | ||
LOG(WARN) << "found launch_async symbol:" << iter->second; | ||
} | ||
static std::string empty(""); | ||
return empty; | ||
} | ||
|
||
static std::unordered_map<std::string, std::unique_ptr<CachedSymbolTable>> | ||
table; | ||
|
||
CachedSymbolTable *createSymbolTable(const std::string &lib, | ||
const void *address) { | ||
auto iter = table.emplace(lib, new CachedSymbolTable(lib, address)); | ||
return iter.first->second.get(); | ||
} | ||
|
||
CachedSymbolTable *getSymbolTable(const std::string &lib) { | ||
return table[lib].get(); | ||
} | ||
|
||
} // 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
Oops, something went wrong.