aboutsummaryrefslogtreecommitdiff
path: root/vtable-security/gcc/cp/vtable-class-hierarchy.c
blob: 958201cbf72180508fafec9a9720492240978212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/* Copyright (C) 2012  Free Software Foundation, Inc.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

/* This file is part of the vtable security implementation.  It collects
   class hierarchy information about the program being compiled and
   inserts calls to __VLTRegisterPair, registering this information.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "timevar.h"
#include "cpplib.h"
#include "tree.h"
#include "cp-tree.h"
#include "intl.h"
#include "c-family/c-pragma.h"
#include "decl.h"
#include "flags.h"
#include "diagnostic-core.h"
#include "output.h"
#include "target.h"
#include "cgraph.h"
#include "c-family/c-common.h"
#include "c-family/c-objc.h"
#include "plugin.h"
#include "tree-threadsafe-analyze.h"
#include "tree-iterator.h"

static GTY(()) tree vlt_register_pairs_fndecl = NULL_TREE;
static GTY(()) tree vlt_change_permission_fndecl = NULL_TREE;

struct list_node {
  tree class_type;
  struct list_node *next;
};

struct node {
  tree ptr_decl;
  struct list_node *class_list;
  struct node *left;
  struct node *right;
};

struct node2 {
  tree base_map_var_decl;
  tree vtable_decl;
  unsigned offset;
  struct node2 *left;
  struct node2 *right;
};

static void init_functions (void);
static void linked_list_insert (struct list_node **, tree);
static void binary_tree_insert (struct node **, tree, tree, tree);
static struct node *binary_tree_find (struct node *, tree);
static tree vtable_find_map_decl (tree var_id);
static void dump_class_hierarchy_information (struct node *root);
static void register_all_pairs (struct node *root, tree body);

static struct node *vlt_class_hierarchy_info = NULL;
static struct node2 *registered_pairs = NULL;


static void
init_functions (void)
{
  tree void_ptr_type = build_pointer_type (void_type_node);
  tree arg_types = NULL_TREE;
  tree register_pairs_type = void_ptr_type;
  tree change_permission_type = void_ptr_type;
  tree char_ptr_type = build_pointer_type (char_type_node);

  arg_types = build_tree_list (NULL_TREE, char_ptr_type);
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, integer_type_node));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, void_type_node));

  change_permission_type = build_function_type (change_permission_type,
                                                arg_types);
  vlt_change_permission_fndecl = build_fn_decl ("__VLTChangePermission",
                                                change_permission_type);
  TREE_NOTHROW (vlt_change_permission_fndecl) = 1;
  DECL_ATTRIBUTES (vlt_change_permission_fndecl) =
                    tree_cons (get_identifier ("leaf"), NULL,
                               DECL_ATTRIBUTES (vlt_change_permission_fndecl));
  TREE_PUBLIC (vlt_change_permission_fndecl) = 1;
  DECL_PRESERVE_P (vlt_change_permission_fndecl) = 1;
  retrofit_lang_decl (vlt_change_permission_fndecl);
  SET_DECL_LANGUAGE (vlt_change_permission_fndecl, lang_cplusplus);

  arg_types = build_tree_list (NULL_TREE, build_pointer_type (void_ptr_type));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, void_ptr_type));
  /* Start: Arg types to be removed when we remove debugging parameters from
     the library function. */
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, char_ptr_type));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, integer_type_node));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, char_ptr_type));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, integer_type_node));
  /* End: Arg types to be removed...*/
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, void_type_node));

  register_pairs_type = build_function_type (register_pairs_type, arg_types);
  vlt_register_pairs_fndecl = build_fn_decl ("__VLTRegisterPair",
                                             register_pairs_type);
  TREE_NOTHROW (vlt_register_pairs_fndecl) = 1;
  DECL_ATTRIBUTES (vlt_register_pairs_fndecl) =
                    tree_cons (get_identifier ("leaf"), NULL,
                               DECL_ATTRIBUTES (vlt_register_pairs_fndecl));
  TREE_PUBLIC (vlt_register_pairs_fndecl) = 1;
  DECL_PRESERVE_P (vlt_register_pairs_fndecl) = 1;
  retrofit_lang_decl (vlt_register_pairs_fndecl);
  SET_DECL_LANGUAGE (vlt_register_pairs_fndecl, lang_cplusplus);
}

static void
dump_class_hierarchy_information (struct node *root)
{
  struct list_node *current;

  /* Dump current node */
  if (!root || ! root->ptr_decl)
    return;

  fprintf (stdout, "Base class '%s' is inherited by: ",
           IDENTIFIER_POINTER (DECL_NAME (root->ptr_decl)));

  current = root->class_list;
  while (current)
    {
      if (current->class_type)
	fprintf (stdout, " (%s, %s)",
		 IDENTIFIER_POINTER (DECL_NAME
				     (TYPE_NAME (current->class_type))),
                 IDENTIFIER_POINTER (get_mangled_id(TREE_CHAIN(current->class_type))));
      current = current->next;
    }
  fprintf (stdout, "\n");

  /* Dump left child */
  dump_class_hierarchy_information (root->left);

  /* Dump right child */
  dump_class_hierarchy_information (root->right);
}

static void
list_append (struct list_node *old_list, struct list_node *new_list)
{
  struct list_node *old_cur;
  struct list_node *old_end;
  struct list_node *new_cur;
  struct list_node *new_node;

  /* Append to the old list anything in the new list that isn't
     already there.*/

  /* Find the end of the old list */
  old_end = old_list;
  while (old_end->next != NULL)
    old_end = old_end->next;

  /* Look at each elemement in new list */
  for (new_cur = new_list; new_cur; new_cur = new_cur->next)
    {
      /* Look for 'new_cur' in the old list.  */
      bool found = false;

      for (old_cur = old_list; old_cur && !found; old_cur = old_cur->next)
        if (get_mangled_id (TREE_CHAIN (new_cur->class_type))
            == get_mangled_id (TREE_CHAIN (old_cur->class_type)))
          found = true;

      /* if not found, copy new node and append to end of old list */
      if (!found)
        {
          new_node = (struct list_node *) xmalloc (sizeof (struct list_node));
          new_node->class_type = new_cur->class_type;
          new_node->next = NULL;

          old_end->next = new_node;
          old_end = old_end->next;
        }
    }

}

static struct node *
binary_tree_find (struct node *root, tree var_decl)
{
  if (!root)
    return NULL;

  if (DECL_NAME (root->ptr_decl) == DECL_NAME (var_decl))
    return root;

  else if (var_decl < root->ptr_decl)
    return binary_tree_find (root->left, var_decl);
  else
    return binary_tree_find (root->right, var_decl);
}

static bool
build_transitive_closure (struct node *root, struct node *cur_node)
{
  bool current_changed = false;
  bool left_changed = false;
  bool right_changed = false;
  struct list_node *cur_list;
  char * cur_node_name;
  char *cptr;

  if (!cur_node)
    return false;

  /* DEBUG
  fprintf(stderr, "build_transitive_closure: cur_node: ");
  debug_c_tree(cur_node->ptr_decl);
  */

  cur_node_name = xstrdup (IDENTIFIER_POINTER
			         (DECL_NAME
                                    (cur_node->ptr_decl)));
  cptr = strstr (cur_node_name, ".vtable_map");
  if (cptr)
    cptr[0] = '\0';
  /* Handle current tree node */
  for (cur_list = cur_node->class_list; cur_list; cur_list = cur_list->next)
    {
      const char *cur_list_name = NULL;
      char *var_id_name = NULL;
      tree var_id = NULL_TREE;
      tree var_decl = NULL_TREE;
      struct node *tmp_node = NULL;
      tree vtbl_var_decl;

      /* DEBUG
      fprintf(stderr, "build_transitive_closure: cur_list:");
      debug_c_tree(cur_list->class_type);
      */

      if ((! cur_list->class_type)
          || (! TYPE_BINFO (cur_list->class_type))
          || (! BINFO_VTABLE (TYPE_BINFO (cur_list->class_type))))
        continue;

      vtbl_var_decl = TREE_OPERAND
          (TREE_OPERAND
           (BINFO_VTABLE
            (TYPE_BINFO
             (cur_list->class_type)), 0), 0);

      if (!vtbl_var_decl)
        continue;
      cur_list_name = (const char *) IDENTIFIER_POINTER
          (DECL_NAME (vtbl_var_decl));

      if (!cur_list_name)
        continue;

      /* TODO: avoid comparing by name */
      if (strcmp (cur_list_name, cur_node_name) == 0)
        continue;

      var_id_name = (char *) xmalloc (strlen (cur_list_name) + 12);
      sprintf (var_id_name, "%s.vtable_map", cur_list_name);
      var_id = get_identifier (var_id_name);
      free (var_id_name);

      var_decl = vtable_find_map_decl (var_id);
      if (!var_decl)
        continue;

      tmp_node = binary_tree_find (root, var_decl);

      if (!tmp_node)
        continue;
      else
        {
          list_append (cur_node->class_list, tmp_node->class_list);
          current_changed = true;
        }
    }

  free(cur_node_name);

  /* Handle left child */
  left_changed = build_transitive_closure (root, cur_node->left);

  /* Handle right child */
  right_changed = build_transitive_closure (root, cur_node->right);

  return (current_changed || left_changed || right_changed);
}


void
vtv_compute_class_hierarchy_transitive_closure (void)
{
  build_transitive_closure (vlt_class_hierarchy_info,
			    vlt_class_hierarchy_info);
}

static bool
tree_three_key_insert (struct node2 **root, tree key1, tree key2, unsigned key3)
{
  /* In "struct node2", base_map_var_decl is the primary sort key (the base
     class .vtable_map variable decl), and vtable_decl is the secondary sort
     key (the var decl the vtable). The third key is the offset added to the
     vtable to get the actual recorded vtable pointer address.  */

  struct node2 *new_node;

  if (!(*root))
    {
      new_node = (struct node2 *) xmalloc (sizeof (struct node2));
      new_node->base_map_var_decl = key1;
      new_node->vtable_decl = key2;
      new_node->offset = key3;
      new_node->left = NULL;
      new_node->right = NULL;
      (*root) = new_node;
      return false;
    }
  else if ((*root)->base_map_var_decl == key1)
    {
      if ((*root)->vtable_decl == key2)
        {
          if ((*root)->offset == key3)
            return true;
          else if (key3 < (*root)->offset)
            return tree_three_key_insert (&((*root)->left), key1, key2, key3);
          else if (key3 > (*root)->offset)
            return tree_three_key_insert (&((*root)->right), key1, key2, key3);
        }
      else if (key2 < (*root)->vtable_decl)
        return tree_three_key_insert (&((*root)->left), key1, key2, key3);
      else if (key2 > (*root)->vtable_decl)
        return tree_three_key_insert (&((*root)->right), key1, key2, key3);
    }
  else if (key1 < (*root)->base_map_var_decl)
    return tree_three_key_insert (&((*root)->left), key1, key2, key3);
  else if (key1 > (*root)->base_map_var_decl)
    return tree_three_key_insert (&((*root)->right), key1, key2, key3);

  return false;
}

static bool
record_register_pairs (tree base_ptr_decl, tree vtable_decl, tree vptr_address)
{
  unsigned offset = TREE_INT_CST_LOW (TREE_OPERAND (vptr_address, 1));
  return tree_three_key_insert (&registered_pairs, base_ptr_decl, vtable_decl, offset);
}

static void
register_vptr_fields (tree base_class_decl_arg, tree record_type, tree body)
{
  /* A class may contain secondary vtables in it, for various
     reasons.  This function goes through the decl chain of a class
     record looking for any fields that point to secondary vtables,
     and adding calls to __VLTRegisterPair for the secondary vtable
     pointers.  */

  tree vtbl_var_decl;
  tree arg1;
  tree arg2;

  if (TREE_CODE (record_type) != RECORD_TYPE)
    return;

  vtbl_var_decl = get_vtbl_decl_for_binfo (TYPE_BINFO (record_type));

  if (vtbl_var_decl)
    {
      tree ztt_decl = DECL_CHAIN (vtbl_var_decl);
      bool already_registered = false;

      /* construction vtable */
      if (ztt_decl != NULL_TREE
          && (DECL_NAME (ztt_decl))
          && (strncmp (IDENTIFIER_POINTER (DECL_NAME (ztt_decl)),
                       "_ZTT", 4) == 0))
        {
          tree values = DECL_INITIAL (ztt_decl);
          struct varpool_node * vp_node = varpool_node (ztt_decl);
          if ( vp_node->needed && vp_node->finalized 
	       && (values != NULL_TREE)
              && (TREE_CODE (values) == CONSTRUCTOR)
              && (TREE_CODE (TREE_TYPE (values)) == ARRAY_TYPE))
            {
              tree call_expr = NULL_TREE;
              unsigned HOST_WIDE_INT cnt;
              constructor_elt *ce;

              for (cnt = 0;
                   VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (values),
				cnt, ce);
                   cnt++)
                {
                  tree value = ce->value;
                  tree val_vtbl_decl = TREE_OPERAND (TREE_OPERAND (value, 0), 0);
                  int len1 = strlen (IDENTIFIER_POINTER
				     (DECL_NAME
				          (TREE_OPERAND
					     (base_class_decl_arg, 0))));
                  int len2 = strlen (IDENTIFIER_POINTER
				     (DECL_NAME (val_vtbl_decl)));
                  arg1 = build_string_literal (len1,
                                               IDENTIFIER_POINTER
					        (DECL_NAME
						 (TREE_OPERAND
						  (base_class_decl_arg, 0))));
                  arg2 = build_string_literal (len2,
                                               IDENTIFIER_POINTER
					        (DECL_NAME (val_vtbl_decl)));

                  already_registered = record_register_pairs (TREE_OPERAND
                                                               (base_class_decl_arg, 0),
                                                              val_vtbl_decl,
                                                              value);

                  if (already_registered)
                    continue;

		  /* This call expr has the 2 "real" arguments, plus 4
		     debugging arguments.  Eventually it will be
		     replaced with the one just below it, which only
		     has the 2 real arguments.  */
                  call_expr = build_call_expr
		                        (vlt_register_pairs_fndecl, 6,
					 base_class_decl_arg, value,
					 arg1,
					 build_int_cst (integer_type_node,
							len1),
					 arg2,
					 build_int_cst (integer_type_node,
							len2));
		  /* See comments above.
                  call_expr = build_call_expr (vlt_register_pairs_fndecl, 2,
                                               base_class_decl_arg, value);
		  */
		  append_to_statement_list (call_expr, &body);
                }
            }
        }
    }
}

static void
register_other_binfo_vtables (tree binfo, tree body, tree arg1, tree str1,
                              int len1, tree str2, int len2)
{
  unsigned ix;
  tree base_binfo;
  tree vtable_decl;
  bool already_registered;

  if (binfo == NULL_TREE)
    return;

  for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
    {
      if ((!BINFO_PRIMARY_P (base_binfo)
           || BINFO_VIRTUAL_P (base_binfo))
          && (vtable_decl=get_vtbl_decl_for_binfo (base_binfo))
          && !(DECL_VTABLE_OR_VTT_P(vtable_decl) && DECL_CONSTRUCTION_VTABLE_P(vtable_decl)))
        {
          tree vtable_address = build_vtbl_address (base_binfo);
          tree call_expr;

          already_registered = record_register_pairs (TREE_OPERAND (arg1, 0),
                                                      vtable_decl,
                                                      vtable_address);
          if (!already_registered)
            {
              call_expr = build_call_expr (vlt_register_pairs_fndecl, 6,
                                            arg1, vtable_address,
                                            str1,
                                            build_int_cst (integer_type_node,
                                                           len1),
                                            str2,
                                            build_int_cst (integer_type_node,
                                                           len2));
              append_to_statement_list (call_expr, &body);
            }
        }

      register_other_binfo_vtables (base_binfo, body, arg1, str1, len1, str2,
                                    len2);
    }
}

static void
register_all_pairs (struct node *root, tree body)
{
  struct list_node *current;
  tree base_ptr_var_decl;

  /* Handle current node */
  if (!root || ! root->ptr_decl)
    return;

  base_ptr_var_decl = root->ptr_decl;

  current = root->class_list;
  while (current)
    {
      if (current->class_type
          && (TREE_CODE (current->class_type) == RECORD_TYPE))
        {
          tree new_type;
          tree arg1;
          tree call_expr;
          bool already_registered;

          tree binfo = TYPE_BINFO (current->class_type);
	  tree vtable = NULL_TREE;
          tree vtable_decl;
          bool vtable_should_be_output = false;
          bool debug = false;

	  if (binfo)
	    vtable = BINFO_VTABLE (binfo);

          if (debug)
          {
            fprintf(stderr, "register_all_pairs: looking at class:\n");
            debug_tree(current->class_type);
          }
          
          vtable_decl = CLASSTYPE_VTABLES (current->class_type);

          /* Handle main vtable for this class. */
          if (vtable_decl)
            {
              struct varpool_node *node = varpool_node (vtable_decl);
              if (debug)
              {
                fprintf(stderr,"Varpool node:\n");
                debug_varpool_node(node);
              }
              vtable_should_be_output = node->needed; 
            }

          if (debug)
          {
            fprintf(stderr, "register_all_pairs: the vtable is (should be output=%s):\n", vtable_should_be_output ? "yes": "no");
            debug_tree(vtable_decl);
          }

          if (vtable_decl && vtable_should_be_output && BINFO_VTABLE (binfo))
            {
              tree vtable_address = build_vtbl_address (binfo);
              int len1  = IDENTIFIER_LENGTH (DECL_NAME (base_ptr_var_decl));
              int len2  = IDENTIFIER_LENGTH (DECL_NAME (vtable_decl));
              tree str1 = build_string_literal (len1,
                                                IDENTIFIER_POINTER
                                                  (DECL_NAME
                                                     (base_ptr_var_decl)));
              tree str2 = build_string_literal (len2,
                                                IDENTIFIER_POINTER
                                                  (DECL_NAME (vtable_decl)));

              already_registered = record_register_pairs (base_ptr_var_decl,
                                                          vtable_decl,
                                                          vtable_address);

              if (!already_registered)
                {
                  if (debug)
                  {
                    fprintf(stderr, "register_all_pairs: building register pair call for vtable:\n");
                    debug_tree(vtable_decl);
                  }

                  new_type = build_pointer_type (TREE_TYPE
                                                   (base_ptr_var_decl));
                  arg1 = build1 (ADDR_EXPR, new_type, base_ptr_var_decl);

                  /* This call expr has the 2 "real" arguments, plus 4
                     debugging arguments.  Eventually it will be replaced
                     with the one just below it, which only has the 2 real
                     arguments.  */
                  call_expr = build_call_expr
                      (vlt_register_pairs_fndecl, 6,
                       arg1, vtable_address,
                       str1, build_int_cst (integer_type_node,
                                            len1),
                       str2,  build_int_cst (integer_type_node,
                                             len2));
                  /* See comments above.  call_expr = build_call_expr
                     (vlt_register_pairs_fndecl, 2, arg1, vtable);  */

                  append_to_statement_list (call_expr, &body);

                  /* Find and handle any 'extra' vtables associated
                     with this class, via virtual inheritance.   */
                  register_vptr_fields (arg1, current->class_type, body);

                  /* Find and handle any 'extra' vtables associated
                     with this class, via multiple inheritance.   */
                  register_other_binfo_vtables (binfo, body, arg1, str1, len1,
                                                str2, len2);
                }
            } /* if vtable_decl && vtable_should_be_output */
        }
      current = current->next;

    } /* while there's a node in the linked list */

  /* Handle left child */
  register_all_pairs (root->left, body);

  /* Handle right child */
  register_all_pairs (root->right, body);
}

static void
linked_list_insert (struct list_node **root, tree new_class)
{
  struct list_node *current;
  struct list_node *prev;
  bool found = false;

  for (prev = NULL, current = (*root); 
       current && !found; 
       prev = current, current = current->next)
    if (current 
	&& current->class_type == new_class)
      found = true;

  if (!found)
    {
      struct list_node *new_node = (struct list_node *)
                                           xmalloc (sizeof (struct list_node));

      new_node->class_type = new_class;
      new_node->next = NULL;

      if (!prev)
	(*root) = new_node;
      else
	prev->next = new_node;
    }
}

static void
binary_tree_insert (struct node **root, tree ptr_decl, tree base_class,
                    tree new_class)
{
  /* DEBUG
  fprintf(stderr, "binary_tree_insert: base_class: \n");
  debug_tree(base_class);
  fprintf(stderr, "binary_tree_insert, derived_class: \n");
  debug_tree(new_class);
  */

  if (!(*root))
    {
      struct node *new_node = (struct node *) xmalloc (sizeof (struct node));
      new_node->ptr_decl = ptr_decl;
      new_node->left = NULL;
      new_node->right = NULL;
      new_node->class_list = NULL;
      linked_list_insert (&(new_node->class_list), base_class);
      linked_list_insert (&(new_node->class_list), new_class);
      (*root) = new_node;
    }
  else if ((DECL_NAME ((*root)->ptr_decl)) == (DECL_NAME (ptr_decl)))
    linked_list_insert (&((*root)->class_list), new_class);
  else if (ptr_decl < (*root)->ptr_decl)
    binary_tree_insert (&((*root)->left), ptr_decl, base_class, new_class);
  else
    binary_tree_insert (&((*root)->right), ptr_decl, base_class, new_class);
}

static void
update_class_hierarchy_information (tree base_class_ptr_decl,
                                    tree base_class,
                                    tree derived_class)
{
  binary_tree_insert (&vlt_class_hierarchy_info, base_class_ptr_decl,
		      base_class, derived_class);
}

bool
vtv_register_class_hierarchy_information (tree body)
{
  tree call_expr;
  tree arg;
  tree arg2;
  bool ret_val = false;

  init_functions ();

  /* DEBUG */
  if (false)  /* This is here for debugging purposes. */
    dump_class_hierarchy_information (vlt_class_hierarchy_info);

  if (vlt_class_hierarchy_info != NULL)
    {
      /* Set permissions on vtable map data structure to be Read/Write. */

      arg = build_string_literal (strlen ("rw"), "rw");
      arg2 = build_int_cst (integer_type_node, 2);
      call_expr = build_call_expr (vlt_change_permission_fndecl, 2, arg, arg2);
      append_to_statement_list (call_expr, &body);

      /* Add class hierarchy pairs to the vtable map data structure. */

      /* compute_hierarchy_transitive_closure (); */
      register_all_pairs (vlt_class_hierarchy_info, body);

      /* Set permission on vtable map data structure to be Read-only.  */

      arg = build_string_literal (strlen ("ro"), "ro");
      call_expr = build_call_expr (vlt_change_permission_fndecl, 2, arg, arg2);
      append_to_statement_list (call_expr, &body);

      ret_val = true;  /* Actually did some work/wrote something out.  */
    }

  return ret_val;
}

static tree
vtable_find_map_decl (tree var_id)
{
  struct varpool_node *node;

  for (node = varpool_nodes; node; node = node->next)
    {
      tree var_decl = node->decl;
      if (DECL_NAME (var_decl) == var_id)
        return var_decl;
    }
  return NULL_TREE;
}

static tree 
vtable_find_or_create_map_decl (tree base_type)
{
  tree base_decl = TREE_CHAIN (base_type);
  tree base_id = get_mangled_id (base_decl);
  tree var_id;
  tree var_decl = NULL;
  char *var_name = NULL;

  /* Verify the type has an associated vtable */
  if (!TYPE_BINFO(base_type) || !BINFO_VTABLE (TYPE_BINFO (base_type)))
    return NULL;

  /* Create map lookup symbol for base class */
  var_name = ACONCAT (("_ZTV", IDENTIFIER_POINTER (base_id),
                       ".vtable_map", NULL));
  var_id = maybe_get_identifier (var_name);
  if (var_id)
    /* We've already created the variable; just look for it */
    var_decl = vtable_find_map_decl (var_id);
  else
    {
      /* If we haven't already created the *.vtable_map
         global variable for this class, do so now, and
         add it to the varpool, to make sure it gets saved
         and written out.  */

      char *sect_name = NULL;
      tree var_type = build_pointer_type (void_type_node);
      tree initial_value = build_int_cst
          (make_node (INTEGER_TYPE), 0);
      var_decl  = build_decl (UNKNOWN_LOCATION, VAR_DECL,
                              get_identifier (var_name),
                              var_type);
      TREE_PUBLIC (var_decl) = 1;
      DECL_EXTERNAL (var_decl) = 0;
      TREE_STATIC (var_decl) = 1;
      SET_DECL_ASSEMBLER_NAME (var_decl,
                               get_identifier (var_name));
      DECL_ARTIFICIAL (var_decl) = 1;
      TREE_READONLY (var_decl) = 1;
      DECL_IGNORED_P (var_decl) = 1;

      /* Once we figure out how to mprotect/un-mprotect this section, and
         get the fix for the binutils bug, we need to make it rel.ro, as
         shown here.
      sect_name = ACONCAT ((".data.rel.ro.", var_name,
                            NULL));
      */
      sect_name = ACONCAT ((".data.", var_name,
                            NULL));

      DECL_SECTION_NAME (var_decl) =
          build_string (strlen (sect_name), sect_name);
      DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl) = true;
      DECL_COMDAT_GROUP (var_decl) = get_identifier (var_name);
      DECL_INITIAL (var_decl) = initial_value;

      varpool_finalize_decl (var_decl);
      save_vtable_map_decl (var_decl);

      update_class_hierarchy_information (var_decl, base_type, base_type);
    }

  gcc_assert(var_decl);
  return var_decl;
}

void 
vtv_save_base_class_info (tree type)
{
  tree binfo =  TYPE_BINFO(type);
  tree base_binfo;
  tree own_map;
  int i;

  /* first make sure to create the map for this record type */
  own_map = vtable_find_or_create_map_decl(type);
  if (own_map == NULL)
    return;

  /* Go through the list of all base classes for the current (derived)
     type, make sure the *.vtable_map global variable for the base class
     exists, and add the base class/derived class pair to the class
     hierarchy information we are accumulating (for vtable pointer
     verification).  */
  for (i = 0; BINFO_BASE_ITERATE(binfo, i, base_binfo); i++)
    {
      tree tree_val = BINFO_TYPE(base_binfo);
      tree var_decl = NULL_TREE;

      var_decl = vtable_find_or_create_map_decl(tree_val);
      if (var_decl != NULL)
        update_class_hierarchy_information (var_decl, tree_val,
                                            type);
    }
}