Skip to content

Commit 7f6945c

Browse files
committed
Better handle symtab loading / optionality
1 parent 73ee7aa commit 7f6945c

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/binary/elf.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,17 @@ namespace detail {
9191
std::string elf::lookup_symbol(frame_ptr pc) {
9292
// TODO: Also search the SHT_DYNSYM at some point, maybe
9393
auto symtab_ = get_symtab();
94-
if(
95-
symtab_.is_error()
96-
|| symtab_.unwrap_value().strtab_link == SHN_UNDEF
97-
|| symtab_.unwrap_value().entries.empty()
98-
) {
94+
if(symtab_.is_error()) {
95+
return "";
96+
}
97+
auto& maybe_symtab = symtab_.unwrap_value();
98+
if(!maybe_symtab) {
99+
return "";
100+
}
101+
auto& symtab = maybe_symtab.unwrap();
102+
if(symtab.strtab_link == SHN_UNDEF) {
99103
return "";
100104
}
101-
auto& symtab = symtab_.unwrap_value();
102105
auto strtab_ = get_strtab(symtab.strtab_link);
103106
if(strtab_.is_error()) {
104107
return "";
@@ -250,7 +253,7 @@ namespace detail {
250253
return entry.data;
251254
}
252255

253-
Result<const elf::symtab_info&, internal_error> elf::get_symtab() {
256+
Result<const optional<elf::symtab_info>&, internal_error> elf::get_symtab() {
254257
if(did_load_symtab) {
255258
return symtab;
256259
}
@@ -266,7 +269,7 @@ namespace detail {
266269
}
267270

268271
template<std::size_t Bits>
269-
Result<const elf::symtab_info&, internal_error> elf::get_symtab_impl() {
272+
Result<const optional<elf::symtab_info>&, internal_error> elf::get_symtab_impl() {
270273
// https://refspecs.linuxfoundation.org/elf/elf.pdf
271274
// page 66: only one sht_symtab and sht_dynsym section per file
272275
// page 32: symtab spec
@@ -292,7 +295,8 @@ namespace detail {
292295
if(std::fread(buffer.data(), section.sh_entsize, buffer.size(), file) != buffer.size()) {
293296
return internal_error("fread error while loading elf symbol table");
294297
}
295-
symtab.entries.reserve(buffer.size());
298+
symtab = symtab_info{};
299+
symtab.unwrap().entries.reserve(buffer.size());
296300
for(const auto& entry : buffer) {
297301
symtab_entry normalized;
298302
normalized.st_name = byteswap_if_needed(entry.st_name);
@@ -301,12 +305,16 @@ namespace detail {
301305
normalized.st_shndx = byteswap_if_needed(entry.st_shndx);
302306
normalized.st_value = byteswap_if_needed(entry.st_value);
303307
normalized.st_size = byteswap_if_needed(entry.st_size);
304-
symtab.entries.push_back(normalized);
308+
symtab.unwrap().entries.push_back(normalized);
305309
}
306-
std::sort(symtab.entries.begin(), symtab.entries.end(), [] (const symtab_entry& a, const symtab_entry& b) {
307-
return a.st_value < b.st_value;
308-
});
309-
symtab.strtab_link = section.sh_link;
310+
std::sort(
311+
symtab.unwrap().entries.begin(),
312+
symtab.unwrap().entries.end(),
313+
[] (const symtab_entry& a, const symtab_entry& b) {
314+
return a.st_value < b.st_value;
315+
}
316+
);
317+
symtab.unwrap().strtab_link = section.sh_link;
310318
did_load_symtab = true;
311319
return symtab;
312320
}

src/binary/elf.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace detail {
6262
};
6363
bool tried_to_load_symtab = false;
6464
bool did_load_symtab = false;
65-
symtab_info symtab;
65+
optional<symtab_info> symtab;
6666

6767
elf(file_wrapper file, const std::string& object_path, bool is_little_endian, bool is_64);
6868

@@ -92,9 +92,9 @@ namespace detail {
9292

9393
Result<const std::vector<char>&, internal_error> get_strtab(std::size_t index);
9494

95-
Result<const symtab_info&, internal_error> get_symtab();
95+
Result<const optional<symtab_info>&, internal_error> get_symtab();
9696
template<std::size_t Bits>
97-
Result<const symtab_info&, internal_error> get_symtab_impl();
97+
Result<const optional<symtab_info>&, internal_error> get_symtab_impl();
9898
};
9999
}
100100
}

0 commit comments

Comments
 (0)