c5f8fe0
--- a/gdb/dwarf2read.c
c5f8fe0
+++ b/gdb/dwarf2read.c
c5f8fe0
@@ -5754,7 +5754,12 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c5f8fe0
 {
c5f8fe0
   struct attribute *attr;
c5f8fe0
   struct symbol *sym;
c5f8fe0
-  CORE_ADDR base = (CORE_ADDR) 0;
c5f8fe0
+  struct objfile *objfile = cu->objfile;
c5f8fe0
+  CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
c5f8fe0
+				 SECT_OFF_TEXT (objfile));
c5f8fe0
+  /* This is used only for DW_AT_data_member_location entries.  */
c5f8fe0
+  CORE_ADDR base = 0;
c5f8fe0
+  int base_p = 0;
c5f8fe0
 
c5f8fe0
   attr = dwarf2_attr (die, DW_AT_location, cu);
c5f8fe0
   if (attr)
c5f8fe0
@@ -5763,6 +5768,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c5f8fe0
       if (attr_form_is_block (attr))
c5f8fe0
 	{
c5f8fe0
 	  base = decode_locdesc (DW_BLOCK (attr), cu);
c5f8fe0
+	  base_p = 1;
c5f8fe0
 	}
c5f8fe0
       else if (attr_form_is_section_offset (attr))
c5f8fe0
 	{
c5f8fe0
@@ -5824,12 +5830,15 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c5f8fe0
 	      else
c5f8fe0
 		dwarf2_complex_location_expr_complaint ();
c5f8fe0
 
c5f8fe0
-	      SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c5f8fe0
+	      if (!base_p)
c5f8fe0
+		dwarf2_invalid_attrib_class_complaint
c5f8fe0
+		  ("DW_AT_data_member_location", "common block member");
c5f8fe0
+	      SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset + baseaddr;
c5f8fe0
 	      add_symbol_to_list (sym, &global_symbols);
c5f8fe0
 	    }
c5f8fe0
 
c5f8fe0
 	  if (SYMBOL_CLASS (sym) == LOC_STATIC)
c5f8fe0
-	    SET_FIELD_PHYSADDR (*field, SYMBOL_VALUE_ADDRESS (sym));
c5f8fe0
+	    SET_FIELD_PHYSADDR (*field, SYMBOL_VALUE_ADDRESS (sym) - baseaddr);
c5f8fe0
 	  else
c5f8fe0
 	    SET_FIELD_PHYSNAME (*field, SYMBOL_LINKAGE_NAME (sym));
c5f8fe0
 	  FIELD_TYPE (*field) = SYMBOL_TYPE (sym);
c5f8fe0
@@ -5843,7 +5852,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c5f8fe0
 
c5f8fe0
       sym = new_symbol (die, type, cu);
c5f8fe0
       /* SYMBOL_VALUE_ADDRESS never gets used as all its fields are static.  */
c5f8fe0
-      SYMBOL_VALUE_ADDRESS (sym) = base;
c5f8fe0
+      SYMBOL_VALUE_ADDRESS (sym) = base + baseaddr;
c5f8fe0
 
c5f8fe0
       set_die_type (die, type, cu);
c5f8fe0
     }
c5f8fe0
--- a/gdb/exec.c
c5f8fe0
+++ b/gdb/exec.c
c5f8fe0
@@ -32,6 +32,7 @@
c5f8fe0
 #include "exec.h"
c5f8fe0
 #include "observer.h"
c5f8fe0
 #include "arch-utils.h"
c5f8fe0
+#include "solib.h"
c5f8fe0
 
c5f8fe0
 #include <fcntl.h>
c5f8fe0
 #include "readline/readline.h"
c5f8fe0
@@ -220,6 +221,10 @@ exec_file_attach (char *filename, int from_tty)
c5f8fe0
       char *scratch_pathname;
c5f8fe0
       int scratch_chan;
c5f8fe0
       struct target_section *sections = NULL, *sections_end = NULL;
c5f8fe0
+      struct target_section *p;
c5f8fe0
+      int addr_bit;
c5f8fe0
+      CORE_ADDR mask = CORE_ADDR_MAX;
c5f8fe0
+      CORE_ADDR displacement;
c5f8fe0
 
c5f8fe0
       scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
c5f8fe0
 		   write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
c5f8fe0
@@ -288,12 +293,23 @@ exec_file_attach (char *filename, int from_tty)
c5f8fe0
 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
c5f8fe0
 	}
c5f8fe0
 
c5f8fe0
+      set_gdbarch_from_file (exec_bfd);
c5f8fe0
+
c5f8fe0
+      addr_bit = gdbarch_addr_bit (target_gdbarch);
c5f8fe0
+      if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
c5f8fe0
+	mask = ((CORE_ADDR) 1 << addr_bit) - 1;
c5f8fe0
+
c5f8fe0
+      displacement = solib_exec_displacement ();
c5f8fe0
+      for (p = sections; p < sections_end; p++)
c5f8fe0
+	{
c5f8fe0
+	  p->addr = (p->addr + displacement) & mask;
c5f8fe0
+	  p->endaddr = (p->endaddr + displacement) & mask;
c5f8fe0
+	}
c5f8fe0
+
c5f8fe0
       exec_bfd_mtime = bfd_get_mtime (exec_bfd);
c5f8fe0
 
c5f8fe0
       validate_files ();
c5f8fe0
 
c5f8fe0
-      set_gdbarch_from_file (exec_bfd);
c5f8fe0
-
c5f8fe0
       /* Add the executable's sections to the current address spaces'
c5f8fe0
 	 list of sections.  */
c5f8fe0
       add_target_sections (sections, sections_end);
c5f8fe0
--- a/gdb/gdbtypes.h
c5f8fe0
+++ b/gdb/gdbtypes.h
c5f8fe0
@@ -966,6 +966,7 @@ extern void allocate_cplus_struct_type (struct type *);
c5f8fe0
 #define FIELD_LOC_KIND(thisfld) ((thisfld).loc_kind)
c5f8fe0
 #define FIELD_BITPOS(thisfld) ((thisfld).loc.bitpos)
c5f8fe0
 #define FIELD_STATIC_PHYSNAME(thisfld) ((thisfld).loc.physname)
c5f8fe0
+/* This address is unrelocated by the objfile's ANOFFSET.  */
c5f8fe0
 #define FIELD_STATIC_PHYSADDR(thisfld) ((thisfld).loc.physaddr)
c5f8fe0
 #define FIELD_DWARF_BLOCK(thisfld) ((thisfld).loc.dwarf_block)
c5f8fe0
 #define SET_FIELD_BITPOS(thisfld, bitpos)			\
c5f8fe0
@@ -974,6 +975,7 @@ extern void allocate_cplus_struct_type (struct type *);
c5f8fe0
 #define SET_FIELD_PHYSNAME(thisfld, name)			\
c5f8fe0
   (FIELD_LOC_KIND (thisfld) = FIELD_LOC_KIND_PHYSNAME,		\
c5f8fe0
    FIELD_STATIC_PHYSNAME (thisfld) = (name))
c5f8fe0
+/* This address is unrelocated by the objfile's ANOFFSET.  */
c5f8fe0
 #define SET_FIELD_PHYSADDR(thisfld, addr)			\
c5f8fe0
   (FIELD_LOC_KIND (thisfld) = FIELD_LOC_KIND_PHYSADDR,		\
c5f8fe0
    FIELD_STATIC_PHYSADDR (thisfld) = (addr))
c5f8fe0
@@ -989,6 +991,7 @@ extern void allocate_cplus_struct_type (struct type *);
c5f8fe0
 #define TYPE_FIELD_LOC_KIND(thistype, n) FIELD_LOC_KIND (TYPE_FIELD (thistype, n))
c5f8fe0
 #define TYPE_FIELD_BITPOS(thistype, n) FIELD_BITPOS (TYPE_FIELD (thistype, n))
c5f8fe0
 #define TYPE_FIELD_STATIC_PHYSNAME(thistype, n) FIELD_STATIC_PHYSNAME (TYPE_FIELD (thistype, n))
c5f8fe0
+/* This address is unrelocated by the objfile's ANOFFSET.  */
c5f8fe0
 #define TYPE_FIELD_STATIC_PHYSADDR(thistype, n) FIELD_STATIC_PHYSADDR (TYPE_FIELD (thistype, n))
c5f8fe0
 #define TYPE_FIELD_DWARF_BLOCK(thistype, n) FIELD_DWARF_BLOCK (TYPE_FIELD (thistype, n))
c5f8fe0
 #define TYPE_FIELD_ARTIFICIAL(thistype, n) FIELD_ARTIFICIAL(TYPE_FIELD(thistype,n))
c5f8fe0
--- a/gdb/jv-lang.c
c5f8fe0
+++ b/gdb/jv-lang.c
c5f8fe0
@@ -416,7 +416,8 @@ java_link_class_type (struct gdbarch *gdbarch,
c5f8fe0
 
c5f8fe0
   fields = NULL;
c5f8fe0
   nfields--;			/* First set up dummy "class" field. */
c5f8fe0
-  SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas));
c5f8fe0
+  SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields), value_address (clas)
c5f8fe0
+    - (TYPE_OBJFILE (type) == NULL ? 0 : ANOFFSET (TYPE_OBJFILE (type)->section_offsets, SECT_OFF_TEXT (TYPE_OBJFILE (type)))));
c5f8fe0
   TYPE_FIELD_NAME (type, nfields) = "class";
c5f8fe0
   TYPE_FIELD_TYPE (type, nfields) = value_type (clas);
c5f8fe0
   SET_TYPE_FIELD_PRIVATE (type, nfields);
c5f8fe0
@@ -462,7 +463,8 @@ java_link_class_type (struct gdbarch *gdbarch,
c5f8fe0
 	  SET_TYPE_FIELD_PROTECTED (type, i);
c5f8fe0
 	}
c5f8fe0
       if (accflags & 0x0008)	/* ACC_STATIC */
c5f8fe0
-	SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset);
c5f8fe0
+	SET_FIELD_PHYSADDR (TYPE_FIELD (type, i), boffset
c5f8fe0
+	  - (TYPE_OBJFILE (type) == NULL ? 0 : ANOFFSET (TYPE_OBJFILE (type)->section_offsets, SECT_OFF_TEXT (TYPE_OBJFILE (type)))));
c5f8fe0
       else
c5f8fe0
 	TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
c5f8fe0
       if (accflags & 0x8000)	/* FIELD_UNRESOLVED_FLAG */
c5f8fe0
--- a/gdb/solib-svr4.c
c5f8fe0
+++ b/gdb/solib-svr4.c
059aafd
@@ -1672,15 +1672,20 @@ static CORE_ADDR
059aafd
 svr4_exec_displacement (void)
059aafd
 {
059aafd
   int found;
059aafd
+  /* ENTRY_POINT is a possible function descriptor - before
059aafd
+     a call to gdbarch_convert_from_func_ptr_addr.  */
059aafd
   CORE_ADDR entry_point;
059aafd
 
059aafd
   if (exec_bfd == NULL)
059aafd
     return 0;
059aafd
 
c5f8fe0
   if (target_auxv_search (&current_target, AT_ENTRY, &entry_point) == 1)
059aafd
-    return entry_point - exec_entry_point (exec_bfd, &current_target);
059aafd
+    return entry_point - bfd_get_start_address (exec_bfd);
c5f8fe0
 
c5f8fe0
-  return svr4_static_exec_displacement ();
c5f8fe0
+  if (!ptid_equal (inferior_ptid, null_ptid))
c5f8fe0
+    return svr4_static_exec_displacement ();
c5f8fe0
+
c5f8fe0
+  return 0;
c5f8fe0
 }
c5f8fe0
 
c5f8fe0
 /* Relocate the main executable.  This function should be called upon
c5f8fe0
@@ -1632,7 +1635,7 @@ svr4_exec_displacement (void)
c5f8fe0
 static void
c5f8fe0
 svr4_relocate_main_executable (void)
c5f8fe0
 {
c5f8fe0
-  CORE_ADDR displacement = svr4_exec_displacement ();
c5f8fe0
+  CORE_ADDR displacement = solib_exec_displacement ();
c5f8fe0
 
c5f8fe0
   /* Even if DISPLACEMENT is 0 still try to relocate it as this is a new
c5f8fe0
      difference of in-memory vs. in-file addresses and we could already
c5f8fe0
@@ -1975,6 +1978,7 @@ _initialize_svr4_solib (void)
c5f8fe0
   svr4_so_ops.free_so = svr4_free_so;
c5f8fe0
   svr4_so_ops.clear_solib = svr4_clear_solib;
c5f8fe0
   svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
c5f8fe0
+  svr4_so_ops.exec_displacement = svr4_exec_displacement;
c5f8fe0
   svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
c5f8fe0
   svr4_so_ops.current_sos = svr4_current_sos;
c5f8fe0
   svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
c5f8fe0
--- a/gdb/solib.c
c5f8fe0
+++ b/gdb/solib.c
c5f8fe0
@@ -1014,6 +1014,19 @@ solib_create_inferior_hook (int from_tty)
c5f8fe0
   ops->solib_create_inferior_hook (from_tty);
c5f8fe0
 }
c5f8fe0
 
c5f8fe0
+/* Query the difference of in-memory VMA addresses vs. exec_bfd VMAs.  */
c5f8fe0
+
c5f8fe0
+CORE_ADDR
c5f8fe0
+solib_exec_displacement (void)
c5f8fe0
+{
c5f8fe0
+  struct target_so_ops *ops = solib_ops (target_gdbarch);
c5f8fe0
+
c5f8fe0
+  if (ops->exec_displacement != NULL)
c5f8fe0
+    return (*ops->exec_displacement) ();
c5f8fe0
+  else
c5f8fe0
+    return 0;
c5f8fe0
+}
c5f8fe0
+
c5f8fe0
 /* GLOBAL FUNCTION
c5f8fe0
 
c5f8fe0
    in_solib_dynsym_resolve_code -- check to see if an address is in
c5f8fe0
--- a/gdb/solib.h
c5f8fe0
+++ b/gdb/solib.h
c5f8fe0
@@ -43,6 +43,8 @@ extern int solib_read_symbols (struct so_list *, int);
c5f8fe0
 
c5f8fe0
 extern void solib_create_inferior_hook (int from_tty);
c5f8fe0
 
c5f8fe0
+extern CORE_ADDR solib_exec_displacement (void);
c5f8fe0
+
c5f8fe0
 /* If ADDR lies in a shared library, return its name.  */
c5f8fe0
 
c5f8fe0
 extern char *solib_name_from_address (CORE_ADDR);
c5f8fe0
--- a/gdb/solist.h
c5f8fe0
+++ b/gdb/solist.h
c5f8fe0
@@ -89,6 +89,9 @@ struct target_so_ops
c5f8fe0
     /* Target dependent code to run after child process fork.  */
c5f8fe0
     void (*solib_create_inferior_hook) (int from_tty);
c5f8fe0
 
c5f8fe0
+    /* Query the difference of in-memory VMA addresses vs. exec_bfd VMAs.  */
c5f8fe0
+    CORE_ADDR (*exec_displacement) (void);
c5f8fe0
+
c5f8fe0
     /* Do additional symbol handling, lookup, etc. after symbols
c5f8fe0
        for a shared object have been loaded.  */
c5f8fe0
     void (*special_symbol_handling) (void);
c5f8fe0
--- a/gdb/symfile.c
c5f8fe0
+++ b/gdb/symfile.c
c5f8fe0
@@ -873,15 +873,36 @@ syms_from_objfile (struct objfile *objfile,
c5f8fe0
      if an error occurs during symbol reading.  */
c5f8fe0
   old_chain = make_cleanup_free_objfile (objfile);
c5f8fe0
 
c5f8fe0
-  /* If ADDRS and OFFSETS are both NULL, put together a dummy address
c5f8fe0
-     list.  We now establish the convention that an addr of zero means
c5f8fe0
-     no load address was specified. */
c5f8fe0
+  /* If ADDRS and OFFSETS are both NULL, put together a dummy offset list.  */
c5f8fe0
+
c5f8fe0
   if (! addrs && ! offsets)
c5f8fe0
     {
c5f8fe0
-      local_addr
c5f8fe0
-	= alloc_section_addr_info (bfd_count_sections (objfile->obfd));
c5f8fe0
-      make_cleanup (xfree, local_addr);
c5f8fe0
-      addrs = local_addr;
c5f8fe0
+      /* Relocateble files have an exception in default_symfile_offsets which
c5f8fe0
+	 applies only for ADDRS.  But calling solib_exec_displacement is more
c5f8fe0
+	 suitable for OFFSETS.  Fortunately we never need the both
c5f8fe0
+	 functionalities simultaneously and in other cases zeroed ADDRS and
c5f8fe0
+	 zeroed OFFSETS are equivalent.  */
c5f8fe0
+
c5f8fe0
+      if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
c5f8fe0
+	{
c5f8fe0
+	  local_addr
c5f8fe0
+		 = alloc_section_addr_info (bfd_count_sections (objfile->obfd));
c5f8fe0
+	  make_cleanup (xfree, local_addr);
c5f8fe0
+	  addrs = local_addr;
c5f8fe0
+	}
c5f8fe0
+      else
c5f8fe0
+	{
c5f8fe0
+	  CORE_ADDR displacement = 0;
c5f8fe0
+	  int i;
c5f8fe0
+
c5f8fe0
+	  if (mainline)
c5f8fe0
+	    displacement = solib_exec_displacement ();
c5f8fe0
+
c5f8fe0
+	  num_offsets = bfd_count_sections (objfile->obfd);
c5f8fe0
+	  offsets = alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets));
c5f8fe0
+	  for (i = 0; i < num_offsets; i++)
c5f8fe0
+	    offsets->offsets[i] = displacement;
c5f8fe0
+	}
c5f8fe0
     }
c5f8fe0
 
c5f8fe0
   /* Now either addrs or offsets is non-zero.  */
c5f8fe0
--- a/gdb/value.c
c5f8fe0
+++ b/gdb/value.c
c5f8fe0
@@ -1890,7 +1890,8 @@ value_static_field (struct type *type, int fieldno)
c5f8fe0
   if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
c5f8fe0
     {
c5f8fe0
       retval = value_at (TYPE_FIELD_TYPE (type, fieldno),
c5f8fe0
-			 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
c5f8fe0
+			 TYPE_FIELD_STATIC_PHYSADDR (type, fieldno)
c5f8fe0
+			   + (TYPE_OBJFILE (type) == NULL ? 0 : ANOFFSET (TYPE_OBJFILE (type)->section_offsets, SECT_OFF_TEXT (TYPE_OBJFILE (type)))));
c5f8fe0
     }
c5f8fe0
   else
c5f8fe0
     {
c5f8fe0
@@ -1920,7 +1921,8 @@ value_static_field (struct type *type, int fieldno)
c5f8fe0
  	}
c5f8fe0
       if (retval && VALUE_LVAL (retval) == lval_memory)
c5f8fe0
 	SET_FIELD_PHYSADDR (TYPE_FIELD (type, fieldno),
c5f8fe0
-			    value_address (retval));
c5f8fe0
+			    value_address (retval)
c5f8fe0
+			      - (TYPE_OBJFILE (type) == NULL ? 0 : ANOFFSET (TYPE_OBJFILE (type)->section_offsets, SECT_OFF_TEXT (TYPE_OBJFILE (type)))));
c5f8fe0
     }
c5f8fe0
   return retval;
c5f8fe0
 }
367485a
--- gdb-7.0/gdb/testsuite/gdb.base/valgrind-db-attach.exp-orig	2009-12-21 16:50:42.000000000 +0100
367485a
+++ gdb-7.0/gdb/testsuite/gdb.base/valgrind-db-attach.exp	2009-12-21 16:51:24.000000000 +0100
367485a
@@ -67,6 +67,8 @@ gdb_test_multiple "" $test {
367485a
     }
367485a
 }
367485a
 
367485a
+gdb_test "" "" "eat first prompt"
367485a
+
367485a
 # Initialization from default_gdb_start.
367485a
 gdb_test "set height 0"
367485a
 gdb_test "set width 0"