+ asection *note_sec = NULL;
+
+ /* An external target method must build the notes section. */
+ /* FIXME: uweigand/2011-10-06: All architectures that support core file
+ generation should be converted to gdbarch_make_corefile_notes; at that
+ point, the target vector method can be removed. */
+ if (!gdbarch_make_corefile_notes_p (target_gdbarch ()))
+ note_data = target_make_corefile_notes (obfd, ¬e_size);
+ else
+ note_data = gdbarch_make_corefile_notes (target_gdbarch (), obfd, ¬e_size);
+
+ cleanup = make_cleanup (xfree, note_data);
+
+ if (note_data == NULL || note_size == 0)
+ error (_("Target does not support core file generation."));
+
+ /* Create the note section. */
+ note_sec = bfd_make_section_anyway_with_flags (obfd, "note0",
+ SEC_HAS_CONTENTS
+ | SEC_READONLY
+ | SEC_ALLOC);
+ if (note_sec == NULL)
+ error (_("Failed to create 'note' section for corefile: %s"),
+ bfd_errmsg (bfd_get_error ()));
+
+ bfd_set_section_vma (obfd, note_sec, 0);
+ bfd_set_section_alignment (obfd, note_sec, 0);
+ bfd_set_section_size (obfd, note_sec, note_size);
+
+ /* Now create the memory/load sections. */
+ if (gcore_memory_sections (obfd) == 0)
+ error (_("gcore: failed to get corefile memory sections from target."));
+
+ /* Write out the contents of the note section. */
+ if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
+ warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
+
+ do_cleanups (cleanup);
+}
+
+/* write_gcore_file -- helper for gcore_command (exported).
+ Compose and write the corefile data to the core file. */
+
+void
+write_gcore_file (bfd *obfd)
+{
+ struct gdb_exception except = exception_none;
+
+ target_prepare_to_generate_core ();
+
+ TRY
+ {
+ write_gcore_file_1 (obfd);
+ }
+ CATCH (e, RETURN_MASK_ALL)
+ {
+ except = e;
+ }
+ END_CATCH
+
+ target_done_generating_core ();
+
+ if (except.reason < 0)
+ throw_exception (except);
+}
+
+/* gcore_command -- implements the 'gcore' command.
+ Generate a core file from the inferior process. */
+
+static void
+gcore_command (char *args, int from_tty)
+{
+ gdb::unique_xmalloc_ptr<char> corefilename;