17d62b00eSchristos /* DWARF 2 debugging format support for GDB. 27d62b00eSchristos 3*6881a400Schristos Copyright (C) 1994-2023 Free Software Foundation, Inc. 47d62b00eSchristos 57d62b00eSchristos This file is part of GDB. 67d62b00eSchristos 77d62b00eSchristos This program is free software; you can redistribute it and/or modify 87d62b00eSchristos it under the terms of the GNU General Public License as published by 97d62b00eSchristos the Free Software Foundation; either version 3 of the License, or 107d62b00eSchristos (at your option) any later version. 117d62b00eSchristos 127d62b00eSchristos This program is distributed in the hope that it will be useful, 137d62b00eSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 147d62b00eSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 157d62b00eSchristos GNU General Public License for more details. 167d62b00eSchristos 177d62b00eSchristos You should have received a copy of the GNU General Public License 187d62b00eSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 197d62b00eSchristos 207d62b00eSchristos #include "defs.h" 21*6881a400Schristos #include "dwarf2/comp-unit-head.h" 227d62b00eSchristos #include "dwarf2/leb.h" 237d62b00eSchristos #include "dwarf2/line-header.h" 247d62b00eSchristos #include "dwarf2/read.h" 257d62b00eSchristos #include "complaints.h" 267d62b00eSchristos #include "filenames.h" 27*6881a400Schristos #include "gdbsupport/pathstuff.h" 287d62b00eSchristos 297d62b00eSchristos void 307d62b00eSchristos line_header::add_include_dir (const char *include_dir) 317d62b00eSchristos { 327d62b00eSchristos if (dwarf_line_debug >= 2) 337d62b00eSchristos { 347d62b00eSchristos size_t new_size; 357d62b00eSchristos if (version >= 5) 367d62b00eSchristos new_size = m_include_dirs.size (); 377d62b00eSchristos else 387d62b00eSchristos new_size = m_include_dirs.size () + 1; 39*6881a400Schristos gdb_printf (gdb_stdlog, "Adding dir %zu: %s\n", 407d62b00eSchristos new_size, include_dir); 417d62b00eSchristos } 427d62b00eSchristos m_include_dirs.push_back (include_dir); 437d62b00eSchristos } 447d62b00eSchristos 457d62b00eSchristos void 467d62b00eSchristos line_header::add_file_name (const char *name, 477d62b00eSchristos dir_index d_index, 487d62b00eSchristos unsigned int mod_time, 497d62b00eSchristos unsigned int length) 507d62b00eSchristos { 51*6881a400Schristos file_name_index index 52*6881a400Schristos = version >= 5 ? file_names_size (): file_names_size () + 1; 53*6881a400Schristos 547d62b00eSchristos if (dwarf_line_debug >= 2) 55*6881a400Schristos gdb_printf (gdb_stdlog, "Adding file %d: %s\n", index, name); 56*6881a400Schristos 57*6881a400Schristos m_file_names.emplace_back (name, index, d_index, mod_time, length); 587d62b00eSchristos } 597d62b00eSchristos 60*6881a400Schristos std::string 61*6881a400Schristos line_header::file_file_name (const file_entry &fe) const 627d62b00eSchristos { 63*6881a400Schristos gdb_assert (is_valid_file_index (fe.index)); 647d62b00eSchristos 65*6881a400Schristos std::string ret = fe.name; 667d62b00eSchristos 67*6881a400Schristos if (IS_ABSOLUTE_PATH (ret)) 68*6881a400Schristos return ret; 697d62b00eSchristos 70*6881a400Schristos const char *dir = fe.include_dir (this); 71*6881a400Schristos if (dir != nullptr) 72*6881a400Schristos ret = path_join (dir, ret.c_str ()); 737d62b00eSchristos 74*6881a400Schristos if (IS_ABSOLUTE_PATH (ret)) 75*6881a400Schristos return ret; 767d62b00eSchristos 77*6881a400Schristos if (m_comp_dir != nullptr) 78*6881a400Schristos ret = path_join (m_comp_dir, ret.c_str ()); 797d62b00eSchristos 80*6881a400Schristos return ret; 817d62b00eSchristos } 827d62b00eSchristos 837d62b00eSchristos static void 847d62b00eSchristos dwarf2_statement_list_fits_in_line_number_section_complaint (void) 857d62b00eSchristos { 867d62b00eSchristos complaint (_("statement list doesn't fit in .debug_line section")); 877d62b00eSchristos } 887d62b00eSchristos 897d62b00eSchristos /* Cover function for read_initial_length. 907d62b00eSchristos Returns the length of the object at BUF, and stores the size of the 917d62b00eSchristos initial length in *BYTES_READ and stores the size that offsets will be in 927d62b00eSchristos *OFFSET_SIZE. 937d62b00eSchristos If the initial length size is not equivalent to that specified in 947d62b00eSchristos CU_HEADER then issue a complaint. 957d62b00eSchristos This is useful when reading non-comp-unit headers. */ 967d62b00eSchristos 977d62b00eSchristos static LONGEST 987d62b00eSchristos read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf, 997d62b00eSchristos const struct comp_unit_head *cu_header, 1007d62b00eSchristos unsigned int *bytes_read, 1017d62b00eSchristos unsigned int *offset_size) 1027d62b00eSchristos { 1037d62b00eSchristos LONGEST length = read_initial_length (abfd, buf, bytes_read); 1047d62b00eSchristos 1057d62b00eSchristos gdb_assert (cu_header->initial_length_size == 4 1067d62b00eSchristos || cu_header->initial_length_size == 8 1077d62b00eSchristos || cu_header->initial_length_size == 12); 1087d62b00eSchristos 1097d62b00eSchristos if (cu_header->initial_length_size != *bytes_read) 1107d62b00eSchristos complaint (_("intermixed 32-bit and 64-bit DWARF sections")); 1117d62b00eSchristos 1127d62b00eSchristos *offset_size = (*bytes_read == 4) ? 4 : 8; 1137d62b00eSchristos return length; 1147d62b00eSchristos } 1157d62b00eSchristos 1167d62b00eSchristos /* Read directory or file name entry format, starting with byte of 1177d62b00eSchristos format count entries, ULEB128 pairs of entry formats, ULEB128 of 1187d62b00eSchristos entries count and the entries themselves in the described entry 1197d62b00eSchristos format. */ 1207d62b00eSchristos 1217d62b00eSchristos static void 1227d62b00eSchristos read_formatted_entries (dwarf2_per_objfile *per_objfile, bfd *abfd, 1237d62b00eSchristos const gdb_byte **bufp, struct line_header *lh, 124*6881a400Schristos unsigned int offset_size, 1257d62b00eSchristos void (*callback) (struct line_header *lh, 1267d62b00eSchristos const char *name, 1277d62b00eSchristos dir_index d_index, 1287d62b00eSchristos unsigned int mod_time, 1297d62b00eSchristos unsigned int length)) 1307d62b00eSchristos { 1317d62b00eSchristos gdb_byte format_count, formati; 1327d62b00eSchristos ULONGEST data_count, datai; 1337d62b00eSchristos const gdb_byte *buf = *bufp; 1347d62b00eSchristos const gdb_byte *format_header_data; 1357d62b00eSchristos unsigned int bytes_read; 1367d62b00eSchristos 1377d62b00eSchristos format_count = read_1_byte (abfd, buf); 1387d62b00eSchristos buf += 1; 1397d62b00eSchristos format_header_data = buf; 1407d62b00eSchristos for (formati = 0; formati < format_count; formati++) 1417d62b00eSchristos { 1427d62b00eSchristos read_unsigned_leb128 (abfd, buf, &bytes_read); 1437d62b00eSchristos buf += bytes_read; 1447d62b00eSchristos read_unsigned_leb128 (abfd, buf, &bytes_read); 1457d62b00eSchristos buf += bytes_read; 1467d62b00eSchristos } 1477d62b00eSchristos 1487d62b00eSchristos data_count = read_unsigned_leb128 (abfd, buf, &bytes_read); 1497d62b00eSchristos buf += bytes_read; 1507d62b00eSchristos for (datai = 0; datai < data_count; datai++) 1517d62b00eSchristos { 1527d62b00eSchristos const gdb_byte *format = format_header_data; 1537d62b00eSchristos struct file_entry fe; 1547d62b00eSchristos 1557d62b00eSchristos for (formati = 0; formati < format_count; formati++) 1567d62b00eSchristos { 1577d62b00eSchristos ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read); 1587d62b00eSchristos format += bytes_read; 1597d62b00eSchristos 1607d62b00eSchristos ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read); 1617d62b00eSchristos format += bytes_read; 1627d62b00eSchristos 1637d62b00eSchristos gdb::optional<const char *> string; 1647d62b00eSchristos gdb::optional<unsigned int> uint; 1657d62b00eSchristos 1667d62b00eSchristos switch (form) 1677d62b00eSchristos { 1687d62b00eSchristos case DW_FORM_string: 1697d62b00eSchristos string.emplace (read_direct_string (abfd, buf, &bytes_read)); 1707d62b00eSchristos buf += bytes_read; 1717d62b00eSchristos break; 1727d62b00eSchristos 1737d62b00eSchristos case DW_FORM_line_strp: 174*6881a400Schristos { 175*6881a400Schristos const char *str 176*6881a400Schristos = per_objfile->read_line_string (buf, offset_size); 177*6881a400Schristos string.emplace (str); 178*6881a400Schristos buf += offset_size; 179*6881a400Schristos } 1807d62b00eSchristos break; 1817d62b00eSchristos 1827d62b00eSchristos case DW_FORM_data1: 1837d62b00eSchristos uint.emplace (read_1_byte (abfd, buf)); 1847d62b00eSchristos buf += 1; 1857d62b00eSchristos break; 1867d62b00eSchristos 1877d62b00eSchristos case DW_FORM_data2: 1887d62b00eSchristos uint.emplace (read_2_bytes (abfd, buf)); 1897d62b00eSchristos buf += 2; 1907d62b00eSchristos break; 1917d62b00eSchristos 1927d62b00eSchristos case DW_FORM_data4: 1937d62b00eSchristos uint.emplace (read_4_bytes (abfd, buf)); 1947d62b00eSchristos buf += 4; 1957d62b00eSchristos break; 1967d62b00eSchristos 1977d62b00eSchristos case DW_FORM_data8: 1987d62b00eSchristos uint.emplace (read_8_bytes (abfd, buf)); 1997d62b00eSchristos buf += 8; 2007d62b00eSchristos break; 2017d62b00eSchristos 2027d62b00eSchristos case DW_FORM_data16: 2037d62b00eSchristos /* This is used for MD5, but file_entry does not record MD5s. */ 2047d62b00eSchristos buf += 16; 2057d62b00eSchristos break; 2067d62b00eSchristos 2077d62b00eSchristos case DW_FORM_udata: 2087d62b00eSchristos uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read)); 2097d62b00eSchristos buf += bytes_read; 2107d62b00eSchristos break; 2117d62b00eSchristos 2127d62b00eSchristos case DW_FORM_block: 2137d62b00eSchristos /* It is valid only for DW_LNCT_timestamp which is ignored by 2147d62b00eSchristos current GDB. */ 2157d62b00eSchristos break; 2167d62b00eSchristos } 2177d62b00eSchristos 218*6881a400Schristos /* Normalize nullptr string. */ 219*6881a400Schristos if (string.has_value () && *string == nullptr) 220*6881a400Schristos string.emplace (""); 221*6881a400Schristos 2227d62b00eSchristos switch (content_type) 2237d62b00eSchristos { 2247d62b00eSchristos case DW_LNCT_path: 2257d62b00eSchristos if (string.has_value ()) 2267d62b00eSchristos fe.name = *string; 2277d62b00eSchristos break; 2287d62b00eSchristos case DW_LNCT_directory_index: 2297d62b00eSchristos if (uint.has_value ()) 2307d62b00eSchristos fe.d_index = (dir_index) *uint; 2317d62b00eSchristos break; 2327d62b00eSchristos case DW_LNCT_timestamp: 2337d62b00eSchristos if (uint.has_value ()) 2347d62b00eSchristos fe.mod_time = *uint; 2357d62b00eSchristos break; 2367d62b00eSchristos case DW_LNCT_size: 2377d62b00eSchristos if (uint.has_value ()) 2387d62b00eSchristos fe.length = *uint; 2397d62b00eSchristos break; 2407d62b00eSchristos case DW_LNCT_MD5: 2417d62b00eSchristos break; 2427d62b00eSchristos default: 2437d62b00eSchristos complaint (_("Unknown format content type %s"), 2447d62b00eSchristos pulongest (content_type)); 2457d62b00eSchristos } 2467d62b00eSchristos } 2477d62b00eSchristos 2487d62b00eSchristos callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length); 2497d62b00eSchristos } 2507d62b00eSchristos 2517d62b00eSchristos *bufp = buf; 2527d62b00eSchristos } 2537d62b00eSchristos 2547d62b00eSchristos /* See line-header.h. */ 2557d62b00eSchristos 2567d62b00eSchristos line_header_up 2577d62b00eSchristos dwarf_decode_line_header (sect_offset sect_off, bool is_dwz, 2587d62b00eSchristos dwarf2_per_objfile *per_objfile, 2597d62b00eSchristos struct dwarf2_section_info *section, 260*6881a400Schristos const struct comp_unit_head *cu_header, 261*6881a400Schristos const char *comp_dir) 2627d62b00eSchristos { 2637d62b00eSchristos const gdb_byte *line_ptr; 2647d62b00eSchristos unsigned int bytes_read, offset_size; 2657d62b00eSchristos int i; 2667d62b00eSchristos const char *cur_dir, *cur_file; 2677d62b00eSchristos 2687d62b00eSchristos bfd *abfd = section->get_bfd_owner (); 2697d62b00eSchristos 2707d62b00eSchristos /* Make sure that at least there's room for the total_length field. 2717d62b00eSchristos That could be 12 bytes long, but we're just going to fudge that. */ 2727d62b00eSchristos if (to_underlying (sect_off) + 4 >= section->size) 2737d62b00eSchristos { 2747d62b00eSchristos dwarf2_statement_list_fits_in_line_number_section_complaint (); 2757d62b00eSchristos return 0; 2767d62b00eSchristos } 2777d62b00eSchristos 278*6881a400Schristos line_header_up lh (new line_header (comp_dir)); 2797d62b00eSchristos 2807d62b00eSchristos lh->sect_off = sect_off; 2817d62b00eSchristos lh->offset_in_dwz = is_dwz; 2827d62b00eSchristos 2837d62b00eSchristos line_ptr = section->buffer + to_underlying (sect_off); 2847d62b00eSchristos 2857d62b00eSchristos /* Read in the header. */ 286*6881a400Schristos LONGEST unit_length 287*6881a400Schristos = read_checked_initial_length_and_offset (abfd, line_ptr, cu_header, 2887d62b00eSchristos &bytes_read, &offset_size); 2897d62b00eSchristos line_ptr += bytes_read; 2907d62b00eSchristos 2917d62b00eSchristos const gdb_byte *start_here = line_ptr; 2927d62b00eSchristos 293*6881a400Schristos if (line_ptr + unit_length > (section->buffer + section->size)) 2947d62b00eSchristos { 2957d62b00eSchristos dwarf2_statement_list_fits_in_line_number_section_complaint (); 2967d62b00eSchristos return 0; 2977d62b00eSchristos } 298*6881a400Schristos lh->statement_program_end = start_here + unit_length; 2997d62b00eSchristos lh->version = read_2_bytes (abfd, line_ptr); 3007d62b00eSchristos line_ptr += 2; 3017d62b00eSchristos if (lh->version > 5) 3027d62b00eSchristos { 3037d62b00eSchristos /* This is a version we don't understand. The format could have 3047d62b00eSchristos changed in ways we don't handle properly so just punt. */ 3057d62b00eSchristos complaint (_("unsupported version in .debug_line section")); 3067d62b00eSchristos return NULL; 3077d62b00eSchristos } 3087d62b00eSchristos if (lh->version >= 5) 3097d62b00eSchristos { 3107d62b00eSchristos gdb_byte segment_selector_size; 3117d62b00eSchristos 3127d62b00eSchristos /* Skip address size. */ 3137d62b00eSchristos read_1_byte (abfd, line_ptr); 3147d62b00eSchristos line_ptr += 1; 3157d62b00eSchristos 3167d62b00eSchristos segment_selector_size = read_1_byte (abfd, line_ptr); 3177d62b00eSchristos line_ptr += 1; 3187d62b00eSchristos if (segment_selector_size != 0) 3197d62b00eSchristos { 3207d62b00eSchristos complaint (_("unsupported segment selector size %u " 3217d62b00eSchristos "in .debug_line section"), 3227d62b00eSchristos segment_selector_size); 3237d62b00eSchristos return NULL; 3247d62b00eSchristos } 3257d62b00eSchristos } 326*6881a400Schristos 327*6881a400Schristos LONGEST header_length = read_offset (abfd, line_ptr, offset_size); 3287d62b00eSchristos line_ptr += offset_size; 329*6881a400Schristos lh->statement_program_start = line_ptr + header_length; 3307d62b00eSchristos lh->minimum_instruction_length = read_1_byte (abfd, line_ptr); 3317d62b00eSchristos line_ptr += 1; 332*6881a400Schristos 3337d62b00eSchristos if (lh->version >= 4) 3347d62b00eSchristos { 3357d62b00eSchristos lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr); 3367d62b00eSchristos line_ptr += 1; 3377d62b00eSchristos } 3387d62b00eSchristos else 3397d62b00eSchristos lh->maximum_ops_per_instruction = 1; 3407d62b00eSchristos 3417d62b00eSchristos if (lh->maximum_ops_per_instruction == 0) 3427d62b00eSchristos { 3437d62b00eSchristos lh->maximum_ops_per_instruction = 1; 3447d62b00eSchristos complaint (_("invalid maximum_ops_per_instruction " 3457d62b00eSchristos "in `.debug_line' section")); 3467d62b00eSchristos } 3477d62b00eSchristos 3487d62b00eSchristos lh->default_is_stmt = read_1_byte (abfd, line_ptr); 3497d62b00eSchristos line_ptr += 1; 3507d62b00eSchristos lh->line_base = read_1_signed_byte (abfd, line_ptr); 3517d62b00eSchristos line_ptr += 1; 3527d62b00eSchristos lh->line_range = read_1_byte (abfd, line_ptr); 3537d62b00eSchristos line_ptr += 1; 3547d62b00eSchristos lh->opcode_base = read_1_byte (abfd, line_ptr); 3557d62b00eSchristos line_ptr += 1; 3567d62b00eSchristos lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]); 3577d62b00eSchristos 3587d62b00eSchristos lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */ 3597d62b00eSchristos for (i = 1; i < lh->opcode_base; ++i) 3607d62b00eSchristos { 3617d62b00eSchristos lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); 3627d62b00eSchristos line_ptr += 1; 3637d62b00eSchristos } 3647d62b00eSchristos 3657d62b00eSchristos if (lh->version >= 5) 3667d62b00eSchristos { 3677d62b00eSchristos /* Read directory table. */ 3687d62b00eSchristos read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (), 369*6881a400Schristos offset_size, 3707d62b00eSchristos [] (struct line_header *header, const char *name, 3717d62b00eSchristos dir_index d_index, unsigned int mod_time, 3727d62b00eSchristos unsigned int length) 3737d62b00eSchristos { 3747d62b00eSchristos header->add_include_dir (name); 3757d62b00eSchristos }); 3767d62b00eSchristos 3777d62b00eSchristos /* Read file name table. */ 3787d62b00eSchristos read_formatted_entries (per_objfile, abfd, &line_ptr, lh.get (), 379*6881a400Schristos offset_size, 3807d62b00eSchristos [] (struct line_header *header, const char *name, 3817d62b00eSchristos dir_index d_index, unsigned int mod_time, 3827d62b00eSchristos unsigned int length) 3837d62b00eSchristos { 3847d62b00eSchristos header->add_file_name (name, d_index, mod_time, length); 3857d62b00eSchristos }); 3867d62b00eSchristos } 3877d62b00eSchristos else 3887d62b00eSchristos { 3897d62b00eSchristos /* Read directory table. */ 3907d62b00eSchristos while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL) 3917d62b00eSchristos { 3927d62b00eSchristos line_ptr += bytes_read; 3937d62b00eSchristos lh->add_include_dir (cur_dir); 3947d62b00eSchristos } 3957d62b00eSchristos line_ptr += bytes_read; 3967d62b00eSchristos 3977d62b00eSchristos /* Read file name table. */ 3987d62b00eSchristos while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL) 3997d62b00eSchristos { 4007d62b00eSchristos unsigned int mod_time, length; 4017d62b00eSchristos dir_index d_index; 4027d62b00eSchristos 4037d62b00eSchristos line_ptr += bytes_read; 4047d62b00eSchristos d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read); 4057d62b00eSchristos line_ptr += bytes_read; 4067d62b00eSchristos mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); 4077d62b00eSchristos line_ptr += bytes_read; 4087d62b00eSchristos length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); 4097d62b00eSchristos line_ptr += bytes_read; 4107d62b00eSchristos 4117d62b00eSchristos lh->add_file_name (cur_file, d_index, mod_time, length); 4127d62b00eSchristos } 4137d62b00eSchristos line_ptr += bytes_read; 4147d62b00eSchristos } 4157d62b00eSchristos 4167d62b00eSchristos if (line_ptr > (section->buffer + section->size)) 4177d62b00eSchristos complaint (_("line number info header doesn't " 4187d62b00eSchristos "fit in `.debug_line' section")); 4197d62b00eSchristos 4207d62b00eSchristos return lh; 4217d62b00eSchristos } 422