1/* Low-level DWARF 2 reading code
2
3   Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5   Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6   Inc.  with support from Florida State University (under contract
7   with the Ada Joint Program Office), and Silicon Graphics, Inc.
8   Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9   based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10   support.
11
12   This file is part of GDB.
13
14   This program is free software; you can redistribute it and/or modify
15   it under the terms of the GNU General Public License as published by
16   the Free Software Foundation; either version 3 of the License, or
17   (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful,
20   but WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   GNU General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
26
27#ifndef GDB_DWARF2_COMP_UNIT_H
28#define GDB_DWARF2_COMP_UNIT_H
29
30#include "dwarf2/leb.h"
31#include "gdbtypes.h"
32
33/* The data in a compilation unit header, after target2host
34   translation, looks like this.  */
35struct comp_unit_head
36{
37  unsigned int length;
38  unsigned char version;
39  unsigned char addr_size;
40  unsigned char signed_addr_p;
41  sect_offset abbrev_sect_off;
42
43  /* Size of file offsets; either 4 or 8.  */
44  unsigned int offset_size;
45
46  /* Size of the length field; either 4 or 12.  */
47  unsigned int initial_length_size;
48
49  enum dwarf_unit_type unit_type;
50
51  /* Offset to first die in this cu from the start of the cu.
52     This will be the first byte following the compilation unit header.  */
53  cu_offset first_die_cu_offset;
54
55  /* Offset to the first byte of this compilation unit header in the
56     .debug_info section, for resolving relative reference dies.  */
57  sect_offset sect_off;
58
59  /* For types, offset in the type's DIE of the type defined by this TU.  */
60  cu_offset type_cu_offset_in_tu;
61
62  /* 64-bit signature of this unit. For type units, it denotes the signature of
63     the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
64     Also used in DWARF 5, to denote the dwo id when the unit type is
65     DW_UT_skeleton or DW_UT_split_compile.  */
66  ULONGEST signature;
67
68  /* Return the total length of the CU described by this header.  */
69  unsigned int get_length () const
70  {
71    return initial_length_size + length;
72  }
73
74  /* Return TRUE if OFF is within this CU.  */
75  bool offset_in_cu_p (sect_offset off) const
76  {
77    sect_offset bottom = sect_off;
78    sect_offset top = sect_off + get_length ();
79    return off >= bottom && off < top;
80  }
81
82  /* Read an offset from the data stream.  The size of the offset is
83     given by cu_header->offset_size.  */
84  LONGEST read_offset (bfd *abfd, const gdb_byte *buf,
85		       unsigned int *bytes_read) const
86  {
87    LONGEST offset = ::read_offset (abfd, buf, offset_size);
88    *bytes_read = offset_size;
89    return offset;
90  }
91
92  /* Read an address from BUF.  BYTES_READ is updated.  */
93  CORE_ADDR read_address (bfd *abfd, const gdb_byte *buf,
94			  unsigned int *bytes_read) const;
95};
96
97/* Expected enum dwarf_unit_type for read_comp_unit_head.  */
98enum class rcuh_kind { COMPILE, TYPE };
99
100/* Read in the comp unit header information from the debug_info at info_ptr.
101   Use rcuh_kind::COMPILE as the default type if not known by the caller.
102   NOTE: This leaves members offset, first_die_offset to be filled in
103   by the caller.  */
104extern const gdb_byte *read_comp_unit_head
105  (struct comp_unit_head *cu_header,
106   const gdb_byte *info_ptr,
107   struct dwarf2_section_info *section,
108   rcuh_kind section_kind);
109
110/* Read in a CU/TU header and perform some basic error checking.
111   The contents of the header are stored in HEADER.
112   The result is a pointer to the start of the first DIE.  */
113extern const gdb_byte *read_and_check_comp_unit_head
114  (dwarf2_per_objfile *per_objfile,
115   struct comp_unit_head *header,
116   struct dwarf2_section_info *section,
117   struct dwarf2_section_info *abbrev_section,
118   const gdb_byte *info_ptr,
119   rcuh_kind section_kind);
120
121#endif /* GDB_DWARF2_COMP_UNIT_H */
122