| 1 | /* DWARF attributes |
| 2 | |
| 3 | Copyright (C) 1994-2020 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 | #include "defs.h" |
| 28 | #include "dwarf2/attribute.h" |
| 29 | #include "dwarf2/stringify.h" |
| 30 | #include "complaints.h" |
| 31 | |
| 32 | /* See attribute.h. */ |
| 33 | |
| 34 | CORE_ADDR |
| 35 | attribute::value_as_address () const |
| 36 | { |
| 37 | CORE_ADDR addr; |
| 38 | |
| 39 | if (form != DW_FORM_addr && form != DW_FORM_addrx |
| 40 | && form != DW_FORM_GNU_addr_index) |
| 41 | { |
| 42 | /* Aside from a few clearly defined exceptions, attributes that |
| 43 | contain an address must always be in DW_FORM_addr form. |
| 44 | Unfortunately, some compilers happen to be violating this |
| 45 | requirement by encoding addresses using other forms, such |
| 46 | as DW_FORM_data4 for example. For those broken compilers, |
| 47 | we try to do our best, without any guarantee of success, |
| 48 | to interpret the address correctly. It would also be nice |
| 49 | to generate a complaint, but that would require us to maintain |
| 50 | a list of legitimate cases where a non-address form is allowed, |
| 51 | as well as update callers to pass in at least the CU's DWARF |
| 52 | version. This is more overhead than what we're willing to |
| 53 | expand for a pretty rare case. */ |
| 54 | addr = DW_UNSND (this); |
| 55 | } |
| 56 | else |
| 57 | addr = DW_ADDR (this); |
| 58 | |
| 59 | return addr; |
| 60 | } |
| 61 | |
| 62 | /* See attribute.h. */ |
| 63 | |
| 64 | const char * |
| 65 | attribute::value_as_string () const |
| 66 | { |
| 67 | if (form == DW_FORM_strp || form == DW_FORM_line_strp |
| 68 | || form == DW_FORM_string |
| 69 | || form == DW_FORM_strx |
| 70 | || form == DW_FORM_strx1 |
| 71 | || form == DW_FORM_strx2 |
| 72 | || form == DW_FORM_strx3 |
| 73 | || form == DW_FORM_strx4 |
| 74 | || form == DW_FORM_GNU_str_index |
| 75 | || form == DW_FORM_GNU_strp_alt) |
| 76 | return DW_STRING (this); |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | /* See attribute.h. */ |
| 81 | |
| 82 | bool |
| 83 | attribute::form_is_block () const |
| 84 | { |
| 85 | return (form == DW_FORM_block1 |
| 86 | || form == DW_FORM_block2 |
| 87 | || form == DW_FORM_block4 |
| 88 | || form == DW_FORM_block |
| 89 | || form == DW_FORM_exprloc); |
| 90 | } |
| 91 | |
| 92 | /* See attribute.h. */ |
| 93 | |
| 94 | bool |
| 95 | attribute::form_is_section_offset () const |
| 96 | { |
| 97 | return (form == DW_FORM_data4 |
| 98 | || form == DW_FORM_data8 |
| 99 | || form == DW_FORM_sec_offset |
| 100 | || form == DW_FORM_loclistx); |
| 101 | } |
| 102 | |
| 103 | /* See attribute.h. */ |
| 104 | |
| 105 | bool |
| 106 | attribute::form_is_constant () const |
| 107 | { |
| 108 | switch (form) |
| 109 | { |
| 110 | case DW_FORM_sdata: |
| 111 | case DW_FORM_udata: |
| 112 | case DW_FORM_data1: |
| 113 | case DW_FORM_data2: |
| 114 | case DW_FORM_data4: |
| 115 | case DW_FORM_data8: |
| 116 | case DW_FORM_implicit_const: |
| 117 | return true; |
| 118 | default: |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* DW_ADDR is always stored already as sect_offset; despite for the forms |
| 124 | besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */ |
| 125 | |
| 126 | bool |
| 127 | attribute::form_is_ref () const |
| 128 | { |
| 129 | switch (form) |
| 130 | { |
| 131 | case DW_FORM_ref_addr: |
| 132 | case DW_FORM_ref1: |
| 133 | case DW_FORM_ref2: |
| 134 | case DW_FORM_ref4: |
| 135 | case DW_FORM_ref8: |
| 136 | case DW_FORM_ref_udata: |
| 137 | case DW_FORM_GNU_ref_alt: |
| 138 | return true; |
| 139 | default: |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* See attribute.h. */ |
| 145 | |
| 146 | sect_offset |
| 147 | attribute::get_ref_die_offset () const |
| 148 | { |
| 149 | if (form_is_ref ()) |
| 150 | return (sect_offset) DW_UNSND (this); |
| 151 | |
| 152 | complaint (_("unsupported die ref attribute form: '%s'"), |
| 153 | dwarf_form_name (form)); |
| 154 | return {}; |
| 155 | } |
| 156 | |
| 157 | /* See attribute.h. */ |
| 158 | |
| 159 | LONGEST |
| 160 | attribute::constant_value (int default_value) const |
| 161 | { |
| 162 | if (form == DW_FORM_sdata || form == DW_FORM_implicit_const) |
| 163 | return DW_SND (this); |
| 164 | else if (form == DW_FORM_udata |
| 165 | || form == DW_FORM_data1 |
| 166 | || form == DW_FORM_data2 |
| 167 | || form == DW_FORM_data4 |
| 168 | || form == DW_FORM_data8) |
| 169 | return DW_UNSND (this); |
| 170 | else |
| 171 | { |
| 172 | /* For DW_FORM_data16 see attribute::form_is_constant. */ |
| 173 | complaint (_("Attribute value is not a constant (%s)"), |
| 174 | dwarf_form_name (form)); |
| 175 | return default_value; |
| 176 | } |
| 177 | } |