2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/alg/alg.h"
|
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/elf/def.h"
|
|
|
|
#include "libc/elf/elf.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/runtime/carsort.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/runtime/symbols.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps debuggable binary into memory and indexes symbol addresses.
|
|
|
|
*
|
2020-10-11 04:18:53 +00:00
|
|
|
* @return object freeable with CloseSymbolTable(), or NULL w/ errno
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
2020-10-11 04:18:53 +00:00
|
|
|
struct SymbolTable *OpenSymbolTable(const char *filename) {
|
2020-08-25 11:23:25 +00:00
|
|
|
unsigned i, j;
|
|
|
|
struct SymbolTable *t;
|
|
|
|
const Elf64_Sym *symtab, *sym;
|
|
|
|
t = MAP_FAILED;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (filename && (t = mapanon(BIGPAGESIZE)) != MAP_FAILED &&
|
2020-11-25 16:19:00 +00:00
|
|
|
MapElfRead(filename, &t->mf) &&
|
2020-11-09 23:41:11 +00:00
|
|
|
(t->name_base = GetElfStringTable(t->elf, t->elfsize)) != NULL &&
|
|
|
|
(symtab = GetElfSymbolTable(t->elf, t->elfsize, &t->count)) &&
|
2020-06-15 14:18:57 +00:00
|
|
|
sizeof(struct SymbolTable) + sizeof(struct Symbol) * t->count <
|
|
|
|
(t->scratch = BIGPAGESIZE)) {
|
2020-11-25 16:19:00 +00:00
|
|
|
GetElfVirtualAddressRange(t->elf, t->elfsize, &t->addr_base, &t->addr_end);
|
2020-08-25 11:23:25 +00:00
|
|
|
for (j = i = 0; i < t->count; ++i) {
|
|
|
|
sym = &symtab[i];
|
2020-11-25 16:19:00 +00:00
|
|
|
if (IsElfSymbolContent(sym) &&
|
2020-06-15 14:18:57 +00:00
|
|
|
(sym->st_value >= t->addr_base && sym->st_value <= t->addr_end)) {
|
|
|
|
t->symbols[j].addr_rva = (unsigned)(sym->st_value - t->addr_base);
|
|
|
|
t->symbols[j].name_rva = sym->st_name;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t->count = j;
|
2020-08-25 11:23:25 +00:00
|
|
|
carsort1000(t->count, (void *)t->symbols);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2020-10-11 04:18:53 +00:00
|
|
|
CloseSymbolTable(&t);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
return t == MAP_FAILED ? NULL : t;
|
|
|
|
}
|