+ 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)
+ {
+ einfo (_("%F%P: CTF linking failed; output will have no CTF section: %s\n"),
+ ctf_errmsg (ctf_errno (ctf_output)));
+ if (output_sect)
+ {
+ 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 late if this emulation says it can do so. */
+ if (ldemul_emit_ctf_early ())
+ return;
+ }
+ else
+ {
+ if (!ldemul_emit_ctf_early ())
+ return;
+ }
+
+ /* Emit CTF. */
+
+ output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
+ if (output_sect)
+ {
+ output_sect->contents = ctf_link_write (ctf_output, &output_size,
+ CTF_COMPRESSION_THRESHOLD);
+ output_sect->size = output_size;
+ output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP;
+
+ 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;
+ }
+ }
+
+ /* This also closes every CTF input file used in the link. */
+ ctf_file_close (ctf_output);
+ ctf_output = NULL;
+
+ LANG_FOR_EACH_INPUT_STATEMENT (file)
+ file->the_ctf = NULL;
+}
+
+/* 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
+ on a list, then, once the output file has been opened, transfer the
+ name to the symbol table. */
+
+typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
+
+#define ldlang_undef_chain_list_head entry_symbol.next
+
+void
+ldlang_add_undef (const char *const name, bfd_boolean cmdline)
+{
+ ldlang_undef_chain_list_type *new_undef;
+
+ undef_from_cmdline = undef_from_cmdline || cmdline;
+ new_undef = stat_alloc (sizeof (*new_undef));
+ new_undef->next = ldlang_undef_chain_list_head;
+ ldlang_undef_chain_list_head = new_undef;
+
+ new_undef->name = xstrdup (name);
+
+ if (link_info.output_bfd != NULL)
+ insert_undefined (new_undef->name);
+}
+
+/* Insert NAME as undefined in the symbol table. */
+
+static void
+insert_undefined (const char *name)
+{
+ struct bfd_link_hash_entry *h;
+
+ h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
+ if (h == NULL)
+ einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
+ if (h->type == bfd_link_hash_new)
+ {
+ h->type = bfd_link_hash_undefined;
+ h->u.undef.abfd = NULL;
+ h->non_ir_ref_regular = TRUE;
+ if (is_elf_hash_table (link_info.hash))
+ ((struct elf_link_hash_entry *) h)->mark = 1;
+ bfd_link_add_undef (link_info.hash, h);
+ }
+}
+
+/* Run through the list of undefineds created above and place them
+ into the linker hash table as undefined symbols belonging to the
+ script file. */
+
+static void
+lang_place_undefineds (void)
+{
+ ldlang_undef_chain_list_type *ptr;
+
+ for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
+ insert_undefined (ptr->name);
+}
+
+/* Structure used to build the list of symbols that the user has required
+ be defined. */
+
+struct require_defined_symbol
+{
+ const char *name;
+ struct require_defined_symbol *next;
+};
+
+/* The list of symbols that the user has required be defined. */
+
+static struct require_defined_symbol *require_defined_symbol_list;
+
+/* Add a new symbol NAME to the list of symbols that are required to be
+ defined. */
+
+void
+ldlang_add_require_defined (const char *const name)
+{
+ struct require_defined_symbol *ptr;
+
+ ldlang_add_undef (name, TRUE);
+ ptr = stat_alloc (sizeof (*ptr));