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