+}
+
+/* Subroutine of source_section_scripts to simplify it.
+ Load FILE as a script in extension language LANGUAGE.
+ The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
+
+static void
+source_script_file (struct auto_load_pspace_info *pspace_info,
+ struct objfile *objfile,
+ const struct extension_language_defn *language,
+ const char *section_name, unsigned int offset,
+ const char *file)
+{
+ int in_hash_table;
+ objfile_script_sourcer_func *sourcer;
+
+ /* Skip this script if support is not compiled in. */
+ sourcer = ext_lang_objfile_script_sourcer (language);
+ if (sourcer == NULL)
+ {
+ /* We don't throw an error, the program is still debuggable. */
+ maybe_print_unsupported_script_warning (pspace_info, objfile, language,
+ section_name, offset);
+ /* We *could* still try to open it, but there's no point. */
+ maybe_add_script_file (pspace_info, 0, file, NULL, language);
+ return;
+ }
+
+ /* Skip this script if auto-loading it has been disabled. */
+ if (!ext_lang_auto_load_enabled (language))
+ {
+ /* No message is printed, just skip it. */
+ return;
+ }
+
+ gdb::optional<open_script> opened = find_and_open_script (file,
+ 1 /*search_path*/);
+
+ if (opened)
+ {
+ if (!file_is_auto_load_safe (opened->full_path.get (),
+ _("auto-load: Loading %s script "
+ "\"%s\" from section \"%s\" of "
+ "objfile \"%s\".\n"),
+ ext_lang_name (language),
+ opened->full_path.get (),
+ section_name, objfile_name (objfile)))
+ opened.reset ();
+ }
+ else
+ {
+ /* If one script isn't found it's not uncommon for more to not be
+ found either. We don't want to print a message for each script,
+ too much noise. Instead, we print the warning once and tell the
+ user how to find the list of scripts that weren't loaded.
+ We don't throw an error, the program is still debuggable.
+
+ IWBN if complaints.c were more general-purpose. */
+
+ maybe_print_script_not_found_warning (pspace_info, objfile, language,
+ section_name, offset);
+ }
+
+ in_hash_table = maybe_add_script_file (pspace_info, bool (opened), file,
+ (opened
+ ? opened->full_path.get ()
+ : NULL),
+ language);
+
+ /* If this file is not currently loaded, load it. */
+ if (opened && !in_hash_table)
+ sourcer (language, objfile, opened->stream.get (),
+ opened->full_path.get ());
+}
+
+/* Subroutine of source_section_scripts to simplify it.
+ Execute SCRIPT as a script in extension language LANG.
+ The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
+
+static void
+execute_script_contents (struct auto_load_pspace_info *pspace_info,
+ struct objfile *objfile,
+ const struct extension_language_defn *language,
+ const char *section_name, unsigned int offset,
+ const char *script)
+{
+ objfile_script_executor_func *executor;
+ const char *newline, *script_text;
+ char *name;
+ int is_safe, in_hash_table;
+ struct cleanup *cleanups;
+
+ cleanups = make_cleanup (null_cleanup, NULL);
+
+ /* The first line of the script is the name of the script.
+ It must not contain any kind of space character. */
+ name = NULL;
+ newline = strchr (script, '\n');
+ if (newline != NULL)
+ {
+ char *buf, *p;
+
+ /* Put the name in a buffer and validate it. */
+ buf = xstrndup (script, newline - script);
+ make_cleanup (xfree, buf);
+ for (p = buf; *p != '\0'; ++p)
+ {
+ if (isspace (*p))
+ break;
+ }
+ /* We don't allow nameless scripts, they're not helpful to the user. */
+ if (p != buf && *p == '\0')
+ name = buf;
+ }
+ if (name == NULL)
+ {
+ /* We don't throw an error, the program is still debuggable. */
+ warning (_("\
+Missing/bad script name in entry at offset %u in section %s\n\
+of file %s."),
+ offset, section_name, objfile_name (objfile));
+ do_cleanups (cleanups);
+ return;
+ }
+ script_text = newline + 1;
+
+ /* Skip this script if support is not compiled in. */
+ executor = ext_lang_objfile_script_executor (language);
+ if (executor == NULL)
+ {
+ /* We don't throw an error, the program is still debuggable. */
+ maybe_print_unsupported_script_warning (pspace_info, objfile, language,
+ section_name, offset);
+ maybe_add_script_text (pspace_info, 0, name, language);
+ do_cleanups (cleanups);
+ return;
+ }
+
+ /* Skip this script if auto-loading it has been disabled. */
+ if (!ext_lang_auto_load_enabled (language))
+ {
+ /* No message is printed, just skip it. */
+ do_cleanups (cleanups);
+ return;
+ }
+
+ is_safe = file_is_auto_load_safe (objfile_name (objfile),
+ _("auto-load: Loading %s script "
+ "\"%s\" from section \"%s\" of "
+ "objfile \"%s\".\n"),
+ ext_lang_name (language), name,
+ section_name, objfile_name (objfile));
+
+ in_hash_table = maybe_add_script_text (pspace_info, is_safe, name, language);
+
+ /* If this file is not currently loaded, load it. */
+ if (is_safe && !in_hash_table)
+ executor (language, objfile, name, script_text);