forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctf-link.c
2099 lines (1755 loc) · 60 KB
/
ctf-link.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 linking.
Copyright (C) 2019-2025 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 <string.h>
#if defined (PIC)
#pragma weak ctf_open
#endif
/* CTF linking consists of adding CTF archives full of content to be merged into
this one to the current file (which must be writable) by calling
ctf_link_add_ctf. Once this is done, a call to ctf_link will merge the type
tables together, generating new CTF files as needed, with this one as a
parent, to contain types from the inputs which conflict. ctf_link_add_strtab
takes a callback which provides string/offset pairs to be added to the
external symbol table and deduplicated from all CTF string tables in the
output link; ctf_link_shuffle_syms takes a callback which provides symtab
entries in ascending order, and shuffles the function and data sections to
match; and ctf_link_write emits a CTF file (if there are no conflicts
requiring per-compilation-unit sub-CTF files) or CTF archives (otherwise) and
returns it, suitable for addition in the .ctf section of the output. */
/* Return the name of the compilation unit this CTF dict or its parent applies
to, or a non-null string otherwise: prefer the parent. Used in debugging
output. Sometimes used for outputs too. */
const char *
ctf_link_input_name (ctf_dict_t *fp)
{
if (fp->ctf_parent && fp->ctf_parent->ctf_cuname)
return fp->ctf_parent->ctf_cuname;
else if (fp->ctf_cuname)
return fp->ctf_cuname;
else
return "(unnamed)";
}
/* Return the cuname of a dict, or the string "unnamed-CU" if none. */
static const char *
ctf_unnamed_cuname (ctf_dict_t *fp)
{
const char *cuname = ctf_cuname (fp);
if (!cuname)
cuname = "unnamed-CU";
return cuname;
}
/* The linker inputs look like this. clin_fp is used for short-circuited
CU-mapped links that can entirely avoid the first link phase in some
situations in favour of just passing on the contained ctf_dict_t: it is
always the sole ctf_dict_t inside the corresponding clin_arc. If set, it
gets assigned directly to the final link inputs and freed from there, so it
never gets explicitly freed in the ctf_link_input. */
typedef struct ctf_link_input
{
char *clin_filename;
ctf_archive_t *clin_arc;
ctf_dict_t *clin_fp;
int n;
} ctf_link_input_t;
static void
ctf_link_input_close (void *input)
{
ctf_link_input_t *i = (ctf_link_input_t *) input;
if (i->clin_arc)
ctf_arc_close (i->clin_arc);
free (i->clin_filename);
free (i);
}
/* Like ctf_link_add_ctf, below, but with no error-checking, so it can be called
in the middle of an ongoing link. */
static int
ctf_link_add_ctf_internal (ctf_dict_t *fp, ctf_archive_t *ctf,
ctf_dict_t *fp_input, const char *name)
{
int existing = 0;
ctf_link_input_t *input;
char *filename, *keyname;
/* Existing: return it, or (if a different dict with the same name
is already there) make up a new unique name. Always use the actual name
for the filename, because that needs to be ctf_open()ed. */
if ((input = ctf_dynhash_lookup (fp->ctf_link_inputs, name)) != NULL)
{
if ((fp_input != NULL && (input->clin_fp == fp_input))
|| (ctf != NULL && (input->clin_arc == ctf)))
return 0;
existing = 1;
}
if ((filename = strdup (name)) == NULL)
goto oom;
if ((input = calloc (1, sizeof (ctf_link_input_t))) == NULL)
goto oom1;
input->clin_arc = ctf;
input->clin_fp = fp_input;
input->clin_filename = filename;
input->n = ctf_dynhash_elements (fp->ctf_link_inputs);
if (existing)
{
if (asprintf (&keyname, "%s#%li", name, (long int)
ctf_dynhash_elements (fp->ctf_link_inputs)) < 0)
goto oom2;
}
else if ((keyname = strdup (name)) == NULL)
goto oom2;
if (ctf_dynhash_insert (fp->ctf_link_inputs, keyname, input) < 0)
goto oom3;
return 0;
oom3:
free (keyname);
oom2:
free (input);
oom1:
free (filename);
oom:
return ctf_set_errno (fp, ENOMEM);
}
/* Add a file, memory buffer, or unopened file (by name) to a link.
You can call this with:
CTF and NAME: link the passed ctf_archive_t, with the given NAME.
NAME alone: open NAME as a CTF file when needed.
BUF and NAME: open the BUF (of length N) as CTF, with the given NAME. (Not
yet implemented.)
Passed in CTF args are owned by the dictionary and will be freed by it.
The BUF arg is *not* owned by the dictionary, and the user should not free
its referent until the link is done.
The order of calls to this function influences the order of types in the
final link output, but otherwise is not important.
Repeated additions of the same NAME have no effect; repeated additions of
different dicts with the same NAME add all the dicts with unique NAMEs
derived from NAME.
Private for now, but may in time become public once support for BUF is
implemented. */
static int
ctf_link_add (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name,
void *buf _libctf_unused_, size_t n _libctf_unused_)
{
if (buf)
return (ctf_set_errno (fp, ECTF_NOTYET));
if (!((ctf && name && !buf)
|| (name && !buf && !ctf)
|| (buf && name && !ctf)))
return (ctf_set_errno (fp, EINVAL));
/* We can only lazily open files if libctf.so is in use rather than
libctf-nobfd.so. This is a little tricky: in shared libraries, we can use
a weak symbol so that -lctf -lctf-nobfd works, but in static libraries we
must distinguish between the two libraries explicitly. */
#if defined (PIC)
if (!buf && !ctf && name && !ctf_open)
return (ctf_set_errno (fp, ECTF_NEEDSBFD));
#elif NOBFD
if (!buf && !ctf && name)
return (ctf_set_errno (fp, ECTF_NEEDSBFD));
#endif
if (fp->ctf_link_outputs)
return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
if (fp->ctf_link_inputs == NULL)
fp->ctf_link_inputs = ctf_dynhash_create (ctf_hash_string,
ctf_hash_eq_string, free,
ctf_link_input_close);
if (fp->ctf_link_inputs == NULL)
return (ctf_set_errno (fp, ENOMEM));
return ctf_link_add_ctf_internal (fp, ctf, NULL, name);
}
/* Add an opened CTF archive or unopened file (by name) to a link.
If CTF is NULL and NAME is non-null, an unopened file is meant:
otherwise, the specified archive is assumed to have the given NAME.
Passed in CTF args are owned by the dictionary and will be freed by it.
The order of calls to this function influences the order of types in the
final link output, but otherwise is not important. */
int
ctf_link_add_ctf (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name)
{
return ctf_link_add (fp, ctf, name, NULL, 0);
}
/* Lazily open a CTF archive for linking, if not already open.
Returns the number of files contained within the opened archive (0 for none),
or -1 on error, as usual. */
static ssize_t
ctf_link_lazy_open (ctf_dict_t *fp, ctf_link_input_t *input)
{
size_t count;
int err;
if (input->clin_arc)
return ctf_archive_count (input->clin_arc);
if (input->clin_fp)
return 1;
/* See ctf_link_add_ctf. */
#if defined (PIC) || !NOBFD
input->clin_arc = ctf_open (input->clin_filename, NULL, &err);
#else
ctf_err_warn (fp, 0, ECTF_NEEDSBFD, _("cannot open %s lazily"),
input->clin_filename);
return ctf_set_errno (fp, ECTF_NEEDSBFD);
#endif
/* Having no CTF sections is not an error. We just don't need to do
anything. */
if (!input->clin_arc)
{
if (err == ECTF_NOCTFDATA)
return 0;
ctf_err_warn (fp, 0, err, _("opening CTF %s failed"),
input->clin_filename);
return ctf_set_errno (fp, err);
}
if ((count = ctf_archive_count (input->clin_arc)) == 0)
ctf_arc_close (input->clin_arc);
return (ssize_t) count;
}
/* Find a non-clashing unique name for a per-CU output dict, to prevent distinct
members corresponding to inputs with identical cunames from overwriting each
other. The name should be something like NAME. */
static char *
ctf_new_per_cu_name (ctf_dict_t *fp, const char *name)
{
char *dynname;
long int i = 0;
if ((dynname = strdup (name)) == NULL)
return NULL;
while ((ctf_dynhash_lookup (fp->ctf_link_outputs, dynname)) != NULL)
{
free (dynname);
if (asprintf (&dynname, "%s#%li", name, i++) < 0)
return NULL;
}
return dynname;
}
/* Return a per-CU output CTF dictionary suitable for the given INPUT or CU,
creating and interning it if need be. */
static ctf_dict_t *
ctf_create_per_cu (ctf_dict_t *fp, ctf_dict_t *input, const char *cu_name)
{
ctf_dict_t *cu_fp;
const char *ctf_name = NULL;
char *dynname = NULL;
/* Already has a per-CU mapping? Just return it. */
if (input && input->ctf_link_in_out)
return input->ctf_link_in_out;
/* Check the mapping table and translate the per-CU name we use
accordingly. */
if (cu_name == NULL)
cu_name = ctf_unnamed_cuname (input);
if (fp->ctf_link_in_cu_mapping)
{
if ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping,
cu_name)) == NULL)
ctf_name = cu_name;
}
if (ctf_name == NULL)
ctf_name = cu_name;
/* Look up the per-CU dict. If we don't know of one, or it is for a different input
CU which just happens to have the same name, create a new one. If we are creating
a dict with no input specified, anything will do. */
if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL
|| (input && cu_fp->ctf_link_in_out != fp))
{
int err;
if ((cu_fp = ctf_create (&err)) == NULL)
{
ctf_set_errno (fp, err);
ctf_err_warn (fp, 0, 0, _("cannot create per-CU CTF archive for "
"input CU %s"), cu_name);
return NULL;
}
/* The deduplicator is ready for strict enumerator value checking. */
cu_fp->ctf_flags |= LCTF_STRICT_NO_DUP_ENUMERATORS;
ctf_import_unref (cu_fp, fp);
if ((dynname = ctf_new_per_cu_name (fp, ctf_name)) == NULL)
goto oom;
ctf_cuname_set (cu_fp, cu_name);
ctf_parent_name_set (cu_fp, _CTF_SECTION);
cu_fp->ctf_link_in_out = fp;
fp->ctf_link_in_out = cu_fp;
if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
goto oom;
}
return cu_fp;
oom:
free (dynname);
ctf_dict_close (cu_fp);
ctf_set_errno (fp, ENOMEM);
return NULL;
}
/* Add a mapping directing that the CU named FROM should have its
conflicting/non-duplicate types (depending on link mode) go into a dict
named TO. Many FROMs can share a TO, but adding the same FROM with
a different TO will replace the old mapping.
We forcibly add a dict named TO in every case, even though it may well
wind up empty, because clients that use this facility usually expect to find
every TO dict present, even if empty, and malfunction otherwise. */
int
ctf_link_add_cu_mapping (ctf_dict_t *fp, const char *from, const char *to)
{
int err;
char *f = NULL, *t = NULL, *existing;
ctf_dynhash_t *one_out;
/* Mappings cannot be set up if per-CU output dicts already exist. */
if (fp->ctf_link_outputs && ctf_dynhash_elements (fp->ctf_link_outputs) != 0)
return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
if (fp->ctf_link_in_cu_mapping == NULL)
fp->ctf_link_in_cu_mapping = ctf_dynhash_create (ctf_hash_string,
ctf_hash_eq_string, free,
free);
if (fp->ctf_link_in_cu_mapping == NULL)
goto oom;
if (fp->ctf_link_out_cu_mapping == NULL)
fp->ctf_link_out_cu_mapping = ctf_dynhash_create (ctf_hash_string,
ctf_hash_eq_string, free,
(ctf_hash_free_fun)
ctf_dynhash_destroy);
if (fp->ctf_link_out_cu_mapping == NULL)
goto oom;
/* If this FROM already exists, remove the mapping from both the FROM->TO
and the TO->FROM lists: the user wants to change it. */
if ((existing = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping, from)) != NULL)
{
one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, existing);
if (!ctf_assert (fp, one_out))
return -1; /* errno is set for us. */
ctf_dynhash_remove (one_out, from);
ctf_dynhash_remove (fp->ctf_link_in_cu_mapping, from);
}
f = strdup (from);
t = strdup (to);
if (!f || !t)
goto oom;
/* Track both in a list from FROM to TO and in a list from TO to a list of
FROM. The former is used to create TUs with the mapped-to name at need:
the latter is used in deduplicating links to pull in all input CUs
corresponding to a single output CU. */
if ((err = ctf_dynhash_insert (fp->ctf_link_in_cu_mapping, f, t)) < 0)
{
ctf_set_errno (fp, err);
goto oom_noerrno;
}
/* f and t are now owned by the in_cu_mapping: reallocate them. */
f = strdup (from);
t = strdup (to);
if (!f || !t)
goto oom;
if ((one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, t)) == NULL)
{
if ((one_out = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
free, NULL)) == NULL)
goto oom;
if ((err = ctf_dynhash_insert (fp->ctf_link_out_cu_mapping,
t, one_out)) < 0)
{
ctf_dynhash_destroy (one_out);
ctf_set_errno (fp, err);
goto oom_noerrno;
}
}
else
{
free (t);
t = NULL;
}
if (ctf_dynhash_insert (one_out, f, NULL) < 0)
{
ctf_set_errno (fp, err);
goto oom_noerrno;
}
return 0;
oom:
ctf_set_errno (fp, errno);
oom_noerrno:
free (f);
free (t);
return -1;
}
/* Set a function which is called to transform the names of archive members.
This is useful for applying regular transformations to many names, where
ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
names. The member name changer is applied at ctf_link_write time, so it
cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
The changer function accepts a name and should return a new
dynamically-allocated name, or NULL if the name should be left unchanged. */
void
ctf_link_set_memb_name_changer (ctf_dict_t *fp,
ctf_link_memb_name_changer_f *changer,
void *arg)
{
fp->ctf_link_memb_name_changer = changer;
fp->ctf_link_memb_name_changer_arg = arg;
}
/* Set a function which is used to filter out unwanted variables from the link. */
int
ctf_link_set_variable_filter (ctf_dict_t *fp, ctf_link_variable_filter_f *filter,
void *arg)
{
fp->ctf_link_variable_filter = filter;
fp->ctf_link_variable_filter_arg = arg;
return 0;
}
/* Check if we can safely add a variable with the given type to this dict. */
static int
check_variable (const char *name, ctf_dict_t *fp, ctf_id_t type,
ctf_dvdef_t **out_dvd)
{
ctf_dvdef_t *dvd;
dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
*out_dvd = dvd;
if (!dvd)
return 1;
if (dvd->dvd_type != type)
{
/* Variable here. Wrong type: cannot add. Just skip it, because there is
no way to express this in CTF. Don't even warn: this case is too
common. (This might be the parent, in which case we'll try adding in
the child first, and only then give up.) */
ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
}
return 0; /* Already exists. */
}
/* Link one variable named NAME of type TYPE found in IN_FP into FP. */
static int
ctf_link_one_variable (ctf_dict_t *fp, ctf_dict_t *in_fp, const char *name,
ctf_id_t type, int cu_mapped)
{
ctf_dict_t *per_cu_out_fp;
ctf_id_t dst_type = 0;
ctf_dvdef_t *dvd;
/* See if this variable is filtered out. */
if (fp->ctf_link_variable_filter)
{
void *farg = fp->ctf_link_variable_filter_arg;
if (fp->ctf_link_variable_filter (in_fp, name, type, farg))
return 0;
}
/* If this type is mapped to a type in the parent dict, we want to try to add
to that first: if it reports a duplicate, or if the type is in a child
already, add straight to the child. */
if ((dst_type = ctf_dedup_type_mapping (fp, in_fp, type)) == CTF_ERR)
return -1; /* errno is set for us. */
if (dst_type != 0)
{
if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
return -1; /* errno is set for us. */
if (check_variable (name, fp, dst_type, &dvd))
{
/* No variable here: we can add it. */
if (ctf_add_variable (fp, name, dst_type) < 0)
return -1; /* errno is set for us. */
return 0;
}
/* Already present? Nothing to do. */
if (dvd && dvd->dvd_type == dst_type)
return 0;
}
/* Can't add to the parent due to a name clash, or because it references a
type only present in the child. Try adding to the child, creating if need
be. If we can't do that, skip it. Don't add to a child if we're doing a
CU-mapped link, since that has only one output. */
if (cu_mapped)
{
ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden "
"due to conflicts: skipped.\n", name,
ctf_unnamed_cuname (in_fp), type);
return 0;
}
if ((per_cu_out_fp = ctf_create_per_cu (fp, in_fp, NULL)) == NULL)
return -1; /* errno is set for us. */
/* If the type was not found, check for it in the child too. */
if (dst_type == 0)
{
if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
in_fp, type)) == CTF_ERR)
return -1; /* errno is set for us. */
if (dst_type == 0)
{
ctf_err_warn (fp, 1, 0, _("type %lx for variable %s in input file %s "
"not found: skipped"), type, name,
ctf_unnamed_cuname (in_fp));
/* Do not terminate the link: just skip the variable. */
return 0;
}
}
if (check_variable (name, per_cu_out_fp, dst_type, &dvd))
if (ctf_add_variable (per_cu_out_fp, name, dst_type) < 0)
return (ctf_set_errno (fp, ctf_errno (per_cu_out_fp)));
return 0;
}
typedef struct link_sort_inputs_cb_arg
{
int is_cu_mapped;
ctf_dict_t *fp;
} link_sort_inputs_cb_arg_t;
/* Sort the inputs by N (the link order). For CU-mapped links, this is a
mapping of input to output name, not a mapping of input name to input
ctf_link_input_t: compensate accordingly. */
static int
ctf_link_sort_inputs (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
void *arg)
{
ctf_link_input_t *input_1;
ctf_link_input_t *input_2;
link_sort_inputs_cb_arg_t *cu_mapped = (link_sort_inputs_cb_arg_t *) arg;
if (!cu_mapped || !cu_mapped->is_cu_mapped)
{
input_1 = (ctf_link_input_t *) one->hkv_value;
input_2 = (ctf_link_input_t *) two->hkv_value;
}
else
{
const char *name_1 = (const char *) one->hkv_key;
const char *name_2 = (const char *) two->hkv_key;
input_1 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_1);
input_2 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_2);
/* There is no guarantee that CU-mappings actually have corresponding
inputs: the relative ordering in that case is unimportant. */
if (!input_1)
return -1;
if (!input_2)
return 1;
}
if (input_1->n < input_2->n)
return -1;
else if (input_1->n > input_2->n)
return 1;
else
return 0;
}
/* Count the number of input dicts in the ctf_link_inputs, or that subset of the
ctf_link_inputs given by CU_NAMES if set. Return the number of input dicts,
and optionally the name and ctf_link_input_t of the single input archive if
only one exists (no matter how many dicts it contains). */
static ssize_t
ctf_link_deduplicating_count_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
ctf_link_input_t **only_one_input)
{
ctf_dynhash_t *inputs = fp->ctf_link_inputs;
ctf_next_t *i = NULL;
void *name, *input;
ctf_link_input_t *one_input = NULL;
const char *one_name = NULL;
ssize_t count = 0, narcs = 0;
int err;
if (cu_names)
inputs = cu_names;
while ((err = ctf_dynhash_next (inputs, &i, &name, &input)) == 0)
{
ssize_t one_count;
one_name = (const char *) name;
/* If we are processing CU names, get the real input. */
if (cu_names)
one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
else
one_input = (ctf_link_input_t *) input;
if (!one_input)
continue;
one_count = ctf_link_lazy_open (fp, one_input);
if (one_count < 0)
{
ctf_next_destroy (i);
return -1; /* errno is set for us. */
}
count += one_count;
narcs++;
}
if (err != ECTF_NEXT_END)
{
ctf_err_warn (fp, 0, err, _("iteration error counting deduplicating "
"CTF link inputs"));
return ctf_set_errno (fp, err);
}
if (!count)
return 0;
if (narcs == 1)
{
if (only_one_input)
*only_one_input = one_input;
}
else if (only_one_input)
*only_one_input = NULL;
return count;
}
/* Allocate and populate an inputs array big enough for a given set of inputs:
either a specific set of CU names (those from that set found in the
ctf_link_inputs), or the entire ctf_link_inputs (if cu_names is not set).
The number of inputs (from ctf_link_deduplicating_count_inputs, above) is
passed in NINPUTS: an array of uint32_t containing parent pointers
(corresponding to those members of the inputs that have parents) is allocated
and returned in PARENTS.
The inputs are *archives*, not files: the archive can have multiple members
if it is the result of a previous incremental link. We want to add every one
in turn, including the shared parent. (The dedup machinery knows that a type
used by a single dictionary and its parent should not be shared in
CTF_LINK_SHARE_DUPLICATED mode.)
If no inputs exist that correspond to these CUs, return NULL with the errno
set to ECTF_NOCTFDATA. */
static ctf_dict_t **
ctf_link_deduplicating_open_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
ssize_t ninputs, uint32_t **parents)
{
ctf_dynhash_t *inputs = fp->ctf_link_inputs;
ctf_next_t *i = NULL;
void *name, *input;
link_sort_inputs_cb_arg_t sort_arg;
ctf_dict_t **dedup_inputs = NULL;
ctf_dict_t **walk;
uint32_t *parents_ = NULL;
int err;
if (cu_names)
inputs = cu_names;
if ((dedup_inputs = calloc (ninputs, sizeof (ctf_dict_t *))) == NULL)
goto oom;
if ((parents_ = calloc (ninputs, sizeof (uint32_t))) == NULL)
goto oom;
walk = dedup_inputs;
/* Counting done: push every input into the array, in the order they were
passed to ctf_link_add_ctf (and ultimately ld). */
sort_arg.is_cu_mapped = (cu_names != NULL);
sort_arg.fp = fp;
while ((err = ctf_dynhash_next_sorted (inputs, &i, &name, &input,
ctf_link_sort_inputs, &sort_arg)) == 0)
{
const char *one_name = (const char *) name;
ctf_link_input_t *one_input;
ctf_dict_t *one_fp;
ctf_dict_t *parent_fp = NULL;
uint32_t parent_i = 0;
ctf_next_t *j = NULL;
/* If we are processing CU names, get the real input. All the inputs
will have been opened, if they contained any CTF at all. */
if (cu_names)
one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
else
one_input = (ctf_link_input_t *) input;
if (!one_input || (!one_input->clin_arc && !one_input->clin_fp))
continue;
/* Short-circuit: if clin_fp is set, just use it. */
if (one_input->clin_fp)
{
parents_[walk - dedup_inputs] = walk - dedup_inputs;
*walk = one_input->clin_fp;
walk++;
continue;
}
/* Get and insert the parent archive (if any), if this archive has
multiple members. We assume, as elsewhere, that the parent is named
_CTF_SECTION. */
if ((parent_fp = ctf_dict_open (one_input->clin_arc, _CTF_SECTION,
&err)) == NULL)
{
if (err != ECTF_NOMEMBNAM)
{
ctf_next_destroy (i);
ctf_set_errno (fp, err);
goto err;
}
}
else
{
*walk = parent_fp;
parent_i = walk - dedup_inputs;
walk++;
}
/* We disregard the input archive name: either it is the parent (which we
already have), or we want to put everything into one TU sharing the
cuname anyway (if this is a CU-mapped link), or this is the final phase
of a relink with CU-mapping off (i.e. ld -r) in which case the cuname
is correctly set regardless. */
while ((one_fp = ctf_archive_next (one_input->clin_arc, &j, NULL,
1, &err)) != NULL)
{
if (one_fp->ctf_flags & LCTF_CHILD)
{
/* The contents of the parents array for elements not
corresponding to children is undefined. If there is no parent
(itself a sign of a likely linker bug or corrupt input), we set
it to itself. */
ctf_import (one_fp, parent_fp);
if (parent_fp)
parents_[walk - dedup_inputs] = parent_i;
else
parents_[walk - dedup_inputs] = walk - dedup_inputs;
}
*walk = one_fp;
walk++;
}
if (err != ECTF_NEXT_END)
{
ctf_next_destroy (i);
goto iterr;
}
}
if (err != ECTF_NEXT_END)
goto iterr;
*parents = parents_;
return dedup_inputs;
oom:
err = ENOMEM;
iterr:
ctf_set_errno (fp, err);
err:
free (dedup_inputs);
free (parents_);
ctf_err_warn (fp, 0, 0, _("error in deduplicating CTF link "
"input allocation"));
return NULL;
}
/* Close INPUTS that have already been linked, first the passed array, and then
that subset of the ctf_link_inputs archives they came from cited by the
CU_NAMES. If CU_NAMES is not specified, close all the ctf_link_inputs in one
go, leaving it empty. */
static int
ctf_link_deduplicating_close_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
ctf_dict_t **inputs, ssize_t ninputs)
{
ctf_next_t *it = NULL;
void *name;
int err;
ssize_t i;
/* This is the inverse of ctf_link_deduplicating_open_inputs: so first, close
all the individual input dicts, opened by the archive iterator. */
for (i = 0; i < ninputs; i++)
ctf_dict_close (inputs[i]);
/* Now close the archives they are part of. */
if (cu_names)
{
while ((err = ctf_dynhash_next (cu_names, &it, &name, NULL)) == 0)
{
/* Remove the input from the linker inputs, if it exists, which also
closes it. */
ctf_dynhash_remove (fp->ctf_link_inputs, (const char *) name);
}
if (err != ECTF_NEXT_END)
{
ctf_set_errno (fp, err);
ctf_err_warn (fp, 0, 0, _("iteration error in deduplicating link "
"input freeing"));
}
}
else
ctf_dynhash_empty (fp->ctf_link_inputs);
return 0;
}
/* Do a deduplicating link of all variables in the inputs.
Also, if we are not omitting the variable section, integrate all symbols from
the symtypetabs into the variable section too. (Duplication with the
symtypetab section in the output will be eliminated at serialization time.) */
static int
ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs,
size_t ninputs, int cu_mapped)
{
size_t i;
for (i = 0; i < ninputs; i++)
{
ctf_next_t *it = NULL;
ctf_id_t type;
const char *name;
/* First the variables on the inputs. */
while ((type = ctf_variable_next (inputs[i], &it, &name)) != CTF_ERR)
{
if (ctf_link_one_variable (fp, inputs[i], name, type, cu_mapped) < 0)
{
ctf_next_destroy (it);
return -1; /* errno is set for us. */
}
}
if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
return ctf_set_errno (fp, ctf_errno (inputs[i]));
/* Next the symbols. We integrate data symbols even though the compiler
is currently doing the same, to allow the compiler to stop in
future. */
while ((type = ctf_symbol_next (inputs[i], &it, &name, 0)) != CTF_ERR)
{
if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
{
ctf_next_destroy (it);
return -1; /* errno is set for us. */
}
}
if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
return ctf_set_errno (fp, ctf_errno (inputs[i]));
/* Finally the function symbols. */
while ((type = ctf_symbol_next (inputs[i], &it, &name, 1)) != CTF_ERR)
{
if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
{
ctf_next_destroy (it);
return -1; /* errno is set for us. */
}
}
if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
return ctf_set_errno (fp, ctf_errno (inputs[i]));
}
return 0;
}
/* Check for symbol conflicts during linking. Three possibilities: already
exists, conflicting, or nonexistent. We don't have a dvd structure we can
use as a flag like check_variable does, so we use a tristate return
value instead: -1: conflicting; 1: nonexistent: 0: already exists. */
static int
check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions)
{
ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash;
ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash;
void *value;
/* Wrong type (function when object is wanted, etc). */
if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL))
return -1;
/* Not present at all yet. */
if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value))
return 1;
/* Already present. */
if ((ctf_id_t) (uintptr_t) value == type)
return 0;
/* Wrong type. */
return -1;
}
/* Do a deduplicating link of one symtypetab (function info or data object) in
one input dict. */
static int
ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input,
int cu_mapped, int functions)
{
ctf_next_t *it = NULL;
const char *name;