uiout->text (".\n");
 }
 
+/* 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 struct 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 ());
+}
+
 /* This function allows the addition of incrementally linked object files.
    It does not modify any state in the target, only in the debugger.  */
 /* Note: ezannoni 2000-04-13 This function/command used to have a
 
   std::vector<sect_opt> sect_opts = { { ".text", NULL } };
   bool stop_processing_options = false;
+  CORE_ADDR offset = 0;
 
   dont_repeat ();
 
     error (_("add-symbol-file takes a file name and an address"));
 
   bool seen_addr = false;
+  bool seen_offset = false;
   gdb_argv argv (args);
 
   for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
          sect_opts.push_back (sect);
          argcnt += 2;
        }
+      else if (strcmp (arg, "-o") == 0)
+       {
+         arg = argv[++argcnt];
+         if (arg == NULL)
+           error (_("Missing argument to -o"));
+
+         offset = parse_and_eval_address (arg);
+         seen_offset = true;
+       }
       else if (strcmp (arg, "--") == 0)
        stop_processing_options = true;
       else
         At this point, we don't know what file type this is,
         so we can't determine what section names are valid.  */
     }
-  if (section_addrs.empty ())
+  if (seen_offset)
+      printf_unfiltered (_("%s offset by %s\n"),
+                        (section_addrs.empty ()
+                         ? _(" with all sections")
+                         : _("with other sections")),
+                        paddress (gdbarch, offset));
+  else if (section_addrs.empty ())
     printf_unfiltered ("\n");
 
   if (from_tty && (!query ("%s", "")))
   objf = symbol_file_add (filename.get (), add_flags, §ion_addrs,
                          flags);
 
+  if (seen_offset)
+    set_objfile_default_section_offset (objf, section_addrs, offset);
+
   add_target_sections_of_objfile (objf);
 
   /* Getting new symbols may change our opinion about what is
 
   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
 Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
-Usage: add-symbol-file FILE [-readnow | -readnever] [ADDR] \
+Usage: add-symbol-file FILE [-readnow | -readnever] [-o OFF] [ADDR] \
 [-s SECT-NAME SECT-ADDR]...\n\
 ADDR is the starting address of the file's text.\n\
 Each '-s' argument provides a section name and address, and\n\
 should be specified if the data and bss segments are not contiguous\n\
-with the text.  SECT-NAME is a section name to be loaded at SECT-ADDR.\n"
+with the text.  SECT-NAME is a section name to be loaded at SECT-ADDR.\n\
+OFF is an optional offset which is added to the default load addresses\n\
+of all sections for which no other address was specified.\n"
 READNOW_READNEVER_HELP),
               &cmdlist);
   set_cmd_completer (c, filename_completer);
 
 gdb_assert {${new_function_foo_addr} == ${function_foo_addr} + $offset} \
     "function foo is moved by offset"
 
+# Load the object using add-symbol-file with an offset and check that
+# all addresses are moved by that offset.
+
+set offset 0x10000
+clean_restart
+gdb_test "add-symbol-file -o $offset $binfile" \
+    "Reading symbols from ${binfile}\.\.\.done\." \
+    "add-symbol-file with offset" \
+    "add symbol table from file \".*${testfile}\\.o\" with all sections offset by $offset\[\r\n\]+\\(y or n\\) " \
+    "y"
+
+# Make sure the address of a static variable is moved by offset.
+set new_static_foo_addr [get_var_address static_foo]
+gdb_assert { ${new_static_foo_addr} == ${static_foo_addr} + $offset } \
+    "static variable foo is moved by offset"
+
+# Make sure the address of a global variable is moved by offset.
+set new_global_foo_addr [get_var_address global_foo]
+gdb_assert { ${new_global_foo_addr} == ${global_foo_addr} + $offset } \
+    "global variable foo is moved by offset"
+
+# Make sure the address of a function is moved by offset.
+set new_function_foo_addr [get_var_address function_foo]
+gdb_assert { ${new_function_foo_addr} == ${function_foo_addr} + $offset } \
+    "function foo is moved by offset"
+
+# Re-load the object giving an explicit address for .text
+
+set text [ format "0x%x" [expr ${function_foo_addr} + 0x20000] ]
+clean_restart
+gdb_test "add-symbol-file $binfile -o $offset $text" \
+    "Reading symbols from ${binfile}\.\.\.done\." \
+    "add-symbol-file with offset, text address given" \
+    "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = ${text}\[\r\n\]+with other sections offset by ${offset}\[\r\n\]+\\(y or n\\) " \
+    "y"
+
+# Make sure function has a different addresses now.
+set function_foo_addr [get_var_address function_foo]
+gdb_assert { ${function_foo_addr} != ${new_function_foo_addr} } \
+    "function foo has a different address"
+
+# Re-load the object giving an explicit address for .data
+
+set data [ format "0x%x" [expr ${global_foo_addr} + 0x20000] ]
+clean_restart
+gdb_test "add-symbol-file $binfile -o $offset -s .data $data" \
+    "Reading symbols from ${binfile}\.\.\.done\." \
+    "add-symbol-file with offset, data address given" \
+    "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.data_addr = ${data}\[\r\n\]+with other sections offset by ${offset}\[\r\n\]+\\(y or n\\) " \
+    "y"
+
+# Make sure variable has a different addresses now.
+set global_foo_addr [get_var_address global_foo]
+gdb_assert { ${global_foo_addr} != ${new_global_foo_addr} } \
+    "global variable foo has a different address"
+
 # Now try loading the object as an exec-file; we should be able to print
 # the values of variables after we do this.