| 1 |
/* Sysprof -- Sampling, systemwide CPU profiler |
| 2 |
* Copyright 2006, 2007, Soeren Sandmann |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#ifdef __APPLE__ |
| 22 |
# include <libelf.h> |
| 23 |
#else |
| 24 |
# include <elf.h> |
| 25 |
#endif |
| 26 |
#include <sys/mman.h> |
| 27 |
|
| 28 |
#include "demangle.h" |
| 29 |
#include "elfparser.h" |
| 30 |
#include "rust-demangle.h" |
| 31 |
|
| 32 |
typedef struct Section Section; |
| 33 |
|
| 34 |
struct ElfSym |
| 35 |
{ |
| 36 |
gulong table; |
| 37 |
gulong offset; |
| 38 |
gulong address; |
| 39 |
}; |
| 40 |
|
| 41 |
struct Section |
| 42 |
{ |
| 43 |
const gchar * name; |
| 44 |
gsize offset; |
| 45 |
gsize size; |
| 46 |
gboolean allocated; |
| 47 |
gulong load_address; |
| 48 |
guint type; |
| 49 |
}; |
| 50 |
|
| 51 |
struct ElfParser |
| 52 |
{ |
| 53 |
gboolean is_64; |
| 54 |
const guchar * data; |
| 55 |
gsize length; |
| 56 |
|
| 57 |
guint n_sections; |
| 58 |
Section ** sections; |
| 59 |
|
| 60 |
guint n_symbols; |
| 61 |
ElfSym * symbols; |
| 62 |
gsize sym_strings; |
| 63 |
|
| 64 |
GMappedFile * file; |
| 65 |
|
| 66 |
char * filename; |
| 67 |
|
| 68 |
gboolean checked_build_id; |
| 69 |
char * build_id; |
| 70 |
|
| 71 |
const Section * text_section; |
| 72 |
}; |
| 73 |
|
| 74 |
/* FIXME: All of these should in principle do endian swapping, |
| 75 |
* but sysprof never has to deal with binaries of a different |
| 76 |
* endianness than sysprof itself |
| 77 |
*/ |
| 78 |
#define GET_FIELD(parser, offset, struct_name, idx, field_name) \ |
| 79 |
(((parser))->is_64? \ |
| 80 |
((Elf64_ ## struct_name *)(gpointer)(((parser)->data + offset)) + (idx))->field_name : \ |
| 81 |
((Elf32_ ## struct_name *)(gpointer)(((parser)->data + offset)) + (idx))->field_name) |
| 82 |
|
| 83 |
#define GET_UINT32(parser, offset) \ |
| 84 |
*((uint32_t *)(gpointer)(parser->data + offset)) \ |
| 85 |
|
| 86 |
#define GET_SIZE(parser, struct_name) \ |
| 87 |
(((parser)->is_64? \ |
| 88 |
sizeof (Elf64_ ## struct_name) : \ |
| 89 |
sizeof (Elf32_ ## struct_name))) |
| 90 |
|
| 91 |
#define MAKE_ELF_UINT_ACCESSOR(field_name) \ |
| 92 |
static uint64_t field_name (ElfParser *parser) \ |
| 93 |
{ \ |
| 94 |
return GET_FIELD (parser, 0, Ehdr, 0, field_name); \ |
| 95 |
} |
| 96 |
|
| 97 |
MAKE_ELF_UINT_ACCESSOR (e_shoff) |
| 98 |
MAKE_ELF_UINT_ACCESSOR (e_shnum) |
| 99 |
MAKE_ELF_UINT_ACCESSOR (e_shstrndx) |
| 100 |
|
| 101 |
#define MAKE_SECTION_HEADER_ACCESSOR(field_name) \ |
| 102 |
static uint64_t field_name (ElfParser *parser, int nth_section) \ |
| 103 |
{ \ |
| 104 |
gsize offset = e_shoff (parser); \ |
| 105 |
\ |
| 106 |
return GET_FIELD (parser, offset, Shdr, nth_section, field_name); \ |
| 107 |
} |
| 108 |
|
| 109 |
MAKE_SECTION_HEADER_ACCESSOR (sh_name); |
| 110 |
MAKE_SECTION_HEADER_ACCESSOR (sh_type); |
| 111 |
MAKE_SECTION_HEADER_ACCESSOR (sh_flags); |
| 112 |
MAKE_SECTION_HEADER_ACCESSOR (sh_addr); |
| 113 |
MAKE_SECTION_HEADER_ACCESSOR (sh_offset); |
| 114 |
MAKE_SECTION_HEADER_ACCESSOR (sh_size); |
| 115 |
|
| 116 |
#define MAKE_SYMBOL_ACCESSOR(field_name) \ |
| 117 |
static uint64_t field_name (ElfParser *parser, gulong offset, gulong nth) \ |
| 118 |
{ \ |
| 119 |
return GET_FIELD (parser, offset, Sym, nth, field_name); \ |
| 120 |
} |
| 121 |
|
| 122 |
MAKE_SYMBOL_ACCESSOR(st_name); |
| 123 |
MAKE_SYMBOL_ACCESSOR(st_info); |
| 124 |
MAKE_SYMBOL_ACCESSOR(st_value); |
| 125 |
MAKE_SYMBOL_ACCESSOR(st_size); |
| 126 |
MAKE_SYMBOL_ACCESSOR(st_shndx); |
| 127 |
|
| 128 |
static gboolean |
| 129 |
in_container (void) |
| 130 |
{ |
| 131 |
static gboolean _in_container; |
| 132 |
static gboolean initialized; |
| 133 |
|
| 134 |
if (!initialized) |
| 135 |
{ |
| 136 |
/* Flatpak has /.flatpak-info |
| 137 |
* Podman has /run/.containerenv |
| 138 |
* |
| 139 |
* Both have access to host files via /var/run/host. |
| 140 |
*/ |
| 141 |
_in_container = g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS) || |
| 142 |
g_file_test ("/run/.containerenv", G_FILE_TEST_EXISTS); |
| 143 |
|
| 144 |
initialized = TRUE; |
| 145 |
} |
| 146 |
|
| 147 |
return _in_container; |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
section_free (Section *section) |
| 152 |
{ |
| 153 |
g_free (section); |
| 154 |
} |
| 155 |
|
| 156 |
static const Section * |
| 157 |
find_section (ElfParser *parser, |
| 158 |
const char *name, |
| 159 |
guint type) |
| 160 |
{ |
| 161 |
guint i; |
| 162 |
|
| 163 |
if (name == NULL) |
| 164 |
return NULL; |
| 165 |
|
| 166 |
for (i = 0; i < parser->n_sections; ++i) |
| 167 |
{ |
| 168 |
Section *section = parser->sections[i]; |
| 169 |
|
| 170 |
if (section->name == NULL || section->type != type) |
| 171 |
continue; |
| 172 |
|
| 173 |
if (strcmp (section->name, name) == 0) |
| 174 |
return section; |
| 175 |
} |
| 176 |
|
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
static gboolean |
| 181 |
parse_elf_signature (const guchar *data, |
| 182 |
gsize length, |
| 183 |
gboolean *is_64, |
| 184 |
gboolean *is_be) |
| 185 |
{ |
| 186 |
/* FIXME: this function should be able to return an error */ |
| 187 |
if (length < EI_NIDENT) |
| 188 |
{ |
| 189 |
/* FIXME set error */ |
| 190 |
return FALSE; |
| 191 |
} |
| 192 |
|
| 193 |
if (data[EI_CLASS] != ELFCLASS32 && |
| 194 |
data[EI_CLASS] != ELFCLASS64) |
| 195 |
{ |
| 196 |
/* FIXME set error */ |
| 197 |
return FALSE; |
| 198 |
} |
| 199 |
|
| 200 |
if (data[EI_DATA] != ELFDATA2LSB && |
| 201 |
data[EI_DATA] != ELFDATA2MSB) |
| 202 |
{ |
| 203 |
/* FIXME set error */ |
| 204 |
return FALSE; |
| 205 |
} |
| 206 |
|
| 207 |
if (is_64) |
| 208 |
*is_64 = (data[EI_CLASS] == ELFCLASS64); |
| 209 |
|
| 210 |
if (is_be) |
| 211 |
*is_be = (data[EI_DATA] == ELFDATA2MSB); |
| 212 |
|
| 213 |
return TRUE; |
| 214 |
} |
| 215 |
|
| 216 |
ElfParser * |
| 217 |
elf_parser_new_from_data (const guchar *data, |
| 218 |
gsize length) |
| 219 |
{ |
| 220 |
ElfParser *parser; |
| 221 |
gboolean is_64, is_big_endian; |
| 222 |
int section_names_idx; |
| 223 |
const guchar *section_names; |
| 224 |
G_GNUC_UNUSED gsize section_headers; |
| 225 |
guint i; |
| 226 |
|
| 227 |
if (!parse_elf_signature (data, length, &is_64, &is_big_endian)) |
| 228 |
{ |
| 229 |
/* FIXME: set error */ |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
|
| 233 |
parser = g_new0 (ElfParser, 1); |
| 234 |
|
| 235 |
parser->is_64 = is_64; |
| 236 |
parser->data = data; |
| 237 |
parser->length = length; |
| 238 |
|
| 239 |
#if 0 |
| 240 |
g_print (" new parser : %p\n", parser); |
| 241 |
#endif |
| 242 |
|
| 243 |
/* Read ELF header */ |
| 244 |
|
| 245 |
parser->n_sections = e_shnum (parser); |
| 246 |
section_names_idx = e_shstrndx (parser); |
| 247 |
section_headers = e_shoff (parser); |
| 248 |
|
| 249 |
/* Read section headers */ |
| 250 |
parser->sections = g_new0 (Section *, parser->n_sections); |
| 251 |
|
| 252 |
section_names = parser->data + sh_offset (parser, section_names_idx); |
| 253 |
|
| 254 |
for (i = 0; i < parser->n_sections; ++i) |
| 255 |
{ |
| 256 |
Section *section = g_new (Section, 1); |
| 257 |
|
| 258 |
section->name = (char *)(section_names + sh_name (parser, i)); |
| 259 |
section->size = sh_size (parser, i); |
| 260 |
section->offset = sh_offset (parser, i); |
| 261 |
section->allocated = !!(sh_flags (parser, i) & SHF_ALLOC); |
| 262 |
|
| 263 |
if (section->allocated) |
| 264 |
section->load_address = sh_addr (parser, i); |
| 265 |
else |
| 266 |
section->load_address = 0; |
| 267 |
|
| 268 |
section->type = sh_type (parser, i); |
| 269 |
|
| 270 |
parser->sections[i] = section; |
| 271 |
} |
| 272 |
|
| 273 |
/* Cache the text section */ |
| 274 |
parser->text_section = find_section (parser, ".text", SHT_PROGBITS); |
| 275 |
if (!parser->text_section) |
| 276 |
parser->text_section = find_section (parser, ".text", SHT_NOBITS); |
| 277 |
|
| 278 |
parser->filename = NULL; |
| 279 |
parser->build_id = NULL; |
| 280 |
|
| 281 |
return parser; |
| 282 |
} |
| 283 |
|
| 284 |
static GMappedFile * |
| 285 |
open_mapped_file (const char *filename, |
| 286 |
GError **error) |
| 287 |
{ |
| 288 |
GMappedFile *file = NULL; |
| 289 |
char *alternate = NULL; |
| 290 |
|
| 291 |
if (in_container () && !g_str_has_prefix (filename, g_get_home_dir ())) |
| 292 |
{ |
| 293 |
alternate = g_build_filename ("/var/run/host", filename, NULL); |
| 294 |
file = g_mapped_file_new (alternate, FALSE, NULL); |
| 295 |
g_free (alternate); |
| 296 |
} |
| 297 |
|
| 298 |
/* Flatpaks with filesystem=host don't have Silverblue's /sysroot in /var/run/host, |
| 299 |
* yet they have it in /, which means the original path might work. |
| 300 |
*/ |
| 301 |
if (!file) |
| 302 |
file = g_mapped_file_new (filename, FALSE, error); |
| 303 |
|
| 304 |
return file; |
| 305 |
} |
| 306 |
|
| 307 |
ElfParser * |
| 308 |
elf_parser_new_from_mmap (GMappedFile *file, |
| 309 |
GError **error) |
| 310 |
{ |
| 311 |
const guchar *data; |
| 312 |
gsize length; |
| 313 |
ElfParser *parser; |
| 314 |
|
| 315 |
if (file == NULL) |
| 316 |
return NULL; |
| 317 |
|
| 318 |
data = (guchar *)g_mapped_file_get_contents (file); |
| 319 |
length = g_mapped_file_get_length (file); |
| 320 |
parser = elf_parser_new_from_data (data, length); |
| 321 |
|
| 322 |
if (!parser) |
| 323 |
{ |
| 324 |
g_set_error (error, |
| 325 |
G_FILE_ERROR, |
| 326 |
G_FILE_ERROR_FAILED, |
| 327 |
"Failed to load ELF from mmap region"); |
| 328 |
g_mapped_file_unref (file); |
| 329 |
return NULL; |
| 330 |
} |
| 331 |
|
| 332 |
parser->filename = NULL; |
| 333 |
parser->file = file; |
| 334 |
|
| 335 |
return parser; |
| 336 |
} |
| 337 |
|
| 338 |
ElfParser * |
| 339 |
elf_parser_new (const char *filename, |
| 340 |
GError **error) |
| 341 |
{ |
| 342 |
GMappedFile *file; |
| 343 |
const guchar *data; |
| 344 |
gsize length; |
| 345 |
ElfParser *parser; |
| 346 |
|
| 347 |
if (!(file = open_mapped_file (filename, error))) |
| 348 |
return NULL; |
| 349 |
|
| 350 |
#if 0 |
| 351 |
g_print ("elf parser new : %s\n", filename); |
| 352 |
#endif |
| 353 |
|
| 354 |
data = (guchar *)g_mapped_file_get_contents (file); |
| 355 |
length = g_mapped_file_get_length (file); |
| 356 |
|
| 357 |
#if 0 |
| 358 |
g_print ("data %p: for %s\n", data, filename); |
| 359 |
#endif |
| 360 |
|
| 361 |
parser = elf_parser_new_from_data (data, length); |
| 362 |
|
| 363 |
#if 0 |
| 364 |
g_print ("Parser for %s: %p\n", filename, parser); |
| 365 |
#endif |
| 366 |
|
| 367 |
if (!parser) |
| 368 |
{ |
| 369 |
g_set_error (error, |
| 370 |
G_FILE_ERROR, |
| 371 |
G_FILE_ERROR_FAILED, |
| 372 |
"Failed to load ELF from file %s", |
| 373 |
filename); |
| 374 |
g_mapped_file_unref (file); |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
|
| 378 |
parser->filename = g_strdup (filename); |
| 379 |
|
| 380 |
parser->file = file; |
| 381 |
|
| 382 |
#if 0 |
| 383 |
g_print ("Elf file: %s (debug: %s)\n", |
| 384 |
filename, elf_parser_get_debug_link (parser, NULL)); |
| 385 |
|
| 386 |
if (!parser->symbols) |
| 387 |
g_print ("at this point %s has no symbols\n", filename); |
| 388 |
#endif |
| 389 |
|
| 390 |
return parser; |
| 391 |
} |
| 392 |
|
| 393 |
guint32 |
| 394 |
elf_parser_get_crc32 (ElfParser *parser) |
| 395 |
{ |
| 396 |
static const unsigned long crc32_table[256] = { |
| 397 |
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, |
| 398 |
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, |
| 399 |
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, |
| 400 |
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, |
| 401 |
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, |
| 402 |
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, |
| 403 |
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, |
| 404 |
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, |
| 405 |
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, |
| 406 |
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, |
| 407 |
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, |
| 408 |
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, |
| 409 |
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, |
| 410 |
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, |
| 411 |
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, |
| 412 |
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, |
| 413 |
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, |
| 414 |
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, |
| 415 |
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, |
| 416 |
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, |
| 417 |
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, |
| 418 |
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, |
| 419 |
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, |
| 420 |
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, |
| 421 |
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, |
| 422 |
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, |
| 423 |
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, |
| 424 |
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, |
| 425 |
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, |
| 426 |
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, |
| 427 |
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, |
| 428 |
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, |
| 429 |
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, |
| 430 |
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, |
| 431 |
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, |
| 432 |
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, |
| 433 |
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, |
| 434 |
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, |
| 435 |
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, |
| 436 |
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, |
| 437 |
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, |
| 438 |
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, |
| 439 |
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d |
| 440 |
}; |
| 441 |
const guchar *data; |
| 442 |
gsize length; |
| 443 |
gulong crc; |
| 444 |
gsize i; |
| 445 |
|
| 446 |
data = parser->data; |
| 447 |
length = parser->length; |
| 448 |
|
| 449 |
crc = 0xffffffff; |
| 450 |
|
| 451 |
madvise ((char *)data, length, MADV_SEQUENTIAL); |
| 452 |
|
| 453 |
for (i = 0; i < length; ++i) |
| 454 |
crc = crc32_table[(crc ^ data[i]) & 0xff] ^ (crc >> 8); |
| 455 |
|
| 456 |
/* We just read the entire file into memory, but we only really |
| 457 |
* need the symbol table, so swap the whole thing out. |
| 458 |
* |
| 459 |
* We could be more exact here, but it's only a few minor |
| 460 |
* pagefaults. |
| 461 |
*/ |
| 462 |
if (parser->file) |
| 463 |
madvise ((char *)data, length, MADV_DONTNEED); |
| 464 |
|
| 465 |
return ~crc & 0xffffffff; |
| 466 |
} |
| 467 |
|
| 468 |
void |
| 469 |
elf_parser_free (ElfParser *parser) |
| 470 |
{ |
| 471 |
guint i; |
| 472 |
|
| 473 |
for (i = 0; i < parser->n_sections; ++i) |
| 474 |
section_free (parser->sections[i]); |
| 475 |
g_free (parser->sections); |
| 476 |
|
| 477 |
if (parser->file) |
| 478 |
g_mapped_file_unref (parser->file); |
| 479 |
|
| 480 |
g_free (parser->symbols); |
| 481 |
|
| 482 |
if (parser->filename) |
| 483 |
g_free (parser->filename); |
| 484 |
|
| 485 |
if (parser->build_id) |
| 486 |
g_free (parser->build_id); |
| 487 |
|
| 488 |
g_free (parser); |
| 489 |
} |
| 490 |
|
| 491 |
gchar * |
| 492 |
elf_demangle (const char *name) |
| 493 |
{ |
| 494 |
/* Try demangling as rust symbol first as legacy rust symbols can demangle as C++ symbols too |
| 495 |
* but will only get partially demangled in that case. |
| 496 |
*/ |
| 497 |
gchar *demangled = sysprof_rust_demangle (name, 0); |
| 498 |
|
| 499 |
if (demangled) |
| 500 |
return demangled; |
| 501 |
|
| 502 |
demangled = sysprof_cplus_demangle (name); |
| 503 |
|
| 504 |
if (demangled) |
| 505 |
return demangled; |
| 506 |
else |
| 507 |
return g_strdup (name); |
| 508 |
} |
| 509 |
|
| 510 |
/* |
| 511 |
* Looking up symbols |
| 512 |
*/ |
| 513 |
static int |
| 514 |
compare_sym (const void *a, const void *b) |
| 515 |
{ |
| 516 |
const ElfSym *sym_a = a; |
| 517 |
const ElfSym *sym_b = b; |
| 518 |
|
| 519 |
if (sym_a->address < sym_b->address) |
| 520 |
return -1; |
| 521 |
else if (sym_a->address == sym_b->address) |
| 522 |
return 0; |
| 523 |
else |
| 524 |
return 1; |
| 525 |
} |
| 526 |
|
| 527 |
#if 0 |
| 528 |
static void |
| 529 |
dump_symbols (ElfParser *parser, ElfSym *syms, guint n_syms) |
| 530 |
{ |
| 531 |
int i; |
| 532 |
|
| 533 |
for (i = 0; i < n_syms; ++i) |
| 534 |
{ |
| 535 |
ElfSym *s = &(syms[i]); |
| 536 |
|
| 537 |
g_print (" %s: %lx\n", elf_parser_get_sym_name (parser, s), s->address); |
| 538 |
} |
| 539 |
} |
| 540 |
#endif |
| 541 |
|
| 542 |
static void |
| 543 |
read_table (ElfParser *parser, |
| 544 |
const Section *sym_table, |
| 545 |
const Section *str_table) |
| 546 |
{ |
| 547 |
int sym_size = GET_SIZE (parser, Sym); |
| 548 |
guint i, n_symbols; |
| 549 |
|
| 550 |
#if 0 |
| 551 |
g_print ("elf: Reading table for %s\n", parser->filename? parser->filename : "<unknown>"); |
| 552 |
#endif |
| 553 |
|
| 554 |
parser->n_symbols = sym_table->size / sym_size; |
| 555 |
parser->symbols = g_new (ElfSym, parser->n_symbols); |
| 556 |
|
| 557 |
#if 0 |
| 558 |
g_print ("sym table offset: %d\n", sym_table->offset); |
| 559 |
#endif |
| 560 |
|
| 561 |
n_symbols = 0; |
| 562 |
#if 0 |
| 563 |
g_print ("n syms: %d\n", parser->n_symbols); |
| 564 |
#endif |
| 565 |
for (i = 0; i < parser->n_symbols; ++i) |
| 566 |
{ |
| 567 |
guint info; |
| 568 |
gulong addr; |
| 569 |
gulong shndx; |
| 570 |
|
| 571 |
info = st_info (parser, sym_table->offset, i); |
| 572 |
addr = st_value (parser, sym_table->offset, i); |
| 573 |
shndx = st_shndx (parser, sym_table->offset, i); |
| 574 |
|
| 575 |
#if 0 |
| 576 |
g_print ("read symbol: %s (section: %d)\n", get_string_indirct (parser->parser, |
| 577 |
parser->sym_format, "st_name", |
| 578 |
str_table->offset), |
| 579 |
shndx); |
| 580 |
#endif |
| 581 |
|
| 582 |
if (addr != 0 && |
| 583 |
shndx < parser->n_sections && |
| 584 |
parser->sections[shndx] == parser->text_section && |
| 585 |
(info & 0xf) == STT_FUNC && |
| 586 |
((info >> 4) == STB_GLOBAL || |
| 587 |
(info >> 4) == STB_LOCAL || |
| 588 |
(info >> 4) == STB_WEAK)) |
| 589 |
{ |
| 590 |
parser->symbols[n_symbols].address = addr; |
| 591 |
parser->symbols[n_symbols].table = sym_table->offset; |
| 592 |
parser->symbols[n_symbols].offset = i; |
| 593 |
|
| 594 |
n_symbols++; |
| 595 |
|
| 596 |
#if 0 |
| 597 |
g_print (" symbol: %s: %lx\n", |
| 598 |
get_string_indirect (parser->parser, |
| 599 |
parser->sym_format, "st_name", |
| 600 |
str_table->offset), |
| 601 |
addr - parser->text_section->load_address); |
| 602 |
g_print (" sym %d in %p (info: %d:%d) (func:global %d:%d)\n", |
| 603 |
addr, parser, info & 0xf, info >> 4, STT_FUNC, STB_GLOBAL); |
| 604 |
#endif |
| 605 |
} |
| 606 |
else if (addr != 0) |
| 607 |
{ |
| 608 |
#if 0 |
| 609 |
g_print (" rejecting %d in %p (info: %d:%d) (func:global %d:%d)\n", |
| 610 |
addr, parser, info & 0xf, info >> 4, STT_FUNC, STB_GLOBAL); |
| 611 |
#endif |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
parser->sym_strings = str_table->offset; |
| 616 |
parser->n_symbols = n_symbols; |
| 617 |
|
| 618 |
/* Allocate space for at least one symbol, so that parser->symbols will be |
| 619 |
* non-NULL. If it ends up being NULL, we will be parsing the file over and |
| 620 |
* over. |
| 621 |
*/ |
| 622 |
parser->symbols = g_renew (ElfSym, parser->symbols, parser->n_symbols + 1); |
| 623 |
|
| 624 |
qsort (parser->symbols, parser->n_symbols, sizeof (ElfSym), compare_sym); |
| 625 |
} |
| 626 |
|
| 627 |
static void |
| 628 |
read_symbols (ElfParser *parser) |
| 629 |
{ |
| 630 |
const Section *symtab = find_section (parser, ".symtab", SHT_SYMTAB); |
| 631 |
const Section *strtab = find_section (parser, ".strtab", SHT_STRTAB); |
| 632 |
const Section *dynsym = find_section (parser, ".dynsym", SHT_DYNSYM); |
| 633 |
const Section *dynstr = find_section (parser, ".dynstr", SHT_STRTAB); |
| 634 |
|
| 635 |
if (symtab && strtab) |
| 636 |
{ |
| 637 |
#if 0 |
| 638 |
g_print ("reading symbol table of %s\n", parser->filename); |
| 639 |
#endif |
| 640 |
read_table (parser, symtab, strtab); |
| 641 |
} |
| 642 |
else if (dynsym && dynstr) |
| 643 |
{ |
| 644 |
#if 0 |
| 645 |
g_print ("reading dynamic symbol table of %s\n", parser->filename); |
| 646 |
#endif |
| 647 |
read_table (parser, dynsym, dynstr); |
| 648 |
} |
| 649 |
else |
| 650 |
{ |
| 651 |
/* To make sure parser->symbols is non-NULL */ |
| 652 |
parser->n_symbols = 0; |
| 653 |
parser->symbols = g_new (ElfSym, 1); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
static ElfSym * |
| 658 |
do_lookup (ElfSym *symbols, |
| 659 |
gulong address, |
| 660 |
int first, |
| 661 |
int last) |
| 662 |
{ |
| 663 |
if (address >= symbols[last].address) |
| 664 |
{ |
| 665 |
return &(symbols[last]); |
| 666 |
} |
| 667 |
else if (last - first < 3) |
| 668 |
{ |
| 669 |
while (last >= first) |
| 670 |
{ |
| 671 |
if (address >= symbols[last].address) |
| 672 |
return &(symbols[last]); |
| 673 |
|
| 674 |
last--; |
| 675 |
} |
| 676 |
|
| 677 |
return NULL; |
| 678 |
} |
| 679 |
else |
| 680 |
{ |
| 681 |
int mid = (first + last) / 2; |
| 682 |
|
| 683 |
if (symbols[mid].address > address) |
| 684 |
return do_lookup (symbols, address, first, mid); |
| 685 |
else |
| 686 |
return do_lookup (symbols, address, mid, last); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
/* Address should be given in 'offset into text segment' */ |
| 691 |
const ElfSym * |
| 692 |
elf_parser_lookup_symbol (ElfParser *parser, |
| 693 |
gulong address) |
| 694 |
{ |
| 695 |
const ElfSym *result; |
| 696 |
|
| 697 |
if (!parser->symbols) |
| 698 |
{ |
| 699 |
#if 0 |
| 700 |
g_print ("reading symbols at %p\n", parser); |
| 701 |
#endif |
| 702 |
read_symbols (parser); |
| 703 |
} |
| 704 |
|
| 705 |
if (parser->n_symbols == 0) |
| 706 |
return NULL; |
| 707 |
|
| 708 |
if (!parser->text_section) |
| 709 |
return NULL; |
| 710 |
|
| 711 |
address += parser->text_section->load_address; |
| 712 |
|
| 713 |
#if 0 |
| 714 |
g_print ("elf: the address we are looking up is %p\n", address); |
| 715 |
#endif |
| 716 |
|
| 717 |
result = do_lookup (parser->symbols, address, 0, parser->n_symbols - 1); |
| 718 |
|
| 719 |
#if 0 |
| 720 |
if (result) |
| 721 |
{ |
| 722 |
g_print (" elf: found %s at %lx\n", elf_parser_get_sym_name (parser, result), result->address); |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
g_print ("elf: not found\n"); |
| 727 |
} |
| 728 |
#endif |
| 729 |
|
| 730 |
if (result) |
| 731 |
{ |
| 732 |
gulong size = st_size (parser, result->table, result->offset); |
| 733 |
|
| 734 |
if (size > 0 && result->address + size <= address) |
| 735 |
{ |
| 736 |
#if 0 |
| 737 |
g_print (" elf: ends at %lx, so rejecting\n", |
| 738 |
result->address + size); |
| 739 |
#endif |
| 740 |
result = NULL; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
if (result) |
| 745 |
{ |
| 746 |
/* Reject the symbols if the address is outside the text section */ |
| 747 |
if (address > parser->text_section->load_address + parser->text_section->size) |
| 748 |
result = NULL; |
| 749 |
} |
| 750 |
|
| 751 |
return result; |
| 752 |
} |
| 753 |
|
| 754 |
gulong |
| 755 |
elf_parser_get_text_offset (ElfParser *parser) |
| 756 |
{ |
| 757 |
g_return_val_if_fail (parser != NULL, (gulong)-1); |
| 758 |
|
| 759 |
if (!parser->text_section) |
| 760 |
return (gulong)-1; |
| 761 |
|
| 762 |
return parser->text_section->offset; |
| 763 |
} |
| 764 |
|
| 765 |
static gchar * |
| 766 |
make_hex_string (const guchar *data, int n_bytes) |
| 767 |
{ |
| 768 |
static const char hex_digits[] = { |
| 769 |
'0', '1', '2', '3', '4', '5', '6', '7', |
| 770 |
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 771 |
}; |
| 772 |
GString *string = g_string_new (NULL); |
| 773 |
int i; |
| 774 |
|
| 775 |
for (i = 0; i < n_bytes; ++i) |
| 776 |
{ |
| 777 |
char c = data[i]; |
| 778 |
|
| 779 |
g_string_append_c (string, hex_digits[(c & 0xf0) >> 4]); |
| 780 |
g_string_append_c (string, hex_digits[(c & 0x0f)]); |
| 781 |
} |
| 782 |
|
| 783 |
return g_string_free (string, FALSE); |
| 784 |
} |
| 785 |
|
| 786 |
const gchar * |
| 787 |
elf_parser_get_build_id (ElfParser *parser) |
| 788 |
{ |
| 789 |
if (!parser->checked_build_id) |
| 790 |
{ |
| 791 |
const Section *build_id = |
| 792 |
find_section (parser, ".note.gnu.build-id", SHT_NOTE); |
| 793 |
guint64 name_size; |
| 794 |
guint64 desc_size; |
| 795 |
guint64 type; |
| 796 |
const char *name; |
| 797 |
guint64 offset; |
| 798 |
|
| 799 |
parser->checked_build_id = TRUE; |
| 800 |
|
| 801 |
if (!build_id) |
| 802 |
return NULL; |
| 803 |
|
| 804 |
offset = build_id->offset; |
| 805 |
|
| 806 |
name_size = GET_FIELD (parser, offset, Nhdr, 0, n_namesz); |
| 807 |
desc_size = GET_FIELD (parser, offset, Nhdr, 0, n_descsz); |
| 808 |
type = GET_FIELD (parser, offset, Nhdr, 0, n_type); |
| 809 |
|
| 810 |
offset += GET_SIZE (parser, Nhdr); |
| 811 |
|
| 812 |
name = (char *)(parser->data + offset); |
| 813 |
|
| 814 |
if (strncmp (name, ELF_NOTE_GNU, name_size) != 0 || type != NT_GNU_BUILD_ID) |
| 815 |
return NULL; |
| 816 |
|
| 817 |
offset += strlen (name); |
| 818 |
|
| 819 |
offset = (offset + 3) & (~0x3); |
| 820 |
|
| 821 |
parser->build_id = make_hex_string (parser->data + offset, desc_size); |
| 822 |
} |
| 823 |
|
| 824 |
return parser->build_id; |
| 825 |
} |
| 826 |
|
| 827 |
const char * |
| 828 |
elf_parser_get_debug_link (ElfParser *parser, |
| 829 |
guint32 *crc32) |
| 830 |
{ |
| 831 |
guint64 offset; |
| 832 |
const Section *debug_link = find_section (parser, ".gnu_debuglink", |
| 833 |
SHT_PROGBITS); |
| 834 |
const gchar *result; |
| 835 |
|
| 836 |
if (!debug_link) |
| 837 |
return NULL; |
| 838 |
|
| 839 |
offset = debug_link->offset; |
| 840 |
|
| 841 |
result = (char *)(parser->data + offset); |
| 842 |
|
| 843 |
if (crc32) |
| 844 |
{ |
| 845 |
int len = strlen (result) + 1; |
| 846 |
offset = (offset + len + 3) & ~0x3; |
| 847 |
|
| 848 |
*crc32 = GET_UINT32 (parser, offset); |
| 849 |
} |
| 850 |
|
| 851 |
return result; |
| 852 |
} |
| 853 |
|
| 854 |
static const guchar * |
| 855 |
get_section (ElfParser *parser, |
| 856 |
const char *name) |
| 857 |
{ |
| 858 |
const Section *section = find_section (parser, name, SHT_PROGBITS); |
| 859 |
|
| 860 |
if (section) |
| 861 |
return parser->data + section->offset; |
| 862 |
else |
| 863 |
return NULL; |
| 864 |
} |
| 865 |
|
| 866 |
const guchar * |
| 867 |
elf_parser_get_eh_frame (ElfParser *parser) |
| 868 |
{ |
| 869 |
return get_section (parser, ".eh_frame"); |
| 870 |
} |
| 871 |
|
| 872 |
const guchar * |
| 873 |
elf_parser_get_debug_frame (ElfParser *parser) |
| 874 |
{ |
| 875 |
return get_section (parser, ".debug_frame"); |
| 876 |
} |
| 877 |
|
| 878 |
const char * |
| 879 |
elf_parser_get_sym_name (ElfParser *parser, |
| 880 |
const ElfSym *sym) |
| 881 |
{ |
| 882 |
g_return_val_if_fail (parser != NULL, NULL); |
| 883 |
|
| 884 |
return (char *)(parser->data + parser->sym_strings + |
| 885 |
st_name (parser, sym->table, sym->offset)); |
| 886 |
} |
| 887 |
|
| 888 |
gboolean |
| 889 |
elf_parser_owns_symbol (ElfParser *parser, |
| 890 |
const ElfSym *sym) |
| 891 |
{ |
| 892 |
ElfSym *first, *last; |
| 893 |
|
| 894 |
if (!parser->n_symbols) |
| 895 |
return FALSE; |
| 896 |
|
| 897 |
first = parser->symbols; |
| 898 |
last = parser->symbols + parser->n_symbols - 1; |
| 899 |
|
| 900 |
return first <= sym && sym <= last; |
| 901 |
} |
| 902 |
|
| 903 |
gulong |
| 904 |
elf_parser_get_sym_address (ElfParser *parser, |
| 905 |
const ElfSym *sym) |
| 906 |
{ |
| 907 |
return sym->address - parser->text_section->load_address; |
| 908 |
} |
| 909 |
|
| 910 |
void |
| 911 |
elf_parser_get_sym_address_range (ElfParser *parser, |
| 912 |
const ElfSym *sym, |
| 913 |
gulong *begin, |
| 914 |
gulong *end) |
| 915 |
{ |
| 916 |
*begin = sym->address - parser->text_section->load_address; |
| 917 |
*end = *begin + st_size (parser, sym->table, sym->offset); |
| 918 |
} |