+/* The options for the "maintenance info sections" command. */
+
+struct maint_info_sections_opts
+{
+ /* For "-all-objects". */
+ bool all_objects = false;
+};
+
+static const gdb::option::option_def maint_info_sections_option_defs[] = {
+
+ gdb::option::flag_option_def<maint_info_sections_opts> {
+ "all-objects",
+ [] (maint_info_sections_opts *opts) { return &opts->all_objects; },
+ N_("Display information from all loaded object files."),
+ },
+};
+
+/* Create an option_def_group for the "maintenance info sections" options,
+ with CC_OPTS as context. */
+
+static inline gdb::option::option_def_group
+make_maint_info_sections_options_def_group (maint_info_sections_opts *cc_opts)
+{
+ return {{maint_info_sections_option_defs}, cc_opts};
+}
+
+/* Completion for the "maintenance info sections" command. */
+
+static void
+maint_info_sections_completer (struct cmd_list_element *cmd,
+ completion_tracker &tracker,
+ const char *text, const char * /* word */)
+{
+ /* Complete command options. */
+ const auto group = make_maint_info_sections_options_def_group (nullptr);
+ if (gdb::option::complete_options
+ (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
+ return;
+ const char *word = advance_to_expression_complete_word_point (tracker, text);
+
+ /* Offer completion for section flags, but not section names. This is
+ only a maintenance command after all, no point going over the top. */
+ std::vector<const char *> flags;
+ for (const auto &f : bfd_flag_info)
+ flags.push_back (f.name);
+ flags.push_back (nullptr);
+ complete_on_enum (tracker, flags.data (), text, word);
+}
+
+/* Implement the "maintenance info sections" command. */
+
+static void
+maintenance_info_sections (const char *arg, int from_tty)
+{
+ /* Check if the "-all-objects" flag was passed. */
+ maint_info_sections_opts opts;
+ const auto group = make_maint_info_sections_options_def_group (&opts);
+ gdb::option::process_options
+ (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
+
+ for (objfile *ofile : current_program_space->objfiles ())
+ {
+ if (ofile->obfd == current_program_space->exec_bfd ())
+ maint_print_all_sections (_("Exec file: "), ofile->obfd, ofile, arg);
+ else if (opts.all_objects)
+ maint_print_all_sections (_("Object file: "), ofile->obfd, ofile, arg);