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

Message ID 20190718171442.376374-1-nick.alcock@oracle.com
State New
Headers show
Series
  • CTF linking support
Related show

Commit Message

Nick Alcock July 18, 2019, 5:14 p.m.
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(-)

-- 
2.22.0.238.g049a27acdc

Patch

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;