forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathctf-create.c
3043 lines (2516 loc) · 87.6 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 file creation.
Copyright (C) 2019-2021 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 <assert.h>
#include <string.h>
#include <unistd.h>
#include <zlib.h>
#include <elf.h>
#include "elf-bfd.h"
#ifndef EOVERFLOW
#define EOVERFLOW ERANGE
#endif
#ifndef roundup
#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
#endif
/* 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;
}
/* 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;
}
/* Delete data symbols that have been assigned names from the variable section.
Must be called from within ctf_serialize, because that is the only place
you can safely delete variables without messing up ctf_rollback. */
static int
symtypetab_delete_nonstatic_vars (ctf_dict_t *fp)
{
ctf_dvdef_t *dvd, *nvd;
ctf_id_t type;
for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd)
{
nvd = ctf_list_next (dvd);
if (((type = (ctf_id_t) (uintptr_t)
ctf_dynhash_lookup (fp->ctf_objthash, dvd->dvd_name)) > 0)
&& type == dvd->dvd_type)
ctf_dvd_delete (fp, dvd);
}
return 0;
}
/* Determine if a symbol is "skippable" and should never appear in the
symtypetab sections. */
int
ctf_symtab_skippable (ctf_link_sym_t *sym)
{
/* Never skip symbols whose name is not yet known. */
if (sym->st_nameidx_set)
return 0;
return (sym->st_name == NULL || sym->st_name[0] == 0
|| sym->st_shndx == SHN_UNDEF
|| strcmp (sym->st_name, "_START_") == 0
|| strcmp (sym->st_name, "_END_") == 0
|| (sym->st_type == STT_OBJECT && sym->st_shndx == SHN_EXTABS
&& sym->st_value == 0));
}
/* Symtypetab emission flags. */
#define CTF_SYMTYPETAB_EMIT_FUNCTION 0x1
#define CTF_SYMTYPETAB_EMIT_PAD 0x2
#define CTF_SYMTYPETAB_FORCE_INDEXED 0x4
/* Get the number of symbols in a symbol hash, the count of symbols, the maximum
seen, the eventual size, without any padding elements, of the func/data and
(if generated) index sections, and the size of accumulated padding elements.
The linker-reported set of symbols is found in SYMFP.
Also figure out if any symbols need to be moved to the variable section, and
add them (if not already present). */
_libctf_nonnull_
static int
symtypetab_density (ctf_dict_t *fp, ctf_dict_t *symfp, ctf_dynhash_t *symhash,
size_t *count, size_t *max, size_t *unpadsize,
size_t *padsize, size_t *idxsize, int flags)
{
ctf_next_t *i = NULL;
const void *name;
const void *ctf_sym;
ctf_dynhash_t *linker_known = NULL;
int err;
int beyond_max = 0;
*count = 0;
*max = 0;
*unpadsize = 0;
*idxsize = 0;
*padsize = 0;
if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
{
/* Make a dynhash citing only symbols reported by the linker of the
appropriate type, then traverse all potential-symbols we know the types
of, removing them from linker_known as we go. Once this is done, the
only symbols remaining in linker_known are symbols we don't know the
types of: we must emit pads for those symbols that are below the
maximum symbol we will emit (any beyond that are simply skipped). */
if ((linker_known = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL)) == NULL)
return (ctf_set_errno (fp, ENOMEM));
while ((err = ctf_dynhash_cnext (symfp->ctf_dynsyms, &i,
&name, &ctf_sym)) == 0)
{
ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym;
if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& sym->st_type != STT_FUNC)
|| (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& sym->st_type != STT_OBJECT))
continue;
if (ctf_symtab_skippable (sym))
continue;
/* This should only be true briefly before all the names are
finalized, long before we get this far. */
if (!ctf_assert (fp, !sym->st_nameidx_set))
return -1; /* errno is set for us. */
if (ctf_dynhash_cinsert (linker_known, name, ctf_sym) < 0)
{
ctf_dynhash_destroy (linker_known);
return (ctf_set_errno (fp, ENOMEM));
}
}
if (err != ECTF_NEXT_END)
{
ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols during "
"serialization"));
ctf_dynhash_destroy (linker_known);
return (ctf_set_errno (fp, err));
}
}
while ((err = ctf_dynhash_cnext (symhash, &i, &name, NULL)) == 0)
{
ctf_link_sym_t *sym;
if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
{
/* Linker did not report symbol in symtab. Remove it from the
set of known data symbols and continue. */
if ((sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, name)) == NULL)
{
ctf_dynhash_remove (symhash, name);
continue;
}
/* We don't remove skippable symbols from the symhash because we don't
want them to be migrated into variables. */
if (ctf_symtab_skippable (sym))
continue;
if ((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& sym->st_type != STT_FUNC)
{
ctf_err_warn (fp, 1, 0, _("Symbol %x added to CTF as a function "
"but is of type %x\n"),
sym->st_symidx, sym->st_type);
ctf_dynhash_remove (symhash, name);
continue;
}
else if (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& sym->st_type != STT_OBJECT)
{
ctf_err_warn (fp, 1, 0, _("Symbol %x added to CTF as a data "
"object but is of type %x\n"),
sym->st_symidx, sym->st_type);
ctf_dynhash_remove (symhash, name);
continue;
}
ctf_dynhash_remove (linker_known, name);
}
*unpadsize += sizeof (uint32_t);
(*count)++;
if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
{
if (*max < sym->st_symidx)
*max = sym->st_symidx;
}
else
(*max)++;
}
if (err != ECTF_NEXT_END)
{
ctf_err_warn (fp, 0, err, _("iterating over CTF symtypetab during "
"serialization"));
ctf_dynhash_destroy (linker_known);
return (ctf_set_errno (fp, err));
}
if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
{
while ((err = ctf_dynhash_cnext (linker_known, &i, NULL, &ctf_sym)) == 0)
{
ctf_link_sym_t *sym = (ctf_link_sym_t *) ctf_sym;
if (sym->st_symidx > *max)
beyond_max++;
}
if (err != ECTF_NEXT_END)
{
ctf_err_warn (fp, 0, err, _("iterating over linker-known symbols "
"during CTF serialization"));
ctf_dynhash_destroy (linker_known);
return (ctf_set_errno (fp, err));
}
}
*idxsize = *count * sizeof (uint32_t);
if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
*padsize = (ctf_dynhash_elements (linker_known) - beyond_max) * sizeof (uint32_t);
ctf_dynhash_destroy (linker_known);
return 0;
}
/* Emit an objt or func symtypetab into DP in a particular order defined by an
array of ctf_link_sym_t or symbol names passed in. The index has NIDX
elements in it: unindexed output would terminate at symbol OUTMAX and is in
any case no larger than SIZE bytes. Some index elements are expected to be
skipped: see symtypetab_density. The linker-reported set of symbols (if any)
is found in SYMFP. */
static int
emit_symtypetab (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
ctf_link_sym_t **idx, const char **nameidx, uint32_t nidx,
uint32_t outmax, int size, int flags)
{
uint32_t i;
uint32_t *dpp = dp;
ctf_dynhash_t *symhash;
ctf_dprintf ("Emitting table of size %i, outmax %u, %u symtypetab entries, "
"flags %i\n", size, outmax, nidx, flags);
/* Empty table? Nothing to do. */
if (size == 0)
return 0;
if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
symhash = fp->ctf_funchash;
else
symhash = fp->ctf_objthash;
for (i = 0; i < nidx; i++)
{
const char *sym_name;
void *type;
/* If we have a linker-reported set of symbols, we may be given that set
to work from, or a set of symbol names. In both cases we want to look
at the corresponding linker-reported symbol (if any). */
if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
{
ctf_link_sym_t *this_link_sym;
if (idx)
this_link_sym = idx[i];
else
this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, nameidx[i]);
/* Unreported symbol number. No pad, no nothing. */
if (!this_link_sym)
continue;
/* Symbol of the wrong type, or skippable? This symbol is not in this
table. */
if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& this_link_sym->st_type != STT_FUNC)
|| (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& this_link_sym->st_type != STT_OBJECT))
continue;
if (ctf_symtab_skippable (this_link_sym))
continue;
sym_name = this_link_sym->st_name;
/* Linker reports symbol of a different type to the symbol we actually
added? Skip the symbol. No pad, since the symbol doesn't actually
belong in this table at all. (Warned about in
symtypetab_density.) */
if ((this_link_sym->st_type == STT_FUNC)
&& (ctf_dynhash_lookup (fp->ctf_objthash, sym_name)))
continue;
if ((this_link_sym->st_type == STT_OBJECT)
&& (ctf_dynhash_lookup (fp->ctf_funchash, sym_name)))
continue;
}
else
sym_name = nameidx[i];
/* Symbol in index but no type set? Silently skip and (optionally)
pad. (In force-indexed mode, this is also where we track symbols of
the wrong type for this round of insertion.) */
if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL)
{
if (flags & CTF_SYMTYPETAB_EMIT_PAD)
*dpp++ = 0;
continue;
}
if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) < size))
return -1; /* errno is set for us. */
*dpp++ = (ctf_id_t) (uintptr_t) type;
/* When emitting unindexed output, all later symbols are pads: stop
early. */
if ((flags & CTF_SYMTYPETAB_EMIT_PAD) && idx[i]->st_symidx == outmax)
break;
}
return 0;
}
/* Emit an objt or func symtypetab index into DP in a paticular order defined by
an array of symbol names passed in. Stop at NIDX. The linker-reported set
of symbols (if any) is found in SYMFP. */
static int
emit_symtypetab_index (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
const char **idx, uint32_t nidx, int size, int flags)
{
uint32_t i;
uint32_t *dpp = dp;
ctf_dynhash_t *symhash;
ctf_dprintf ("Emitting index of size %i, %u entries reported by linker, "
"flags %i\n", size, nidx, flags);
/* Empty table? Nothing to do. */
if (size == 0)
return 0;
if (flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
symhash = fp->ctf_funchash;
else
symhash = fp->ctf_objthash;
/* Indexes should always be unpadded. */
if (!ctf_assert (fp, !(flags & CTF_SYMTYPETAB_EMIT_PAD)))
return -1; /* errno is set for us. */
for (i = 0; i < nidx; i++)
{
const char *sym_name;
void *type;
if (!(flags & CTF_SYMTYPETAB_FORCE_INDEXED))
{
ctf_link_sym_t *this_link_sym;
this_link_sym = ctf_dynhash_lookup (symfp->ctf_dynsyms, idx[i]);
/* This is an index: unreported symbols should never appear in it. */
if (!ctf_assert (fp, this_link_sym != NULL))
return -1; /* errno is set for us. */
/* Symbol of the wrong type, or skippable? This symbol is not in this
table. */
if (((flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& this_link_sym->st_type != STT_FUNC)
|| (!(flags & CTF_SYMTYPETAB_EMIT_FUNCTION)
&& this_link_sym->st_type != STT_OBJECT))
continue;
if (ctf_symtab_skippable (this_link_sym))
continue;
sym_name = this_link_sym->st_name;
/* Linker reports symbol of a different type to the symbol we actually
added? Skip the symbol. */
if ((this_link_sym->st_type == STT_FUNC)
&& (ctf_dynhash_lookup (fp->ctf_objthash, sym_name)))
continue;
if ((this_link_sym->st_type == STT_OBJECT)
&& (ctf_dynhash_lookup (fp->ctf_funchash, sym_name)))
continue;
}
else
sym_name = idx[i];
/* Symbol in index and reported by linker, but no type set? Silently skip
and (optionally) pad. (In force-indexed mode, this is also where we
track symbols of the wrong type for this round of insertion.) */
if ((type = ctf_dynhash_lookup (symhash, sym_name)) == NULL)
continue;
ctf_str_add_ref (fp, sym_name, dpp++);
if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) <= size))
return -1; /* errno is set for us. */
}
return 0;
}
static unsigned char *
ctf_copy_smembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
{
ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
ctf_member_t ctm;
for (; dmd != NULL; dmd = ctf_list_next (dmd))
{
ctf_member_t *copied;
ctm.ctm_name = 0;
ctm.ctm_type = (uint32_t) dmd->dmd_type;
ctm.ctm_offset = (uint32_t) dmd->dmd_offset;
memcpy (t, &ctm, sizeof (ctm));
copied = (ctf_member_t *) t;
if (dmd->dmd_name)
ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctm_name);
t += sizeof (ctm);
}
return t;
}
static unsigned char *
ctf_copy_lmembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
{
ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
ctf_lmember_t ctlm;
for (; dmd != NULL; dmd = ctf_list_next (dmd))
{
ctf_lmember_t *copied;
ctlm.ctlm_name = 0;
ctlm.ctlm_type = (uint32_t) dmd->dmd_type;
ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI (dmd->dmd_offset);
ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO (dmd->dmd_offset);
memcpy (t, &ctlm, sizeof (ctlm));
copied = (ctf_lmember_t *) t;
if (dmd->dmd_name)
ctf_str_add_ref (fp, dmd->dmd_name, &copied->ctlm_name);
t += sizeof (ctlm);
}
return t;
}
static unsigned char *
ctf_copy_emembers (ctf_dict_t *fp, ctf_dtdef_t *dtd, unsigned char *t)
{
ctf_dmdef_t *dmd = ctf_list_next (&dtd->dtd_u.dtu_members);
ctf_enum_t cte;
for (; dmd != NULL; dmd = ctf_list_next (dmd))
{
ctf_enum_t *copied;
cte.cte_value = dmd->dmd_value;
memcpy (t, &cte, sizeof (cte));
copied = (ctf_enum_t *) t;
ctf_str_add_ref (fp, dmd->dmd_name, &copied->cte_name);
t += sizeof (cte);
}
return t;
}
/* Sort a newly-constructed static variable array. */
typedef struct ctf_sort_var_arg_cb
{
ctf_dict_t *fp;
ctf_strs_t *strtab;
} ctf_sort_var_arg_cb_t;
static int
ctf_sort_var (const void *one_, const void *two_, void *arg_)
{
const ctf_varent_t *one = one_;
const ctf_varent_t *two = two_;
ctf_sort_var_arg_cb_t *arg = arg_;
return (strcmp (ctf_strraw_explicit (arg->fp, one->ctv_name, arg->strtab),
ctf_strraw_explicit (arg->fp, two->ctv_name, arg->strtab)));
}
/* 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;
}
/* If the specified CTF dict is writable and has been modified, reload this dict
with the updated type definitions, ready for serialization. In order to make
this code and the rest of libctf as simple as possible, we perform updates by
taking the dynamic type definitions and creating an in-memory CTF dict
containing the definitions, and then call ctf_simple_open_internal() on it.
We perform one extra trick here for the benefit of callers and to keep our
code simple: ctf_simple_open_internal() will return a new ctf_dict_t, but we
want to keep the fp constant for the caller, so after
ctf_simple_open_internal() returns, we use memcpy to swap the interior of the
old and new ctf_dict_t's, and then free the old. */
int
ctf_serialize (ctf_dict_t *fp)
{
ctf_dict_t ofp, *nfp;
ctf_header_t hdr, *hdrp;
ctf_dtdef_t *dtd;
ctf_dvdef_t *dvd;
ctf_varent_t *dvarents;
ctf_strs_writable_t strtab;
ctf_dict_t *symfp = fp;
unsigned char *t;
unsigned long i;
int symflags = 0;
size_t buf_size, type_size, objt_size, func_size;
size_t objt_unpadsize, func_unpadsize, objt_padsize, func_padsize;
size_t funcidx_size, objtidx_size;
size_t nvars, nfuncs, nobjts, maxobjt, maxfunc;
size_t ndynsyms = 0;
const char **sym_name_order = NULL;
unsigned char *buf = NULL, *newbuf;
int err;
if (!(fp->ctf_flags & LCTF_RDWR))
return (ctf_set_errno (fp, ECTF_RDONLY));
/* Update required? */
if (!(fp->ctf_flags & LCTF_DIRTY))
return 0;
/* Fill in an initial CTF header. We will leave the label, object,
and function sections empty and only output a header, type section,
and string table. The type section begins at a 4-byte aligned
boundary past the CTF header itself (at relative offset zero). The flag
indicating a new-style function info section (an array of CTF_K_FUNCTION
type IDs in the types section) is flipped on. */
memset (&hdr, 0, sizeof (hdr));
hdr.cth_magic = CTF_MAGIC;
hdr.cth_version = CTF_VERSION;
/* This is a new-format func info section, and the symtab and strtab come out
of the dynsym and dynstr these days. */
hdr.cth_flags = (CTF_F_NEWFUNCINFO | CTF_F_DYNSTR);
/* Iterate through the dynamic type definition list and compute the
size of the CTF type section we will need to generate. */
for (type_size = 0, dtd = ctf_list_next (&fp->ctf_dtdefs);
dtd != NULL; dtd = ctf_list_next (dtd))
{
uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
type_size += sizeof (ctf_stype_t);
else
type_size += sizeof (ctf_type_t);
switch (kind)
{
case CTF_K_INTEGER:
case CTF_K_FLOAT:
type_size += sizeof (uint32_t);
break;
case CTF_K_ARRAY:
type_size += sizeof (ctf_array_t);
break;
case CTF_K_SLICE:
type_size += sizeof (ctf_slice_t);
break;
case CTF_K_FUNCTION:
type_size += sizeof (uint32_t) * (vlen + (vlen & 1));
break;
case CTF_K_STRUCT:
case CTF_K_UNION:
if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
type_size += sizeof (ctf_member_t) * vlen;
else
type_size += sizeof (ctf_lmember_t) * vlen;
break;
case CTF_K_ENUM:
type_size += sizeof (ctf_enum_t) * vlen;
break;
}
}
/* Symbol table stuff is done only if the linker has told this dict about
potential symbols (usually the case for parent dicts only). The linker
will report symbols to the parent dict in a parent/child link, as usual
with all linker-related matters. */
if (!fp->ctf_dynsyms && fp->ctf_parent && fp->ctf_parent->ctf_dynsyms)
symfp = fp->ctf_parent;
/* No linker-reported symbols at all: ctf_link_shuffle_syms was never called.
This must be an unsorted, indexed dict. Otherwise, this is a sorted
dict, and the header flags indicate as much. */
if (!symfp->ctf_dynsyms)
symflags = CTF_SYMTYPETAB_FORCE_INDEXED;
else
hdr.cth_flags |= CTF_F_IDXSORTED;
/* Work out the sizes of the object and function sections, and work out the
number of pad (unassigned) symbols in each, and the overall size of the
sections. */
if (symtypetab_density (fp, symfp, fp->ctf_objthash, &nobjts, &maxobjt,
&objt_unpadsize, &objt_padsize, &objtidx_size,
symflags) < 0)
return -1; /* errno is set for us. */
ctf_dprintf ("Object symtypetab: %i objects, max %i, unpadded size %i, "
"%i bytes of pads, index size %i\n", (int) nobjts, (int) maxobjt,
(int) objt_unpadsize, (int) objt_padsize, (int) objtidx_size);
if (symtypetab_density (fp, symfp, fp->ctf_funchash, &nfuncs, &maxfunc,
&func_unpadsize, &func_padsize, &funcidx_size,
symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
return -1; /* errno is set for us. */
ctf_dprintf ("Function symtypetab: %i functions, max %i, unpadded size %i, "
"%i bytes of pads, index size %i\n", (int) nfuncs, (int) maxfunc,
(int) func_unpadsize, (int) func_padsize, (int) funcidx_size);
/* If the linker has reported any symbols at all, those symbols that the
linker has not reported are now removed from the ctf_objthash and
ctf_funchash. Delete entries from the variable section that duplicate
newly-added data symbols. There's no need to migrate new ones in, because
linker invocations (even ld -r) can only introduce new symbols, not remove
symbols that already exist, and the compiler always emits both a variable
and a data symbol simultaneously. */
if (symtypetab_delete_nonstatic_vars (fp) < 0)
return -1;
/* It is worth indexing each section if it would save space to do so, due to
reducing the number of pads sufficiently. A pad is the same size as a
single index entry: but index sections compress relatively poorly compared
to constant pads, so it takes a lot of contiguous padding to equal one
index section entry. It would be nice to be able to *verify* whether we
would save space after compression rather than guessing, but this seems
difficult, since it would require complete reserialization. Regardless, if
the linker has not reported any symbols (e.g. if this is not a final link
but just an ld -r), we must emit things in indexed fashion just as the
compiler does. */
objt_size = objt_unpadsize;
if (!(symflags & CTF_SYMTYPETAB_FORCE_INDEXED)
&& ((objt_padsize + objt_unpadsize) * CTF_INDEX_PAD_THRESHOLD
> objt_padsize))
{
objt_size += objt_padsize;
objtidx_size = 0;
}
func_size = func_unpadsize;
if (!(symflags & CTF_SYMTYPETAB_FORCE_INDEXED)
&& ((func_padsize + func_unpadsize) * CTF_INDEX_PAD_THRESHOLD
> func_padsize))
{
func_size += func_padsize;
funcidx_size = 0;
}
/* Computing the number of entries in the CTF variable section is much
simpler. */
for (nvars = 0, dvd = ctf_list_next (&fp->ctf_dvdefs);
dvd != NULL; dvd = ctf_list_next (dvd), nvars++);
/* Compute the size of the CTF buffer we need, sans only the string table,
then allocate a new buffer and memcpy the finished header to the start of
the buffer. (We will adjust this later with strtab length info.) */
hdr.cth_lbloff = hdr.cth_objtoff = 0;
hdr.cth_funcoff = hdr.cth_objtoff + objt_size;
hdr.cth_objtidxoff = hdr.cth_funcoff + func_size;
hdr.cth_funcidxoff = hdr.cth_objtidxoff + objtidx_size;
hdr.cth_varoff = hdr.cth_funcidxoff + funcidx_size;
hdr.cth_typeoff = hdr.cth_varoff + (nvars * sizeof (ctf_varent_t));
hdr.cth_stroff = hdr.cth_typeoff + type_size;
hdr.cth_strlen = 0;
buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
if ((buf = malloc (buf_size)) == NULL)
return (ctf_set_errno (fp, EAGAIN));
memcpy (buf, &hdr, sizeof (ctf_header_t));
t = (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_objtoff;
hdrp = (ctf_header_t *) buf;
if ((fp->ctf_flags & LCTF_CHILD) && (fp->ctf_parname != NULL))
ctf_str_add_ref (fp, fp->ctf_parname, &hdrp->cth_parname);
if (fp->ctf_cuname != NULL)
ctf_str_add_ref (fp, fp->ctf_cuname, &hdrp->cth_cuname);
/* Sort the linker's symbols into name order if need be: if
ctf_link_shuffle_syms has not been called at all, just use all the symbols
that were added to this dict, and don't bother sorting them since this is
probably an ld -r and will likely just be consumed by ld again, with no
ctf_lookup_by_symbol()s ever done on it. */
if ((objtidx_size != 0) || (funcidx_size != 0))
{
ctf_next_t *i = NULL;
void *symname;
const char **walk;
int err;
if (symfp->ctf_dynsyms)
ndynsyms = ctf_dynhash_elements (symfp->ctf_dynsyms);
else
ndynsyms = ctf_dynhash_elements (symfp->ctf_objthash)
+ ctf_dynhash_elements (symfp->ctf_funchash);
if ((sym_name_order = calloc (ndynsyms, sizeof (const char *))) == NULL)
goto oom;
walk = sym_name_order;
if (symfp->ctf_dynsyms)
{
while ((err = ctf_dynhash_next_sorted (symfp->ctf_dynsyms, &i, &symname,
NULL, ctf_dynhash_sort_by_name,
NULL)) == 0)
*walk++ = (const char *) symname;
if (err != ECTF_NEXT_END)
goto symerr;
}
else
{
while ((err = ctf_dynhash_next (symfp->ctf_objthash, &i, &symname,
NULL)) == 0)
*walk++ = (const char *) symname;
if (err != ECTF_NEXT_END)
goto symerr;
while ((err = ctf_dynhash_next (symfp->ctf_funchash, &i, &symname,
NULL)) == 0)
*walk++ = (const char *) symname;
if (err != ECTF_NEXT_END)
goto symerr;
}
}
/* Emit the object and function sections, and if necessary their indexes.
Emission is done in symtab order if there is no index, and in index
(name) order otherwise. */
if ((objtidx_size == 0) && symfp->ctf_dynsymidx)
{
ctf_dprintf ("Emitting unindexed objt symtypetab\n");
if (emit_symtypetab (fp, symfp, (uint32_t *) t, symfp->ctf_dynsymidx,
NULL, symfp->ctf_dynsymmax + 1, maxobjt, objt_size,
symflags | CTF_SYMTYPETAB_EMIT_PAD) < 0)
goto err; /* errno is set for us. */
}
else
{
ctf_dprintf ("Emitting indexed objt symtypetab\n");
if (emit_symtypetab (fp, symfp, (uint32_t *) t, NULL, sym_name_order,
ndynsyms, maxobjt, objt_size, symflags) < 0)
goto err; /* errno is set for us. */
}
t += objt_size;
if ((funcidx_size == 0) && symfp->ctf_dynsymidx)
{
ctf_dprintf ("Emitting unindexed func symtypetab\n");
if (emit_symtypetab (fp, symfp, (uint32_t *) t, symfp->ctf_dynsymidx,
NULL, symfp->ctf_dynsymmax + 1, maxfunc,
func_size, symflags | CTF_SYMTYPETAB_EMIT_FUNCTION
| CTF_SYMTYPETAB_EMIT_PAD) < 0)
goto err; /* errno is set for us. */
}
else
{
ctf_dprintf ("Emitting indexed func symtypetab\n");
if (emit_symtypetab (fp, symfp, (uint32_t *) t, NULL, sym_name_order,
ndynsyms, maxfunc, func_size,
symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
goto err; /* errno is set for us. */
}
t += func_size;
if (objtidx_size > 0)
if (emit_symtypetab_index (fp, symfp, (uint32_t *) t, sym_name_order,
ndynsyms, objtidx_size, symflags) < 0)
goto err;
t += objtidx_size;
if (funcidx_size > 0)
if (emit_symtypetab_index (fp, symfp, (uint32_t *) t, sym_name_order,
ndynsyms, funcidx_size,
symflags | CTF_SYMTYPETAB_EMIT_FUNCTION) < 0)
goto err;
t += funcidx_size;
free (sym_name_order);
sym_name_order = NULL;
/* Work over the variable list, translating everything into ctf_varent_t's and
prepping the string table. */
dvarents = (ctf_varent_t *) t;
for (i = 0, dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL;
dvd = ctf_list_next (dvd), i++)
{
ctf_varent_t *var = &dvarents[i];
ctf_str_add_ref (fp, dvd->dvd_name, &var->ctv_name);
var->ctv_type = (uint32_t) dvd->dvd_type;
}
assert (i == nvars);
t += sizeof (ctf_varent_t) * nvars;
assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_typeoff);
/* We now take a final lap through the dynamic type definition list and copy
the appropriate type records to the output buffer, noting down the
strings as we go. */
for (dtd = ctf_list_next (&fp->ctf_dtdefs);
dtd != NULL; dtd = ctf_list_next (dtd))
{