comp-unit-head.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* DWARF 2 debugging format support for GDB.
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
  4. Inc. with support from Florida State University (under contract
  5. with the Ada Joint Program Office), and Silicon Graphics, Inc.
  6. Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
  7. based on Fred Fish's (Cygnus Support) implementation of DWARF 1
  8. support.
  9. This file is part of GDB.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 3 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  20. #include "defs.h"
  21. #include "dwarf2/comp-unit-head.h"
  22. #include "dwarf2/leb.h"
  23. #include "dwarf2/read.h"
  24. #include "dwarf2/section.h"
  25. #include "dwarf2/stringify.h"
  26. /* See comp-unit-head.h. */
  27. const gdb_byte *
  28. read_comp_unit_head (struct comp_unit_head *cu_header,
  29. const gdb_byte *info_ptr,
  30. struct dwarf2_section_info *section,
  31. rcuh_kind section_kind)
  32. {
  33. int signed_addr;
  34. unsigned int bytes_read;
  35. const char *filename = section->get_file_name ();
  36. bfd *abfd = section->get_bfd_owner ();
  37. cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
  38. cu_header->initial_length_size = bytes_read;
  39. cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
  40. info_ptr += bytes_read;
  41. unsigned version = read_2_bytes (abfd, info_ptr);
  42. if (version < 2 || version > 5)
  43. error (_("Dwarf Error: wrong version in compilation unit header "
  44. "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
  45. version, filename);
  46. cu_header->version = version;
  47. info_ptr += 2;
  48. if (cu_header->version < 5)
  49. switch (section_kind)
  50. {
  51. case rcuh_kind::COMPILE:
  52. cu_header->unit_type = DW_UT_compile;
  53. break;
  54. case rcuh_kind::TYPE:
  55. cu_header->unit_type = DW_UT_type;
  56. break;
  57. default:
  58. internal_error (__FILE__, __LINE__,
  59. _("read_comp_unit_head: invalid section_kind"));
  60. }
  61. else
  62. {
  63. cu_header->unit_type = static_cast<enum dwarf_unit_type>
  64. (read_1_byte (abfd, info_ptr));
  65. info_ptr += 1;
  66. switch (cu_header->unit_type)
  67. {
  68. case DW_UT_compile:
  69. case DW_UT_partial:
  70. case DW_UT_skeleton:
  71. case DW_UT_split_compile:
  72. if (section_kind != rcuh_kind::COMPILE)
  73. error (_("Dwarf Error: wrong unit_type in compilation unit header "
  74. "(is %s, should be %s) [in module %s]"),
  75. dwarf_unit_type_name (cu_header->unit_type),
  76. dwarf_unit_type_name (DW_UT_type), filename);
  77. break;
  78. case DW_UT_type:
  79. case DW_UT_split_type:
  80. section_kind = rcuh_kind::TYPE;
  81. break;
  82. default:
  83. error (_("Dwarf Error: wrong unit_type in compilation unit header "
  84. "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
  85. "[in module %s]"), cu_header->unit_type,
  86. dwarf_unit_type_name (DW_UT_compile),
  87. dwarf_unit_type_name (DW_UT_skeleton),
  88. dwarf_unit_type_name (DW_UT_split_compile),
  89. dwarf_unit_type_name (DW_UT_type),
  90. dwarf_unit_type_name (DW_UT_split_type), filename);
  91. }
  92. cu_header->addr_size = read_1_byte (abfd, info_ptr);
  93. info_ptr += 1;
  94. }
  95. cu_header->abbrev_sect_off
  96. = (sect_offset) cu_header->read_offset (abfd, info_ptr, &bytes_read);
  97. info_ptr += bytes_read;
  98. if (cu_header->version < 5)
  99. {
  100. cu_header->addr_size = read_1_byte (abfd, info_ptr);
  101. info_ptr += 1;
  102. }
  103. signed_addr = bfd_get_sign_extend_vma (abfd);
  104. if (signed_addr < 0)
  105. internal_error (__FILE__, __LINE__,
  106. _("read_comp_unit_head: dwarf from non elf file"));
  107. cu_header->signed_addr_p = signed_addr;
  108. bool header_has_signature = section_kind == rcuh_kind::TYPE
  109. || cu_header->unit_type == DW_UT_skeleton
  110. || cu_header->unit_type == DW_UT_split_compile;
  111. if (header_has_signature)
  112. {
  113. cu_header->signature = read_8_bytes (abfd, info_ptr);
  114. info_ptr += 8;
  115. }
  116. if (section_kind == rcuh_kind::TYPE)
  117. {
  118. LONGEST type_offset;
  119. type_offset = cu_header->read_offset (abfd, info_ptr, &bytes_read);
  120. info_ptr += bytes_read;
  121. cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
  122. if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
  123. error (_("Dwarf Error: Too big type_offset in compilation unit "
  124. "header (is %s) [in module %s]"), plongest (type_offset),
  125. filename);
  126. }
  127. return info_ptr;
  128. }
  129. /* Subroutine of read_and_check_comp_unit_head and
  130. read_and_check_type_unit_head to simplify them.
  131. Perform various error checking on the header. */
  132. static void
  133. error_check_comp_unit_head (dwarf2_per_objfile *per_objfile,
  134. struct comp_unit_head *header,
  135. struct dwarf2_section_info *section,
  136. struct dwarf2_section_info *abbrev_section)
  137. {
  138. const char *filename = section->get_file_name ();
  139. if (to_underlying (header->abbrev_sect_off)
  140. >= abbrev_section->get_size (per_objfile->objfile))
  141. error (_("Dwarf Error: bad offset (%s) in compilation unit header "
  142. "(offset %s + 6) [in module %s]"),
  143. sect_offset_str (header->abbrev_sect_off),
  144. sect_offset_str (header->sect_off),
  145. filename);
  146. /* Cast to ULONGEST to use 64-bit arithmetic when possible to
  147. avoid potential 32-bit overflow. */
  148. if (((ULONGEST) header->sect_off + header->get_length ())
  149. > section->size)
  150. error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
  151. "(offset %s + 0) [in module %s]"),
  152. header->length, sect_offset_str (header->sect_off),
  153. filename);
  154. }
  155. /* See comp-unit-head.h. */
  156. const gdb_byte *
  157. read_and_check_comp_unit_head (dwarf2_per_objfile *per_objfile,
  158. struct comp_unit_head *header,
  159. struct dwarf2_section_info *section,
  160. struct dwarf2_section_info *abbrev_section,
  161. const gdb_byte *info_ptr,
  162. rcuh_kind section_kind)
  163. {
  164. const gdb_byte *beg_of_comp_unit = info_ptr;
  165. header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
  166. info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
  167. header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
  168. error_check_comp_unit_head (per_objfile, header, section, abbrev_section);
  169. return info_ptr;
  170. }
  171. CORE_ADDR
  172. comp_unit_head::read_address (bfd *abfd, const gdb_byte *buf,
  173. unsigned int *bytes_read) const
  174. {
  175. CORE_ADDR retval = 0;
  176. if (signed_addr_p)
  177. {
  178. switch (addr_size)
  179. {
  180. case 2:
  181. retval = bfd_get_signed_16 (abfd, buf);
  182. break;
  183. case 4:
  184. retval = bfd_get_signed_32 (abfd, buf);
  185. break;
  186. case 8:
  187. retval = bfd_get_signed_64 (abfd, buf);
  188. break;
  189. default:
  190. internal_error (__FILE__, __LINE__,
  191. _("read_address: bad switch, signed [in module %s]"),
  192. bfd_get_filename (abfd));
  193. }
  194. }
  195. else
  196. {
  197. switch (addr_size)
  198. {
  199. case 2:
  200. retval = bfd_get_16 (abfd, buf);
  201. break;
  202. case 4:
  203. retval = bfd_get_32 (abfd, buf);
  204. break;
  205. case 8:
  206. retval = bfd_get_64 (abfd, buf);
  207. break;
  208. default:
  209. internal_error (__FILE__, __LINE__,
  210. _("read_address: bad switch, "
  211. "unsigned [in module %s]"),
  212. bfd_get_filename (abfd));
  213. }
  214. }
  215. *bytes_read = addr_size;
  216. return retval;
  217. }