+/* An object of this type is passed to find_section_offset. */
+
+struct find_section_offset_arg
+{
+ /* The objfile. */
+
+ struct objfile *objfile;
+
+ /* Flags to invert. */
+
+ flagword invert;
+
+ /* Flags to look for. */
+
+ flagword flags;
+
+ /* A text section with non-zero size, if any. */
+
+ asection *best_section;
+
+ /* An empty text section, if any. */
+
+ asection *empty_section;
+};
+
+/* A callback for bfd_map_over_sections that tries to find a section
+ with particular flags in an objfile. */
+
+static void
+find_section_offset (bfd *abfd, asection *sect, void *arg)
+{
+ struct find_section_offset_arg *info = arg;
+ flagword aflag;
+
+ aflag = bfd_get_section_flags (abfd, sect);
+
+ aflag ^= info->invert;
+
+ if ((aflag & info->flags) == info->flags)
+ {
+ if (bfd_section_size (abfd, sect) > 0)
+ {
+ if (info->best_section == NULL)
+ info->best_section = sect;
+ }
+ else
+ {
+ if (info->empty_section == NULL)
+ info->empty_section = sect;
+ }
+ }
+}
+
+/* Set a section index from a BFD. */
+
+static void
+set_section_index (struct objfile *objfile, flagword invert, flagword flags,
+ int *index_ptr)
+{
+ struct find_section_offset_arg info;
+
+ info.objfile = objfile;
+ info.best_section = NULL;
+ info.empty_section = NULL;
+ info.invert = invert;
+ info.flags = flags;
+ bfd_map_over_sections (objfile->obfd, find_section_offset, &info);
+
+ if (info.best_section)
+ *index_ptr = info.best_section->index;
+ else if (info.empty_section)
+ *index_ptr = info.empty_section->index;
+}
+