+/* 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)
+{
+ FILE *stream;
+ char *full_path;
+ int opened, in_hash_table;
+ struct cleanup *cleanups;
+ 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;
+ }
+
+ opened = find_and_open_script (file, 1 /*search_path*/,
+ &stream, &full_path);
+
+ cleanups = make_cleanup (null_cleanup, NULL);
+ if (opened)
+ {
+ make_cleanup_fclose (stream);
+ make_cleanup (xfree, full_path);
+
+ if (!file_is_auto_load_safe (full_path,
+ _("auto-load: Loading %s script "
+ "\"%s\" from section \"%s\" of "
+ "objfile \"%s\".\n"),
+ ext_lang_name (language), full_path,
+ section_name, objfile_name (objfile)))
+ opened = 0;
+ }
+ else
+ {
+ full_path = NULL;
+
+ /* 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, opened, file, full_path,
+ language);
+
+ /* If this file is not currently loaded, load it. */
+ if (opened && !in_hash_table)
+ sourcer (language, objfile, stream, full_path);
+
+ do_cleanups (cleanups);
+}
+
+/* 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);
+
+ do_cleanups (cleanups);
+}
+
+/* Load scripts specified in OBJFILE.
+ START,END delimit a buffer containing a list of nul-terminated
+ file names.
+ SECTION_NAME is used in error messages.
+
+ Scripts specified as file names are found per normal "source -s" command
+ processing. First the script is looked for in $cwd. If not found there
+ the source search path is used.
+
+ The section contains a list of path names of script files to load or
+ actual script contents. Each entry is nul-terminated. */
+
+static void
+source_section_scripts (struct objfile *objfile, const char *section_name,
+ const char *start, const char *end)
+{
+ const char *p;
+ struct auto_load_pspace_info *pspace_info;
+
+ pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
+
+ for (p = start; p < end; ++p)
+ {
+ const char *entry;
+ const struct extension_language_defn *language;
+ unsigned int offset = p - start;
+ int code = *p;
+
+ switch (code)
+ {
+ case SECTION_SCRIPT_ID_PYTHON_FILE:
+ case SECTION_SCRIPT_ID_PYTHON_TEXT:
+ language = get_ext_lang_defn (EXT_LANG_PYTHON);
+ break;
+ case SECTION_SCRIPT_ID_SCHEME_FILE:
+ case SECTION_SCRIPT_ID_SCHEME_TEXT:
+ language = get_ext_lang_defn (EXT_LANG_GUILE);
+ break;
+ default:
+ warning (_("Invalid entry in %s section"), section_name);
+ /* We could try various heuristics to find the next valid entry,
+ but it's safer to just punt. */
+ return;
+ }
+ entry = ++p;
+
+ while (p < end && *p != '\0')
+ ++p;
+ if (p == end)
+ {
+ warning (_("Non-nul-terminated entry in %s at offset %u"),
+ section_name, offset);
+ /* Don't load/execute it. */
+ break;
+ }
+
+ switch (code)
+ {
+ case SECTION_SCRIPT_ID_PYTHON_FILE:
+ case SECTION_SCRIPT_ID_SCHEME_FILE:
+ if (p == entry)
+ {
+ warning (_("Empty entry in %s at offset %u"),
+ section_name, offset);
+ continue;
+ }
+ source_script_file (pspace_info, objfile, language,
+ section_name, offset, entry);
+ break;
+ case SECTION_SCRIPT_ID_PYTHON_TEXT:
+ case SECTION_SCRIPT_ID_SCHEME_TEXT:
+ execute_script_contents (pspace_info, objfile, language,
+ section_name, offset, entry);
+ break;
+ }
+ }
+}
+
+/* Load scripts specified in section SECTION_NAME of OBJFILE. */
+
+static void
+auto_load_section_scripts (struct objfile *objfile, const char *section_name)
+{
+ bfd *abfd = objfile->obfd;
+ asection *scripts_sect;
+ bfd_byte *data = NULL;
+
+ scripts_sect = bfd_get_section_by_name (abfd, section_name);
+ if (scripts_sect == NULL)
+ return;
+
+ if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
+ warning (_("Couldn't read %s section of %s"),
+ section_name, bfd_get_filename (abfd));
+ else
+ {
+ struct cleanup *cleanups;
+ char *p = (char *) data;
+
+ cleanups = make_cleanup (xfree, p);
+ source_section_scripts (objfile, section_name, p,
+ p + bfd_get_section_size (scripts_sect));
+ do_cleanups (cleanups);
+ }
+}
+