forked from sillyc0n/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctf-create.c
2024 lines (1643 loc) · 58.5 KB
/
ctf-create.c
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CTF dict creation.
Copyright (C) 2019-2024 Free Software Foundation, Inc.
This file is part of libctf.
libctf 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.
This program 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 this program; see the file COPYING. If not see
<http://www.gnu.org/licenses/>. */
#include <ctf-impl.h>
#include <sys/param.h>
#include <string.h>
#include <unistd.h>
#ifndef EOVERFLOW
#define EOVERFLOW ERANGE
#endif
#ifndef roundup
#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
#endif
/* The initial size of a dynamic type's vlen in members. Arbitrary: the bigger
this is, the less allocation needs to be done for small structure
initialization, and the more memory is wasted for small structures during CTF
construction. No effect on generated CTF or ctf_open()ed CTF. */
#define INITIAL_VLEN 16
/* Make sure the ptrtab has enough space for at least one more type.
We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25%
at a time. */
static int
ctf_grow_ptrtab (ctf_dict_t *fp)
{
size_t new_ptrtab_len = fp->ctf_ptrtab_len;
/* We allocate one more ptrtab entry than we need, for the initial zero,
plus one because the caller will probably allocate a new type. */
if (fp->ctf_ptrtab == NULL)
new_ptrtab_len = 1024;
else if ((fp->ctf_typemax + 2) > fp->ctf_ptrtab_len)
new_ptrtab_len = fp->ctf_ptrtab_len * 1.25;
if (new_ptrtab_len != fp->ctf_ptrtab_len)
{
uint32_t *new_ptrtab;
if ((new_ptrtab = realloc (fp->ctf_ptrtab,
new_ptrtab_len * sizeof (uint32_t))) == NULL)
return (ctf_set_errno (fp, ENOMEM));
fp->ctf_ptrtab = new_ptrtab;
memset (fp->ctf_ptrtab + fp->ctf_ptrtab_len, 0,
(new_ptrtab_len - fp->ctf_ptrtab_len) * sizeof (uint32_t));
fp->ctf_ptrtab_len = new_ptrtab_len;
}
return 0;
}
/* Make sure a vlen has enough space: expand it otherwise. Unlike the ptrtab,
which grows quite slowly, the vlen grows in big jumps because it is quite
expensive to expand: the caller has to scan the old vlen for string refs
first and remove them, then re-add them afterwards. The initial size is
more or less arbitrary. */
static int
ctf_grow_vlen (ctf_dict_t *fp, ctf_dtdef_t *dtd, size_t vlen)
{
unsigned char *old = dtd->dtd_vlen;
if (dtd->dtd_vlen_alloc > vlen)
return 0;
if ((dtd->dtd_vlen = realloc (dtd->dtd_vlen,
dtd->dtd_vlen_alloc * 2)) == NULL)
{
dtd->dtd_vlen = old;
return (ctf_set_errno (fp, ENOMEM));
}
memset (dtd->dtd_vlen + dtd->dtd_vlen_alloc, 0, dtd->dtd_vlen_alloc);
dtd->dtd_vlen_alloc *= 2;
return 0;
}
/* To create an empty CTF dict, we just declare a zeroed header and call
ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and
initialize the dynamic members. We start assigning type IDs at 1 because
type ID 0 is used as a sentinel and a not-found indicator. */
ctf_dict_t *
ctf_create (int *errp)
{
static const ctf_header_t hdr = { .cth_preamble = { CTF_MAGIC, CTF_VERSION, 0 } };
ctf_dynhash_t *dthash;
ctf_dynhash_t *dvhash;
ctf_dynhash_t *structs = NULL, *unions = NULL, *enums = NULL, *names = NULL;
ctf_dynhash_t *objthash = NULL, *funchash = NULL;
ctf_sect_t cts;
ctf_dict_t *fp;
libctf_init_debug();
dthash = ctf_dynhash_create (ctf_hash_integer, ctf_hash_eq_integer,
NULL, NULL);
if (dthash == NULL)
{
ctf_set_open_errno (errp, EAGAIN);
goto err;
}
dvhash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
if (dvhash == NULL)
{
ctf_set_open_errno (errp, EAGAIN);
goto err_dt;
}
structs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
unions = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
enums = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
names = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
objthash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
free, NULL);
funchash = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
free, NULL);
if (!structs || !unions || !enums || !names)
{
ctf_set_open_errno (errp, EAGAIN);
goto err_dv;
}
cts.cts_name = _CTF_SECTION;
cts.cts_data = &hdr;
cts.cts_size = sizeof (hdr);
cts.cts_entsize = 1;
if ((fp = ctf_bufopen_internal (&cts, NULL, NULL, NULL, 1, errp)) == NULL)
goto err_dv;
fp->ctf_structs.ctn_writable = structs;
fp->ctf_unions.ctn_writable = unions;
fp->ctf_enums.ctn_writable = enums;
fp->ctf_names.ctn_writable = names;
fp->ctf_objthash = objthash;
fp->ctf_funchash = funchash;
fp->ctf_dthash = dthash;
fp->ctf_dvhash = dvhash;
fp->ctf_dtoldid = 0;
fp->ctf_snapshots = 1;
fp->ctf_snapshot_lu = 0;
fp->ctf_flags |= LCTF_DIRTY;
ctf_set_ctl_hashes (fp);
ctf_setmodel (fp, CTF_MODEL_NATIVE);
if (ctf_grow_ptrtab (fp) < 0)
{
ctf_set_open_errno (errp, ctf_errno (fp));
ctf_dict_close (fp);
return NULL;
}
return fp;
err_dv:
ctf_dynhash_destroy (structs);
ctf_dynhash_destroy (unions);
ctf_dynhash_destroy (enums);
ctf_dynhash_destroy (names);
ctf_dynhash_destroy (objthash);
ctf_dynhash_destroy (funchash);
ctf_dynhash_destroy (dvhash);
err_dt:
ctf_dynhash_destroy (dthash);
err:
return NULL;
}
/* Compatibility: just update the threshold for ctf_discard. */
int
ctf_update (ctf_dict_t *fp)
{
if (!(fp->ctf_flags & LCTF_RDWR))
return (ctf_set_errno (fp, ECTF_RDONLY));
fp->ctf_dtoldid = fp->ctf_typemax;
return 0;
}
ctf_names_t *
ctf_name_table (ctf_dict_t *fp, int kind)
{
switch (kind)
{
case CTF_K_STRUCT:
return &fp->ctf_structs;
case CTF_K_UNION:
return &fp->ctf_unions;
case CTF_K_ENUM:
return &fp->ctf_enums;
default:
return &fp->ctf_names;
}
}
int
ctf_dtd_insert (ctf_dict_t *fp, ctf_dtdef_t *dtd, int flag, int kind)
{
const char *name;
if (ctf_dynhash_insert (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type,
dtd) < 0)
return ctf_set_errno (fp, ENOMEM);
if (flag == CTF_ADD_ROOT && dtd->dtd_data.ctt_name
&& (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL)
{
if (ctf_dynhash_insert (ctf_name_table (fp, kind)->ctn_writable,
(char *) name, (void *) (uintptr_t)
dtd->dtd_type) < 0)
{
ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t)
dtd->dtd_type);
return ctf_set_errno (fp, ENOMEM);
}
}
ctf_list_append (&fp->ctf_dtdefs, dtd);
return 0;
}
void
ctf_dtd_delete (ctf_dict_t *fp, ctf_dtdef_t *dtd)
{
int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
size_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
int name_kind = kind;
const char *name;
ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type);
switch (kind)
{
case CTF_K_STRUCT:
case CTF_K_UNION:
{
ctf_lmember_t *memb = (ctf_lmember_t *) dtd->dtd_vlen;
size_t i;
for (i = 0; i < vlen; i++)
ctf_str_remove_ref (fp, ctf_strraw (fp, memb[i].ctlm_name),
&memb[i].ctlm_name);
}
break;
case CTF_K_ENUM:
{
ctf_enum_t *en = (ctf_enum_t *) dtd->dtd_vlen;
size_t i;
for (i = 0; i < vlen; i++)
ctf_str_remove_ref (fp, ctf_strraw (fp, en[i].cte_name),
&en[i].cte_name);
}
break;
case CTF_K_FORWARD:
name_kind = dtd->dtd_data.ctt_type;
break;
}
free (dtd->dtd_vlen);
dtd->dtd_vlen_alloc = 0;
if (dtd->dtd_data.ctt_name
&& (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
&& LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
{
ctf_dynhash_remove (ctf_name_table (fp, name_kind)->ctn_writable,
name);
ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
}
ctf_list_delete (&fp->ctf_dtdefs, dtd);
free (dtd);
}
ctf_dtdef_t *
ctf_dtd_lookup (const ctf_dict_t *fp, ctf_id_t type)
{
if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, type))
fp = fp->ctf_parent;
return (ctf_dtdef_t *)
ctf_dynhash_lookup (fp->ctf_dthash, (void *) (uintptr_t) type);
}
ctf_dtdef_t *
ctf_dynamic_type (const ctf_dict_t *fp, ctf_id_t id)
{
ctf_id_t idx;
if (!(fp->ctf_flags & LCTF_RDWR))
return NULL;
if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id))
fp = fp->ctf_parent;
idx = LCTF_TYPE_TO_INDEX(fp, id);
if ((unsigned long) idx <= fp->ctf_typemax)
return ctf_dtd_lookup (fp, id);
return NULL;
}
int
ctf_dvd_insert (ctf_dict_t *fp, ctf_dvdef_t *dvd)
{
if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0)
return ctf_set_errno (fp, ENOMEM);
ctf_list_append (&fp->ctf_dvdefs, dvd);
return 0;
}
void
ctf_dvd_delete (ctf_dict_t *fp, ctf_dvdef_t *dvd)
{
ctf_dynhash_remove (fp->ctf_dvhash, dvd->dvd_name);
free (dvd->dvd_name);
ctf_list_delete (&fp->ctf_dvdefs, dvd);
free (dvd);
}
ctf_dvdef_t *
ctf_dvd_lookup (const ctf_dict_t *fp, const char *name)
{
return (ctf_dvdef_t *) ctf_dynhash_lookup (fp->ctf_dvhash, name);
}
/* Discard all of the dynamic type definitions and variable definitions that
have been added to the dict since the last call to ctf_update(). We locate
such types by scanning the dtd list and deleting elements that have type IDs
greater than ctf_dtoldid, which is set by ctf_update(), above, and by
scanning the variable list and deleting elements that have update IDs equal
to the current value of the last-update snapshot count (indicating that they
were added after the most recent call to ctf_update()). */
int
ctf_discard (ctf_dict_t *fp)
{
ctf_snapshot_id_t last_update =
{ fp->ctf_dtoldid,
fp->ctf_snapshot_lu + 1 };
/* Update required? */
if (!(fp->ctf_flags & LCTF_DIRTY))
return 0;
return (ctf_rollback (fp, last_update));
}
ctf_snapshot_id_t
ctf_snapshot (ctf_dict_t *fp)
{
ctf_snapshot_id_t snapid;
snapid.dtd_id = fp->ctf_typemax;
snapid.snapshot_id = fp->ctf_snapshots++;
return snapid;
}
/* Like ctf_discard(), only discards everything after a particular ID. */
int
ctf_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id)
{
ctf_dtdef_t *dtd, *ntd;
ctf_dvdef_t *dvd, *nvd;
if (!(fp->ctf_flags & LCTF_RDWR))
return (ctf_set_errno (fp, ECTF_RDONLY));
if (fp->ctf_snapshot_lu >= id.snapshot_id)
return (ctf_set_errno (fp, ECTF_OVERROLLBACK));
for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd)
{
int kind;
const char *name;
ntd = ctf_list_next (dtd);
if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id)
continue;
kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
if (kind == CTF_K_FORWARD)
kind = dtd->dtd_data.ctt_type;
if (dtd->dtd_data.ctt_name
&& (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL
&& LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info))
{
ctf_dynhash_remove (ctf_name_table (fp, kind)->ctn_writable,
name);
ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name);
}
ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type);
ctf_dtd_delete (fp, dtd);
}
for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
{
nvd = ctf_list_next (dvd);
if (dvd->dvd_snapshots <= id.snapshot_id)
continue;
ctf_dvd_delete (fp, dvd);
}
fp->ctf_typemax = id.dtd_id;
fp->ctf_snapshots = id.snapshot_id;
if (fp->ctf_snapshots == fp->ctf_snapshot_lu)
fp->ctf_flags &= ~LCTF_DIRTY;
return 0;
}
/* Note: vlen is the amount of space *allocated* for the vlen. It may well not
be the amount of space used (yet): the space used is declared in per-kind
fashion in the dtd_data's info word. */
static ctf_id_t
ctf_add_generic (ctf_dict_t *fp, uint32_t flag, const char *name, int kind,
size_t vlen, ctf_dtdef_t **rp)
{
ctf_dtdef_t *dtd;
ctf_id_t type;
if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
return (ctf_set_typed_errno (fp, EINVAL));
if (!(fp->ctf_flags & LCTF_RDWR))
return (ctf_set_typed_errno (fp, ECTF_RDONLY));
if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE)
return (ctf_set_typed_errno (fp, ECTF_FULL));
if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1))
return (ctf_set_typed_errno (fp, ECTF_FULL));
/* Make sure ptrtab always grows to be big enough for all types. */
if (ctf_grow_ptrtab (fp) < 0)
return CTF_ERR; /* errno is set for us. */
if ((dtd = calloc (1, sizeof (ctf_dtdef_t))) == NULL)
return (ctf_set_typed_errno (fp, EAGAIN));
dtd->dtd_vlen_alloc = vlen;
if (vlen > 0)
{
if ((dtd->dtd_vlen = calloc (1, vlen)) == NULL)
goto oom;
}
else
dtd->dtd_vlen = NULL;
type = ++fp->ctf_typemax;
type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD));
dtd->dtd_data.ctt_name = ctf_str_add_pending (fp, name,
&dtd->dtd_data.ctt_name);
dtd->dtd_type = type;
if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0')
goto oom;
if (ctf_dtd_insert (fp, dtd, flag, kind) < 0)
goto err; /* errno is set for us. */
fp->ctf_flags |= LCTF_DIRTY;
*rp = dtd;
return type;
oom:
ctf_set_errno (fp, EAGAIN);
err:
free (dtd->dtd_vlen);
free (dtd);
return CTF_ERR;
}
/* When encoding integer sizes, we want to convert a byte count in the range
1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */
static size_t
clp2 (size_t x)
{
x--;
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return (x + 1);
}
ctf_id_t
ctf_add_encoded (ctf_dict_t *fp, uint32_t flag,
const char *name, const ctf_encoding_t *ep, uint32_t kind)
{
ctf_dtdef_t *dtd;
ctf_id_t type;
uint32_t encoding;
if (ep == NULL)
return (ctf_set_typed_errno (fp, EINVAL));
if (name == NULL || name[0] == '\0')
return (ctf_set_typed_errno (fp, ECTF_NONAME));
if (!ctf_assert (fp, kind == CTF_K_INTEGER || kind == CTF_K_FLOAT))
return CTF_ERR; /* errno is set for us. */
if ((type = ctf_add_generic (fp, flag, name, kind, sizeof (uint32_t),
&dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
/ CHAR_BIT);
switch (kind)
{
case CTF_K_INTEGER:
encoding = CTF_INT_DATA (ep->cte_format, ep->cte_offset, ep->cte_bits);
break;
case CTF_K_FLOAT:
encoding = CTF_FP_DATA (ep->cte_format, ep->cte_offset, ep->cte_bits);
break;
}
memcpy (dtd->dtd_vlen, &encoding, sizeof (encoding));
return type;
}
ctf_id_t
ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind)
{
ctf_dtdef_t *dtd;
ctf_id_t type;
ctf_dict_t *tmp = fp;
int child = fp->ctf_flags & LCTF_CHILD;
if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
return (ctf_set_typed_errno (fp, EINVAL));
if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL)
return CTF_ERR; /* errno is set for us. */
if ((type = ctf_add_generic (fp, flag, NULL, kind, 0, &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0);
dtd->dtd_data.ctt_type = (uint32_t) ref;
if (kind != CTF_K_POINTER)
return type;
/* If we are adding a pointer, update the ptrtab, pointing at this type from
the type it points to. Note that ctf_typemax is at this point one higher
than we want to check against, because it's just been incremented for the
addition of this type. The pptrtab is lazily-updated as needed, so is not
touched here. */
uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type);
uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref);
if (LCTF_TYPE_ISCHILD (fp, ref) == child
&& ref_idx < fp->ctf_typemax)
fp->ctf_ptrtab[ref_idx] = type_idx;
return type;
}
ctf_id_t
ctf_add_slice (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref,
const ctf_encoding_t *ep)
{
ctf_dtdef_t *dtd;
ctf_slice_t slice;
ctf_id_t resolved_ref = ref;
ctf_id_t type;
int kind;
const ctf_type_t *tp;
ctf_dict_t *tmp = fp;
if (ep == NULL)
return (ctf_set_typed_errno (fp, EINVAL));
if ((ep->cte_bits > 255) || (ep->cte_offset > 255))
return (ctf_set_typed_errno (fp, ECTF_SLICEOVERFLOW));
if (ref == CTF_ERR || ref > CTF_MAX_TYPE)
return (ctf_set_typed_errno (fp, EINVAL));
if (ref != 0 && ((tp = ctf_lookup_by_id (&tmp, ref)) == NULL))
return CTF_ERR; /* errno is set for us. */
/* Make sure we ultimately point to an integral type. We also allow slices to
point to the unimplemented type, for now, because the compiler can emit
such slices, though they're not very much use. */
resolved_ref = ctf_type_resolve_unsliced (fp, ref);
kind = ctf_type_kind_unsliced (fp, resolved_ref);
if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) &&
(kind != CTF_K_ENUM)
&& (ref != 0))
return (ctf_set_typed_errno (fp, ECTF_NOTINTFP));
if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_SLICE,
sizeof (ctf_slice_t), &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
memset (&slice, 0, sizeof (ctf_slice_t));
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0);
dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT)
/ CHAR_BIT);
slice.cts_type = (uint32_t) ref;
slice.cts_bits = ep->cte_bits;
slice.cts_offset = ep->cte_offset;
memcpy (dtd->dtd_vlen, &slice, sizeof (ctf_slice_t));
return type;
}
ctf_id_t
ctf_add_integer (ctf_dict_t *fp, uint32_t flag,
const char *name, const ctf_encoding_t *ep)
{
return (ctf_add_encoded (fp, flag, name, ep, CTF_K_INTEGER));
}
ctf_id_t
ctf_add_float (ctf_dict_t *fp, uint32_t flag,
const char *name, const ctf_encoding_t *ep)
{
return (ctf_add_encoded (fp, flag, name, ep, CTF_K_FLOAT));
}
ctf_id_t
ctf_add_pointer (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref)
{
return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER));
}
ctf_id_t
ctf_add_array (ctf_dict_t *fp, uint32_t flag, const ctf_arinfo_t *arp)
{
ctf_dtdef_t *dtd;
ctf_array_t cta;
ctf_id_t type;
ctf_dict_t *tmp = fp;
if (arp == NULL)
return (ctf_set_typed_errno (fp, EINVAL));
if (arp->ctr_contents != 0
&& ctf_lookup_by_id (&tmp, arp->ctr_contents) == NULL)
return CTF_ERR; /* errno is set for us. */
tmp = fp;
if (ctf_lookup_by_id (&tmp, arp->ctr_index) == NULL)
return CTF_ERR; /* errno is set for us. */
if (ctf_type_kind (fp, arp->ctr_index) == CTF_K_FORWARD)
{
ctf_err_warn (fp, 1, ECTF_INCOMPLETE,
_("ctf_add_array: index type %lx is incomplete"),
arp->ctr_contents);
return (ctf_set_typed_errno (fp, ECTF_INCOMPLETE));
}
if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY,
sizeof (ctf_array_t), &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
memset (&cta, 0, sizeof (ctf_array_t));
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0);
dtd->dtd_data.ctt_size = 0;
cta.cta_contents = (uint32_t) arp->ctr_contents;
cta.cta_index = (uint32_t) arp->ctr_index;
cta.cta_nelems = arp->ctr_nelems;
memcpy (dtd->dtd_vlen, &cta, sizeof (ctf_array_t));
return type;
}
int
ctf_set_array (ctf_dict_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
{
ctf_dict_t *ofp = fp;
ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type);
ctf_array_t *vlen;
if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, type))
fp = fp->ctf_parent;
if (!(ofp->ctf_flags & LCTF_RDWR))
return (ctf_set_errno (ofp, ECTF_RDONLY));
if (!(fp->ctf_flags & LCTF_RDWR))
return (ctf_set_errno (ofp, ECTF_RDONLY));
if (dtd == NULL
|| LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
return (ctf_set_errno (ofp, ECTF_BADID));
vlen = (ctf_array_t *) dtd->dtd_vlen;
fp->ctf_flags |= LCTF_DIRTY;
vlen->cta_contents = (uint32_t) arp->ctr_contents;
vlen->cta_index = (uint32_t) arp->ctr_index;
vlen->cta_nelems = arp->ctr_nelems;
return 0;
}
ctf_id_t
ctf_add_function (ctf_dict_t *fp, uint32_t flag,
const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
{
ctf_dtdef_t *dtd;
ctf_id_t type;
uint32_t vlen;
uint32_t *vdat;
ctf_dict_t *tmp = fp;
size_t initial_vlen;
size_t i;
if (!(fp->ctf_flags & LCTF_RDWR))
return (ctf_set_typed_errno (fp, ECTF_RDONLY));
if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0
|| (ctc->ctc_argc != 0 && argv == NULL))
return (ctf_set_typed_errno (fp, EINVAL));
vlen = ctc->ctc_argc;
if (ctc->ctc_flags & CTF_FUNC_VARARG)
vlen++; /* Add trailing zero to indicate varargs (see below). */
if (ctc->ctc_return != 0
&& ctf_lookup_by_id (&tmp, ctc->ctc_return) == NULL)
return CTF_ERR; /* errno is set for us. */
if (vlen > CTF_MAX_VLEN)
return (ctf_set_typed_errno (fp, EOVERFLOW));
/* One word extra allocated for padding for 4-byte alignment if need be.
Not reflected in vlen: we don't want to copy anything into it, and
it's in addition to (e.g.) the trailing 0 indicating varargs. */
initial_vlen = (sizeof (uint32_t) * (vlen + (vlen & 1)));
if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_FUNCTION,
initial_vlen, &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
vdat = (uint32_t *) dtd->dtd_vlen;
for (i = 0; i < ctc->ctc_argc; i++)
{
tmp = fp;
if (argv[i] != 0 && ctf_lookup_by_id (&tmp, argv[i]) == NULL)
return CTF_ERR; /* errno is set for us. */
vdat[i] = (uint32_t) argv[i];
}
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
dtd->dtd_data.ctt_type = (uint32_t) ctc->ctc_return;
if (ctc->ctc_flags & CTF_FUNC_VARARG)
vdat[vlen - 1] = 0; /* Add trailing zero to indicate varargs. */
return type;
}
ctf_id_t
ctf_add_struct_sized (ctf_dict_t *fp, uint32_t flag, const char *name,
size_t size)
{
ctf_dtdef_t *dtd;
ctf_id_t type = 0;
size_t initial_vlen = sizeof (ctf_lmember_t) * INITIAL_VLEN;
/* Promote root-visible forwards to structs. */
if (name != NULL)
type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name);
if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
dtd = ctf_dtd_lookup (fp, type);
else if ((type = ctf_add_generic (fp, flag, name, CTF_K_STRUCT,
initial_vlen, &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
/* Forwards won't have any vlen yet. */
if (dtd->dtd_vlen_alloc == 0)
{
if ((dtd->dtd_vlen = calloc (1, initial_vlen)) == NULL)
return (ctf_set_typed_errno (fp, ENOMEM));
dtd->dtd_vlen_alloc = initial_vlen;
}
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_STRUCT, flag, 0);
dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
return type;
}
ctf_id_t
ctf_add_struct (ctf_dict_t *fp, uint32_t flag, const char *name)
{
return (ctf_add_struct_sized (fp, flag, name, 0));
}
ctf_id_t
ctf_add_union_sized (ctf_dict_t *fp, uint32_t flag, const char *name,
size_t size)
{
ctf_dtdef_t *dtd;
ctf_id_t type = 0;
size_t initial_vlen = sizeof (ctf_lmember_t) * INITIAL_VLEN;
/* Promote root-visible forwards to unions. */
if (name != NULL)
type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name);
if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
dtd = ctf_dtd_lookup (fp, type);
else if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNION,
initial_vlen, &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us */
/* Forwards won't have any vlen yet. */
if (dtd->dtd_vlen_alloc == 0)
{
if ((dtd->dtd_vlen = calloc (1, initial_vlen)) == NULL)
return (ctf_set_typed_errno (fp, ENOMEM));
dtd->dtd_vlen_alloc = initial_vlen;
}
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNION, flag, 0);
dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size);
dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size);
return type;
}
ctf_id_t
ctf_add_union (ctf_dict_t *fp, uint32_t flag, const char *name)
{
return (ctf_add_union_sized (fp, flag, name, 0));
}
ctf_id_t
ctf_add_enum (ctf_dict_t *fp, uint32_t flag, const char *name)
{
ctf_dtdef_t *dtd;
ctf_id_t type = 0;
size_t initial_vlen = sizeof (ctf_enum_t) * INITIAL_VLEN;
/* Promote root-visible forwards to enums. */
if (name != NULL)
type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD)
dtd = ctf_dtd_lookup (fp, type);
else if ((type = ctf_add_generic (fp, flag, name, CTF_K_ENUM,
initial_vlen, &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
/* Forwards won't have any vlen yet. */
if (dtd->dtd_vlen_alloc == 0)
{
if ((dtd->dtd_vlen = calloc (1, initial_vlen)) == NULL)
return (ctf_set_typed_errno (fp, ENOMEM));
dtd->dtd_vlen_alloc = initial_vlen;
}
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0);
dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
return type;
}
ctf_id_t
ctf_add_enum_encoded (ctf_dict_t *fp, uint32_t flag, const char *name,
const ctf_encoding_t *ep)
{
ctf_id_t type = 0;
/* First, create the enum if need be, using most of the same machinery as
ctf_add_enum(), to ensure that we do not allow things past that are not
enums or forwards to them. (This includes other slices: you cannot slice a
slice, which would be a useless thing to do anyway.) */
if (name != NULL)
type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
if (type != 0)
{
if ((ctf_type_kind (fp, type) != CTF_K_FORWARD) &&
(ctf_type_kind_unsliced (fp, type) != CTF_K_ENUM))
return (ctf_set_typed_errno (fp, ECTF_NOTINTFP));
}
else if ((type = ctf_add_enum (fp, flag, name)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
/* Now attach a suitable slice to it. */
return ctf_add_slice (fp, flag, type, ep);
}
ctf_id_t
ctf_add_forward (ctf_dict_t *fp, uint32_t flag, const char *name,
uint32_t kind)
{
ctf_dtdef_t *dtd;
ctf_id_t type = 0;
if (!ctf_forwardable_kind (kind))
return (ctf_set_typed_errno (fp, ECTF_NOTSUE));
if (name == NULL || name[0] == '\0')
return (ctf_set_typed_errno (fp, ECTF_NONAME));
/* If the type is already defined or exists as a forward tag, just
return the ctf_id_t of the existing definition. */
type = ctf_lookup_by_rawname (fp, kind, name);
if (type)
return type;
if ((type = ctf_add_generic (fp, flag, name, kind, 0, &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0);
dtd->dtd_data.ctt_type = kind;
return type;
}
ctf_id_t
ctf_add_unknown (ctf_dict_t *fp, uint32_t flag, const char *name)
{
ctf_dtdef_t *dtd;
ctf_id_t type = 0;
/* If a type is already defined with this name, error (if not CTF_K_UNKNOWN)
or just return it. */
if (name != NULL && name[0] != '\0' && flag == CTF_ADD_ROOT
&& (type = ctf_lookup_by_rawname (fp, CTF_K_UNKNOWN, name)))
{
if (ctf_type_kind (fp, type) == CTF_K_UNKNOWN)
return type;
else
{
ctf_err_warn (fp, 1, ECTF_CONFLICT,
_("ctf_add_unknown: cannot add unknown type "
"named %s: type of this name already defined"),
name ? name : _("(unnamed type)"));
return (ctf_set_typed_errno (fp, ECTF_CONFLICT));
}
}
if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNKNOWN, 0, &dtd)) == CTF_ERR)
return CTF_ERR; /* errno is set for us. */
dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNKNOWN, flag, 0);
dtd->dtd_data.ctt_type = 0;