[PATCH 00/19] CTF linking support

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 00/19] CTF linking support

Nick Alcock
On 16 Jul 2019, Nick Alcock outgrape:

> This is the beginning of support for linking CTF sections.  The type
> deduplicator is not very good,

For the avoidance of doubt: this series doesn't actually *contain* the
type deduplicator. We use ctf_add_type() as the deduplicator, which
already existed (and was already not very good). We'll improve the
deduplicator in a later series, but right now just wiring it up made for
a series that was quite large enough! ctf_add_type() itself will remain
the core of the deduplicator: it'll just get much better at spotting
duplicates reliably, while retaining a very similar interface to what it
has now.

The only part of *this series* that I worry might not be very good is
the ld/bfd part, because, well, I've never done anything this big in ld
before and it might well be doing everything wrong but just happen to be
working anyway. I don't know. That's why I'd like review :)
Reply | Threaded
Open this post in threaded view
|

[PATCH 20/19 REVIEW] ld: Make CTF section linking work with non-ELF

Nick Alcock
In reply to this post by Nick Alcock
Basically just moves stuff into the emulation.

Incremental patch for ease of reviewing, will be squashed into the
previous one in the next series (or I can send out a squash now, if
people would find that easier to review).

No regressions on x86_64: builds fine with --enable-targets=all: cross-builds
and checks now show no regressions for what I believe is every target
supported by binutils (where before this patch non-ELF targets failed).
Additionally verified that (except where custom linker scripts cause the .ctf
section not to be mapped into the output) make check on all targets always
causes CTF to be emitted via one of the two possible paths (early versus late):
on ELF, the only paths that don't lead to strtab deduplication and symtab
shuffling are links with no strings or symbols (and in that case, the resulting
CTF file still *works* fine, it might just be a little larger or slower to open
than otherwise).

Full changelog for both patches combined:

        * Makefile.def (dependencies): all-ld depends on all-libctf.
        * Makefile.in: Regenerated.

include/
        * bfdlink.h (elf_strtab_hash): New forward.
        (elf_sym_strtab): Likewise.
        (struct bfd_link_callbacks <examine_strtab>): New.
        (struct bfd_link_callbacks <emit_ctf>): Likewise.

bfd/
        * elf-bfd.h (SECTION_IS_CTF): New macro.
        * elf.c (special_sections_c): Add ".ctf".
        (assign_file_positions_for_non_load_sections): Note that
        compressed debugging sections etc are not assigned here.
        Treat CTF sections like SEC_ELF_COMPRESS sections: sh_offset -1.
        (assign_file_positions_except_relocs): Likewise.
        (find_section_in_list): Note that debugging and CTF sections, as
        well as reloc sections, are assigned later.
        (_bfd_elf_assign_file_positions_for_non_load): CTF sections get
        their size and contents updated.
        (_bfd_elf_set_section_contents): Skip CTF sections: unlike
        compressed sections, they have no uncompressed content to copy at
        this stage.
        * elflink.c (elf_link_swap_symbols_out): Call the examine_strtab
        callback right before the strtab is written out.
        (bfd_elf_final_link): Don't cache the section contents of CTF
        sections: they are not populated yet.  Call the emit_ctf callback
        right at the end, after all the symbols and strings are flushed
        out.
ld/
        * scripttempl/DWARF.sc: Add .ctf.
        * ldlang.h (includes): Add elf-bfd.h, ctf-api.h.  Prevent NAME in
        elf-bfd.h from wreaking havoc.
        (struct lang_input_statement_struct): Add the_ctf.
        (ldlang_ctf_apply_strsym): Declare.
        (ldlang_write_ctf_late): Likewise.
        * ldemul.h (ldemul_emit_ctf_early): New.
        (ldemul_examine_strtab_for_ctf): Likewise.
        (ld_emulation_xfer_type) <emit_ctf_early>: Likewise.
        (ld_emulation_xfer_type) <examine_strtab_for_ctf>: Likewise.
        * ldemul.c (ldemul_emit_ctf_early): New.
        (ldemul_examine_strtab_for_ctf): Likewise.
        * ldlang.c (includes): elf-bfd.h is now included by ldlang.h.
        Include elf/internal.h.
        * ldmain.c (link_callbacks): Add ldlang_ctf_apply_strsym,
        ldlang_write_ctf_late.
        (ctf_output): New. Initialized in...
        (ldlang_open_ctf): ... this new function.  Open all the CTF
        sections in the input files: mark them non-loaded and empty
        so as not to copy their contents to the output.
        (ldlang_merge_ctf): New, merge types via ctf_link_add_ctf and
        ctf_link.
        (ldlang_ctf_apply_strsym): New, an examine_strtab callback: wrap
        ldemul_examine_strtab_for_ctf.
        (lang_write_ctf): New, write out the CTF section.
        (ldlang_write_ctf_late): New, late call via bfd's emit_ctf hook.
        (lang_process): Call ldlang_open_ctf, ldlang_merge_ctf, and
        lang_write_ctf.

        * emultempl/elf-generic.em
        (gld_${EMULATION_NAME}_emit_ctf_early): New.
        (gld_${EMULATION_NAME}_examine_strtab_for_ctf): tell libctf about
        the symtab and strtab.
        (struct ctf_strsym_iter_cb_arg): New, state to do so.
        (gld_${EMULATION_NAME}_ctf_strtab_iter_cb): New: tell libctf about
        each string in the strtab in turn.
        (gld_${EMULATION_NAME}_ctf_symbols_iter_cb): New, tell libctf
        about each symbol in the symtab in turn.
        (LDEMUL_EMIT_CTF_EARLY): Set it.
        (LDEMUL_EXAMINE_STRTAB_FOR_CTF): Likewise.
        * emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Add
        emit_ctf_early and examine_strtab_for_ctf, NULL by default.
        * emultempl/armcoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/beos.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/elf32.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/generic.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/linux.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/msp430.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/pe.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/pep.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/ticoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/vanilla.em (ld_${EMULATION_NAME}_emulation): Likewise.

        * Makefile.am: Pull in libctf (and zlib, a transitive requirement
        for compressed CTF section emission).  Pass it on to DejaGNU.
        * configure.ac: Add AM_ZLIB.
        * aclocal.m4: Added zlib.m4.
        * Makefile.in: Regenerated.
        * testsuite/ld-bootstrap/bootstrap.exp: Use it when relinking ld.
---
 ld/ChangeLog                |  51 ++++++++++----
 ld/emultempl/aix.em         |   4 +-
 ld/emultempl/armcoff.em     |   4 +-
 ld/emultempl/beos.em        |   4 +-
 ld/emultempl/elf-generic.em | 115 ++++++++++++++++++++++++++++++
 ld/emultempl/elf32.em       |   4 +-
 ld/emultempl/generic.em     |   4 +-
 ld/emultempl/linux.em       |   4 +-
 ld/emultempl/msp430.em      |   4 +-
 ld/emultempl/pe.em          |   4 +-
 ld/emultempl/pep.em         |   4 +-
 ld/emultempl/ticoff.em      |   4 +-
 ld/emultempl/vanilla.em     |   4 +-
 ld/ldemul.c                 |  22 ++++++
 ld/ldemul.h                 |  21 ++++++
 ld/ldlang.c                 | 135 ++++++++++--------------------------
 ld/ldlang.h                 |   2 +-
 ld/ldmain.c                 |   2 +-
 18 files changed, 268 insertions(+), 124 deletions(-)

diff --git a/ld/emultempl/aix.em b/ld/emultempl/aix.em
index bcf959d613..6b78d168f0 100644
--- a/ld/emultempl/aix.em
+++ b/ld/emultempl/aix.em
@@ -1560,6 +1560,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = {
   NULL, /* recognized_file */
   NULL, /* find potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/armcoff.em b/ld/emultempl/armcoff.em
index 20122aa03f..51ee0259f3 100644
--- a/ld/emultempl/armcoff.em
+++ b/ld/emultempl/armcoff.em
@@ -283,6 +283,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/beos.em b/ld/emultempl/beos.em
index bf75c5450a..bcf93124bb 100644
--- a/ld/emultempl/beos.em
+++ b/ld/emultempl/beos.em
@@ -787,6 +787,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/elf-generic.em b/ld/emultempl/elf-generic.em
index ac60f99925..e8c0a5eb9d 100644
--- a/ld/emultempl/elf-generic.em
+++ b/ld/emultempl/elf-generic.em
@@ -68,4 +68,119 @@ gld${EMULATION_NAME}_map_segments (bfd_boolean need_layout)
   if (tries == 0)
     einfo (_("%F%P: looping in map_segments"));
 }
+
+/* We want to emit CTF early if and only if we are not targetting ELF with this
+   invocation.  */
+
+static int
+gld_${EMULATION_NAME}_emit_ctf_early (void)
+{
+  if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
+    return 0;
+  return 1;
+}
+
+/* Callbacks used to map from bfd types to libctf types, under libctf's
+   control.  */
+
+struct ctf_strsym_iter_cb_arg
+{
+  struct elf_sym_strtab *syms;
+  bfd_size_type symcount;
+  struct elf_strtab_hash *symstrtab;
+  size_t next_i;
+  size_t next_idx;
+};
+
+/* Return strings from the strtab to libctf, one by one.  Returns NULL when
+   iteration is complete.  */
+
+static const char *
+gld_${EMULATION_NAME}_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
+{
+  bfd_size_type off;
+  const char *ret;
+
+  struct ctf_strsym_iter_cb_arg *arg =
+    (struct ctf_strsym_iter_cb_arg *) arg_;
+
+  /* There is no zeroth string.  */
+  if (arg->next_i == 0)
+    arg->next_i = 1;
+
+  if (arg->next_i >= _bfd_elf_strtab_len (arg->symstrtab))
+    {
+      arg->next_i = 0;
+      return NULL;
+    }
+
+  ret = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i++, &off);
+  *offset = off;
+
+  /* If we've overflowed, we cannot share any further strings: the CTF
+     format cannot encode strings with such high offsets.  */
+  if (*offset != off)
+    return NULL;
+
+  return ret;
+}
+
+/* Return symbols from the symbol table to libctf, one by one.  We assume (and
+   assert) that the symbols in the elf_link_hash_table are in strictly ascending
+   order, and that none will be added in between existing ones.  Returns NULL
+   when iteration is complete.  */
+
+static struct ctf_link_sym *
+gld_${EMULATION_NAME}_ctf_symbols_iter_cb (struct ctf_link_sym *dest,
+                                           void *arg_)
+{
+  struct ctf_strsym_iter_cb_arg *arg =
+    (struct ctf_strsym_iter_cb_arg *) arg_;
+
+  if (arg->next_i > arg->symcount)
+    {
+      arg->next_i = 0;
+      arg->next_idx = 0;
+      return NULL;
+    }
+
+  ASSERT (arg->syms[arg->next_i].dest_index == arg->next_idx);
+  dest->st_name = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i, NULL);
+  dest->st_shndx = arg->syms[arg->next_i].sym.st_shndx;
+  dest->st_type = ELF_ST_TYPE (arg->syms[arg->next_i].sym.st_info);
+  dest->st_value = arg->syms[arg->next_i].sym.st_value;
+  arg->next_i++;
+  return dest;
+}
+
+static void
+gld_${EMULATION_NAME}_examine_strtab_for_ctf
+  (ctf_file_t *ctf_output, struct elf_sym_strtab *syms,
+   bfd_size_type symcount, struct elf_strtab_hash *symstrtab)
+{   struct ctf_strsym_iter_cb_arg args = { syms, symcount, symstrtab,
+                                          0, 0 };
+   if (!ctf_output)
+     return;
+
+   if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
+       && !bfd_link_relocatable (&link_info))
+    {
+      if (ctf_link_add_strtab (ctf_output,
+                               gld_${EMULATION_NAME}_ctf_strtab_iter_cb,
+                               &args) < 0)
+        einfo (_("%F%P: warning: CTF strtab association failed; strings will "
+                 "not be shared: %s\n"),
+               ctf_errmsg (ctf_errno (ctf_output)));
+
+      if (ctf_link_shuffle_syms (ctf_output,
+                                 gld_${EMULATION_NAME}_ctf_symbols_iter_cb,
+                                 &args) < 0)
+        einfo (_("%F%P: warning: CTF symbol shuffling failed; slight space "
+                 "cost: %s\n"), ctf_errmsg (ctf_errno (ctf_output)));
+    }
+}
 EOF
+# Put these extra routines in ld${EMULATION_NAME}_emulation
+#
+LDEMUL_EMIT_CTF_EARLY=gld_${EMULATION_NAME}_emit_ctf_early
+LDEMUL_EXAMINE_STRTAB_FOR_CTF=gld_${EMULATION_NAME}_examine_strtab_for_ctf
diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
index 3e8ee4693e..7fe8d0a6c7 100644
--- a/ld/emultempl/elf32.em
+++ b/ld/emultempl/elf32.em
@@ -3069,6 +3069,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
   ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
   ${LDEMUL_NEW_VERS_PATTERN-NULL},
-  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
+  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/generic.em b/ld/emultempl/generic.em
index 5cfe5a78a5..e9ebba3be7 100644
--- a/ld/emultempl/generic.em
+++ b/ld/emultempl/generic.em
@@ -157,6 +157,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   ${LDEMUL_RECOGNIZED_FILE-NULL},
   ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
   ${LDEMUL_NEW_VERS_PATTERN-NULL},
-  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
+  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/linux.em b/ld/emultempl/linux.em
index 5a01155984..fdd7bbd42b 100644
--- a/ld/emultempl/linux.em
+++ b/ld/emultempl/linux.em
@@ -209,6 +209,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/msp430.em b/ld/emultempl/msp430.em
index 765a9ea288..6cde975f5e 100644
--- a/ld/emultempl/msp430.em
+++ b/ld/emultempl/msp430.em
@@ -842,7 +842,9 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   ${LDEMUL_RECOGNIZED_FILE-NULL},
   ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
   ${LDEMUL_NEW_VERS_PATTERN-NULL},
-  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
+  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
 #
diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 218f98acf9..52722a6cde 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -2371,6 +2371,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   gld_${EMULATION_NAME}_recognized_file,
   gld_${EMULATION_NAME}_find_potential_libraries,
   NULL, /* new_vers_pattern.  */
-  NULL /* extra_map_file_text.  */
+  NULL, /* extra_map_file_text.  */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index 649efaf92c..486f3d471d 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -2168,6 +2168,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   gld_${EMULATION_NAME}_recognized_file,
   gld_${EMULATION_NAME}_find_potential_libraries,
   NULL, /* new_vers_pattern.  */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/ticoff.em b/ld/emultempl/ticoff.em
index 65b03b668f..3e9056f5fb 100644
--- a/ld/emultempl/ticoff.em
+++ b/ld/emultempl/ticoff.em
@@ -182,6 +182,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL  /* extra_map_file_text */
+  NULL,  /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/vanilla.em b/ld/emultempl/vanilla.em
index c57950ab6b..f3654fda2d 100644
--- a/ld/emultempl/vanilla.em
+++ b/ld/emultempl/vanilla.em
@@ -83,6 +83,8 @@ struct ld_emulation_xfer_struct ld_vanilla_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  NULL, /* emit_ctf_early */
+  NULL  /* examine_strtab_for_ctf */
 };
 EOF
diff --git a/ld/ldemul.c b/ld/ldemul.c
index 16ddb6dcf5..b9dd007a85 100644
--- a/ld/ldemul.c
+++ b/ld/ldemul.c
@@ -371,3 +371,25 @@ ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf)
   if (ld_emulation->extra_map_file_text)
     ld_emulation->extra_map_file_text (abfd, info, mapf);
 }
+
+int
+ldemul_emit_ctf_early (void)
+{
+  if (ld_emulation->emit_ctf_early)
+    return ld_emulation->emit_ctf_early ();
+  /* If the emulation doesn't know if it wants to emit CTF early, it is going
+     to do so.  */
+  return 1;
+}
+
+void
+ldemul_examine_strtab_for_ctf (ctf_file_t *ctf_output,
+                               struct elf_sym_strtab *syms,
+                               bfd_size_type symcount,
+                               struct elf_strtab_hash *symstrtab)
+
+{
+  if (ld_emulation->examine_strtab_for_ctf)
+    ld_emulation->examine_strtab_for_ctf (ctf_output, syms,
+                                          symcount, symstrtab);
+}
diff --git a/ld/ldemul.h b/ld/ldemul.h
index 5b6549f837..1de29d0584 100644
--- a/ld/ldemul.h
+++ b/ld/ldemul.h
@@ -100,6 +100,14 @@ extern struct bfd_elf_version_expr *ldemul_new_vers_pattern
   (struct bfd_elf_version_expr *);
 extern void ldemul_extra_map_file_text
   (bfd *, struct bfd_link_info *, FILE *);
+/* Return 1 if we are emitting CTF early, and 0 if ldemul_examine_strtab_for_ctf
+   will be called by the target.  */
+extern int ldemul_emit_ctf_early
+  (void);
+/* Called from per-target code to examine the strtab and symtab.  */
+extern void ldemul_examine_strtab_for_ctf
+  (ctf_file_t *, struct elf_sym_strtab *, bfd_size_type,
+   struct elf_strtab_hash *);
 
 typedef struct ld_emulation_xfer_struct {
   /* Run before parsing the command line and script file.
@@ -208,6 +216,19 @@ typedef struct ld_emulation_xfer_struct {
   void (*extra_map_file_text)
     (bfd *, struct bfd_link_info *, FILE *);
 
+  /* If this returns true, we emit CTF as early as possible: if false, we emit
+     CTF once the strtab and symtab are laid out.  */
+  int (*emit_ctf_early)
+    (void);
+
+  /* Called to examine the string and symbol table late enough in linking that
+     they are finally laid out.  If emit_ctf_early returns true, this is not
+     called and ldemul_maybe_emit_ctf() emits CTF in 'early' mode: otherwise, it
+     waits until 'late'. (Late mode needs explicit support at per-target link
+     time to get called at all).  If set, called by ld when the examine_strtab
+     bfd_link_callback is invoked by per-target code.  */
+  void (*examine_strtab_for_ctf) (ctf_file_t *, struct elf_sym_strtab *,
+                                  bfd_size_type, struct elf_strtab_hash *);
 } ld_emulation_xfer_type;
 
 typedef enum {
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 466c7f0748..3b88e6df53 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -3666,112 +3666,21 @@ lang_merge_ctf (void)
     }
 }
 
-/* Callbacks used to map from bfd types to libctf types, under libctf's
-   control.  */
-
-struct ldlang_ctf_strsym_iter_cb_arg
-{
-  struct elf_sym_strtab *syms;
-  bfd_size_type symcount;
-  struct elf_strtab_hash *symstrtab;
-  size_t next_i;
-  size_t next_idx;
-};
-
-/* Return strings from the strtab to libctf, one by one.  Returns NULL when
-   iteration is complete.  */
-
-static const char *
-ldlang_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
-{
-  bfd_size_type off;
-  const char *ret;
-
-  struct ldlang_ctf_strsym_iter_cb_arg *arg =
-    (struct ldlang_ctf_strsym_iter_cb_arg *) arg_;
-
-  /* There is no zeroth string.  */
-  if (arg->next_i == 0)
-    arg->next_i = 1;
-
-  if (arg->next_i >= _bfd_elf_strtab_len (arg->symstrtab))
-    {
-      arg->next_i = 0;
-      return NULL;
-    }
-
-  ret = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i++, &off);
-  *offset = off;
-
-  /* If we've overflowed, we cannot share any further strings: the CTF
-     format cannot encode strings with such high offsets.  */
-  if (*offset != off)
-    return NULL;
-
-  return ret;
-}
-
-/* Return symbols from the symbol table to libctf, one by one.  We assume (and
-   assert) that the symbols in the elf_link_hash_table are in strictly ascending
-   order, and that none will be added in between existing ones.  Returns NULL
-   when iteration is complete.  */
-
-static struct ctf_link_sym *
-ldlang_ctf_symbols_iter_cb (struct ctf_link_sym *dest, void *arg_)
-{
-  struct ldlang_ctf_strsym_iter_cb_arg *arg =
-    (struct ldlang_ctf_strsym_iter_cb_arg *) arg_;
-
-  if (arg->next_i > arg->symcount)
-    {
-      arg->next_i = 0;
-      arg->next_idx = 0;
-      return NULL;
-    }
-
-  ASSERT (arg->syms[arg->next_i].dest_index == arg->next_idx);
-  dest->st_name = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i, NULL);
-  dest->st_shndx = arg->syms[arg->next_i].sym.st_shndx;
-  dest->st_type = ELF_ST_TYPE (arg->syms[arg->next_i].sym.st_info);
-  dest->st_value = arg->syms[arg->next_i].sym.st_value;
-  arg->next_i++;
-  return dest;
-}
-
-/* Suck in the final string table for deduplication, and reshuffle the output
-   CTF file in accordance with the final symbol indexes.  We only do this on the
-   last, nonrelocatable link, since only at that time is the symbol table
-   finalized and unchanging.  */
+/* Let the emulation examine the symbol table and strtab to help it optimize the
+   CTF, if supported.  */
 
 void
 ldlang_ctf_apply_strsym (struct elf_sym_strtab *syms, bfd_size_type symcount,
                          struct elf_strtab_hash *symstrtab)
 {
-  struct ldlang_ctf_strsym_iter_cb_arg args = { syms, symcount, symstrtab,
-                                                0, 0 };
-
-  if (!ctf_output)
-    return;
-
-  if (!bfd_link_relocatable (&link_info))
-    {
-      if (ctf_link_add_strtab (ctf_output, ldlang_ctf_strtab_iter_cb,
-                               &args) < 0)
-        einfo (_("%F%P: warning: CTF strtab association failed; strings will "
-                 "not be shared: %s\n"),
-               ctf_errmsg (ctf_errno (ctf_output)));
-
-      if (ctf_link_shuffle_syms (ctf_output, ldlang_ctf_symbols_iter_cb,
-                                 &args) < 0)
-        einfo (_("%F%P: warning: CTF symbol shuffling failed; slight space "
-                 "cost: %s\n"), ctf_errmsg (ctf_errno (ctf_output)));
-    }
+  ldemul_examine_strtab_for_ctf (ctf_output, syms, symcount, symstrtab);
 }
 
-/* Write out the CTF section.  */
-
-void
-ldlang_write_ctf (void)
+/* Write out the CTF section.  Called early, if the emulation isn't going to
+   need to dedup against the strtab and symtab, then possibly called from the
+   target linker code if the dedup has happened.  */
+static void
+lang_write_ctf (int late)
 {
   size_t output_size;
   asection *output_sect;
@@ -3779,6 +3688,20 @@ ldlang_write_ctf (void)
   if (!ctf_output)
     return;
 
+  if (!late)
+    {
+      /* Emit CTF early if this emulation says it needs to.  */
+      if (ldemul_emit_ctf_early ())
+        return;
+    }
+  else
+    {
+      if (!ldemul_emit_ctf_early ())
+        return;
+    }
+
+  /* Emit CTF.  */
+
   /* 4096 below is an arbitrary size above which we want CTF files to be
      compressed.  TODO: maybe this should be configurable?  */
   output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
@@ -3796,8 +3719,18 @@ ldlang_write_ctf (void)
       output_sect->flags |= SEC_EXCLUDE;
     }
   ctf_file_close (ctf_output);
+  ctf_output = NULL;
   /* TODO: what about the input files' CTF sections?  Should we free them?  */
 }
+/* Write out the CTF section late, if the emulation needs that.  */
+
+void
+ldlang_write_ctf_late (void)
+{
+  /* Trigger a "late call", if the emulation needs one.  */
+
+  lang_write_ctf (1);
+}
 
 /* Add the supplied name to the symbol table as an undefined reference.
    This is a two step process as the symbol table doesn't even exist at
@@ -7807,6 +7740,10 @@ lang_process (void)
      function and data object sections need adjustment.  */
   lang_merge_ctf ();
 
+  /* Emit the CTF, iff the emulation doesn't need to do late emission after
+     examining things laid out late, like the strtab.  */
+  lang_write_ctf (0);
+
   /* Copy forward lma regions for output sections in same lma region.  */
   lang_propagate_lma_regions ();
 
diff --git a/ld/ldlang.h b/ld/ldlang.h
index 00170f2c9a..c53d5806e7 100644
--- a/ld/ldlang.h
+++ b/ld/ldlang.h
@@ -699,7 +699,7 @@ extern bfd_boolean load_symbols
 
 extern void ldlang_ctf_apply_strsym
   (struct elf_sym_strtab *, bfd_size_type, struct elf_strtab_hash *);
-extern void ldlang_write_ctf
+extern void ldlang_write_ctf_late
   (void);
 extern bfd_boolean
 ldlang_override_segment_assignment
diff --git a/ld/ldmain.c b/ld/ldmain.c
index e62f92fbb1..477910073e 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -150,7 +150,7 @@ static struct bfd_link_callbacks link_callbacks =
   minfo,
   ldlang_override_segment_assignment,
   ldlang_ctf_apply_strsym,
-  ldlang_write_ctf
+  ldlang_write_ctf_late
 };
 
 static bfd_assert_handler_type default_bfd_assert_handler;
--
2.22.0.238.g049a27acdc

Reply | Threaded
Open this post in threaded view
|

[PATCH v2 19/19 REVIEW RFC] bfd, ld: add CTF section linking

Nick Alcock
In reply to this post by Nick Alcock
This is quite complicated because the CTF section's contents depend on
the final contents of the symtab and strtab, because it has two sections
whose contents are shuffled to be in 1:1 correspondence with the symtab,
and an internal strtab that gets deduplicated against the ELF strtab
(with offsets adjusted to point into the ELF strtab instead).  It is
also compressed if large enough, so its size depends on its contents!

So we cannot construct it as early as most sections: we cannot even
*begin* construction until after the symtab and strtab are finalized.
Thankfully there is already one section treated similarly: compressed
debugging sections: the only differences are that compressed debugging
sections have extra handling to deal with their changing name if
compressed (CTF sections are always called ".ctf" for now, though we
have reserved ".ctf.*" against future use), and that compressed
debugging sections have previously-uncompressed content which has to be
stashed away for later compression, while CTF sections have no content
at all until we generate it (very late).

BFD also cannot do the link itself: libctf knows how to do it, and BFD
cannot call libctf directly because libctf already depends on bfd for
file I/O.  So we have to use a pair of callbacks, one, examine_strtab,
which allows a caller to examine the symtab and strtab after
finalization (called from elf_link_swap_symbols_out(), right before the
symtabs are written, and after the strtab has been finalized), and one
which actually does the emission (called emit_ctf simply because it is
grouped with a bunch of section-specific late-emission function calls at
the bottom of bfd_elf_final_link, and a section-specific name seems best
for that).  emit_ctf is actually called *twice*: once from lang_process
if the emulation suggests that this bfd target does not examine the
symtab or strtab, and once via a bfd callback if it does.  (This means
that non-ELF targets still get CTF emitted, even though the late CTF
emission stage is never called for them).

Caveats and things I want help with / careful review of, oh are there a
lot of them! I struggled with this code for ages and this is not
particularly pretty: it's just the first thing I hit on that happened to
work.

 - There are no SEC_* flags left, and we do not have a SHT_ type for CTF
   sections yet -- so we are reduced to doing string comparisons to
   determine whether we have to do special things for the CTF section.
   Nearly all of these things apply to any such "late-generated
   section", but in the absence of any remaining SEC_ flags I cannot say
   that.  Does anyone know if any of these flags are unused, or if we
   can make the flags word larger, or something?  I have defined a macro,
   SECTION_IS_CTF, to do the ugly string comparisons for now.

 - The naming of functions in ldlang.c is opaque: some are ldlang_* and
   some are lang_*.  I've split the difference so things are named
   similarly to the functions called near them in lang_process, but this
   feels... wrong.  Is there any consistency here?

 - There is an arbitrary threshold value above which CTF sections are
   compressed (roughly: it is actually the threshold above which
   ctf_file_t's are compressed, and a CTF section may be comprised of
   many of these in a ctf_archive_t).  Right now I have arbitrarily
   hardwired this threshold at 4096 bytes.  Should it be configurable?
   Is a somewhat bigger value saner, on the grounds that wasting space
   on compression dictionaries for 4KiB files is just nuts? (64KiB is
   probably too big...)

 - We might well have memory leaks: we open the CTF sections for the
   input files and then never close them again.  Should we? ld does seem
   to operate on the basis that input files live forever so it doesn't
   matter if they are never freed...

 - Is the whole "set the length of the input CTF sections to zero to force
   their contents to not get copied, then forcibly emit them and jam their
   sizes in in emit_ctf/ldlang_write_ctf/
   _bfd_elf_assign_file_positions_for_non_load" thing too ugly to live?  I'm
   fairly happy with turning on SEC_NEVER_LOAD in the code rather than in
   the linker script simply because loading the thing via the ELF loader is
   almost certainly always a mistake (we already have functions to load CTF,
   and it's not done that way).  But is that *also* too ugly to live?  I
   don't know.

 - I'm fairly unhappy that I have to modify the default linker script,
   because that means that any project wanting CTF support and providing its
   own linker script will have to change -- including the Linux kernel, a
   major existing user (albeit not upstream yet, so that isn't so much of a
   problem: I can just make that change myself).  But without doing this, we
   never get an input->output mapping and the CTF sections are simply never
   emitted.  This seems to be because non-loaded sections are simply thrown
   away in the elf32.em orphan-assignment code: but even if it might be
   desirable, we *cannot* make this a loaded section, since that would
   require its size and position to be computed before the strtab is laid
   out, while the strtab dedup, symtab shuffling, and compression requires us
   to compute it afterwards.  So we could avoid modifying the linker script
   by modifying the ELF orphan-assignment code, I suppose, but I have no idea
   which might be preferable.  The linker script modification is certainly
   simpler.

v2: merged with non-ELF support patch: slight commit message adjustments.

        * Makefile.def (dependencies): all-ld depends on all-libctf.
        * Makefile.in: Regenerated.

include/
        * bfdlink.h (elf_strtab_hash): New forward.
        (elf_sym_strtab): Likewise.
        (struct bfd_link_callbacks <examine_strtab>): New.
        (struct bfd_link_callbacks <emit_ctf>): Likewise.

bfd/
        * elf-bfd.h (SECTION_IS_CTF): New macro.
        * elf.c (special_sections_c): Add ".ctf".
        (assign_file_positions_for_non_load_sections): Note that
        compressed debugging sections etc are not assigned here.
        Treat CTF sections like SEC_ELF_COMPRESS sections: sh_offset -1.
        (assign_file_positions_except_relocs): Likewise.
        (find_section_in_list): Note that debugging and CTF sections, as
        well as reloc sections, are assigned later.
        (_bfd_elf_assign_file_positions_for_non_load): CTF sections get
        their size and contents updated.
        (_bfd_elf_set_section_contents): Skip CTF sections: unlike
        compressed sections, they have no uncompressed content to copy at
        this stage.
        * elflink.c (elf_link_swap_symbols_out): Call the examine_strtab
        callback right before the strtab is written out.
        (bfd_elf_final_link): Don't cache the section contents of CTF
        sections: they are not populated yet.  Call the emit_ctf callback
        right at the end, after all the symbols and strings are flushed
        out.
ld/
        * scripttempl/DWARF.sc: Add .ctf.
        * ldlang.h (includes): Add elf-bfd.h, ctf-api.h.  Prevent NAME in
        elf-bfd.h from wreaking havoc.
        (struct lang_input_statement_struct): Add the_ctf.
        (ldlang_ctf_apply_strsym): Declare.
        (ldlang_write_ctf_late): Likewise.
        * ldemul.h (ldemul_emit_ctf_early): New.
        (ldemul_examine_strtab_for_ctf): Likewise.
        (ld_emulation_xfer_type) <emit_ctf_early>: Likewise.
        (ld_emulation_xfer_type) <examine_strtab_for_ctf>: Likewise.
        * ldemul.c (ldemul_emit_ctf_early): New.
        (ldemul_examine_strtab_for_ctf): Likewise.
        * ldlang.c (includes): elf-bfd.h is now included by ldlang.h.
        Include elf/internal.h.
        * ldmain.c (link_callbacks): Add ldlang_ctf_apply_strsym,
        ldlang_write_ctf_late.
        (ctf_output): New. Initialized in...
        (ldlang_open_ctf): ... this new function.  Open all the CTF
        sections in the input files: mark them non-loaded and empty
        so as not to copy their contents to the output.
        (ldlang_merge_ctf): New, merge types via ctf_link_add_ctf and
        ctf_link.
        (ldlang_ctf_apply_strsym): New, an examine_strtab callback: wrap
        ldemul_examine_strtab_for_ctf.
        (lang_write_ctf): New, write out the CTF section.
        (ldlang_write_ctf_late): New, late call via bfd's emit_ctf hook.
        (lang_process): Call ldlang_open_ctf, ldlang_merge_ctf, and
        lang_write_ctf.

        * emultempl/elf-generic.em
        (gld_${EMULATION_NAME}_emit_ctf_early): New.
        (gld_${EMULATION_NAME}_examine_strtab_for_ctf): tell libctf about
        the symtab and strtab.
        (struct ctf_strsym_iter_cb_arg): New, state to do so.
        (gld_${EMULATION_NAME}_ctf_strtab_iter_cb): New: tell libctf about
        each string in the strtab in turn.
        (gld_${EMULATION_NAME}_ctf_symbols_iter_cb): New, tell libctf
        about each symbol in the symtab in turn.
        (LDEMUL_EMIT_CTF_EARLY): Set it.
        (LDEMUL_EXAMINE_STRTAB_FOR_CTF): Likewise.
        * emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Add
        emit_ctf_early and examine_strtab_for_ctf, NULL by default.
        * emultempl/armcoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/beos.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/elf32.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/generic.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/linux.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/msp430.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/pe.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/pep.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/ticoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
        * emultempl/vanilla.em (ld_${EMULATION_NAME}_emulation): Likewise.

        * Makefile.am: Pull in libctf (and zlib, a transitive requirement
        for compressed CTF section emission).  Pass it on to DejaGNU.
        * configure.ac: Add AM_ZLIB.
        * aclocal.m4: Added zlib.m4.
        * Makefile.in: Regenerated.
        * testsuite/ld-bootstrap/bootstrap.exp: Use it when relinking ld.
---
 ChangeLog                               |   5 +
 Makefile.def                            |   1 +
 Makefile.in                             |   7 +
 bfd/elf-bfd.h                           |   6 +-
 bfd/elf.c                               |  45 +++++--
 bfd/elflink.c                           |  55 +++++---
 include/bfdlink.h                       |  15 +++
 ld/Makefile.am                          |  15 ++-
 ld/Makefile.in                          |  23 +++-
 ld/aclocal.m4                           |   1 +
 ld/configure                            |  28 +++-
 ld/configure.ac                         |   4 +
 ld/emultempl/aix.em                     |   4 +-
 ld/emultempl/armcoff.em                 |   4 +-
 ld/emultempl/beos.em                    |   4 +-
 ld/emultempl/elf-generic.em             | 115 ++++++++++++++++
 ld/emultempl/elf32.em                   |   4 +-
 ld/emultempl/generic.em                 |   4 +-
 ld/emultempl/linux.em                   |   4 +-
 ld/emultempl/msp430.em                  |   4 +-
 ld/emultempl/pe.em                      |   4 +-
 ld/emultempl/pep.em                     |   4 +-
 ld/emultempl/ticoff.em                  |   4 +-
 ld/emultempl/vanilla.em                 |   4 +-
 ld/ldemul.c                             |  22 +++
 ld/ldemul.h                             |  21 +++
 ld/ldlang.c                             | 169 +++++++++++++++++++++++-
 ld/ldlang.h                             |  13 ++
 ld/ldmain.c                             |   4 +-
 ld/scripttempl/DWARF.sc                 |   3 +
 ld/testsuite/ld-bootstrap/bootstrap.exp |   8 +-
 31 files changed, 540 insertions(+), 64 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 52d97d5fb8..8569bcf5ef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2019-07-13  Nick Alcock  <[hidden email]>
+
+ * Makefile.def (dependencies): all-ld depends on all-libctf.
+ * Makefile.in: Regenerated.
+
 2019-07-13  Joel Brobecker  <[hidden email]>
 
  * src-release (getver): If $tool/gdbsupport/create-version.sh
diff --git a/Makefile.def b/Makefile.def
index 28bf61d771..e887f498f4 100644
--- a/Makefile.def
+++ b/Makefile.def
@@ -432,6 +432,7 @@ dependencies = { module=all-binutils; on=all-build-bison; };
 dependencies = { module=all-binutils; on=all-intl; };
 dependencies = { module=all-binutils; on=all-gas; };
 dependencies = { module=all-binutils; on=all-libctf; };
+dependencies = { module=all-ld; on=all-libctf; };
 
 // We put install-opcodes before install-binutils because the installed
 // binutils might be on PATH, and they might need the shared opcodes
diff --git a/Makefile.in b/Makefile.in
index 7a6700af96..eeba51e829 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -51159,6 +51159,13 @@ all-stage3-binutils: maybe-all-stage3-libctf
 all-stage4-binutils: maybe-all-stage4-libctf
 all-stageprofile-binutils: maybe-all-stageprofile-libctf
 all-stagefeedback-binutils: maybe-all-stagefeedback-libctf
+all-ld: maybe-all-libctf
+all-stage1-ld: maybe-all-stage1-libctf
+all-stage2-ld: maybe-all-stage2-libctf
+all-stage3-ld: maybe-all-stage3-libctf
+all-stage4-ld: maybe-all-stage4-libctf
+all-stageprofile-ld: maybe-all-stageprofile-libctf
+all-stagefeedback-ld: maybe-all-stagefeedback-libctf
 install-binutils: maybe-install-opcodes
 install-strip-binutils: maybe-install-strip-opcodes
 install-opcodes: maybe-install-bfd
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 26f35a0f8a..823e83eafb 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2242,7 +2242,7 @@ extern bfd_size_type _bfd_elf_strtab_len
 extern bfd_size_type _bfd_elf_strtab_offset
   (struct elf_strtab_hash *, size_t);
 extern const char * _bfd_elf_strtab_str
-  (struct elf_strtab_hash *, size_t idx, size_t *offset);
+  (struct elf_strtab_hash *, size_t idx, bfd_size_type *offset);
 extern bfd_boolean _bfd_elf_strtab_emit
   (bfd *, struct elf_strtab_hash *);
 extern void _bfd_elf_strtab_finalize
@@ -2901,6 +2901,10 @@ extern asection _bfd_elf_large_com_section;
  || (H)->start_stop \
  || ((INFO)->dynamic && !(H)->dynamic)))
 
+/* Determine if a section contains CTF data, using its name.  */
+#define SECTION_IS_CTF(name) \
+ (strcmp ((name), ".ctf") == 0 || strncmp ((name), ".ctf.", 5) == 0)
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/bfd/elf.c b/bfd/elf.c
index 1c843327cf..f78a3a7fc4 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -2619,6 +2619,7 @@ static const struct bfd_elf_special_section special_sections_b[] =
 static const struct bfd_elf_special_section special_sections_c[] =
 {
   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
+  { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS,    0 },
   { NULL, 0, 0, 0,    0 }
 };
 
@@ -5839,7 +5840,8 @@ is_debuginfo_file (bfd *abfd)
   return TRUE;
 }
 
-/* Assign file positions for the other sections.  */
+/* Assign file positions for the other sections, except for compressed debugging
+   and other sections assigned in _bfd_elf_assign_file_positions_for_non_load().  */
 
 static bfd_boolean
 assign_file_positions_for_non_load_sections (bfd *abfd,
@@ -5899,8 +5901,10 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
  && hdr->bfd_section == NULL)
        || (hdr->bfd_section != NULL
-   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
-   /* Compress DWARF debug sections.  */
+   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
+                       || SECTION_IS_CTF (hdr->bfd_section->name)))
+   /* We don't know the offset of these sections yet: their size
+                      has not been decided.  */
        || hdr == i_shdrpp[elf_onesymtab (abfd)]
        || (elf_symtab_shndx_list (abfd) != NULL
    && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
@@ -6168,11 +6172,12 @@ find_section_in_list (unsigned int i, elf_section_list * list)
    VMAs must be known before this is called.
 
    Reloc sections come in two flavours: Those processed specially as
-   "side-channel" data attached to a section to which they apply, and
-   those that bfd doesn't process as relocations.  The latter sort are
-   stored in a normal bfd section by bfd_section_from_shdr.   We don't
-   consider the former sort here, unless they form part of the loadable
-   image.  Reloc sections not assigned here will be handled later by
+   "side-channel" data attached to a section to which they apply, and those that
+   bfd doesn't process as relocations.  The latter sort are stored in a normal
+   bfd section by bfd_section_from_shdr.  We don't consider the former sort
+   here, unless they form part of the loadable image.  Reloc sections not
+   assigned here (and compressed debugging sections and CTF sections which
+   nothing else in the file can rely upon) will be handled later by
    assign_file_positions_for_relocs.
 
    We also don't set the positions of the .symtab and .strtab here.  */
@@ -6208,8 +6213,10 @@ assign_file_positions_except_relocs (bfd *abfd,
   if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
        && hdr->bfd_section == NULL)
       || (hdr->bfd_section != NULL
-  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
-  /* Compress DWARF debug sections.  */
+                  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
+                       || SECTION_IS_CTF (hdr->bfd_section->name)))
+              /* Do not assign offsets for these sections yet: we don't know
+                 their sizes.  */
       || i == elf_onesymtab (abfd)
       || (elf_symtab_shndx_list (abfd) != NULL
   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
@@ -6417,12 +6424,14 @@ _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   asection *sec = shdrp->bfd_section;
   bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
  || shdrp->sh_type == SHT_RELA);
+          bfd_boolean is_ctf = sec && SECTION_IS_CTF (sec->name);
   if (is_rel
+              || is_ctf
       || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
     {
-      if (!is_rel)
+      if (!is_rel && !is_ctf)
  {
-  const char *name = sec->name;
+                  const char *name = sec->name;
   struct bfd_elf_section_data *d;
 
   /* Compress DWARF debug sections.  */
@@ -6466,6 +6475,13 @@ _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   shdrp->contents = sec->contents;
   shdrp->bfd_section->contents = NULL;
  }
+              else if (is_ctf)
+                {
+  /* Update section size and contents.  */
+  shdrp->sh_size = sec->size;
+  shdrp->contents = sec->contents;
+                }
+
       off = _bfd_elf_assign_file_position_for_section (shdrp,
        off,
        TRUE);
@@ -9045,6 +9061,11 @@ _bfd_elf_set_section_contents (bfd *abfd,
   hdr = &elf_section_data (section)->this_hdr;
   if (hdr->sh_offset == (file_ptr) -1)
     {
+      if (SECTION_IS_CTF (section->name))
+        /* Nothing to do with this section: the contents are generated
+           later.  */
+        return TRUE;
+
       /* We must compress this section.  Write output to the buffer.  */
       unsigned char *contents = hdr->contents;
       if ((offset + count) > hdr->sh_size
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 9175d3fa20..258c2408bb 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -9507,6 +9507,15 @@ elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
  + elfsym->destshndx_index));
     }
 
+  /* Allow the linker to examine the strtab and symtab now they are
+     populated.  */
+
+  if (flinfo->info->callbacks->examine_strtab)
+    flinfo->info->callbacks->examine_strtab (hash_table->strtab,
+                                             hash_table->strtabcount,
+                                             flinfo->symstrtab);
+
+
   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
   pos = hdr->sh_offset + hdr->sh_size;
   amt = hash_table->strtabcount * bed->s->sizeof_sym;
@@ -11779,7 +11788,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
 
   /* The object attributes have been merged.  Remove the input
      sections from the link, and set the contents of the output
-     secton.  */
+     section.  */
   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
   for (o = abfd->sections; o != NULL; o = o->next)
     {
@@ -12001,26 +12010,27 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
       esdo->rel.count = 0;
       esdo->rela.count = 0;
 
-      if (esdo->this_hdr.sh_offset == (file_ptr) -1)
- {
-  /* Cache the section contents so that they can be compressed
-     later.  Use bfd_malloc since it will be freed by
-     bfd_compress_section_contents.  */
-  unsigned char *contents = esdo->this_hdr.contents;
-  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
-    abort ();
-  contents
-    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
-  if (contents == NULL)
-    goto error_return;
-  esdo->this_hdr.contents = contents;
- }
-    }
-
-  /* We have now assigned file positions for all the sections except
-     .symtab, .strtab, and non-loaded reloc sections.  We start the
-     .symtab section at the current file position, and write directly
-     to it.  We build the .strtab section in memory.  */
+      if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
+          && !SECTION_IS_CTF (o->name))
+        {
+          /* Cache the section contents so that they can be compressed
+             later.  Use bfd_malloc since it will be freed by
+             bfd_compress_section_contents.  */
+          unsigned char *contents = esdo->this_hdr.contents;
+          if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
+            abort ();
+          contents
+            = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
+          if (contents == NULL)
+            goto error_return;
+          esdo->this_hdr.contents = contents;
+        }
+    }
+
+  /* We have now assigned file positions for all the sections except .symtab,
+     .strtab, and non-loaded reloc and compressed debugging sections.  We start
+     the .symtab section at the current file position, and write directly to it.
+     We build the .strtab section in memory.  */
   bfd_get_symcount (abfd) = 0;
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   /* sh_name is set in prep_headers.  */
@@ -12806,6 +12816,9 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
     goto error_return;
 
+  if (info->callbacks->emit_ctf)
+      info->callbacks->emit_ctf ();
+
   elf_final_link_free (abfd, &flinfo);
 
   elf_linker (abfd) = TRUE;
diff --git a/include/bfdlink.h b/include/bfdlink.h
index c35469dd11..ca24ae7fbf 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -630,6 +630,11 @@ struct bfd_link_info
   struct bfd_elf_version_tree *version_info;
 };
 
+/* Some forward-definitions used by some callbacks.  */
+
+struct elf_strtab_hash;
+struct elf_sym_strtab;
+
 /* This structures holds a set of callback functions.  These are called
    by the BFD linker routines.  */
 
@@ -751,6 +756,16 @@ struct bfd_link_callbacks
     (struct bfd_link_info *, bfd * abfd,
      asection * current_section, asection * previous_section,
      bfd_boolean new_segment);
+  /* This callback provides a chance for callers of the BFD to examine the
+     ELF string table and symbol table once they are complete and indexes and
+     offsets assigned.  */
+  void (*examine_strtab)
+    (struct elf_sym_strtab *syms, bfd_size_type symcount,
+     struct elf_strtab_hash *symstrtab);
+  /* This callback should emit the CTF section into a non-loadable section in
+     the output BFD named .ctf or a name beginning with ".ctf.".  */
+  void (*emit_ctf)
+    (void);
 };
 
 /* The linker builds link_order structures which tell the code how to
diff --git a/ld/Makefile.am b/ld/Makefile.am
index 0509c2e50f..6b2453a240 100644
--- a/ld/Makefile.am
+++ b/ld/Makefile.am
@@ -34,6 +34,12 @@ LEX = `if [ -f ../flex/flex ]; then echo ../flex/flex; else echo @LEX@; fi`
 am__skiplex =
 am__skipyacc =
 
+# This is where we get zlib from.  zlibdir is -L../zlib and zlibinc is
+# -I../zlib, unless we were configured with --with-system-zlib, in which
+# case both are empty.
+ZLIB = @zlibdir@ -lz
+ZLIBINC = @zlibinc@
+
 ELF_CLFAGS=-DELF_LIST_OPTIONS=@elf_list_options@ \
    -DELF_SHLIB_LIST_OPTIONS=@elf_shlib_list_options@ \
    -DELF_PLT_UNWIND_LIST_OPTIONS=@elf_plt_unwind_list_options@
@@ -145,12 +151,13 @@ AM_MAKEINFOFLAGS = -I $(srcdir) -I $(BFDDIR)/doc -I ../bfd/doc \
 TEXI2DVI = texi2dvi -I $(srcdir) -I $(BFDDIR)/doc -I ../bfd/doc \
     -I $(top_srcdir)/../libiberty
 
-AM_CPPFLAGS = -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) \
+AM_CPPFLAGS = -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) @zlibinc@ \
  @INCINTL@ $(HDEFINES) $(CFLAGS) $(PLUGIN_CFLAGS) \
  -DLOCALEDIR="\"$(datadir)/locale\""
 
 BFDLIB = ../bfd/libbfd.la
 LIBIBERTY = ../libiberty/libiberty.a
+LIBCTF = ../libctf/libctf.a
 
 # These all start with e so 'make clean' can find them.
 ALL_EMULATION_SOURCES = \
@@ -957,8 +964,8 @@ ld_new_SOURCES = ldgram.y ldlex-wrapper.c lexsup.c ldlang.c mri.c ldctor.c ldmai
  ldwrite.c ldexp.c ldemul.c ldver.c ldmisc.c ldfile.c ldcref.c $(PLUGIN_C) \
  ldbuildid.c
 ld_new_DEPENDENCIES = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) \
-      $(BFDLIB) $(LIBIBERTY) $(LIBINTL_DEP)
-ld_new_LDADD = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) $(BFDLIB) $(LIBIBERTY) $(LIBINTL)
+      $(BFDLIB) $(LIBCTF) $(LIBIBERTY) $(LIBINTL_DEP)
+ld_new_LDADD = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) $(BFDLIB) $(LIBCTF) $(LIBIBERTY) $(LIBINTL) $(ZLIB)
 
 # Dependency tracking for the generated emulation files.
 EXTRA_ld_new_SOURCES += $(ALL_EMULATION_SOURCES) $(ALL_64_EMULATION_SOURCES)
@@ -977,7 +984,7 @@ check-DEJAGNU: site.exp
  CC="$(CC_FOR_TARGET)" CFLAGS="$(CFLAGS_FOR_TARGET)" \
  CXX="$(CXX_FOR_TARGET)" CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
  CC_FOR_HOST="$(CC)" CFLAGS_FOR_HOST="$(CFLAGS)" \
- OFILES="$(OFILES)" BFDLIB="$(TESTBFDLIB)" \
+ OFILES="$(OFILES)" BFDLIB="$(TESTBFDLIB)" LIBCTF="$(LIBCTF) $(ZLIB)" \
  LIBIBERTY="$(LIBIBERTY) $(LIBINTL)" LIBS="$(LIBS)" \
  DO_COMPARE="`echo '$(do_compare)' | sed -e 's,\\$$,,g'`" \
  $(RUNTESTFLAGS); \
diff --git a/ld/Makefile.in b/ld/Makefile.in
index 9898392a77..6e7212dbf9 100644
--- a/ld/Makefile.in
+++ b/ld/Makefile.in
@@ -122,9 +122,9 @@ am__aclocal_m4_deps = $(top_srcdir)/../bfd/acinclude.m4 \
  $(top_srcdir)/../config/plugins.m4 \
  $(top_srcdir)/../config/po.m4 \
  $(top_srcdir)/../config/progtest.m4 \
- $(top_srcdir)/../libtool.m4 $(top_srcdir)/../ltoptions.m4 \
- $(top_srcdir)/../ltsugar.m4 $(top_srcdir)/../ltversion.m4 \
- $(top_srcdir)/../lt~obsolete.m4 \
+ $(top_srcdir)/../config/zlib.m4 $(top_srcdir)/../libtool.m4 \
+ $(top_srcdir)/../ltoptions.m4 $(top_srcdir)/../ltsugar.m4 \
+ $(top_srcdir)/../ltversion.m4 $(top_srcdir)/../lt~obsolete.m4 \
  $(top_srcdir)/../bfd/version.m4 $(top_srcdir)/configure.ac
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
  $(ACLOCAL_M4)
@@ -534,6 +534,8 @@ top_build_prefix = @top_build_prefix@
 top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
 use_sysroot = @use_sysroot@
+zlibdir = @zlibdir@
+zlibinc = @zlibinc@
 AUTOMAKE_OPTIONS = dejagnu no-texinfo.tex no-dist foreign info-in-builddir
 ACLOCAL_AMFLAGS = -I .. -I ../config -I ../bfd
 TEXINFO_TEX = ../texinfo/texinfo.tex
@@ -544,6 +546,12 @@ tooldir = $(exec_prefix)/$(target_alias)
 # maintainer mode is disabled.  Avoid this.
 am__skiplex =
 am__skipyacc =
+
+# This is where we get zlib from.  zlibdir is -L../zlib and zlibinc is
+# -I../zlib, unless we were configured with --with-system-zlib, in which
+# case both are empty.
+ZLIB = @zlibdir@ -lz
+ZLIBINC = @zlibinc@
 ELF_CLFAGS = -DELF_LIST_OPTIONS=@elf_list_options@ \
    -DELF_SHLIB_LIST_OPTIONS=@elf_shlib_list_options@ \
    -DELF_PLT_UNWIND_LIST_OPTIONS=@elf_plt_unwind_list_options@
@@ -632,12 +640,13 @@ AM_MAKEINFOFLAGS = -I $(srcdir) -I $(BFDDIR)/doc -I ../bfd/doc \
 TEXI2DVI = texi2dvi -I $(srcdir) -I $(BFDDIR)/doc -I ../bfd/doc \
     -I $(top_srcdir)/../libiberty
 
-AM_CPPFLAGS = -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) \
+AM_CPPFLAGS = -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) @zlibinc@ \
  @INCINTL@ $(HDEFINES) $(CFLAGS) $(PLUGIN_CFLAGS) \
  -DLOCALEDIR="\"$(datadir)/locale\""
 
 BFDLIB = ../bfd/libbfd.la
 LIBIBERTY = ../libiberty/libiberty.a
+LIBCTF = ../libctf/libctf.a
 
 # These all start with e so 'make clean' can find them.
 ALL_EMULATION_SOURCES = \
@@ -998,9 +1007,9 @@ ld_new_SOURCES = ldgram.y ldlex-wrapper.c lexsup.c ldlang.c mri.c ldctor.c ldmai
  ldbuildid.c
 
 ld_new_DEPENDENCIES = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) \
-      $(BFDLIB) $(LIBIBERTY) $(LIBINTL_DEP)
+      $(BFDLIB) $(LIBCTF) $(LIBIBERTY) $(LIBINTL_DEP)
 
-ld_new_LDADD = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) $(BFDLIB) $(LIBIBERTY) $(LIBINTL)
+ld_new_LDADD = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) $(BFDLIB) $(LIBCTF) $(LIBIBERTY) $(LIBINTL) $(ZLIB)
 #
 #
 # Build a dummy plugin using libtool.
@@ -2560,7 +2569,7 @@ check-DEJAGNU: site.exp
  CC="$(CC_FOR_TARGET)" CFLAGS="$(CFLAGS_FOR_TARGET)" \
  CXX="$(CXX_FOR_TARGET)" CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
  CC_FOR_HOST="$(CC)" CFLAGS_FOR_HOST="$(CFLAGS)" \
- OFILES="$(OFILES)" BFDLIB="$(TESTBFDLIB)" \
+ OFILES="$(OFILES)" BFDLIB="$(TESTBFDLIB)" LIBCTF="$(LIBCTF) $(ZLIB)" \
  LIBIBERTY="$(LIBIBERTY) $(LIBINTL)" LIBS="$(LIBS)" \
  DO_COMPARE="`echo '$(do_compare)' | sed -e 's,\\$$,,g'`" \
  $(RUNTESTFLAGS); \
diff --git a/ld/aclocal.m4 b/ld/aclocal.m4
index 4408082888..7df8bf68f1 100644
--- a/ld/aclocal.m4
+++ b/ld/aclocal.m4
@@ -1198,6 +1198,7 @@ m4_include([../config/override.m4])
 m4_include([../config/plugins.m4])
 m4_include([../config/po.m4])
 m4_include([../config/progtest.m4])
+m4_include([../config/zlib.m4])
 m4_include([../libtool.m4])
 m4_include([../ltoptions.m4])
 m4_include([../ltsugar.m4])
diff --git a/ld/configure b/ld/configure
index 3b50f5db8e..db3424fdb8 100755
--- a/ld/configure
+++ b/ld/configure
@@ -645,6 +645,8 @@ elf_plt_unwind_list_options
 elf_shlib_list_options
 elf_list_options
 STRINGIFY
+zlibinc
+zlibdir
 enable_initfini_array
 ENABLE_PLUGINS_FALSE
 ENABLE_PLUGINS_TRUE
@@ -834,6 +836,7 @@ enable_werror
 enable_build_warnings
 enable_nls
 enable_initfini_array
+with_system_zlib
 '
       ac_precious_vars='build_alias
 host_alias
@@ -1510,6 +1513,7 @@ Optional Packages:
   --with-gnu-ld           assume the C compiler uses GNU ld [default=no]
   --with-lib-path=dir1:dir2...  set default LIB_PATH
   --with-sysroot=DIR Search for usr/lib et al within DIR.
+  --with-system-zlib      use installed libz
 
 Some influential environment variables:
   CC          C compiler command
@@ -12027,7 +12031,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 12030 "configure"
+#line 12034 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -12133,7 +12137,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 12136 "configure"
+#line 12140 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -17385,6 +17389,26 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+# Link in zlib if we can.  This allows us to read and write
+# compressed CTF sections.
+
+  # Use the system's zlib library.
+  zlibdir="-L\$(top_builddir)/../zlib"
+  zlibinc="-I\$(top_srcdir)/../zlib"
+
+# Check whether --with-system-zlib was given.
+if test "${with_system_zlib+set}" = set; then :
+  withval=$with_system_zlib; if test x$with_system_zlib = xyes ; then
+    zlibdir=
+    zlibinc=
+  fi
+
+fi
+
+
+
+
+
 # When converting linker scripts into strings for use in emulation
 # files, use astring.sed if the compiler supports ANSI string
 # concatenation, or ostring.sed otherwise.  This is to support the
diff --git a/ld/configure.ac b/ld/configure.ac
index ee62d10ac5..ab65d569c3 100644
--- a/ld/configure.ac
+++ b/ld/configure.ac
@@ -293,6 +293,10 @@ BFD_BINARY_FOPEN
 
 AC_CHECK_DECLS([strstr, free, sbrk, getenv, environ])
 
+# Link in zlib if we can.  This allows us to read and write
+# compressed CTF sections.
+AM_ZLIB
+
 # When converting linker scripts into strings for use in emulation
 # files, use astring.sed if the compiler supports ANSI string
 # concatenation, or ostring.sed otherwise.  This is to support the
diff --git a/ld/emultempl/aix.em b/ld/emultempl/aix.em
index bcf959d613..6b78d168f0 100644
--- a/ld/emultempl/aix.em
+++ b/ld/emultempl/aix.em
@@ -1560,6 +1560,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = {
   NULL, /* recognized_file */
   NULL, /* find potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/armcoff.em b/ld/emultempl/armcoff.em
index 20122aa03f..51ee0259f3 100644
--- a/ld/emultempl/armcoff.em
+++ b/ld/emultempl/armcoff.em
@@ -283,6 +283,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/beos.em b/ld/emultempl/beos.em
index bf75c5450a..bcf93124bb 100644
--- a/ld/emultempl/beos.em
+++ b/ld/emultempl/beos.em
@@ -787,6 +787,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/elf-generic.em b/ld/emultempl/elf-generic.em
index ac60f99925..e8c0a5eb9d 100644
--- a/ld/emultempl/elf-generic.em
+++ b/ld/emultempl/elf-generic.em
@@ -68,4 +68,119 @@ gld${EMULATION_NAME}_map_segments (bfd_boolean need_layout)
   if (tries == 0)
     einfo (_("%F%P: looping in map_segments"));
 }
+
+/* We want to emit CTF early if and only if we are not targetting ELF with this
+   invocation.  */
+
+static int
+gld_${EMULATION_NAME}_emit_ctf_early (void)
+{
+  if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
+    return 0;
+  return 1;
+}
+
+/* Callbacks used to map from bfd types to libctf types, under libctf's
+   control.  */
+
+struct ctf_strsym_iter_cb_arg
+{
+  struct elf_sym_strtab *syms;
+  bfd_size_type symcount;
+  struct elf_strtab_hash *symstrtab;
+  size_t next_i;
+  size_t next_idx;
+};
+
+/* Return strings from the strtab to libctf, one by one.  Returns NULL when
+   iteration is complete.  */
+
+static const char *
+gld_${EMULATION_NAME}_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
+{
+  bfd_size_type off;
+  const char *ret;
+
+  struct ctf_strsym_iter_cb_arg *arg =
+    (struct ctf_strsym_iter_cb_arg *) arg_;
+
+  /* There is no zeroth string.  */
+  if (arg->next_i == 0)
+    arg->next_i = 1;
+
+  if (arg->next_i >= _bfd_elf_strtab_len (arg->symstrtab))
+    {
+      arg->next_i = 0;
+      return NULL;
+    }
+
+  ret = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i++, &off);
+  *offset = off;
+
+  /* If we've overflowed, we cannot share any further strings: the CTF
+     format cannot encode strings with such high offsets.  */
+  if (*offset != off)
+    return NULL;
+
+  return ret;
+}
+
+/* Return symbols from the symbol table to libctf, one by one.  We assume (and
+   assert) that the symbols in the elf_link_hash_table are in strictly ascending
+   order, and that none will be added in between existing ones.  Returns NULL
+   when iteration is complete.  */
+
+static struct ctf_link_sym *
+gld_${EMULATION_NAME}_ctf_symbols_iter_cb (struct ctf_link_sym *dest,
+                                           void *arg_)
+{
+  struct ctf_strsym_iter_cb_arg *arg =
+    (struct ctf_strsym_iter_cb_arg *) arg_;
+
+  if (arg->next_i > arg->symcount)
+    {
+      arg->next_i = 0;
+      arg->next_idx = 0;
+      return NULL;
+    }
+
+  ASSERT (arg->syms[arg->next_i].dest_index == arg->next_idx);
+  dest->st_name = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i, NULL);
+  dest->st_shndx = arg->syms[arg->next_i].sym.st_shndx;
+  dest->st_type = ELF_ST_TYPE (arg->syms[arg->next_i].sym.st_info);
+  dest->st_value = arg->syms[arg->next_i].sym.st_value;
+  arg->next_i++;
+  return dest;
+}
+
+static void
+gld_${EMULATION_NAME}_examine_strtab_for_ctf
+  (ctf_file_t *ctf_output, struct elf_sym_strtab *syms,
+   bfd_size_type symcount, struct elf_strtab_hash *symstrtab)
+{   struct ctf_strsym_iter_cb_arg args = { syms, symcount, symstrtab,
+                                          0, 0 };
+   if (!ctf_output)
+     return;
+
+   if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
+       && !bfd_link_relocatable (&link_info))
+    {
+      if (ctf_link_add_strtab (ctf_output,
+                               gld_${EMULATION_NAME}_ctf_strtab_iter_cb,
+                               &args) < 0)
+        einfo (_("%F%P: warning: CTF strtab association failed; strings will "
+                 "not be shared: %s\n"),
+               ctf_errmsg (ctf_errno (ctf_output)));
+
+      if (ctf_link_shuffle_syms (ctf_output,
+                                 gld_${EMULATION_NAME}_ctf_symbols_iter_cb,
+                                 &args) < 0)
+        einfo (_("%F%P: warning: CTF symbol shuffling failed; slight space "
+                 "cost: %s\n"), ctf_errmsg (ctf_errno (ctf_output)));
+    }
+}
 EOF
+# Put these extra routines in ld${EMULATION_NAME}_emulation
+#
+LDEMUL_EMIT_CTF_EARLY=gld_${EMULATION_NAME}_emit_ctf_early
+LDEMUL_EXAMINE_STRTAB_FOR_CTF=gld_${EMULATION_NAME}_examine_strtab_for_ctf
diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em
index f3c6e3adee..8ad8b9aefe 100644
--- a/ld/emultempl/elf32.em
+++ b/ld/emultempl/elf32.em
@@ -3072,6 +3072,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
   ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
   ${LDEMUL_NEW_VERS_PATTERN-NULL},
-  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
+  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/generic.em b/ld/emultempl/generic.em
index 5cfe5a78a5..e9ebba3be7 100644
--- a/ld/emultempl/generic.em
+++ b/ld/emultempl/generic.em
@@ -157,6 +157,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   ${LDEMUL_RECOGNIZED_FILE-NULL},
   ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
   ${LDEMUL_NEW_VERS_PATTERN-NULL},
-  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
+  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/linux.em b/ld/emultempl/linux.em
index 5a01155984..fdd7bbd42b 100644
--- a/ld/emultempl/linux.em
+++ b/ld/emultempl/linux.em
@@ -209,6 +209,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/msp430.em b/ld/emultempl/msp430.em
index 765a9ea288..6cde975f5e 100644
--- a/ld/emultempl/msp430.em
+++ b/ld/emultempl/msp430.em
@@ -842,7 +842,9 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   ${LDEMUL_RECOGNIZED_FILE-NULL},
   ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
   ${LDEMUL_NEW_VERS_PATTERN-NULL},
-  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
+  ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL},
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
 #
diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 218f98acf9..52722a6cde 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -2371,6 +2371,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   gld_${EMULATION_NAME}_recognized_file,
   gld_${EMULATION_NAME}_find_potential_libraries,
   NULL, /* new_vers_pattern.  */
-  NULL /* extra_map_file_text.  */
+  NULL, /* extra_map_file_text.  */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index 649efaf92c..486f3d471d 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -2168,6 +2168,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   gld_${EMULATION_NAME}_recognized_file,
   gld_${EMULATION_NAME}_find_potential_libraries,
   NULL, /* new_vers_pattern.  */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/ticoff.em b/ld/emultempl/ticoff.em
index 65b03b668f..3e9056f5fb 100644
--- a/ld/emultempl/ticoff.em
+++ b/ld/emultempl/ticoff.em
@@ -182,6 +182,8 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL  /* extra_map_file_text */
+  NULL,  /* extra_map_file_text */
+  ${LDEMUL_EMIT_CTF_EARLY-NULL},
+  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
 };
 EOF
diff --git a/ld/emultempl/vanilla.em b/ld/emultempl/vanilla.em
index c57950ab6b..f3654fda2d 100644
--- a/ld/emultempl/vanilla.em
+++ b/ld/emultempl/vanilla.em
@@ -83,6 +83,8 @@ struct ld_emulation_xfer_struct ld_vanilla_emulation =
   NULL, /* recognized file */
   NULL, /* find_potential_libraries */
   NULL, /* new_vers_pattern */
-  NULL /* extra_map_file_text */
+  NULL, /* extra_map_file_text */
+  NULL, /* emit_ctf_early */
+  NULL  /* examine_strtab_for_ctf */
 };
 EOF
diff --git a/ld/ldemul.c b/ld/ldemul.c
index 16ddb6dcf5..b9dd007a85 100644
--- a/ld/ldemul.c
+++ b/ld/ldemul.c
@@ -371,3 +371,25 @@ ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf)
   if (ld_emulation->extra_map_file_text)
     ld_emulation->extra_map_file_text (abfd, info, mapf);
 }
+
+int
+ldemul_emit_ctf_early (void)
+{
+  if (ld_emulation->emit_ctf_early)
+    return ld_emulation->emit_ctf_early ();
+  /* If the emulation doesn't know if it wants to emit CTF early, it is going
+     to do so.  */
+  return 1;
+}
+
+void
+ldemul_examine_strtab_for_ctf (ctf_file_t *ctf_output,
+                               struct elf_sym_strtab *syms,
+                               bfd_size_type symcount,
+                               struct elf_strtab_hash *symstrtab)
+
+{
+  if (ld_emulation->examine_strtab_for_ctf)
+    ld_emulation->examine_strtab_for_ctf (ctf_output, syms,
+                                          symcount, symstrtab);
+}
diff --git a/ld/ldemul.h b/ld/ldemul.h
index 5b6549f837..1de29d0584 100644
--- a/ld/ldemul.h
+++ b/ld/ldemul.h
@@ -100,6 +100,14 @@ extern struct bfd_elf_version_expr *ldemul_new_vers_pattern
   (struct bfd_elf_version_expr *);
 extern void ldemul_extra_map_file_text
   (bfd *, struct bfd_link_info *, FILE *);
+/* Return 1 if we are emitting CTF early, and 0 if ldemul_examine_strtab_for_ctf
+   will be called by the target.  */
+extern int ldemul_emit_ctf_early
+  (void);
+/* Called from per-target code to examine the strtab and symtab.  */
+extern void ldemul_examine_strtab_for_ctf
+  (ctf_file_t *, struct elf_sym_strtab *, bfd_size_type,
+   struct elf_strtab_hash *);
 
 typedef struct ld_emulation_xfer_struct {
   /* Run before parsing the command line and script file.
@@ -208,6 +216,19 @@ typedef struct ld_emulation_xfer_struct {
   void (*extra_map_file_text)
     (bfd *, struct bfd_link_info *, FILE *);
 
+  /* If this returns true, we emit CTF as early as possible: if false, we emit
+     CTF once the strtab and symtab are laid out.  */
+  int (*emit_ctf_early)
+    (void);
+
+  /* Called to examine the string and symbol table late enough in linking that
+     they are finally laid out.  If emit_ctf_early returns true, this is not
+     called and ldemul_maybe_emit_ctf() emits CTF in 'early' mode: otherwise, it
+     waits until 'late'. (Late mode needs explicit support at per-target link
+     time to get called at all).  If set, called by ld when the examine_strtab
+     bfd_link_callback is invoked by per-target code.  */
+  void (*examine_strtab_for_ctf) (ctf_file_t *, struct elf_sym_strtab *,
+                                  bfd_size_type, struct elf_strtab_hash *);
 } ld_emulation_xfer_type;
 
 typedef enum {
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 3f25b363d0..3b88e6df53 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -39,7 +39,6 @@
 #include "fnmatch.h"
 #include "demangle.h"
 #include "hashtab.h"
-#include "elf-bfd.h"
 #ifdef ENABLE_PLUGINS
 #include "plugin.h"
 #endif /* ENABLE_PLUGINS */
@@ -127,6 +126,7 @@ bfd_boolean delete_output_file_on_failure = FALSE;
 struct lang_phdr *lang_phdr_list;
 struct lang_nocrossrefs *nocrossref_list;
 struct asneeded_minfo **asneeded_list_tail;
+static ctf_file_t *ctf_output;
 
  /* Functions that traverse the linker script and might evaluate
     DEFINED() need to increment this at the start of the traversal.  */
@@ -3575,6 +3575,163 @@ open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode)
     einfo ("%F");
 }
 
+/* Open the CTF sections in the input files with libctf: if any were opened,
+   create a fake input file that we'll write the merged CTF data to later
+   on.  */
+
+static void
+ldlang_open_ctf (void)
+{
+  int any_ctf = 0;
+  int err;
+
+  LANG_FOR_EACH_INPUT_STATEMENT (file)
+    {
+      asection *sect;
+
+      /* Incoming files from the compiler have a single ctf_file_t in them
+         (which is presented to us by the libctf API in a ctf_archive_t
+         wrapper): files derived from a previous relocatable link have a CTF
+         archive containing possibly many CTF files.  */
+
+      if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL)
+        {
+          if (err != ECTF_NOCTFDATA)
+            einfo (_("%P: warning: CTF section in `%pI' not loaded: "
+                     "its types will be discarded: `%s'\n"), file,
+                     ctf_errmsg (err));
+          continue;
+        }
+
+      /* Prevent the contents of this section from being written, while
+         requiring the section itself to be duplicated in the output.  */
+      /* This section must exist if ctf_bfdopen() succeeded.  */
+      sect = bfd_get_section_by_name (file->the_bfd, ".ctf");
+      sect->size = 0;
+      sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS;
+
+      any_ctf = 1;
+    }
+
+  if (!any_ctf)
+    {
+      ctf_output = NULL;
+      return;
+    }
+
+  if ((ctf_output = ctf_create (&err)) != NULL)
+    return;
+
+  einfo (_("%P: warning: CTF output not created: `s'\n"),
+         ctf_errmsg (err));
+
+  LANG_FOR_EACH_INPUT_STATEMENT (errfile)
+    ctf_close (errfile->the_ctf);
+}
+
+/* Merge together CTF sections.  After this, only the symtab-dependent
+   function and data object sections need adjustment.  */
+
+static void
+lang_merge_ctf (void)
+{
+  if (!ctf_output)
+    return;
+
+  LANG_FOR_EACH_INPUT_STATEMENT (file)
+    {
+      if (!file->the_ctf)
+        continue;
+
+      /* Takes ownership of file->u.the_ctfa.  */
+      if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0)
+        {
+          einfo (_("%F%P: cannot link with CTF in %pB: %s\n"), file->the_bfd,
+                 ctf_errmsg (ctf_errno (ctf_output)));
+          ctf_close (file->the_ctf);
+          file->the_ctf = NULL;
+          continue;
+        }
+    }
+
+  if (ctf_link (ctf_output, CTF_LINK_SHARE_UNCONFLICTED) < 0)
+    {
+      asection *output_sect;
+
+      einfo (_("%F%P: CTF linking failed; output will have no CTF section: %s\n"),
+             ctf_errmsg (ctf_errno (ctf_output)));
+      output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
+      output_sect->size = 0;
+      output_sect->flags |= SEC_EXCLUDE;
+    }
+}
+
+/* Let the emulation examine the symbol table and strtab to help it optimize the
+   CTF, if supported.  */
+
+void
+ldlang_ctf_apply_strsym (struct elf_sym_strtab *syms, bfd_size_type symcount,
+                         struct elf_strtab_hash *symstrtab)
+{
+  ldemul_examine_strtab_for_ctf (ctf_output, syms, symcount, symstrtab);
+}
+
+/* Write out the CTF section.  Called early, if the emulation isn't going to
+   need to dedup against the strtab and symtab, then possibly called from the
+   target linker code if the dedup has happened.  */
+static void
+lang_write_ctf (int late)
+{
+  size_t output_size;
+  asection *output_sect;
+
+  if (!ctf_output)
+    return;
+
+  if (!late)
+    {
+      /* Emit CTF early if this emulation says it needs to.  */
+      if (ldemul_emit_ctf_early ())
+        return;
+    }
+  else
+    {
+      if (!ldemul_emit_ctf_early ())
+        return;
+    }
+
+  /* Emit CTF.  */
+
+  /* 4096 below is an arbitrary size above which we want CTF files to be
+     compressed.  TODO: maybe this should be configurable?  */
+  output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
+  output_sect->contents = ctf_link_write (ctf_output, &output_size, 4096);
+  output_sect->size = output_size;
+
+  /* TODO: nothing ever frees this: is that OK?  Lots of other section contents
+     at BFD link time seem to leak as well..  */
+
+  if (!output_sect->contents)
+    {
+      einfo (_("%F%P: CTF section emission failed; output will have no "
+               "CTF section: %s\n"), ctf_errmsg (ctf_errno (ctf_output)));
+      output_sect->size = 0;
+      output_sect->flags |= SEC_EXCLUDE;
+    }
+  ctf_file_close (ctf_output);
+  ctf_output = NULL;
+  /* TODO: what about the input files' CTF sections?  Should we free them?  */
+}
+/* Write out the CTF section late, if the emulation needs that.  */
+
+void
+ldlang_write_ctf_late (void)
+{
+  /* Trigger a "late call", if the emulation needs one.  */
+
+  lang_write_ctf (1);
+}
+
 /* Add the supplied name to the symbol table as an undefined reference.
    This is a two step process as the symbol table doesn't even exist at
    the time the ld command line is processed.  First we put the name
@@ -7503,6 +7660,8 @@ lang_process (void)
   if (config.map_file != NULL)
     lang_print_asneeded ();
 
+  ldlang_open_ctf ();
+
   bfd_section_already_linked_table_free ();
 
   /* Make sure that we're not mixing architectures.  We call this
@@ -7577,6 +7736,14 @@ lang_process (void)
  }
     }
 
+  /* Merge together CTF sections.  After this, only the symtab-dependent
+     function and data object sections need adjustment.  */
+  lang_merge_ctf ();
+
+  /* Emit the CTF, iff the emulation doesn't need to do late emission after
+     examining things laid out late, like the strtab.  */
+  lang_write_ctf (0);
+
   /* Copy forward lma regions for output sections in same lma region.  */
   lang_propagate_lma_regions ();
 
diff --git a/ld/ldlang.h b/ld/ldlang.h
index 4e96a20c15..c53d5806e7 100644
--- a/ld/ldlang.h
+++ b/ld/ldlang.h
@@ -21,6 +21,13 @@
 #ifndef LDLANG_H
 #define LDLANG_H
 
+#include "elf-bfd.h"
+#include "ctf-api.h"
+
+/* This is defined in elf-bfd.h and far too invasively named to be allowed into
+   ld.  */
+#undef NAME
+
 #define DEFAULT_MEMORY_REGION   "*default*"
 
 typedef enum
@@ -306,6 +313,8 @@ typedef struct lang_input_statement_struct
 
   bfd *the_bfd;
 
+  ctf_archive_t *the_ctf;
+
   struct flag_info *section_flag_list;
 
   /* Next pointer for file_chain statement list.  */
@@ -688,6 +697,10 @@ extern void add_excluded_libs (const char *);
 extern bfd_boolean load_symbols
   (lang_input_statement_type *, lang_statement_list_type *);
 
+extern void ldlang_ctf_apply_strsym
+  (struct elf_sym_strtab *, bfd_size_type, struct elf_strtab_hash *);
+extern void ldlang_write_ctf_late
+  (void);
 extern bfd_boolean
 ldlang_override_segment_assignment
   (struct bfd_link_info *, bfd *, asection *, asection *, bfd_boolean);
diff --git a/ld/ldmain.c b/ld/ldmain.c
index a7ca4f487d..477910073e 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -148,7 +148,9 @@ static struct bfd_link_callbacks link_callbacks =
   einfo,
   info_msg,
   minfo,
-  ldlang_override_segment_assignment
+  ldlang_override_segment_assignment,
+  ldlang_ctf_apply_strsym,
+  ldlang_write_ctf_late
 };
 
 static bfd_assert_handler_type default_bfd_assert_handler;
diff --git a/ld/scripttempl/DWARF.sc b/ld/scripttempl/DWARF.sc
index 9c9cb649ef..bd86cadd47 100644
--- a/ld/scripttempl/DWARF.sc
+++ b/ld/scripttempl/DWARF.sc
@@ -43,4 +43,7 @@ cat <<EOF
   /* DWARF Extension.  */
   .debug_macro    0 : { *(.debug_macro) }
   .debug_addr     0 : { *(.debug_addr) }
+
+  /* CTF.  */
+  .ctf            : { KEEP(*(.ctf)) }
 EOF
diff --git a/ld/testsuite/ld-bootstrap/bootstrap.exp b/ld/testsuite/ld-bootstrap/bootstrap.exp
index ee9442da16..a303c05e10 100644
--- a/ld/testsuite/ld-bootstrap/bootstrap.exp
+++ b/ld/testsuite/ld-bootstrap/bootstrap.exp
@@ -160,7 +160,7 @@ foreach flags $test_flags {
  setup_xfail "mips*-*-irix5*"
     }
 
-    if ![ld_link $CC tmpdir/ld1 "$flags tmpdir/ld-partial.o $BFDLIB $LIBIBERTY $extralibs"] {
+    if ![ld_link $CC tmpdir/ld1 "$flags tmpdir/ld-partial.o $LIBCTF $BFDLIB $LIBIBERTY $extralibs"] {
  fail $testname
  continue
     }
@@ -177,13 +177,13 @@ foreach flags $test_flags {
     }
 
     regsub /tmpdir/ld/ $gcc_B_opt_save /tmpdir/gccld1/ gcc_B_opt
-    if ![ld_link $CC tmpdir/ld2 "$flags $OFILES $BFDLIB $LIBIBERTY $extralibs"] {
+    if ![ld_link $CC tmpdir/ld2 "$flags $OFILES $LIBCTF $BFDLIB $LIBIBERTY $extralibs"] {
  fail $testname
  continue
     }
 
     regsub /tmpdir/ld/ $gcc_B_opt_save /tmpdir/gccld2/ gcc_B_opt
-    if ![ld_link $CC tmpdir/ld3 "$flags $OFILES $BFDLIB $LIBIBERTY $extralibs"] {
+    if ![ld_link $CC tmpdir/ld3 "$flags $OFILES $LIBCTF $BFDLIB $LIBIBERTY $extralibs"] {
  fail $testname
  continue
     }
@@ -196,7 +196,7 @@ foreach flags $test_flags {
  # generated by different linkers, tmpdir/ld1 and tmpdir/ld2.
  # So we rebuild tmpdir/ld2 with tmpdir/ld3.
  regsub /tmpdir/ld/ $gcc_B_opt_save /tmpdir/gccld3/ gcc_B_opt
- if ![ld_link $CC tmpdir/ld2 "$flags $OFILES $BFDLIB $LIBIBERTY $extralibs"] {
+ if ![ld_link $CC tmpdir/ld2 "$flags $OFILES $LIBCTF $BFDLIB $LIBIBERTY $extralibs"] {
     fail $testname
     continue
  }
--
2.22.0.238.g049a27acdc

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 00/19] CTF linking support

Nick Alcock
In reply to this post by Nick Alcock
On 16 Jul 2019, Nick Alcock outgrape:

> This is the beginning of support for linking CTF sections.  The type
> deduplicator is not very good, creation of "subsections" for types and variables

Ping? Anyone want to review this one? If the ld/bfd parts are totally
wrong I'd like to know (and also how to do this right, of course).

The v2 0019 patch I just posted to this thread adds non-ELF support: the
testsuite is happy on all targets now. (I observed no regressions.)

The patches touching things outside the libctf tree that need review all
have REVIEW in their subject lines: patches 4, 6, 7, and of course 18
and the new v2 19 (a squash of the old 19 and '20/19' in case you prefer
to review the final form in one go).
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 19/19 REVIEW RFC] bfd, ld: add CTF section linking

Nick Alcock
In reply to this post by Nick Alcock
On 22 Jul 2019, Nick Alcock verbalised:

> This is quite complicated because the CTF section's contents depend on
> the final contents of the symtab and strtab, because it has two sections
> whose contents are shuffled to be in 1:1 correspondence with the symtab,
> and an internal strtab that gets deduplicated against the ELF strtab
> (with offsets adjusted to point into the ELF strtab instead).  It is
> also compressed if large enough, so its size depends on its contents!

This breaks gas, in that you get a CTF section that is filled with
nothing but zero bytes. (The non-ELF support also broke emission of CTF
on ELF platforms, because I dyslexically mixed up the meanings of the
words 'early' and 'late', or something like that.)

(I didn't notice the gas breakage because GCC was preferring the system
gas without my realizing it. Ah well, it's spotted now.)

Fix coming tomorrow (still have to test it and it's gone midnight here).

(this sort of thing is why I need this stuff reviewed! :) )
12