gnulib: define the path to gnulib's parent dir
[binutils-gdb.git] / gdb / solib-svr4.c
index 637a0fcfac41162c9cb7cef6345b04e010957125..a8a7d1171dc6e4b39f88f364d59231b97cfa10bc 100644 (file)
@@ -1,6 +1,6 @@
 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
 
-   Copyright (C) 1990-2020 Free Software Foundation, Inc.
+   Copyright (C) 1990-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -428,11 +428,14 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
   int pt_phdr_p = 0;
 
   /* Get required auxv elements from target.  */
-  if (target_auxv_search (current_top_target (), AT_PHDR, &at_phdr) <= 0)
+  if (target_auxv_search (current_inferior ()->top_target (),
+                         AT_PHDR, &at_phdr) <= 0)
     return {};
-  if (target_auxv_search (current_top_target (), AT_PHENT, &at_phent) <= 0)
+  if (target_auxv_search (current_inferior ()->top_target (),
+                         AT_PHENT, &at_phent) <= 0)
     return {};
-  if (target_auxv_search (current_top_target (), AT_PHNUM, &at_phnum) <= 0)
+  if (target_auxv_search (current_inferior ()->top_target (),
+                         AT_PHNUM, &at_phnum) <= 0)
     return {};
   if (!at_phdr || !at_phnum)
     return {};
@@ -610,7 +613,8 @@ scan_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr,
     return 0;
 
   bool found = false;
-  for (target_section &target_section : current_program_space->target_sections)
+  for (const target_section &target_section
+        : current_program_space->target_sections ())
     if (sect == target_section.the_bfd_section)
       {
        dyn_addr = target_section.addr;
@@ -806,7 +810,8 @@ elf_locate_base (void)
 
   /* This may be a static executable.  Look for the symbol
      conventionally named _r_debug, as a last resort.  */
-  msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
+  msymbol = lookup_minimal_symbol ("_r_debug", NULL,
+                                  current_program_space->symfile_object_file);
   if (msymbol.minsym != NULL)
     return BMSYMBOL_VALUE_ADDRESS (msymbol);
 
@@ -871,7 +876,7 @@ solib_svr4_r_map (struct svr4_info *info)
   try
     {
       addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset,
-                                        ptr_type);
+                                       ptr_type);
     }
   catch (const gdb_exception_error &ex)
     {
@@ -971,7 +976,7 @@ open_symbol_file_object (int from_tty)
   if (from_tty)
     add_flags |= SYMFILE_VERBOSE;
 
-  if (symfile_objfile)
+  if (current_program_space->symfile_object_file)
     if (!query (_("Attempt to reload symbols from process? ")))
       return 0;
 
@@ -1237,7 +1242,8 @@ svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
 
   /* Fetch the list of shared libraries.  */
   gdb::optional<gdb::char_vector> svr4_library_document
-    = target_read_stralloc (current_top_target (), TARGET_OBJECT_LIBRARIES_SVR4,
+    = target_read_stralloc (current_inferior ()->top_target (),
+                           TARGET_OBJECT_LIBRARIES_SVR4,
                            annex);
   if (!svr4_library_document)
     return 0;
@@ -1317,10 +1323,10 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
        }
 
       /* For SVR4 versions, the first entry in the link map is for the
-         inferior executable, so we must ignore it.  For some versions of
-         SVR4, it has no name.  For others (Solaris 2.3 for example), it
-         does have a name, so we can no longer use a missing name to
-         decide when to ignore it.  */
+        inferior executable, so we must ignore it.  For some versions of
+        SVR4, it has no name.  For others (Solaris 2.3 for example), it
+        does have a name, so we can no longer use a missing name to
+        decide when to ignore it.  */
       if (ignore_first && li->l_prev == 0)
        {
          first_l_name = li->l_name;
@@ -1544,7 +1550,7 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
     solib_add (NULL, 0, auto_solib_add);
 
   /* svr4_current_sos() will set main_lm_addr for the main executable.  */
-  if (objfile == symfile_objfile)
+  if (objfile == current_program_space->symfile_object_file)
     return info->main_lm_addr;
 
   /* If OBJFILE is a separate debug object file, look for the
@@ -2013,15 +2019,13 @@ svr4_handle_solib_event (void)
 static bool
 svr4_update_solib_event_breakpoint (struct breakpoint *b)
 {
-  struct bp_location *loc;
-
   if (b->type != bp_shlib_event)
     {
       /* Continue iterating.  */
       return false;
     }
 
-  for (loc = b->loc; loc != NULL; loc = loc->next)
+  for (bp_location *loc : b->locations ())
     {
       struct svr4_info *info;
       struct probe_and_action *pa;
@@ -2055,7 +2059,8 @@ svr4_update_solib_event_breakpoint (struct breakpoint *b)
 static void
 svr4_update_solib_event_breakpoints (void)
 {
-  iterate_over_breakpoints (svr4_update_solib_event_breakpoint);
+  for (breakpoint *bp : all_breakpoints_safe ())
+    svr4_update_solib_event_breakpoint (bp);
 }
 
 /* Create and register solib event breakpoints.  PROBES is an array
@@ -2240,9 +2245,8 @@ enable_break (struct svr4_info *info, int from_tty)
 
       sym_addr = gdbarch_addr_bits_remove
        (target_gdbarch (),
-        gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
-                                            sym_addr,
-                                            current_top_target ()));
+        gdbarch_convert_from_func_ptr_addr
+          (target_gdbarch (), sym_addr, current_inferior ()->top_target ()));
 
       /* On at least some versions of Solaris there's a dynamic relocation
         on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
@@ -2311,17 +2315,17 @@ enable_break (struct svr4_info *info, int from_tty)
       sym_addr = 0;
 
       /* Now we need to figure out where the dynamic linker was
-         loaded so that we can load its symbols and place a breakpoint
-         in the dynamic linker itself.
+        loaded so that we can load its symbols and place a breakpoint
+        in the dynamic linker itself.
 
-         This address is stored on the stack.  However, I've been unable
-         to find any magic formula to find it for Solaris (appears to
-         be trivial on GNU/Linux).  Therefore, we have to try an alternate
-         mechanism to find the dynamic linker's base address.  */
+        This address is stored on the stack.  However, I've been unable
+        to find any magic formula to find it for Solaris (appears to
+        be trivial on GNU/Linux).  Therefore, we have to try an alternate
+        mechanism to find the dynamic linker's base address.  */
 
       gdb_bfd_ref_ptr tmp_bfd;
       try
-        {
+       {
          tmp_bfd = solib_bfd_open (interp_name);
        }
       catch (const gdb_exception &ex)
@@ -2332,12 +2336,11 @@ enable_break (struct svr4_info *info, int from_tty)
        goto bkpt_at_symbol;
 
       /* Now convert the TMP_BFD into a target.  That way target, as
-         well as BFD operations can be used.  target_bfd_reopen
-         acquires its own reference.  */
-      tmp_bfd_target = target_bfd_reopen (tmp_bfd.get ());
+        well as BFD operations can be used.  */
+      tmp_bfd_target = target_bfd_reopen (tmp_bfd);
 
       /* On a running target, we can get the dynamic linker's base
-         address from the shared library table.  */
+        address from the shared library table.  */
       for (struct so_list *so : current_program_space->solibs ())
        {
          if (svr4_same_1 (interp_name, so->so_original_name))
@@ -2350,9 +2353,10 @@ enable_break (struct svr4_info *info, int from_tty)
        }
 
       /* If we were not able to find the base address of the loader
-         from our so_list, then try using the AT_BASE auxilliary entry.  */
+        from our so_list, then try using the AT_BASE auxilliary entry.  */
       if (!load_addr_found)
-       if (target_auxv_search (current_top_target (), AT_BASE, &load_addr) > 0)
+       if (target_auxv_search (current_inferior ()->top_target (),
+                               AT_BASE, &load_addr) > 0)
          {
            int addr_bit = gdbarch_addr_bit (target_gdbarch ());
 
@@ -2384,9 +2388,9 @@ enable_break (struct svr4_info *info, int from_tty)
         the current pc (which should point at the entry point for the
         dynamic linker) and subtracting the offset of the entry point.
 
-         This is more fragile than the previous approaches, but is a good
-         fallback method because it has actually been working well in
-         most cases.  */
+        This is more fragile than the previous approaches, but is a good
+        fallback method because it has actually been working well in
+        most cases.  */
       if (!load_addr_found)
        {
          struct regcache *regcache
@@ -2406,7 +2410,7 @@ enable_break (struct svr4_info *info, int from_tty)
        }
 
       /* Record the relocated start and end address of the dynamic linker
-         text and plt section for svr4_in_dynsym_resolve_code.  */
+        text and plt section for svr4_in_dynsym_resolve_code.  */
       interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
       if (interp_sect)
        {
@@ -2443,8 +2447,8 @@ enable_break (struct svr4_info *info, int from_tty)
                                                       tmp_bfd_target);
 
       /* We're done with both the temporary bfd and target.  Closing
-         the target closes the underlying bfd, because it holds the
-         only remaining reference.  */
+        the target closes the underlying bfd, because it holds the
+        only remaining reference.  */
       target_close (tmp_bfd_target);
 
       if (sym_addr != 0)
@@ -2455,26 +2459,26 @@ enable_break (struct svr4_info *info, int from_tty)
        }
 
       /* For whatever reason we couldn't set a breakpoint in the dynamic
-         linker.  Warn and drop into the old code.  */
+        linker.  Warn and drop into the old code.  */
     bkpt_at_symbol:
       warning (_("Unable to find dynamic linker breakpoint function.\n"
-               "GDB will be unable to debug shared library initializers\n"
-               "and track explicitly loaded dynamic code."));
+              "GDB will be unable to debug shared library initializers\n"
+              "and track explicitly loaded dynamic code."));
     }
 
   /* Scan through the lists of symbols, trying to look up the symbol and
      set a breakpoint there.  Terminate loop when we/if we succeed.  */
 
+  objfile *objf = current_program_space->symfile_object_file;
   for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
     {
-      msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
+      msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
       if ((msymbol.minsym != NULL)
          && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
        {
          sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
-         sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
-                                                        sym_addr,
-                                                        current_top_target ());
+         sym_addr = gdbarch_convert_from_func_ptr_addr
+           (target_gdbarch (), sym_addr, current_inferior ()->top_target ());
          svr4_create_solib_event_breakpoints (info, target_gdbarch (),
                                               sym_addr);
          return 1;
@@ -2485,14 +2489,14 @@ enable_break (struct svr4_info *info, int from_tty)
     {
       for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
        {
-         msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
+         msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
          if ((msymbol.minsym != NULL)
              && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
            {
              sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
-             sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
-                                                            sym_addr,
-                                                            current_top_target ());
+             sym_addr = gdbarch_convert_from_func_ptr_addr
+               (target_gdbarch (), sym_addr,
+                current_inferior ()->top_target ());
              svr4_create_solib_event_breakpoints (info, target_gdbarch (),
                                                   sym_addr);
              return 1;
@@ -2580,7 +2584,8 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
   if ((bfd_get_file_flags (current_program_space->exec_bfd ()) & DYNAMIC) == 0)
     return 0;
 
-  if (target_auxv_search (current_top_target (), AT_ENTRY, &entry_point) <= 0)
+  if (target_auxv_search (current_inferior ()->top_target (),
+                         AT_ENTRY, &entry_point) <= 0)
     return 0;
 
   exec_displacement
@@ -2643,7 +2648,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
            return 0;
          else if (arch_size == 32
                   && phdrs_target->size () >= sizeof (Elf32_External_Phdr)
-                  && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
+                  && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
            {
              Elf_Internal_Ehdr *ehdr2
                = elf_tdata (current_program_space->exec_bfd ())->elf_header;
@@ -2685,7 +2690,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
                  }
 
              /* Now compare program headers from the target and the binary
-                with optional DISPLACEMENT.  */
+                with optional DISPLACEMENT.  */
 
              for (i = 0;
                   i < phdrs_target->size () / sizeof (Elf32_External_Phdr);
@@ -2781,7 +2786,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
            }
          else if (arch_size == 64
                   && phdrs_target->size () >= sizeof (Elf64_External_Phdr)
-                  && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
+                  && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
            {
              Elf_Internal_Ehdr *ehdr2
                = elf_tdata (current_program_space->exec_bfd ())->elf_header;
@@ -2981,11 +2986,12 @@ svr4_relocate_main_executable (void)
   /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
      addresses.  */
 
-  if (symfile_objfile)
+  objfile *objf = current_program_space->symfile_object_file;
+  if (objf)
     {
-      section_offsets new_offsets (symfile_objfile->section_offsets.size (),
+      section_offsets new_offsets (objf->section_offsets.size (),
                                   displacement);
-      objfile_relocate (symfile_objfile, new_offsets);
+      objfile_relocate (objf, new_offsets);
     }
   else if (current_program_space->exec_bfd ())
     {
@@ -3083,7 +3089,7 @@ svr4_truncate_ptr (CORE_ADDR addr)
 
 static void
 svr4_relocate_section_addresses (struct so_list *so,
-                                 struct target_section *sec)
+                                struct target_section *sec)
 {
   bfd *abfd = sec->the_bfd_section->owner;
 
@@ -3120,7 +3126,7 @@ solib_svr4_init (struct obstack *obstack)
 
 void
 set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
-                                       struct link_map_offsets *(*flmo) (void))
+                                      struct link_map_offsets *(*flmo) (void))
 {
   struct solib_svr4_ops *ops
     = (struct solib_svr4_ops *) gdbarch_data (gdbarch, solib_svr4_data);
@@ -3244,9 +3250,9 @@ svr4_iterate_over_objfiles_in_search_order
       bfd *abfd;
 
       if (current_objfile->separate_debug_objfile_backlink != nullptr)
-        current_objfile = current_objfile->separate_debug_objfile_backlink;
+       current_objfile = current_objfile->separate_debug_objfile_backlink;
 
-      if (current_objfile == symfile_objfile)
+      if (current_objfile == current_program_space->symfile_object_file)
        abfd = current_program_space->exec_bfd ();
       else
        abfd = current_objfile->obfd;
@@ -3289,5 +3295,6 @@ _initialize_svr4_solib ()
   svr4_so_ops.update_breakpoints = svr4_update_solib_event_breakpoints;
   svr4_so_ops.handle_event = svr4_handle_solib_event;
 
-  gdb::observers::free_objfile.attach (svr4_free_objfile_observer);
+  gdb::observers::free_objfile.attach (svr4_free_objfile_observer,
+                                      "solib-svr4");
 }