@@ -91,14 +91,17 @@ namespace detail {
91
91
std::string elf::lookup_symbol (frame_ptr pc) {
92
92
// TODO: Also search the SHT_DYNSYM at some point, maybe
93
93
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) {
99
103
return " " ;
100
104
}
101
- auto & symtab = symtab_.unwrap_value ();
102
105
auto strtab_ = get_strtab (symtab.strtab_link );
103
106
if (strtab_.is_error ()) {
104
107
return " " ;
@@ -250,7 +253,7 @@ namespace detail {
250
253
return entry.data ;
251
254
}
252
255
253
- Result<const elf::symtab_info&, internal_error> elf::get_symtab () {
256
+ Result<const optional< elf::symtab_info> &, internal_error> elf::get_symtab () {
254
257
if (did_load_symtab) {
255
258
return symtab;
256
259
}
@@ -266,7 +269,7 @@ namespace detail {
266
269
}
267
270
268
271
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 () {
270
273
// https://refspecs.linuxfoundation.org/elf/elf.pdf
271
274
// page 66: only one sht_symtab and sht_dynsym section per file
272
275
// page 32: symtab spec
@@ -292,7 +295,8 @@ namespace detail {
292
295
if (std::fread (buffer.data (), section.sh_entsize , buffer.size (), file) != buffer.size ()) {
293
296
return internal_error (" fread error while loading elf symbol table" );
294
297
}
295
- symtab.entries .reserve (buffer.size ());
298
+ symtab = symtab_info{};
299
+ symtab.unwrap ().entries .reserve (buffer.size ());
296
300
for (const auto & entry : buffer) {
297
301
symtab_entry normalized;
298
302
normalized.st_name = byteswap_if_needed (entry.st_name );
@@ -301,12 +305,16 @@ namespace detail {
301
305
normalized.st_shndx = byteswap_if_needed (entry.st_shndx );
302
306
normalized.st_value = byteswap_if_needed (entry.st_value );
303
307
normalized.st_size = byteswap_if_needed (entry.st_size );
304
- symtab.entries .push_back (normalized);
308
+ symtab.unwrap (). entries .push_back (normalized);
305
309
}
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 ;
310
318
did_load_symtab = true ;
311
319
return symtab;
312
320
}
0 commit comments