+/* Add an OFFSET to the start address of each section in OBJF, except
+ sections that were specified in ADDRS. */
+
+static void
+set_objfile_default_section_offset (struct objfile *objf,
+ const section_addr_info &addrs,
+ CORE_ADDR offset)
+{
+ /* Add OFFSET to all sections by default. */
+ std::vector<struct section_offsets> offsets (objf->num_sections,
+ { { offset } });
+
+ /* Create sorted lists of all sections in ADDRS as well as all
+ sections in OBJF. */
+
+ std::vector<const struct other_sections *> addrs_sorted
+ = addrs_section_sort (addrs);
+
+ section_addr_info objf_addrs
+ = build_section_addr_info_from_objfile (objf);
+ std::vector<const struct other_sections *> objf_addrs_sorted
+ = addrs_section_sort (objf_addrs);
+
+ /* Walk the BFD section list, and if a matching section is found in
+ ADDRS_SORTED_LIST, set its offset to zero to keep its address
+ unchanged.
+
+ Note that both lists may contain multiple sections with the same
+ name, and then the sections from ADDRS are matched in BFD order
+ (thanks to sectindex). */
+
+ std::vector<const struct other_sections *>::iterator addrs_sorted_iter
+ = addrs_sorted.begin ();
+ for (const other_sections *objf_sect : objf_addrs_sorted)
+ {
+ const char *objf_name = addr_section_name (objf_sect->name.c_str ());
+ int cmp = -1;
+
+ while (cmp < 0 && addrs_sorted_iter != addrs_sorted.end ())
+ {
+ const struct other_sections *sect = *addrs_sorted_iter;
+ const char *sect_name = addr_section_name (sect->name.c_str ());
+ cmp = strcmp (sect_name, objf_name);
+ if (cmp <= 0)
+ ++addrs_sorted_iter;
+ }
+
+ if (cmp == 0)
+ offsets[objf_sect->sectindex].offsets[0] = 0;
+ }
+
+ /* Apply the new section offsets. */
+ objfile_relocate (objf, offsets.data ());
+}
+