xref: /aosp_15_r20/external/elfutils/libdw/libdw_visit_scopes.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Helper functions to descend DWARF scope trees.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2005,2006,2007,2015 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker 
5*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker    it under the terms of either
7*7304104dSAndroid Build Coastguard Worker 
8*7304104dSAndroid Build Coastguard Worker      * the GNU Lesser General Public License as published by the Free
9*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 3 of the License, or (at
10*7304104dSAndroid Build Coastguard Worker        your option) any later version
11*7304104dSAndroid Build Coastguard Worker 
12*7304104dSAndroid Build Coastguard Worker    or
13*7304104dSAndroid Build Coastguard Worker 
14*7304104dSAndroid Build Coastguard Worker      * the GNU General Public License as published by the Free
15*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 2 of the License, or (at
16*7304104dSAndroid Build Coastguard Worker        your option) any later version
17*7304104dSAndroid Build Coastguard Worker 
18*7304104dSAndroid Build Coastguard Worker    or both in parallel, as here.
19*7304104dSAndroid Build Coastguard Worker 
20*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
21*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
22*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23*7304104dSAndroid Build Coastguard Worker    General Public License for more details.
24*7304104dSAndroid Build Coastguard Worker 
25*7304104dSAndroid Build Coastguard Worker    You should have received copies of the GNU General Public License and
26*7304104dSAndroid Build Coastguard Worker    the GNU Lesser General Public License along with this program.  If
27*7304104dSAndroid Build Coastguard Worker    not, see <http://www.gnu.org/licenses/>.  */
28*7304104dSAndroid Build Coastguard Worker 
29*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
30*7304104dSAndroid Build Coastguard Worker # include <config.h>
31*7304104dSAndroid Build Coastguard Worker #endif
32*7304104dSAndroid Build Coastguard Worker 
33*7304104dSAndroid Build Coastguard Worker #include "libdwP.h"
34*7304104dSAndroid Build Coastguard Worker #include <dwarf.h>
35*7304104dSAndroid Build Coastguard Worker 
36*7304104dSAndroid Build Coastguard Worker 
37*7304104dSAndroid Build Coastguard Worker static bool
may_have_scopes(Dwarf_Die * die)38*7304104dSAndroid Build Coastguard Worker may_have_scopes (Dwarf_Die *die)
39*7304104dSAndroid Build Coastguard Worker {
40*7304104dSAndroid Build Coastguard Worker   switch (INTUSE(dwarf_tag) (die))
41*7304104dSAndroid Build Coastguard Worker     {
42*7304104dSAndroid Build Coastguard Worker       /* DIEs with addresses we can try to match.  */
43*7304104dSAndroid Build Coastguard Worker     case DW_TAG_compile_unit:
44*7304104dSAndroid Build Coastguard Worker     case DW_TAG_module:
45*7304104dSAndroid Build Coastguard Worker     case DW_TAG_lexical_block:
46*7304104dSAndroid Build Coastguard Worker     case DW_TAG_with_stmt:
47*7304104dSAndroid Build Coastguard Worker     case DW_TAG_catch_block:
48*7304104dSAndroid Build Coastguard Worker     case DW_TAG_try_block:
49*7304104dSAndroid Build Coastguard Worker     case DW_TAG_entry_point:
50*7304104dSAndroid Build Coastguard Worker     case DW_TAG_inlined_subroutine:
51*7304104dSAndroid Build Coastguard Worker     case DW_TAG_subprogram:
52*7304104dSAndroid Build Coastguard Worker       return true;
53*7304104dSAndroid Build Coastguard Worker 
54*7304104dSAndroid Build Coastguard Worker       /* DIEs without addresses that can own DIEs with addresses.  */
55*7304104dSAndroid Build Coastguard Worker     case DW_TAG_namespace:
56*7304104dSAndroid Build Coastguard Worker     case DW_TAG_class_type:
57*7304104dSAndroid Build Coastguard Worker     case DW_TAG_structure_type:
58*7304104dSAndroid Build Coastguard Worker       return true;
59*7304104dSAndroid Build Coastguard Worker 
60*7304104dSAndroid Build Coastguard Worker       /* Other DIEs we have no reason to descend.  */
61*7304104dSAndroid Build Coastguard Worker     default:
62*7304104dSAndroid Build Coastguard Worker       break;
63*7304104dSAndroid Build Coastguard Worker     }
64*7304104dSAndroid Build Coastguard Worker   return false;
65*7304104dSAndroid Build Coastguard Worker }
66*7304104dSAndroid Build Coastguard Worker 
67*7304104dSAndroid Build Coastguard Worker struct walk_children_state
68*7304104dSAndroid Build Coastguard Worker {
69*7304104dSAndroid Build Coastguard Worker   /* Parameters of __libdw_visit_scopes. */
70*7304104dSAndroid Build Coastguard Worker   unsigned int depth;
71*7304104dSAndroid Build Coastguard Worker   struct Dwarf_Die_Chain *imports;
72*7304104dSAndroid Build Coastguard Worker   int (*previsit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
73*7304104dSAndroid Build Coastguard Worker   int (*postvisit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
74*7304104dSAndroid Build Coastguard Worker   void *arg;
75*7304104dSAndroid Build Coastguard Worker   /* Extra local variables for the walker. */
76*7304104dSAndroid Build Coastguard Worker   struct Dwarf_Die_Chain child;
77*7304104dSAndroid Build Coastguard Worker };
78*7304104dSAndroid Build Coastguard Worker 
79*7304104dSAndroid Build Coastguard Worker static inline int
80*7304104dSAndroid Build Coastguard Worker walk_children (struct walk_children_state *state);
81*7304104dSAndroid Build Coastguard Worker 
82*7304104dSAndroid Build Coastguard Worker int
83*7304104dSAndroid Build Coastguard Worker internal_function
__libdw_visit_scopes(unsigned int depth,struct Dwarf_Die_Chain * root,struct Dwarf_Die_Chain * imports,int (* previsit)(unsigned int,struct Dwarf_Die_Chain *,void *),int (* postvisit)(unsigned int,struct Dwarf_Die_Chain *,void *),void * arg)84*7304104dSAndroid Build Coastguard Worker __libdw_visit_scopes (unsigned int depth, struct Dwarf_Die_Chain *root,
85*7304104dSAndroid Build Coastguard Worker 		      struct Dwarf_Die_Chain *imports,
86*7304104dSAndroid Build Coastguard Worker 		      int (*previsit) (unsigned int,
87*7304104dSAndroid Build Coastguard Worker 				       struct Dwarf_Die_Chain *,
88*7304104dSAndroid Build Coastguard Worker 				       void *),
89*7304104dSAndroid Build Coastguard Worker 		      int (*postvisit) (unsigned int,
90*7304104dSAndroid Build Coastguard Worker 					struct Dwarf_Die_Chain *,
91*7304104dSAndroid Build Coastguard Worker 					void *),
92*7304104dSAndroid Build Coastguard Worker 		      void *arg)
93*7304104dSAndroid Build Coastguard Worker {
94*7304104dSAndroid Build Coastguard Worker   struct walk_children_state state =
95*7304104dSAndroid Build Coastguard Worker     {
96*7304104dSAndroid Build Coastguard Worker       .depth = depth,
97*7304104dSAndroid Build Coastguard Worker       .imports = imports,
98*7304104dSAndroid Build Coastguard Worker       .previsit = previsit,
99*7304104dSAndroid Build Coastguard Worker       .postvisit = postvisit,
100*7304104dSAndroid Build Coastguard Worker       .arg = arg
101*7304104dSAndroid Build Coastguard Worker     };
102*7304104dSAndroid Build Coastguard Worker 
103*7304104dSAndroid Build Coastguard Worker   state.child.parent = root;
104*7304104dSAndroid Build Coastguard Worker   int ret;
105*7304104dSAndroid Build Coastguard Worker   if ((ret = INTUSE(dwarf_child) (&root->die, &state.child.die)) != 0)
106*7304104dSAndroid Build Coastguard Worker     return ret < 0 ? -1 : 0; // Having zero children is legal.
107*7304104dSAndroid Build Coastguard Worker 
108*7304104dSAndroid Build Coastguard Worker   return walk_children (&state);
109*7304104dSAndroid Build Coastguard Worker }
110*7304104dSAndroid Build Coastguard Worker 
111*7304104dSAndroid Build Coastguard Worker static inline int
walk_children(struct walk_children_state * state)112*7304104dSAndroid Build Coastguard Worker walk_children (struct walk_children_state *state)
113*7304104dSAndroid Build Coastguard Worker {
114*7304104dSAndroid Build Coastguard Worker   int ret;
115*7304104dSAndroid Build Coastguard Worker   do
116*7304104dSAndroid Build Coastguard Worker     {
117*7304104dSAndroid Build Coastguard Worker       /* For an imported unit, it is logically as if the children of
118*7304104dSAndroid Build Coastguard Worker 	 that unit are siblings of the other children.  So don't do
119*7304104dSAndroid Build Coastguard Worker 	 a full recursion into the imported unit, but just walk the
120*7304104dSAndroid Build Coastguard Worker 	 children in place before moving to the next real child.  */
121*7304104dSAndroid Build Coastguard Worker       while (INTUSE(dwarf_tag) (&state->child.die) == DW_TAG_imported_unit)
122*7304104dSAndroid Build Coastguard Worker 	{
123*7304104dSAndroid Build Coastguard Worker 	  Dwarf_Die orig_child_die = state->child.die;
124*7304104dSAndroid Build Coastguard Worker 	  Dwarf_Attribute attr_mem;
125*7304104dSAndroid Build Coastguard Worker 	  Dwarf_Attribute *attr = INTUSE(dwarf_attr) (&state->child.die,
126*7304104dSAndroid Build Coastguard Worker 						      DW_AT_import,
127*7304104dSAndroid Build Coastguard Worker 						      &attr_mem);
128*7304104dSAndroid Build Coastguard Worker 	  /* Some gcc -flto versions imported other top-level compile units,
129*7304104dSAndroid Build Coastguard Worker 	     skip those.  */
130*7304104dSAndroid Build Coastguard Worker 	  if (INTUSE(dwarf_formref_die) (attr, &state->child.die) != NULL
131*7304104dSAndroid Build Coastguard Worker 	      && INTUSE(dwarf_tag) (&state->child.die) != DW_TAG_compile_unit
132*7304104dSAndroid Build Coastguard Worker 	      && (INTUSE(dwarf_child) (&state->child.die, &state->child.die)
133*7304104dSAndroid Build Coastguard Worker 		  == 0))
134*7304104dSAndroid Build Coastguard Worker 	    {
135*7304104dSAndroid Build Coastguard Worker 	      /* Checks the given DIE hasn't been imported yet
136*7304104dSAndroid Build Coastguard Worker 	         to prevent cycles.  */
137*7304104dSAndroid Build Coastguard Worker 	      bool imported = false;
138*7304104dSAndroid Build Coastguard Worker 	      for (struct Dwarf_Die_Chain *import = state->imports; import != NULL;
139*7304104dSAndroid Build Coastguard Worker 	        import = import->parent)
140*7304104dSAndroid Build Coastguard Worker 	        if (import->die.addr == orig_child_die.addr)
141*7304104dSAndroid Build Coastguard Worker 	          {
142*7304104dSAndroid Build Coastguard Worker 	            imported = true;
143*7304104dSAndroid Build Coastguard Worker 	            break;
144*7304104dSAndroid Build Coastguard Worker 	          }
145*7304104dSAndroid Build Coastguard Worker 	      if (imported)
146*7304104dSAndroid Build Coastguard Worker 		{
147*7304104dSAndroid Build Coastguard Worker 		  __libdw_seterrno (DWARF_E_INVALID_DWARF);
148*7304104dSAndroid Build Coastguard Worker 		  return -1;
149*7304104dSAndroid Build Coastguard Worker 		}
150*7304104dSAndroid Build Coastguard Worker 	      struct Dwarf_Die_Chain *orig_imports = state->imports;
151*7304104dSAndroid Build Coastguard Worker 	      struct Dwarf_Die_Chain import = { .die = orig_child_die,
152*7304104dSAndroid Build Coastguard Worker 					        .parent = orig_imports };
153*7304104dSAndroid Build Coastguard Worker 	      state->imports = &import;
154*7304104dSAndroid Build Coastguard Worker 	      int result = walk_children (state);
155*7304104dSAndroid Build Coastguard Worker 	      state->imports = orig_imports;
156*7304104dSAndroid Build Coastguard Worker 	      if (result != DWARF_CB_OK)
157*7304104dSAndroid Build Coastguard Worker 		return result;
158*7304104dSAndroid Build Coastguard Worker 	    }
159*7304104dSAndroid Build Coastguard Worker 
160*7304104dSAndroid Build Coastguard Worker 	  /* Any "real" children left?  */
161*7304104dSAndroid Build Coastguard Worker 	  if ((ret = INTUSE(dwarf_siblingof) (&orig_child_die,
162*7304104dSAndroid Build Coastguard Worker 					      &state->child.die)) != 0)
163*7304104dSAndroid Build Coastguard Worker 	    return ret < 0 ? -1 : 0;
164*7304104dSAndroid Build Coastguard Worker 	};
165*7304104dSAndroid Build Coastguard Worker 
166*7304104dSAndroid Build Coastguard Worker 	state->child.prune = false;
167*7304104dSAndroid Build Coastguard Worker 
168*7304104dSAndroid Build Coastguard Worker 	/* previsit is declared NN */
169*7304104dSAndroid Build Coastguard Worker 	int result = (*state->previsit) (state->depth + 1, &state->child, state->arg);
170*7304104dSAndroid Build Coastguard Worker 	if (result != DWARF_CB_OK)
171*7304104dSAndroid Build Coastguard Worker 	  return result;
172*7304104dSAndroid Build Coastguard Worker 
173*7304104dSAndroid Build Coastguard Worker 	if (!state->child.prune && may_have_scopes (&state->child.die)
174*7304104dSAndroid Build Coastguard Worker 	    && INTUSE(dwarf_haschildren) (&state->child.die))
175*7304104dSAndroid Build Coastguard Worker 	  {
176*7304104dSAndroid Build Coastguard Worker 	    result = __libdw_visit_scopes (state->depth + 1, &state->child, state->imports,
177*7304104dSAndroid Build Coastguard Worker 				           state->previsit, state->postvisit, state->arg);
178*7304104dSAndroid Build Coastguard Worker 	    if (result != DWARF_CB_OK)
179*7304104dSAndroid Build Coastguard Worker 	      return result;
180*7304104dSAndroid Build Coastguard Worker 	  }
181*7304104dSAndroid Build Coastguard Worker 
182*7304104dSAndroid Build Coastguard Worker 	if (state->postvisit != NULL)
183*7304104dSAndroid Build Coastguard Worker 	  {
184*7304104dSAndroid Build Coastguard Worker 	    result = (*state->postvisit) (state->depth + 1, &state->child, state->arg);
185*7304104dSAndroid Build Coastguard Worker 	    if (result != DWARF_CB_OK)
186*7304104dSAndroid Build Coastguard Worker 	      return result;
187*7304104dSAndroid Build Coastguard Worker 	  }
188*7304104dSAndroid Build Coastguard Worker     }
189*7304104dSAndroid Build Coastguard Worker   while ((ret = INTUSE(dwarf_siblingof) (&state->child.die, &state->child.die)) == 0);
190*7304104dSAndroid Build Coastguard Worker 
191*7304104dSAndroid Build Coastguard Worker   return ret < 0 ? -1 : 0;
192*7304104dSAndroid Build Coastguard Worker }
193