/* * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
if (ElfFile::_do_not_cache_elf_section) {
log_develop_debug(decoder)("Elf section cache is disabled"); return NullDecoder::no_error;
}
_section_data = os::malloc(shdr.sh_size, mtInternal); // No enough memory for caching. It is okay, we can try to read from // file instead. if (_section_data == NULL) return NullDecoder::no_error;
// Check signature if (!is_elf_file(_elfHdr)) { return NullDecoder::file_invalid;
}
// walk elf file's section headers, and load string tables
Elf_Shdr shdr; if (!freader.set_position(_elfHdr.e_shoff)) { return NullDecoder::file_invalid;
}
for (int index = 0; index < _elfHdr.e_shnum; index ++) { if (!freader.read(&shdr, sizeof(shdr))) { return NullDecoder::file_invalid;
}
if (shdr.sh_type == SHT_STRTAB) { // string tables
ElfStringTable* table = new (std::nothrow) ElfStringTable(fd(), shdr, index); if (table == NULL) { return NullDecoder::out_of_memory;
} if (index == _elfHdr.e_shstrndx) {
assert(_shdr_string_table == NULL, "Only set once");
_shdr_string_table = table;
} else {
add_string_table(table);
}
} elseif (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) { // symbol tables
ElfSymbolTable* table = new (std::nothrow) ElfSymbolTable(fd(), shdr); if (table == NULL) { return NullDecoder::out_of_memory;
}
add_symbol_table(table);
}
} #ifdefined(PPC64) && !defined(ABI_ELFv2) // Now read the .opd section which contains the PPC64 function descriptor table. // The .opd section is only available on PPC64 (see for example: // http://refspecs.linuxfoundation.org/LSB_3.1.1/LSB-Core-PPC64/LSB-Core-PPC64/specialsections.html) // so this code should do no harm on other platforms but because of performance reasons we only // execute it on PPC64 platforms. // Notice that we can only find the .opd section after we have successfully read in the string // tables in the previous loop, because we need to query the name of each section which is // contained in one of the string tables (i.e. the one with the index m_elfHdr.e_shstrndx).
// Reset the file pointer int sect_index = section_by_name(".opd", shdr);
if (sect_index == -1) { return NullDecoder::file_invalid;
}
_funcDesc_table = new (std::nothrow) ElfFuncDescTable(_file, shdr, sect_index); if (_funcDesc_table == NULL) { return NullDecoder::out_of_memory;
} #endif return NullDecoder::no_error;
}
#ifdefined(PPC64) && !defined(ABI_ELFv2) int ElfFile::section_by_name(constchar* name, Elf_Shdr& hdr) {
assert(name != NULL, "No section name");
size_t len = strlen(name) + 1; char* buf = (char*)os::malloc(len, mtInternal); if (buf == NULL) { return -1;
}
assert(_shdr_string_table != NULL, "Section header string table should be loaded");
ElfStringTable* const table = _shdr_string_table;
MarkedFileReader mfd(fd()); if (!mfd.has_mark() || !mfd.set_position(_elfHdr.e_shoff)) return -1;
int sect_index = -1; for (int index = 0; index < _elfHdr.e_shnum; index ++) { if (!mfd.read((void*)&hdr, sizeof(hdr))) { break;
} if (table->string_at(hdr.sh_name, buf, len)) { if (strncmp(buf, name, len) == 0) {
sect_index = index; break;
}
}
}
os::free(buf);
return sect_index;
} #endif
bool ElfFile::decode(address addr, char* buf, int buflen, int* offset) { // something already went wrong, just give up if (NullDecoder::is_error(_status)) { returnfalse;
}
int string_table_index; int pos_in_string_table; int off = INT_MAX; bool found_symbol = false;
ElfSymbolTable* symbol_table = _symbol_tables;
while (symbol_table != NULL) { if (symbol_table->lookup(addr, &string_table_index, &pos_in_string_table, &off, _funcDesc_table)) {
found_symbol = true; break;
}
symbol_table = symbol_table->next();
} if (!found_symbol) { returnfalse;
}
ElfStringTable* ElfFile::get_string_table(int index) {
ElfStringTable* p = _string_tables; while (p != NULL) { if (p->index() == index) return p;
p = p->next();
} return NULL;
}
// Use unified logging to report errors rather than assert() throughout this method as this code is already part of the error reporting // and the debug symbols might be in an unsupported DWARF version or wrong format. 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) { if (!load_dwarf_file()) { // Some ELF libraries do not provide separate .debuginfo files. Check if the current ELF file has the required // DWARF sections. If so, treat the current ELF file as DWARF file. if (!is_valid_dwarf_file()) {
DWARF_LOG_ERROR("Failed to load DWARF file for library %s or find DWARF sections directly inside it.", _filepath); returnfalse;
}
DWARF_LOG_INFO("No separate .debuginfo file for library %s. It already contains the required DWARF sections.",
_filepath); if (!create_new_dwarf_file(_filepath)) { returnfalse;
}
}
// Store result in filename and line pointer. if (!_dwarf_file->get_filename_and_line_number(offset_in_library, filename, filename_len, line, is_pc_after_call)) {
DWARF_LOG_ERROR("Failed to retrieve file and line number information for %s at offset: " UINT32_FORMAT_X_0, _filepath,
offset_in_library); returnfalse;
} returntrue;
}
// (1) Load the debuginfo file from the path specified in this ELF file in the .gnu_debuglink section. // Adapted from Serviceability Agent. bool ElfFile::load_dwarf_file() { if (_dwarf_file != nullptr) { returntrue; // Already opened.
}
DebugInfo debug_info; if (!read_debug_info(&debug_info)) {
DWARF_LOG_DEBUG("Could not read debug info from .gnu_debuglink section"); returnfalse;
}
if (debug_info->_dwarf_filename[filename_max_len - 1] != '\0') { // Filename not null-terminated (i.e. overflowed).
DWARF_LOG_ERROR("Dwarf filename is not null-terminated"); returnfalse;
}
bool ElfFile::DwarfFilePath::copy_to_path_index(uint16_t index_in_path, constchar* src) { if (index_in_path >= MAX_DWARF_PATH_LENGTH - 1) { // Should not override '\0' at _path[MAX_DWARF_PATH_LENGTH - 1]
DWARF_LOG_ERROR("Dwarf file path buffer is too small"); returnfalse;
}
uint16_t max_len = MAX_DWARF_PATH_LENGTH - index_in_path; int bytes_written = jio_snprintf(_path + index_in_path, max_len, "%s", src); if (bytes_written < 0 || bytes_written >= max_len) {
DWARF_LOG_ERROR("Dwarf file path buffer is too small"); returnfalse;
}
update_null_terminator_index(); return check_valid_path(); // Sanity check
}
// Try to load the dwarf file from the same directory as the library file. bool ElfFile::load_dwarf_file_from_same_directory(DwarfFilePath& dwarf_file_path) { if (!dwarf_file_path.set(_filepath)
|| !dwarf_file_path.set_filename_after_last_slash()) { returnfalse;
} return open_valid_debuginfo_file(dwarf_file_path);
}
// Try to load the dwarf file from a user specified path in environmental variable _JVM_DWARF_PATH. bool ElfFile::load_dwarf_file_from_env_var_path(DwarfFilePath& dwarf_file_path) { constchar* dwarf_path_from_env = ::getenv("_JVM_DWARF_PATH"); if (dwarf_path_from_env != nullptr) {
DWARF_LOG_DEBUG("_JVM_DWARF_PATH: %s", dwarf_path_from_env); return (load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/lib/server/")
|| load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/lib/")
|| load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/bin/")
|| load_dwarf_file_from_env_path_folder(dwarf_file_path, dwarf_path_from_env, "/"));
} returnfalse;
}
bool ElfFile::load_dwarf_file_from_env_path_folder(DwarfFilePath& dwarf_file_path, constchar* dwarf_path_from_env, constchar* folder) { if (!dwarf_file_path.set(dwarf_path_from_env)
|| !dwarf_file_path.append(folder)
|| !dwarf_file_path.append(dwarf_file_path.filename())) {
DWARF_LOG_ERROR("Dwarf file path buffer is too small"); returnfalse;
} return open_valid_debuginfo_file(dwarf_file_path);
}
// Try to load the dwarf file from a subdirectory named .debug within the directory of the library file. bool ElfFile::load_dwarf_file_from_debug_sub_directory(DwarfFilePath& dwarf_file_path) { if (!dwarf_file_path.set(_filepath)
|| !dwarf_file_path.set_after_last_slash(".debug/")
|| !dwarf_file_path.append(dwarf_file_path.filename())) {
DWARF_LOG_ERROR("Dwarf file path buffer is too small"); returnfalse;
} return open_valid_debuginfo_file(dwarf_file_path);
}
// Try to load the dwarf file from /usr/lib/debug + the full pathname. bool ElfFile::load_dwarf_file_from_usr_lib_debug(DwarfFilePath& dwarf_file_path) { if (!dwarf_file_path.set(USR_LIB_DEBUG_DIRECTORY)
|| !dwarf_file_path.append(_filepath)
|| !dwarf_file_path.set_filename_after_last_slash()) {
DWARF_LOG_ERROR("Dwarf file path buffer is too small"); returnfalse;
} return open_valid_debuginfo_file(dwarf_file_path);
}
bool ElfFile::read_section_header(constchar* name, Elf_Shdr& hdr) const { if (_shdr_string_table == nullptr) {
assert(false, "section header string table should be loaded"); returnfalse;
} const uint8_t buf_len = 24; char buf[buf_len];
size_t len = strlen(name) + 1; if (len > buf_len) {
DWARF_LOG_ERROR("Section header name buffer is too small: Required: %zu, Found: %d", len, buf_len); returnfalse;
}
MarkedFileReader mfd(fd()); if (!mfd.has_mark() || !mfd.set_position(_elfHdr.e_shoff)) { returnfalse;
}
for (int index = 0; index < _elfHdr.e_shnum; index++) { if (!mfd.read((void*)&hdr, sizeof(hdr))) { returnfalse;
} if (_shdr_string_table->string_at(hdr.sh_name, buf, buf_len)) { if (strncmp(buf, name, buf_len) == 0) { returntrue;
}
}
} returnfalse;
}
constchar* filepath = dwarf_file_path.path();
FILE* file = fopen(filepath, "r"); if (file == nullptr) {
DWARF_LOG_DEBUG("Could not open dwarf file %s (%s)", filepath, os::strerror(errno)); returnfalse;
}
uint32_t file_crc = get_file_crc(file);
fclose(file); // Close it here to reopen it again when the DwarfFile object is created below.
if (dwarf_file_path.crc() != file_crc) { // Must be equal, otherwise the file is corrupted.
DWARF_LOG_ERROR("CRC did not match. Expected: " INT32_FORMAT_X_0 ", found: " INT32_FORMAT_X_0, dwarf_file_path.crc(),
file_crc); returnfalse;
} return create_new_dwarf_file(filepath);
}
bool ElfFile::create_new_dwarf_file(constchar* filepath) {
DWARF_LOG_SUMMARY("Open DWARF file: %s", filepath);
_dwarf_file = new (std::nothrow) DwarfFile(filepath); if (_dwarf_file == nullptr) {
DWARF_LOG_ERROR("Failed to create new DwarfFile object for %s.", _filepath); returnfalse;
} if (!_dwarf_file->is_valid_dwarf_file()) {
DWARF_LOG_ERROR("Did not find required DWARF sections in %s", filepath); returnfalse;
} returntrue;
}
// Starting point of reading line number and filename information from the DWARF file. bool DwarfFile::get_filename_and_line_number(const uint32_t offset_in_library, char* filename, const size_t filename_len, int* line, constbool is_pc_after_call) {
DebugAranges debug_aranges(this);
uint32_t compilation_unit_offset = 0; // 4-bytes for 32-bit DWARF if (!debug_aranges.find_compilation_unit_offset(offset_in_library, &compilation_unit_offset)) {
DWARF_LOG_ERROR("Failed to find .debug_info offset for the compilation unit."); returnfalse;
}
DWARF_LOG_INFO(".debug_info offset: " INT32_FORMAT_X_0, compilation_unit_offset);
CompilationUnit compilation_unit(this, compilation_unit_offset);
uint32_t debug_line_offset = 0; // 4-bytes for 32-bit DWARF if (!compilation_unit.find_debug_line_offset(&debug_line_offset)) {
DWARF_LOG_ERROR("Failed to find .debug_line offset for the line number program."); returnfalse;
}
DWARF_LOG_INFO(".debug_line offset: " INT32_FORMAT_X_0, debug_line_offset);
LineNumberProgram line_number_program(this, offset_in_library, debug_line_offset, is_pc_after_call); if (!line_number_program.find_filename_and_line_number(filename, filename_len, line)) {
DWARF_LOG_ERROR("Failed to process the line number program correctly."); returnfalse;
} returntrue;
}
// (2) The .debug_aranges section contains a number of entries/sets. Each set contains one or multiple address range descriptors of the // form [beginning_address, beginning_address+length). Start reading these sets and their descriptors until we find one that contains // 'offset_in_library'. Read the debug_info_offset field from the header of this set which defines the offset for the compilation unit. // This process is described in section 6.1.2 of the DWARF 4 spec. bool DwarfFile::DebugAranges::find_compilation_unit_offset(const uint32_t offset_in_library, uint32_t* compilation_unit_offset) { if (!read_section_header()) {
DWARF_LOG_ERROR("Failed to read a .debug_aranges header."); returnfalse;
}
DebugArangesSetHeader set_header; bool found_matching_set = false; while (_reader.has_bytes_left()) { // Read multiple sets and therefore multiple headers. if (!read_set_header(set_header)) {
DWARF_LOG_ERROR("Failed to read a .debug_aranges header."); returnfalse;
}
if (!read_address_descriptors(set_header, offset_in_library, found_matching_set)) { returnfalse;
}
if (found_matching_set) { // Found the correct set, read the debug_info_offset from the header of this set.
DWARF_LOG_INFO(".debug_aranges offset: " UINT32_FORMAT, (uint32_t)_reader.get_position());
*compilation_unit_offset = set_header._debug_info_offset; returntrue;
}
}
assert(false, "No address descriptor found containing offset_in_library."); returnfalse;
}
// Parse set header as specified in section 6.1.2 of the DWARF 4 spec. bool DwarfFile::DebugAranges::read_set_header(DebugArangesSetHeader& header) { if (!_reader.read_dword(&header._unit_length) || header._unit_length == 0xFFFFFFFF) { // For 64-bit DWARF, the first 32-bit value is 0xFFFFFFFF. The current implementation only supports 32-bit DWARF // format since GCC only emits 32-bit DWARF.
DWARF_LOG_ERROR("64-bit DWARF is not supported for .debug_aranges") returnfalse;
}
if (!_reader.read_word(&header._version) || header._version != 2) { // DWARF 4 uses version 2 as specified in Appendix F of the DWARF 4 spec.
DWARF_LOG_ERROR(".debug_aranges in unsupported DWARF version %" PRIu16, header._version) returnfalse;
}
if (!_reader.read_dword(&header._debug_info_offset)) { returnfalse;
}
if (!_reader.read_byte(&header._address_size) || header._address_size != DwarfFile::ADDRESS_SIZE) { // Addresses must be either 4 bytes for 32-bit architectures or 8 bytes for 64-bit architectures.
DWARF_LOG_ERROR(".debug_aranges specifies wrong address size %" PRIu8, header._address_size); returnfalse;
}
if (!_reader.read_byte(&header._segment_size) || header._segment_size != 0) { // Segment size should be 0.
DWARF_LOG_ERROR(".debug_aranges segment size is non-zero: %" PRIu8, header._segment_size); returnfalse;
}
// We must align to twice the address size.
uint8_t alignment = DwarfFile::ADDRESS_SIZE * 2;
uint8_t padding = alignment - (_reader.get_position() - _section_start_address) % alignment; return _reader.move_position(padding);
}
bool DwarfFile::DebugAranges::read_address_descriptors(const DwarfFile::DebugAranges::DebugArangesSetHeader& header, const uint32_t offset_in_library, bool& found_matching_set) {
AddressDescriptor descriptor; do { if (!read_address_descriptor(descriptor)) { returnfalse;
}
if (does_match_offset(offset_in_library, descriptor)) {
found_matching_set = true; returntrue;
}
} while (!is_terminating_entry(header, descriptor) && _reader.has_bytes_left());
// Set does not match offset_in_library. Continue with next. returntrue;
}
// Find the .debug_line offset for the line number program by reading from the .debug_abbrev and .debug_info section. bool DwarfFile::CompilationUnit::find_debug_line_offset(uint32_t* debug_line_offset) { // (3a,b) if (!read_header()) {
DWARF_LOG_ERROR("Failed to read the compilation unit header."); returnfalse;
}
// (3c) Read the abbreviation code immediately following the compilation unit header which is an offset to the // correct abbreviation table in .debug_abbrev for this compilation unit.
uint64_t abbrev_code; if (!_reader.read_uleb128(&abbrev_code)) { returnfalse;
}
DebugAbbrev debug_abbrev(_dwarf_file, this); if (!debug_abbrev.read_section_header(_header._debug_abbrev_offset)) {
DWARF_LOG_ERROR("Failed to read the .debug_abbrev header at " UINT32_FORMAT_X_0, _header._debug_abbrev_offset); returnfalse;
} if (!debug_abbrev.find_debug_line_offset(abbrev_code)) { returnfalse;
}
*debug_line_offset = _debug_line_offset; // Result was stored in _debug_line_offset. returntrue;
}
// (3a) Parse header as specified in section 7.5.1.1 of the DWARF 4 spec. bool DwarfFile::CompilationUnit::read_header() {
Elf_Shdr shdr; if (!_dwarf_file->read_section_header(".debug_info", shdr)) {
DWARF_LOG_ERROR("Failed to read the .debug_info section header."); returnfalse;
}
if (!_reader.set_position(shdr.sh_offset + _compilation_unit_offset)) { returnfalse;
}
if (!_reader.read_dword(&_header._unit_length) || _header._unit_length == 0xFFFFFFFF) { // For 64-bit DWARF, the first 32-bit value is 0xFFFFFFFF. The current implementation only supports 32-bit DWARF // format since GCC only emits 32-bit DWARF.
DWARF_LOG_ERROR("64-bit DWARF is not supported for .debug_info") returnfalse;
}
if (!_reader.read_word(&_header._version) || _header._version != 4) { // DWARF 4 uses version 4 as specified in Appendix F of the DWARF 4 spec.
DWARF_LOG_ERROR(".debug_info in unsupported DWARF version %" PRIu16, _header._version) returnfalse;
}
// (3b) Offset into .debug_abbrev section. if (!_reader.read_dword(&_header._debug_abbrev_offset)) { returnfalse;
}
if (!_reader.read_byte(&_header._address_size) || _header._address_size != DwarfFile::ADDRESS_SIZE) { // Addresses must be either 4 bytes for 32-bit architectures or 8 bytes for 64-bit architectures.
DWARF_LOG_ERROR(".debug_info specifies wrong address size %" PRIu8, _header._address_size); returnfalse;
}
// Add because _unit_length is not included.
_reader.set_max_pos(_reader.get_position() + _header._unit_length + 4); returntrue;
}
// (3d) The abbreviations table for a compilation unit consists of a series of abbreviation declarations. Each declaration // specifies an abbrev code and a tag. Parse all declarations until we find the declaration which matches 'abbrev_code'. // Read the attribute values from the compilation unit in .debug_info by using the format described in the declaration. // This process is described in section 7.5 and 7.5.3 of the DWARF 4 spec. bool DwarfFile::DebugAbbrev::find_debug_line_offset(const uint64_t abbrev_code) {
DWARF_LOG_TRACE("Series of declarations [code, tag]:");
AbbreviationDeclaration declaration; while (_reader.has_bytes_left()) { if (!read_declaration(declaration)) { returnfalse;
}
DWARF_LOG_TRACE(" Series of attributes [name, form]:"); if (declaration._abbrev_code == abbrev_code) { // Found the correct declaration. if (is_wrong_or_unsupported_format(declaration)) { returnfalse;
}
DWARF_LOG_INFO(".debug_abbrev offset: " UINT32_FORMAT_X_0, (uint32_t)_reader.get_position());
DWARF_LOG_TRACE(" Read the following attribute values from compilation unit:"); return read_attribute_specifications(true);
} else { // Not the correct declaration. Read its attributes and continue with the next declaration. if (!read_attribute_specifications(false)) { returnfalse;
}
}
}
assert(false, ".debug_line offset not found"); returnfalse;
}
bool DwarfFile::DebugAbbrev::read_declaration(DwarfFile::DebugAbbrev::AbbreviationDeclaration& declaration) { if (!_reader.read_uleb128(&declaration._abbrev_code)) { returnfalse;
}
if (declaration._abbrev_code == 0) { // Reached the end of the abbreviation declarations for this compilation unit.
DWARF_LOG_ERROR("abbrev_code not found in any declaration"); returnfalse;
}
if (!_reader.read_uleb128(&declaration._tag) || !_reader.read_byte(&declaration._has_children)) { returnfalse;
}
bool DwarfFile::DebugAbbrev::is_wrong_or_unsupported_format(const DwarfFile::DebugAbbrev::AbbreviationDeclaration& declaration) { if (declaration._tag != DW_TAG_compile_unit) { // Is not DW_TAG_compile_unit as specified in Figure 18 in section 7.5 of the DWARF 4 spec. It could also // be DW_TAG_partial_unit (0x3c) which is currently not supported by this parser.
DWARF_LOG_ERROR("Found unsupported tag in compilation unit: " UINT64_FORMAT_X, declaration._tag); returntrue;
} if (declaration._has_children != DW_CHILDREN_yes) {
DWARF_LOG_ERROR("Must have children but none specified"); returntrue;
} returnfalse;
}
// Read the attribute names and forms which define the actual attribute values that follow the abbrev code in the compilation unit. All // attributes need to be read from the compilation unit until we reach the DW_AT_stmt_list attribute which specifies the offset for the // line number program into the .debug_line section. The offset is stored in the _debug_line_offset field of the compilation unit. bool DwarfFile::DebugAbbrev::read_attribute_specifications(constbool is_DW_TAG_compile_unit) {
AttributeSpecification attribute_specification; while (_reader.has_bytes_left()) { if (!read_attribute_specification(attribute_specification)) { returnfalse;
}
if (is_terminating_specification(attribute_specification)) { // Parsed all attributes of this declaration. if (is_DW_TAG_compile_unit) {
DWARF_LOG_ERROR("Did not find DW_AT_stmt_list in .debug_abbrev"); returnfalse;
} else { // Continue with next declaration if this was not DW_TAG_compile_unit. returntrue;
}
}
if (is_DW_TAG_compile_unit) { // Read attribute from compilation unit if (attribute_specification._name == DW_AT_stmt_list) { // This attribute represents the .debug_line offset. Read it and then stop parsing. return _compilation_unit->read_attribute_value(attribute_specification._form, true);
} else { // Not DW_AT_stmt_list, read it and continue with the next attribute. if (!_compilation_unit->read_attribute_value(attribute_specification._form, false)) { returnfalse;
}
}
}
}
assert(false, ".debug_abbrev section appears to be corrupted"); returnfalse;
}
// (3e) Read the actual attribute values from the compilation unit in the .debug_info section. Each attribute has an encoding // that specifies which values need to be read for it. This is specified in section 7.5.4 of the DWARF 4 spec. // If is_DW_AT_stmt_list_attribute is: // - False: Ignore the read attribute value. // - True: We are going to read the attribute value of the DW_AT_stmt_list attribute which specifies the offset into the // .debug_line section for the line number program. Store this offset in the _debug_line_offset field. bool DwarfFile::CompilationUnit::read_attribute_value(const uint64_t attribute_form, constbool is_DW_AT_stmt_list_attribute) { // Reset to the stored _cur_pos of the reader since the DebugAbbrev reader changed the index into the file with its reader.
_reader.update_to_stored_position();
uint8_t next_byte = 0;
uint16_t next_word = 0;
uint32_t next_dword = 0;
uint64_t next_qword = 0;
switch (attribute_form) { case DW_FORM_addr: // Move position by the size of an address.
_reader.move_position(DwarfFile::ADDRESS_SIZE); break; case DW_FORM_block2: // New position: length + data length (next_word) if (!_reader.read_word(&next_word) || !_reader.move_position(next_word)) { returnfalse;
} break; case DW_FORM_block4: // New position: length + data length (next_dword) if (!_reader.read_dword(&next_dword) || !_reader.move_position(next_dword)) { returnfalse;
} break; case DW_FORM_data2: case DW_FORM_ref2: if (!_reader.move_position(2)) { returnfalse;
} break; case DW_FORM_data4: case DW_FORM_strp: // 4 bytes in 32-bit DWARF case DW_FORM_ref_addr: // second type of reference: 4 bytes in 32-bit DWARF case DW_FORM_ref4: if (!_reader.move_position(4)) { returnfalse;
} break; case DW_FORM_data8: case DW_FORM_ref8: case DW_FORM_ref_sig8: // 64-bit type signature if (!_reader.move_position(8)) { returnfalse;
} break; case DW_FORM_string: if (!_reader.read_string()) { returnfalse;
} break; case DW_FORM_block: case DW_FORM_exprloc: // New position: length + data length (next_qword). if (!_reader.read_uleb128(&next_qword) || !_reader.move_position(next_qword)) { returnfalse;
} break; case DW_FORM_block1: // New position: length + data length (next_byte). if (!_reader.read_byte(&next_byte) || !_reader.move_position(next_byte)) { returnfalse;
} break; case DW_FORM_data1: case DW_FORM_ref1: case DW_FORM_flag: case DW_FORM_flag_present: if (!_reader.move_position(1)) { returnfalse;
} break; case DW_FORM_sdata: case DW_FORM_udata: case DW_FORM_ref_udata: if (!_reader.read_uleb128(&next_qword)) { returnfalse;
} break; case DW_FORM_indirect: // Should not be used and therefore is not supported by this parser.
DWARF_LOG_ERROR("DW_FORM_indirect is not supported."); returnfalse; case DW_FORM_sec_offset: if (is_DW_AT_stmt_list_attribute) { // DW_AT_stmt_list has the DW_FORM_sec_offset attribute encoding. Store the result in _debug_line_offset. // 4 bytes for 32-bit DWARF.
DWARF_LOG_TRACE(" Name: DW_AT_stmt_list, Form: DW_FORM_sec_offset");
DWARF_LOG_TRACE(" Reading .debug_line offset from compilation unit at " UINT32_FORMAT_X_0,
(uint32_t)_reader.get_position()); if (!_reader.read_dword(&_debug_line_offset)) { returnfalse;
} break;
} else { if (!_reader.move_position(DwarfFile::DWARF_SECTION_OFFSET_SIZE)) { returnfalse;
} break;
} default:
assert(false, "Unknown DW_FORM_* attribute encoding."); returnfalse;
} // Reset the index into the file to the original position where the DebugAbbrev reader stopped reading before calling this method.
_reader.reset_to_previous_position(); returntrue;
}
bool DwarfFile::LineNumberProgram::find_filename_and_line_number(char* filename, const size_t filename_len, int* line) { if (!read_header()) {
DWARF_LOG_ERROR("Failed to parse the line number program header correctly."); returnfalse;
} return run_line_number_program(filename, filename_len, line);
}
// Parsing header as specified in section 6.2.4 of DWARF 4 spec. We do not read the file_names field, yet. bool DwarfFile::LineNumberProgram::read_header() {
Elf_Shdr shdr; if (!_dwarf_file->read_section_header(".debug_line", shdr)) {
DWARF_LOG_ERROR("Failed to read the .debug_line section header."); returnfalse;
}
if (!_reader.set_position(shdr.sh_offset + _debug_line_offset)) { returnfalse;
}
if (!_reader.read_dword(&_header._unit_length) || _header._unit_length == 0xFFFFFFFF) { // For 64-bit DWARF, the first 32-bit value is 0xFFFFFFFF. The current implementation only supports 32-bit DWARF // format since GCC only emits 32-bit DWARF.
DWARF_LOG_ERROR("64-bit DWARF is not supported for .debug_line") returnfalse;
}
if (!_reader.read_word(&_header._version) || _header._version < 2 || _header._version > 4) { // DWARF 3 uses version 3 and DWARF 4 uses version 4 as specified in Appendix F of the DWARF 3 and 4 spec, respectively. // For some reason, GCC is not following the standard here. While GCC emits DWARF 4 for the other parsed sections, // it chooses a different DWARF standard for .debug_line based on the GCC version: // - GCC 8 and earlier: .debug_line is in DWARF 2 format (= version 2). // - GCC 9 and 10: .debug_line is in DWARF 3 format (= version 3). // - GCC 11: .debug_line is in DWARF 4 format (= version 4).
DWARF_LOG_ERROR(".debug_line in unsupported DWARF version %" PRIu16, _header._version) returnfalse;
}
if (!_reader.read_dword(&_header._header_length)) { returnfalse;
}
// To ensure not to read too many bytes in case of file corruption when reading the path_names field.
_reader.set_max_pos(_reader.get_position() + _header._header_length);
if (!_reader.read_byte(&_header._minimum_instruction_length)) { returnfalse;
}
if (_header._version == 4) { if (!_reader.read_byte(&_header._maximum_operations_per_instruction)) { returnfalse;
}
}
if (!_reader.read_byte(&_header._default_is_stmt)) { returnfalse;
}
if (!_reader.read_byte(&_header._line_base)) { returnfalse;
}
if (!_reader.read_byte(&_header._line_range)) { returnfalse;
}
if (!_reader.read_byte(&_header._opcode_base) || _header._opcode_base - 1 != 12) { // There are 12 standard opcodes for DWARF 3 and 4.
DWARF_LOG_ERROR("Wrong number of opcodes: %" PRIu8, _header._opcode_base) returnfalse;
}
for (uint8_t i = 0; i < _header._opcode_base - 1; i++) { if (!_reader.read_byte(&_header._standard_opcode_lengths[i])) { returnfalse;
}
}
// Read field include_directories which is a sequence of path names. These are terminated by a single null byte. // We do not care about them, just read the strings and move on. while (_reader.read_string()) { }
// Delay reading file_names until we found the correct file index in the line number program. Store the position where // the file names start to parse them later. We directly jump to the line number program which starts at offset // header_size (=HEADER_DESCRIPTION_BYTES + _header_length) + _debug_line_offset
_header._file_names_offset = _reader.get_position();
uint32_t header_size = LineNumberProgramHeader::HEADER_DESCRIPTION_BYTES + _header._header_length; if (!_reader.set_position(shdr.sh_offset + header_size + _debug_line_offset)) { returnfalse;
}
// Now reset the max position to where the line number information for this compilation unit ends (i.e. where the state // machine gets terminated). Add 4 bytes to the offset because the size of the _unit_length field is not included in this // value.
_reader.set_max_pos(shdr.sh_offset + _debug_line_offset + _header._unit_length + 4); returntrue;
}
// Create the line number information matrix as described in section 6.2 of the DWARF 4 spec. Try to find the correct entry // by comparing the address register belonging to each matrix row with _offset_in_library. Once it is found, we can read // the line number from the line register and the filename by parsing the file_names list from the header until we reach // the correct filename as specified by the file register. // // If space was not a problem, the .debug_line section could provide a large matrix that contains an entry for each // compiler instruction that contains the line number, the column number, the filename etc. But that's impractical. // Two techniques optimize such a matrix: // (1) If two offsets share the same file, line and column (and discriminator) information, the row is dropped. // (2) We store a stream of bytes that represent opcodes to be executed in a well-defined state machine language // instead of actually storing the entire matrix row by row. // // Let's consider a simple example: // 25: int iFld = 42; // 26: // 27: void bar(int i) { // 28: } // 29: // 30: void foo() { // 31: bar(*iFld); // 32: } // // Disassembly of foo() with source code: // 30: void foo() { // 0x55d132: 55 push rbp // 0x55d133: 48 89 e5 mov rbp,rsp // 31: bar(*iFld); // 0x55d136: 48 8b 05 b3 ee e8 01 mov rax,QWORD PTR [rip+0x1e8eeb3] # 23ebff0 <iFld> // 0x55d13d: 8b 00 mov eax,DWORD PTR [rax] // 0x55d13f: 89 c7 mov edi,eax // 0x55d141: e8 e2 ff ff ff call 55d128 <_Z3bari> // 32: } // 0x55d146: 90 nop // 0x55d147: 5d pop rbp // 0x55d148: c3 ret // // This would produce the following matrix for foo() where duplicated lines (0x55d133, 0x55d13d, 0x55d13f) were removed // according to (1): // Address: Line: Column: File: // 0x55d132 30 12 1 // 0x55d136 31 6 1 // 0x55d146 32 1 1 // // 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: // - Directly find the last entry in the matrix for which address == x (there could be multiple entries with the same address). // - If there is no matching address for x: // 1. Find two consecutive entries in the matrix for which: address_entry_1 < x < address_entry_2. // 2. Then take the entry of address_entry_1. // E.g. x = 0x55d13f -> 0x55d136 < 0x55d13f < 0x55d146 -> Take entry 0x55d136. // // Enable logging with debug level to print the generated line number information matrix. bool DwarfFile::LineNumberProgram::run_line_number_program(char* filename, const size_t filename_len, int* line) {
DWARF_LOG_DEBUG("");
DWARF_LOG_DEBUG("Line Number Information Matrix");
DWARF_LOG_DEBUG("------------------------------"); #ifndef _LP64
DWARF_LOG_DEBUG("Address: Line: Column: File:"); #else
DWARF_LOG_DEBUG("Address: Line: Column: File:"); #endif
_state = new (std::nothrow) LineNumberProgramState(_header); if (_state == nullptr) {
DWARF_LOG_ERROR("Failed to create new LineNumberProgramState object"); returnfalse;
}
uintptr_t previous_address = 0;
uint32_t previous_file = 0;
uint32_t previous_line = 0; bool found_entry = false; bool candidate = false; bool first_in_sequence = true; while (_reader.has_bytes_left()) { if (!apply_opcode()) {
assert(false, "Could not apply opcode"); returnfalse;
}
if (_state->_append_row) { // Append a new line to the line number information matrix. if (_state->_first_entry_in_sequence) { // First entry in sequence: Check if _offset_in_library >= _state->address. If not, then all following entries // belonging to this sequence cannot match our _offset_in_library because the addresses are always increasing // in a sequence.
_state->_can_sequence_match_offset = _offset_in_library >= _state->_address;
_state->_first_entry_in_sequence = false;
} if (does_offset_match_entry(previous_address, previous_file, previous_line)) { // We are using an int for the line number which should never be larger than INT_MAX for any files.
*line = (int)_state->_line; return get_filename_from_header(_state->_file, filename, filename_len);
}
// We do not actually store the matrix while searching the correct entry. Enable logging to print/debug it.
DWARF_LOG_DEBUG(INTPTR_FORMAT " %-5u %-3u %-4u",
_state->_address, _state->_line, _state->_column, _state->_file);
previous_file = _state->_file;
previous_line = _state->_line;
previous_address = _state->_address;
_state->_append_row = false; if (_state->_do_reset) { // Current sequence terminated.
_state->reset_fields();
}
}
}
assert(false, "Did not find an entry in the line number information matrix that matches " UINT32_FORMAT_X_0, _offset_in_library); returnfalse;
}
// Apply next opcode to update the state machine. bool DwarfFile::LineNumberProgram::apply_opcode() {
uint8_t opcode; if (!_reader.read_byte(&opcode)) { returnfalse;
}
DWARF_LOG_TRACE(" Opcode: 0x%02x ", opcode); if (opcode == 0) { // Extended opcodes start with a zero byte. if (!apply_extended_opcode()) {
assert(false, "Could not apply extended opcode"); returnfalse;
}
} elseif (opcode <= 12) { // 12 standard opcodes in DWARF 3 and 4. if (!apply_standard_opcode(opcode)) {
assert(false, "Could not apply standard opcode"); returnfalse;
}
} else { // Special opcodes range from 13 until 255.
apply_special_opcode(opcode);
} returntrue;
}
// Specified in section 6.2.5.3 of the DWARF 4 spec. bool DwarfFile::LineNumberProgram::apply_extended_opcode() {
uint64_t extended_opcode_length; // Does not include the already written zero byte and the length leb128.
uint8_t extended_opcode; if (!_reader.read_uleb128(&extended_opcode_length) || !_reader.read_byte(&extended_opcode)) { returnfalse;
}
switch (extended_opcode) { case DW_LNE_end_sequence: // No operands
DWARF_LOG_TRACE(" DW_LNE_end_sequence");
_state->_end_sequence = true;
_state->_append_row = true;
_state->_do_reset = true; break; case DW_LNE_set_address: // 1 operand if (!_reader.read_address_sized(&_state->_address)) { returnfalse;
}
DWARF_LOG_TRACE(" DW_LNE_set_address " INTPTR_FORMAT, _state->_address); if (_state->_dwarf_version == 4) {
_state->_op_index = 0;
} break; case DW_LNE_define_file: // 4 operands
DWARF_LOG_TRACE(" DW_LNE_define_file"); if (!_reader.read_string()) { returnfalse;
} // Operand 2-4: uleb128 numbers we do not care about. if (!_reader.read_uleb128_ignore()
|| !_reader.read_uleb128_ignore()
|| !_reader.read_uleb128_ignore()) { returnfalse;
} break; case DW_LNE_set_discriminator: // 1 operand
DWARF_LOG_TRACE(" DW_LNE_set_discriminator");
uint64_t discriminator; // For some reason, GCC emits this opcode even for earlier versions than DWARF 4 which introduced this opcode. // We need to consume it. if (!_reader.read_uleb128(&discriminator, 4)) { // Must be an unsigned integer as specified in section 6.2.2 of the DWARF 4 spec for the discriminator register. returnfalse;
}
_state->_discriminator = discriminator; break; default:
assert(false, "Unknown extended opcode"); returnfalse;
} returntrue;
}
// Specified in section 6.2.5.2 of the DWARF 4 spec. bool DwarfFile::LineNumberProgram::apply_standard_opcode(const uint8_t opcode) { switch (opcode) { case DW_LNS_copy: // No operands
DWARF_LOG_TRACE(" DW_LNS_copy");
_state->_append_row = true;
_state->_basic_block = false;
_state->_prologue_end = false;
_state->_epilogue_begin = false; if (_state->_dwarf_version == 4) {
_state->_discriminator = 0;
} break; case DW_LNS_advance_pc: { // 1 operand
uint64_t operation_advance; if (!_reader.read_uleb128(&operation_advance, 4)) { // Must be at most 4 bytes because the index register is only 4 bytes wide. returnfalse;
}
_state->add_to_address_register(operation_advance, _header); if (_state->_dwarf_version == 4) {
_state->set_index_register(operation_advance, _header);
}
DWARF_LOG_TRACE(" DW_LNS_advance_pc (" INTPTR_FORMAT ")", _state->_address); break;
} case DW_LNS_advance_line: // 1 operand
int64_t line; if (!_reader.read_sleb128(&line, 4)) { // line register is 4 bytes wide. returnfalse;
}
_state->_line += line;
DWARF_LOG_TRACE(" DW_LNS_advance_line (%d)", _state->_line); break; case DW_LNS_set_file: // 1 operand
uint64_t file; if (!_reader.read_uleb128(&file, 4)) { // file register is 4 bytes wide. returnfalse;
}
_state->_file = file;
DWARF_LOG_TRACE(" DW_LNS_set_file (%u)", _state->_file); break; case DW_LNS_set_column: // 1 operand
uint64_t column; if (!_reader.read_uleb128(&column, 4)) { // column register is 4 bytes wide. returnfalse;
}
_state->_column = column;
DWARF_LOG_TRACE(" DW_LNS_set_column (%u)", _state->_column); break; case DW_LNS_negate_stmt: // No operands
DWARF_LOG_TRACE(" DW_LNS_negate_stmt");
_state->_is_stmt = !_state->_is_stmt; break; case DW_LNS_set_basic_block: // No operands
DWARF_LOG_TRACE(" DW_LNS_set_basic_block");
_state->_basic_block = true; break; case DW_LNS_const_add_pc: { // No operands // Update address and op_index registers by the increments of special opcode 255.
uint8_t adjusted_opcode_255 = 255 - _header._opcode_base;
uint8_t operation_advance = adjusted_opcode_255 / _header._line_range;
uintptr_t old_address = _state->_address;
_state->add_to_address_register(operation_advance, _header); if (_state->_dwarf_version == 4) {
_state->set_index_register(operation_advance, _header);
}
DWARF_LOG_TRACE(" DW_LNS_const_add_pc (" INTPTR_FORMAT ")", _state->_address - old_address); break;
} case DW_LNS_fixed_advance_pc: // 1 operand
uint16_t operand; if (!_reader.read_word(&operand)) { returnfalse;
}
_state->_address += operand;
_state->_op_index = 0;
DWARF_LOG_TRACE(" DW_LNS_fixed_advance_pc (" INTPTR_FORMAT ")", _state->_address); break; case DW_LNS_set_prologue_end: // No operands
DWARF_LOG_TRACE(" DW_LNS_set_basic_block");
_state->_prologue_end = true; break; case DW_LNS_set_epilogue_begin: // No operands
DWARF_LOG_TRACE(" DW_LNS_set_epilogue_begin");
_state->_epilogue_begin = true; break; case DW_LNS_set_isa: // 1 operand
uint64_t isa; if (!_reader.read_uleb128(&isa, 4)) { // isa register is 4 bytes wide. returnfalse;
}
_state->_isa = isa;
DWARF_LOG_TRACE(" DW_LNS_set_isa (%u)", _state->_isa); break; default:
assert(false, "Unknown standard opcode"); returnfalse;
} returntrue;
}
bool DwarfFile::LineNumberProgram::does_offset_match_entry(const uintptr_t previous_address, const uint32_t previous_file, const uint32_t previous_line) { if (_state->_can_sequence_match_offset) { bool matches_entry_directly = _offset_in_library == _state->_address; if (matches_entry_directly
|| (_offset_in_library > previous_address && _offset_in_library < _state->_address)) { // in between two entries
_state->_found_match = true; if (!matches_entry_directly || _is_pc_after_call) { // We take the previous row in the matrix either when: // - We try to match an offset that is between two entries. // - We have an offset from a PC that is at a call-site in which case we need to get the line information for // the call instruction in the previous entry.
print_and_store_prev_entry(previous_file, previous_line); returntrue;
} elseif (!_reader.has_bytes_left()) { // We take the current entry when this is the very last entry in the matrix (i.e. must be the right one).
DWARF_LOG_DEBUG("^^^ Found line for requested offset " UINT32_FORMAT_X_0 " ^^^", _offset_in_library); returntrue;
} // Else: Exact match. We cannot take this entry because we do not know if there are more entries following this // one with the same offset (we could have multiple entries for the same address in the matrix). Continue // to parse entries. When we have the first non-exact match, then we know that the previous entry is the // correct one to take (handled in the else-if-case below). If this is the very last entry in a matrix, // we will take the current entry (handled in else-if-case above).
} elseif (_state->_found_match) { // We found an entry before with an exact match. This is now the first entry with a new offset. Pick the previous // entry which matches our offset and is guaranteed to be the last entry which matches our offset (if there are // multiple entries with the same offset).
print_and_store_prev_entry(previous_file, previous_line); returntrue;
}
} returnfalse;
}
void DwarfFile::LineNumberProgram::print_and_store_prev_entry(const uint32_t previous_file, const uint32_t previous_line) {
_state->_file = previous_file;
_state->_line = previous_line;
DWARF_LOG_DEBUG("^^^ Found line for requested offset " UINT32_FORMAT_X_0 " ^^^", _offset_in_library); // Also print the currently parsed entry.
DWARF_LOG_DEBUG(INTPTR_FORMAT " %-5u %-3u %-4u",
_state->_address, _state->_line, _state->_column, _state->_file);
}
// Read field file_names from the header as specified in section 6.2.4 of the DWARF 4 spec. bool DwarfFile::LineNumberProgram::get_filename_from_header(const uint32_t file_index, char* filename, const size_t filename_len) { // We do not need to restore the position afterwards as this is the last step of parsing from the file for this compilation unit.
_reader.set_position(_header._file_names_offset);
uint32_t current_index = 1; // file_names start at index 1 while (_reader.has_bytes_left()) { if (current_index == file_index) { // Found correct file. return read_filename(filename, filename_len);
} elseif (!_reader.read_string()) { // We don't care about this filename string. Read and ignore it. // Either an error while reading or we have reached the end of the file_names section before reaching the file_index. // Both should not happen. returnfalse;
}
// We don't care about these values. if (!_reader.read_uleb128_ignore() // Read directory index
|| !_reader.read_uleb128_ignore() // Read last modification of file
|| !_reader.read_uleb128_ignore()) { // Read file length returnfalse;
}
current_index++;
}
DWARF_LOG_DEBUG("Did not find filename entry at index " UINT32_FORMAT " in .debug_line header", file_index); returnfalse;
}
// Read the filename into the provided 'filename' buffer. If it does not fit, an alternative smaller tag will be emitted // in order to let the DWARF parser succeed. The line number with a function name will almost always be sufficient to get // to the actual source code location. bool DwarfFile::LineNumberProgram::read_filename(char* filename, const size_t filename_len) { char next_char; if (!_reader.read_non_null_char(&next_char)) { // Either error while reading or read an empty string which indicates the end of the file_names section. // Both should not happen. returnfalse;
}
filename[0] = next_char;
size_t index = 1; bool overflow_filename = false; // Is the currently read filename overflowing the provided 'filename' buffer? while (next_char != '\0' && _reader.has_bytes_left()) { if (!_reader.read_byte(&next_char)) { returnfalse;
} if (next_char == *os::file_separator()) { // Skip file separator to get to the actual filename and reset the buffer and overflow flag. GCC does not emit // file separators while Clang does.
index = 0;
overflow_filename = false;
} elseif (index == filename_len) { // Just keep reading as we could read another file separator and reset the buffer again. But don't bother to store // the additionally read characters as it would not fit into the buffer anyway.
overflow_filename = true;
} else {
assert(!overflow_filename, "sanity check");
filename[index] = next_char;
index++;
}
}
if (overflow_filename) { // 'filename' buffer overflow. Store either a generic overflow message or a minimal filename.
write_filename_for_overflow(filename, filename_len);
} returntrue;
}
// Try to write a generic overflow message to the provided buffer. If it does not fit, store the minimal filename "L" // which always fits to get the source information in the form "L:line_number". void DwarfFile::LineNumberProgram::write_filename_for_overflow(char* filename, const size_t filename_len) {
DWARF_LOG_ERROR("DWARF filename string is too large to fit into the provided buffer of size %zu.", filename_len);
--> --------------------
--> maximum size reached
--> --------------------
¤ Dauer der Verarbeitung: 0.30 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.