• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

Revisão0aae381999d90b36375e9bf621a633b86a459161 (tree)
Hora2019-09-23 22:12:54
AutorNick Alcock <nick.alcock@orac...>
CommiterNick Alcock

Mensagem de Log

libctf: add the ctf_link machinery

This is the start of work on the core of the linking mechanism for CTF
sections. This commit handles the type and string sections.

The linker calls these functions in sequence:

ctf_link_add_ctf: to add each CTF section in the input in turn to a

newly-created ctf_file_t (which will appear in the output, and which
itself will become the shared parent that contains types that all
TUs have in common (in all link modes) and all types that do not
have conflicting definitions between types (by default). Input files
that are themselves products of ld -r are supported, though this is
not heavily tested yet.

ctf_link: called once all input files are added to merge the types in

all the input containers into the output container, eliminating
duplicates.

ctf_link_add_strtab: called once the ELF string table is finalized and

all its offsets are known, this calls a callback provided by the
linker which returns the string content and offset of every string in
the ELF strtab in turn: all these strings which appear in the input
CTF strtab are eliminated from it in favour of the ELF strtab:
equally, any strings that only appear in the input strtab will
reappear in the internal CTF strtab of the output.

ctf_link_shuffle_syms (not yet implemented): called once the ELF symtab

is finalized, this calls a callback provided by the linker which
returns information on every symbol in turn as a ctf_link_sym_t. This
is then used to shuffle the function info and data object sections in
the CTF section into symbol table order, eliminating the index
sections which map those sections to symbol names before that point.
Currently just returns ECTF_NOTYET.

ctf_link_write: Returns a buffer containing either a serialized

ctf_file_t (if there are no types with conflicting definitions in the
object files in the link) or a ctf_archive_t containing a large
ctf_file_t (the common types) and a bunch of small ones named after
individual CUs in which conflicting types are found (containing the
conflicting types, and all types that reference them). A threshold
size above which compression takes place is passed as one parameter.
(Currently, only gzip compression is supported, but I hope to add lzma
as well.)

Lifetime rules for this are simple: don't close the input CTF files
until you've called ctf_link for the last time. We do not assume
that symbols or strings passed in by the callback outlast the
call to ctf_link_add_strtab or ctf_link_shuffle_syms.

Right now, the duplicate elimination mechanism is the one already
present as part of the ctf_add_type function, and is not particularly
good: it misses numerous actual duplicates, and the conflicting-types
detection hardly ever reports that types conflict, even when they do
(one of them just tends to get silently dropped): it is also very slow.
This will all be fixed in the next few weeks, but the fix hardly touches
any of this code, and the linker does work without it, just not as
well as it otherwise might. (And when no CTF section is present,
there is no effect on performance, of course. So only people using
a trunk GCC with not-yet-committed patches will even notice. By the
time it gets upstream, things should be better.)

v3: Fix error handling.
v4: check for strdup failure.

include/
* ctf-api.h (struct ctf_link_sym): New, a symbol in flight to the
libctf linking machinery.
(CTF_LINK_SHARE_UNCONFLICTED): New.
(CTF_LINK_SHARE_DUPLICATED): New.
(ECTF_LINKADDEDLATE): New, replacing ECTF_UNUSED.
(ECTF_NOTYET): New, a 'not yet implemented' message.
(ctf_link_add_ctf): New, add an input file's CTF to the link.
(ctf_link): New, merge the type and string sections.
(ctf_link_strtab_string_f): New, callback for feeding strtab info.
(ctf_link_iter_symbol_f): New, callback for feeding symtab info.
(ctf_link_add_strtab): New, tell the CTF linker about the ELF
strtab's strings.
(ctf_link_shuffle_syms): New, ask the CTF linker to shuffle its
symbols into symtab order.
(ctf_link_write): New, ask the CTF linker to write the CTF out.

libctf/
* ctf-link.c: New file, linking of the string and type sections.
* Makefile.am (libctf_a_SOURCES): Add it.
* Makefile.in: Regenerate.

* ctf-impl.h (ctf_file_t): New fields ctf_link_inputs,
ctf_link_outputs.
* ctf-create.c (ctf_update): Update accordingly.
* ctf-open.c (ctf_file_close): Likewise.
* ctf-error.c (_ctf_errlist): Updated with new errors.

Mudança Sumário

Diff

--- a/include/ChangeLog
+++ b/include/ChangeLog
@@ -1,5 +1,23 @@
11 2019-07-13 Nick Alcock <nick.alcock@oracle.com>
22
3+ * ctf-api.h (struct ctf_link_sym): New, a symbol in flight to the
4+ libctf linking machinery.
5+ (CTF_LINK_SHARE_UNCONFLICTED): New.
6+ (CTF_LINK_SHARE_DUPLICATED): New.
7+ (ECTF_LINKADDEDLATE): New, replacing ECTF_UNUSED.
8+ (ECTF_NOTYET): New, a 'not yet implemented' message.
9+ (ctf_link_add_ctf): New, add an input file's CTF to the link.
10+ (ctf_link): New, merge the type and string sections.
11+ (ctf_link_strtab_string_f): New, callback for feeding strtab info.
12+ (ctf_link_iter_symbol_f): New, callback for feeding symtab info.
13+ (ctf_link_add_strtab): New, tell the CTF linker about the ELF
14+ strtab's strings.
15+ (ctf_link_shuffle_syms): New, ask the CTF linker to shuffle its
16+ symbols into symtab order.
17+ (ctf_link_write): New, ask the CTF linker to write the CTF out.
18+
19+2019-07-13 Nick Alcock <nick.alcock@oracle.com>
20+
321 * ctf-api.h (ctf_arc_write_fd): New.
422 (ctf_write_mem): Likewise.
523 (ctf_gzwrite): Spacing fix.
--- a/include/ctf-api.h
+++ b/include/ctf-api.h
@@ -65,6 +65,28 @@ typedef struct ctf_sect
6565 size_t cts_entsize; /* Size of each section entry (symtab only). */
6666 } ctf_sect_t;
6767
68+/* A minimal symbol extracted from a linker's internal symbol table
69+ representation. */
70+
71+typedef struct ctf_link_sym
72+{
73+ /* The st_name will not be accessed outside the call to
74+ ctf_link_shuffle_syms(). */
75+
76+ const char *st_name;
77+ uint32_t st_shndx;
78+ uint32_t st_type;
79+ uint32_t st_value;
80+} ctf_link_sym_t;
81+
82+/* Indication of how to share types when linking. */
83+
84+/* Share all types thare are not in conflict. The default. */
85+#define CTF_LINK_SHARE_UNCONFLICTED 0x0
86+
87+/* Share only types that are used by multiple inputs. Not implemented yet. */
88+#define CTF_LINK_SHARE_DUPLICATED 0x1
89+
6890 /* Symbolic names for CTF sections. */
6991
7092 typedef enum ctf_sect_names
@@ -145,7 +167,7 @@ enum
145167 ECTF_NOSYMTAB, /* Symbol table data is not available. */
146168 ECTF_NOPARENT, /* Parent CTF container is not available. */
147169 ECTF_DMODEL, /* Data model mismatch. */
148- ECTF_UNUSED, /* Unused error. */
170+ ECTF_LINKADDEDLATE, /* File added to link too late. */
149171 ECTF_ZALLOC, /* Failed to allocate (de)compression buffer. */
150172 ECTF_DECOMPRESS, /* Failed to decompress CTF data. */
151173 ECTF_STRTAB, /* String table for this string is missing. */
@@ -180,7 +202,8 @@ enum
180202 ECTF_ARNNAME, /* Name not found in CTF archive. */
181203 ECTF_SLICEOVERFLOW, /* Overflow of type bitness or offset in slice. */
182204 ECTF_DUMPSECTUNKNOWN, /* Unknown section number in dump. */
183- ECTF_DUMPSECTCHANGED /* Section changed in middle of dump. */
205+ ECTF_DUMPSECTCHANGED, /* Section changed in middle of dump. */
206+ ECTF_NOTYET /* Feature not yet implemented. */
184207 };
185208
186209 /* The CTF data model is inferred to be the caller's data model or the data
@@ -385,6 +408,18 @@ extern int ctf_gzwrite (ctf_file_t *fp, gzFile fd);
385408 extern int ctf_compress_write (ctf_file_t * fp, int fd);
386409 extern unsigned char *ctf_write_mem (ctf_file_t *, size_t *, size_t threshold);
387410
411+extern int ctf_link_add_ctf (ctf_file_t *, ctf_archive_t *, const char *);
412+extern int ctf_link (ctf_file_t *, int share_mode);
413+typedef const char *ctf_link_strtab_string_f (uint32_t *offset, void *arg);
414+extern int ctf_link_add_strtab (ctf_file_t *, ctf_link_strtab_string_f *,
415+ void *);
416+typedef ctf_link_sym_t *ctf_link_iter_symbol_f (ctf_link_sym_t *dest,
417+ void *arg);
418+extern int ctf_link_shuffle_syms (ctf_file_t *, ctf_link_iter_symbol_f *,
419+ void *);
420+extern unsigned char *ctf_link_write (ctf_file_t *, size_t *size,
421+ size_t threshold);
422+
388423 extern void ctf_setdebug (int debug);
389424 extern int ctf_getdebug (void);
390425
--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,5 +1,17 @@
11 2019-07-13 Nick Alcock <nick.alcock@oracle.com>
22
3+ * ctf-link.c: New file, linking of the string and type sections.
4+ * Makefile.am (libctf_a_SOURCES): Add it.
5+ * Makefile.in: Regenerate.
6+
7+ * ctf-impl.h (ctf_file_t): New fields ctf_link_inputs,
8+ ctf_link_outputs.
9+ * ctf-create.c (ctf_update): Update accordingly.
10+ * ctf-open.c (ctf_file_close): Likewise.
11+ * ctf-error.c (_ctf_errlist): Updated with new errors.
12+
13+2019-07-13 Nick Alcock <nick.alcock@oracle.com>
14+
315 * ctf-dump.c (ctf_dump_funcs): Check the right error value.
416
517 2019-07-13 Nick Alcock <nick.alcock@oracle.com>
--- a/libctf/Makefile.am
+++ b/libctf/Makefile.am
@@ -33,8 +33,8 @@ AM_CFLAGS = -std=gnu99 @ac_libctf_warn_cflags@ @warn@ @c_warn@ @WARN_PEDANTIC@ @
3333 noinst_LIBRARIES = libctf.a
3434
3535 libctf_a_SOURCES = ctf-archive.c ctf-dump.c ctf-create.c ctf-decl.c ctf-error.c \
36- ctf-hash.c ctf-labels.c ctf-lookup.c ctf-open.c ctf-open-bfd.c \
37- ctf-string.c ctf-subr.c ctf-types.c ctf-util.c
36+ ctf-hash.c ctf-labels.c ctf-link.c ctf-lookup.c ctf-open.c \
37+ ctf-open-bfd.c ctf-string.c ctf-subr.c ctf-types.c ctf-util.c
3838 if NEED_CTF_QSORT_R
3939 libctf_a_SOURCES += ctf-qsort_r.c
4040 endif
--- a/libctf/Makefile.in
+++ b/libctf/Makefile.in
@@ -131,16 +131,16 @@ am__v_AR_1 =
131131 libctf_a_AR = $(AR) $(ARFLAGS)
132132 libctf_a_LIBADD =
133133 am__libctf_a_SOURCES_DIST = ctf-archive.c ctf-dump.c ctf-create.c \
134- ctf-decl.c ctf-error.c ctf-hash.c ctf-labels.c ctf-lookup.c \
135- ctf-open.c ctf-open-bfd.c ctf-string.c ctf-subr.c ctf-types.c \
136- ctf-util.c ctf-qsort_r.c
134+ ctf-decl.c ctf-error.c ctf-hash.c ctf-labels.c ctf-link.c \
135+ ctf-lookup.c ctf-open.c ctf-open-bfd.c ctf-string.c ctf-subr.c \
136+ ctf-types.c ctf-util.c ctf-qsort_r.c
137137 @NEED_CTF_QSORT_R_TRUE@am__objects_1 = ctf-qsort_r.$(OBJEXT)
138138 am_libctf_a_OBJECTS = ctf-archive.$(OBJEXT) ctf-dump.$(OBJEXT) \
139139 ctf-create.$(OBJEXT) ctf-decl.$(OBJEXT) ctf-error.$(OBJEXT) \
140- ctf-hash.$(OBJEXT) ctf-labels.$(OBJEXT) ctf-lookup.$(OBJEXT) \
141- ctf-open.$(OBJEXT) ctf-open-bfd.$(OBJEXT) ctf-string.$(OBJEXT) \
142- ctf-subr.$(OBJEXT) ctf-types.$(OBJEXT) ctf-util.$(OBJEXT) \
143- $(am__objects_1)
140+ ctf-hash.$(OBJEXT) ctf-labels.$(OBJEXT) ctf-link.$(OBJEXT) \
141+ ctf-lookup.$(OBJEXT) ctf-open.$(OBJEXT) ctf-open-bfd.$(OBJEXT) \
142+ ctf-string.$(OBJEXT) ctf-subr.$(OBJEXT) ctf-types.$(OBJEXT) \
143+ ctf-util.$(OBJEXT) $(am__objects_1)
144144 libctf_a_OBJECTS = $(am_libctf_a_OBJECTS)
145145 AM_V_P = $(am__v_P_@AM_V@)
146146 am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -331,9 +331,9 @@ AM_CPPFLAGS = -D_GNU_SOURCE -I$(top_srcdir) -I$(top_srcdir)/../include -I$(top_s
331331 AM_CFLAGS = -std=gnu99 @ac_libctf_warn_cflags@ @warn@ @c_warn@ @WARN_PEDANTIC@ @WERROR@ $(ZLIBINC)
332332 noinst_LIBRARIES = libctf.a
333333 libctf_a_SOURCES = ctf-archive.c ctf-dump.c ctf-create.c ctf-decl.c \
334- ctf-error.c ctf-hash.c ctf-labels.c ctf-lookup.c ctf-open.c \
335- ctf-open-bfd.c ctf-string.c ctf-subr.c ctf-types.c ctf-util.c \
336- $(am__append_1)
334+ ctf-error.c ctf-hash.c ctf-labels.c ctf-link.c ctf-lookup.c \
335+ ctf-open.c ctf-open-bfd.c ctf-string.c ctf-subr.c ctf-types.c \
336+ ctf-util.c $(am__append_1)
337337 all: config.h
338338 $(MAKE) $(AM_MAKEFLAGS) all-am
339339
@@ -409,6 +409,7 @@ distclean-compile:
409409 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctf-error.Po@am__quote@
410410 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctf-hash.Po@am__quote@
411411 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctf-labels.Po@am__quote@
412+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctf-link.Po@am__quote@
412413 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctf-lookup.Po@am__quote@
413414 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctf-open-bfd.Po@am__quote@
414415 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ctf-open.Po@am__quote@
--- a/libctf/ctf-create.c
+++ b/libctf/ctf-create.c
@@ -470,6 +470,8 @@ ctf_update (ctf_file_t *fp)
470470 nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
471471 nfp->ctf_snapshots = fp->ctf_snapshots + 1;
472472 nfp->ctf_specific = fp->ctf_specific;
473+ nfp->ctf_link_inputs = fp->ctf_link_inputs;
474+ nfp->ctf_link_outputs = fp->ctf_link_outputs;
473475 nfp->ctf_syn_ext_strtab = fp->ctf_syn_ext_strtab;
474476
475477 nfp->ctf_snapshot_lu = fp->ctf_snapshots;
@@ -480,6 +482,8 @@ ctf_update (ctf_file_t *fp)
480482 nfp->ctf_str_atoms = fp->ctf_str_atoms;
481483 fp->ctf_str_atoms = NULL;
482484 memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t));
485+ fp->ctf_link_inputs = NULL;
486+ fp->ctf_link_outputs = NULL;
483487 fp->ctf_syn_ext_strtab = NULL;
484488
485489 fp->ctf_dvhash = NULL;
--- a/libctf/ctf-error.c
+++ b/libctf/ctf-error.c
@@ -33,7 +33,7 @@ static const char *const _ctf_errlist[] = {
3333 "Symbol table information is not available", /* ECTF_NOSYMTAB */
3434 "Type information is in parent and unavailable", /* ECTF_NOPARENT */
3535 "Cannot import types with different data model", /* ECTF_DMODEL */
36- "Unused error", /* ECTF_UNUSED */
36+ "File added to link too late", /* ECTF_LINKADDEDLATE */
3737 "Failed to allocate (de)compression buffer", /* ECTF_ZALLOC */
3838 "Failed to decompress CTF data", /* ECTF_DECOMPRESS */
3939 "External string table is not available", /* ECTF_STRTAB */
@@ -68,7 +68,8 @@ static const char *const _ctf_errlist[] = {
6868 "Name not found in CTF archive", /* ECTF_ARNNAME */
6969 "Overflow of type bitness or offset in slice", /* ECTF_SLICEOVERFLOW */
7070 "Unknown section number in dump", /* ECTF_DUMPSECTUNKNOWN */
71- "Section changed in middle of dump" /* ECTF_DUMPSECTCHANGED */
71+ "Section changed in middle of dump", /* ECTF_DUMPSECTCHANGED */
72+ "Feature not yet implemented" /* ECTF_NOTYET */
7273 };
7374
7475 static const int _ctf_nerr = sizeof (_ctf_errlist) / sizeof (_ctf_errlist[0]);
--- a/libctf/ctf-impl.h
+++ b/libctf/ctf-impl.h
@@ -267,6 +267,8 @@ struct ctf_file
267267 unsigned long ctf_snapshots; /* ctf_snapshot() plus ctf_update() count. */
268268 unsigned long ctf_snapshot_lu; /* ctf_snapshot() call count at last update. */
269269 ctf_archive_t *ctf_archive; /* Archive this ctf_file_t came from. */
270+ ctf_dynhash_t *ctf_link_inputs; /* Inputs to this link. */
271+ ctf_dynhash_t *ctf_link_outputs; /* Additional outputs from this link. */
270272 char *ctf_tmp_typeslice; /* Storage for slicing up type names. */
271273 size_t ctf_tmp_typeslicelen; /* Size of the typeslice. */
272274 void *ctf_specific; /* Data for ctf_get/setspecific(). */
--- /dev/null
+++ b/libctf/ctf-link.c
@@ -0,0 +1,530 @@
1+/* CTF linking.
2+ Copyright (C) 2019 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+/* Type tracking machinery. */
24+/* Linker machinery.
25+
26+ CTF linking consists of adding CTF archives full of content to be merged into
27+ this one to the current file (which must be writable) by calling
28+ ctf_link_add_ctf(). Once this is done, a call to ctf_link() will merge the
29+ type tables together, generating new CTF files as needed, with this one as a
30+ parent, to contain types from the inputs which conflict.
31+ ctf_link_add_strtab() takes a callback which provides string/offset pairs to
32+ be added to the external symbol table and deduplicated from all CTF string
33+ tables in the output link; ctf_link_shuffle_syms() takes a callback which
34+ provides symtab entries in ascending order, and shuffles the function and
35+ data sections to match; and ctf_link_write() emits a CTF file (if there are
36+ no conflicts requiring per-compilation-unit sub-CTF files) or CTF archives
37+ (otherwise) and returns it, suitable for addition in the .ctf section of the
38+ output. */
39+
40+/* Add a file to a link. */
41+
42+static void ctf_arc_close_thunk (void *arc)
43+{
44+ ctf_arc_close ((ctf_archive_t *) arc);
45+}
46+
47+static void ctf_file_close_thunk (void *file)
48+{
49+ ctf_file_close ((ctf_file_t *) file);
50+}
51+
52+int
53+ctf_link_add_ctf (ctf_file_t *fp, ctf_archive_t *ctf, const char *name)
54+{
55+ char *dupname = NULL;
56+
57+ if (fp->ctf_link_outputs)
58+ return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
59+ if (fp->ctf_link_inputs == NULL)
60+ fp->ctf_link_inputs = ctf_dynhash_create (ctf_hash_string,
61+ ctf_hash_eq_string, free,
62+ ctf_arc_close_thunk);
63+
64+ if (fp->ctf_link_inputs == NULL)
65+ goto oom;
66+
67+ if ((dupname = strdup (name)) == NULL)
68+ goto oom;
69+
70+ if (ctf_dynhash_insert (fp->ctf_link_inputs, dupname, ctf) < 0)
71+ goto oom;
72+
73+ return 0;
74+ oom:
75+ free (fp->ctf_link_inputs);
76+ fp->ctf_link_inputs = NULL;
77+ free (dupname);
78+ return (ctf_set_errno (fp, ENOMEM));
79+}
80+
81+typedef struct ctf_link_in_member_cb_arg
82+{
83+ ctf_file_t *out_fp;
84+ const char *file_name;
85+ ctf_file_t *in_fp;
86+ ctf_file_t *main_input_fp;
87+ const char *cu_name;
88+ char *arcname;
89+ int done_main_member;
90+ int share_mode;
91+ int in_input_cu_file;
92+} ctf_link_in_member_cb_arg_t;
93+
94+
95+/* Link one type into the link. We rely on ctf_add_type() to detect
96+ duplicates. This is not terribly reliable yet (unnmamed types will be
97+ mindlessly duplicated), but will improve shortly. */
98+
99+static int
100+ctf_link_one_type (ctf_id_t type, int isroot _libctf_unused_, void *arg_)
101+{
102+ ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
103+ ctf_file_t *per_cu_out_fp;
104+ int err;
105+
106+ if (arg->share_mode != CTF_LINK_SHARE_UNCONFLICTED)
107+ {
108+ ctf_dprintf ("Share-duplicated mode not yet implemented.\n");
109+ return ctf_set_errno (arg->out_fp, ECTF_NOTYET);
110+ }
111+
112+ /* Simply call ctf_add_type: if it reports a conflict and we're adding to the
113+ main CTF file, add to the per-CU archive member instead, creating it if
114+ necessary. If we got this type from a per-CU archive member, add it
115+ straight back to the corresponding member in the output. */
116+
117+ if (!arg->in_input_cu_file)
118+ {
119+ if (ctf_add_type (arg->out_fp, arg->in_fp, type) != CTF_ERR)
120+ return 0;
121+
122+ err = ctf_errno (arg->out_fp);
123+ if (err != ECTF_CONFLICT)
124+ {
125+ ctf_dprintf ("Cannot link type %lx from archive member %s, input file %s "
126+ "into output link: %s\n", type, arg->arcname, arg->file_name,
127+ ctf_errmsg (err));
128+ return -1;
129+ }
130+ ctf_set_errno (arg->out_fp, 0);
131+ }
132+
133+ if ((per_cu_out_fp = ctf_dynhash_lookup (arg->out_fp->ctf_link_outputs,
134+ arg->arcname)) == NULL)
135+ {
136+ int err;
137+
138+ if ((per_cu_out_fp = ctf_create (&err)) == NULL)
139+ {
140+ ctf_dprintf ("Cannot create per-CU CTF archive for member %s: %s\n",
141+ arg->arcname, ctf_errmsg (err));
142+ ctf_set_errno (arg->out_fp, err);
143+ return -1;
144+ }
145+
146+ if (ctf_dynhash_insert (arg->out_fp->ctf_link_outputs, arg->arcname,
147+ per_cu_out_fp) < 0)
148+ {
149+ ctf_set_errno (arg->out_fp, ENOMEM);
150+ return -1;
151+ }
152+
153+ ctf_import (per_cu_out_fp, arg->out_fp);
154+ ctf_cuname_set (per_cu_out_fp, arg->cu_name);
155+ }
156+
157+ if (ctf_add_type (per_cu_out_fp, arg->in_fp, type) != CTF_ERR)
158+ return 0;
159+
160+ err = ctf_errno (per_cu_out_fp);
161+ if (err == ECTF_CONFLICT)
162+ /* Conflicts are possible at this stage only if a non-ld user has combined
163+ multiple TUs into a single output dictionary. Even in this case we do not
164+ want to stop the link or propagate the error. */
165+ ctf_set_errno (arg->out_fp, 0);
166+
167+ return 0; /* As above: do not lose types. */
168+}
169+
170+/* Merge every type and variable in this archive member into the link, so we can
171+ relink things that have already had ld run on them. We use the archive
172+ member name, sans any leading '.ctf.', as the CU name for ambiguous types if
173+ there is one and it's not the default: otherwise, we use the name of the
174+ input file. */
175+static int
176+ctf_link_one_input_archive_member (ctf_file_t *in_fp, const char *name, void *arg_)
177+{
178+ ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
179+ int err = 0;
180+
181+ if (strcmp (name, _CTF_SECTION) == 0)
182+ {
183+ /* This file is the default member of this archive, and has already been
184+ explicitly processed.
185+
186+ In the default sharing mode of CTF_LINK_SHARE_UNCONFLICTED, it does no
187+ harm to rescan an existing shared repo again: all the types will just
188+ end up in the same place. But in CTF_LINK_SHARE_DUPLICATED mode, this
189+ causes the system to erroneously conclude that all types are duplicated
190+ and should be shared, even if they are not. */
191+
192+ if (arg->done_main_member)
193+ return 0;
194+ arg->arcname = strdup (".ctf.");
195+ if (arg->arcname)
196+ {
197+ char *new_name;
198+
199+ new_name = ctf_str_append (arg->arcname, arg->file_name);
200+ if (new_name)
201+ arg->arcname = new_name;
202+ else
203+ free (arg->arcname);
204+ }
205+ }
206+ else
207+ {
208+ arg->arcname = strdup (name);
209+
210+ /* Get ambiguous types from our parent. */
211+ ctf_import (in_fp, arg->main_input_fp);
212+ arg->in_input_cu_file = 1;
213+ }
214+
215+ if (!arg->arcname)
216+ return ctf_set_errno (in_fp, ENOMEM);
217+
218+ arg->cu_name = name;
219+ if (strncmp (arg->cu_name, ".ctf.", strlen (".ctf.")) == 0)
220+ arg->cu_name += strlen (".ctf.");
221+ arg->in_fp = in_fp;
222+
223+ err = ctf_type_iter_all (in_fp, ctf_link_one_type, arg);
224+
225+ arg->in_input_cu_file = 0;
226+ free (arg->arcname);
227+
228+ if (err < 0)
229+ return -1; /* Errno is set for us. */
230+
231+ return 0;
232+}
233+
234+/* Link one input file's types into the output file. */
235+static void
236+ctf_link_one_input_archive (void *key, void *value, void *arg_)
237+{
238+ const char *file_name = (const char *) key;
239+ ctf_archive_t *arc = (ctf_archive_t *) value;
240+ ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
241+ int err;
242+
243+ arg->file_name = file_name;
244+ arg->done_main_member = 0;
245+ if ((arg->main_input_fp = ctf_arc_open_by_name (arc, NULL, &err)) == NULL)
246+ if (err != ECTF_ARNNAME)
247+ {
248+ ctf_dprintf ("Cannot open main archive member in input file %s in the "
249+ "link: skipping: %s.\n", arg->file_name,
250+ ctf_errmsg (err));
251+ return;
252+ }
253+
254+ if (ctf_link_one_input_archive_member (arg->main_input_fp,
255+ _CTF_SECTION, arg) < 0)
256+ {
257+ ctf_file_close (arg->main_input_fp);
258+ return;
259+ }
260+ arg->done_main_member = 1;
261+ if (ctf_archive_iter (arc, ctf_link_one_input_archive_member, arg) < 0)
262+ ctf_dprintf ("Cannot traverse archive in input file %s: link "
263+ "cannot continue: %s.\n", arg->file_name,
264+ ctf_errmsg (ctf_errno (arg->out_fp)));
265+ else
266+ {
267+ /* The only error indication to the caller is the errno: so ensure that it
268+ is zero if there was no actual error from the caller. */
269+ ctf_set_errno (arg->out_fp, 0);
270+ }
271+ ctf_file_close (arg->main_input_fp);
272+}
273+
274+/* Merge types and variable sections in all files added to the link
275+ together. */
276+int
277+ctf_link (ctf_file_t *fp, int share_mode)
278+{
279+ ctf_link_in_member_cb_arg_t arg;
280+
281+ memset (&arg, 0, sizeof (struct ctf_link_in_member_cb_arg));
282+ arg.out_fp = fp;
283+ arg.share_mode = share_mode;
284+
285+ if (fp->ctf_link_inputs == NULL)
286+ return 0; /* Nothing to do. */
287+
288+ if (fp->ctf_link_outputs == NULL)
289+ fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
290+ ctf_hash_eq_string, free,
291+ ctf_file_close_thunk);
292+
293+ if (fp->ctf_link_outputs == NULL)
294+ return ctf_set_errno (fp, ENOMEM);
295+
296+ ctf_dynhash_iter (fp->ctf_link_inputs, ctf_link_one_input_archive,
297+ &arg);
298+
299+ if (ctf_errno (fp) != 0)
300+ return -1;
301+ return 0;
302+}
303+
304+typedef struct ctf_link_out_string_cb_arg
305+{
306+ const char *str;
307+ uint32_t offset;
308+ int err;
309+} ctf_link_out_string_cb_arg_t;
310+
311+/* Intern a string in the string table of an output per-CU CTF file. */
312+static void
313+ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
314+ void *arg_)
315+{
316+ ctf_file_t *fp = (ctf_file_t *) value;
317+ ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
318+
319+ fp->ctf_flags |= LCTF_DIRTY;
320+ if (ctf_str_add_external (fp, arg->str, arg->offset) == NULL)
321+ arg->err = ENOMEM;
322+}
323+
324+/* Repeatedly call ADD_STRING to acquire strings from the external string table,
325+ adding them to the atoms table for this CU and all subsidiary CUs.
326+
327+ If ctf_link() is also called, it must be called first if you want the new CTF
328+ files ctf_link() can create to get their strings dedupped against the ELF
329+ strtab properly. */
330+int
331+ctf_link_add_strtab (ctf_file_t *fp, ctf_link_strtab_string_f *add_string,
332+ void *arg)
333+{
334+ const char *str;
335+ uint32_t offset;
336+ int err = 0;
337+
338+ while ((str = add_string (&offset, arg)) != NULL)
339+ {
340+ ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
341+
342+ fp->ctf_flags |= LCTF_DIRTY;
343+ if (ctf_str_add_external (fp, str, offset) == NULL)
344+ err = ENOMEM;
345+
346+ ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
347+ &iter_arg);
348+ if (iter_arg.err)
349+ err = iter_arg.err;
350+ }
351+
352+ return -err;
353+}
354+
355+/* Not yet implemented. */
356+int
357+ctf_link_shuffle_syms (ctf_file_t *fp _libctf_unused_,
358+ ctf_link_iter_symbol_f *add_sym _libctf_unused_,
359+ void *arg _libctf_unused_)
360+{
361+ return 0;
362+}
363+
364+typedef struct ctf_name_list_accum_cb_arg
365+{
366+ char **names;
367+ ctf_file_t *fp;
368+ ctf_file_t **files;
369+ size_t i;
370+} ctf_name_list_accum_cb_arg_t;
371+
372+/* Accumulate the names and a count of the names in the link output hash,
373+ and run ctf_update() on them to generate them. */
374+static void
375+ctf_accumulate_archive_names (void *key, void *value, void *arg_)
376+{
377+ const char *name = (const char *) key;
378+ ctf_file_t *fp = (ctf_file_t *) value;
379+ char **names;
380+ ctf_file_t **files;
381+ ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
382+ int err;
383+
384+ if ((err = ctf_update (fp)) < 0)
385+ {
386+ ctf_set_errno (arg->fp, ctf_errno (fp));
387+ return;
388+ }
389+
390+ if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
391+ {
392+ (arg->i)--;
393+ ctf_set_errno (arg->fp, ENOMEM);
394+ return;
395+ }
396+
397+ if ((files = realloc (arg->files, sizeof (ctf_file_t *) * arg->i)) == NULL)
398+ {
399+ (arg->i)--;
400+ ctf_set_errno (arg->fp, ENOMEM);
401+ return;
402+ }
403+ arg->names = names;
404+ arg->names[(arg->i) - 1] = (char *) name;
405+ arg->files = files;
406+ arg->files[(arg->i) - 1] = fp;
407+}
408+
409+/* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
410+ (otherwise) into a new dynamically-allocated string, and return it.
411+ Members with sizes above THRESHOLD are compressed. */
412+unsigned char *
413+ctf_link_write (ctf_file_t *fp, size_t *size, size_t threshold)
414+{
415+ ctf_name_list_accum_cb_arg_t arg;
416+ char **names;
417+ ctf_file_t **files;
418+ FILE *f = NULL;
419+ int err;
420+ long fsize;
421+ const char *errloc;
422+ unsigned char *buf = NULL;
423+
424+ memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
425+ arg.fp = fp;
426+
427+ if (ctf_update (fp) < 0)
428+ {
429+ errloc = "CTF file construction";
430+ goto err;
431+ }
432+
433+ if (fp->ctf_link_outputs)
434+ {
435+ ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
436+ if (ctf_errno (fp) < 0)
437+ {
438+ errloc = "hash creation";
439+ goto err;
440+ }
441+ }
442+
443+ /* No extra outputs? Just write a simple ctf_file_t. */
444+ if (arg.i == 0)
445+ return ctf_write_mem (fp, size, threshold);
446+
447+ /* Writing an archive. Stick ourselves (the shared repository, parent of all
448+ other archives) on the front of it with the default name. */
449+ if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL)
450+ {
451+ errloc = "name reallocation";
452+ goto err_no;
453+ }
454+ arg.names = names;
455+ memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
456+ arg.names[0] = (char *) _CTF_SECTION;
457+
458+ if ((files = realloc (arg.files,
459+ sizeof (struct ctf_file *) * (arg.i + 1))) == NULL)
460+ {
461+ errloc = "ctf_file reallocation";
462+ goto err_no;
463+ }
464+ arg.files = files;
465+ memmove (&(arg.files[1]), arg.files, sizeof (ctf_file_t *) * (arg.i));
466+ arg.files[0] = fp;
467+
468+ if ((f = tmpfile ()) == NULL)
469+ {
470+ errloc = "tempfile creation";
471+ goto err_no;
472+ }
473+
474+ if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1,
475+ (const char **) arg.names,
476+ threshold)) < 0)
477+ {
478+ errloc = "archive writing";
479+ ctf_set_errno (fp, err);
480+ goto err;
481+ }
482+
483+ if (fseek (f, 0, SEEK_END) < 0)
484+ {
485+ errloc = "seeking to end";
486+ goto err_no;
487+ }
488+
489+ if ((fsize = ftell (f)) < 0)
490+ {
491+ errloc = "filesize determination";
492+ goto err_no;
493+ }
494+
495+ if (fseek (f, 0, SEEK_SET) < 0)
496+ {
497+ errloc = "filepos resetting";
498+ goto err_no;
499+ }
500+
501+ if ((buf = malloc (fsize)) == NULL)
502+ {
503+ errloc = "CTF archive buffer allocation";
504+ goto err_no;
505+ }
506+
507+ while (!feof (f) && fread (buf, fsize, 1, f) == 0)
508+ if (ferror (f))
509+ {
510+ errloc = "reading archive from temporary file";
511+ goto err_no;
512+ }
513+
514+ *size = fsize;
515+ free (arg.names);
516+ free (arg.files);
517+ return buf;
518+
519+ err_no:
520+ ctf_set_errno (fp, errno);
521+ err:
522+ free (buf);
523+ if (f)
524+ fclose (f);
525+ free (arg.names);
526+ free (arg.files);
527+ ctf_dprintf ("Cannot write archive in link: %s failure: %s\n", errloc,
528+ ctf_errmsg (ctf_errno (fp)));
529+ return NULL;
530+}
--- a/libctf/ctf-open.c
+++ b/libctf/ctf-open.c
@@ -1626,6 +1626,8 @@ ctf_file_close (ctf_file_t *fp)
16261626 ctf_free (fp->ctf_dynbase);
16271627
16281628 ctf_dynhash_destroy (fp->ctf_syn_ext_strtab);
1629+ ctf_dynhash_destroy (fp->ctf_link_inputs);
1630+ ctf_dynhash_destroy (fp->ctf_link_outputs);
16291631
16301632 ctf_free (fp->ctf_sxlate);
16311633 ctf_free (fp->ctf_txlate);