Skip to content

Commit

Permalink
Templated lambdas aren't a thing in C++11 :(
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Jan 26, 2025
1 parent 76a21d2 commit b705afb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
104 changes: 54 additions & 50 deletions src/binary/elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,38 @@ namespace detail {

Result<std::uintptr_t, internal_error> elf::get_module_image_base() {
// get image base
auto impl = [this]<std::size_t Bits>() -> Result<std::uintptr_t, internal_error> {
static_assert(Bits == 32 || Bits == 64, "Unexpected Bits argument");
using PHeader = typename std::conditional<Bits == 32, Elf32_Phdr, Elf64_Phdr>::type;
auto header = get_header_info();
if(header.is_error()) {
return std::move(header).unwrap_error();
}
const auto& header_info = header.unwrap_value();
// PT_PHDR will occur at most once
// Should be somewhat reliable https://stackoverflow.com/q/61568612/15675011
// It should occur at the beginning but may as well loop just in case
for(unsigned i = 0; i < header_info.e_phnum; i++) {
auto loaded_ph = load_bytes<PHeader>(file, header_info.e_phoff + header_info.e_phentsize * i);
if(loaded_ph.is_error()) {
return std::move(loaded_ph).unwrap_error();
}
const PHeader& program_header = loaded_ph.unwrap_value();
if(byteswap_if_needed(program_header.p_type, is_little_endian) == PT_PHDR) {
return byteswap_if_needed(program_header.p_vaddr, is_little_endian) -
byteswap_if_needed(program_header.p_offset, is_little_endian);
}
}
// Apparently some objects like shared objects can end up missing this header. 0 as a base seems correct.
return 0;
};
if(is_64) {
return impl.operator()<64>();
return get_module_image_base_impl<64>();
} else {
return impl.operator()<32>();
return get_module_image_base_impl<32>();
}
}

template<std::size_t Bits>
Result<std::uintptr_t, internal_error> elf::get_module_image_base_impl() {
static_assert(Bits == 32 || Bits == 64, "Unexpected Bits argument");
using PHeader = typename std::conditional<Bits == 32, Elf32_Phdr, Elf64_Phdr>::type;
auto header = get_header_info();
if(header.is_error()) {
return std::move(header).unwrap_error();
}
const auto& header_info = header.unwrap_value();
// PT_PHDR will occur at most once
// Should be somewhat reliable https://stackoverflow.com/q/61568612/15675011
// It should occur at the beginning but may as well loop just in case
for(unsigned i = 0; i < header_info.e_phnum; i++) {
auto loaded_ph = load_bytes<PHeader>(file, header_info.e_phoff + header_info.e_phentsize * i);
if(loaded_ph.is_error()) {
return std::move(loaded_ph).unwrap_error();
}
const PHeader& program_header = loaded_ph.unwrap_value();
if(byteswap_if_needed(program_header.p_type, is_little_endian) == PT_PHDR) {
return byteswap_if_needed(program_header.p_vaddr, is_little_endian) -
byteswap_if_needed(program_header.p_offset, is_little_endian);
}
}
// Apparently some objects like shared objects can end up missing this header. 0 as a base seems correct.
return 0;
}

template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type>
Expand All @@ -99,32 +101,34 @@ namespace detail {
if(header) {
return header.unwrap();
}
auto impl = [this]<std::size_t Bits>() -> Result<header_info, internal_error> {
static_assert(Bits == 32 || Bits == 64, "Unexpected Bits argument");
using Header = typename std::conditional<Bits == 32, Elf32_Ehdr, Elf64_Ehdr>::type;
auto loaded_header = load_bytes<Header>(file, 0);
if(loaded_header.is_error()) {
return std::move(loaded_header).unwrap_error();
}
const Header& file_header = loaded_header.unwrap_value();
if(file_header.e_ehsize != sizeof(Header)) {
return internal_error("ELF file header size mismatch" + object_path);
}
header_info info;
info.e_phoff = file_header.e_phoff;
info.e_phnum = file_header.e_phnum;
info.e_phentsize = file_header.e_phentsize;
info.e_shoff = file_header.e_shoff;
info.e_shnum = file_header.e_shnum;
info.e_shentsize = file_header.e_shentsize;
header = info;
return header.unwrap();
};
if(is_64) {
return impl.operator()<64>();
return get_header_info_impl<64>();
} else {
return impl.operator()<32>();
return get_header_info_impl<32>();
}
}

template<std::size_t Bits>
Result<elf::header_info, internal_error> elf::get_header_info_impl() {
static_assert(Bits == 32 || Bits == 64, "Unexpected Bits argument");
using Header = typename std::conditional<Bits == 32, Elf32_Ehdr, Elf64_Ehdr>::type;
auto loaded_header = load_bytes<Header>(file, 0);
if(loaded_header.is_error()) {
return std::move(loaded_header).unwrap_error();
}
const Header& file_header = loaded_header.unwrap_value();
if(file_header.e_ehsize != sizeof(Header)) {
return internal_error("ELF file header size mismatch" + object_path);
}
header_info info;
info.e_phoff = file_header.e_phoff;
info.e_phnum = file_header.e_phnum;
info.e_phentsize = file_header.e_phentsize;
info.e_shoff = file_header.e_shoff;
info.e_shnum = file_header.e_shnum;
info.e_shentsize = file_header.e_shentsize;
header = info;
return header.unwrap();
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/binary/elf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ namespace detail {
public:
static NODISCARD Result<elf, internal_error> open_elf(const std::string& object_path);

public:
Result<std::uintptr_t, internal_error> get_module_image_base();
private:
template<std::size_t Bits>
Result<std::uintptr_t, internal_error> get_module_image_base_impl();

private:
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
T byteswap_if_needed(T value, bool elf_is_little);

Result<header_info, internal_error> get_header_info();
template<std::size_t Bits>
Result<header_info, internal_error> get_header_info_impl();
};
}
}
Expand Down

0 comments on commit b705afb

Please sign in to comment.