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 #include "defs.h" 287d62b00eSchristos #include "dwarf2/attribute.h" 297d62b00eSchristos #include "dwarf2/stringify.h" 307d62b00eSchristos #include "complaints.h" 317d62b00eSchristos 327d62b00eSchristos /* See attribute.h. */ 337d62b00eSchristos 347d62b00eSchristos CORE_ADDR 35*6881a400Schristos attribute::as_address () const 367d62b00eSchristos { 377d62b00eSchristos CORE_ADDR addr; 387d62b00eSchristos 39*6881a400Schristos gdb_assert (!requires_reprocessing); 40*6881a400Schristos 417d62b00eSchristos if (form != DW_FORM_addr && form != DW_FORM_addrx 427d62b00eSchristos && form != DW_FORM_GNU_addr_index) 437d62b00eSchristos { 447d62b00eSchristos /* Aside from a few clearly defined exceptions, attributes that 457d62b00eSchristos contain an address must always be in DW_FORM_addr form. 467d62b00eSchristos Unfortunately, some compilers happen to be violating this 477d62b00eSchristos requirement by encoding addresses using other forms, such 487d62b00eSchristos as DW_FORM_data4 for example. For those broken compilers, 497d62b00eSchristos we try to do our best, without any guarantee of success, 507d62b00eSchristos to interpret the address correctly. It would also be nice 517d62b00eSchristos to generate a complaint, but that would require us to maintain 527d62b00eSchristos a list of legitimate cases where a non-address form is allowed, 537d62b00eSchristos as well as update callers to pass in at least the CU's DWARF 547d62b00eSchristos version. This is more overhead than what we're willing to 557d62b00eSchristos expand for a pretty rare case. */ 56*6881a400Schristos addr = u.unsnd; 577d62b00eSchristos } 587d62b00eSchristos else 59*6881a400Schristos addr = u.addr; 607d62b00eSchristos 617d62b00eSchristos return addr; 627d62b00eSchristos } 637d62b00eSchristos 647d62b00eSchristos /* See attribute.h. */ 657d62b00eSchristos 66*6881a400Schristos bool 67*6881a400Schristos attribute::form_is_string () const 687d62b00eSchristos { 69*6881a400Schristos return (form == DW_FORM_strp || form == DW_FORM_line_strp 707d62b00eSchristos || form == DW_FORM_string 717d62b00eSchristos || form == DW_FORM_strx 727d62b00eSchristos || form == DW_FORM_strx1 737d62b00eSchristos || form == DW_FORM_strx2 747d62b00eSchristos || form == DW_FORM_strx3 757d62b00eSchristos || form == DW_FORM_strx4 767d62b00eSchristos || form == DW_FORM_GNU_str_index 77*6881a400Schristos || form == DW_FORM_GNU_strp_alt); 78*6881a400Schristos } 79*6881a400Schristos 80*6881a400Schristos /* See attribute.h. */ 81*6881a400Schristos 82*6881a400Schristos const char * 83*6881a400Schristos attribute::as_string () const 84*6881a400Schristos { 85*6881a400Schristos gdb_assert (!requires_reprocessing); 86*6881a400Schristos if (form_is_string ()) 87*6881a400Schristos return u.str; 887d62b00eSchristos return nullptr; 897d62b00eSchristos } 907d62b00eSchristos 917d62b00eSchristos /* See attribute.h. */ 927d62b00eSchristos 937d62b00eSchristos bool 947d62b00eSchristos attribute::form_is_block () const 957d62b00eSchristos { 967d62b00eSchristos return (form == DW_FORM_block1 977d62b00eSchristos || form == DW_FORM_block2 987d62b00eSchristos || form == DW_FORM_block4 997d62b00eSchristos || form == DW_FORM_block 100*6881a400Schristos || form == DW_FORM_exprloc 101*6881a400Schristos || form == DW_FORM_data16); 1027d62b00eSchristos } 1037d62b00eSchristos 1047d62b00eSchristos /* See attribute.h. */ 1057d62b00eSchristos 1067d62b00eSchristos bool 1077d62b00eSchristos attribute::form_is_section_offset () const 1087d62b00eSchristos { 1097d62b00eSchristos return (form == DW_FORM_data4 1107d62b00eSchristos || form == DW_FORM_data8 1117d62b00eSchristos || form == DW_FORM_sec_offset 1127d62b00eSchristos || form == DW_FORM_loclistx); 1137d62b00eSchristos } 1147d62b00eSchristos 1157d62b00eSchristos /* See attribute.h. */ 1167d62b00eSchristos 1177d62b00eSchristos bool 1187d62b00eSchristos attribute::form_is_constant () const 1197d62b00eSchristos { 1207d62b00eSchristos switch (form) 1217d62b00eSchristos { 1227d62b00eSchristos case DW_FORM_sdata: 1237d62b00eSchristos case DW_FORM_udata: 1247d62b00eSchristos case DW_FORM_data1: 1257d62b00eSchristos case DW_FORM_data2: 1267d62b00eSchristos case DW_FORM_data4: 1277d62b00eSchristos case DW_FORM_data8: 1287d62b00eSchristos case DW_FORM_implicit_const: 1297d62b00eSchristos return true; 1307d62b00eSchristos default: 1317d62b00eSchristos return false; 1327d62b00eSchristos } 1337d62b00eSchristos } 1347d62b00eSchristos 1357d62b00eSchristos /* See attribute.h. */ 1367d62b00eSchristos 1377d62b00eSchristos void 1387d62b00eSchristos attribute::get_ref_die_offset_complaint () const 1397d62b00eSchristos { 1407d62b00eSchristos complaint (_("unsupported die ref attribute form: '%s'"), 1417d62b00eSchristos dwarf_form_name (form)); 1427d62b00eSchristos } 1437d62b00eSchristos 1447d62b00eSchristos /* See attribute.h. */ 1457d62b00eSchristos 1467d62b00eSchristos LONGEST 1477d62b00eSchristos attribute::constant_value (int default_value) const 1487d62b00eSchristos { 1497d62b00eSchristos if (form == DW_FORM_sdata || form == DW_FORM_implicit_const) 150*6881a400Schristos return u.snd; 1517d62b00eSchristos else if (form == DW_FORM_udata 1527d62b00eSchristos || form == DW_FORM_data1 1537d62b00eSchristos || form == DW_FORM_data2 1547d62b00eSchristos || form == DW_FORM_data4 1557d62b00eSchristos || form == DW_FORM_data8) 156*6881a400Schristos return u.unsnd; 1577d62b00eSchristos else 1587d62b00eSchristos { 1597d62b00eSchristos /* For DW_FORM_data16 see attribute::form_is_constant. */ 1607d62b00eSchristos complaint (_("Attribute value is not a constant (%s)"), 1617d62b00eSchristos dwarf_form_name (form)); 1627d62b00eSchristos return default_value; 1637d62b00eSchristos } 1647d62b00eSchristos } 165*6881a400Schristos 166*6881a400Schristos /* See attribute.h. */ 167*6881a400Schristos 168*6881a400Schristos bool 169*6881a400Schristos attribute::form_is_unsigned () const 170*6881a400Schristos { 171*6881a400Schristos return (form == DW_FORM_ref_addr 172*6881a400Schristos || form == DW_FORM_GNU_ref_alt 173*6881a400Schristos || form == DW_FORM_data2 174*6881a400Schristos || form == DW_FORM_data4 175*6881a400Schristos || form == DW_FORM_data8 176*6881a400Schristos || form == DW_FORM_sec_offset 177*6881a400Schristos || form == DW_FORM_data1 178*6881a400Schristos || form == DW_FORM_flag 179*6881a400Schristos || form == DW_FORM_flag_present 180*6881a400Schristos || form == DW_FORM_udata 181*6881a400Schristos || form == DW_FORM_rnglistx 182*6881a400Schristos || form == DW_FORM_loclistx 183*6881a400Schristos || form == DW_FORM_ref1 184*6881a400Schristos || form == DW_FORM_ref2 185*6881a400Schristos || form == DW_FORM_ref4 186*6881a400Schristos || form == DW_FORM_ref8 187*6881a400Schristos || form == DW_FORM_ref_udata); 188*6881a400Schristos } 189*6881a400Schristos 190*6881a400Schristos /* See attribute.h. */ 191*6881a400Schristos 192*6881a400Schristos bool 193*6881a400Schristos attribute::form_is_signed () const 194*6881a400Schristos { 195*6881a400Schristos return form == DW_FORM_sdata || form == DW_FORM_implicit_const; 196*6881a400Schristos } 197*6881a400Schristos 198*6881a400Schristos /* See attribute.h. */ 199*6881a400Schristos 200*6881a400Schristos bool 201*6881a400Schristos attribute::form_requires_reprocessing () const 202*6881a400Schristos { 203*6881a400Schristos return (form == DW_FORM_strx 204*6881a400Schristos || form == DW_FORM_strx1 205*6881a400Schristos || form == DW_FORM_strx2 206*6881a400Schristos || form == DW_FORM_strx3 207*6881a400Schristos || form == DW_FORM_strx4 208*6881a400Schristos || form == DW_FORM_GNU_str_index 209*6881a400Schristos || form == DW_FORM_addrx 210*6881a400Schristos || form == DW_FORM_GNU_addr_index 211*6881a400Schristos || form == DW_FORM_rnglistx 212*6881a400Schristos || form == DW_FORM_loclistx); 213*6881a400Schristos } 214*6881a400Schristos 215*6881a400Schristos /* See attribute.h. */ 216*6881a400Schristos 217*6881a400Schristos dwarf_defaulted_attribute 218*6881a400Schristos attribute::defaulted () const 219*6881a400Schristos { 220*6881a400Schristos LONGEST value = constant_value (-1); 221*6881a400Schristos 222*6881a400Schristos switch (value) 223*6881a400Schristos { 224*6881a400Schristos case DW_DEFAULTED_no: 225*6881a400Schristos case DW_DEFAULTED_in_class: 226*6881a400Schristos case DW_DEFAULTED_out_of_class: 227*6881a400Schristos return (dwarf_defaulted_attribute) value; 228*6881a400Schristos } 229*6881a400Schristos 230*6881a400Schristos /* If the form was not constant, we already complained in 231*6881a400Schristos constant_value, so there's no need to complain again. */ 232*6881a400Schristos if (form_is_constant ()) 233*6881a400Schristos complaint (_("unrecognized DW_AT_defaulted value (%s)"), 234*6881a400Schristos plongest (value)); 235*6881a400Schristos return DW_DEFAULTED_no; 236*6881a400Schristos } 237*6881a400Schristos 238*6881a400Schristos /* See attribute.h. */ 239*6881a400Schristos 240*6881a400Schristos dwarf_virtuality_attribute 241*6881a400Schristos attribute::as_virtuality () const 242*6881a400Schristos { 243*6881a400Schristos LONGEST value = constant_value (-1); 244*6881a400Schristos 245*6881a400Schristos switch (value) 246*6881a400Schristos { 247*6881a400Schristos case DW_VIRTUALITY_none: 248*6881a400Schristos case DW_VIRTUALITY_virtual: 249*6881a400Schristos case DW_VIRTUALITY_pure_virtual: 250*6881a400Schristos return (dwarf_virtuality_attribute) value; 251*6881a400Schristos } 252*6881a400Schristos 253*6881a400Schristos /* If the form was not constant, we already complained in 254*6881a400Schristos constant_value, so there's no need to complain again. */ 255*6881a400Schristos if (form_is_constant ()) 256*6881a400Schristos complaint (_("unrecognized DW_AT_virtuality value (%s)"), 257*6881a400Schristos plongest (value)); 258*6881a400Schristos return DW_VIRTUALITY_none; 259*6881a400Schristos } 260*6881a400Schristos 261*6881a400Schristos /* See attribute.h. */ 262*6881a400Schristos 263*6881a400Schristos bool 264*6881a400Schristos attribute::as_boolean () const 265*6881a400Schristos { 266*6881a400Schristos if (form == DW_FORM_flag_present) 267*6881a400Schristos return true; 268*6881a400Schristos else if (form == DW_FORM_flag) 269*6881a400Schristos return u.unsnd != 0; 270*6881a400Schristos return constant_value (0) != 0; 271*6881a400Schristos } 272