]> Git Repo - binutils.git/blob - libctf/ctf-link.c
libctf, link: add lazy linking: clean up input members: err/warn cleanup
[binutils.git] / libctf / ctf-link.c
1 /* CTF linking.
2    Copyright (C) 2019-2020 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 container 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 container 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_file_t *src_fp, ctf_id_t src_type,
42                       ctf_file_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   /* This dynhash is a bit tricky: it has a multivalued (structural) key, so we
55      need to use the sized-hash machinery to generate key hashing and equality
56      functions.  */
57
58   if (dst_fp->ctf_link_type_mapping == NULL)
59     {
60       ctf_hash_fun f = ctf_hash_type_key;
61       ctf_hash_eq_fun e = ctf_hash_eq_type_key;
62
63       if ((dst_fp->ctf_link_type_mapping = ctf_dynhash_create (f, e, free,
64                                                                NULL)) == NULL)
65         return;
66     }
67
68   ctf_link_type_key_t *key;
69   key = calloc (1, sizeof (struct ctf_link_type_key));
70   if (!key)
71     return;
72
73   key->cltk_fp = src_fp;
74   key->cltk_idx = src_type;
75
76   /* No OOM checking needed, because if this doesn't work the worst we'll do is
77      add a few more duplicate types (which will probably run out of memory
78      anyway).  */
79   ctf_dynhash_insert (dst_fp->ctf_link_type_mapping, key,
80                       (void *) (uintptr_t) dst_type);
81 }
82
83 /* Look up a type mapping: return 0 if none.  The DST_FP is modified to point to
84    the parent if need be.  The ID returned is from the dst_fp's perspective.  */
85 ctf_id_t
86 ctf_type_mapping (ctf_file_t *src_fp, ctf_id_t src_type, ctf_file_t **dst_fp)
87 {
88   ctf_link_type_key_t key;
89   ctf_file_t *target_fp = *dst_fp;
90   ctf_id_t dst_type = 0;
91
92   if (LCTF_TYPE_ISPARENT (src_fp, src_type) && src_fp->ctf_parent)
93     src_fp = src_fp->ctf_parent;
94
95   src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type);
96   key.cltk_fp = src_fp;
97   key.cltk_idx = src_type;
98
99   if (target_fp->ctf_link_type_mapping)
100     dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping,
101                                                &key);
102
103   if (dst_type != 0)
104     {
105       dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
106                                      target_fp->ctf_parent != NULL);
107       *dst_fp = target_fp;
108       return dst_type;
109     }
110
111   if (target_fp->ctf_parent)
112     target_fp = target_fp->ctf_parent;
113   else
114     return 0;
115
116   if (target_fp->ctf_link_type_mapping)
117     dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping,
118                                                &key);
119
120   if (dst_type)
121     dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
122                                    target_fp->ctf_parent != NULL);
123
124   *dst_fp = target_fp;
125   return dst_type;
126 }
127
128 /* Linker machinery.
129
130    CTF linking consists of adding CTF archives full of content to be merged into
131    this one to the current file (which must be writable) by calling
132    ctf_link_add_ctf().  Once this is done, a call to ctf_link() will merge the
133    type tables together, generating new CTF files as needed, with this one as a
134    parent, to contain types from the inputs which conflict.
135    ctf_link_add_strtab() takes a callback which provides string/offset pairs to
136    be added to the external symbol table and deduplicated from all CTF string
137    tables in the output link; ctf_link_shuffle_syms() takes a callback which
138    provides symtab entries in ascending order, and shuffles the function and
139    data sections to match; and ctf_link_write() emits a CTF file (if there are
140    no conflicts requiring per-compilation-unit sub-CTF files) or CTF archives
141    (otherwise) and returns it, suitable for addition in the .ctf section of the
142    output.  */
143
144 /* Return the name of the compilation unit this CTF dict or its parent applies
145    to, or a non-null string otherwise: prefer the parent.  Used in debugging
146    output.  Sometimes used for outputs too.  */
147 const char *
148 ctf_link_input_name (ctf_file_t *fp)
149 {
150   if (fp->ctf_parent && fp->ctf_parent->ctf_cuname)
151     return fp->ctf_parent->ctf_cuname;
152   else if (fp->ctf_cuname)
153     return fp->ctf_cuname;
154   else
155     return "(unnamed)";
156 }
157
158 /* The linker inputs look like this.  clin_fp is used for short-circuited
159    CU-mapped links that can entirely avoid the first link phase in some
160    situations in favour of just passing on the contained ctf_file_t: it is
161    always the sole ctf_file_t inside the corresponding clin_arc.  If set, it
162    gets assigned directly to the final link inputs and freed from there, so it
163    never gets explicitly freed in the ctf_link_input.  */
164 typedef struct ctf_link_input
165 {
166   const char *clin_filename;
167   ctf_archive_t *clin_arc;
168   ctf_file_t *clin_fp;
169   int n;
170 } ctf_link_input_t;
171
172 static void
173 ctf_link_input_close (void *input)
174 {
175   ctf_link_input_t *i = (ctf_link_input_t *) input;
176   if (i->clin_arc)
177     ctf_arc_close (i->clin_arc);
178   free (i);
179 }
180
181 /* Like ctf_link_add_ctf, below, but with no error-checking, so it can be called
182    in the middle of an ongoing link.  */
183 static int
184 ctf_link_add_ctf_internal (ctf_file_t *fp, ctf_archive_t *ctf,
185                            ctf_file_t *fp_input, const char *name)
186 {
187   ctf_link_input_t *input = NULL;
188   char *dupname = NULL;
189
190   if ((input = calloc (1, sizeof (ctf_link_input_t))) == NULL)
191     goto oom;
192
193   if ((dupname = strdup (name)) == NULL)
194     goto oom;
195
196   input->clin_arc = ctf;
197   input->clin_fp = fp_input;
198   input->clin_filename = dupname;
199   input->n = ctf_dynhash_elements (fp->ctf_link_inputs);
200
201   if (ctf_dynhash_insert (fp->ctf_link_inputs, dupname, input) < 0)
202     goto oom;
203
204   return 0;
205  oom:
206   free (input);
207   free (dupname);
208   return ctf_set_errno (fp, ENOMEM);
209 }
210
211 /* Add a file, memory buffer, or unopened file (by name) to a link.
212
213    You can call this with:
214
215     CTF and NAME: link the passed ctf_archive_t, with the given NAME.
216     NAME alone: open NAME as a CTF file when needed.
217     BUF and NAME: open the BUF (of length N) as CTF, with the given NAME.  (Not
218     yet implemented.)
219
220     Passed in CTF args are owned by the dictionary and will be freed by it.
221     The BUF arg is *not* owned by the dictionary, and the user should not free
222     its referent until the link is done.
223
224     The order of calls to this function influences the order of types in the
225     final link output, but otherwise is not important.
226
227     Private for now, but may in time become public once support for BUF is
228     implemented.  */
229
230 static int
231 ctf_link_add (ctf_file_t *fp, ctf_archive_t *ctf, const char *name,
232               void *buf _libctf_unused_, size_t n _libctf_unused_)
233 {
234   if (buf)
235     return (ctf_set_errno (fp, ECTF_NOTYET));
236
237   if (!((ctf && name && !buf)
238         || (name && !buf && !ctf)
239         || (buf && name && !ctf)))
240     return (ctf_set_errno (fp, EINVAL));
241
242   /* We can only lazily open files if libctf.so is in use rather than
243      libctf-nobfd.so.  This is a little tricky: in shared libraries, we can use
244      a weak symbol so that -lctf -lctf-nobfd works, but in static libraries we
245      must distinguish between the two libraries explicitly.  */
246
247 #if defined (PIC)
248   if (!buf && !ctf && name && !ctf_open)
249     return (ctf_set_errno (fp, ECTF_NEEDSBFD));
250 #elif NOBFD
251   if (!buf && !ctf && name)
252     return (ctf_set_errno (fp, ECTF_NEEDSBFD));
253 #endif
254
255   if (fp->ctf_link_outputs)
256     return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
257   if (fp->ctf_link_inputs == NULL)
258     fp->ctf_link_inputs = ctf_dynhash_create (ctf_hash_string,
259                                               ctf_hash_eq_string, free,
260                                               ctf_link_input_close);
261
262   if (fp->ctf_link_inputs == NULL)
263     return (ctf_set_errno (fp, ENOMEM));
264
265   return ctf_link_add_ctf_internal (fp, ctf, NULL, name);
266 }
267
268 /* Add an opened CTF archive or unopened file (by name) to a link.
269    If CTF is NULL and NAME is non-null, an unopened file is meant:
270    otherwise, the specified archive is assumed to have the given NAME.
271
272     Passed in CTF args are owned by the dictionary and will be freed by it.
273
274     The order of calls to this function influences the order of types in the
275     final link output, but otherwise is not important.  */
276
277 int
278 ctf_link_add_ctf (ctf_file_t *fp, ctf_archive_t *ctf, const char *name)
279 {
280   return ctf_link_add (fp, ctf, name, NULL, 0);
281 }
282
283 /* Return a per-CU output CTF dictionary suitable for the given CU, creating and
284    interning it if need be.  */
285
286 static ctf_file_t *
287 ctf_create_per_cu (ctf_file_t *fp, const char *filename, const char *cuname)
288 {
289   ctf_file_t *cu_fp;
290   const char *ctf_name = NULL;
291   char *dynname = NULL;
292
293   /* First, check the mapping table and translate the per-CU name we use
294      accordingly.  We check both the input filename and the CU name.  Only if
295      neither are set do we fall back to the input filename as the per-CU
296      dictionary name.  We prefer the filename because this is easier for likely
297      callers to determine.  */
298
299   if (fp->ctf_link_cu_mapping)
300     {
301       if (((ctf_name = ctf_dynhash_lookup (fp->ctf_link_cu_mapping, filename)) == NULL) &&
302           ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_cu_mapping, cuname)) == NULL))
303         ctf_name = filename;
304     }
305
306   if (ctf_name == NULL)
307     ctf_name = filename;
308
309   if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL)
310     {
311       int err;
312
313       if ((cu_fp = ctf_create (&err)) == NULL)
314         {
315           ctf_err_warn (fp, 0, "Cannot create per-CU CTF archive for "
316                         "CU %s from input file %s: %s", cuname, filename,
317                         ctf_errmsg (err));
318           ctf_set_errno (fp, err);
319           return NULL;
320         }
321
322       if ((dynname = strdup (ctf_name)) == NULL)
323         goto oom;
324       if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
325         goto oom;
326
327       ctf_import_unref (cu_fp, fp);
328       ctf_cuname_set (cu_fp, cuname);
329       ctf_parent_name_set (cu_fp, _CTF_SECTION);
330     }
331   return cu_fp;
332
333  oom:
334   free (dynname);
335   ctf_file_close (cu_fp);
336   ctf_set_errno (fp, ENOMEM);
337   return NULL;
338 }
339
340 /* Add a mapping directing that the CU named FROM should have its
341    conflicting/non-duplicate types (depending on link mode) go into a container
342    named TO.  Many FROMs can share a TO: in this case, the effect on conflicting
343    types is not yet defined (but in time an auto-renaming algorithm will be
344    added: ugly, but there is really no right thing one can do in this
345    situation).
346
347    We forcibly add a container named TO in every case, even though it may well
348    wind up empty, because clients that use this facility usually expect to find
349    every TO container present, even if empty, and malfunction otherwise.  */
350
351 int
352 ctf_link_add_cu_mapping (ctf_file_t *fp, const char *from, const char *to)
353 {
354   int err;
355   char *f, *t;
356
357   if (fp->ctf_link_cu_mapping == NULL)
358     fp->ctf_link_cu_mapping = ctf_dynhash_create (ctf_hash_string,
359                                                   ctf_hash_eq_string, free,
360                                                   free);
361   if (fp->ctf_link_cu_mapping == NULL)
362     return ctf_set_errno (fp, ENOMEM);
363
364   if (fp->ctf_link_outputs == NULL)
365     fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
366                                                ctf_hash_eq_string, free,
367                                                (ctf_hash_free_fun)
368                                                ctf_file_close);
369
370   if (fp->ctf_link_outputs == NULL)
371     return ctf_set_errno (fp, ENOMEM);
372
373   f = strdup (from);
374   t = strdup (to);
375   if (!f || !t)
376     goto oom;
377
378   if (ctf_create_per_cu (fp, t, t) == NULL)
379     goto oom_noerrno;                           /* Errno is set for us.  */
380
381   err = ctf_dynhash_insert (fp->ctf_link_cu_mapping, f, t);
382   if (err)
383     {
384       ctf_set_errno (fp, err);
385       goto oom_noerrno;
386     }
387
388   return 0;
389
390  oom:
391   ctf_set_errno (fp, errno);
392  oom_noerrno:
393   free (f);
394   free (t);
395   return -1;
396 }
397
398 /* Set a function which is called to transform the names of archive members.
399    This is useful for applying regular transformations to many names, where
400    ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
401    names.  The member name changer is applied at ctf_link_write time, so it
402    cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
403    The changer function accepts a name and should return a new
404    dynamically-allocated name, or NULL if the name should be left unchanged.  */
405 void
406 ctf_link_set_memb_name_changer (ctf_file_t *fp,
407                                 ctf_link_memb_name_changer_f *changer,
408                                 void *arg)
409 {
410   fp->ctf_link_memb_name_changer = changer;
411   fp->ctf_link_memb_name_changer_arg = arg;
412 }
413
414 typedef struct ctf_link_in_member_cb_arg
415 {
416   /* The shared output dictionary.  */
417   ctf_file_t *out_fp;
418
419   /* The filename of the input file, and an fp to each dictionary in that file
420      in turn.  */
421   const char *in_file_name;
422   ctf_file_t *in_fp;
423
424   /* The CU name of the dict being processed.  */
425   const char *cu_name;
426   int in_input_cu_file;
427
428   /* The parent dictionary in the input, and whether it's been processed yet.
429      Not needed by ctf_link_one_type / ctf_link_one_variable, only by higher
430      layers.  */
431   ctf_file_t *in_fp_parent;
432   int done_parent;
433
434   /* If true, this is the CU-mapped portion of a deduplicating link: no child
435      dictionaries should be created.  */
436   int cu_mapped;
437 } ctf_link_in_member_cb_arg_t;
438
439 /* Link one type into the link.  We rely on ctf_add_type() to detect
440    duplicates.  This is not terribly reliable yet (unnmamed types will be
441    mindlessly duplicated), but will improve shortly.  */
442
443 static int
444 ctf_link_one_type (ctf_id_t type, int isroot _libctf_unused_, void *arg_)
445 {
446   ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
447   ctf_file_t *per_cu_out_fp;
448   int err;
449
450   if (arg->in_fp->ctf_link_flags != CTF_LINK_SHARE_UNCONFLICTED)
451     {
452       ctf_err_warn (arg->out_fp, 0, "Share-duplicated mode not yet implemented");
453       return ctf_set_errno (arg->out_fp, ECTF_NOTYET);
454     }
455
456   /* Simply call ctf_add_type: if it reports a conflict and we're adding to the
457      main CTF file, add to the per-CU archive member instead, creating it if
458      necessary.  If we got this type from a per-CU archive member, add it
459      straight back to the corresponding member in the output.  */
460
461   if (!arg->in_input_cu_file)
462     {
463       if (ctf_add_type (arg->out_fp, arg->in_fp, type) != CTF_ERR)
464         return 0;
465
466       err = ctf_errno (arg->out_fp);
467       if (err != ECTF_CONFLICT)
468         {
469           if (err != ECTF_NONREPRESENTABLE)
470             ctf_err_warn (arg->out_fp, 1, "Cannot link type %lx from input file %s, "
471                           "CU %s into output link: %s", type, arg->cu_name,
472                          arg->in_file_name, ctf_errmsg (err));
473           /* We must ignore this problem or we end up losing future types, then
474              trying to link the variables in, then exploding.  Better to link as
475              much as possible.  */
476           return 0;
477         }
478       ctf_set_errno (arg->out_fp, 0);
479     }
480
481   if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->in_file_name,
482                                           arg->cu_name)) == NULL)
483     return -1;                                  /* Errno is set for us.  */
484
485   if (ctf_add_type (per_cu_out_fp, arg->in_fp, type) != CTF_ERR)
486     return 0;
487
488   err = ctf_errno (per_cu_out_fp);
489   if (err != ECTF_NONREPRESENTABLE)
490     ctf_err_warn (arg->out_fp, 1, "Cannot link type %lx from input file %s, CU %s "
491                  "into output per-CU CTF archive member %s: %s: skipped", type,
492                  ctf_link_input_name (arg->in_fp), arg->in_file_name,
493                  ctf_link_input_name (per_cu_out_fp), ctf_errmsg (err));
494   if (err == ECTF_CONFLICT)
495       /* Conflicts are possible at this stage only if a non-ld user has combined
496          multiple TUs into a single output dictionary.  Even in this case we do not
497          want to stop the link or propagate the error.  */
498       ctf_set_errno (arg->out_fp, 0);
499
500   return 0;                                     /* As above: do not lose types.  */
501 }
502
503 /* Check if we can safely add a variable with the given type to this container.  */
504
505 static int
506 check_variable (const char *name, ctf_file_t *fp, ctf_id_t type,
507                 ctf_dvdef_t **out_dvd)
508 {
509   ctf_dvdef_t *dvd;
510
511   dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
512   *out_dvd = dvd;
513   if (!dvd)
514     return 1;
515
516   if (dvd->dvd_type != type)
517     {
518       /* Variable here.  Wrong type: cannot add.  Just skip it, because there is
519          no way to express this in CTF.  Don't even warn: this case is too
520          common.  (This might be the parent, in which case we'll try adding in
521          the child first, and only then give up.)  */
522       ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
523     }
524
525   return 0;                                   /* Already exists.  */
526 }
527
528 /* Link one variable in.  */
529
530 static int
531 ctf_link_one_variable (const char *name, ctf_id_t type, void *arg_)
532 {
533   ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
534   ctf_file_t *per_cu_out_fp;
535   ctf_id_t dst_type = 0;
536   ctf_file_t *check_fp;
537   ctf_dvdef_t *dvd;
538
539   /* In unconflicted link mode, if this type is mapped to a type in the parent
540      container, we want to try to add to that first: if it reports a duplicate,
541      or if the type is in a child already, add straight to the child.  */
542
543   check_fp = arg->out_fp;
544
545   dst_type = ctf_type_mapping (arg->in_fp, type, &check_fp);
546   if (dst_type != 0)
547     {
548       if (check_fp == arg->out_fp)
549         {
550           if (check_variable (name, check_fp, dst_type, &dvd))
551             {
552               /* No variable here: we can add it.  */
553               if (ctf_add_variable (check_fp, name, dst_type) < 0)
554                 return (ctf_set_errno (arg->out_fp, ctf_errno (check_fp)));
555               return 0;
556             }
557
558           /* Already present?  Nothing to do.  */
559           if (dvd && dvd->dvd_type == type)
560             return 0;
561         }
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 (arg->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, arg->in_file_name,
573                    type);
574       return 0;
575     }
576
577   if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->in_file_name,
578                                           arg->cu_name)) == NULL)
579     return -1;                                  /* Errno is set for us.  */
580
581   /* If the type was not found, check for it in the child too. */
582   if (dst_type == 0)
583     {
584       check_fp = per_cu_out_fp;
585       dst_type = ctf_type_mapping (arg->in_fp, type, &check_fp);
586
587       if (dst_type == 0)
588         {
589           ctf_err_warn (arg->out_fp, 1, "Type %lx for variable %s in input "
590                         "file %s not found: skipped", type, name,
591                         arg->in_file_name);
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 (arg->out_fp, ctf_errno (per_cu_out_fp)));
600   return 0;
601 }
602
603 /* Merge every type (and optionally, variable) in this archive member into the
604    link, so we can relink things that have already had ld run on them.  We use
605    the archive member name, sans any leading '.ctf.', as the CU name for
606    ambiguous types if there is one and it's not the default: otherwise, we use
607    the name of the input file.  */
608 static int
609 ctf_link_one_input_archive_member (ctf_file_t *in_fp, const char *name, void *arg_)
610 {
611   ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
612   int err = 0;
613
614   if (strcmp (name, _CTF_SECTION) == 0)
615     {
616       /* This file is the default member of this archive, and has already been
617          explicitly processed.
618
619          In the default sharing mode of CTF_LINK_SHARE_UNCONFLICTED, it does no
620          harm to rescan an existing shared repo again: all the types will just
621          end up in the same place.  But in CTF_LINK_SHARE_DUPLICATED mode, this
622          causes the system to erroneously conclude that all types are duplicated
623          and should be shared, even if they are not.  */
624
625       if (arg->done_parent)
626         return 0;
627     }
628   else
629     {
630       /* Get ambiguous types from our parent.  */
631       ctf_import (in_fp, arg->in_fp_parent);
632       arg->in_input_cu_file = 1;
633     }
634
635   arg->cu_name = name;
636   if (strncmp (arg->cu_name, ".ctf.", strlen (".ctf.")) == 0)
637     arg->cu_name += strlen (".ctf.");
638   arg->in_fp = in_fp;
639
640   if ((err = ctf_type_iter_all (in_fp, ctf_link_one_type, arg)) > -1)
641     err = ctf_variable_iter (in_fp, ctf_link_one_variable, arg);
642
643   arg->in_input_cu_file = 0;
644
645   if (err < 0)
646       return -1;                                /* Errno is set for us.  */
647
648   return 0;
649 }
650
651 /* Dump the unnecessary link type mapping after one input file is processed.  */
652 static void
653 empty_link_type_mapping (void *key _libctf_unused_, void *value,
654                          void *arg _libctf_unused_)
655 {
656   ctf_file_t *fp = (ctf_file_t *) value;
657
658   if (fp->ctf_link_type_mapping)
659     ctf_dynhash_empty (fp->ctf_link_type_mapping);
660 }
661
662 /* Lazily open a CTF archive for linking, if not already open.
663
664    Returns the number of files contained within the opened archive (0 for none),
665    or -1 on error, as usual.  */
666 static ssize_t
667 ctf_link_lazy_open (ctf_file_t *fp, ctf_link_input_t *input)
668 {
669   size_t count;
670   int err;
671
672   if (input->clin_arc)
673     return ctf_archive_count (input->clin_arc);
674
675   if (input->clin_fp)
676     return 1;
677
678   /* See ctf_link_add_ctf.  */
679 #if defined (PIC) || !NOBFD
680   input->clin_arc = ctf_open (input->clin_filename, NULL, &err);
681 #else
682   ctf_err_warn (fp, 0, "Cannot open %s lazily: %s", input->clin_filename,
683                 ctf_errmsg (ECTF_NEEDSBFD));
684   ctf_set_errno (fp, ECTF_NEEDSBFD);
685   return -1;
686 #endif
687
688   /* Having no CTF sections is not an error.  We just don't need to do
689      anything.  */
690
691   if (!input->clin_arc)
692     {
693       if (err == ECTF_NOCTFDATA)
694         return 0;
695
696       ctf_err_warn (fp, 0, "Opening CTF %s failed: %s",
697                     input->clin_filename, ctf_errmsg (err));
698       ctf_set_errno (fp, err);
699       return -1;
700     }
701
702   if ((count = ctf_archive_count (input->clin_arc)) == 0)
703     ctf_arc_close (input->clin_arc);
704
705   return (ssize_t) count;
706 }
707
708 /* Close an input, as a ctf_dynhash_iter iterator.  */
709 static void
710 ctf_link_close_one_input_archive (void *key _libctf_unused_, void *value,
711                                   void *arg _libctf_unused_)
712 {
713   ctf_link_input_t *input = (ctf_link_input_t *) value;
714   if (input->clin_arc)
715     ctf_arc_close (input->clin_arc);
716   input->clin_arc = NULL;
717 }
718
719 /* Link one input file's types into the output file.  */
720 static void
721 ctf_link_one_input_archive (void *key, void *value, void *arg_)
722 {
723   const char *file_name = (const char *) key;
724   ctf_link_input_t *input = (ctf_link_input_t *)value;
725   ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
726   int err = 0;
727
728   if (!input->clin_arc)
729     {
730       err = ctf_link_lazy_open (arg->out_fp, input);
731       if (err == 0)                             /* Just no CTF.  */
732         return;
733
734       if (err < 0)
735         return;                                 /* errno is set for us.  */
736     }
737
738   arg->in_file_name = file_name;
739   arg->done_parent = 0;
740   if ((arg->in_fp_parent = ctf_arc_open_by_name (input->clin_arc, NULL,
741                                                  &err)) == NULL)
742     if (err != ECTF_ARNNAME)
743       {
744         ctf_err_warn (arg->out_fp, 0, "Cannot open main archive member in "
745                       "input file %s in the link: skipping: %s",
746                       arg->in_file_name, ctf_errmsg (err));
747         goto out;
748       }
749
750   if (ctf_link_one_input_archive_member (arg->in_fp_parent,
751                                          _CTF_SECTION, arg) < 0)
752     {
753       ctf_file_close (arg->in_fp_parent);
754       goto out;
755     }
756   arg->done_parent = 1;
757   if (ctf_archive_iter (input->clin_arc, ctf_link_one_input_archive_member,
758                         arg) < 0)
759     ctf_err_warn (arg->out_fp, 0, "Cannot traverse archive in input file %s: "
760                   "link cannot continue: %s", arg->in_file_name,
761                   ctf_errmsg (ctf_errno (arg->out_fp)));
762   else
763     {
764       /* The only error indication to the caller is the errno: so ensure that it
765          is zero if there was no actual error from the caller.  */
766       ctf_set_errno (arg->out_fp, 0);
767     }
768   ctf_file_close (arg->in_fp_parent);
769
770  out:
771   ctf_link_close_one_input_archive (key, value, NULL);
772 }
773
774 /* Merge types and variable sections in all files added to the link
775    together.  All the added files are closed.  */
776 int
777 ctf_link (ctf_file_t *fp, int flags)
778 {
779   ctf_link_in_member_cb_arg_t arg;
780
781   memset (&arg, 0, sizeof (struct ctf_link_in_member_cb_arg));
782   arg.out_fp = fp;
783   fp->ctf_link_flags = flags;
784
785   if (fp->ctf_link_inputs == NULL)
786     return 0;                                   /* Nothing to do. */
787
788   if (fp->ctf_link_outputs == NULL)
789     fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
790                                                ctf_hash_eq_string, free,
791                                                (ctf_hash_free_fun)
792                                                ctf_file_close);
793
794   if (fp->ctf_link_outputs == NULL)
795     return ctf_set_errno (fp, ENOMEM);
796
797   ctf_dynhash_iter (fp->ctf_link_inputs, ctf_link_one_input_archive,
798                     &arg);
799
800   /* Discard the now-unnecessary mapping table data from all the outputs.  */
801   if (fp->ctf_link_type_mapping)
802     ctf_dynhash_empty (fp->ctf_link_type_mapping);
803   ctf_dynhash_iter (fp->ctf_link_outputs, empty_link_type_mapping, NULL);
804
805   if ((ctf_errno (fp) != 0) && (ctf_errno (fp) != ECTF_NOCTFDATA))
806     return -1;
807   return 0;
808 }
809
810 typedef struct ctf_link_out_string_cb_arg
811 {
812   const char *str;
813   uint32_t offset;
814   int err;
815 } ctf_link_out_string_cb_arg_t;
816
817 /* Intern a string in the string table of an output per-CU CTF file.  */
818 static void
819 ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
820                                void *arg_)
821 {
822   ctf_file_t *fp = (ctf_file_t *) value;
823   ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
824
825   fp->ctf_flags |= LCTF_DIRTY;
826   if (!ctf_str_add_external (fp, arg->str, arg->offset))
827     arg->err = ENOMEM;
828 }
829
830 /* Repeatedly call ADD_STRING to acquire strings from the external string table,
831    adding them to the atoms table for this CU and all subsidiary CUs.
832
833    If ctf_link() is also called, it must be called first if you want the new CTF
834    files ctf_link() can create to get their strings dedupped against the ELF
835    strtab properly.  */
836 int
837 ctf_link_add_strtab (ctf_file_t *fp, ctf_link_strtab_string_f *add_string,
838                      void *arg)
839 {
840   const char *str;
841   uint32_t offset;
842   int err = 0;
843
844   while ((str = add_string (&offset, arg)) != NULL)
845     {
846       ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
847
848       fp->ctf_flags |= LCTF_DIRTY;
849       if (!ctf_str_add_external (fp, str, offset))
850         err = ENOMEM;
851
852       ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
853                         &iter_arg);
854       if (iter_arg.err)
855         err = iter_arg.err;
856     }
857
858   return -err;
859 }
860
861 /* Not yet implemented.  */
862 int
863 ctf_link_shuffle_syms (ctf_file_t *fp _libctf_unused_,
864                        ctf_link_iter_symbol_f *add_sym _libctf_unused_,
865                        void *arg _libctf_unused_)
866 {
867   return 0;
868 }
869
870 typedef struct ctf_name_list_accum_cb_arg
871 {
872   char **names;
873   ctf_file_t *fp;
874   ctf_file_t **files;
875   size_t i;
876   char **dynames;
877   size_t ndynames;
878 } ctf_name_list_accum_cb_arg_t;
879
880 /* Accumulate the names and a count of the names in the link output hash.  */
881 static void
882 ctf_accumulate_archive_names (void *key, void *value, void *arg_)
883 {
884   const char *name = (const char *) key;
885   ctf_file_t *fp = (ctf_file_t *) value;
886   char **names;
887   ctf_file_t **files;
888   ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
889
890   if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
891     {
892       (arg->i)--;
893       ctf_set_errno (arg->fp, ENOMEM);
894       return;
895     }
896
897   if ((files = realloc (arg->files, sizeof (ctf_file_t *) * arg->i)) == NULL)
898     {
899       (arg->i)--;
900       ctf_set_errno (arg->fp, ENOMEM);
901       return;
902     }
903
904   /* Allow the caller to get in and modify the name at the last minute.  If the
905      caller *does* modify the name, we have to stash away the new name the
906      caller returned so we can free it later on.  (The original name is the key
907      of the ctf_link_outputs hash and is freed by the dynhash machinery.)  */
908
909   if (fp->ctf_link_memb_name_changer)
910     {
911       char **dynames;
912       char *dyname;
913       void *nc_arg = fp->ctf_link_memb_name_changer_arg;
914
915       dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg);
916
917       if (dyname != NULL)
918         {
919           if ((dynames = realloc (arg->dynames,
920                                   sizeof (char *) * ++(arg->ndynames))) == NULL)
921             {
922               (arg->ndynames)--;
923               ctf_set_errno (arg->fp, ENOMEM);
924               return;
925             }
926             arg->dynames = dynames;
927             name = (const char *) dyname;
928         }
929     }
930
931   arg->names = names;
932   arg->names[(arg->i) - 1] = (char *) name;
933   arg->files = files;
934   arg->files[(arg->i) - 1] = fp;
935 }
936
937 /* Change the name of the parent CTF section, if the name transformer has got to
938    it.  */
939 static void
940 ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg)
941 {
942   ctf_file_t *fp = (ctf_file_t *) value;
943   const char *name = (const char *) arg;
944
945   ctf_parent_name_set (fp, name);
946 }
947
948 /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
949    (otherwise) into a new dynamically-allocated string, and return it.
950    Members with sizes above THRESHOLD are compressed.  */
951 unsigned char *
952 ctf_link_write (ctf_file_t *fp, size_t *size, size_t threshold)
953 {
954   ctf_name_list_accum_cb_arg_t arg;
955   char **names;
956   char *transformed_name = NULL;
957   ctf_file_t **files;
958   FILE *f = NULL;
959   int err;
960   long fsize;
961   const char *errloc;
962   unsigned char *buf = NULL;
963
964   memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
965   arg.fp = fp;
966
967   if (fp->ctf_link_outputs)
968     {
969       ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
970       if (ctf_errno (fp) < 0)
971         {
972           errloc = "hash creation";
973           goto err;
974         }
975     }
976
977   /* No extra outputs? Just write a simple ctf_file_t.  */
978   if (arg.i == 0)
979     return ctf_write_mem (fp, size, threshold);
980
981   /* Writing an archive.  Stick ourselves (the shared repository, parent of all
982      other archives) on the front of it with the default name.  */
983   if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL)
984     {
985       errloc = "name reallocation";
986       goto err_no;
987     }
988   arg.names = names;
989   memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
990
991   arg.names[0] = (char *) _CTF_SECTION;
992   if (fp->ctf_link_memb_name_changer)
993     {
994       void *nc_arg = fp->ctf_link_memb_name_changer_arg;
995
996       transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION,
997                                                          nc_arg);
998
999       if (transformed_name != NULL)
1000         {
1001           arg.names[0] = transformed_name;
1002           ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name,
1003                             transformed_name);
1004         }
1005     }
1006
1007   if ((files = realloc (arg.files,
1008                         sizeof (struct ctf_file *) * (arg.i + 1))) == NULL)
1009     {
1010       errloc = "ctf_file reallocation";
1011       goto err_no;
1012     }
1013   arg.files = files;
1014   memmove (&(arg.files[1]), arg.files, sizeof (ctf_file_t *) * (arg.i));
1015   arg.files[0] = fp;
1016
1017   if ((f = tmpfile ()) == NULL)
1018     {
1019       errloc = "tempfile creation";
1020       goto err_no;
1021     }
1022
1023   if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1,
1024                                (const char **) arg.names,
1025                                threshold)) < 0)
1026     {
1027       errloc = "archive writing";
1028       ctf_set_errno (fp, err);
1029       goto err;
1030     }
1031
1032   if (fseek (f, 0, SEEK_END) < 0)
1033     {
1034       errloc = "seeking to end";
1035       goto err_no;
1036     }
1037
1038   if ((fsize = ftell (f)) < 0)
1039     {
1040       errloc = "filesize determination";
1041       goto err_no;
1042     }
1043
1044   if (fseek (f, 0, SEEK_SET) < 0)
1045     {
1046       errloc = "filepos resetting";
1047       goto err_no;
1048     }
1049
1050   if ((buf = malloc (fsize)) == NULL)
1051     {
1052       errloc = "CTF archive buffer allocation";
1053       goto err_no;
1054     }
1055
1056   while (!feof (f) && fread (buf, fsize, 1, f) == 0)
1057     if (ferror (f))
1058       {
1059         errloc = "reading archive from temporary file";
1060         goto err_no;
1061       }
1062
1063   *size = fsize;
1064   free (arg.names);
1065   free (arg.files);
1066   free (transformed_name);
1067   if (arg.ndynames)
1068     {
1069       size_t i;
1070       for (i = 0; i < arg.ndynames; i++)
1071         free (arg.dynames[i]);
1072       free (arg.dynames);
1073     }
1074   return buf;
1075
1076  err_no:
1077   ctf_set_errno (fp, errno);
1078  err:
1079   free (buf);
1080   if (f)
1081     fclose (f);
1082   free (arg.names);
1083   free (arg.files);
1084   free (transformed_name);
1085   if (arg.ndynames)
1086     {
1087       size_t i;
1088       for (i = 0; i < arg.ndynames; i++)
1089         free (arg.dynames[i]);
1090       free (arg.dynames);
1091     }
1092   ctf_err_warn (fp, 0, "Cannot write archive in link: %s failure: %s", errloc,
1093                 ctf_errmsg (ctf_errno (fp)));
1094   return NULL;
1095 }
This page took 0.086241 seconds and 4 git commands to generate.