1da0c48c4Sopenharmony_ci/* Look up the DIE in a reference-form attribute. 2da0c48c4Sopenharmony_ci Copyright (C) 2005-2010, 2018 Red Hat, Inc. 3da0c48c4Sopenharmony_ci This file is part of elfutils. 4da0c48c4Sopenharmony_ci 5da0c48c4Sopenharmony_ci This file is free software; you can redistribute it and/or modify 6da0c48c4Sopenharmony_ci it under the terms of either 7da0c48c4Sopenharmony_ci 8da0c48c4Sopenharmony_ci * the GNU Lesser General Public License as published by the Free 9da0c48c4Sopenharmony_ci Software Foundation; either version 3 of the License, or (at 10da0c48c4Sopenharmony_ci your option) any later version 11da0c48c4Sopenharmony_ci 12da0c48c4Sopenharmony_ci or 13da0c48c4Sopenharmony_ci 14da0c48c4Sopenharmony_ci * the GNU General Public License as published by the Free 15da0c48c4Sopenharmony_ci Software Foundation; either version 2 of the License, or (at 16da0c48c4Sopenharmony_ci your option) any later version 17da0c48c4Sopenharmony_ci 18da0c48c4Sopenharmony_ci or both in parallel, as here. 19da0c48c4Sopenharmony_ci 20da0c48c4Sopenharmony_ci elfutils is distributed in the hope that it will be useful, but 21da0c48c4Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 22da0c48c4Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23da0c48c4Sopenharmony_ci General Public License for more details. 24da0c48c4Sopenharmony_ci 25da0c48c4Sopenharmony_ci You should have received copies of the GNU General Public License and 26da0c48c4Sopenharmony_ci the GNU Lesser General Public License along with this program. If 27da0c48c4Sopenharmony_ci not, see <http://www.gnu.org/licenses/>. */ 28da0c48c4Sopenharmony_ci 29da0c48c4Sopenharmony_ci#ifdef HAVE_CONFIG_H 30da0c48c4Sopenharmony_ci# include <config.h> 31da0c48c4Sopenharmony_ci#endif 32da0c48c4Sopenharmony_ci 33da0c48c4Sopenharmony_ci#include <string.h> 34da0c48c4Sopenharmony_ci#include "libdwP.h" 35da0c48c4Sopenharmony_ci#include <dwarf.h> 36da0c48c4Sopenharmony_ci 37da0c48c4Sopenharmony_ci 38da0c48c4Sopenharmony_ciDwarf_Die * 39da0c48c4Sopenharmony_cidwarf_formref_die (Dwarf_Attribute *attr, Dwarf_Die *result) 40da0c48c4Sopenharmony_ci{ 41da0c48c4Sopenharmony_ci if (attr == NULL) 42da0c48c4Sopenharmony_ci return NULL; 43da0c48c4Sopenharmony_ci 44da0c48c4Sopenharmony_ci struct Dwarf_CU *cu = attr->cu; 45da0c48c4Sopenharmony_ci 46da0c48c4Sopenharmony_ci Dwarf_Off offset; 47da0c48c4Sopenharmony_ci if (attr->form == DW_FORM_ref_addr || attr->form == DW_FORM_GNU_ref_alt 48da0c48c4Sopenharmony_ci || attr->form == DW_FORM_ref_sup4 || attr->form == DW_FORM_ref_sup8) 49da0c48c4Sopenharmony_ci { 50da0c48c4Sopenharmony_ci /* This has an absolute offset. */ 51da0c48c4Sopenharmony_ci 52da0c48c4Sopenharmony_ci uint8_t ref_size; 53da0c48c4Sopenharmony_ci if (cu->version == 2 && attr->form == DW_FORM_ref_addr) 54da0c48c4Sopenharmony_ci ref_size = cu->address_size; 55da0c48c4Sopenharmony_ci else if (attr->form == DW_FORM_ref_sup4) 56da0c48c4Sopenharmony_ci ref_size = 4; 57da0c48c4Sopenharmony_ci else if (attr->form == DW_FORM_ref_sup8) 58da0c48c4Sopenharmony_ci ref_size = 8; 59da0c48c4Sopenharmony_ci else 60da0c48c4Sopenharmony_ci ref_size = cu->offset_size; 61da0c48c4Sopenharmony_ci 62da0c48c4Sopenharmony_ci Dwarf *dbg_ret = (attr->form == DW_FORM_GNU_ref_alt 63da0c48c4Sopenharmony_ci ? INTUSE(dwarf_getalt) (cu->dbg) : cu->dbg); 64da0c48c4Sopenharmony_ci 65da0c48c4Sopenharmony_ci if (dbg_ret == NULL) 66da0c48c4Sopenharmony_ci { 67da0c48c4Sopenharmony_ci __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK); 68da0c48c4Sopenharmony_ci return NULL; 69da0c48c4Sopenharmony_ci } 70da0c48c4Sopenharmony_ci 71da0c48c4Sopenharmony_ci if (__libdw_read_offset (cu->dbg, dbg_ret, IDX_debug_info, attr->valp, 72da0c48c4Sopenharmony_ci ref_size, &offset, IDX_debug_info, 0)) 73da0c48c4Sopenharmony_ci return NULL; 74da0c48c4Sopenharmony_ci 75da0c48c4Sopenharmony_ci return INTUSE(dwarf_offdie) (dbg_ret, offset, result); 76da0c48c4Sopenharmony_ci } 77da0c48c4Sopenharmony_ci 78da0c48c4Sopenharmony_ci const unsigned char *datap; 79da0c48c4Sopenharmony_ci size_t size; 80da0c48c4Sopenharmony_ci if (attr->form == DW_FORM_ref_sig8) 81da0c48c4Sopenharmony_ci { 82da0c48c4Sopenharmony_ci /* This doesn't have an offset, but instead a value we 83da0c48c4Sopenharmony_ci have to match in the type unit headers. */ 84da0c48c4Sopenharmony_ci 85da0c48c4Sopenharmony_ci uint64_t sig = read_8ubyte_unaligned (cu->dbg, attr->valp); 86da0c48c4Sopenharmony_ci cu = Dwarf_Sig8_Hash_find (&cu->dbg->sig8_hash, sig); 87da0c48c4Sopenharmony_ci if (cu == NULL) 88da0c48c4Sopenharmony_ci { 89da0c48c4Sopenharmony_ci /* Not seen before. We have to scan through the type units. 90da0c48c4Sopenharmony_ci Since DWARFv5 these can (also) be found in .debug_info, 91da0c48c4Sopenharmony_ci so scan that first. */ 92da0c48c4Sopenharmony_ci bool scan_debug_types = false; 93da0c48c4Sopenharmony_ci do 94da0c48c4Sopenharmony_ci { 95da0c48c4Sopenharmony_ci cu = __libdw_intern_next_unit (attr->cu->dbg, scan_debug_types); 96da0c48c4Sopenharmony_ci if (cu == NULL) 97da0c48c4Sopenharmony_ci { 98da0c48c4Sopenharmony_ci if (scan_debug_types == false) 99da0c48c4Sopenharmony_ci scan_debug_types = true; 100da0c48c4Sopenharmony_ci else 101da0c48c4Sopenharmony_ci { 102da0c48c4Sopenharmony_ci __libdw_seterrno (INTUSE(dwarf_errno) () 103da0c48c4Sopenharmony_ci ?: DWARF_E_INVALID_REFERENCE); 104da0c48c4Sopenharmony_ci return NULL; 105da0c48c4Sopenharmony_ci } 106da0c48c4Sopenharmony_ci } 107da0c48c4Sopenharmony_ci } 108da0c48c4Sopenharmony_ci while (cu == NULL || cu->unit_id8 != sig); 109da0c48c4Sopenharmony_ci } 110da0c48c4Sopenharmony_ci 111da0c48c4Sopenharmony_ci int secid = cu_sec_idx (cu); 112da0c48c4Sopenharmony_ci datap = cu->dbg->sectiondata[secid]->d_buf; 113da0c48c4Sopenharmony_ci size = cu->dbg->sectiondata[secid]->d_size; 114da0c48c4Sopenharmony_ci offset = cu->start + cu->subdie_offset; 115da0c48c4Sopenharmony_ci } 116da0c48c4Sopenharmony_ci else 117da0c48c4Sopenharmony_ci { 118da0c48c4Sopenharmony_ci /* Other forms produce an offset from the CU. */ 119da0c48c4Sopenharmony_ci if (unlikely (__libdw_formref (attr, &offset) != 0)) 120da0c48c4Sopenharmony_ci return NULL; 121da0c48c4Sopenharmony_ci 122da0c48c4Sopenharmony_ci datap = cu->startp; 123da0c48c4Sopenharmony_ci size = cu->endp - cu->startp; 124da0c48c4Sopenharmony_ci } 125da0c48c4Sopenharmony_ci 126da0c48c4Sopenharmony_ci if (unlikely (offset >= size)) 127da0c48c4Sopenharmony_ci { 128da0c48c4Sopenharmony_ci __libdw_seterrno (DWARF_E_INVALID_DWARF); 129da0c48c4Sopenharmony_ci return NULL; 130da0c48c4Sopenharmony_ci } 131da0c48c4Sopenharmony_ci 132da0c48c4Sopenharmony_ci memset (result, '\0', sizeof (Dwarf_Die)); 133da0c48c4Sopenharmony_ci result->addr = (char *) datap + offset; 134da0c48c4Sopenharmony_ci result->cu = cu; 135da0c48c4Sopenharmony_ci return result; 136da0c48c4Sopenharmony_ci} 137da0c48c4Sopenharmony_ciINTDEF (dwarf_formref_die) 138