Skip to content

Commit

Permalink
fix: code improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Olivier <[email protected]>
  • Loading branch information
martin-olivier committed Dec 25, 2024
1 parent ceb6820 commit 14450a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ std::string get_demangled_name(const char *symbol) {
#include <cstring>

std::string get_demangled_name(const char *symbol) {
std::string result;
size_t size = strlen(symbol);
std::string result;
int status;
char *buf;
char *res;
Expand Down
26 changes: 13 additions & 13 deletions src/dylib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ static void close_lib(dylib::native_handle_type lib) noexcept {

static std::string get_error_description() noexcept {
#if (defined(_WIN32) || defined(_WIN64))
constexpr const size_t buf_size = 512;
auto error_code = GetLastError();
WORD lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
DWORD error_code = GetLastError();
char description[512];

if (!error_code)
return "Unknown error (GetLastError failed)";
char description[512];
auto lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
const DWORD length =
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error_code, lang, description, buf_size, nullptr);

const DWORD length = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error_code,
lang, description, 512, nullptr);

return (length == 0) ? "Unknown error (FormatMessage failed)" : description;
#else
auto description = dlerror();
char *description = dlerror();

return (description == nullptr) ? "Unknown error (dlerror failed)" : description;
#endif
}
Expand Down Expand Up @@ -126,22 +129,19 @@ dylib::~dylib() {
dylib::native_symbol_type dylib::get_symbol(const char *symbol_name) const {
std::vector<std::string> matching_symbols;
std::vector<std::string> all_symbols;
dylib::native_symbol_type symbol;

if (!symbol_name)
throw std::invalid_argument("Null parameter");
if (!m_handle)
throw std::logic_error("The dynamic library handle is null");

auto symbol = locate_symbol(m_handle, symbol_name);

symbol = locate_symbol(m_handle, symbol_name);
if (symbol == nullptr) {
all_symbols = symbols();

for (auto &sym : all_symbols) {
auto demangled = get_demangled_name(sym.c_str());

if (demangled.empty())
continue;
std::string demangled = get_demangled_name(sym.c_str());

if (demangled.find(symbol_name) == 0 &&
(demangled[strlen(symbol_name)] == '(' ||
Expand Down
2 changes: 1 addition & 1 deletion src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void add_sym_separator(std::string &input, char symbol) {
input.replace(pos, 1, std::string(" ") + symbol);
pos += 2;
} else {
pos++;
pos += 1;
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ std::vector<std::string> get_symbols(HMODULE handle, bool demangle, bool loadabl

static std::vector<std::string> get_symbols_at_off(void *handle, int fd, bool demangle, bool loadable, off_t offset, bool is_64_bit) {
std::vector<std::string> result;

lseek(fd, offset, SEEK_SET);

struct mach_header_64 mh64;
struct mach_header mh;

lseek(fd, offset, SEEK_SET);

if (is_64_bit)
read(fd, &mh64, sizeof(mh64));
else
Expand Down Expand Up @@ -235,8 +234,7 @@ std::vector<std::string> get_symbols(void *handle, int fd, bool demangle, bool l
}

if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
Elf_Data *data = NULL;
data = elf_getdata(scn, data);
Elf_Data *data = elf_getdata(scn, data);
if (!data) {
elf_end(elf);
throw std::string("elf_getdata() failed");
Expand All @@ -245,12 +243,19 @@ std::vector<std::string> get_symbols(void *handle, int fd, bool demangle, bool l
int count = shdr.sh_size / shdr.sh_entsize;
for (int i = 0; i < count; i++) {
GElf_Sym sym;
char *name;

if (!gelf_getsym(data, i, &sym)) {
elf_end(elf);
throw std::string("gelf_getsym() failed");
}

const char *name = elf_strptr(elf, shdr.sh_link, sym.st_name);
name = elf_strptr(elf, shdr.sh_link, sym.st_name);
if (!name) {
elf_end(elf);
throw std::string("elf_strptr() failed");
}

if (!loadable || dlsym(handle, name))
add_symbol(result, name, demangle);
}
Expand Down

0 comments on commit 14450a1

Please sign in to comment.