aboutsummaryrefslogtreecommitdiff
path: root/gdb/dwarf2/comp-unit-head.h
blob: 65a16f4ef1458e8054c664efa2735ad3d1b0a0ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Low-level DWARF 2 reading code

   Copyright (C) 1994-2023 Free Software Foundation, Inc.

   Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
   Inc.  with support from Florida State University (under contract
   with the Ada Joint Program Office), and Silicon Graphics, Inc.
   Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
   based on Fred Fish's (Cygnus Support) implementation of DWARF 1
   support.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program 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 for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef GDB_DWARF2_COMP_UNIT_H
#define GDB_DWARF2_COMP_UNIT_H

#include "dwarf2.h"
#include "dwarf2/leb.h"
#include "dwarf2/types.h"

struct dwarf2_per_objfile;

/* The data in a compilation unit header, after target2host
   translation, looks like this.  */
struct comp_unit_head
{
private:
  unsigned int m_length = 0;
public:
  unsigned char version = 0;
  unsigned char addr_size = 0;
  unsigned char signed_addr_p = 0;
  sect_offset abbrev_sect_off {};

  /* Size of file offsets; either 4 or 8.  */
  unsigned int offset_size = 0;

  /* Size of the length field; either 4 or 12.  */
  unsigned int initial_length_size = 0;

  enum dwarf_unit_type unit_type {};

  /* Offset to first die in this cu from the start of the cu.
     This will be the first byte following the compilation unit header.  */
  cu_offset first_die_cu_offset {};

  /* Offset to the first byte of this compilation unit header in the
     .debug_info section, for resolving relative reference dies.  */
  sect_offset sect_off {};

  /* For types, offset in the type's DIE of the type defined by this TU.  */
  cu_offset type_cu_offset_in_tu {};

  /* 64-bit signature of this unit. For type units, it denotes the signature of
     the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
     Also used in DWARF 5, to denote the dwo id when the unit type is
     DW_UT_skeleton or DW_UT_split_compile.  */
  ULONGEST signature = 0;

  void set_length (unsigned int length)
  {
    m_length = length;
  }

  /* Return the total length of the CU described by this header, including the
     initial length field.  */
  unsigned int get_length_with_initial () const
  {
    return m_length + initial_length_size;
  }

  /* Return the total length of the CU described by this header, excluding the
     initial length field.  */
  unsigned int get_length_without_initial () const
  {
    return m_length;
  }

  /* Return TRUE if OFF is within this CU.  */
  bool offset_in_cu_p (sect_offset off) const
  {
    sect_offset bottom = sect_off;
    sect_offset top = sect_off + get_length_with_initial ();
    return off >= bottom && off < top;
  }

  /* Read an offset from the data stream.  The size of the offset is
     given by cu_header->offset_size.  */
  LONGEST read_offset (bfd *abfd, const gdb_byte *buf,
		       unsigned int *bytes_read) const
  {
    LONGEST offset = ::read_offset (abfd, buf, offset_size);
    *bytes_read = offset_size;
    return offset;
  }

  /* Read an address from BUF.  BYTES_READ is updated.  */
  CORE_ADDR read_address (bfd *abfd, const gdb_byte *buf,
			  unsigned int *bytes_read) const;
};

/* Expected enum dwarf_unit_type for read_comp_unit_head.  */
enum class rcuh_kind { COMPILE, TYPE };

/* Read in the comp unit header information from the debug_info at info_ptr.
   Use rcuh_kind::COMPILE as the default type if not known by the caller.
   NOTE: This leaves members offset, first_die_offset to be filled in
   by the caller.  */
extern const gdb_byte *read_comp_unit_head
  (struct comp_unit_head *cu_header,
   const gdb_byte *info_ptr,
   struct dwarf2_section_info *section,
   rcuh_kind section_kind);

/* Read in a CU/TU header and perform some basic error checking.
   The contents of the header are stored in HEADER.
   The result is a pointer to the start of the first DIE.  */
extern const gdb_byte *read_and_check_comp_unit_head
  (dwarf2_per_objfile *per_objfile,
   struct comp_unit_head *header,
   struct dwarf2_section_info *section,
   struct dwarf2_section_info *abbrev_section,
   const gdb_byte *info_ptr,
   rcuh_kind section_kind);

#endif /* GDB_DWARF2_COMP_UNIT_H */