2
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
#include "precompiled.hpp"
27
#if !defined(_WINDOWS) && !defined(__APPLE__)
30
#include "logging/log.hpp"
31
#include "memory/allocation.inline.hpp"
32
#include "utilities/checkedCast.hpp"
33
#include "utilities/decoder.hpp"
34
#include "utilities/elfFile.hpp"
35
#include "utilities/elfFuncDescTable.hpp"
36
#include "utilities/elfStringTable.hpp"
37
#include "utilities/elfSymbolTable.hpp"
38
#include "utilities/ostream.hpp"
45
const char* ElfFile::USR_LIB_DEBUG_DIRECTORY = "/usr/lib/debug";
47
// For test only, disable elf section cache and force to read from file directly.
48
bool ElfFile::_do_not_cache_elf_section = false;
50
ElfSection::ElfSection(FILE* fd, const Elf_Shdr& hdr) : _section_data(nullptr) {
51
_stat = load_section(fd, hdr);
54
ElfSection::~ElfSection() {
55
if (_section_data != nullptr) {
56
os::free(_section_data);
60
NullDecoder::decoder_status ElfSection::load_section(FILE* const fd, const Elf_Shdr& shdr) {
61
memcpy((void*)&_section_hdr, (const void*)&shdr, sizeof(shdr));
63
if (ElfFile::_do_not_cache_elf_section) {
64
log_develop_debug(decoder)("Elf section cache is disabled");
65
return NullDecoder::no_error;
68
_section_data = os::malloc(shdr.sh_size, mtInternal);
69
// No enough memory for caching. It is okay, we can try to read from
71
if (_section_data == nullptr) return NullDecoder::no_error;
73
MarkedFileReader mfd(fd);
75
mfd.set_position(shdr.sh_offset) &&
76
mfd.read(_section_data, shdr.sh_size)) {
77
return NullDecoder::no_error;
79
os::free(_section_data);
80
_section_data = nullptr;
81
return NullDecoder::file_invalid;
85
bool FileReader::read(void* buf, size_t size) {
86
assert(buf != nullptr, "no buffer");
87
assert(size > 0, "no space");
88
return fread(buf, size, 1, _fd) == 1;
91
size_t FileReader::read_buffer(void* buf, size_t size) {
92
assert(buf != nullptr, "no buffer");
93
assert(size > 0, "no space");
94
return fread(buf, 1, size, _fd);
97
bool FileReader::set_position(long offset) {
98
return fseek(_fd, offset, SEEK_SET) == 0;
101
MarkedFileReader::MarkedFileReader(FILE* fd) : FileReader(fd), _marked_pos(ftell(fd)) {
104
MarkedFileReader::~MarkedFileReader() {
105
if (_marked_pos != -1) {
106
set_position(_marked_pos);
110
ElfFile::ElfFile(const char* filepath) :
111
_next(nullptr), _filepath(os::strdup(filepath)), _file(nullptr),
112
_symbol_tables(nullptr), _string_tables(nullptr), _shdr_string_table(nullptr), _funcDesc_table(nullptr),
113
_status(NullDecoder::no_error), _dwarf_file(nullptr) {
114
memset(&_elfHdr, 0, sizeof(_elfHdr));
115
if (_filepath == nullptr) {
116
_status = NullDecoder::out_of_memory;
118
_status = parse_elf(filepath);
125
if (_file != nullptr) {
129
if (_filepath != nullptr) {
130
os::free((void*) _filepath);
134
if (_shdr_string_table != nullptr) {
135
delete _shdr_string_table;
136
_shdr_string_table = nullptr;
139
if (_next != nullptr) {
144
if (_dwarf_file != nullptr) {
146
_dwarf_file = nullptr;
150
void ElfFile::cleanup_tables() {
151
if (_string_tables != nullptr) {
152
delete _string_tables;
153
_string_tables = nullptr;
155
if (_symbol_tables != nullptr) {
156
delete _symbol_tables;
157
_symbol_tables = nullptr;
159
if (_funcDesc_table != nullptr) {
160
delete _funcDesc_table;
161
_funcDesc_table = nullptr;
165
NullDecoder::decoder_status ElfFile::parse_elf(const char* filepath) {
166
assert(filepath, "null file path");
168
_file = os::fopen(filepath, "r");
169
if (_file != nullptr) {
170
return load_tables();
172
return NullDecoder::file_not_found;
176
//Check elf header to ensure the file is valid.
177
bool ElfFile::is_elf_file(Elf_Ehdr& hdr) {
178
return (ELFMAG0 == hdr.e_ident[EI_MAG0] &&
179
ELFMAG1 == hdr.e_ident[EI_MAG1] &&
180
ELFMAG2 == hdr.e_ident[EI_MAG2] &&
181
ELFMAG3 == hdr.e_ident[EI_MAG3] &&
182
ELFCLASSNONE != hdr.e_ident[EI_CLASS] &&
183
ELFDATANONE != hdr.e_ident[EI_DATA]);
186
NullDecoder::decoder_status ElfFile::load_tables() {
187
assert(_file, "file not open");
188
assert(!NullDecoder::is_error(_status), "already in error");
190
FileReader freader(fd());
191
// read elf file header
192
if (!freader.read(&_elfHdr, sizeof(_elfHdr))) {
193
return NullDecoder::file_invalid;
197
if (!is_elf_file(_elfHdr)) {
198
return NullDecoder::file_invalid;
201
// walk elf file's section headers, and load string tables
203
if (!freader.set_position(_elfHdr.e_shoff)) {
204
return NullDecoder::file_invalid;
207
for (int index = 0; index < _elfHdr.e_shnum; index ++) {
208
if (!freader.read(&shdr, sizeof(shdr))) {
209
return NullDecoder::file_invalid;
212
if (shdr.sh_type == SHT_STRTAB) {
214
ElfStringTable* table = new (std::nothrow) ElfStringTable(fd(), shdr, index);
215
if (table == nullptr) {
216
return NullDecoder::out_of_memory;
218
if (index == _elfHdr.e_shstrndx) {
219
assert(_shdr_string_table == nullptr, "Only set once");
220
_shdr_string_table = table;
222
add_string_table(table);
224
} else if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
226
ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(fd(), shdr);
227
if (table == nullptr) {
228
return NullDecoder::out_of_memory;
230
add_symbol_table(table);
233
#if defined(PPC64) && !defined(ABI_ELFv2)
234
// Now read the .opd section which contains the PPC64 function descriptor table.
235
// The .opd section is only available on PPC64 (see for example:
236
// http://refspecs.linuxfoundation.org/LSB_3.1.1/LSB-Core-PPC64/LSB-Core-PPC64/specialsections.html)
237
// so this code should do no harm on other platforms but because of performance reasons we only
238
// execute it on PPC64 platforms.
239
// Notice that we can only find the .opd section after we have successfully read in the string
240
// tables in the previous loop, because we need to query the name of each section which is
241
// contained in one of the string tables (i.e. the one with the index m_elfHdr.e_shstrndx).
243
// Reset the file pointer
244
int sect_index = section_by_name(".opd", shdr);
246
if (sect_index == -1) {
247
return NullDecoder::file_invalid;
250
_funcDesc_table = new (std::nothrow) ElfFuncDescTable(_file, shdr, sect_index);
251
if (_funcDesc_table == nullptr) {
252
return NullDecoder::out_of_memory;
255
return NullDecoder::no_error;
258
#if defined(PPC64) && !defined(ABI_ELFv2)
259
int ElfFile::section_by_name(const char* name, Elf_Shdr& hdr) {
260
assert(name != nullptr, "No section name");
261
size_t len = strlen(name) + 1;
262
char* buf = (char*)os::malloc(len, mtInternal);
263
if (buf == nullptr) {
267
assert(_shdr_string_table != nullptr, "Section header string table should be loaded");
268
ElfStringTable* const table = _shdr_string_table;
269
MarkedFileReader mfd(fd());
270
if (!mfd.has_mark() || !mfd.set_position(_elfHdr.e_shoff)) return -1;
273
for (int index = 0; index < _elfHdr.e_shnum; index ++) {
274
if (!mfd.read((void*)&hdr, sizeof(hdr))) {
277
if (table->string_at(hdr.sh_name, buf, len)) {
278
if (strncmp(buf, name, len) == 0) {
291
bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) {
292
// something already went wrong, just give up
293
if (NullDecoder::is_error(_status)) {
297
int string_table_index;
298
int pos_in_string_table;
300
bool found_symbol = false;
301
ElfSymbolTable* symbol_table = _symbol_tables;
303
while (symbol_table != nullptr) {
304
if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off, _funcDesc_table)) {
308
symbol_table = symbol_table->next();
314
ElfStringTable* string_table = get_string_table(string_table_index);
316
if (string_table == nullptr) {
317
_status = NullDecoder::file_invalid;
320
if (offset) *offset = off;
322
return string_table->string_at(pos_in_string_table, buf, buflen);
325
void ElfFile::add_symbol_table(ElfSymbolTable* table) {
326
if (_symbol_tables == nullptr) {
327
_symbol_tables = table;
329
table->set_next(_symbol_tables);
330
_symbol_tables = table;
334
void ElfFile::add_string_table(ElfStringTable* table) {
335
if (_string_tables == nullptr) {
336
_string_tables = table;
338
table->set_next(_string_tables);
339
_string_tables = table;
343
ElfStringTable* ElfFile::get_string_table(int index) {
344
ElfStringTable* p = _string_tables;
345
while (p != nullptr) {
346
if (p->index() == index) return p;
352
// Use unified logging to report errors rather than assert() throughout this method as this code is already part of the error reporting
353
// and the debug symbols might be in an unsupported DWARF version or wrong format.
354
bool ElfFile::get_source_info(const uint32_t offset_in_library, char* filename, const size_t filename_len, int* line, bool is_pc_after_call) {
355
if (!load_dwarf_file()) {
356
// Some ELF libraries do not provide separate .debuginfo files. Check if the current ELF file has the required
357
// DWARF sections. If so, treat the current ELF file as DWARF file.
358
if (!is_valid_dwarf_file()) {
359
DWARF_LOG_ERROR("Failed to load DWARF file for library %s or find DWARF sections directly inside it.", _filepath);
362
DWARF_LOG_INFO("No separate .debuginfo file for library %s. It already contains the required DWARF sections.",
364
if (!create_new_dwarf_file(_filepath)) {
369
// Store result in filename and line pointer.
370
if (!_dwarf_file->get_filename_and_line_number(offset_in_library, filename, filename_len, line, is_pc_after_call)) {
371
DWARF_LOG_ERROR("Failed to retrieve file and line number information for %s at offset: " UINT32_FORMAT_X_0, _filepath,
378
bool ElfFile::is_valid_dwarf_file() const {
380
return read_section_header(".debug_abbrev", shdr) && read_section_header(".debug_aranges", shdr)
381
&& read_section_header(".debug_info", shdr) && read_section_header(".debug_line", shdr);
384
// (1) Load the debuginfo file from the path specified in this ELF file in the .gnu_debuglink section.
385
// Adapted from Serviceability Agent.
386
bool ElfFile::load_dwarf_file() {
387
if (_dwarf_file != nullptr) {
388
return true; // Already opened.
391
DebugInfo debug_info;
392
if (!read_debug_info(&debug_info)) {
393
DWARF_LOG_DEBUG("Could not read debug info from .gnu_debuglink section");
397
DwarfFilePath dwarf_file_path(debug_info);
398
return load_dwarf_file_from_same_directory(dwarf_file_path)
399
|| load_dwarf_file_from_env_var_path(dwarf_file_path)
400
|| load_dwarf_file_from_debug_sub_directory(dwarf_file_path)
401
|| load_dwarf_file_from_usr_lib_debug(dwarf_file_path);
404
// Read .gnu_debuglink section which contains:
405
// Filename (null terminated) + 0-3 padding bytes (to 4 byte align) + CRC (4 bytes)
406
bool ElfFile::read_debug_info(DebugInfo* debug_info) const {
408
if (!read_section_header(".gnu_debuglink", shdr)) {
409
DWARF_LOG_DEBUG("Failed to read the .gnu_debuglink header.");
413
if (shdr.sh_size % 4 != 0) {
414
DWARF_LOG_ERROR(".gnu_debuglink section is not 4 byte aligned (i.e. file is corrupted)");
418
MarkedFileReader mfd(fd());
419
if (!mfd.has_mark() || !mfd.set_position(_elfHdr.e_shoff)) {
423
uint64_t filename_max_len = shdr.sh_size - DebugInfo::CRC_LEN;
424
mfd.set_position(shdr.sh_offset);
425
if (!mfd.read(&debug_info->_dwarf_filename, filename_max_len)) {
429
if (debug_info->_dwarf_filename[filename_max_len - 1] != '\0') {
430
// Filename not null-terminated (i.e. overflowed).
431
DWARF_LOG_ERROR("Dwarf filename is not null-terminated");
435
return mfd.read(&debug_info->_crc, DebugInfo::CRC_LEN);
438
bool ElfFile::DwarfFilePath::set(const char* src) {
439
int bytes_written = jio_snprintf(_path, MAX_DWARF_PATH_LENGTH, "%s", src);
440
if (bytes_written < 0 || bytes_written >= MAX_DWARF_PATH_LENGTH) {
441
DWARF_LOG_ERROR("Dwarf file path buffer is too small");
444
update_null_terminator_index();
445
return check_valid_path(); // Sanity check
448
bool ElfFile::DwarfFilePath::set_after_last_slash(const char* src) {
449
char* last_slash = strrchr(_path, *os::file_separator());
450
if (last_slash == nullptr) {
451
// Should always find a slash.
455
uint16_t index_after_slash = (uint16_t)(last_slash + 1 - _path);
456
return copy_to_path_index(index_after_slash, src);
459
bool ElfFile::DwarfFilePath::append(const char* src) {
460
return copy_to_path_index(_null_terminator_index, src);
463
bool ElfFile::DwarfFilePath::copy_to_path_index(uint16_t index_in_path, const char* src) {
464
if (index_in_path >= MAX_DWARF_PATH_LENGTH - 1) {
465
// Should not override '\0' at _path[MAX_DWARF_PATH_LENGTH - 1]
466
DWARF_LOG_ERROR("Dwarf file path buffer is too small");
470
uint16_t max_len = MAX_DWARF_PATH_LENGTH - index_in_path;
471
int bytes_written = jio_snprintf(_path + index_in_path, max_len, "%s", src);
472
if (bytes_written < 0 || bytes_written >= max_len) {
473
DWARF_LOG_ERROR("Dwarf file path buffer is too small");
476
update_null_terminator_index();
477
return check_valid_path(); // Sanity check
480
// Try to load the dwarf file from the same directory as the library file.
481
bool ElfFile::load_dwarf_file_from_same_directory(DwarfFilePath& dwarf_file_path) {
482
if (!dwarf_file_path.set(_filepath)
483
|| !dwarf_file_path.set_filename_after_last_slash()) {
486
return open_valid_debuginfo_file(dwarf_file_path);
489
// Try to load the dwarf file from a user specified path in environmental variable _JVM_DWARF_PATH.
490
bool ElfFile::load_dwarf_file_from_env_var_path(DwarfFilePath& dwarf_file_path) {
491
const char* dwarf_path_from_env = ::getenv("_JVM_DWARF_PATH");
492
if (dwarf_path_from_env != nullptr) {
493
DWARF_LOG_DEBUG("_JVM_DWARF_PATH: %s", dwarf_path_from_env);
494
return (load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/lib/server/")
495
|| load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/lib/")
496
|| load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/bin/")
497
|| load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/"));
502
bool ElfFile::load_dwarf_file_from_env_path_folder(DwarfFilePath& dwarf_file_path, const char* dwarf_path_from_env,
503
const char* folder) {
504
if (!dwarf_file_path.set(dwarf_path_from_env)
505
|| !dwarf_file_path.append(folder)
506
|| !dwarf_file_path.append(dwarf_file_path.filename())) {
507
DWARF_LOG_ERROR("Dwarf file path buffer is too small");
510
return open_valid_debuginfo_file(dwarf_file_path);
513
// Try to load the dwarf file from a subdirectory named .debug within the directory of the library file.
514
bool ElfFile::load_dwarf_file_from_debug_sub_directory(DwarfFilePath& dwarf_file_path) {
515
if (!dwarf_file_path.set(_filepath)
516
|| !dwarf_file_path.set_after_last_slash(".debug/")
517
|| !dwarf_file_path.append(dwarf_file_path.filename())) {
518
DWARF_LOG_ERROR("Dwarf file path buffer is too small");
521
return open_valid_debuginfo_file(dwarf_file_path);
524
// Try to load the dwarf file from /usr/lib/debug + the full pathname.
525
bool ElfFile::load_dwarf_file_from_usr_lib_debug(DwarfFilePath& dwarf_file_path) {
526
if (!dwarf_file_path.set(USR_LIB_DEBUG_DIRECTORY)
527
|| !dwarf_file_path.append(_filepath)
528
|| !dwarf_file_path.set_filename_after_last_slash()) {
529
DWARF_LOG_ERROR("Dwarf file path buffer is too small");
532
return open_valid_debuginfo_file(dwarf_file_path);
535
bool ElfFile::read_section_header(const char* name, Elf_Shdr& hdr) const {
536
if (_shdr_string_table == nullptr) {
537
assert(false, "section header string table should be loaded");
540
const uint8_t buf_len = 24;
542
size_t len = strlen(name) + 1;
544
DWARF_LOG_ERROR("Section header name buffer is too small: Required: %zu, Found: %d", len, buf_len);
548
MarkedFileReader mfd(fd());
549
if (!mfd.has_mark() || !mfd.set_position(_elfHdr.e_shoff)) {
553
for (int index = 0; index < _elfHdr.e_shnum; index++) {
554
if (!mfd.read((void*)&hdr, sizeof(hdr))) {
557
if (_shdr_string_table->string_at(hdr.sh_name, buf, buf_len)) {
558
if (strncmp(buf, name, buf_len) == 0) {
566
// Taken from https://sourceware.org/gdb/current/onlinedocs/gdb/Separate-Debug-Files.html#Separate-Debug-Files
567
static const uint32_t crc32_table[256] = {
568
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
569
0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
570
0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
571
0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
572
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
573
0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
574
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
575
0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
576
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
577
0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
578
0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
579
0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
580
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
581
0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
582
0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
583
0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
584
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
585
0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
586
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
587
0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
588
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
589
0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
590
0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
591
0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
592
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
593
0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
594
0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
595
0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
596
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
597
0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
598
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
599
0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
600
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
601
0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
602
0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
603
0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
604
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
605
0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
606
0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
607
0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
608
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
609
0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
610
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
611
0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
612
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
613
0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
614
0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
615
0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
616
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
617
0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
618
0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
622
bool ElfFile::open_valid_debuginfo_file(const DwarfFilePath& dwarf_file_path) {
623
if (_dwarf_file != nullptr) {
628
const char* filepath = dwarf_file_path.path();
629
FILE* file = fopen(filepath, "r");
630
if (file == nullptr) {
631
DWARF_LOG_DEBUG("Could not open dwarf file %s (%s)", filepath, os::strerror(errno));
635
uint32_t file_crc = get_file_crc(file);
636
fclose(file); // Close it here to reopen it again when the DwarfFile object is created below.
638
if (dwarf_file_path.crc() != file_crc) {
639
// Must be equal, otherwise the file is corrupted.
640
DWARF_LOG_ERROR("CRC did not match. Expected: " INT32_FORMAT_X_0 ", found: " INT32_FORMAT_X_0, dwarf_file_path.crc(),
644
return create_new_dwarf_file(filepath);
647
uint32_t ElfFile::get_file_crc(FILE* const file) {
648
uint32_t file_crc = 0;
649
uint8_t buffer[8 * 1024];
650
MarkedFileReader reader(file);
652
size_t len = reader.read_buffer(buffer, sizeof(buffer));
656
file_crc = gnu_debuglink_crc32(file_crc, buffer, len);
661
// The CRC used in gnu_debuglink, retrieved from
662
// http://sourceware.org/gdb/current/onlinedocs/gdb/Separate-Debug-Files.html#Separate-Debug-Files.
663
uint32_t ElfFile::gnu_debuglink_crc32(uint32_t crc, uint8_t* buf, const size_t len) {
665
for (uint8_t* end = buf + len; buf < end; buf++) {
666
crc = crc32_table[(crc ^ *buf) & 0xffu] ^ (crc >> 8u);
671
bool ElfFile::create_new_dwarf_file(const char* filepath) {
672
DWARF_LOG_SUMMARY("Open DWARF file: %s", filepath);
673
_dwarf_file = new (std::nothrow) DwarfFile(filepath);
674
if (_dwarf_file == nullptr) {
675
DWARF_LOG_ERROR("Failed to create new DwarfFile object for %s.", _filepath);
678
if (!_dwarf_file->is_valid_dwarf_file()) {
679
DWARF_LOG_ERROR("Did not find required DWARF sections in %s", filepath);
685
// Starting point of reading line number and filename information from the DWARF file.
686
bool DwarfFile::get_filename_and_line_number(const uint32_t offset_in_library, char* filename, const size_t filename_len,
687
int* line, const bool is_pc_after_call) {
688
DebugAranges debug_aranges(this);
689
uint32_t compilation_unit_offset = 0; // 4-bytes for 32-bit DWARF
690
if (!debug_aranges.find_compilation_unit_offset(offset_in_library, &compilation_unit_offset)) {
691
DWARF_LOG_ERROR("Failed to find .debug_info offset for the compilation unit.");
694
DWARF_LOG_INFO(".debug_info offset: " INT32_FORMAT_X_0, compilation_unit_offset);
696
CompilationUnit compilation_unit(this, compilation_unit_offset);
697
uint32_t debug_line_offset = 0; // 4-bytes for 32-bit DWARF
698
if (!compilation_unit.find_debug_line_offset(&debug_line_offset)) {
699
DWARF_LOG_ERROR("Failed to find .debug_line offset for the line number program.");
702
DWARF_LOG_INFO(".debug_line offset: " INT32_FORMAT_X_0, debug_line_offset);
704
LineNumberProgram line_number_program(this, offset_in_library, debug_line_offset, is_pc_after_call);
705
if (!line_number_program.find_filename_and_line_number(filename, filename_len, line)) {
706
DWARF_LOG_ERROR("Failed to process the line number program correctly.");
712
// (2) The .debug_aranges section contains a number of entries/sets. Each set contains one or multiple address range descriptors of the
713
// form [beginning_address, beginning_address+length). Start reading these sets and their descriptors until we find one that contains
714
// 'offset_in_library'. Read the debug_info_offset field from the header of this set which defines the offset for the compilation unit.
715
// This process is described in section 6.1.2 of the DWARF 4 spec.
716
bool DwarfFile::DebugAranges::find_compilation_unit_offset(const uint32_t offset_in_library, uint32_t* compilation_unit_offset) {
717
if (!read_section_header()) {
718
DWARF_LOG_ERROR("Failed to read a .debug_aranges header.");
722
DebugArangesSetHeader set_header;
723
bool found_matching_set = false;
724
while (_reader.has_bytes_left()) {
725
// Read multiple sets and therefore multiple headers.
726
if (!read_set_header(set_header)) {
727
DWARF_LOG_ERROR("Failed to read a .debug_aranges header.");
731
if (!read_address_descriptors(set_header, offset_in_library, found_matching_set)) {
735
if (found_matching_set) {
736
// Found the correct set, read the debug_info_offset from the header of this set.
737
DWARF_LOG_INFO(".debug_aranges offset: " UINT32_FORMAT, (uint32_t)_reader.get_position());
738
*compilation_unit_offset = set_header._debug_info_offset;
743
assert(false, "No address descriptor found containing offset_in_library.");
747
bool DwarfFile::DebugAranges::read_section_header() {
749
if (!_dwarf_file->read_section_header(".debug_aranges", shdr)) {
753
_section_start_address = shdr.sh_offset;
754
_reader.set_max_pos(shdr.sh_offset + shdr.sh_size);
755
return _reader.set_position(shdr.sh_offset);
758
// Parse set header as specified in section 6.1.2 of the DWARF 4 spec.
759
bool DwarfFile::DebugAranges::read_set_header(DebugArangesSetHeader& header) {
760
if (!_reader.read_dword(&header._unit_length) || header._unit_length == 0xFFFFFFFF) {
761
// For 64-bit DWARF, the first 32-bit value is 0xFFFFFFFF. The current implementation only supports 32-bit DWARF
762
// format since GCC only emits 32-bit DWARF.
763
DWARF_LOG_ERROR("64-bit DWARF is not supported for .debug_aranges")
767
_entry_end = _reader.get_position() + header._unit_length;
769
if (!_reader.read_word(&header._version) || header._version != 2) {
770
// DWARF 4 uses version 2 as specified in Appendix F of the DWARF 4 spec.
771
DWARF_LOG_ERROR(".debug_aranges in unsupported DWARF version %" PRIu16, header._version)
775
if (!_reader.read_dword(&header._debug_info_offset)) {
779
if (!_reader.read_byte(&header._address_size) || header._address_size != DwarfFile::ADDRESS_SIZE) {
780
// Addresses must be either 4 bytes for 32-bit architectures or 8 bytes for 64-bit architectures.
781
DWARF_LOG_ERROR(".debug_aranges specifies wrong address size %" PRIu8, header._address_size);
785
if (!_reader.read_byte(&header._segment_size) || header._segment_size != 0) {
786
// Segment size should be 0.
787
DWARF_LOG_ERROR(".debug_aranges segment size is non-zero: %" PRIu8, header._segment_size);
791
// We must align to twice the address size.
792
uint8_t alignment = DwarfFile::ADDRESS_SIZE * 2;
793
long padding = alignment - (_reader.get_position() - _section_start_address) % alignment;
794
return _reader.move_position(padding);
797
bool DwarfFile::DebugAranges::read_address_descriptors(const DwarfFile::DebugAranges::DebugArangesSetHeader& header,
798
const uint32_t offset_in_library, bool& found_matching_set) {
799
AddressDescriptor descriptor;
801
if (!read_address_descriptor(descriptor)) {
805
if (does_match_offset(offset_in_library, descriptor)) {
806
found_matching_set = true;
809
} while (!is_terminating_entry(header, descriptor) && _reader.has_bytes_left());
811
// Set does not match offset_in_library. Continue with next.
815
bool DwarfFile::DebugAranges::read_address_descriptor(AddressDescriptor& descriptor) {
816
return _reader.read_address_sized(&descriptor.beginning_address)
817
&& _reader.read_address_sized(&descriptor.range_length);
820
bool DwarfFile::DebugAranges::does_match_offset(const uint32_t offset_in_library, const AddressDescriptor& descriptor) {
821
return descriptor.beginning_address <= offset_in_library
822
&& offset_in_library < descriptor.beginning_address + descriptor.range_length;
825
bool DwarfFile::DebugAranges::is_terminating_entry(const DwarfFile::DebugAranges::DebugArangesSetHeader& header,
826
const AddressDescriptor& descriptor) {
827
bool is_terminating = _reader.get_position() >= _entry_end;
828
assert(!is_terminating || (descriptor.beginning_address == 0 && descriptor.range_length == 0),
829
"a terminating entry needs a pair of zero");
830
return is_terminating;
833
// Find the .debug_line offset for the line number program by reading from the .debug_abbrev and .debug_info section.
834
bool DwarfFile::CompilationUnit::find_debug_line_offset(uint32_t* debug_line_offset) {
836
if (!read_header()) {
837
DWARF_LOG_ERROR("Failed to read the compilation unit header.");
841
// (3c) Read the abbreviation code immediately following the compilation unit header which is an offset to the
842
// correct abbreviation table in .debug_abbrev for this compilation unit.
843
uint64_t abbrev_code;
844
if (!_reader.read_uleb128(&abbrev_code)) {
848
DebugAbbrev debug_abbrev(_dwarf_file, this);
849
if (!debug_abbrev.read_section_header(_header._debug_abbrev_offset)) {
850
DWARF_LOG_ERROR("Failed to read the .debug_abbrev header at " UINT32_FORMAT_X_0, _header._debug_abbrev_offset);
853
if (!debug_abbrev.find_debug_line_offset(abbrev_code)) {
856
*debug_line_offset = _debug_line_offset; // Result was stored in _debug_line_offset.
860
// (3a) Parse header as specified in section 7.5.1.1 of the DWARF 4 spec.
861
bool DwarfFile::CompilationUnit::read_header() {
863
if (!_dwarf_file->read_section_header(".debug_info", shdr)) {
864
DWARF_LOG_ERROR("Failed to read the .debug_info section header.");
868
if (!_reader.set_position(shdr.sh_offset + _compilation_unit_offset)) {
872
if (!_reader.read_dword(&_header._unit_length) || _header._unit_length == 0xFFFFFFFF) {
873
// For 64-bit DWARF, the first 32-bit value is 0xFFFFFFFF. The current implementation only supports 32-bit DWARF
874
// format since GCC only emits 32-bit DWARF.
875
DWARF_LOG_ERROR("64-bit DWARF is not supported for .debug_info")
879
if (!_reader.read_word(&_header._version) || _header._version != 4) {
880
// DWARF 4 uses version 4 as specified in Appendix F of the DWARF 4 spec.
881
DWARF_LOG_ERROR(".debug_info in unsupported DWARF version %" PRIu16, _header._version)
885
// (3b) Offset into .debug_abbrev section.
886
if (!_reader.read_dword(&_header._debug_abbrev_offset)) {
890
if (!_reader.read_byte(&_header._address_size) || _header._address_size != DwarfFile::ADDRESS_SIZE) {
891
// Addresses must be either 4 bytes for 32-bit architectures or 8 bytes for 64-bit architectures.
892
DWARF_LOG_ERROR(".debug_info specifies wrong address size %" PRIu8, _header._address_size);
896
// Add because _unit_length is not included.
897
_reader.set_max_pos(_reader.get_position() + _header._unit_length + 4);
901
bool DwarfFile::DebugAbbrev::read_section_header(uint32_t debug_abbrev_offset) {
903
if (!_dwarf_file->read_section_header(".debug_abbrev", shdr)) {
907
_reader.set_max_pos(shdr.sh_offset + shdr.sh_size);
908
if (!_reader.set_position(shdr.sh_offset + debug_abbrev_offset)) {
914
// (3d) The abbreviations table for a compilation unit consists of a series of abbreviation declarations. Each declaration
915
// specifies an abbrev code and a tag. Parse all declarations until we find the declaration which matches 'abbrev_code'.
916
// Read the attribute values from the compilation unit in .debug_info by using the format described in the declaration.
917
// This process is described in section 7.5 and 7.5.3 of the DWARF 4 spec.
918
bool DwarfFile::DebugAbbrev::find_debug_line_offset(const uint64_t abbrev_code) {
919
DWARF_LOG_TRACE("Series of declarations [code, tag]:");
920
AbbreviationDeclaration declaration;
921
while (_reader.has_bytes_left()) {
922
if (!read_declaration(declaration)) {
926
DWARF_LOG_TRACE(" Series of attributes [name, form]:");
927
if (declaration._abbrev_code == abbrev_code) {
928
// Found the correct declaration.
929
if (is_wrong_or_unsupported_format(declaration)) {
932
DWARF_LOG_INFO(".debug_abbrev offset: " UINT32_FORMAT_X_0, (uint32_t)_reader.get_position());
933
DWARF_LOG_TRACE(" Read the following attribute values from compilation unit:");
934
return read_attribute_specifications(true);
936
// Not the correct declaration. Read its attributes and continue with the next declaration.
937
if (!read_attribute_specifications(false)) {
943
assert(false, ".debug_line offset not found");
947
bool DwarfFile::DebugAbbrev::read_declaration(DwarfFile::DebugAbbrev::AbbreviationDeclaration& declaration) {
948
if (!_reader.read_uleb128(&declaration._abbrev_code)) {
952
if (declaration._abbrev_code == 0) {
953
// Reached the end of the abbreviation declarations for this compilation unit.
954
DWARF_LOG_ERROR("abbrev_code not found in any declaration");
958
if (!_reader.read_uleb128(&declaration._tag) || !_reader.read_byte(&declaration._has_children)) {
962
DWARF_LOG_TRACE("Code: " UINT64_FORMAT_X ", Tag: " UINT64_FORMAT_X, declaration._abbrev_code, declaration._tag);
966
bool DwarfFile::DebugAbbrev::is_wrong_or_unsupported_format(const DwarfFile::DebugAbbrev::AbbreviationDeclaration& declaration) {
967
if (declaration._tag != DW_TAG_compile_unit) {
968
// Is not DW_TAG_compile_unit as specified in Figure 18 in section 7.5 of the DWARF 4 spec. It could also
969
// be DW_TAG_partial_unit (0x3c) which is currently not supported by this parser.
970
DWARF_LOG_ERROR("Found unsupported tag in compilation unit: " UINT64_FORMAT_X, declaration._tag);
973
if (declaration._has_children != DW_CHILDREN_yes) {
974
DWARF_LOG_ERROR("Must have children but none specified");
980
// Read the attribute names and forms which define the actual attribute values that follow the abbrev code in the compilation unit. All
981
// attributes need to be read from the compilation unit until we reach the DW_AT_stmt_list attribute which specifies the offset for the
982
// line number program into the .debug_line section. The offset is stored in the _debug_line_offset field of the compilation unit.
983
bool DwarfFile::DebugAbbrev::read_attribute_specifications(const bool is_DW_TAG_compile_unit) {
984
AttributeSpecification attribute_specification;
985
while (_reader.has_bytes_left()) {
986
if (!read_attribute_specification(attribute_specification)) {
990
if (is_terminating_specification(attribute_specification)) {
991
// Parsed all attributes of this declaration.
992
if (is_DW_TAG_compile_unit) {
993
DWARF_LOG_ERROR("Did not find DW_AT_stmt_list in .debug_abbrev");
996
// Continue with next declaration if this was not DW_TAG_compile_unit.
1001
if (is_DW_TAG_compile_unit) {
1002
// Read attribute from compilation unit
1003
if (attribute_specification._name == DW_AT_stmt_list) {
1004
// This attribute represents the .debug_line offset. Read it and then stop parsing.
1005
return _compilation_unit->read_attribute_value(attribute_specification._form, true);
1007
// Not DW_AT_stmt_list, read it and continue with the next attribute.
1008
if (!_compilation_unit->read_attribute_value(attribute_specification._form, false)) {
1015
assert(false, ".debug_abbrev section appears to be corrupted");
1019
bool DwarfFile::DebugAbbrev::read_attribute_specification(DwarfFile::DebugAbbrev::AttributeSpecification& specification) {
1020
bool result = _reader.read_uleb128(&specification._name) && _reader.read_uleb128(&specification._form);
1021
DWARF_LOG_TRACE(" Name: " UINT64_FORMAT_X ", Form: " UINT64_FORMAT_X,
1022
specification._name, specification._form);
1026
bool DwarfFile::DebugAbbrev::is_terminating_specification(const DwarfFile::DebugAbbrev::AttributeSpecification& specification) {
1027
return specification._name == 0 && specification._form == 0;
1031
// (3e) Read the actual attribute values from the compilation unit in the .debug_info section. Each attribute has an encoding
1032
// that specifies which values need to be read for it. This is specified in section 7.5.4 of the DWARF 4 spec.
1033
// If is_DW_AT_stmt_list_attribute is:
1034
// - False: Ignore the read attribute value.
1035
// - True: We are going to read the attribute value of the DW_AT_stmt_list attribute which specifies the offset into the
1036
// .debug_line section for the line number program. Store this offset in the _debug_line_offset field.
1037
bool DwarfFile::CompilationUnit::read_attribute_value(const uint64_t attribute_form, const bool is_DW_AT_stmt_list_attribute) {
1038
// Reset to the stored _cur_pos of the reader since the DebugAbbrev reader changed the index into the file with its reader.
1039
_reader.update_to_stored_position();
1040
uint8_t next_byte = 0;
1041
uint16_t next_word = 0;
1042
uint32_t next_dword = 0;
1043
uint64_t next_qword = 0;
1045
switch (attribute_form) {
1047
// Move position by the size of an address.
1048
_reader.move_position(DwarfFile::ADDRESS_SIZE);
1050
case DW_FORM_block2:
1051
// New position: length + data length (next_word)
1052
if (!_reader.read_word(&next_word) || !_reader.move_position(next_word)) {
1056
case DW_FORM_block4:
1057
// New position: length + data length (next_dword)
1058
if (!_reader.read_dword(&next_dword) || !_reader.move_position(next_dword)) {
1064
if (!_reader.move_position(2)) {
1069
case DW_FORM_strp: // 4 bytes in 32-bit DWARF
1070
case DW_FORM_ref_addr: // second type of reference: 4 bytes in 32-bit DWARF
1072
if (!_reader.move_position(4)) {
1078
case DW_FORM_ref_sig8: // 64-bit type signature
1079
if (!_reader.move_position(8)) {
1083
case DW_FORM_string:
1084
if (!_reader.read_string()) {
1089
case DW_FORM_exprloc:
1090
// New position: length + data length (next_qword).
1091
if (!_reader.read_uleb128(&next_qword) || !_reader.move_position(next_qword)) {
1095
case DW_FORM_block1:
1096
// New position: length + data length (next_byte).
1097
if (!_reader.read_byte(&next_byte) || !_reader.move_position(next_byte)) {
1104
case DW_FORM_flag_present:
1105
if (!_reader.move_position(1)) {
1111
case DW_FORM_ref_udata:
1112
if (!_reader.read_uleb128(&next_qword)) {
1116
case DW_FORM_indirect:
1117
// Should not be used and therefore is not supported by this parser.
1118
DWARF_LOG_ERROR("DW_FORM_indirect is not supported.");
1120
case DW_FORM_sec_offset:
1121
if (is_DW_AT_stmt_list_attribute) {
1122
// DW_AT_stmt_list has the DW_FORM_sec_offset attribute encoding. Store the result in _debug_line_offset.
1123
// 4 bytes for 32-bit DWARF.
1124
DWARF_LOG_TRACE(" Name: DW_AT_stmt_list, Form: DW_FORM_sec_offset");
1125
DWARF_LOG_TRACE(" Reading .debug_line offset from compilation unit at " UINT32_FORMAT_X_0,
1126
(uint32_t)_reader.get_position());
1127
if (!_reader.read_dword(&_debug_line_offset)) {
1132
if (!_reader.move_position(DwarfFile::DWARF_SECTION_OFFSET_SIZE)) {
1138
assert(false, "Unknown DW_FORM_* attribute encoding.");
1141
// Reset the index into the file to the original position where the DebugAbbrev reader stopped reading before calling this method.
1142
_reader.reset_to_previous_position();
1146
bool DwarfFile::LineNumberProgram::find_filename_and_line_number(char* filename, const size_t filename_len, int* line) {
1147
if (!read_header()) {
1148
DWARF_LOG_ERROR("Failed to parse the line number program header correctly.");
1151
return run_line_number_program(filename, filename_len, line);
1154
// Parsing header as specified in section 6.2.4 of DWARF 4 spec. We do not read the file_names field, yet.
1155
bool DwarfFile::LineNumberProgram::read_header() {
1157
if (!_dwarf_file->read_section_header(".debug_line", shdr)) {
1158
DWARF_LOG_ERROR("Failed to read the .debug_line section header.");
1162
if (!_reader.set_position(shdr.sh_offset + _debug_line_offset)) {
1166
if (!_reader.read_dword(&_header._unit_length) || _header._unit_length == 0xFFFFFFFF) {
1167
// For 64-bit DWARF, the first 32-bit value is 0xFFFFFFFF. The current implementation only supports 32-bit DWARF
1168
// format since GCC only emits 32-bit DWARF.
1169
DWARF_LOG_ERROR("64-bit DWARF is not supported for .debug_line")
1173
if (!_reader.read_word(&_header._version) || _header._version < 2 || _header._version > 4) {
1174
// DWARF 3 uses version 3 and DWARF 4 uses version 4 as specified in Appendix F of the DWARF 3 and 4 spec, respectively.
1175
// For some reason, GCC is not following the standard here. While GCC emits DWARF 4 for the other parsed sections,
1176
// it chooses a different DWARF standard for .debug_line based on the GCC version:
1177
// - GCC 8 and earlier: .debug_line is in DWARF 2 format (= version 2).
1178
// - GCC 9 and 10: .debug_line is in DWARF 3 format (= version 3).
1179
// - GCC 11: .debug_line is in DWARF 4 format (= version 4).
1180
DWARF_LOG_ERROR(".debug_line in unsupported DWARF version %" PRIu16, _header._version)
1184
if (!_reader.read_dword(&_header._header_length)) {
1188
// To ensure not to read too many bytes in case of file corruption when reading the path_names field.
1189
_reader.set_max_pos(_reader.get_position() + _header._header_length);
1191
if (!_reader.read_byte(&_header._minimum_instruction_length)) {
1195
if (_header._version == 4) {
1196
if (!_reader.read_byte(&_header._maximum_operations_per_instruction)) {
1201
if (!_reader.read_byte(&_header._default_is_stmt)) {
1205
if (!_reader.read_byte(&_header._line_base)) {
1209
if (!_reader.read_byte(&_header._line_range)) {
1213
if (!_reader.read_byte(&_header._opcode_base) || _header._opcode_base - 1 != 12) {
1214
// There are 12 standard opcodes for DWARF 3 and 4.
1215
DWARF_LOG_ERROR("Wrong number of opcodes: %" PRIu8, _header._opcode_base)
1219
for (uint8_t i = 0; i < _header._opcode_base - 1; i++) {
1220
if (!_reader.read_byte(&_header._standard_opcode_lengths[i])) {
1225
// Read field include_directories which is a sequence of path names. These are terminated by a single null byte.
1226
// We do not care about them, just read the strings and move on.
1227
while (_reader.read_string()) { }
1229
// Delay reading file_names until we found the correct file index in the line number program. Store the position where
1230
// the file names start to parse them later. We directly jump to the line number program which starts at offset
1231
// header_size (=HEADER_DESCRIPTION_BYTES + _header_length) + _debug_line_offset
1232
_header._file_names_offset = _reader.get_position();
1233
uint32_t header_size = LineNumberProgramHeader::HEADER_DESCRIPTION_BYTES + _header._header_length;
1234
if (!_reader.set_position(shdr.sh_offset + header_size + _debug_line_offset)) {
1238
// Now reset the max position to where the line number information for this compilation unit ends (i.e. where the state
1239
// machine gets terminated). Add 4 bytes to the offset because the size of the _unit_length field is not included in this
1241
_reader.set_max_pos(shdr.sh_offset + _debug_line_offset + _header._unit_length + 4);
1245
// Create the line number information matrix as described in section 6.2 of the DWARF 4 spec. Try to find the correct entry
1246
// by comparing the address register belonging to each matrix row with _offset_in_library. Once it is found, we can read
1247
// the line number from the line register and the filename by parsing the file_names list from the header until we reach
1248
// the correct filename as specified by the file register.
1250
// If space was not a problem, the .debug_line section could provide a large matrix that contains an entry for each
1251
// compiler instruction that contains the line number, the column number, the filename etc. But that's impractical.
1252
// Two techniques optimize such a matrix:
1253
// (1) If two offsets share the same file, line and column (and discriminator) information, the row is dropped.
1254
// (2) We store a stream of bytes that represent opcodes to be executed in a well-defined state machine language
1255
// instead of actually storing the entire matrix row by row.
1257
// Let's consider a simple example:
1258
// 25: int iFld = 42;
1260
// 27: void bar(int i) {
1267
// Disassembly of foo() with source code:
1269
// 0x55d132: 55 push rbp
1270
// 0x55d133: 48 89 e5 mov rbp,rsp
1272
// 0x55d136: 48 8b 05 b3 ee e8 01 mov rax,QWORD PTR [rip+0x1e8eeb3] # 23ebff0 <iFld>
1273
// 0x55d13d: 8b 00 mov eax,DWORD PTR [rax]
1274
// 0x55d13f: 89 c7 mov edi,eax
1275
// 0x55d141: e8 e2 ff ff ff call 55d128 <_Z3bari>
1278
// 0x55d147: 5d pop rbp
1281
// This would produce the following matrix for foo() where duplicated lines (0x55d133, 0x55d13d, 0x55d13f) were removed
1283
// Address: Line: Column: File:
1288
// When trying to get the line number for a PC, which is translated into an offset address x into the library file, we can either:
1289
// - Directly find the last entry in the matrix for which address == x (there could be multiple entries with the same address).
1290
// - If there is no matching address for x:
1291
// 1. Find two consecutive entries in the matrix for which: address_entry_1 < x < address_entry_2.
1292
// 2. Then take the entry of address_entry_1.
1293
// E.g. x = 0x55d13f -> 0x55d136 < 0x55d13f < 0x55d146 -> Take entry 0x55d136.
1295
// Enable logging with debug level to print the generated line number information matrix.
1296
bool DwarfFile::LineNumberProgram::run_line_number_program(char* filename, const size_t filename_len, int* line) {
1297
DWARF_LOG_DEBUG("");
1298
DWARF_LOG_DEBUG("Line Number Information Matrix");
1299
DWARF_LOG_DEBUG("------------------------------");
1301
DWARF_LOG_DEBUG("Address: Line: Column: File:");
1303
DWARF_LOG_DEBUG("Address: Line: Column: File:");
1305
_state = new (std::nothrow) LineNumberProgramState(_header);
1306
if (_state == nullptr) {
1307
DWARF_LOG_ERROR("Failed to create new LineNumberProgramState object");
1310
uintptr_t previous_address = 0;
1311
uint32_t previous_file = 0;
1312
uint32_t previous_line = 0;
1313
bool found_entry = false;
1314
bool candidate = false;
1315
bool first_in_sequence = true;
1316
while (_reader.has_bytes_left()) {
1317
if (!apply_opcode()) {
1318
assert(false, "Could not apply opcode");
1322
if (_state->_append_row) {
1323
// Append a new line to the line number information matrix.
1324
if (_state->_first_entry_in_sequence) {
1325
// First entry in sequence: Check if _offset_in_library >= _state->address. If not, then all following entries
1326
// belonging to this sequence cannot match our _offset_in_library because the addresses are always increasing
1328
_state->_can_sequence_match_offset = _offset_in_library >= _state->_address;
1329
_state->_first_entry_in_sequence = false;
1331
if (does_offset_match_entry(previous_address, previous_file, previous_line)) {
1332
// We are using an int for the line number which should never be larger than INT_MAX for any files.
1333
*line = (int)_state->_line;
1334
return get_filename_from_header(_state->_file, filename, filename_len);
1337
// We do not actually store the matrix while searching the correct entry. Enable logging to print/debug it.
1338
DWARF_LOG_DEBUG(INTPTR_FORMAT " %-5u %-3u %-4u",
1339
_state->_address, _state->_line, _state->_column, _state->_file);
1340
previous_file = _state->_file;
1341
previous_line = _state->_line;
1342
previous_address = _state->_address;
1343
_state->_append_row = false;
1344
if (_state->_do_reset) {
1345
// Current sequence terminated.
1346
_state->reset_fields();
1351
assert(false, "Did not find an entry in the line number information matrix that matches " UINT32_FORMAT_X_0, _offset_in_library);
1355
// Apply next opcode to update the state machine.
1356
bool DwarfFile::LineNumberProgram::apply_opcode() {
1358
if (!_reader.read_byte(&opcode)) {
1362
DWARF_LOG_TRACE(" Opcode: 0x%02x ", opcode);
1364
// Extended opcodes start with a zero byte.
1365
if (!apply_extended_opcode()) {
1366
assert(false, "Could not apply extended opcode");
1369
} else if (opcode <= 12) {
1370
// 12 standard opcodes in DWARF 3 and 4.
1371
if (!apply_standard_opcode(opcode)) {
1372
assert(false, "Could not apply standard opcode");
1376
// Special opcodes range from 13 until 255.
1377
apply_special_opcode(opcode);
1382
// Specified in section 6.2.5.3 of the DWARF 4 spec.
1383
bool DwarfFile::LineNumberProgram::apply_extended_opcode() {
1384
uint64_t extended_opcode_length; // Does not include the already written zero byte and the length leb128.
1385
uint8_t extended_opcode;
1386
if (!_reader.read_uleb128(&extended_opcode_length) || !_reader.read_byte(&extended_opcode)) {
1390
switch (extended_opcode) {
1391
case DW_LNE_end_sequence: // No operands
1392
DWARF_LOG_TRACE(" DW_LNE_end_sequence");
1393
_state->_end_sequence = true;
1394
_state->_append_row = true;
1395
_state->_do_reset = true;
1397
case DW_LNE_set_address: // 1 operand
1398
if (!_reader.read_address_sized(&_state->_address)) {
1401
DWARF_LOG_TRACE(" DW_LNE_set_address " INTPTR_FORMAT, _state->_address);
1402
if (_state->_dwarf_version == 4) {
1403
_state->_op_index = 0;
1406
case DW_LNE_define_file: // 4 operands
1407
DWARF_LOG_TRACE(" DW_LNE_define_file");
1408
if (!_reader.read_string()) {
1411
// Operand 2-4: uleb128 numbers we do not care about.
1412
if (!_reader.read_uleb128_ignore()
1413
|| !_reader.read_uleb128_ignore()
1414
|| !_reader.read_uleb128_ignore()) {
1418
case DW_LNE_set_discriminator: // 1 operand
1419
DWARF_LOG_TRACE(" DW_LNE_set_discriminator");
1420
uint64_t discriminator;
1421
// For some reason, GCC emits this opcode even for earlier versions than DWARF 4 which introduced this opcode.
1422
// We need to consume it.
1423
if (!_reader.read_uleb128(&discriminator, 4)) {
1424
// Must be an unsigned integer as specified in section 6.2.2 of the DWARF 4 spec for the discriminator register.
1427
_state->_discriminator = static_cast<uint32_t>(discriminator);
1430
assert(false, "Unknown extended opcode");
1436
// Specified in section 6.2.5.2 of the DWARF 4 spec.
1437
bool DwarfFile::LineNumberProgram::apply_standard_opcode(const uint8_t opcode) {
1439
case DW_LNS_copy: // No operands
1440
DWARF_LOG_TRACE(" DW_LNS_copy");
1441
_state->_append_row = true;
1442
_state->_basic_block = false;
1443
_state->_prologue_end = false;
1444
_state->_epilogue_begin = false;
1445
if (_state->_dwarf_version == 4) {
1446
_state->_discriminator = 0;
1449
case DW_LNS_advance_pc: { // 1 operand
1451
if (!_reader.read_uleb128(&adv, 4)) {
1452
// Must be at most 4 bytes because the index register is only 4 bytes wide.
1455
uint32_t operation_advance = checked_cast<uint32_t>(adv);
1456
_state->add_to_address_register(operation_advance, _header);
1457
if (_state->_dwarf_version == 4) {
1458
_state->set_index_register(operation_advance, _header);
1460
DWARF_LOG_TRACE(" DW_LNS_advance_pc (" INTPTR_FORMAT ")", _state->_address);
1463
case DW_LNS_advance_line: // 1 operand
1465
if (!_reader.read_sleb128(&line, 4)) {
1466
// line register is 4 bytes wide.
1469
_state->_line += static_cast<uint32_t>(line);
1470
DWARF_LOG_TRACE(" DW_LNS_advance_line (%d)", _state->_line);
1472
case DW_LNS_set_file: // 1 operand
1474
if (!_reader.read_uleb128(&file, 4)) {
1475
// file register is 4 bytes wide.
1478
_state->_file = static_cast<uint32_t>(file);
1479
DWARF_LOG_TRACE(" DW_LNS_set_file (%u)", _state->_file);
1481
case DW_LNS_set_column: // 1 operand
1483
if (!_reader.read_uleb128(&column, 4)) {
1484
// column register is 4 bytes wide.
1487
_state->_column = static_cast<uint32_t>(column);
1488
DWARF_LOG_TRACE(" DW_LNS_set_column (%u)", _state->_column);
1490
case DW_LNS_negate_stmt: // No operands
1491
DWARF_LOG_TRACE(" DW_LNS_negate_stmt");
1492
_state->_is_stmt = !_state->_is_stmt;
1494
case DW_LNS_set_basic_block: // No operands
1495
DWARF_LOG_TRACE(" DW_LNS_set_basic_block");
1496
_state->_basic_block = true;
1498
case DW_LNS_const_add_pc: { // No operands
1499
// Update address and op_index registers by the increments of special opcode 255.
1500
uint8_t adjusted_opcode_255 = 255 - _header._opcode_base;
1501
uint8_t operation_advance = adjusted_opcode_255 / _header._line_range;
1502
uintptr_t old_address = _state->_address;
1503
_state->add_to_address_register(operation_advance, _header);
1504
if (_state->_dwarf_version == 4) {
1505
_state->set_index_register(operation_advance, _header);
1507
DWARF_LOG_TRACE(" DW_LNS_const_add_pc (" INTPTR_FORMAT ")", _state->_address - old_address);
1510
case DW_LNS_fixed_advance_pc: // 1 operand
1512
if (!_reader.read_word(&operand)) {
1515
_state->_address += operand;
1516
_state->_op_index = 0;
1517
DWARF_LOG_TRACE(" DW_LNS_fixed_advance_pc (" INTPTR_FORMAT ")", _state->_address);
1519
case DW_LNS_set_prologue_end: // No operands
1520
DWARF_LOG_TRACE(" DW_LNS_set_basic_block");
1521
_state->_prologue_end = true;
1523
case DW_LNS_set_epilogue_begin: // No operands
1524
DWARF_LOG_TRACE(" DW_LNS_set_epilogue_begin");
1525
_state->_epilogue_begin = true;
1527
case DW_LNS_set_isa: // 1 operand
1529
if (!_reader.read_uleb128(&isa, 4)) {
1530
// isa register is 4 bytes wide.
1533
_state->_isa = static_cast<uint32_t>(isa); // only save 4 bytes
1534
DWARF_LOG_TRACE(" DW_LNS_set_isa (%u)", _state->_isa);
1537
assert(false, "Unknown standard opcode");
1543
// Specified in section 6.2.5.1 of the DWARF 4 spec.
1544
void DwarfFile::LineNumberProgram::apply_special_opcode(const uint8_t opcode) {
1545
uintptr_t old_address = _state->_address;
1546
uint32_t old_line = _state->_line;
1547
uint8_t adjusted_opcode = opcode - _header._opcode_base;
1548
uint8_t operation_advance = adjusted_opcode / _header._line_range;
1549
_state->add_to_address_register(operation_advance, _header);
1550
if (_state->_dwarf_version == 4) {
1551
_state->set_index_register(operation_advance, _header);
1552
_state->_discriminator = 0;
1554
_state->_line += _header._line_base + (adjusted_opcode % _header._line_range);
1555
DWARF_LOG_TRACE(" address += " INTPTR_FORMAT ", line += %d", _state->_address - old_address,
1556
_state->_line - old_line);
1557
_state->_append_row = true;
1558
_state->_basic_block = false;
1559
_state->_prologue_end = false;
1560
_state->_epilogue_begin = false;
1563
bool DwarfFile::LineNumberProgram::does_offset_match_entry(const uintptr_t previous_address, const uint32_t previous_file,
1564
const uint32_t previous_line) {
1565
if (_state->_can_sequence_match_offset) {
1566
bool matches_entry_directly = _offset_in_library == _state->_address;
1567
if (matches_entry_directly
1568
|| (_offset_in_library > previous_address && _offset_in_library < _state->_address)) { // in between two entries
1569
_state->_found_match = true;
1570
if (!matches_entry_directly || _is_pc_after_call) {
1571
// We take the previous row in the matrix either when:
1572
// - We try to match an offset that is between two entries.
1573
// - We have an offset from a PC that is at a call-site in which case we need to get the line information for
1574
// the call instruction in the previous entry.
1575
print_and_store_prev_entry(previous_file, previous_line);
1577
} else if (!_reader.has_bytes_left()) {
1578
// We take the current entry when this is the very last entry in the matrix (i.e. must be the right one).
1579
DWARF_LOG_DEBUG("^^^ Found line for requested offset " UINT32_FORMAT_X_0 " ^^^", _offset_in_library);
1582
// Else: Exact match. We cannot take this entry because we do not know if there are more entries following this
1583
// one with the same offset (we could have multiple entries for the same address in the matrix). Continue
1584
// to parse entries. When we have the first non-exact match, then we know that the previous entry is the
1585
// correct one to take (handled in the else-if-case below). If this is the very last entry in a matrix,
1586
// we will take the current entry (handled in else-if-case above).
1587
} else if (_state->_found_match) {
1588
// We found an entry before with an exact match. This is now the first entry with a new offset. Pick the previous
1589
// entry which matches our offset and is guaranteed to be the last entry which matches our offset (if there are
1590
// multiple entries with the same offset).
1591
print_and_store_prev_entry(previous_file, previous_line);
1598
void DwarfFile::LineNumberProgram::print_and_store_prev_entry(const uint32_t previous_file, const uint32_t previous_line) {
1599
_state->_file = previous_file;
1600
_state->_line = previous_line;
1601
DWARF_LOG_DEBUG("^^^ Found line for requested offset " UINT32_FORMAT_X_0 " ^^^", _offset_in_library);
1602
// Also print the currently parsed entry.
1603
DWARF_LOG_DEBUG(INTPTR_FORMAT " %-5u %-3u %-4u",
1604
_state->_address, _state->_line, _state->_column, _state->_file);
1607
// Read field file_names from the header as specified in section 6.2.4 of the DWARF 4 spec.
1608
bool DwarfFile::LineNumberProgram::get_filename_from_header(const uint32_t file_index, char* filename, const size_t filename_len) {
1609
// We do not need to restore the position afterwards as this is the last step of parsing from the file for this compilation unit.
1610
_reader.set_position(_header._file_names_offset);
1611
uint32_t current_index = 1; // file_names start at index 1
1612
while (_reader.has_bytes_left()) {
1613
if (current_index == file_index) {
1614
// Found correct file.
1615
return read_filename(filename, filename_len);
1616
} else if (!_reader.read_string()) { // We don't care about this filename string. Read and ignore it.
1617
// Either an error while reading or we have reached the end of the file_names section before reaching the file_index.
1618
// Both should not happen.
1622
// We don't care about these values.
1623
if (!_reader.read_uleb128_ignore() // Read directory index
1624
|| !_reader.read_uleb128_ignore() // Read last modification of file
1625
|| !_reader.read_uleb128_ignore()) { // Read file length
1630
DWARF_LOG_DEBUG("Did not find filename entry at index " UINT32_FORMAT " in .debug_line header", file_index);
1634
// Read the filename into the provided 'filename' buffer. If it does not fit, an alternative smaller tag will be emitted
1635
// in order to let the DWARF parser succeed. The line number with a function name will almost always be sufficient to get
1636
// to the actual source code location.
1637
bool DwarfFile::LineNumberProgram::read_filename(char* filename, const size_t filename_len) {
1639
if (!_reader.read_non_null_char(&next_char)) {
1640
// Either error while reading or read an empty string which indicates the end of the file_names section.
1641
// Both should not happen.
1645
filename[0] = next_char;
1647
bool overflow_filename = false; // Is the currently read filename overflowing the provided 'filename' buffer?
1648
while (next_char != '\0' && _reader.has_bytes_left()) {
1649
if (!_reader.read_byte(&next_char)) {
1652
if (next_char == *os::file_separator()) {
1653
// Skip file separator to get to the actual filename and reset the buffer and overflow flag. GCC does not emit
1654
// file separators while Clang does.
1656
overflow_filename = false;
1657
} else if (index == filename_len) {
1658
// Just keep reading as we could read another file separator and reset the buffer again. But don't bother to store
1659
// the additionally read characters as it would not fit into the buffer anyway.
1660
overflow_filename = true;
1662
assert(!overflow_filename, "sanity check");
1663
filename[index] = next_char;
1668
if (overflow_filename) {
1669
// 'filename' buffer overflow. Store either a generic overflow message or a minimal filename.
1670
write_filename_for_overflow(filename, filename_len);
1675
// Try to write a generic overflow message to the provided buffer. If it does not fit, store the minimal filename "L"
1676
// which always fits to get the source information in the form "L:line_number".
1677
void DwarfFile::LineNumberProgram::write_filename_for_overflow(char* filename, const size_t filename_len) {
1678
DWARF_LOG_ERROR("DWARF filename string is too large to fit into the provided buffer of size %zu.", filename_len);
1679
const size_t filename_overflow_message_length = strlen(overflow_filename) + 1;
1680
if (filename_overflow_message_length <= filename_len) {
1681
jio_snprintf(filename, filename_overflow_message_length, "%s", overflow_filename);
1682
DWARF_LOG_ERROR("Use overflow filename: %s", overflow_filename);
1684
// Buffer too small of generic overflow message.
1685
DWARF_LOG_ERROR("Too small for overflow filename, use minimal filename: %c", minimal_overflow_filename);
1686
assert(filename_len > 1, "sanity check");
1687
filename[0] = minimal_overflow_filename;
1692
void DwarfFile::LineNumberProgram::LineNumberProgramState::reset_fields() {
1698
_is_stmt = _initial_is_stmt;
1699
_basic_block = false;
1700
_end_sequence = false;
1701
_prologue_end = false;
1702
_epilogue_begin = false;
1705
_append_row = false;
1707
_first_entry_in_sequence = true;
1708
_can_sequence_match_offset = false;
1711
// Defined in section 6.2.5.1 of the DWARF 4 spec.
1712
void DwarfFile::LineNumberProgram::LineNumberProgramState::add_to_address_register(const uint32_t operation_advance,
1713
const LineNumberProgramHeader& header) {
1714
if (_dwarf_version == 2 || _dwarf_version == 3) {
1715
_address += (uintptr_t)(operation_advance * header._minimum_instruction_length);
1716
} else if (_dwarf_version == 4) {
1717
_address += (uintptr_t)(header._minimum_instruction_length *
1718
((_op_index + operation_advance) / header._maximum_operations_per_instruction));
1722
// Defined in section 6.2.5.1 of the DWARF 4 spec.
1723
void DwarfFile::LineNumberProgram::LineNumberProgramState::set_index_register(const uint32_t operation_advance,
1724
const LineNumberProgramHeader& header) {
1725
_op_index = (_op_index + operation_advance) % header._maximum_operations_per_instruction;
1728
bool DwarfFile::MarkedDwarfFileReader::set_position(const long new_pos) {
1732
_current_pos = new_pos;
1733
return FileReader::set_position(new_pos);
1736
bool DwarfFile::MarkedDwarfFileReader::has_bytes_left() const {
1737
if (_max_pos == -1) {
1740
return _current_pos < _max_pos;
1743
bool DwarfFile::MarkedDwarfFileReader::update_to_stored_position() {
1744
_marked_pos = ftell(_fd);
1745
if (_marked_pos < 0) {
1748
return FileReader::set_position(_current_pos);
1751
bool DwarfFile::MarkedDwarfFileReader::reset_to_previous_position() {
1752
return FileReader::set_position(_marked_pos);
1755
bool DwarfFile::MarkedDwarfFileReader::move_position(const long offset) {
1759
return set_position(_current_pos + offset);
1762
bool DwarfFile::MarkedDwarfFileReader::read_byte(void* result) {
1764
return read(result, 1);
1767
bool DwarfFile::MarkedDwarfFileReader::read_word(uint16_t* result) {
1769
return read(result, 2);
1772
bool DwarfFile::MarkedDwarfFileReader::read_dword(uint32_t* result) {
1774
return read(result, 4);
1777
bool DwarfFile::MarkedDwarfFileReader::read_qword(uint64_t* result) {
1779
return read(result, 8);
1782
bool DwarfFile::MarkedDwarfFileReader::read_address_sized(uintptr_t* result) {
1783
_current_pos += DwarfFile::ADDRESS_SIZE;
1784
return read(result, DwarfFile::ADDRESS_SIZE);
1787
// See Figure 46/47 in Appendix C of the DWARF 4 spec.
1788
bool DwarfFile::MarkedDwarfFileReader::read_leb128(uint64_t* result, const int8_t check_size, bool is_signed) {
1789
*result = 0; // Ensure a proper result by zeroing it first.
1792
uint8_t bytes_read = 0;
1793
// leb128 is not larger than 8 bytes.
1794
while (bytes_read < 8) {
1795
if (!read_byte(&buf)) {
1799
*result |= (buf & 0x7fu) << shift;
1801
if ((buf & 0x80u) == 0) {
1805
if (bytes_read > 8 || (check_size != -1 && bytes_read > check_size)) {
1806
// Invalid leb128 encoding or the read leb128 was larger than expected.
1810
if (is_signed && (shift < 64) && (buf & 0x40u)) {
1811
*result |= static_cast<uint64_t>(-1L) << shift;
1816
bool DwarfFile::MarkedDwarfFileReader::read_uleb128_ignore(const int8_t check_size) {
1818
return read_leb128(&dont_care, check_size, false);
1821
bool DwarfFile::MarkedDwarfFileReader::read_uleb128(uint64_t* result, const int8_t check_size) {
1822
return read_leb128(result, check_size, false);
1825
bool DwarfFile::MarkedDwarfFileReader::read_sleb128(int64_t* result, const int8_t check_size) {
1826
return read_leb128((uint64_t*)result, check_size, true);
1829
// If result is a null, we do not care about the content of the string being read.
1830
bool DwarfFile::MarkedDwarfFileReader::read_string(char* result, const size_t result_len) {
1832
if (!read_non_null_char(&first_char)) {
1836
if (result != nullptr) {
1837
if (result_len < 2) {
1838
// Strings must contain at least one non-null byte and a null byte terminator.
1841
result[0] = first_char;
1845
size_t char_index = 1;
1846
bool exceeded_buffer = false;
1847
while (has_bytes_left()) {
1848
// Read until we find a null byte which terminates the string.
1849
if (!read_byte(&next_byte)) {
1853
if (result != nullptr) {
1854
if (char_index >= result_len) {
1855
// Exceeded buffer size of 'result'.
1856
exceeded_buffer = true;
1858
result[char_index] = (char)next_byte;
1862
if (next_byte == 0) {
1863
if (exceeded_buffer) {
1864
result[result_len - 1] = '\0'; // Mark end of string.
1865
DWARF_LOG_ERROR("Tried to read " SIZE_FORMAT " bytes but exceeded buffer size of " SIZE_FORMAT ". Truncating string.",
1866
char_index, result_len);
1874
bool DwarfFile::MarkedDwarfFileReader::read_non_null_char(char* result) {
1875
if (!read_byte(result)) {
1878
return *result != '\0';
1881
#endif // !_WINDOWS && !__APPLE__