xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/dwarf2/attribute.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* DWARF attributes
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 1994-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
67d62b00eSchristos    Inc.  with support from Florida State University (under contract
77d62b00eSchristos    with the Ada Joint Program Office), and Silicon Graphics, Inc.
87d62b00eSchristos    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
97d62b00eSchristos    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
107d62b00eSchristos    support.
117d62b00eSchristos 
127d62b00eSchristos    This file is part of GDB.
137d62b00eSchristos 
147d62b00eSchristos    This program is free software; you can redistribute it and/or modify
157d62b00eSchristos    it under the terms of the GNU General Public License as published by
167d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
177d62b00eSchristos    (at your option) any later version.
187d62b00eSchristos 
197d62b00eSchristos    This program is distributed in the hope that it will be useful,
207d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
217d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
227d62b00eSchristos    GNU General Public License for more details.
237d62b00eSchristos 
247d62b00eSchristos    You should have received a copy of the GNU General Public License
257d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
267d62b00eSchristos 
277d62b00eSchristos #ifndef GDB_DWARF2_ATTRIBUTE_H
287d62b00eSchristos #define GDB_DWARF2_ATTRIBUTE_H
297d62b00eSchristos 
307d62b00eSchristos #include "dwarf2.h"
317d62b00eSchristos #include "gdbtypes.h"
32*6881a400Schristos #include "gdbsupport/gdb_optional.h"
337d62b00eSchristos 
347d62b00eSchristos /* Blocks are a bunch of untyped bytes.  */
357d62b00eSchristos struct dwarf_block
367d62b00eSchristos {
377d62b00eSchristos   size_t size;
387d62b00eSchristos 
397d62b00eSchristos   /* Valid only if SIZE is not zero.  */
407d62b00eSchristos   const gdb_byte *data;
417d62b00eSchristos };
427d62b00eSchristos 
437d62b00eSchristos /* Attributes have a name and a value.  */
447d62b00eSchristos struct attribute
457d62b00eSchristos {
467d62b00eSchristos   /* Read the given attribute value as an address, taking the
477d62b00eSchristos      attribute's form into account.  */
48*6881a400Schristos   CORE_ADDR as_address () const;
497d62b00eSchristos 
507d62b00eSchristos   /* If the attribute has a string form, return the string value;
517d62b00eSchristos      otherwise return NULL.  */
52*6881a400Schristos   const char *as_string () const;
53*6881a400Schristos 
54*6881a400Schristos   /* Return the block value.  The attribute must have block form.  */
55*6881a400Schristos   dwarf_block *as_block () const
56*6881a400Schristos   {
57*6881a400Schristos     gdb_assert (form_is_block ());
58*6881a400Schristos     return u.blk;
59*6881a400Schristos   }
60*6881a400Schristos 
61*6881a400Schristos   /* Return the signature.  The attribute must have signature
62*6881a400Schristos      form.  */
63*6881a400Schristos   ULONGEST as_signature () const
64*6881a400Schristos   {
65*6881a400Schristos     gdb_assert (form == DW_FORM_ref_sig8);
66*6881a400Schristos     return u.signature;
67*6881a400Schristos   }
68*6881a400Schristos 
69*6881a400Schristos   /* Return the signed value.  The attribute must have the appropriate
70*6881a400Schristos      form.  */
71*6881a400Schristos   LONGEST as_signed () const
72*6881a400Schristos   {
73*6881a400Schristos     gdb_assert (form_is_signed ());
74*6881a400Schristos     return u.snd;
75*6881a400Schristos   }
76*6881a400Schristos 
77*6881a400Schristos   /* Return the unsigned value, but only for attributes requiring
78*6881a400Schristos      reprocessing.  */
79*6881a400Schristos   ULONGEST as_unsigned_reprocess () const
80*6881a400Schristos   {
81*6881a400Schristos     gdb_assert (form_requires_reprocessing ());
82*6881a400Schristos     gdb_assert (requires_reprocessing);
83*6881a400Schristos     return u.unsnd;
84*6881a400Schristos   }
85*6881a400Schristos 
86*6881a400Schristos   /* Return the unsigned value.  Requires that the form be an unsigned
87*6881a400Schristos      form, and that reprocessing not be needed.  */
88*6881a400Schristos   ULONGEST as_unsigned () const
89*6881a400Schristos   {
90*6881a400Schristos     gdb_assert (form_is_unsigned ());
91*6881a400Schristos     gdb_assert (!requires_reprocessing);
92*6881a400Schristos     return u.unsnd;
93*6881a400Schristos   }
94*6881a400Schristos 
95*6881a400Schristos   /* Return true if the value is nonnegative.  Requires that that
96*6881a400Schristos      reprocessing not be needed.  */
97*6881a400Schristos   bool is_nonnegative () const
98*6881a400Schristos   {
99*6881a400Schristos     if (form_is_unsigned ())
100*6881a400Schristos       return true;
101*6881a400Schristos     if (form_is_signed ())
102*6881a400Schristos       return as_signed () >= 0;
103*6881a400Schristos     return false;
104*6881a400Schristos   }
105*6881a400Schristos 
106*6881a400Schristos   /* Return the nonnegative value.  Requires that that reprocessing not be
107*6881a400Schristos      needed.  */
108*6881a400Schristos   ULONGEST as_nonnegative () const
109*6881a400Schristos   {
110*6881a400Schristos     if (form_is_unsigned ())
111*6881a400Schristos       return as_unsigned ();
112*6881a400Schristos     if (form_is_signed ())
113*6881a400Schristos       return (ULONGEST)as_signed ();
114*6881a400Schristos     gdb_assert (false);
115*6881a400Schristos   }
1167d62b00eSchristos 
1177d62b00eSchristos   /* Return non-zero if ATTR's value is a section offset --- classes
1187d62b00eSchristos      lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
119*6881a400Schristos      You may use the as_unsigned method to retrieve such offsets.
1207d62b00eSchristos 
1217d62b00eSchristos      Section 7.5.4, "Attribute Encodings", explains that no attribute
1227d62b00eSchristos      may have a value that belongs to more than one of these classes; it
1237d62b00eSchristos      would be ambiguous if we did, because we use the same forms for all
1247d62b00eSchristos      of them.  */
1257d62b00eSchristos 
1267d62b00eSchristos   bool form_is_section_offset () const;
1277d62b00eSchristos 
1287d62b00eSchristos   /* Return non-zero if ATTR's value falls in the 'constant' class, or
1297d62b00eSchristos      zero otherwise.  When this function returns true, you can apply
1307d62b00eSchristos      the constant_value method to it.
1317d62b00eSchristos 
1327d62b00eSchristos      However, note that for some attributes you must check
1337d62b00eSchristos      attr_form_is_section_offset before using this test.  DW_FORM_data4
1347d62b00eSchristos      and DW_FORM_data8 are members of both the constant class, and of
1357d62b00eSchristos      the classes that contain offsets into other debug sections
1367d62b00eSchristos      (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
1377d62b00eSchristos      that, if an attribute's can be either a constant or one of the
1387d62b00eSchristos      section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
1397d62b00eSchristos      taken as section offsets, not constants.
1407d62b00eSchristos 
1417d62b00eSchristos      DW_FORM_data16 is not considered as constant_value cannot handle
1427d62b00eSchristos      that.  */
1437d62b00eSchristos 
1447d62b00eSchristos   bool form_is_constant () const;
1457d62b00eSchristos 
146*6881a400Schristos   /* The address is always stored already as sect_offset; despite for
147*6881a400Schristos      the forms besides DW_FORM_ref_addr it is stored as cu_offset in
148*6881a400Schristos      the DWARF file.  */
1497d62b00eSchristos 
1507d62b00eSchristos   bool form_is_ref () const
1517d62b00eSchristos   {
1527d62b00eSchristos     return (form == DW_FORM_ref_addr
1537d62b00eSchristos 	    || form == DW_FORM_ref1
1547d62b00eSchristos 	    || form == DW_FORM_ref2
1557d62b00eSchristos 	    || form == DW_FORM_ref4
1567d62b00eSchristos 	    || form == DW_FORM_ref8
1577d62b00eSchristos 	    || form == DW_FORM_ref_udata
1587d62b00eSchristos 	    || form == DW_FORM_GNU_ref_alt);
1597d62b00eSchristos   }
1607d62b00eSchristos 
1617d62b00eSchristos   /* Check if the attribute's form is a DW_FORM_block*
1627d62b00eSchristos      if so return true else false.  */
1637d62b00eSchristos 
1647d62b00eSchristos   bool form_is_block () const;
1657d62b00eSchristos 
166*6881a400Schristos   /* Check if the attribute's form is a string form.  */
167*6881a400Schristos   bool form_is_string () const;
168*6881a400Schristos 
169*6881a400Schristos   /* Check if the attribute's form is an unsigned integer form.  */
170*6881a400Schristos   bool form_is_unsigned () const;
171*6881a400Schristos 
172*6881a400Schristos   /* Check if the attribute's form is a signed integer form.  */
173*6881a400Schristos   bool form_is_signed () const;
174*6881a400Schristos 
175*6881a400Schristos   /* Check if the attribute's form is a form that requires
176*6881a400Schristos      "reprocessing".  */
177*6881a400Schristos   bool form_requires_reprocessing () const;
178*6881a400Schristos 
1797d62b00eSchristos   /* Return DIE offset of this attribute.  Return 0 with complaint if
1807d62b00eSchristos      the attribute is not of the required kind.  */
1817d62b00eSchristos 
1827d62b00eSchristos   sect_offset get_ref_die_offset () const
1837d62b00eSchristos   {
1847d62b00eSchristos     if (form_is_ref ())
1857d62b00eSchristos       return (sect_offset) u.unsnd;
1867d62b00eSchristos     get_ref_die_offset_complaint ();
1877d62b00eSchristos     return {};
1887d62b00eSchristos   }
1897d62b00eSchristos 
1907d62b00eSchristos   /* Return the constant value held by this attribute.  Return
1917d62b00eSchristos      DEFAULT_VALUE if the value held by the attribute is not
1927d62b00eSchristos      constant.  */
1937d62b00eSchristos 
1947d62b00eSchristos   LONGEST constant_value (int default_value) const;
1957d62b00eSchristos 
196*6881a400Schristos   /* Return true if this attribute holds a canonical string.  In some
197*6881a400Schristos      cases, like C++ names, gdb will rewrite the name of a DIE to a
198*6881a400Schristos      canonical form.  This makes lookups robust when a name can be
199*6881a400Schristos      spelled different ways (e.g., "signed" or "signed int").  This
200*6881a400Schristos      flag indicates whether the value has been canonicalized.  */
201*6881a400Schristos   bool canonical_string_p () const
202*6881a400Schristos   {
203*6881a400Schristos     gdb_assert (form_is_string ());
204*6881a400Schristos     return string_is_canonical;
205*6881a400Schristos   }
2067d62b00eSchristos 
207*6881a400Schristos   /* Initialize this attribute to hold a non-canonical string
208*6881a400Schristos      value.  */
209*6881a400Schristos   void set_string_noncanonical (const char *str)
210*6881a400Schristos   {
211*6881a400Schristos     gdb_assert (form_is_string ());
212*6881a400Schristos     u.str = str;
213*6881a400Schristos     string_is_canonical = 0;
214*6881a400Schristos     requires_reprocessing = 0;
215*6881a400Schristos   }
216*6881a400Schristos 
217*6881a400Schristos   /* Set the canonical string value for this attribute.  */
218*6881a400Schristos   void set_string_canonical (const char *str)
219*6881a400Schristos   {
220*6881a400Schristos     gdb_assert (form_is_string ());
221*6881a400Schristos     u.str = str;
222*6881a400Schristos     string_is_canonical = 1;
223*6881a400Schristos   }
224*6881a400Schristos 
225*6881a400Schristos   /* Set the block value for this attribute.  */
226*6881a400Schristos   void set_block (dwarf_block *blk)
227*6881a400Schristos   {
228*6881a400Schristos     gdb_assert (form_is_block ());
229*6881a400Schristos     u.blk = blk;
230*6881a400Schristos   }
231*6881a400Schristos 
232*6881a400Schristos   /* Set the signature value for this attribute.  */
233*6881a400Schristos   void set_signature (ULONGEST signature)
234*6881a400Schristos   {
235*6881a400Schristos     gdb_assert (form == DW_FORM_ref_sig8);
236*6881a400Schristos     u.signature = signature;
237*6881a400Schristos   }
238*6881a400Schristos 
239*6881a400Schristos   /* Set this attribute to a signed integer.  */
240*6881a400Schristos   void set_signed (LONGEST snd)
241*6881a400Schristos   {
242*6881a400Schristos     gdb_assert (form == DW_FORM_sdata || form == DW_FORM_implicit_const);
243*6881a400Schristos     u.snd = snd;
244*6881a400Schristos   }
245*6881a400Schristos 
246*6881a400Schristos   /* Set this attribute to an unsigned integer.  */
247*6881a400Schristos   void set_unsigned (ULONGEST unsnd)
248*6881a400Schristos   {
249*6881a400Schristos     gdb_assert (form_is_unsigned ());
250*6881a400Schristos     u.unsnd = unsnd;
251*6881a400Schristos     requires_reprocessing = 0;
252*6881a400Schristos   }
253*6881a400Schristos 
254*6881a400Schristos   /* Temporarily set this attribute to an unsigned integer.  This is
255*6881a400Schristos      used only for those forms that require reprocessing.  */
256*6881a400Schristos   void set_unsigned_reprocess (ULONGEST unsnd)
257*6881a400Schristos   {
258*6881a400Schristos     gdb_assert (form_requires_reprocessing ());
259*6881a400Schristos     u.unsnd = unsnd;
260*6881a400Schristos     requires_reprocessing = 1;
261*6881a400Schristos   }
262*6881a400Schristos 
263*6881a400Schristos   /* Set this attribute to an address.  */
264*6881a400Schristos   void set_address (CORE_ADDR addr)
265*6881a400Schristos   {
266*6881a400Schristos     gdb_assert (form == DW_FORM_addr
267*6881a400Schristos 		|| ((form == DW_FORM_addrx
268*6881a400Schristos 		     || form == DW_FORM_GNU_addr_index)
269*6881a400Schristos 		    && requires_reprocessing));
270*6881a400Schristos     u.addr = addr;
271*6881a400Schristos     requires_reprocessing = 0;
272*6881a400Schristos   }
273*6881a400Schristos 
274*6881a400Schristos   /* True if this attribute requires reprocessing.  */
275*6881a400Schristos   bool requires_reprocessing_p () const
276*6881a400Schristos   {
277*6881a400Schristos     return requires_reprocessing;
278*6881a400Schristos   }
279*6881a400Schristos 
280*6881a400Schristos   /* Return the value as one of the recognized enum
281*6881a400Schristos      dwarf_defaulted_attribute constants according to DWARF5 spec,
282*6881a400Schristos      Table 7.24.  If the value is incorrect, or if this attribute has
283*6881a400Schristos      the wrong form, then a complaint is issued and DW_DEFAULTED_no is
284*6881a400Schristos      returned.  */
285*6881a400Schristos   dwarf_defaulted_attribute defaulted () const;
286*6881a400Schristos 
287*6881a400Schristos   /* Return the attribute's value as a dwarf_virtuality_attribute
288*6881a400Schristos      constant according to DWARF spec.  An unrecognized value will
289*6881a400Schristos      issue a complaint and return DW_VIRTUALITY_none.  */
290*6881a400Schristos   dwarf_virtuality_attribute as_virtuality () const;
291*6881a400Schristos 
292*6881a400Schristos   /* Return the attribute's value as a boolean.  An unrecognized form
293*6881a400Schristos      will issue a complaint and return false.  */
294*6881a400Schristos   bool as_boolean () const;
295*6881a400Schristos 
296*6881a400Schristos   ENUM_BITFIELD(dwarf_attribute) name : 15;
297*6881a400Schristos 
298*6881a400Schristos   /* A boolean that is used for forms that require reprocessing.  A
299*6881a400Schristos      form may require data not directly available in the attribute.
300*6881a400Schristos      E.g., DW_FORM_strx requires the corresponding
301*6881a400Schristos      DW_AT_str_offsets_base.  In this case, the processing for the
302*6881a400Schristos      attribute must be done in two passes.  In the first past, this
303*6881a400Schristos      flag is set and the value is an unsigned.  In the second pass,
304*6881a400Schristos      the unsigned value is turned into the correct value for the form,
305*6881a400Schristos      and this flag is cleared.  This flag is unused for other
306*6881a400Schristos      forms.  */
307*6881a400Schristos   unsigned int requires_reprocessing : 1;
308*6881a400Schristos 
3097d62b00eSchristos   ENUM_BITFIELD(dwarf_form) form : 15;
3107d62b00eSchristos 
311*6881a400Schristos   /* Has u.str already been updated by dwarf2_canonicalize_name?  This
312*6881a400Schristos      field should be in u.str but it is kept here for better struct
313*6881a400Schristos      attribute alignment.  */
3147d62b00eSchristos   unsigned int string_is_canonical : 1;
3157d62b00eSchristos 
3167d62b00eSchristos   union
3177d62b00eSchristos     {
3187d62b00eSchristos       const char *str;
3197d62b00eSchristos       struct dwarf_block *blk;
3207d62b00eSchristos       ULONGEST unsnd;
3217d62b00eSchristos       LONGEST snd;
3227d62b00eSchristos       CORE_ADDR addr;
3237d62b00eSchristos       ULONGEST signature;
3247d62b00eSchristos     }
3257d62b00eSchristos   u;
3267d62b00eSchristos 
3277d62b00eSchristos private:
3287d62b00eSchristos 
3297d62b00eSchristos   /* Used by get_ref_die_offset to issue a complaint.  */
3307d62b00eSchristos 
3317d62b00eSchristos   void get_ref_die_offset_complaint () const;
3327d62b00eSchristos };
3337d62b00eSchristos 
3347d62b00eSchristos #endif /* GDB_DWARF2_ATTRIBUTE_H */
335