]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - libctf/ctf-link.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / libctf / ctf-link.c
... / ...
CommitLineData
1/* CTF linking.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3
4 This file is part of libctf.
5
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
19
20#include <ctf-impl.h>
21#include <string.h>
22
23#if defined (PIC)
24#pragma weak ctf_open
25#endif
26
27/* CTF linking consists of adding CTF archives full of content to be merged into
28 this one to the current file (which must be writable) by calling
29 ctf_link_add_ctf. Once this is done, a call to ctf_link will merge the type
30 tables together, generating new CTF files as needed, with this one as a
31 parent, to contain types from the inputs which conflict. ctf_link_add_strtab
32 takes a callback which provides string/offset pairs to be added to the
33 external symbol table and deduplicated from all CTF string tables in the
34 output link; ctf_link_shuffle_syms takes a callback which provides symtab
35 entries in ascending order, and shuffles the function and data sections to
36 match; and ctf_link_write emits a CTF file (if there are no conflicts
37 requiring per-compilation-unit sub-CTF files) or CTF archives (otherwise) and
38 returns it, suitable for addition in the .ctf section of the output. */
39
40/* Return the name of the compilation unit this CTF dict or its parent applies
41 to, or a non-null string otherwise: prefer the parent. Used in debugging
42 output. Sometimes used for outputs too. */
43const char *
44ctf_link_input_name (ctf_dict_t *fp)
45{
46 if (fp->ctf_parent && fp->ctf_parent->ctf_cuname)
47 return fp->ctf_parent->ctf_cuname;
48 else if (fp->ctf_cuname)
49 return fp->ctf_cuname;
50 else
51 return "(unnamed)";
52}
53
54/* Return the cuname of a dict, or the string "unnamed-CU" if none. */
55
56static const char *
57ctf_unnamed_cuname (ctf_dict_t *fp)
58{
59 const char *cuname = ctf_cuname (fp);
60
61 if (!cuname)
62 cuname = "unnamed-CU";
63
64 return cuname;
65}
66
67/* The linker inputs look like this. clin_fp is used for short-circuited
68 CU-mapped links that can entirely avoid the first link phase in some
69 situations in favour of just passing on the contained ctf_dict_t: it is
70 always the sole ctf_dict_t inside the corresponding clin_arc. If set, it
71 gets assigned directly to the final link inputs and freed from there, so it
72 never gets explicitly freed in the ctf_link_input. */
73typedef struct ctf_link_input
74{
75 char *clin_filename;
76 ctf_archive_t *clin_arc;
77 ctf_dict_t *clin_fp;
78 int n;
79} ctf_link_input_t;
80
81static void
82ctf_link_input_close (void *input)
83{
84 ctf_link_input_t *i = (ctf_link_input_t *) input;
85 if (i->clin_arc)
86 ctf_arc_close (i->clin_arc);
87 free (i->clin_filename);
88 free (i);
89}
90
91/* Like ctf_link_add_ctf, below, but with no error-checking, so it can be called
92 in the middle of an ongoing link. */
93static int
94ctf_link_add_ctf_internal (ctf_dict_t *fp, ctf_archive_t *ctf,
95 ctf_dict_t *fp_input, const char *name)
96{
97 int existing = 0;
98 ctf_link_input_t *input;
99 char *filename, *keyname;
100
101 /* Existing: return it, or (if a different dict with the same name
102 is already there) make up a new unique name. Always use the actual name
103 for the filename, because that needs to be ctf_open()ed. */
104
105 if ((input = ctf_dynhash_lookup (fp->ctf_link_inputs, name)) != NULL)
106 {
107 if ((fp_input != NULL && (input->clin_fp == fp_input))
108 || (ctf != NULL && (input->clin_arc == ctf)))
109 return 0;
110 existing = 1;
111 }
112
113 if ((filename = strdup (name)) == NULL)
114 goto oom;
115
116 if ((input = calloc (1, sizeof (ctf_link_input_t))) == NULL)
117 goto oom1;
118
119 input->clin_arc = ctf;
120 input->clin_fp = fp_input;
121 input->clin_filename = filename;
122 input->n = ctf_dynhash_elements (fp->ctf_link_inputs);
123
124 if (existing)
125 {
126 if (asprintf (&keyname, "%s#%li", name, (long int)
127 ctf_dynhash_elements (fp->ctf_link_inputs)) < 0)
128 goto oom2;
129 }
130 else if ((keyname = strdup (name)) == NULL)
131 goto oom2;
132
133 if (ctf_dynhash_insert (fp->ctf_link_inputs, keyname, input) < 0)
134 goto oom3;
135
136 return 0;
137
138 oom3:
139 free (keyname);
140 oom2:
141 free (input);
142 oom1:
143 free (filename);
144 oom:
145 return ctf_set_errno (fp, ENOMEM);
146}
147
148/* Add a file, memory buffer, or unopened file (by name) to a link.
149
150 You can call this with:
151
152 CTF and NAME: link the passed ctf_archive_t, with the given NAME.
153 NAME alone: open NAME as a CTF file when needed.
154 BUF and NAME: open the BUF (of length N) as CTF, with the given NAME. (Not
155 yet implemented.)
156
157 Passed in CTF args are owned by the dictionary and will be freed by it.
158 The BUF arg is *not* owned by the dictionary, and the user should not free
159 its referent until the link is done.
160
161 The order of calls to this function influences the order of types in the
162 final link output, but otherwise is not important.
163
164 Repeated additions of the same NAME have no effect; repeated additions of
165 different dicts with the same NAME add all the dicts with unique NAMEs
166 derived from NAME.
167
168 Private for now, but may in time become public once support for BUF is
169 implemented. */
170
171static int
172ctf_link_add (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name,
173 void *buf _libctf_unused_, size_t n _libctf_unused_)
174{
175 if (buf)
176 return (ctf_set_errno (fp, ECTF_NOTYET));
177
178 if (!((ctf && name && !buf)
179 || (name && !buf && !ctf)
180 || (buf && name && !ctf)))
181 return (ctf_set_errno (fp, EINVAL));
182
183 /* We can only lazily open files if libctf.so is in use rather than
184 libctf-nobfd.so. This is a little tricky: in shared libraries, we can use
185 a weak symbol so that -lctf -lctf-nobfd works, but in static libraries we
186 must distinguish between the two libraries explicitly. */
187
188#if defined (PIC)
189 if (!buf && !ctf && name && !ctf_open)
190 return (ctf_set_errno (fp, ECTF_NEEDSBFD));
191#elif NOBFD
192 if (!buf && !ctf && name)
193 return (ctf_set_errno (fp, ECTF_NEEDSBFD));
194#endif
195
196 if (fp->ctf_link_outputs)
197 return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
198 if (fp->ctf_link_inputs == NULL)
199 fp->ctf_link_inputs = ctf_dynhash_create (ctf_hash_string,
200 ctf_hash_eq_string, free,
201 ctf_link_input_close);
202
203 if (fp->ctf_link_inputs == NULL)
204 return (ctf_set_errno (fp, ENOMEM));
205
206 return ctf_link_add_ctf_internal (fp, ctf, NULL, name);
207}
208
209/* Add an opened CTF archive or unopened file (by name) to a link.
210 If CTF is NULL and NAME is non-null, an unopened file is meant:
211 otherwise, the specified archive is assumed to have the given NAME.
212
213 Passed in CTF args are owned by the dictionary and will be freed by it.
214
215 The order of calls to this function influences the order of types in the
216 final link output, but otherwise is not important. */
217
218int
219ctf_link_add_ctf (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name)
220{
221 return ctf_link_add (fp, ctf, name, NULL, 0);
222}
223
224/* Lazily open a CTF archive for linking, if not already open.
225
226 Returns the number of files contained within the opened archive (0 for none),
227 or -1 on error, as usual. */
228static ssize_t
229ctf_link_lazy_open (ctf_dict_t *fp, ctf_link_input_t *input)
230{
231 size_t count;
232 int err;
233
234 if (input->clin_arc)
235 return ctf_archive_count (input->clin_arc);
236
237 if (input->clin_fp)
238 return 1;
239
240 /* See ctf_link_add_ctf. */
241#if defined (PIC) || !NOBFD
242 input->clin_arc = ctf_open (input->clin_filename, NULL, &err);
243#else
244 ctf_err_warn (fp, 0, ECTF_NEEDSBFD, _("cannot open %s lazily"),
245 input->clin_filename);
246 return ctf_set_errno (fp, ECTF_NEEDSBFD);
247#endif
248
249 /* Having no CTF sections is not an error. We just don't need to do
250 anything. */
251
252 if (!input->clin_arc)
253 {
254 if (err == ECTF_NOCTFDATA)
255 return 0;
256
257 ctf_err_warn (fp, 0, err, _("opening CTF %s failed"),
258 input->clin_filename);
259 return ctf_set_errno (fp, err);
260 }
261
262 if ((count = ctf_archive_count (input->clin_arc)) == 0)
263 ctf_arc_close (input->clin_arc);
264
265 return (ssize_t) count;
266}
267
268/* Find a non-clashing unique name for a per-CU output dict, to prevent distinct
269 members corresponding to inputs with identical cunames from overwriting each
270 other. The name should be something like NAME. */
271
272static char *
273ctf_new_per_cu_name (ctf_dict_t *fp, const char *name)
274{
275 char *dynname;
276 long int i = 0;
277
278 if ((dynname = strdup (name)) == NULL)
279 return NULL;
280
281 while ((ctf_dynhash_lookup (fp->ctf_link_outputs, dynname)) != NULL)
282 {
283 free (dynname);
284 if (asprintf (&dynname, "%s#%li", name, i++) < 0)
285 return NULL;
286 }
287
288 return dynname;
289}
290
291/* Return a per-CU output CTF dictionary suitable for the given INPUT or CU,
292 creating and interning it if need be. */
293
294static ctf_dict_t *
295ctf_create_per_cu (ctf_dict_t *fp, ctf_dict_t *input, const char *cu_name)
296{
297 ctf_dict_t *cu_fp;
298 const char *ctf_name = NULL;
299 char *dynname = NULL;
300
301 /* Already has a per-CU mapping? Just return it. */
302
303 if (input && input->ctf_link_in_out)
304 return input->ctf_link_in_out;
305
306 /* Check the mapping table and translate the per-CU name we use
307 accordingly. */
308
309 if (cu_name == NULL)
310 cu_name = ctf_unnamed_cuname (input);
311
312 if (fp->ctf_link_in_cu_mapping)
313 {
314 if ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping,
315 cu_name)) == NULL)
316 ctf_name = cu_name;
317 }
318
319 if (ctf_name == NULL)
320 ctf_name = cu_name;
321
322 /* Look up the per-CU dict. If we don't know of one, or it is for a different input
323 CU which just happens to have the same name, create a new one. If we are creating
324 a dict with no input specified, anything will do. */
325
326 if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL
327 || (input && cu_fp->ctf_link_in_out != fp))
328 {
329 int err;
330
331 if ((cu_fp = ctf_create (&err)) == NULL)
332 {
333 ctf_set_errno (fp, err);
334 ctf_err_warn (fp, 0, 0, _("cannot create per-CU CTF archive for "
335 "input CU %s"), cu_name);
336 return NULL;
337 }
338
339 /* The deduplicator is ready for strict enumerator value checking. */
340 cu_fp->ctf_flags |= LCTF_STRICT_NO_DUP_ENUMERATORS;
341 ctf_import_unref (cu_fp, fp);
342
343 if ((dynname = ctf_new_per_cu_name (fp, ctf_name)) == NULL)
344 goto oom;
345
346 ctf_cuname_set (cu_fp, cu_name);
347
348 ctf_parent_name_set (cu_fp, _CTF_SECTION);
349 cu_fp->ctf_link_in_out = fp;
350 fp->ctf_link_in_out = cu_fp;
351
352 if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
353 goto oom;
354 }
355 return cu_fp;
356
357 oom:
358 free (dynname);
359 ctf_dict_close (cu_fp);
360 ctf_set_errno (fp, ENOMEM);
361 return NULL;
362}
363
364/* Add a mapping directing that the CU named FROM should have its
365 conflicting/non-duplicate types (depending on link mode) go into a dict
366 named TO. Many FROMs can share a TO, but adding the same FROM with
367 a different TO will replace the old mapping.
368
369 We forcibly add a dict named TO in every case, even though it may well
370 wind up empty, because clients that use this facility usually expect to find
371 every TO dict present, even if empty, and malfunction otherwise. */
372
373int
374ctf_link_add_cu_mapping (ctf_dict_t *fp, const char *from, const char *to)
375{
376 int err;
377 char *f = NULL, *t = NULL, *existing;
378 ctf_dynhash_t *one_out;
379
380 /* Mappings cannot be set up if per-CU output dicts already exist. */
381 if (fp->ctf_link_outputs && ctf_dynhash_elements (fp->ctf_link_outputs) != 0)
382 return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
383
384 if (fp->ctf_link_in_cu_mapping == NULL)
385 fp->ctf_link_in_cu_mapping = ctf_dynhash_create (ctf_hash_string,
386 ctf_hash_eq_string, free,
387 free);
388 if (fp->ctf_link_in_cu_mapping == NULL)
389 goto oom;
390
391 if (fp->ctf_link_out_cu_mapping == NULL)
392 fp->ctf_link_out_cu_mapping = ctf_dynhash_create (ctf_hash_string,
393 ctf_hash_eq_string, free,
394 (ctf_hash_free_fun)
395 ctf_dynhash_destroy);
396 if (fp->ctf_link_out_cu_mapping == NULL)
397 goto oom;
398
399 /* If this FROM already exists, remove the mapping from both the FROM->TO
400 and the TO->FROM lists: the user wants to change it. */
401
402 if ((existing = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping, from)) != NULL)
403 {
404 one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, existing);
405 if (!ctf_assert (fp, one_out))
406 return -1; /* errno is set for us. */
407
408 ctf_dynhash_remove (one_out, from);
409 ctf_dynhash_remove (fp->ctf_link_in_cu_mapping, from);
410 }
411
412 f = strdup (from);
413 t = strdup (to);
414 if (!f || !t)
415 goto oom;
416
417 /* Track both in a list from FROM to TO and in a list from TO to a list of
418 FROM. The former is used to create TUs with the mapped-to name at need:
419 the latter is used in deduplicating links to pull in all input CUs
420 corresponding to a single output CU. */
421
422 if ((err = ctf_dynhash_insert (fp->ctf_link_in_cu_mapping, f, t)) < 0)
423 {
424 ctf_set_errno (fp, err);
425 goto oom_noerrno;
426 }
427
428 /* f and t are now owned by the in_cu_mapping: reallocate them. */
429 f = strdup (from);
430 t = strdup (to);
431 if (!f || !t)
432 goto oom;
433
434 if ((one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, t)) == NULL)
435 {
436 if ((one_out = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
437 free, NULL)) == NULL)
438 goto oom;
439 if ((err = ctf_dynhash_insert (fp->ctf_link_out_cu_mapping,
440 t, one_out)) < 0)
441 {
442 ctf_dynhash_destroy (one_out);
443 ctf_set_errno (fp, err);
444 goto oom_noerrno;
445 }
446 }
447 else
448 {
449 free (t);
450 t = NULL;
451 }
452
453 if (ctf_dynhash_insert (one_out, f, NULL) < 0)
454 {
455 ctf_set_errno (fp, err);
456 goto oom_noerrno;
457 }
458
459 return 0;
460
461 oom:
462 ctf_set_errno (fp, errno);
463 oom_noerrno:
464 free (f);
465 free (t);
466 return -1;
467}
468
469/* Set a function which is called to transform the names of archive members.
470 This is useful for applying regular transformations to many names, where
471 ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
472 names. The member name changer is applied at ctf_link_write time, so it
473 cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
474 The changer function accepts a name and should return a new
475 dynamically-allocated name, or NULL if the name should be left unchanged. */
476void
477ctf_link_set_memb_name_changer (ctf_dict_t *fp,
478 ctf_link_memb_name_changer_f *changer,
479 void *arg)
480{
481 fp->ctf_link_memb_name_changer = changer;
482 fp->ctf_link_memb_name_changer_arg = arg;
483}
484
485/* Set a function which is used to filter out unwanted variables from the link. */
486int
487ctf_link_set_variable_filter (ctf_dict_t *fp, ctf_link_variable_filter_f *filter,
488 void *arg)
489{
490 fp->ctf_link_variable_filter = filter;
491 fp->ctf_link_variable_filter_arg = arg;
492 return 0;
493}
494
495/* Check if we can safely add a variable with the given type to this dict. */
496
497static int
498check_variable (const char *name, ctf_dict_t *fp, ctf_id_t type,
499 ctf_dvdef_t **out_dvd)
500{
501 ctf_dvdef_t *dvd;
502
503 dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
504 *out_dvd = dvd;
505 if (!dvd)
506 return 1;
507
508 if (dvd->dvd_type != type)
509 {
510 /* Variable here. Wrong type: cannot add. Just skip it, because there is
511 no way to express this in CTF. Don't even warn: this case is too
512 common. (This might be the parent, in which case we'll try adding in
513 the child first, and only then give up.) */
514 ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
515 }
516
517 return 0; /* Already exists. */
518}
519
520/* Link one variable named NAME of type TYPE found in IN_FP into FP. */
521
522static int
523ctf_link_one_variable (ctf_dict_t *fp, ctf_dict_t *in_fp, const char *name,
524 ctf_id_t type, int cu_mapped)
525{
526 ctf_dict_t *per_cu_out_fp;
527 ctf_id_t dst_type = 0;
528 ctf_dvdef_t *dvd;
529
530 /* See if this variable is filtered out. */
531
532 if (fp->ctf_link_variable_filter)
533 {
534 void *farg = fp->ctf_link_variable_filter_arg;
535 if (fp->ctf_link_variable_filter (in_fp, name, type, farg))
536 return 0;
537 }
538
539 /* If this type is mapped to a type in the parent dict, we want to try to add
540 to that first: if it reports a duplicate, or if the type is in a child
541 already, add straight to the child. */
542
543 if ((dst_type = ctf_dedup_type_mapping (fp, in_fp, type)) == CTF_ERR)
544 return -1; /* errno is set for us. */
545
546 if (dst_type != 0)
547 {
548 if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
549 return -1; /* errno is set for us. */
550
551 if (check_variable (name, fp, dst_type, &dvd))
552 {
553 /* No variable here: we can add it. */
554 if (ctf_add_variable (fp, name, dst_type) < 0)
555 return -1; /* errno is set for us. */
556 return 0;
557 }
558
559 /* Already present? Nothing to do. */
560 if (dvd && dvd->dvd_type == dst_type)
561 return 0;
562 }
563
564 /* Can't add to the parent due to a name clash, or because it references a
565 type only present in the child. Try adding to the child, creating if need
566 be. If we can't do that, skip it. Don't add to a child if we're doing a
567 CU-mapped link, since that has only one output. */
568
569 if (cu_mapped)
570 {
571 ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden "
572 "due to conflicts: skipped.\n", name,
573 ctf_unnamed_cuname (in_fp), type);
574 return 0;
575 }
576
577 if ((per_cu_out_fp = ctf_create_per_cu (fp, in_fp, NULL)) == NULL)
578 return -1; /* errno is set for us. */
579
580 /* If the type was not found, check for it in the child too. */
581 if (dst_type == 0)
582 {
583 if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
584 in_fp, type)) == CTF_ERR)
585 return -1; /* errno is set for us. */
586
587 if (dst_type == 0)
588 {
589 ctf_err_warn (fp, 1, 0, _("type %lx for variable %s in input file %s "
590 "not found: skipped"), type, name,
591 ctf_unnamed_cuname (in_fp));
592 /* Do not terminate the link: just skip the variable. */
593 return 0;
594 }
595 }
596
597 if (check_variable (name, per_cu_out_fp, dst_type, &dvd))
598 if (ctf_add_variable (per_cu_out_fp, name, dst_type) < 0)
599 return (ctf_set_errno (fp, ctf_errno (per_cu_out_fp)));
600 return 0;
601}
602
603typedef struct link_sort_inputs_cb_arg
604{
605 int is_cu_mapped;
606 ctf_dict_t *fp;
607} link_sort_inputs_cb_arg_t;
608
609/* Sort the inputs by N (the link order). For CU-mapped links, this is a
610 mapping of input to output name, not a mapping of input name to input
611 ctf_link_input_t: compensate accordingly. */
612static int
613ctf_link_sort_inputs (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
614 void *arg)
615{
616 ctf_link_input_t *input_1;
617 ctf_link_input_t *input_2;
618 link_sort_inputs_cb_arg_t *cu_mapped = (link_sort_inputs_cb_arg_t *) arg;
619
620 if (!cu_mapped || !cu_mapped->is_cu_mapped)
621 {
622 input_1 = (ctf_link_input_t *) one->hkv_value;
623 input_2 = (ctf_link_input_t *) two->hkv_value;
624 }
625 else
626 {
627 const char *name_1 = (const char *) one->hkv_key;
628 const char *name_2 = (const char *) two->hkv_key;
629
630 input_1 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_1);
631 input_2 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_2);
632
633 /* There is no guarantee that CU-mappings actually have corresponding
634 inputs: the relative ordering in that case is unimportant. */
635 if (!input_1)
636 return -1;
637 if (!input_2)
638 return 1;
639 }
640
641 if (input_1->n < input_2->n)
642 return -1;
643 else if (input_1->n > input_2->n)
644 return 1;
645 else
646 return 0;
647}
648
649/* Count the number of input dicts in the ctf_link_inputs, or that subset of the
650 ctf_link_inputs given by CU_NAMES if set. Return the number of input dicts,
651 and optionally the name and ctf_link_input_t of the single input archive if
652 only one exists (no matter how many dicts it contains). */
653static ssize_t
654ctf_link_deduplicating_count_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
655 ctf_link_input_t **only_one_input)
656{
657 ctf_dynhash_t *inputs = fp->ctf_link_inputs;
658 ctf_next_t *i = NULL;
659 void *name, *input;
660 ctf_link_input_t *one_input = NULL;
661 const char *one_name = NULL;
662 ssize_t count = 0, narcs = 0;
663 int err;
664
665 if (cu_names)
666 inputs = cu_names;
667
668 while ((err = ctf_dynhash_next (inputs, &i, &name, &input)) == 0)
669 {
670 ssize_t one_count;
671
672 one_name = (const char *) name;
673 /* If we are processing CU names, get the real input. */
674 if (cu_names)
675 one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
676 else
677 one_input = (ctf_link_input_t *) input;
678
679 if (!one_input)
680 continue;
681
682 one_count = ctf_link_lazy_open (fp, one_input);
683
684 if (one_count < 0)
685 {
686 ctf_next_destroy (i);
687 return -1; /* errno is set for us. */
688 }
689
690 count += one_count;
691 narcs++;
692 }
693 if (err != ECTF_NEXT_END)
694 {
695 ctf_err_warn (fp, 0, err, _("iteration error counting deduplicating "
696 "CTF link inputs"));
697 return ctf_set_errno (fp, err);
698 }
699
700 if (!count)
701 return 0;
702
703 if (narcs == 1)
704 {
705 if (only_one_input)
706 *only_one_input = one_input;
707 }
708 else if (only_one_input)
709 *only_one_input = NULL;
710
711 return count;
712}
713
714/* Allocate and populate an inputs array big enough for a given set of inputs:
715 either a specific set of CU names (those from that set found in the
716 ctf_link_inputs), or the entire ctf_link_inputs (if cu_names is not set).
717 The number of inputs (from ctf_link_deduplicating_count_inputs, above) is
718 passed in NINPUTS: an array of uint32_t containing parent pointers
719 (corresponding to those members of the inputs that have parents) is allocated
720 and returned in PARENTS.
721
722 The inputs are *archives*, not files: the archive can have multiple members
723 if it is the result of a previous incremental link. We want to add every one
724 in turn, including the shared parent. (The dedup machinery knows that a type
725 used by a single dictionary and its parent should not be shared in
726 CTF_LINK_SHARE_DUPLICATED mode.)
727
728 If no inputs exist that correspond to these CUs, return NULL with the errno
729 set to ECTF_NOCTFDATA. */
730static ctf_dict_t **
731ctf_link_deduplicating_open_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
732 ssize_t ninputs, uint32_t **parents)
733{
734 ctf_dynhash_t *inputs = fp->ctf_link_inputs;
735 ctf_next_t *i = NULL;
736 void *name, *input;
737 link_sort_inputs_cb_arg_t sort_arg;
738 ctf_dict_t **dedup_inputs = NULL;
739 ctf_dict_t **walk;
740 uint32_t *parents_ = NULL;
741 int err;
742
743 if (cu_names)
744 inputs = cu_names;
745
746 if ((dedup_inputs = calloc (ninputs, sizeof (ctf_dict_t *))) == NULL)
747 goto oom;
748
749 if ((parents_ = calloc (ninputs, sizeof (uint32_t))) == NULL)
750 goto oom;
751
752 walk = dedup_inputs;
753
754 /* Counting done: push every input into the array, in the order they were
755 passed to ctf_link_add_ctf (and ultimately ld). */
756
757 sort_arg.is_cu_mapped = (cu_names != NULL);
758 sort_arg.fp = fp;
759
760 while ((err = ctf_dynhash_next_sorted (inputs, &i, &name, &input,
761 ctf_link_sort_inputs, &sort_arg)) == 0)
762 {
763 const char *one_name = (const char *) name;
764 ctf_link_input_t *one_input;
765 ctf_dict_t *one_fp;
766 ctf_dict_t *parent_fp = NULL;
767 uint32_t parent_i = 0;
768 ctf_next_t *j = NULL;
769
770 /* If we are processing CU names, get the real input. All the inputs
771 will have been opened, if they contained any CTF at all. */
772 if (cu_names)
773 one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
774 else
775 one_input = (ctf_link_input_t *) input;
776
777 if (!one_input || (!one_input->clin_arc && !one_input->clin_fp))
778 continue;
779
780 /* Short-circuit: if clin_fp is set, just use it. */
781 if (one_input->clin_fp)
782 {
783 parents_[walk - dedup_inputs] = walk - dedup_inputs;
784 *walk = one_input->clin_fp;
785 walk++;
786 continue;
787 }
788
789 /* Get and insert the parent archive (if any), if this archive has
790 multiple members. We assume, as elsewhere, that the parent is named
791 _CTF_SECTION. */
792
793 if ((parent_fp = ctf_dict_open (one_input->clin_arc, _CTF_SECTION,
794 &err)) == NULL)
795 {
796 if (err != ECTF_NOMEMBNAM)
797 {
798 ctf_next_destroy (i);
799 ctf_set_errno (fp, err);
800 goto err;
801 }
802 }
803 else
804 {
805 *walk = parent_fp;
806 parent_i = walk - dedup_inputs;
807 walk++;
808 }
809
810 /* We disregard the input archive name: either it is the parent (which we
811 already have), or we want to put everything into one TU sharing the
812 cuname anyway (if this is a CU-mapped link), or this is the final phase
813 of a relink with CU-mapping off (i.e. ld -r) in which case the cuname
814 is correctly set regardless. */
815 while ((one_fp = ctf_archive_next (one_input->clin_arc, &j, NULL,
816 1, &err)) != NULL)
817 {
818 if (one_fp->ctf_flags & LCTF_CHILD)
819 {
820 /* The contents of the parents array for elements not
821 corresponding to children is undefined. If there is no parent
822 (itself a sign of a likely linker bug or corrupt input), we set
823 it to itself. */
824
825 ctf_import (one_fp, parent_fp);
826 if (parent_fp)
827 parents_[walk - dedup_inputs] = parent_i;
828 else
829 parents_[walk - dedup_inputs] = walk - dedup_inputs;
830 }
831 *walk = one_fp;
832 walk++;
833 }
834 if (err != ECTF_NEXT_END)
835 {
836 ctf_next_destroy (i);
837 goto iterr;
838 }
839 }
840 if (err != ECTF_NEXT_END)
841 goto iterr;
842
843 *parents = parents_;
844
845 return dedup_inputs;
846
847 oom:
848 err = ENOMEM;
849
850 iterr:
851 ctf_set_errno (fp, err);
852
853 err:
854 free (dedup_inputs);
855 free (parents_);
856 ctf_err_warn (fp, 0, 0, _("error in deduplicating CTF link "
857 "input allocation"));
858 return NULL;
859}
860
861/* Close INPUTS that have already been linked, first the passed array, and then
862 that subset of the ctf_link_inputs archives they came from cited by the
863 CU_NAMES. If CU_NAMES is not specified, close all the ctf_link_inputs in one
864 go, leaving it empty. */
865static int
866ctf_link_deduplicating_close_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
867 ctf_dict_t **inputs, ssize_t ninputs)
868{
869 ctf_next_t *it = NULL;
870 void *name;
871 int err;
872 ssize_t i;
873
874 /* This is the inverse of ctf_link_deduplicating_open_inputs: so first, close
875 all the individual input dicts, opened by the archive iterator. */
876 for (i = 0; i < ninputs; i++)
877 ctf_dict_close (inputs[i]);
878
879 /* Now close the archives they are part of. */
880 if (cu_names)
881 {
882 while ((err = ctf_dynhash_next (cu_names, &it, &name, NULL)) == 0)
883 {
884 /* Remove the input from the linker inputs, if it exists, which also
885 closes it. */
886
887 ctf_dynhash_remove (fp->ctf_link_inputs, (const char *) name);
888 }
889 if (err != ECTF_NEXT_END)
890 {
891 ctf_set_errno (fp, err);
892 ctf_err_warn (fp, 0, 0, _("iteration error in deduplicating link "
893 "input freeing"));
894 }
895 }
896 else
897 ctf_dynhash_empty (fp->ctf_link_inputs);
898
899 return 0;
900}
901
902/* Do a deduplicating link of all variables in the inputs.
903
904 Also, if we are not omitting the variable section, integrate all symbols from
905 the symtypetabs into the variable section too. (Duplication with the
906 symtypetab section in the output will be eliminated at serialization time.) */
907
908static int
909ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs,
910 size_t ninputs, int cu_mapped)
911{
912 size_t i;
913
914 for (i = 0; i < ninputs; i++)
915 {
916 ctf_next_t *it = NULL;
917 ctf_id_t type;
918 const char *name;
919
920 /* First the variables on the inputs. */
921
922 while ((type = ctf_variable_next (inputs[i], &it, &name)) != CTF_ERR)
923 {
924 if (ctf_link_one_variable (fp, inputs[i], name, type, cu_mapped) < 0)
925 {
926 ctf_next_destroy (it);
927 return -1; /* errno is set for us. */
928 }
929 }
930 if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
931 return ctf_set_errno (fp, ctf_errno (inputs[i]));
932
933 /* Next the symbols. We integrate data symbols even though the compiler
934 is currently doing the same, to allow the compiler to stop in
935 future. */
936
937 while ((type = ctf_symbol_next (inputs[i], &it, &name, 0)) != CTF_ERR)
938 {
939 if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
940 {
941 ctf_next_destroy (it);
942 return -1; /* errno is set for us. */
943 }
944 }
945 if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
946 return ctf_set_errno (fp, ctf_errno (inputs[i]));
947
948 /* Finally the function symbols. */
949
950 while ((type = ctf_symbol_next (inputs[i], &it, &name, 1)) != CTF_ERR)
951 {
952 if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
953 {
954 ctf_next_destroy (it);
955 return -1; /* errno is set for us. */
956 }
957 }
958 if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
959 return ctf_set_errno (fp, ctf_errno (inputs[i]));
960 }
961 return 0;
962}
963
964/* Check for symbol conflicts during linking. Three possibilities: already
965 exists, conflicting, or nonexistent. We don't have a dvd structure we can
966 use as a flag like check_variable does, so we use a tristate return
967 value instead: -1: conflicting; 1: nonexistent: 0: already exists. */
968
969static int
970check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions)
971{
972 ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash;
973 ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash;
974 void *value;
975
976 /* Wrong type (function when object is wanted, etc). */
977 if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL))
978 return -1;
979
980 /* Not present at all yet. */
981 if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value))
982 return 1;
983
984 /* Already present. */
985 if ((ctf_id_t) (uintptr_t) value == type)
986 return 0;
987
988 /* Wrong type. */
989 return -1;
990}
991
992/* Do a deduplicating link of one symtypetab (function info or data object) in
993 one input dict. */
994
995static int
996ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input,
997 int cu_mapped, int functions)
998{
999 ctf_next_t *it = NULL;
1000 const char *name;
1001 ctf_id_t type;
1002
1003 while ((type = ctf_symbol_next (input, &it, &name, functions)) != CTF_ERR)
1004 {
1005 ctf_id_t dst_type;
1006 ctf_dict_t *per_cu_out_fp;
1007 int sym;
1008
1009 /* Look in the parent first. */
1010
1011 if ((dst_type = ctf_dedup_type_mapping (fp, input, type)) == CTF_ERR)
1012 return -1; /* errno is set for us. */
1013
1014 if (dst_type != 0)
1015 {
1016 if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
1017 return -1; /* errno is set for us. */
1018
1019 sym = check_sym (fp, name, dst_type, functions);
1020
1021 /* Already present: next symbol. */
1022 if (sym == 0)
1023 continue;
1024 /* Not present: add it. */
1025 else if (sym > 0)
1026 {
1027 if (ctf_add_funcobjt_sym (fp, functions,
1028 name, dst_type) < 0)
1029 return -1; /* errno is set for us. */
1030 continue;
1031 }
1032 }
1033
1034 /* Can't add to the parent due to a name clash (most unlikely), or because
1035 it references a type only present in the child. Try adding to the
1036 child, creating if need be. If we can't do that, skip it. Don't add
1037 to a child if we're doing a CU-mapped link, since that has only one
1038 output. */
1039 if (cu_mapped)
1040 {
1041 ctf_dprintf ("Symbol %s in input file %s depends on a type %lx "
1042 "hidden due to conflicts: skipped.\n", name,
1043 ctf_unnamed_cuname (input), type);
1044 continue;
1045 }
1046
1047 if ((per_cu_out_fp = ctf_create_per_cu (fp, input, NULL)) == NULL)
1048 return -1; /* errno is set for us. */
1049
1050 /* If the type was not found, check for it in the child too. */
1051 if (dst_type == 0)
1052 {
1053 if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
1054 input, type)) == CTF_ERR)
1055 return -1; /* errno is set for us. */
1056
1057 if (dst_type == 0)
1058 {
1059 ctf_err_warn (fp, 1, 0,
1060 _("type %lx for symbol %s in input file %s "
1061 "not found: skipped"), type, name,
1062 ctf_unnamed_cuname (input));
1063 continue;
1064 }
1065 }
1066
1067 sym = check_sym (per_cu_out_fp, name, dst_type, functions);
1068
1069 /* Already present: next symbol. */
1070 if (sym == 0)
1071 continue;
1072 /* Not present: add it. */
1073 else if (sym > 0)
1074 {
1075 if (ctf_add_funcobjt_sym (per_cu_out_fp, functions,
1076 name, dst_type) < 0)
1077 return -1; /* errno is set for us. */
1078 }
1079 else
1080 {
1081 /* Perhaps this should be an assertion failure. */
1082 ctf_err_warn (fp, 0, ECTF_DUPLICATE,
1083 _("symbol %s in input file %s found conflicting "
1084 "even when trying in per-CU dict."), name,
1085 ctf_unnamed_cuname (input));
1086 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1087 }
1088 }
1089 if (ctf_errno (input) != ECTF_NEXT_END)
1090 {
1091 ctf_set_errno (fp, ctf_errno (input));
1092 ctf_err_warn (fp, 0, 0, functions ?
1093 _("iterating over function symbols") :
1094 _("iterating over data symbols"));
1095 return -1;
1096 }
1097
1098 return 0;
1099}
1100
1101/* Do a deduplicating link of the function info and data objects
1102 in the inputs. */
1103static int
1104ctf_link_deduplicating_syms (ctf_dict_t *fp, ctf_dict_t **inputs,
1105 size_t ninputs, int cu_mapped)
1106{
1107 size_t i;
1108
1109 for (i = 0; i < ninputs; i++)
1110 {
1111 if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
1112 cu_mapped, 0) < 0)
1113 return -1; /* errno is set for us. */
1114
1115 if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
1116 cu_mapped, 1) < 0)
1117 return -1; /* errno is set for us. */
1118 }
1119
1120 return 0;
1121}
1122
1123/* Do the per-CU part of a deduplicating link. */
1124static int
1125ctf_link_deduplicating_per_cu (ctf_dict_t *fp)
1126{
1127 ctf_next_t *i = NULL;
1128 int err;
1129 void *out_cu;
1130 void *in_cus;
1131
1132 /* Links with a per-CU mapping in force get a first pass of deduplication,
1133 dedupping the inputs for a given CU mapping into the output for that
1134 mapping. The outputs from this process get fed back into the final pass
1135 that is carried out even for non-CU links. */
1136
1137 while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &out_cu,
1138 &in_cus)) == 0)
1139 {
1140 const char *out_name = (const char *) out_cu;
1141 ctf_dynhash_t *in = (ctf_dynhash_t *) in_cus;
1142 ctf_dict_t *out = NULL;
1143 ctf_dict_t **inputs;
1144 ctf_dict_t **outputs;
1145 ctf_archive_t *in_arc;
1146 ssize_t ninputs;
1147 ctf_link_input_t *only_input;
1148 uint32_t noutputs;
1149 uint32_t *parents;
1150
1151 if ((ninputs = ctf_link_deduplicating_count_inputs (fp, in,
1152 &only_input)) == -1)
1153 goto err_open_inputs;
1154
1155 /* CU mapping with no inputs? Skip. */
1156 if (ninputs == 0)
1157 continue;
1158
1159 if (labs ((long int) ninputs) > 0xfffffffe)
1160 {
1161 ctf_set_errno (fp, EFBIG);
1162 ctf_err_warn (fp, 0, 0, _("too many inputs in deduplicating "
1163 "link: %li"), (long int) ninputs);
1164 goto err_open_inputs;
1165 }
1166
1167 /* Short-circuit: a cu-mapped link with only one input archive with
1168 unconflicting contents is a do-nothing, and we can just leave the input
1169 in place: we do have to change the cuname, though, so we unwrap it,
1170 change the cuname, then stuff it back in the linker input again, via
1171 the clin_fp short-circuit member. ctf_link_deduplicating_open_inputs
1172 will spot this member and jam it straight into the next link phase,
1173 ignoring the corresponding archive. */
1174 if (only_input && ninputs == 1)
1175 {
1176 ctf_next_t *ai = NULL;
1177 int err;
1178
1179 /* We can abuse an archive iterator to get the only member cheaply, no
1180 matter what its name. */
1181 only_input->clin_fp = ctf_archive_next (only_input->clin_arc,
1182 &ai, NULL, 0, &err);
1183 if (!only_input->clin_fp)
1184 {
1185 ctf_set_errno (fp, err);
1186 ctf_err_warn (fp, 0, 0, _("cannot open archive %s in "
1187 "CU-mapped CTF link"),
1188 only_input->clin_filename);
1189 goto err_open_inputs;
1190 }
1191 ctf_next_destroy (ai);
1192
1193 if (strcmp (only_input->clin_filename, out_name) != 0)
1194 {
1195 /* Renaming. We need to add a new input, then null out the
1196 clin_arc and clin_fp of the old one to stop it being
1197 auto-closed on removal. The new input needs its cuname changed
1198 to out_name, which is doable only because the cuname is a
1199 dynamic property which can be changed even in readonly
1200 dicts. */
1201
1202 ctf_cuname_set (only_input->clin_fp, out_name);
1203 if (ctf_link_add_ctf_internal (fp, only_input->clin_arc,
1204 only_input->clin_fp,
1205 out_name) < 0)
1206 {
1207 ctf_err_warn (fp, 0, 0, _("cannot add intermediate files "
1208 "to link"));
1209 goto err_open_inputs;
1210 }
1211 only_input->clin_arc = NULL;
1212 only_input->clin_fp = NULL;
1213 ctf_dynhash_remove (fp->ctf_link_inputs,
1214 only_input->clin_filename);
1215 }
1216 continue;
1217 }
1218
1219 /* This is a real CU many-to-one mapping: we must dedup the inputs into
1220 a new output to be used in the final link phase. */
1221
1222 if ((inputs = ctf_link_deduplicating_open_inputs (fp, in, ninputs,
1223 &parents)) == NULL)
1224 {
1225 ctf_next_destroy (i);
1226 goto err_open_inputs;
1227 }
1228
1229 if ((out = ctf_create (&err)) == NULL)
1230 {
1231 ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive "
1232 "for %s"),
1233 out_name);
1234 ctf_set_errno (fp, err);
1235 goto err_inputs;
1236 }
1237
1238 /* The deduplicator is ready for strict enumerator value checking. */
1239 out->ctf_flags |= LCTF_STRICT_NO_DUP_ENUMERATORS;
1240
1241 /* Share the atoms table to reduce memory usage. */
1242 out->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc;
1243
1244 /* No ctf_imports at this stage: this per-CU dictionary has no parents.
1245 Parent/child deduplication happens in the link's final pass. However,
1246 the cuname *is* important, as it is propagated into the final
1247 dictionary. */
1248 ctf_cuname_set (out, out_name);
1249
1250 if (ctf_dedup (out, inputs, ninputs, 1) < 0)
1251 {
1252 ctf_set_errno (fp, ctf_errno (out));
1253 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplication failed for %s"),
1254 out_name);
1255 goto err_inputs;
1256 }
1257
1258 if ((outputs = ctf_dedup_emit (out, inputs, ninputs, parents,
1259 &noutputs, 1)) == NULL)
1260 {
1261 ctf_set_errno (fp, ctf_errno (out));
1262 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link type emission "
1263 "failed for %s"), out_name);
1264 goto err_inputs;
1265 }
1266 if (!ctf_assert (fp, noutputs == 1))
1267 {
1268 size_t j;
1269 for (j = 1; j < noutputs; j++)
1270 ctf_dict_close (outputs[j]);
1271 goto err_inputs_outputs;
1272 }
1273
1274 if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1275 && ctf_link_deduplicating_variables (out, inputs, ninputs, 1) < 0)
1276 {
1277 ctf_set_errno (fp, ctf_errno (out));
1278 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link variable "
1279 "emission failed for %s"), out_name);
1280 goto err_inputs_outputs;
1281 }
1282
1283 ctf_dedup_fini (out, outputs, noutputs);
1284
1285 /* For now, we omit symbol section linking for CU-mapped links, until it
1286 is clear how to unify the symbol table across such links. (Perhaps we
1287 should emit an unconditionally indexed symtab, like the compiler
1288 does.) */
1289
1290 if (ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs) < 0)
1291 {
1292 free (inputs);
1293 free (parents);
1294 goto err_outputs;
1295 }
1296 free (inputs);
1297 free (parents);
1298
1299 /* Splice any errors or warnings created during this link back into the
1300 dict that the caller knows about. */
1301 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1302
1303 /* This output now becomes an input to the next link phase, with a name
1304 equal to the CU name. We have to wrap it in an archive wrapper
1305 first. */
1306
1307 if ((in_arc = ctf_new_archive_internal (0, 0, NULL, outputs[0], NULL,
1308 NULL, &err)) == NULL)
1309 {
1310 ctf_set_errno (fp, err);
1311 goto err_outputs;
1312 }
1313
1314 if (ctf_link_add_ctf_internal (fp, in_arc, NULL,
1315 ctf_cuname (outputs[0])) < 0)
1316 {
1317 ctf_err_warn (fp, 0, 0, _("cannot add intermediate files to link"));
1318 goto err_outputs;
1319 }
1320
1321 ctf_dict_close (out);
1322 free (outputs);
1323 continue;
1324
1325 err_inputs_outputs:
1326 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1327 ctf_dict_close (outputs[0]);
1328 free (outputs);
1329 err_inputs:
1330 ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs);
1331 ctf_dict_close (out);
1332 free (inputs);
1333 free (parents);
1334 err_open_inputs:
1335 ctf_next_destroy (i);
1336 return -1;
1337
1338 err_outputs:
1339 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1340 ctf_dict_close (outputs[0]);
1341 free (outputs);
1342 ctf_next_destroy (i);
1343 return -1; /* Errno is set for us. */
1344 }
1345 if (err != ECTF_NEXT_END)
1346 {
1347 ctf_err_warn (fp, 0, err, _("iteration error in CU-mapped deduplicating "
1348 "link"));
1349 return ctf_set_errno (fp, err);
1350 }
1351
1352 return 0;
1353}
1354
1355/* Empty all the ctf_link_outputs. */
1356static int
1357ctf_link_empty_outputs (ctf_dict_t *fp)
1358{
1359 ctf_next_t *i = NULL;
1360 void *v;
1361 int err;
1362
1363 ctf_dynhash_empty (fp->ctf_link_outputs);
1364
1365 while ((err = ctf_dynhash_next (fp->ctf_link_inputs, &i, NULL, &v)) == 0)
1366 {
1367 ctf_dict_t *in = (ctf_dict_t *) v;
1368 in->ctf_link_in_out = NULL;
1369 }
1370 if (err != ECTF_NEXT_END)
1371 {
1372 fp->ctf_flags &= ~LCTF_LINKING;
1373 ctf_err_warn (fp, 1, err, _("iteration error removing old outputs"));
1374 return ctf_set_errno (fp, err);
1375 }
1376 return 0;
1377}
1378
1379/* Do a deduplicating link using the ctf-dedup machinery. */
1380static void
1381ctf_link_deduplicating (ctf_dict_t *fp)
1382{
1383 size_t i;
1384 ctf_dict_t **inputs, **outputs = NULL;
1385 ssize_t ninputs;
1386 uint32_t noutputs;
1387 uint32_t *parents;
1388 int cu_phase = 0;
1389
1390 if (ctf_dedup_atoms_init (fp) < 0)
1391 {
1392 ctf_err_warn (fp, 0, 0, _("allocating CTF dedup atoms table"));
1393 return; /* Errno is set for us. */
1394 }
1395
1396 /* Trigger a CU-mapped link if need be: one pass of dedups squashing inputs into
1397 single child dicts corresponding to each CU mapping, and one pass that
1398 treats those as if they are ordinary inputs and links them together.
1399
1400 This latter pass does need to act very slightly differently from normal, so we
1401 keep track of the "CU phase", with 0 being a normal link, 1 being the
1402 squash-together phase, and 2 being the final act-as-if-it-were-normal pass. */
1403
1404 if (fp->ctf_link_out_cu_mapping)
1405 {
1406 if (ctf_link_deduplicating_per_cu (fp) < 0)
1407 return; /* Errno is set for us. */
1408 cu_phase = 2;
1409 }
1410
1411 if ((ninputs = ctf_link_deduplicating_count_inputs (fp, NULL, NULL)) < 0)
1412 return; /* Errno is set for us. */
1413
1414 if ((inputs = ctf_link_deduplicating_open_inputs (fp, NULL, ninputs,
1415 &parents)) == NULL)
1416 return; /* Errno is set for us. */
1417
1418 if (ninputs == 1 && ctf_cuname (inputs[0]) != NULL)
1419 ctf_cuname_set (fp, ctf_cuname (inputs[0]));
1420
1421 if (ctf_dedup (fp, inputs, ninputs, cu_phase) < 0)
1422 {
1423 ctf_err_warn (fp, 0, 0, _("deduplication failed for %s"),
1424 ctf_link_input_name (fp));
1425 goto err;
1426 }
1427
1428 if ((outputs = ctf_dedup_emit (fp, inputs, ninputs, parents, &noutputs,
1429 cu_phase)) == NULL)
1430 {
1431 ctf_err_warn (fp, 0, 0, _("deduplicating link type emission failed "
1432 "for %s"), ctf_link_input_name (fp));
1433 goto err;
1434 }
1435
1436 if (!ctf_assert (fp, outputs[0] == fp))
1437 {
1438 for (i = 1; i < noutputs; i++)
1439 ctf_dict_close (outputs[i]);
1440 goto err;
1441 }
1442
1443 for (i = 0; i < noutputs; i++)
1444 {
1445 char *dynname;
1446
1447 /* We already have access to this one. Close the duplicate. */
1448 if (i == 0)
1449 {
1450 ctf_dict_close (outputs[0]);
1451 continue;
1452 }
1453
1454 if ((dynname = ctf_new_per_cu_name (fp, ctf_cuname (outputs[i]))) == NULL)
1455 goto oom_one_output;
1456
1457 if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, outputs[i]) < 0)
1458 goto oom_one_output;
1459
1460 continue;
1461
1462 oom_one_output:
1463 ctf_set_errno (fp, ENOMEM);
1464 ctf_err_warn (fp, 0, 0, _("out of memory allocating link outputs"));
1465 free (dynname);
1466
1467 for (; i < noutputs; i++)
1468 ctf_dict_close (outputs[i]);
1469 goto err;
1470 }
1471
1472 if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1473 && ctf_link_deduplicating_variables (fp, inputs, ninputs, 0) < 0)
1474 {
1475 ctf_err_warn (fp, 0, 0, _("deduplicating link variable emission failed for "
1476 "%s"), ctf_link_input_name (fp));
1477 goto err_clean_outputs;
1478 }
1479
1480 if (ctf_link_deduplicating_syms (fp, inputs, ninputs, 0) < 0)
1481 {
1482 ctf_err_warn (fp, 0, 0, _("deduplicating link symbol emission failed for "
1483 "%s"), ctf_link_input_name (fp));
1484 goto err_clean_outputs;
1485 }
1486
1487 ctf_dedup_fini (fp, outputs, noutputs);
1488
1489 /* Now close all the inputs, including per-CU intermediates. */
1490
1491 if (ctf_link_deduplicating_close_inputs (fp, NULL, inputs, ninputs) < 0)
1492 return; /* errno is set for us. */
1493
1494 ninputs = 0; /* Prevent double-close. */
1495 ctf_set_errno (fp, 0);
1496
1497 /* Fall through. */
1498
1499 err:
1500 for (i = 0; i < (size_t) ninputs; i++)
1501 ctf_dict_close (inputs[i]);
1502 free (inputs);
1503 free (parents);
1504 free (outputs);
1505 return;
1506
1507 err_clean_outputs:
1508 ctf_link_empty_outputs (fp);
1509 goto err;
1510}
1511
1512/* Merge types and variable sections in all dicts added to the link together.
1513 The result of any previous link is discarded. */
1514int
1515ctf_link (ctf_dict_t *fp, int flags)
1516{
1517 int err;
1518 int oldflags = fp->ctf_flags;
1519
1520 fp->ctf_link_flags = flags;
1521
1522 if (fp->ctf_link_inputs == NULL)
1523 return 0; /* Nothing to do. */
1524
1525 if (fp->ctf_link_outputs != NULL)
1526 ctf_link_empty_outputs (fp);
1527 else
1528 fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
1529 ctf_hash_eq_string, free,
1530 (ctf_hash_free_fun)
1531 ctf_dict_close);
1532
1533 if (fp->ctf_link_outputs == NULL)
1534 return ctf_set_errno (fp, ENOMEM);
1535
1536 fp->ctf_flags |= LCTF_LINKING & LCTF_STRICT_NO_DUP_ENUMERATORS;
1537 ctf_link_deduplicating (fp);
1538 fp->ctf_flags = oldflags;
1539
1540 if ((ctf_errno (fp) != 0) && (ctf_errno (fp) != ECTF_NOCTFDATA))
1541 return -1;
1542
1543 /* Create empty CUs if requested. We do not currently claim that multiple
1544 links in succession with CTF_LINK_EMPTY_CU_MAPPINGS set in some calls and
1545 not set in others will do anything especially sensible. */
1546
1547 if (fp->ctf_link_out_cu_mapping && (flags & CTF_LINK_EMPTY_CU_MAPPINGS))
1548 {
1549 ctf_next_t *i = NULL;
1550 void *k;
1551
1552 while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &k,
1553 NULL)) == 0)
1554 {
1555 const char *to = (const char *) k;
1556 if (ctf_create_per_cu (fp, NULL, to) == NULL)
1557 {
1558 fp->ctf_flags = oldflags;
1559 ctf_next_destroy (i);
1560 return -1; /* Errno is set for us. */
1561 }
1562 }
1563 if (err != ECTF_NEXT_END)
1564 {
1565 fp->ctf_flags = oldflags;
1566 ctf_err_warn (fp, 1, err, _("iteration error creating empty CUs"));
1567 return ctf_set_errno (fp, err);
1568 }
1569 }
1570
1571 return 0;
1572}
1573
1574typedef struct ctf_link_out_string_cb_arg
1575{
1576 const char *str;
1577 uint32_t offset;
1578 int err;
1579} ctf_link_out_string_cb_arg_t;
1580
1581/* Intern a string in the string table of an output per-CU CTF file. */
1582static void
1583ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
1584 void *arg_)
1585{
1586 ctf_dict_t *fp = (ctf_dict_t *) value;
1587 ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
1588
1589 if (!ctf_str_add_external (fp, arg->str, arg->offset))
1590 arg->err = ENOMEM;
1591}
1592
1593/* Repeatedly call ADD_STRING to acquire strings from the external string table,
1594 adding them to the atoms table for this CU and all subsidiary CUs.
1595
1596 Must be called on a dict that has not yet been serialized.
1597
1598 If ctf_link is also called, it must be called first if you want the new CTF
1599 files ctf_link can create to get their strings dedupped against the ELF
1600 strtab properly. */
1601int
1602ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string,
1603 void *arg)
1604{
1605 const char *str;
1606 uint32_t offset;
1607 int err = 0;
1608
1609 if (fp->ctf_stypes > 0)
1610 return ctf_set_errno (fp, ECTF_RDONLY);
1611
1612 while ((str = add_string (&offset, arg)) != NULL)
1613 {
1614 ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
1615
1616 if (!ctf_str_add_external (fp, str, offset))
1617 err = ENOMEM;
1618
1619 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
1620 &iter_arg);
1621 if (iter_arg.err)
1622 err = iter_arg.err;
1623 }
1624
1625 if (err)
1626 ctf_set_errno (fp, err);
1627
1628 return -err;
1629}
1630
1631/* Inform the ctf-link machinery of a new symbol in the target symbol table
1632 (which must be some symtab that is not usually stripped, and which
1633 is in agreement with ctf_bfdopen_ctfsect). May be called either before or
1634 after ctf_link_add_strtab. As with that function, must be called on a dict which
1635 has not yet been serialized. */
1636int
1637ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym)
1638{
1639 ctf_in_flight_dynsym_t *cid;
1640
1641 /* Cheat a little: if there is already an ENOMEM error code recorded against
1642 this dict, we shouldn't even try to add symbols because there will be no
1643 memory to do so: probably we failed to add some previous symbol. This
1644 makes out-of-memory exits 'sticky' across calls to this function, so the
1645 caller doesn't need to worry about error conditions. */
1646
1647 if (ctf_errno (fp) == ENOMEM)
1648 return -ENOMEM; /* errno is set for us. */
1649
1650 if (fp->ctf_stypes > 0)
1651 return ctf_set_errno (fp, ECTF_RDONLY);
1652
1653 if (ctf_symtab_skippable (sym))
1654 return 0;
1655
1656 if (sym->st_type != STT_OBJECT && sym->st_type != STT_FUNC)
1657 return 0;
1658
1659 /* Add the symbol to the in-flight list. */
1660
1661 if ((cid = malloc (sizeof (ctf_in_flight_dynsym_t))) == NULL)
1662 goto oom;
1663
1664 cid->cid_sym = *sym;
1665 ctf_list_append (&fp->ctf_in_flight_dynsyms, cid);
1666
1667 return 0;
1668
1669 oom:
1670 ctf_dynhash_destroy (fp->ctf_dynsyms);
1671 fp->ctf_dynsyms = NULL;
1672 ctf_set_errno (fp, ENOMEM);
1673 return -ENOMEM;
1674}
1675
1676/* Impose an ordering on symbols. The ordering takes effect immediately, but
1677 since the ordering info does not include type IDs, lookups may return nothing
1678 until such IDs are added by calls to ctf_add_*_sym. Must be called after
1679 ctf_link_add_strtab and ctf_link_add_linker_symbol. */
1680int
1681ctf_link_shuffle_syms (ctf_dict_t *fp)
1682{
1683 ctf_in_flight_dynsym_t *did, *nid;
1684 ctf_next_t *i = NULL;
1685 int err = ENOMEM;
1686 void *name_, *sym_;
1687
1688 if (fp->ctf_stypes > 0)
1689 return ctf_set_errno (fp, ECTF_RDONLY);
1690
1691 if (!fp->ctf_dynsyms)
1692 {
1693 fp->ctf_dynsyms = ctf_dynhash_create (ctf_hash_string,
1694 ctf_hash_eq_string,
1695 NULL, free);
1696 if (!fp->ctf_dynsyms)
1697 {
1698 ctf_set_errno (fp, ENOMEM);
1699 return -ENOMEM;
1700 }
1701 }
1702
1703 /* Add all the symbols, excluding only those we already know are prohibited
1704 from appearing in symtypetabs. */
1705
1706 for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid)
1707 {
1708 ctf_link_sym_t *new_sym;
1709
1710 nid = ctf_list_next (did);
1711 ctf_list_delete (&fp->ctf_in_flight_dynsyms, did);
1712
1713 /* We might get a name or an external strtab offset. The strtab offset is
1714 guaranteed resolvable at this point, so turn it into a string. */
1715
1716 if (did->cid_sym.st_name == NULL)
1717 {
1718 uint32_t off = CTF_SET_STID (did->cid_sym.st_nameidx, CTF_STRTAB_1);
1719
1720 did->cid_sym.st_name = ctf_strraw (fp, off);
1721 did->cid_sym.st_nameidx_set = 0;
1722 if (!ctf_assert (fp, did->cid_sym.st_name != NULL))
1723 return -ECTF_INTERNAL; /* errno is set for us. */
1724 }
1725
1726 /* The symbol might have turned out to be nameless, so we have to recheck
1727 for skippability here. */
1728 if (!ctf_symtab_skippable (&did->cid_sym))
1729 {
1730 ctf_dprintf ("symbol from linker: %s (%x)\n", did->cid_sym.st_name,
1731 did->cid_sym.st_symidx);
1732
1733 if ((new_sym = malloc (sizeof (ctf_link_sym_t))) == NULL)
1734 goto local_oom;
1735
1736 memcpy (new_sym, &did->cid_sym, sizeof (ctf_link_sym_t));
1737 if (ctf_dynhash_cinsert (fp->ctf_dynsyms, new_sym->st_name, new_sym) < 0)
1738 goto local_oom;
1739
1740 if (fp->ctf_dynsymmax < new_sym->st_symidx)
1741 fp->ctf_dynsymmax = new_sym->st_symidx;
1742 }
1743
1744 free (did);
1745 continue;
1746
1747 local_oom:
1748 free (did);
1749 free (new_sym);
1750 goto err;
1751 }
1752
1753 /* If no symbols are reported, unwind what we have done and return. This
1754 makes it a bit easier for the serializer to tell that no symbols have been
1755 reported and that it should look elsewhere for reported symbols. */
1756 if (!ctf_dynhash_elements (fp->ctf_dynsyms))
1757 {
1758 ctf_dprintf ("No symbols: not a final link.\n");
1759 ctf_dynhash_destroy (fp->ctf_dynsyms);
1760 fp->ctf_dynsyms = NULL;
1761 return 0;
1762 }
1763
1764 /* Construct a mapping from shndx to the symbol info. */
1765 free (fp->ctf_dynsymidx);
1766 if ((fp->ctf_dynsymidx = calloc (fp->ctf_dynsymmax + 1,
1767 sizeof (ctf_link_sym_t *))) == NULL)
1768 goto err;
1769
1770 while ((err = ctf_dynhash_next (fp->ctf_dynsyms, &i, &name_, &sym_)) == 0)
1771 {
1772 const char *name = (const char *) name;
1773 ctf_link_sym_t *symp = (ctf_link_sym_t *) sym_;
1774
1775 if (!ctf_assert (fp, symp->st_symidx <= fp->ctf_dynsymmax))
1776 {
1777 ctf_next_destroy (i);
1778 err = ctf_errno (fp);
1779 goto err;
1780 }
1781 fp->ctf_dynsymidx[symp->st_symidx] = symp;
1782 }
1783 if (err != ECTF_NEXT_END)
1784 {
1785 ctf_err_warn (fp, 0, err, _("error iterating over shuffled symbols"));
1786 goto err;
1787 }
1788 return 0;
1789
1790 err:
1791 /* Leave the in-flight symbols around: they'll be freed at
1792 dict close time regardless. */
1793 ctf_dynhash_destroy (fp->ctf_dynsyms);
1794 fp->ctf_dynsyms = NULL;
1795 free (fp->ctf_dynsymidx);
1796 fp->ctf_dynsymidx = NULL;
1797 fp->ctf_dynsymmax = 0;
1798 ctf_set_errno (fp, err);
1799 return -err;
1800}
1801
1802typedef struct ctf_name_list_accum_cb_arg
1803{
1804 char **names;
1805 ctf_dict_t *fp;
1806 ctf_dict_t **files;
1807 size_t i;
1808 char **dynames;
1809 size_t ndynames;
1810} ctf_name_list_accum_cb_arg_t;
1811
1812/* Accumulate the names and a count of the names in the link output hash. */
1813static void
1814ctf_accumulate_archive_names (void *key, void *value, void *arg_)
1815{
1816 const char *name = (const char *) key;
1817 ctf_dict_t *fp = (ctf_dict_t *) value;
1818 char **names;
1819 ctf_dict_t **files;
1820 ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
1821
1822 if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
1823 {
1824 (arg->i)--;
1825 ctf_set_errno (arg->fp, ENOMEM);
1826 return;
1827 }
1828
1829 if ((files = realloc (arg->files, sizeof (ctf_dict_t *) * arg->i)) == NULL)
1830 {
1831 (arg->i)--;
1832 ctf_set_errno (arg->fp, ENOMEM);
1833 return;
1834 }
1835
1836 /* Allow the caller to get in and modify the name at the last minute. If the
1837 caller *does* modify the name, we have to stash away the new name the
1838 caller returned so we can free it later on. (The original name is the key
1839 of the ctf_link_outputs hash and is freed by the dynhash machinery.) */
1840
1841 if (fp->ctf_link_memb_name_changer)
1842 {
1843 char **dynames;
1844 char *dyname;
1845 void *nc_arg = fp->ctf_link_memb_name_changer_arg;
1846
1847 dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg);
1848
1849 if (dyname != NULL)
1850 {
1851 if ((dynames = realloc (arg->dynames,
1852 sizeof (char *) * ++(arg->ndynames))) == NULL)
1853 {
1854 (arg->ndynames)--;
1855 ctf_set_errno (arg->fp, ENOMEM);
1856 return;
1857 }
1858 arg->dynames = dynames;
1859 name = (const char *) dyname;
1860 }
1861 }
1862
1863 arg->names = names;
1864 arg->names[(arg->i) - 1] = (char *) name;
1865 arg->files = files;
1866 arg->files[(arg->i) - 1] = fp;
1867}
1868
1869/* Change the name of the parent CTF section, if the name transformer has got to
1870 it. */
1871static void
1872ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg)
1873{
1874 ctf_dict_t *fp = (ctf_dict_t *) value;
1875 const char *name = (const char *) arg;
1876
1877 ctf_parent_name_set (fp, name);
1878}
1879
1880/* Warn if we may suffer information loss because the CTF input files are too
1881 old. Usually we provide complete backward compatibility, but compiler
1882 changes etc which never hit a release may have a flag in the header that
1883 simply prevents those changes from being used. */
1884static void
1885ctf_link_warn_outdated_inputs (ctf_dict_t *fp)
1886{
1887 ctf_next_t *i = NULL;
1888 void *name_;
1889 void *input_;
1890 int err;
1891
1892 while ((err = ctf_dynhash_next (fp->ctf_link_inputs, &i, &name_, &input_)) == 0)
1893 {
1894 const char *name = (const char *) name_;
1895 ctf_link_input_t *input = (ctf_link_input_t *) input_;
1896 ctf_next_t *j = NULL;
1897 ctf_dict_t *ifp;
1898 int err;
1899
1900 /* We only care about CTF archives by this point: lazy-opened archives
1901 have always been opened by this point, and short-circuited entries have
1902 a matching corresponding archive member. Entries with NULL clin_arc can
1903 exist, and constitute old entries renamed via a name changer: the
1904 renamed entries exist elsewhere in the list, so we can just skip
1905 those. */
1906
1907 if (!input->clin_arc)
1908 continue;
1909
1910 /* All entries in the archive will necessarily contain the same
1911 CTF_F_NEWFUNCINFO flag, so we only need to check the first. We don't
1912 even need to do that if we can't open it for any reason at all: the
1913 link will fail later on regardless, since an input can't be opened. */
1914
1915 ifp = ctf_archive_next (input->clin_arc, &j, NULL, 0, &err);
1916 if (!ifp)
1917 continue;
1918 ctf_next_destroy (j);
1919
1920 if (!(ifp->ctf_header->cth_flags & CTF_F_NEWFUNCINFO)
1921 && (ifp->ctf_header->cth_varoff - ifp->ctf_header->cth_funcoff) > 0)
1922 ctf_err_warn (fp, 1, 0, _("linker input %s has CTF func info but uses "
1923 "an old, unreleased func info format: "
1924 "this func info section will be dropped."),
1925 name);
1926 }
1927 if (err != ECTF_NEXT_END)
1928 ctf_err_warn (fp, 0, err, _("error checking for outdated inputs"));
1929}
1930
1931/* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
1932 (otherwise) into a new dynamically-allocated string, and return it.
1933 Members with sizes above THRESHOLD are compressed. */
1934unsigned char *
1935ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold)
1936{
1937 ctf_name_list_accum_cb_arg_t arg;
1938 char **names;
1939 char *transformed_name = NULL;
1940 ctf_dict_t **files;
1941 FILE *f = NULL;
1942 size_t i;
1943 int err;
1944 long fsize;
1945 const char *errloc;
1946 unsigned char *buf = NULL;
1947
1948 memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
1949 arg.fp = fp;
1950 fp->ctf_flags |= LCTF_LINKING;
1951
1952 ctf_link_warn_outdated_inputs (fp);
1953
1954 if (fp->ctf_link_outputs)
1955 {
1956 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
1957 if (ctf_errno (fp) < 0)
1958 {
1959 errloc = "hash creation";
1960 goto err;
1961 }
1962 }
1963
1964 /* No extra outputs? Just write a simple ctf_dict_t. */
1965 if (arg.i == 0)
1966 {
1967 unsigned char *ret = ctf_write_mem (fp, size, threshold);
1968 fp->ctf_flags &= ~LCTF_LINKING;
1969 return ret;
1970 }
1971
1972 /* Writing an archive. Stick ourselves (the shared repository, parent of all
1973 other archives) on the front of it with the default name. */
1974 if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL)
1975 {
1976 errloc = "name reallocation";
1977 goto err_no;
1978 }
1979 arg.names = names;
1980 memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
1981
1982 arg.names[0] = (char *) _CTF_SECTION;
1983 if (fp->ctf_link_memb_name_changer)
1984 {
1985 void *nc_arg = fp->ctf_link_memb_name_changer_arg;
1986
1987 transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION,
1988 nc_arg);
1989
1990 if (transformed_name != NULL)
1991 {
1992 arg.names[0] = transformed_name;
1993 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name,
1994 transformed_name);
1995 }
1996 }
1997
1998 /* Propagate the link flags to all the dicts in this link. */
1999 for (i = 0; i < arg.i; i++)
2000 {
2001 arg.files[i]->ctf_link_flags = fp->ctf_link_flags;
2002 arg.files[i]->ctf_flags |= LCTF_LINKING;
2003 }
2004
2005 if ((files = realloc (arg.files,
2006 sizeof (struct ctf_dict *) * (arg.i + 1))) == NULL)
2007 {
2008 errloc = "ctf_dict reallocation";
2009 goto err_no;
2010 }
2011 arg.files = files;
2012 memmove (&(arg.files[1]), arg.files, sizeof (ctf_dict_t *) * (arg.i));
2013 arg.files[0] = fp;
2014
2015 if ((f = tmpfile ()) == NULL)
2016 {
2017 errloc = "tempfile creation";
2018 goto err_no;
2019 }
2020
2021 if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1,
2022 (const char **) arg.names,
2023 threshold)) < 0)
2024 {
2025 errloc = "archive writing";
2026 ctf_set_errno (fp, err);
2027 goto err;
2028 }
2029
2030 if (fseek (f, 0, SEEK_END) < 0)
2031 {
2032 errloc = "seeking to end";
2033 goto err_no;
2034 }
2035
2036 if ((fsize = ftell (f)) < 0)
2037 {
2038 errloc = "filesize determination";
2039 goto err_no;
2040 }
2041
2042 if (fseek (f, 0, SEEK_SET) < 0)
2043 {
2044 errloc = "filepos resetting";
2045 goto err_no;
2046 }
2047
2048 if ((buf = malloc (fsize)) == NULL)
2049 {
2050 errloc = "CTF archive buffer allocation";
2051 goto err_no;
2052 }
2053
2054 while (!feof (f) && fread (buf, fsize, 1, f) == 0)
2055 if (ferror (f))
2056 {
2057 errloc = "reading archive from temporary file";
2058 goto err_no;
2059 }
2060
2061 /* Turn off the is-linking flag on all the dicts in this link: if the strict enum
2062 checking flag is off on the parent, turn it off on all the children too. */
2063 for (i = 0; i < arg.i; i++)
2064 {
2065 arg.files[i]->ctf_flags &= ~LCTF_LINKING;
2066 if (!(fp->ctf_flags & LCTF_STRICT_NO_DUP_ENUMERATORS))
2067 arg.files[i]->ctf_flags &= ~LCTF_STRICT_NO_DUP_ENUMERATORS;
2068 }
2069
2070 *size = fsize;
2071 free (arg.names);
2072 free (arg.files);
2073 free (transformed_name);
2074 if (arg.ndynames)
2075 {
2076 size_t i;
2077 for (i = 0; i < arg.ndynames; i++)
2078 free (arg.dynames[i]);
2079 free (arg.dynames);
2080 }
2081 fclose (f);
2082 return buf;
2083
2084 err_no:
2085 ctf_set_errno (fp, errno);
2086
2087 /* Turn off the is-linking flag on all the dicts in this link, as above. */
2088 for (i = 0; i < arg.i; i++)
2089 {
2090 arg.files[i]->ctf_flags &= ~LCTF_LINKING;
2091 if (!(fp->ctf_flags & LCTF_STRICT_NO_DUP_ENUMERATORS))
2092 arg.files[i]->ctf_flags &= ~LCTF_STRICT_NO_DUP_ENUMERATORS;
2093 }
2094 err:
2095 free (buf);
2096 if (f)
2097 fclose (f);
2098 free (arg.names);
2099 free (arg.files);
2100 free (transformed_name);
2101 if (arg.ndynames)
2102 {
2103 size_t i;
2104 for (i = 0; i < arg.ndynames; i++)
2105 free (arg.dynames[i]);
2106 free (arg.dynames);
2107 }
2108 ctf_err_warn (fp, 0, 0, _("cannot write archive in link: %s failure"),
2109 errloc);
2110 return NULL;
2111}