1b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Return sibling of given DIE. 243e924ee0bb01576cb30a7069ad9183e55b1093bMark Wielaard Copyright (C) 2003-2010, 2014, 2015 Red Hat, Inc. 3de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard This file is part of elfutils. 4b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper Written by Ulrich Drepper <drepper@redhat.com>, 2003. 5b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 6de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard This file is free software; you can redistribute it and/or modify 7de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard it under the terms of either 8b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 9de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard * the GNU Lesser General Public License as published by the Free 10de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard Software Foundation; either version 3 of the License, or (at 11de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard your option) any later version 12de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard 13de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard or 14de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard 15de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard * the GNU General Public License as published by the Free 16de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard Software Foundation; either version 2 of the License, or (at 17de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard your option) any later version 18de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard 19de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard or both in parallel, as here. 20de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard 21de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard elfutils is distributed in the hope that it will be useful, but 22361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper WITHOUT ANY WARRANTY; without even the implied warranty of 23361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper General Public License for more details. 25361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper 26de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard You should have received copies of the GNU General Public License and 27de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard the GNU Lesser General Public License along with this program. If 28de2ed97f33139af5c7a0811e4ec66fc896a13cf2Mark Wielaard not, see <http://www.gnu.org/licenses/>. */ 29b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 30b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#ifdef HAVE_CONFIG_H 31b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper# include <config.h> 32b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#endif 33b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 34b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include "libdwP.h" 35b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <dwarf.h> 36b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <string.h> 37b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 38c911d5c907edcaf80792be1abac0262439fd9082Ulrich Drepper 39b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperint 401ccdfb683ad6c7e59793136c3a657ddf131cafd1Mark Wielaarddwarf_siblingof (Dwarf_Die *die, Dwarf_Die *result) 41b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{ 42b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Ignore previous errors. */ 43b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper if (die == NULL) 44b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper return -1; 45b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 462d982861e5e23d38653df7d8dce1d2282cda8ce1Chih-Hung Hsieh /* result is declared NN */ 4735f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper 4835f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper if (result != die) 4935f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper result->addr = NULL; 5035f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper 51b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper unsigned int level = 0; 52b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 53b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Copy of the current DIE. */ 54b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper Dwarf_Die this_die = *die; 55b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Temporary attributes we create. */ 56b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper Dwarf_Attribute sibattr; 57b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Copy of the CU in the request. */ 58b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper sibattr.cu = this_die.cu; 59b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* That's the address we start looking. */ 60b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper unsigned char *addr = this_die.addr; 61b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* End of the buffer. */ 628d200b5b728b7ee6150796e4baa07b54e1ac8cd2Josh Stone unsigned char *endp = sibattr.cu->endp; 63b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 64b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Search for the beginning of the next die on this level. We 65b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper must not return the dies for children of the given die. */ 66b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper do 67b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper { 68b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Find the end of the DIE or the sibling attribute. */ 69b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper addr = __libdw_find_attr (&this_die, DW_AT_sibling, &sibattr.code, 70b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper &sibattr.form); 7159480ba86fdaad7eb2e104b69af4a3b07e20d6fbMark Wielaard if (addr != NULL && sibattr.code == DW_AT_sibling) 72b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper { 73b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper Dwarf_Off offset; 74b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper sibattr.valp = addr; 758d358d95cc4b7389dec1962f6062af8e90ab93d9Ulrich Drepper if (unlikely (__libdw_formref (&sibattr, &offset) != 0)) 76b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Something went wrong. */ 77b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper return -1; 78b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 7943e924ee0bb01576cb30a7069ad9183e55b1093bMark Wielaard /* The sibling attribute should point after this DIE in the CU. 8043e924ee0bb01576cb30a7069ad9183e55b1093bMark Wielaard But not after the end of the CU. */ 813fa1796a550bade1e59f05eba9ae1008773cb645Mark Wielaard size_t size = sibattr.cu->endp - sibattr.cu->startp; 8243e924ee0bb01576cb30a7069ad9183e55b1093bMark Wielaard size_t die_off = this_die.addr - this_die.cu->startp; 8343e924ee0bb01576cb30a7069ad9183e55b1093bMark Wielaard if (unlikely (offset >= size || offset <= die_off)) 843fa1796a550bade1e59f05eba9ae1008773cb645Mark Wielaard { 853fa1796a550bade1e59f05eba9ae1008773cb645Mark Wielaard __libdw_seterrno (DWARF_E_INVALID_DWARF); 863fa1796a550bade1e59f05eba9ae1008773cb645Mark Wielaard return -1; 873fa1796a550bade1e59f05eba9ae1008773cb645Mark Wielaard } 883fa1796a550bade1e59f05eba9ae1008773cb645Mark Wielaard 89b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Compute the next address. */ 908d200b5b728b7ee6150796e4baa07b54e1ac8cd2Josh Stone addr = sibattr.cu->startp + offset; 91b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper } 92b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper else if (unlikely (addr == NULL) 9335f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper || unlikely (this_die.abbrev == DWARF_END_ABBREV)) 94b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper return -1; 95b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper else if (this_die.abbrev->has_children) 96b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* This abbreviation has children. */ 97b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper ++level; 98b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 99c911d5c907edcaf80792be1abac0262439fd9082Ulrich Drepper 100b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper while (1) 101b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper { 102b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Make sure we are still in range. Some producers might skip 103b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper the trailing NUL bytes. */ 104b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper if (addr >= endp) 105b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper return 1; 106b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 107b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper if (*addr != '\0') 108b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper break; 109b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 110b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper if (level-- == 0) 11135f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper { 11235f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper if (result != die) 11335f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper result->addr = addr; 11435f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper /* No more sibling at all. */ 11535f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper return 1; 11635f08c4d52d0ffd9f8aa50f47b84de5603842b1fUlrich Drepper } 117b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 118b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper ++addr; 119b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper } 120b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 121b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Initialize the 'current DIE'. */ 122b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper this_die.addr = addr; 123b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper this_die.abbrev = NULL; 124b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper } 125b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper while (level > 0); 126b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 127b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Maybe we reached the end of the CU. */ 128b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper if (addr >= endp) 129b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper return 1; 130b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 131b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Clear the entire DIE structure. This signals we have not yet 132b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper determined any of the information. */ 133b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper memset (result, '\0', sizeof (Dwarf_Die)); 134b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 135b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* We have the address. */ 136b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper result->addr = addr; 137b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 138b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper /* Same CU as the parent. */ 139b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper result->cu = sibattr.cu; 140b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper 141b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper return 0; 142b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper} 143b08d5a8fb42f4586d756068065186b5af7e48daUlrich DrepperINTDEF(dwarf_siblingof) 144