1*38fd1498Szrj /* Copyright (C) 2012-2018 Free Software Foundation, Inc.
2*38fd1498Szrj
3*38fd1498Szrj This file is part of GCC.
4*38fd1498Szrj
5*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it
6*38fd1498Szrj under the terms of the GNU General Public License as published by
7*38fd1498Szrj the Free Software Foundation; either version 3, or (at your option)
8*38fd1498Szrj any later version.
9*38fd1498Szrj
10*38fd1498Szrj GCC is distributed in the hope that it will be useful, but
11*38fd1498Szrj WITHOUT ANY WARRANTY; without even the implied warranty of
12*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*38fd1498Szrj General Public License for more details.
14*38fd1498Szrj
15*38fd1498Szrj You should have received a copy of the GNU General Public License
16*38fd1498Szrj along with GCC; see the file COPYING3. If not see
17*38fd1498Szrj <http://www.gnu.org/licenses/>. */
18*38fd1498Szrj
19*38fd1498Szrj /* Virtual Table Pointer Security Pass - Detect corruption of vtable pointers
20*38fd1498Szrj before using them for virtual method dispatches. */
21*38fd1498Szrj
22*38fd1498Szrj /* This file is part of the vtable security feature implementation.
23*38fd1498Szrj The vtable security feature is designed to detect when a virtual
24*38fd1498Szrj call is about to be made through an invalid vtable pointer
25*38fd1498Szrj (possibly due to data corruption or malicious attacks). The
26*38fd1498Szrj compiler finds every virtual call, and inserts a verification call
27*38fd1498Szrj before the virtual call. The verification call takes the actual
28*38fd1498Szrj vtable pointer value in the object through which the virtual call
29*38fd1498Szrj is being made, and compares the vtable pointer against a set of all
30*38fd1498Szrj valid vtable pointers that the object could contain (this set is
31*38fd1498Szrj based on the declared type of the object). If the pointer is in
32*38fd1498Szrj the valid set, execution is allowed to continue; otherwise the
33*38fd1498Szrj program is halted.
34*38fd1498Szrj
35*38fd1498Szrj There are several pieces needed in order to make this work: 1. For
36*38fd1498Szrj every virtual class in the program (i.e. a class that contains
37*38fd1498Szrj virtual methods), we need to build the set of all possible valid
38*38fd1498Szrj vtables that an object of that class could point to. This includes
39*38fd1498Szrj vtables for any class(es) that inherit from the class under
40*38fd1498Szrj consideration. 2. For every such data set we build up, we need a
41*38fd1498Szrj way to find and reference the data set. This is complicated by the
42*38fd1498Szrj fact that the real vtable addresses are not known until runtime,
43*38fd1498Szrj when the program is loaded into memory, but we need to reference the
44*38fd1498Szrj sets at compile time when we are inserting verification calls into
45*38fd1498Szrj the program. 3. We need to find every virtual call in the program,
46*38fd1498Szrj and insert the verification call (with the appropriate arguments)
47*38fd1498Szrj before the virtual call. 4. We need some runtime library pieces:
48*38fd1498Szrj the code to build up the data sets at runtime; the code to actually
49*38fd1498Szrj perform the verification using the data sets; and some code to set
50*38fd1498Szrj protections on the data sets, so they themselves do not become
51*38fd1498Szrj hacker targets.
52*38fd1498Szrj
53*38fd1498Szrj To find and reference the set of valid vtable pointers for any given
54*38fd1498Szrj virtual class, we create a special global varible for each virtual
55*38fd1498Szrj class. We refer to this as the "vtable map variable" for that
56*38fd1498Szrj class. The vtable map variable has the type "void *", and is
57*38fd1498Szrj initialized by the compiler to NULL. At runtime when the set of
58*38fd1498Szrj valid vtable pointers for a virtual class, e.g. class Foo, is built,
59*38fd1498Szrj the vtable map variable for class Foo is made to point to the set.
60*38fd1498Szrj During compile time, when the compiler is inserting verification
61*38fd1498Szrj calls into the program, it passes the vtable map variable for the
62*38fd1498Szrj appropriate class to the verification call, so that at runtime the
63*38fd1498Szrj verification call can find the appropriate data set.
64*38fd1498Szrj
65*38fd1498Szrj The actual set of valid vtable pointers for a virtual class,
66*38fd1498Szrj e.g. class Foo, cannot be built until runtime, when the vtables get
67*38fd1498Szrj loaded into memory and their addresses are known. But the knowledge
68*38fd1498Szrj about which vtables belong in which class' hierarchy is only known
69*38fd1498Szrj at compile time. Therefore at compile time we collect class
70*38fd1498Szrj hierarchy and vtable information about every virtual class, and we
71*38fd1498Szrj generate calls to build up the data sets at runtime. To build the
72*38fd1498Szrj data sets, we call one of the functions we add to the runtime
73*38fd1498Szrj library, __VLTRegisterPair. __VLTRegisterPair takes two arguments,
74*38fd1498Szrj a vtable map variable and the address of a vtable. If the vtable
75*38fd1498Szrj map variable is currently NULL, it creates a new data set (hash
76*38fd1498Szrj table), makes the vtable map variable point to the new data set, and
77*38fd1498Szrj inserts the vtable address into the data set. If the vtable map
78*38fd1498Szrj variable is not NULL, it just inserts the vtable address into the
79*38fd1498Szrj data set. In order to make sure that our data sets are built before
80*38fd1498Szrj any verification calls happen, we create a special constructor
81*38fd1498Szrj initialization function for each compilation unit, give it a very
82*38fd1498Szrj high initialization priority, and insert all of our calls to
83*38fd1498Szrj __VLTRegisterPair into our special constructor initialization
84*38fd1498Szrj function.
85*38fd1498Szrj
86*38fd1498Szrj The vtable verification feature is controlled by the flag
87*38fd1498Szrj '-fvtable-verify='. There are three flavors of this:
88*38fd1498Szrj '-fvtable-verify=std', '-fvtable-verify=preinit', and
89*38fd1498Szrj '-fvtable-verify=none'. If the option '-fvtable-verfy=preinit' is
90*38fd1498Szrj used, then our constructor initialization function gets put into the
91*38fd1498Szrj preinit array. This is necessary if there are data sets that need
92*38fd1498Szrj to be built very early in execution. If the constructor
93*38fd1498Szrj initialization function gets put into the preinit array, the we also
94*38fd1498Szrj add calls to __VLTChangePermission at the beginning and end of the
95*38fd1498Szrj function. The call at the beginning sets the permissions on the
96*38fd1498Szrj data sets and vtable map variables to read/write, and the one at the
97*38fd1498Szrj end makes them read-only. If the '-fvtable-verify=std' option is
98*38fd1498Szrj used, the constructor initialization functions are executed at their
99*38fd1498Szrj normal time, and the __VLTChangePermission calls are handled
100*38fd1498Szrj differently (see the comments in libstdc++-v3/libsupc++/vtv_rts.cc).
101*38fd1498Szrj The option '-fvtable-verify=none' turns off vtable verification.
102*38fd1498Szrj
103*38fd1498Szrj This file contains code to find and record the class hierarchies for
104*38fd1498Szrj the virtual classes in a program, and all the vtables associated
105*38fd1498Szrj with each such class; to generate the vtable map variables; and to
106*38fd1498Szrj generate the constructor initialization function (with the calls to
107*38fd1498Szrj __VLTRegisterPair, and __VLTChangePermission). The main data
108*38fd1498Szrj structures used for collecting the class hierarchy data and
109*38fd1498Szrj building/maintaining the vtable map variable data are defined in
110*38fd1498Szrj gcc/vtable-verify.h, because they are used both here and in
111*38fd1498Szrj gcc/vtable-verify.c. */
112*38fd1498Szrj
113*38fd1498Szrj #include "config.h"
114*38fd1498Szrj #include "system.h"
115*38fd1498Szrj #include "coretypes.h"
116*38fd1498Szrj #include "vtable-verify.h"
117*38fd1498Szrj #include "cp-tree.h"
118*38fd1498Szrj #include "stringpool.h"
119*38fd1498Szrj #include "cgraph.h"
120*38fd1498Szrj #include "output.h"
121*38fd1498Szrj #include "tree-iterator.h"
122*38fd1498Szrj #include "gimplify.h"
123*38fd1498Szrj #include "stor-layout.h"
124*38fd1498Szrj
125*38fd1498Szrj static int num_calls_to_regset = 0;
126*38fd1498Szrj static int num_calls_to_regpair = 0;
127*38fd1498Szrj static int current_set_size;
128*38fd1498Szrj
129*38fd1498Szrj /* Mark these specially since they need to be stored in precompiled
130*38fd1498Szrj header IR. */
131*38fd1498Szrj static GTY (()) vec<tree, va_gc> *vlt_saved_class_info;
132*38fd1498Szrj static GTY (()) tree vlt_register_pairs_fndecl = NULL_TREE;
133*38fd1498Szrj static GTY (()) tree vlt_register_set_fndecl = NULL_TREE;
134*38fd1498Szrj
135*38fd1498Szrj struct work_node {
136*38fd1498Szrj struct vtv_graph_node *node;
137*38fd1498Szrj struct work_node *next;
138*38fd1498Szrj };
139*38fd1498Szrj
140*38fd1498Szrj struct vtbl_map_node *vtable_find_or_create_map_decl (tree);
141*38fd1498Szrj
142*38fd1498Szrj /* As part of vtable verification the compiler generates and inserts
143*38fd1498Szrj calls to __VLTVerifyVtablePointer, which is in libstdc++. This
144*38fd1498Szrj function builds and initializes the function decl that is used
145*38fd1498Szrj in generating those function calls.
146*38fd1498Szrj
147*38fd1498Szrj In addition to __VLTVerifyVtablePointer there is also
148*38fd1498Szrj __VLTVerifyVtablePointerDebug which can be used in place of
149*38fd1498Szrj __VLTVerifyVtablePointer, and which takes extra parameters and
150*38fd1498Szrj outputs extra information, to help debug problems. The debug
151*38fd1498Szrj version of this function is generated and used if flag_vtv_debug is
152*38fd1498Szrj true.
153*38fd1498Szrj
154*38fd1498Szrj The signatures for these functions are:
155*38fd1498Szrj
156*38fd1498Szrj void * __VLTVerifyVtablePointer (void **, void*);
157*38fd1498Szrj void * __VLTVerifyVtablePointerDebug (void**, void *, char *, char *);
158*38fd1498Szrj */
159*38fd1498Szrj
160*38fd1498Szrj void
vtv_build_vtable_verify_fndecl(void)161*38fd1498Szrj vtv_build_vtable_verify_fndecl (void)
162*38fd1498Szrj {
163*38fd1498Szrj tree func_type = NULL_TREE;
164*38fd1498Szrj
165*38fd1498Szrj if (verify_vtbl_ptr_fndecl != NULL_TREE
166*38fd1498Szrj && TREE_CODE (verify_vtbl_ptr_fndecl) != ERROR_MARK)
167*38fd1498Szrj return;
168*38fd1498Szrj
169*38fd1498Szrj if (flag_vtv_debug)
170*38fd1498Szrj {
171*38fd1498Szrj func_type = build_function_type_list (const_ptr_type_node,
172*38fd1498Szrj build_pointer_type (ptr_type_node),
173*38fd1498Szrj const_ptr_type_node,
174*38fd1498Szrj const_string_type_node,
175*38fd1498Szrj const_string_type_node,
176*38fd1498Szrj NULL_TREE);
177*38fd1498Szrj verify_vtbl_ptr_fndecl =
178*38fd1498Szrj build_lang_decl (FUNCTION_DECL,
179*38fd1498Szrj get_identifier ("__VLTVerifyVtablePointerDebug"),
180*38fd1498Szrj func_type);
181*38fd1498Szrj }
182*38fd1498Szrj else
183*38fd1498Szrj {
184*38fd1498Szrj func_type = build_function_type_list (const_ptr_type_node,
185*38fd1498Szrj build_pointer_type (ptr_type_node),
186*38fd1498Szrj const_ptr_type_node,
187*38fd1498Szrj NULL_TREE);
188*38fd1498Szrj verify_vtbl_ptr_fndecl =
189*38fd1498Szrj build_lang_decl (FUNCTION_DECL,
190*38fd1498Szrj get_identifier ("__VLTVerifyVtablePointer"),
191*38fd1498Szrj func_type);
192*38fd1498Szrj }
193*38fd1498Szrj
194*38fd1498Szrj TREE_NOTHROW (verify_vtbl_ptr_fndecl) = 1;
195*38fd1498Szrj DECL_ATTRIBUTES (verify_vtbl_ptr_fndecl)
196*38fd1498Szrj = tree_cons (get_identifier ("leaf"), NULL,
197*38fd1498Szrj DECL_ATTRIBUTES (verify_vtbl_ptr_fndecl));
198*38fd1498Szrj DECL_PURE_P (verify_vtbl_ptr_fndecl) = 1;
199*38fd1498Szrj TREE_PUBLIC (verify_vtbl_ptr_fndecl) = 1;
200*38fd1498Szrj DECL_PRESERVE_P (verify_vtbl_ptr_fndecl) = 1;
201*38fd1498Szrj }
202*38fd1498Szrj
203*38fd1498Szrj /* As part of vtable verification the compiler generates and inserts
204*38fd1498Szrj calls to __VLTRegisterSet and __VLTRegisterPair, which are in
205*38fd1498Szrj libsupc++. This function builds and initializes the function decls
206*38fd1498Szrj that are used in generating those function calls.
207*38fd1498Szrj
208*38fd1498Szrj The signatures for these functions are:
209*38fd1498Szrj
210*38fd1498Szrj void __VLTRegisterSetDebug (void **, const void *, std::size_t,
211*38fd1498Szrj size_t, void **);
212*38fd1498Szrj
213*38fd1498Szrj void __VLTRegisterSet (void **, const void *, std::size_t,
214*38fd1498Szrj size_t, void **);
215*38fd1498Szrj
216*38fd1498Szrj void __VLTRegisterPairDebug (void **, const void *, size_t,
217*38fd1498Szrj const void *, const char *, const char *);
218*38fd1498Szrj
219*38fd1498Szrj void __VLTRegisterPair (void **, const void *, size_t, const void *);
220*38fd1498Szrj */
221*38fd1498Szrj
222*38fd1498Szrj static void
init_functions(void)223*38fd1498Szrj init_functions (void)
224*38fd1498Szrj {
225*38fd1498Szrj tree register_set_type;
226*38fd1498Szrj tree register_pairs_type;
227*38fd1498Szrj
228*38fd1498Szrj if (vlt_register_set_fndecl != NULL_TREE)
229*38fd1498Szrj return;
230*38fd1498Szrj
231*38fd1498Szrj gcc_assert (vlt_register_pairs_fndecl == NULL_TREE);
232*38fd1498Szrj gcc_assert (vlt_register_set_fndecl == NULL_TREE);
233*38fd1498Szrj
234*38fd1498Szrj /* Build function decl for __VLTRegisterSet*. */
235*38fd1498Szrj
236*38fd1498Szrj register_set_type = build_function_type_list
237*38fd1498Szrj (void_type_node,
238*38fd1498Szrj build_pointer_type (ptr_type_node),
239*38fd1498Szrj const_ptr_type_node,
240*38fd1498Szrj size_type_node,
241*38fd1498Szrj size_type_node,
242*38fd1498Szrj build_pointer_type (ptr_type_node),
243*38fd1498Szrj NULL_TREE);
244*38fd1498Szrj
245*38fd1498Szrj if (flag_vtv_debug)
246*38fd1498Szrj vlt_register_set_fndecl = build_lang_decl
247*38fd1498Szrj (FUNCTION_DECL,
248*38fd1498Szrj get_identifier ("__VLTRegisterSetDebug"),
249*38fd1498Szrj register_set_type);
250*38fd1498Szrj else
251*38fd1498Szrj vlt_register_set_fndecl = build_lang_decl
252*38fd1498Szrj (FUNCTION_DECL,
253*38fd1498Szrj get_identifier ("__VLTRegisterSet"),
254*38fd1498Szrj register_set_type);
255*38fd1498Szrj
256*38fd1498Szrj
257*38fd1498Szrj TREE_NOTHROW (vlt_register_set_fndecl) = 1;
258*38fd1498Szrj DECL_ATTRIBUTES (vlt_register_set_fndecl) =
259*38fd1498Szrj tree_cons (get_identifier ("leaf"), NULL,
260*38fd1498Szrj DECL_ATTRIBUTES (vlt_register_set_fndecl));
261*38fd1498Szrj DECL_EXTERNAL(vlt_register_set_fndecl) = 1;
262*38fd1498Szrj TREE_PUBLIC (vlt_register_set_fndecl) = 1;
263*38fd1498Szrj DECL_PRESERVE_P (vlt_register_set_fndecl) = 1;
264*38fd1498Szrj SET_DECL_LANGUAGE (vlt_register_set_fndecl, lang_cplusplus);
265*38fd1498Szrj
266*38fd1498Szrj /* Build function decl for __VLTRegisterPair*. */
267*38fd1498Szrj
268*38fd1498Szrj if (flag_vtv_debug)
269*38fd1498Szrj {
270*38fd1498Szrj register_pairs_type = build_function_type_list (void_type_node,
271*38fd1498Szrj build_pointer_type
272*38fd1498Szrj (ptr_type_node),
273*38fd1498Szrj const_ptr_type_node,
274*38fd1498Szrj size_type_node,
275*38fd1498Szrj const_ptr_type_node,
276*38fd1498Szrj const_string_type_node,
277*38fd1498Szrj const_string_type_node,
278*38fd1498Szrj NULL_TREE);
279*38fd1498Szrj
280*38fd1498Szrj vlt_register_pairs_fndecl = build_lang_decl
281*38fd1498Szrj (FUNCTION_DECL,
282*38fd1498Szrj get_identifier ("__VLTRegisterPairDebug"),
283*38fd1498Szrj register_pairs_type);
284*38fd1498Szrj }
285*38fd1498Szrj else
286*38fd1498Szrj {
287*38fd1498Szrj register_pairs_type = build_function_type_list (void_type_node,
288*38fd1498Szrj build_pointer_type
289*38fd1498Szrj (ptr_type_node),
290*38fd1498Szrj const_ptr_type_node,
291*38fd1498Szrj size_type_node,
292*38fd1498Szrj const_ptr_type_node,
293*38fd1498Szrj NULL_TREE);
294*38fd1498Szrj
295*38fd1498Szrj vlt_register_pairs_fndecl = build_lang_decl
296*38fd1498Szrj (FUNCTION_DECL,
297*38fd1498Szrj get_identifier ("__VLTRegisterPair"),
298*38fd1498Szrj register_pairs_type);
299*38fd1498Szrj }
300*38fd1498Szrj
301*38fd1498Szrj TREE_NOTHROW (vlt_register_pairs_fndecl) = 1;
302*38fd1498Szrj DECL_ATTRIBUTES (vlt_register_pairs_fndecl) =
303*38fd1498Szrj tree_cons (get_identifier ("leaf"), NULL,
304*38fd1498Szrj DECL_ATTRIBUTES (vlt_register_pairs_fndecl));
305*38fd1498Szrj DECL_EXTERNAL(vlt_register_pairs_fndecl) = 1;
306*38fd1498Szrj TREE_PUBLIC (vlt_register_pairs_fndecl) = 1;
307*38fd1498Szrj DECL_PRESERVE_P (vlt_register_pairs_fndecl) = 1;
308*38fd1498Szrj SET_DECL_LANGUAGE (vlt_register_pairs_fndecl, lang_cplusplus);
309*38fd1498Szrj
310*38fd1498Szrj }
311*38fd1498Szrj
312*38fd1498Szrj /* This is a helper function for
313*38fd1498Szrj vtv_compute_class_hierarchy_transitive_closure. It adds a
314*38fd1498Szrj vtv_graph_node to the WORKLIST, which is a linked list of
315*38fd1498Szrj seen-but-not-yet-processed nodes. INSERTED is a bitmap, one bit
316*38fd1498Szrj per node, to help make sure that we don't insert a node into the
317*38fd1498Szrj worklist more than once. Each node represents a class somewhere in
318*38fd1498Szrj our class hierarchy information. Every node in the graph gets added
319*38fd1498Szrj to the worklist exactly once and removed from the worklist exactly
320*38fd1498Szrj once (when all of its children have been processed). */
321*38fd1498Szrj
322*38fd1498Szrj static void
add_to_worklist(struct work_node ** worklist,struct vtv_graph_node * node,sbitmap inserted)323*38fd1498Szrj add_to_worklist (struct work_node **worklist, struct vtv_graph_node *node,
324*38fd1498Szrj sbitmap inserted)
325*38fd1498Szrj {
326*38fd1498Szrj struct work_node *new_work_node;
327*38fd1498Szrj
328*38fd1498Szrj if (bitmap_bit_p (inserted, node->class_uid))
329*38fd1498Szrj return;
330*38fd1498Szrj
331*38fd1498Szrj new_work_node = XNEW (struct work_node);
332*38fd1498Szrj new_work_node->next = *worklist;
333*38fd1498Szrj new_work_node->node = node;
334*38fd1498Szrj *worklist = new_work_node;
335*38fd1498Szrj
336*38fd1498Szrj bitmap_set_bit (inserted, node->class_uid);
337*38fd1498Szrj }
338*38fd1498Szrj
339*38fd1498Szrj /* This is a helper function for
340*38fd1498Szrj vtv_compute_class_hierarchy_transitive_closure. It goes through
341*38fd1498Szrj the WORKLIST of class hierarchy nodes looking for a "leaf" node,
342*38fd1498Szrj i.e. a node whose children in the hierarchy have all been
343*38fd1498Szrj processed. When it finds the next leaf node, it removes it from
344*38fd1498Szrj the linked list (WORKLIST) and returns the node. */
345*38fd1498Szrj
346*38fd1498Szrj static struct vtv_graph_node *
find_and_remove_next_leaf_node(struct work_node ** worklist)347*38fd1498Szrj find_and_remove_next_leaf_node (struct work_node **worklist)
348*38fd1498Szrj {
349*38fd1498Szrj struct work_node *prev, *cur;
350*38fd1498Szrj struct vtv_graph_node *ret_val = NULL;
351*38fd1498Szrj
352*38fd1498Szrj for (prev = NULL, cur = *worklist; cur; prev = cur, cur = cur->next)
353*38fd1498Szrj {
354*38fd1498Szrj if ((cur->node->children).length() == cur->node->num_processed_children)
355*38fd1498Szrj {
356*38fd1498Szrj if (prev == NULL)
357*38fd1498Szrj (*worklist) = cur->next;
358*38fd1498Szrj else
359*38fd1498Szrj prev->next = cur->next;
360*38fd1498Szrj
361*38fd1498Szrj cur->next = NULL;
362*38fd1498Szrj ret_val = cur->node;
363*38fd1498Szrj free (cur);
364*38fd1498Szrj return ret_val;
365*38fd1498Szrj }
366*38fd1498Szrj }
367*38fd1498Szrj
368*38fd1498Szrj return NULL;
369*38fd1498Szrj }
370*38fd1498Szrj
371*38fd1498Szrj /* In our class hierarchy graph, each class node contains a bitmap,
372*38fd1498Szrj with one bit for each class in the hierarchy. The bits are set for
373*38fd1498Szrj classes that are descendants in the graph of the current node.
374*38fd1498Szrj Initially the descendants bitmap is only set for immediate
375*38fd1498Szrj descendants. This function traverses the class hierarchy graph,
376*38fd1498Szrj bottom up, filling in the transitive closures for the descendants
377*38fd1498Szrj as we rise up the graph. */
378*38fd1498Szrj
379*38fd1498Szrj void
vtv_compute_class_hierarchy_transitive_closure(void)380*38fd1498Szrj vtv_compute_class_hierarchy_transitive_closure (void)
381*38fd1498Szrj {
382*38fd1498Szrj struct work_node *worklist = NULL;
383*38fd1498Szrj sbitmap inserted = sbitmap_alloc (num_vtable_map_nodes);
384*38fd1498Szrj unsigned i;
385*38fd1498Szrj unsigned j;
386*38fd1498Szrj
387*38fd1498Szrj /* Note: Every node in the graph gets added to the worklist exactly
388*38fd1498Szrj once and removed from the worklist exactly once (when all of its
389*38fd1498Szrj children have been processed). Each node's children edges are
390*38fd1498Szrj followed exactly once, and each node's parent edges are followed
391*38fd1498Szrj exactly once. So this algorithm is roughly O(V + 2E), i.e.
392*38fd1498Szrj O(E + V). */
393*38fd1498Szrj
394*38fd1498Szrj /* Set-up: */
395*38fd1498Szrj /* Find all the "leaf" nodes in the graph, and add them to the worklist. */
396*38fd1498Szrj bitmap_clear (inserted);
397*38fd1498Szrj for (j = 0; j < num_vtable_map_nodes; ++j)
398*38fd1498Szrj {
399*38fd1498Szrj struct vtbl_map_node *cur = vtbl_map_nodes_vec[j];
400*38fd1498Szrj if (cur->class_info
401*38fd1498Szrj && ((cur->class_info->children).length() == 0)
402*38fd1498Szrj && ! (bitmap_bit_p (inserted, cur->class_info->class_uid)))
403*38fd1498Szrj add_to_worklist (&worklist, cur->class_info, inserted);
404*38fd1498Szrj }
405*38fd1498Szrj
406*38fd1498Szrj /* Main work: pull next leaf node off work list, process it, add its
407*38fd1498Szrj parents to the worklist, where a 'leaf' node is one that has no
408*38fd1498Szrj children, or all of its children have been processed. */
409*38fd1498Szrj while (worklist)
410*38fd1498Szrj {
411*38fd1498Szrj struct vtv_graph_node *temp_node =
412*38fd1498Szrj find_and_remove_next_leaf_node (&worklist);
413*38fd1498Szrj
414*38fd1498Szrj gcc_assert (temp_node != NULL);
415*38fd1498Szrj temp_node->descendants = sbitmap_alloc (num_vtable_map_nodes);
416*38fd1498Szrj bitmap_clear (temp_node->descendants);
417*38fd1498Szrj bitmap_set_bit (temp_node->descendants, temp_node->class_uid);
418*38fd1498Szrj for (i = 0; i < (temp_node->children).length(); ++i)
419*38fd1498Szrj bitmap_ior (temp_node->descendants, temp_node->descendants,
420*38fd1498Szrj temp_node->children[i]->descendants);
421*38fd1498Szrj for (i = 0; i < (temp_node->parents).length(); ++i)
422*38fd1498Szrj {
423*38fd1498Szrj temp_node->parents[i]->num_processed_children =
424*38fd1498Szrj temp_node->parents[i]->num_processed_children + 1;
425*38fd1498Szrj if (!bitmap_bit_p (inserted, temp_node->parents[i]->class_uid))
426*38fd1498Szrj add_to_worklist (&worklist, temp_node->parents[i], inserted);
427*38fd1498Szrj }
428*38fd1498Szrj }
429*38fd1498Szrj }
430*38fd1498Szrj
431*38fd1498Szrj /* Keep track of which pairs we have already created __VLTRegisterPair
432*38fd1498Szrj calls for, to prevent creating duplicate calls within the same
433*38fd1498Szrj compilation unit. VTABLE_DECL is the var decl for the vtable of
434*38fd1498Szrj the (descendant) class that we are adding to our class hierarchy
435*38fd1498Szrj data. VPTR_ADDRESS is an expression for calculating the correct
436*38fd1498Szrj offset into the vtable (VTABLE_DECL). It is the actual vtable
437*38fd1498Szrj pointer address that will be stored in our list of valid vtable
438*38fd1498Szrj pointers for BASE_CLASS. BASE_CLASS is the record_type node for
439*38fd1498Szrj the base class to whose hiearchy we want to add
440*38fd1498Szrj VPTR_ADDRESS. (VTABLE_DECL should be the vtable for BASE_CLASS or
441*38fd1498Szrj one of BASE_CLASS' descendents. */
442*38fd1498Szrj
443*38fd1498Szrj static bool
check_and_record_registered_pairs(tree vtable_decl,tree vptr_address,tree base_class)444*38fd1498Szrj check_and_record_registered_pairs (tree vtable_decl, tree vptr_address,
445*38fd1498Szrj tree base_class)
446*38fd1498Szrj {
447*38fd1498Szrj unsigned offset;
448*38fd1498Szrj struct vtbl_map_node *base_vtable_map_node;
449*38fd1498Szrj bool inserted_something = false;
450*38fd1498Szrj
451*38fd1498Szrj
452*38fd1498Szrj if (TREE_CODE (vptr_address) == ADDR_EXPR
453*38fd1498Szrj && TREE_CODE (TREE_OPERAND (vptr_address, 0)) == MEM_REF)
454*38fd1498Szrj vptr_address = TREE_OPERAND (vptr_address, 0);
455*38fd1498Szrj
456*38fd1498Szrj if (TREE_OPERAND_LENGTH (vptr_address) > 1)
457*38fd1498Szrj offset = TREE_INT_CST_LOW (TREE_OPERAND (vptr_address, 1));
458*38fd1498Szrj else
459*38fd1498Szrj offset = 0;
460*38fd1498Szrj
461*38fd1498Szrj base_vtable_map_node = vtbl_map_get_node (TYPE_MAIN_VARIANT (base_class));
462*38fd1498Szrj
463*38fd1498Szrj inserted_something = vtbl_map_node_registration_insert
464*38fd1498Szrj (base_vtable_map_node,
465*38fd1498Szrj vtable_decl,
466*38fd1498Szrj offset);
467*38fd1498Szrj return !inserted_something;
468*38fd1498Szrj }
469*38fd1498Szrj
470*38fd1498Szrj /* Given an IDENTIFIER_NODE, build and return a string literal based on it. */
471*38fd1498Szrj
472*38fd1498Szrj static tree
build_string_from_id(tree identifier)473*38fd1498Szrj build_string_from_id (tree identifier)
474*38fd1498Szrj {
475*38fd1498Szrj int len;
476*38fd1498Szrj
477*38fd1498Szrj gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE);
478*38fd1498Szrj
479*38fd1498Szrj len = IDENTIFIER_LENGTH (identifier);
480*38fd1498Szrj return build_string_literal (len + 1, IDENTIFIER_POINTER (identifier));
481*38fd1498Szrj }
482*38fd1498Szrj
483*38fd1498Szrj /* A class may contain secondary vtables in it, for various reasons.
484*38fd1498Szrj This function goes through the decl chain of a class record looking
485*38fd1498Szrj for any fields that point to secondary vtables, and adding calls to
486*38fd1498Szrj __VLTRegisterPair for the secondary vtable pointers.
487*38fd1498Szrj
488*38fd1498Szrj BASE_CLASS_DECL_ARG is an expression for the address of the vtable
489*38fd1498Szrj map variable for the BASE_CLASS (whose hierarchy we are currently
490*38fd1498Szrj updating). BASE_CLASS is the record_type node for the base class.
491*38fd1498Szrj RECORD_TYPE is the record_type node for the descendant class that
492*38fd1498Szrj we are possibly adding to BASE_CLASS's hierarchy. BODY is the
493*38fd1498Szrj function body for the constructor init function to which we are
494*38fd1498Szrj adding our calls to __VLTRegisterPair. */
495*38fd1498Szrj
496*38fd1498Szrj static void
register_construction_vtables(tree base_class,tree record_type,vec<tree> * vtable_ptr_array)497*38fd1498Szrj register_construction_vtables (tree base_class, tree record_type,
498*38fd1498Szrj vec<tree> *vtable_ptr_array)
499*38fd1498Szrj {
500*38fd1498Szrj tree vtbl_var_decl;
501*38fd1498Szrj
502*38fd1498Szrj if (TREE_CODE (record_type) != RECORD_TYPE)
503*38fd1498Szrj return;
504*38fd1498Szrj
505*38fd1498Szrj vtbl_var_decl = CLASSTYPE_VTABLES (record_type);
506*38fd1498Szrj
507*38fd1498Szrj if (CLASSTYPE_VBASECLASSES (record_type))
508*38fd1498Szrj {
509*38fd1498Szrj tree vtt_decl;
510*38fd1498Szrj bool already_registered = false;
511*38fd1498Szrj tree val_vtbl_decl = NULL_TREE;
512*38fd1498Szrj
513*38fd1498Szrj vtt_decl = DECL_CHAIN (vtbl_var_decl);
514*38fd1498Szrj
515*38fd1498Szrj /* Check to see if we have found a VTT. Add its data if appropriate. */
516*38fd1498Szrj if (vtt_decl)
517*38fd1498Szrj {
518*38fd1498Szrj tree values = DECL_INITIAL (vtt_decl);
519*38fd1498Szrj if (TREE_ASM_WRITTEN (vtt_decl)
520*38fd1498Szrj && values != NULL_TREE
521*38fd1498Szrj && TREE_CODE (values) == CONSTRUCTOR
522*38fd1498Szrj && TREE_CODE (TREE_TYPE (values)) == ARRAY_TYPE)
523*38fd1498Szrj {
524*38fd1498Szrj unsigned HOST_WIDE_INT cnt;
525*38fd1498Szrj constructor_elt *ce;
526*38fd1498Szrj
527*38fd1498Szrj /* Loop through the initialization values for this
528*38fd1498Szrj vtable to get all the correct vtable pointer
529*38fd1498Szrj addresses that we need to add to our set of valid
530*38fd1498Szrj vtable pointers for the current base class. This may
531*38fd1498Szrj result in adding more than just the element assigned
532*38fd1498Szrj to the primary vptr of the class, so we may end up
533*38fd1498Szrj with more vtable pointers than are strictly
534*38fd1498Szrj necessary. */
535*38fd1498Szrj
536*38fd1498Szrj for (cnt = 0;
537*38fd1498Szrj vec_safe_iterate (CONSTRUCTOR_ELTS (values),
538*38fd1498Szrj cnt, &ce);
539*38fd1498Szrj cnt++)
540*38fd1498Szrj {
541*38fd1498Szrj tree value = ce->value;
542*38fd1498Szrj
543*38fd1498Szrj /* Search for the ADDR_EXPR operand within the value. */
544*38fd1498Szrj
545*38fd1498Szrj while (value
546*38fd1498Szrj && TREE_OPERAND (value, 0)
547*38fd1498Szrj && TREE_CODE (TREE_OPERAND (value, 0)) == ADDR_EXPR)
548*38fd1498Szrj value = TREE_OPERAND (value, 0);
549*38fd1498Szrj
550*38fd1498Szrj /* The VAR_DECL for the vtable should be the first
551*38fd1498Szrj argument of the ADDR_EXPR, which is the first
552*38fd1498Szrj argument of value.*/
553*38fd1498Szrj
554*38fd1498Szrj if (TREE_OPERAND (value, 0))
555*38fd1498Szrj val_vtbl_decl = TREE_OPERAND (value, 0);
556*38fd1498Szrj
557*38fd1498Szrj while (!VAR_P (val_vtbl_decl)
558*38fd1498Szrj && TREE_OPERAND (val_vtbl_decl, 0))
559*38fd1498Szrj val_vtbl_decl = TREE_OPERAND (val_vtbl_decl, 0);
560*38fd1498Szrj
561*38fd1498Szrj gcc_assert (VAR_P (val_vtbl_decl));
562*38fd1498Szrj
563*38fd1498Szrj /* Check to see if we already have this vtable pointer in
564*38fd1498Szrj our valid set for this base class. */
565*38fd1498Szrj
566*38fd1498Szrj already_registered = check_and_record_registered_pairs
567*38fd1498Szrj (val_vtbl_decl,
568*38fd1498Szrj value,
569*38fd1498Szrj base_class);
570*38fd1498Szrj
571*38fd1498Szrj if (already_registered)
572*38fd1498Szrj continue;
573*38fd1498Szrj
574*38fd1498Szrj /* Add this vtable pointer to our set of valid
575*38fd1498Szrj pointers for the base class. */
576*38fd1498Szrj
577*38fd1498Szrj vtable_ptr_array->safe_push (value);
578*38fd1498Szrj current_set_size++;
579*38fd1498Szrj }
580*38fd1498Szrj }
581*38fd1498Szrj }
582*38fd1498Szrj }
583*38fd1498Szrj }
584*38fd1498Szrj
585*38fd1498Szrj /* This function iterates through all the vtables it can find from the
586*38fd1498Szrj BINFO of a class, to make sure we have found ALL of the vtables
587*38fd1498Szrj that an object of that class could point to. Generate calls to
588*38fd1498Szrj __VLTRegisterPair for those vtable pointers that we find.
589*38fd1498Szrj
590*38fd1498Szrj BINFO is the tree_binfo node for the BASE_CLASS. BODY is the
591*38fd1498Szrj function body for the constructor init function to which we are
592*38fd1498Szrj adding calls to __VLTRegisterPair. ARG1 is an expression for the
593*38fd1498Szrj address of the vtable map variable (for the BASE_CLASS), that will
594*38fd1498Szrj point to the updated data set. BASE_CLASS is the record_type node
595*38fd1498Szrj for the base class whose set of valid vtable pointers we are
596*38fd1498Szrj updating. STR1 and STR2 are all debugging information, to be passed
597*38fd1498Szrj as parameters to __VLTRegisterPairDebug. STR1 represents the name
598*38fd1498Szrj of the vtable map variable to be updated by the call. Similarly,
599*38fd1498Szrj STR2 represents the name of the class whose vtable pointer is being
600*38fd1498Szrj added to the hierarchy. */
601*38fd1498Szrj
602*38fd1498Szrj static void
register_other_binfo_vtables(tree binfo,tree base_class,vec<tree> * vtable_ptr_array)603*38fd1498Szrj register_other_binfo_vtables (tree binfo, tree base_class,
604*38fd1498Szrj vec<tree> *vtable_ptr_array)
605*38fd1498Szrj {
606*38fd1498Szrj unsigned ix;
607*38fd1498Szrj tree base_binfo;
608*38fd1498Szrj tree vtable_decl;
609*38fd1498Szrj bool already_registered;
610*38fd1498Szrj
611*38fd1498Szrj if (binfo == NULL_TREE)
612*38fd1498Szrj return;
613*38fd1498Szrj
614*38fd1498Szrj for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
615*38fd1498Szrj {
616*38fd1498Szrj if ((!BINFO_PRIMARY_P (base_binfo)
617*38fd1498Szrj || BINFO_VIRTUAL_P (base_binfo))
618*38fd1498Szrj && (vtable_decl = get_vtbl_decl_for_binfo (base_binfo)))
619*38fd1498Szrj {
620*38fd1498Szrj tree vtable_address = build_vtbl_address (base_binfo);
621*38fd1498Szrj
622*38fd1498Szrj already_registered = check_and_record_registered_pairs
623*38fd1498Szrj (vtable_decl,
624*38fd1498Szrj vtable_address,
625*38fd1498Szrj base_class);
626*38fd1498Szrj if (!already_registered)
627*38fd1498Szrj {
628*38fd1498Szrj vtable_ptr_array->safe_push (vtable_address);
629*38fd1498Szrj current_set_size++;
630*38fd1498Szrj }
631*38fd1498Szrj }
632*38fd1498Szrj
633*38fd1498Szrj register_other_binfo_vtables (base_binfo, base_class, vtable_ptr_array);
634*38fd1498Szrj }
635*38fd1498Szrj }
636*38fd1498Szrj
637*38fd1498Szrj /* The set of valid vtable pointers for any given class are stored in
638*38fd1498Szrj a hash table. For reasons of efficiency, that hash table size is
639*38fd1498Szrj always a power of two. In order to try to prevent re-sizing the
640*38fd1498Szrj hash tables very often, we pass __VLTRegisterPair an initial guess
641*38fd1498Szrj as to the number of entries the hashtable will eventually need
642*38fd1498Szrj (rounded up to the nearest power of two). This function takes the
643*38fd1498Szrj class information we have collected for a particular class,
644*38fd1498Szrj CLASS_NODE, and calculates the hash table size guess. */
645*38fd1498Szrj
646*38fd1498Szrj static int
guess_num_vtable_pointers(struct vtv_graph_node * class_node)647*38fd1498Szrj guess_num_vtable_pointers (struct vtv_graph_node *class_node)
648*38fd1498Szrj {
649*38fd1498Szrj tree vtbl;
650*38fd1498Szrj int total_num_vtbls = 0;
651*38fd1498Szrj int num_vtbls_power_of_two = 1;
652*38fd1498Szrj unsigned i;
653*38fd1498Szrj
654*38fd1498Szrj for (i = 0; i < num_vtable_map_nodes; ++i)
655*38fd1498Szrj if (bitmap_bit_p (class_node->descendants, i))
656*38fd1498Szrj {
657*38fd1498Szrj tree class_type = vtbl_map_nodes_vec[i]->class_info->class_type;
658*38fd1498Szrj for (vtbl = CLASSTYPE_VTABLES (class_type); vtbl;
659*38fd1498Szrj vtbl = DECL_CHAIN (vtbl))
660*38fd1498Szrj {
661*38fd1498Szrj total_num_vtbls++;
662*38fd1498Szrj if (total_num_vtbls > num_vtbls_power_of_two)
663*38fd1498Szrj num_vtbls_power_of_two <<= 1;
664*38fd1498Szrj }
665*38fd1498Szrj }
666*38fd1498Szrj return num_vtbls_power_of_two;
667*38fd1498Szrj }
668*38fd1498Szrj
669*38fd1498Szrj /* A simple hash function on strings */
670*38fd1498Szrj /* Be careful about changing this routine. The values generated will
671*38fd1498Szrj be stored in the calls to InitSet. So, changing this routine may
672*38fd1498Szrj cause a binary incompatibility. */
673*38fd1498Szrj
674*38fd1498Szrj static uint32_t
vtv_string_hash(const char * in)675*38fd1498Szrj vtv_string_hash (const char *in)
676*38fd1498Szrj {
677*38fd1498Szrj const char *s = in;
678*38fd1498Szrj uint32_t h = 0;
679*38fd1498Szrj
680*38fd1498Szrj gcc_assert (in != NULL);
681*38fd1498Szrj for ( ; *s; ++s)
682*38fd1498Szrj h = 5 * h + *s;
683*38fd1498Szrj return h;
684*38fd1498Szrj }
685*38fd1498Szrj
686*38fd1498Szrj static char *
get_log_file_name(const char * fname)687*38fd1498Szrj get_log_file_name (const char *fname)
688*38fd1498Szrj {
689*38fd1498Szrj const char *tmp_dir = concat (dump_dir_name, NULL);
690*38fd1498Szrj char *full_name;
691*38fd1498Szrj int dir_len;
692*38fd1498Szrj int fname_len;
693*38fd1498Szrj
694*38fd1498Szrj dir_len = strlen (tmp_dir);
695*38fd1498Szrj fname_len = strlen (fname);
696*38fd1498Szrj
697*38fd1498Szrj full_name = XNEWVEC (char, dir_len + fname_len + 1);
698*38fd1498Szrj strcpy (full_name, tmp_dir);
699*38fd1498Szrj strcpy (full_name + dir_len, fname);
700*38fd1498Szrj
701*38fd1498Szrj return full_name;
702*38fd1498Szrj }
703*38fd1498Szrj
704*38fd1498Szrj static void
write_out_current_set_data(tree base_class,int set_size)705*38fd1498Szrj write_out_current_set_data (tree base_class, int set_size)
706*38fd1498Szrj {
707*38fd1498Szrj static int class_data_log_fd = -1;
708*38fd1498Szrj char buffer[1024];
709*38fd1498Szrj int bytes_written __attribute__ ((unused));
710*38fd1498Szrj char *file_name = get_log_file_name ("vtv_class_set_sizes.log");
711*38fd1498Szrj
712*38fd1498Szrj if (class_data_log_fd == -1)
713*38fd1498Szrj class_data_log_fd = open (file_name,
714*38fd1498Szrj O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
715*38fd1498Szrj
716*38fd1498Szrj if (class_data_log_fd == -1)
717*38fd1498Szrj {
718*38fd1498Szrj warning_at (UNKNOWN_LOCATION, 0,
719*38fd1498Szrj "unable to open log file %<vtv_class_set_sizes.log%>: %m");
720*38fd1498Szrj return;
721*38fd1498Szrj }
722*38fd1498Szrj
723*38fd1498Szrj snprintf (buffer, sizeof (buffer), "%s %d\n",
724*38fd1498Szrj IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (base_class))),
725*38fd1498Szrj set_size);
726*38fd1498Szrj bytes_written = write (class_data_log_fd, buffer, strlen (buffer));
727*38fd1498Szrj }
728*38fd1498Szrj
729*38fd1498Szrj static tree
build_key_buffer_arg(tree base_ptr_var_decl)730*38fd1498Szrj build_key_buffer_arg (tree base_ptr_var_decl)
731*38fd1498Szrj {
732*38fd1498Szrj const int key_type_fixed_size = 8;
733*38fd1498Szrj uint32_t len1 = IDENTIFIER_LENGTH (DECL_NAME (base_ptr_var_decl));
734*38fd1498Szrj uint32_t hash_value = vtv_string_hash (IDENTIFIER_POINTER
735*38fd1498Szrj (DECL_NAME (base_ptr_var_decl)));
736*38fd1498Szrj void *key_buffer = xmalloc (len1 + key_type_fixed_size);
737*38fd1498Szrj uint32_t *value_ptr = (uint32_t *) key_buffer;
738*38fd1498Szrj tree ret_value;
739*38fd1498Szrj
740*38fd1498Szrj /* Set the len and hash for the string. */
741*38fd1498Szrj *value_ptr = len1;
742*38fd1498Szrj value_ptr++;
743*38fd1498Szrj *value_ptr = hash_value;
744*38fd1498Szrj
745*38fd1498Szrj /* Now copy the string representation of the vtbl map name... */
746*38fd1498Szrj memcpy ((char *) key_buffer + key_type_fixed_size,
747*38fd1498Szrj IDENTIFIER_POINTER (DECL_NAME (base_ptr_var_decl)),
748*38fd1498Szrj len1);
749*38fd1498Szrj
750*38fd1498Szrj /* ... and build a string literal from it. This will make a copy
751*38fd1498Szrj so the key_bufffer is not needed anymore after this. */
752*38fd1498Szrj ret_value = build_string_literal (len1 + key_type_fixed_size,
753*38fd1498Szrj (char *) key_buffer);
754*38fd1498Szrj free (key_buffer);
755*38fd1498Szrj return ret_value;
756*38fd1498Szrj }
757*38fd1498Szrj
758*38fd1498Szrj static void
insert_call_to_register_set(tree class_name,vec<tree> * vtbl_ptr_array,tree body,tree arg1,tree arg2,tree size_hint_arg)759*38fd1498Szrj insert_call_to_register_set (tree class_name,
760*38fd1498Szrj vec<tree> *vtbl_ptr_array, tree body, tree arg1,
761*38fd1498Szrj tree arg2, tree size_hint_arg)
762*38fd1498Szrj {
763*38fd1498Szrj tree call_expr;
764*38fd1498Szrj int num_args = vtbl_ptr_array->length();
765*38fd1498Szrj char *array_arg_name = ACONCAT (("__vptr_array_",
766*38fd1498Szrj IDENTIFIER_POINTER (class_name), NULL));
767*38fd1498Szrj tree array_arg_type = build_array_type_nelts (build_pointer_type
768*38fd1498Szrj (build_pointer_type
769*38fd1498Szrj (void_type_node)),
770*38fd1498Szrj num_args);
771*38fd1498Szrj tree array_arg = build_decl (UNKNOWN_LOCATION, VAR_DECL,
772*38fd1498Szrj get_identifier (array_arg_name),
773*38fd1498Szrj array_arg_type);
774*38fd1498Szrj int k;
775*38fd1498Szrj
776*38fd1498Szrj vec<constructor_elt, va_gc> *array_elements;
777*38fd1498Szrj vec_alloc (array_elements, num_args);
778*38fd1498Szrj
779*38fd1498Szrj tree initial = NULL_TREE;
780*38fd1498Szrj tree arg3 = NULL_TREE;
781*38fd1498Szrj
782*38fd1498Szrj TREE_PUBLIC (array_arg) = 0;
783*38fd1498Szrj DECL_EXTERNAL (array_arg) = 0;
784*38fd1498Szrj TREE_STATIC (array_arg) = 1;
785*38fd1498Szrj DECL_ARTIFICIAL (array_arg) = 0;
786*38fd1498Szrj TREE_READONLY (array_arg) = 1;
787*38fd1498Szrj DECL_IGNORED_P (array_arg) = 0;
788*38fd1498Szrj DECL_PRESERVE_P (array_arg) = 0;
789*38fd1498Szrj DECL_VISIBILITY (array_arg) = VISIBILITY_HIDDEN;
790*38fd1498Szrj
791*38fd1498Szrj for (k = 0; k < num_args; ++k)
792*38fd1498Szrj {
793*38fd1498Szrj CONSTRUCTOR_APPEND_ELT (array_elements, NULL_TREE, (*vtbl_ptr_array)[k]);
794*38fd1498Szrj }
795*38fd1498Szrj
796*38fd1498Szrj initial = build_constructor (TREE_TYPE (array_arg), array_elements);
797*38fd1498Szrj
798*38fd1498Szrj TREE_CONSTANT (initial) = 1;
799*38fd1498Szrj TREE_STATIC (initial) = 1;
800*38fd1498Szrj DECL_INITIAL (array_arg) = initial;
801*38fd1498Szrj relayout_decl (array_arg);
802*38fd1498Szrj varpool_node::finalize_decl (array_arg);
803*38fd1498Szrj
804*38fd1498Szrj arg3 = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (array_arg)), array_arg);
805*38fd1498Szrj
806*38fd1498Szrj TREE_TYPE (arg3) = build_pointer_type (TREE_TYPE (array_arg));
807*38fd1498Szrj
808*38fd1498Szrj call_expr = build_call_expr (vlt_register_set_fndecl, 5, arg1,
809*38fd1498Szrj arg2, /* set_symbol_key */
810*38fd1498Szrj size_hint_arg, build_int_cst (size_type_node,
811*38fd1498Szrj num_args),
812*38fd1498Szrj arg3);
813*38fd1498Szrj append_to_statement_list (call_expr, &body);
814*38fd1498Szrj num_calls_to_regset++;
815*38fd1498Szrj }
816*38fd1498Szrj
817*38fd1498Szrj static void
insert_call_to_register_pair(vec<tree> * vtbl_ptr_array,tree arg1,tree arg2,tree size_hint_arg,tree str1,tree str2,tree body)818*38fd1498Szrj insert_call_to_register_pair (vec<tree> *vtbl_ptr_array, tree arg1,
819*38fd1498Szrj tree arg2, tree size_hint_arg, tree str1,
820*38fd1498Szrj tree str2, tree body)
821*38fd1498Szrj {
822*38fd1498Szrj tree call_expr;
823*38fd1498Szrj int num_args = vtbl_ptr_array->length();
824*38fd1498Szrj tree vtable_address = NULL_TREE;
825*38fd1498Szrj
826*38fd1498Szrj if (num_args == 0)
827*38fd1498Szrj vtable_address = build_int_cst (build_pointer_type (void_type_node), 0);
828*38fd1498Szrj else
829*38fd1498Szrj vtable_address = (*vtbl_ptr_array)[0];
830*38fd1498Szrj
831*38fd1498Szrj if (flag_vtv_debug)
832*38fd1498Szrj call_expr = build_call_expr (vlt_register_pairs_fndecl, 6, arg1, arg2,
833*38fd1498Szrj size_hint_arg, vtable_address, str1, str2);
834*38fd1498Szrj else
835*38fd1498Szrj call_expr = build_call_expr (vlt_register_pairs_fndecl, 4, arg1, arg2,
836*38fd1498Szrj size_hint_arg, vtable_address);
837*38fd1498Szrj
838*38fd1498Szrj append_to_statement_list (call_expr, &body);
839*38fd1498Szrj num_calls_to_regpair++;
840*38fd1498Szrj }
841*38fd1498Szrj
842*38fd1498Szrj static void
output_set_info(tree record_type,vec<tree> vtbl_ptr_array)843*38fd1498Szrj output_set_info (tree record_type, vec<tree> vtbl_ptr_array)
844*38fd1498Szrj {
845*38fd1498Szrj static int vtv_debug_log_fd = -1;
846*38fd1498Szrj char buffer[1024];
847*38fd1498Szrj int bytes_written __attribute__ ((unused));
848*38fd1498Szrj int array_len = vtbl_ptr_array.length();
849*38fd1498Szrj const char *class_name =
850*38fd1498Szrj IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (record_type)));
851*38fd1498Szrj char *file_name = get_log_file_name ("vtv_set_ptr_data.log");
852*38fd1498Szrj
853*38fd1498Szrj if (vtv_debug_log_fd == -1)
854*38fd1498Szrj vtv_debug_log_fd = open (file_name,
855*38fd1498Szrj O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
856*38fd1498Szrj if (vtv_debug_log_fd == -1)
857*38fd1498Szrj {
858*38fd1498Szrj warning_at (UNKNOWN_LOCATION, 0,
859*38fd1498Szrj "unable to open log file %<vtv_set_ptr_data.log%>: %m");
860*38fd1498Szrj return;
861*38fd1498Szrj }
862*38fd1498Szrj
863*38fd1498Szrj for (int i = 0; i < array_len; ++i)
864*38fd1498Szrj {
865*38fd1498Szrj const char *vptr_name = "unknown";
866*38fd1498Szrj int vptr_offset = 0;
867*38fd1498Szrj
868*38fd1498Szrj if (TREE_CODE (vtbl_ptr_array[i]) == POINTER_PLUS_EXPR)
869*38fd1498Szrj {
870*38fd1498Szrj tree arg0 = TREE_OPERAND (vtbl_ptr_array[i], 0);
871*38fd1498Szrj tree arg1 = TREE_OPERAND (vtbl_ptr_array[i], 1);
872*38fd1498Szrj
873*38fd1498Szrj if (TREE_CODE (arg0) == ADDR_EXPR)
874*38fd1498Szrj arg0 = TREE_OPERAND (arg0, 0);
875*38fd1498Szrj
876*38fd1498Szrj if (VAR_P (arg0))
877*38fd1498Szrj vptr_name = IDENTIFIER_POINTER (DECL_NAME (arg0));
878*38fd1498Szrj
879*38fd1498Szrj if (TREE_CODE (arg1) == INTEGER_CST)
880*38fd1498Szrj vptr_offset = TREE_INT_CST_LOW (arg1);
881*38fd1498Szrj }
882*38fd1498Szrj
883*38fd1498Szrj snprintf (buffer, sizeof (buffer), "%s %s %s + %d\n",
884*38fd1498Szrj main_input_filename, class_name, vptr_name, vptr_offset);
885*38fd1498Szrj bytes_written = write (vtv_debug_log_fd, buffer, strlen(buffer));
886*38fd1498Szrj }
887*38fd1498Szrj
888*38fd1498Szrj }
889*38fd1498Szrj
890*38fd1498Szrj /* This function goes through our internal class hierarchy & vtable
891*38fd1498Szrj pointer data structure and outputs calls to __VLTRegisterPair for
892*38fd1498Szrj every class-vptr pair (for those classes whose vtable would be
893*38fd1498Szrj output in the current compilation unit). These calls get put into
894*38fd1498Szrj our constructor initialization function. BODY is the function
895*38fd1498Szrj body, so far, of our constructor initialization function, to which we
896*38fd1498Szrj add the calls. */
897*38fd1498Szrj
898*38fd1498Szrj static bool
register_all_pairs(tree body)899*38fd1498Szrj register_all_pairs (tree body)
900*38fd1498Szrj {
901*38fd1498Szrj bool registered_at_least_one = false;
902*38fd1498Szrj vec<tree> *vtbl_ptr_array = NULL;
903*38fd1498Szrj unsigned j;
904*38fd1498Szrj
905*38fd1498Szrj for (j = 0; j < num_vtable_map_nodes; ++j)
906*38fd1498Szrj {
907*38fd1498Szrj struct vtbl_map_node *current = vtbl_map_nodes_vec[j];
908*38fd1498Szrj unsigned i = 0;
909*38fd1498Szrj tree base_class = current->class_info->class_type;
910*38fd1498Szrj tree base_ptr_var_decl = current->vtbl_map_decl;
911*38fd1498Szrj tree arg1;
912*38fd1498Szrj tree arg2;
913*38fd1498Szrj tree new_type;
914*38fd1498Szrj tree str1 = NULL_TREE;
915*38fd1498Szrj tree str2 = NULL_TREE;
916*38fd1498Szrj size_t size_hint;
917*38fd1498Szrj tree size_hint_arg;
918*38fd1498Szrj
919*38fd1498Szrj gcc_assert (current->class_info != NULL);
920*38fd1498Szrj
921*38fd1498Szrj
922*38fd1498Szrj if (flag_vtv_debug)
923*38fd1498Szrj str1 = build_string_from_id (DECL_NAME (base_ptr_var_decl));
924*38fd1498Szrj
925*38fd1498Szrj new_type = build_pointer_type (TREE_TYPE (base_ptr_var_decl));
926*38fd1498Szrj arg1 = build1 (ADDR_EXPR, new_type, base_ptr_var_decl);
927*38fd1498Szrj
928*38fd1498Szrj /* We need a fresh vector for each iteration. */
929*38fd1498Szrj if (vtbl_ptr_array)
930*38fd1498Szrj vec_free (vtbl_ptr_array);
931*38fd1498Szrj
932*38fd1498Szrj vec_alloc (vtbl_ptr_array, 10);
933*38fd1498Szrj
934*38fd1498Szrj for (i = 0; i < num_vtable_map_nodes; ++i)
935*38fd1498Szrj if (bitmap_bit_p (current->class_info->descendants, i))
936*38fd1498Szrj {
937*38fd1498Szrj struct vtbl_map_node *vtbl_class_node = vtbl_map_nodes_vec[i];
938*38fd1498Szrj tree class_type = vtbl_class_node->class_info->class_type;
939*38fd1498Szrj
940*38fd1498Szrj if (class_type
941*38fd1498Szrj && (TREE_CODE (class_type) == RECORD_TYPE))
942*38fd1498Szrj {
943*38fd1498Szrj bool already_registered;
944*38fd1498Szrj
945*38fd1498Szrj tree binfo = TYPE_BINFO (class_type);
946*38fd1498Szrj tree vtable_decl;
947*38fd1498Szrj bool vtable_should_be_output = false;
948*38fd1498Szrj
949*38fd1498Szrj vtable_decl = CLASSTYPE_VTABLES (class_type);
950*38fd1498Szrj
951*38fd1498Szrj /* Handle main vtable for this class. */
952*38fd1498Szrj
953*38fd1498Szrj if (vtable_decl)
954*38fd1498Szrj {
955*38fd1498Szrj vtable_should_be_output = TREE_ASM_WRITTEN (vtable_decl);
956*38fd1498Szrj str2 = build_string_from_id (DECL_NAME (vtable_decl));
957*38fd1498Szrj }
958*38fd1498Szrj
959*38fd1498Szrj if (vtable_decl && vtable_should_be_output)
960*38fd1498Szrj {
961*38fd1498Szrj tree vtable_address = build_vtbl_address (binfo);
962*38fd1498Szrj
963*38fd1498Szrj already_registered = check_and_record_registered_pairs
964*38fd1498Szrj (vtable_decl,
965*38fd1498Szrj vtable_address,
966*38fd1498Szrj base_class);
967*38fd1498Szrj
968*38fd1498Szrj
969*38fd1498Szrj if (!already_registered)
970*38fd1498Szrj {
971*38fd1498Szrj vtbl_ptr_array->safe_push (vtable_address);
972*38fd1498Szrj
973*38fd1498Szrj /* Find and handle any 'extra' vtables associated
974*38fd1498Szrj with this class, via virtual inheritance. */
975*38fd1498Szrj register_construction_vtables (base_class, class_type,
976*38fd1498Szrj vtbl_ptr_array);
977*38fd1498Szrj
978*38fd1498Szrj /* Find and handle any 'extra' vtables associated
979*38fd1498Szrj with this class, via multiple inheritance. */
980*38fd1498Szrj register_other_binfo_vtables (binfo, base_class,
981*38fd1498Szrj vtbl_ptr_array);
982*38fd1498Szrj }
983*38fd1498Szrj }
984*38fd1498Szrj }
985*38fd1498Szrj }
986*38fd1498Szrj current_set_size = vtbl_ptr_array->length();
987*38fd1498Szrj
988*38fd1498Szrj /* Sometimes we need to initialize the set symbol even if we are
989*38fd1498Szrj not adding any vtable pointers to the set in the current
990*38fd1498Szrj compilation unit. In that case, we need to initialize the
991*38fd1498Szrj set to our best guess as to what the eventual size of the set
992*38fd1498Szrj hash table will be (to prevent having to re-size the hash
993*38fd1498Szrj table later). */
994*38fd1498Szrj
995*38fd1498Szrj size_hint = guess_num_vtable_pointers (current->class_info);
996*38fd1498Szrj
997*38fd1498Szrj /* If we have added vtable pointers to the set in this
998*38fd1498Szrj compilation unit, adjust the size hint for the set's hash
999*38fd1498Szrj table appropriately. */
1000*38fd1498Szrj if (vtbl_ptr_array->length() > 0)
1001*38fd1498Szrj {
1002*38fd1498Szrj unsigned len = vtbl_ptr_array->length();
1003*38fd1498Szrj while ((size_t) len > size_hint)
1004*38fd1498Szrj size_hint <<= 1;
1005*38fd1498Szrj }
1006*38fd1498Szrj size_hint_arg = build_int_cst (size_type_node, size_hint);
1007*38fd1498Szrj
1008*38fd1498Szrj /* Get the key-buffer argument. */
1009*38fd1498Szrj arg2 = build_key_buffer_arg (base_ptr_var_decl);
1010*38fd1498Szrj
1011*38fd1498Szrj if (str2 == NULL_TREE)
1012*38fd1498Szrj str2 = build_string_literal (strlen ("unknown") + 1,
1013*38fd1498Szrj "unknown");
1014*38fd1498Szrj
1015*38fd1498Szrj if (flag_vtv_debug)
1016*38fd1498Szrj output_set_info (current->class_info->class_type,
1017*38fd1498Szrj *vtbl_ptr_array);
1018*38fd1498Szrj
1019*38fd1498Szrj if (vtbl_ptr_array->length() > 1)
1020*38fd1498Szrj {
1021*38fd1498Szrj insert_call_to_register_set (current->class_name,
1022*38fd1498Szrj vtbl_ptr_array, body, arg1, arg2,
1023*38fd1498Szrj size_hint_arg);
1024*38fd1498Szrj registered_at_least_one = true;
1025*38fd1498Szrj }
1026*38fd1498Szrj else
1027*38fd1498Szrj {
1028*38fd1498Szrj
1029*38fd1498Szrj if (vtbl_ptr_array->length() > 0
1030*38fd1498Szrj || (current->is_used
1031*38fd1498Szrj || (current->registered->size() > 0)))
1032*38fd1498Szrj {
1033*38fd1498Szrj insert_call_to_register_pair (vtbl_ptr_array,
1034*38fd1498Szrj arg1, arg2, size_hint_arg, str1,
1035*38fd1498Szrj str2, body);
1036*38fd1498Szrj registered_at_least_one = true;
1037*38fd1498Szrj }
1038*38fd1498Szrj }
1039*38fd1498Szrj
1040*38fd1498Szrj if (flag_vtv_counts && current_set_size > 0)
1041*38fd1498Szrj write_out_current_set_data (base_class, current_set_size);
1042*38fd1498Szrj
1043*38fd1498Szrj }
1044*38fd1498Szrj
1045*38fd1498Szrj return registered_at_least_one;
1046*38fd1498Szrj }
1047*38fd1498Szrj
1048*38fd1498Szrj /* Given a tree containing a class type (CLASS_TYPE), this function
1049*38fd1498Szrj finds and returns the class hierarchy node for that class in our
1050*38fd1498Szrj data structure. */
1051*38fd1498Szrj
1052*38fd1498Szrj static struct vtv_graph_node *
find_graph_node(tree class_type)1053*38fd1498Szrj find_graph_node (tree class_type)
1054*38fd1498Szrj {
1055*38fd1498Szrj struct vtbl_map_node *vtbl_node;
1056*38fd1498Szrj
1057*38fd1498Szrj vtbl_node = vtbl_map_get_node (TYPE_MAIN_VARIANT (class_type));
1058*38fd1498Szrj if (vtbl_node)
1059*38fd1498Szrj return vtbl_node->class_info;
1060*38fd1498Szrj
1061*38fd1498Szrj return NULL;
1062*38fd1498Szrj }
1063*38fd1498Szrj
1064*38fd1498Szrj /* Add base class/derived class pair to our internal class hierarchy
1065*38fd1498Szrj data structure. BASE_NODE is our vtv_graph_node that corresponds
1066*38fd1498Szrj to a base class. DERIVED_NODE is our vtv_graph_node that
1067*38fd1498Szrj corresponds to a class that is a descendant of the base class
1068*38fd1498Szrj (possibly the base class itself). */
1069*38fd1498Szrj
1070*38fd1498Szrj static void
add_hierarchy_pair(struct vtv_graph_node * base_node,struct vtv_graph_node * derived_node)1071*38fd1498Szrj add_hierarchy_pair (struct vtv_graph_node *base_node,
1072*38fd1498Szrj struct vtv_graph_node *derived_node)
1073*38fd1498Szrj {
1074*38fd1498Szrj (base_node->children).safe_push (derived_node);
1075*38fd1498Szrj (derived_node->parents).safe_push (base_node);
1076*38fd1498Szrj }
1077*38fd1498Szrj
1078*38fd1498Szrj /* This functions adds a new base class/derived class relationship to
1079*38fd1498Szrj our class hierarchy data structure. Both parameters are trees
1080*38fd1498Szrj representing the class types, i.e. RECORD_TYPE trees.
1081*38fd1498Szrj DERIVED_CLASS can be the same as BASE_CLASS. */
1082*38fd1498Szrj
1083*38fd1498Szrj static void
update_class_hierarchy_information(tree base_class,tree derived_class)1084*38fd1498Szrj update_class_hierarchy_information (tree base_class,
1085*38fd1498Szrj tree derived_class)
1086*38fd1498Szrj {
1087*38fd1498Szrj struct vtv_graph_node *base_node = find_graph_node (base_class);
1088*38fd1498Szrj struct vtv_graph_node *derived_node = find_graph_node (derived_class);
1089*38fd1498Szrj
1090*38fd1498Szrj add_hierarchy_pair (base_node, derived_node);
1091*38fd1498Szrj }
1092*38fd1498Szrj
1093*38fd1498Szrj
1094*38fd1498Szrj static void
write_out_vtv_count_data(void)1095*38fd1498Szrj write_out_vtv_count_data (void)
1096*38fd1498Szrj {
1097*38fd1498Szrj static int vtv_count_log_fd = -1;
1098*38fd1498Szrj char buffer[1024];
1099*38fd1498Szrj int unused_vtbl_map_vars = 0;
1100*38fd1498Szrj int bytes_written __attribute__ ((unused));
1101*38fd1498Szrj char *file_name = get_log_file_name ("vtv_count_data.log");
1102*38fd1498Szrj
1103*38fd1498Szrj if (vtv_count_log_fd == -1)
1104*38fd1498Szrj vtv_count_log_fd = open (file_name,
1105*38fd1498Szrj O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
1106*38fd1498Szrj if (vtv_count_log_fd == -1)
1107*38fd1498Szrj {
1108*38fd1498Szrj warning_at (UNKNOWN_LOCATION, 0,
1109*38fd1498Szrj "unable to open log file %<vtv_count_data.log%>: %m");
1110*38fd1498Szrj return;
1111*38fd1498Szrj }
1112*38fd1498Szrj
1113*38fd1498Szrj for (unsigned i = 0; i < num_vtable_map_nodes; ++i)
1114*38fd1498Szrj {
1115*38fd1498Szrj struct vtbl_map_node *current = vtbl_map_nodes_vec[i];
1116*38fd1498Szrj if (!current->is_used
1117*38fd1498Szrj && current->registered->size() == 0)
1118*38fd1498Szrj unused_vtbl_map_vars++;
1119*38fd1498Szrj }
1120*38fd1498Szrj
1121*38fd1498Szrj snprintf (buffer, sizeof (buffer), "%s %d %d %d %d %d\n",
1122*38fd1498Szrj main_input_filename, total_num_virtual_calls,
1123*38fd1498Szrj total_num_verified_vcalls, num_calls_to_regset,
1124*38fd1498Szrj num_calls_to_regpair, unused_vtbl_map_vars);
1125*38fd1498Szrj
1126*38fd1498Szrj bytes_written = write (vtv_count_log_fd, buffer, strlen (buffer));
1127*38fd1498Szrj }
1128*38fd1498Szrj
1129*38fd1498Szrj /* This function calls register_all_pairs, which actually generates
1130*38fd1498Szrj all the calls to __VLTRegisterPair (in the verification constructor
1131*38fd1498Szrj init function). It also generates the calls to
1132*38fd1498Szrj __VLTChangePermission, if the verification constructor init
1133*38fd1498Szrj function is going into the preinit array. INIT_ROUTINE_BODY is
1134*38fd1498Szrj the body of our constructior initialization function, to which we
1135*38fd1498Szrj add our function calls.*/
1136*38fd1498Szrj
1137*38fd1498Szrj bool
vtv_register_class_hierarchy_information(tree init_routine_body)1138*38fd1498Szrj vtv_register_class_hierarchy_information (tree init_routine_body)
1139*38fd1498Szrj {
1140*38fd1498Szrj bool registered_something = false;
1141*38fd1498Szrj
1142*38fd1498Szrj init_functions ();
1143*38fd1498Szrj
1144*38fd1498Szrj if (num_vtable_map_nodes == 0)
1145*38fd1498Szrj return false;
1146*38fd1498Szrj
1147*38fd1498Szrj /* Add class hierarchy pairs to the vtable map data structure. */
1148*38fd1498Szrj registered_something = register_all_pairs (init_routine_body);
1149*38fd1498Szrj
1150*38fd1498Szrj if (flag_vtv_counts)
1151*38fd1498Szrj write_out_vtv_count_data ();
1152*38fd1498Szrj
1153*38fd1498Szrj return registered_something;
1154*38fd1498Szrj }
1155*38fd1498Szrj
1156*38fd1498Szrj
1157*38fd1498Szrj /* Generate the special constructor function that calls
1158*38fd1498Szrj __VLTChangePermission and __VLTRegisterPairs, and give it a very
1159*38fd1498Szrj high initialization priority. */
1160*38fd1498Szrj
1161*38fd1498Szrj void
vtv_generate_init_routine(void)1162*38fd1498Szrj vtv_generate_init_routine (void)
1163*38fd1498Szrj {
1164*38fd1498Szrj tree init_routine_body;
1165*38fd1498Szrj bool vtable_classes_found = false;
1166*38fd1498Szrj
1167*38fd1498Szrj push_lang_context (lang_name_c);
1168*38fd1498Szrj
1169*38fd1498Szrj /* The priority for this init function (constructor) is carefully
1170*38fd1498Szrj chosen so that it will happen after the calls to unprotect the
1171*38fd1498Szrj memory used for vtable verification and before the memory is
1172*38fd1498Szrj protected again. */
1173*38fd1498Szrj init_routine_body = vtv_start_verification_constructor_init_function ();
1174*38fd1498Szrj
1175*38fd1498Szrj vtable_classes_found =
1176*38fd1498Szrj vtv_register_class_hierarchy_information (init_routine_body);
1177*38fd1498Szrj
1178*38fd1498Szrj if (vtable_classes_found)
1179*38fd1498Szrj {
1180*38fd1498Szrj tree vtv_fndecl =
1181*38fd1498Szrj vtv_finish_verification_constructor_init_function (init_routine_body);
1182*38fd1498Szrj TREE_STATIC (vtv_fndecl) = 1;
1183*38fd1498Szrj TREE_USED (vtv_fndecl) = 1;
1184*38fd1498Szrj DECL_PRESERVE_P (vtv_fndecl) = 1;
1185*38fd1498Szrj /* We are running too late to generate any meaningful debug information
1186*38fd1498Szrj for this routine. */
1187*38fd1498Szrj DECL_IGNORED_P (vtv_fndecl) = 1;
1188*38fd1498Szrj if (flag_vtable_verify == VTV_PREINIT_PRIORITY && !TARGET_PECOFF)
1189*38fd1498Szrj DECL_STATIC_CONSTRUCTOR (vtv_fndecl) = 0;
1190*38fd1498Szrj
1191*38fd1498Szrj gimplify_function_tree (vtv_fndecl);
1192*38fd1498Szrj cgraph_node::add_new_function (vtv_fndecl, false);
1193*38fd1498Szrj
1194*38fd1498Szrj symtab->process_new_functions ();
1195*38fd1498Szrj
1196*38fd1498Szrj if (flag_vtable_verify == VTV_PREINIT_PRIORITY && !TARGET_PECOFF)
1197*38fd1498Szrj assemble_vtv_preinit_initializer (vtv_fndecl);
1198*38fd1498Szrj
1199*38fd1498Szrj }
1200*38fd1498Szrj pop_lang_context ();
1201*38fd1498Szrj }
1202*38fd1498Szrj
1203*38fd1498Szrj /* This funtion takes a tree containing a class type (BASE_TYPE), and
1204*38fd1498Szrj it either finds the existing vtbl_map_node for that class in our
1205*38fd1498Szrj data structure, or it creates a new node and adds it to the data
1206*38fd1498Szrj structure if there is not one for the class already. As part of
1207*38fd1498Szrj this process it also creates the global vtable map variable for the
1208*38fd1498Szrj class. */
1209*38fd1498Szrj
1210*38fd1498Szrj struct vtbl_map_node *
vtable_find_or_create_map_decl(tree base_type)1211*38fd1498Szrj vtable_find_or_create_map_decl (tree base_type)
1212*38fd1498Szrj {
1213*38fd1498Szrj char *var_name = NULL;
1214*38fd1498Szrj struct vtbl_map_node *vtable_map_node = NULL;
1215*38fd1498Szrj
1216*38fd1498Szrj /* Verify the type has an associated vtable. */
1217*38fd1498Szrj if (!TYPE_BINFO (base_type) || !BINFO_VTABLE (TYPE_BINFO (base_type)))
1218*38fd1498Szrj return NULL;
1219*38fd1498Szrj
1220*38fd1498Szrj /* Create map lookup symbol for base class */
1221*38fd1498Szrj var_name = get_mangled_vtable_map_var_name (base_type);
1222*38fd1498Szrj
1223*38fd1498Szrj /* We've already created the variable; just look it. */
1224*38fd1498Szrj vtable_map_node = vtbl_map_get_node (TYPE_MAIN_VARIANT (base_type));
1225*38fd1498Szrj
1226*38fd1498Szrj if (!vtable_map_node || (vtable_map_node->vtbl_map_decl == NULL_TREE))
1227*38fd1498Szrj {
1228*38fd1498Szrj /* If we haven't already created the *__vtable_map global
1229*38fd1498Szrj variable for this class, do so now, and add it to the
1230*38fd1498Szrj varpool, to make sure it gets saved and written out. */
1231*38fd1498Szrj
1232*38fd1498Szrj tree var_decl = NULL;
1233*38fd1498Szrj tree var_type = build_pointer_type (void_type_node);
1234*38fd1498Szrj tree initial_value = integer_zero_node;
1235*38fd1498Szrj
1236*38fd1498Szrj var_decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
1237*38fd1498Szrj get_identifier (var_name), var_type);
1238*38fd1498Szrj
1239*38fd1498Szrj DECL_EXTERNAL (var_decl) = 0;
1240*38fd1498Szrj TREE_STATIC (var_decl) = 1;
1241*38fd1498Szrj DECL_VISIBILITY (var_decl) = VISIBILITY_HIDDEN;
1242*38fd1498Szrj SET_DECL_ASSEMBLER_NAME (var_decl, get_identifier (var_name));
1243*38fd1498Szrj DECL_ARTIFICIAL (var_decl) = 1;
1244*38fd1498Szrj /* We cannot mark this variable as read-only because we want to be
1245*38fd1498Szrj able to write to it at runtime. */
1246*38fd1498Szrj TREE_READONLY (var_decl) = 0;
1247*38fd1498Szrj DECL_IGNORED_P (var_decl) = 1;
1248*38fd1498Szrj DECL_PRESERVE_P (var_decl) = 1;
1249*38fd1498Szrj
1250*38fd1498Szrj /* Put these mmap variables in thr .vtable_map_vars section, so
1251*38fd1498Szrj we can find and protect them. */
1252*38fd1498Szrj
1253*38fd1498Szrj set_decl_section_name (var_decl, ".vtable_map_vars");
1254*38fd1498Szrj symtab_node::get (var_decl)->implicit_section = true;
1255*38fd1498Szrj DECL_INITIAL (var_decl) = initial_value;
1256*38fd1498Szrj
1257*38fd1498Szrj comdat_linkage (var_decl);
1258*38fd1498Szrj
1259*38fd1498Szrj varpool_node::finalize_decl (var_decl);
1260*38fd1498Szrj if (!vtable_map_node)
1261*38fd1498Szrj vtable_map_node =
1262*38fd1498Szrj find_or_create_vtbl_map_node (TYPE_MAIN_VARIANT (base_type));
1263*38fd1498Szrj if (vtable_map_node->vtbl_map_decl == NULL_TREE)
1264*38fd1498Szrj vtable_map_node->vtbl_map_decl = var_decl;
1265*38fd1498Szrj }
1266*38fd1498Szrj
1267*38fd1498Szrj gcc_assert (vtable_map_node);
1268*38fd1498Szrj return vtable_map_node;
1269*38fd1498Szrj }
1270*38fd1498Szrj
1271*38fd1498Szrj /* This function is used to build up our class hierarchy data for a
1272*38fd1498Szrj particular class. TYPE is the record_type tree node for the
1273*38fd1498Szrj class. */
1274*38fd1498Szrj
1275*38fd1498Szrj static void
vtv_insert_single_class_info(tree type)1276*38fd1498Szrj vtv_insert_single_class_info (tree type)
1277*38fd1498Szrj {
1278*38fd1498Szrj if (flag_vtable_verify)
1279*38fd1498Szrj {
1280*38fd1498Szrj tree binfo = TYPE_BINFO (type);
1281*38fd1498Szrj tree base_binfo;
1282*38fd1498Szrj struct vtbl_map_node *own_map;
1283*38fd1498Szrj int i;
1284*38fd1498Szrj
1285*38fd1498Szrj /* First make sure to create the map for this record type. */
1286*38fd1498Szrj own_map = vtable_find_or_create_map_decl (type);
1287*38fd1498Szrj if (own_map == NULL)
1288*38fd1498Szrj return;
1289*38fd1498Szrj
1290*38fd1498Szrj /* Go through the list of all base classes for the current
1291*38fd1498Szrj (derived) type, make sure the *__vtable_map global variable
1292*38fd1498Szrj for the base class exists, and add the base class/derived
1293*38fd1498Szrj class pair to the class hierarchy information we are
1294*38fd1498Szrj accumulating (for vtable pointer verification). */
1295*38fd1498Szrj for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1296*38fd1498Szrj {
1297*38fd1498Szrj tree tree_val = BINFO_TYPE (base_binfo);
1298*38fd1498Szrj struct vtbl_map_node *vtable_map_node = NULL;
1299*38fd1498Szrj
1300*38fd1498Szrj vtable_map_node = vtable_find_or_create_map_decl (tree_val);
1301*38fd1498Szrj
1302*38fd1498Szrj if (vtable_map_node != NULL)
1303*38fd1498Szrj update_class_hierarchy_information (tree_val, type);
1304*38fd1498Szrj }
1305*38fd1498Szrj }
1306*38fd1498Szrj }
1307*38fd1498Szrj
1308*38fd1498Szrj /* This function adds classes we are interested in to a list of
1309*38fd1498Szrj classes. RECORD is the record_type node for the class we are
1310*38fd1498Szrj adding to the list. */
1311*38fd1498Szrj
1312*38fd1498Szrj void
vtv_save_class_info(tree record)1313*38fd1498Szrj vtv_save_class_info (tree record)
1314*38fd1498Szrj {
1315*38fd1498Szrj if (!flag_vtable_verify || TREE_CODE (record) == UNION_TYPE)
1316*38fd1498Szrj return;
1317*38fd1498Szrj
1318*38fd1498Szrj if (!vlt_saved_class_info)
1319*38fd1498Szrj vec_alloc (vlt_saved_class_info, 10);
1320*38fd1498Szrj
1321*38fd1498Szrj gcc_assert (TREE_CODE (record) == RECORD_TYPE);
1322*38fd1498Szrj
1323*38fd1498Szrj vec_safe_push (vlt_saved_class_info, record);
1324*38fd1498Szrj }
1325*38fd1498Szrj
1326*38fd1498Szrj
1327*38fd1498Szrj /* This function goes through the list of classes we saved and calls
1328*38fd1498Szrj vtv_insert_single_class_info on each one, to build up our class
1329*38fd1498Szrj hierarchy data structure. */
1330*38fd1498Szrj
1331*38fd1498Szrj void
vtv_recover_class_info(void)1332*38fd1498Szrj vtv_recover_class_info (void)
1333*38fd1498Szrj {
1334*38fd1498Szrj tree current_class;
1335*38fd1498Szrj unsigned i;
1336*38fd1498Szrj
1337*38fd1498Szrj if (vlt_saved_class_info)
1338*38fd1498Szrj {
1339*38fd1498Szrj for (i = 0; i < vlt_saved_class_info->length(); ++i)
1340*38fd1498Szrj {
1341*38fd1498Szrj current_class = (*vlt_saved_class_info)[i];
1342*38fd1498Szrj gcc_assert (TREE_CODE (current_class) == RECORD_TYPE);
1343*38fd1498Szrj vtv_insert_single_class_info (current_class);
1344*38fd1498Szrj }
1345*38fd1498Szrj }
1346*38fd1498Szrj }
1347*38fd1498Szrj
1348*38fd1498Szrj #include "gt-cp-vtable-class-hierarchy.h"
1349