+/* Lookup the value for a specific symbol from dynamic symbol table. Look
+ up symbol from ABFD. MATCH_SYM is a callback function to determine
+ whether to pick up a symbol. DATA is the input of this callback
+ function. Return NULL if symbol is not found. */
+
+CORE_ADDR
+gdb_bfd_lookup_symbol_from_symtab (bfd *abfd,
+ int (*match_sym) (const asymbol *,
+ const void *),
+ const void *data)
+{
+ long storage_needed = bfd_get_symtab_upper_bound (abfd);
+ CORE_ADDR symaddr = 0;
+
+ if (storage_needed > 0)
+ {
+ unsigned int i;
+
+ asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
+ struct cleanup *back_to = make_cleanup (xfree, symbol_table);
+ unsigned int number_of_symbols =
+ bfd_canonicalize_symtab (abfd, symbol_table);
+
+ for (i = 0; i < number_of_symbols; i++)
+ {
+ asymbol *sym = *symbol_table++;
+
+ if (match_sym (sym, data))
+ {
+ struct gdbarch *gdbarch = target_gdbarch ();
+ symaddr = sym->value;
+
+ /* Some ELF targets fiddle with addresses of symbols they
+ consider special. They use minimal symbols to do that
+ and this is needed for correct breakpoint placement,
+ but we do not have full data here to build a complete
+ minimal symbol, so just set the address and let the
+ targets cope with that. */
+ if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+ && gdbarch_elf_make_msymbol_special_p (gdbarch))
+ {
+ struct minimal_symbol msym;
+
+ memset (&msym, 0, sizeof (msym));
+ SET_MSYMBOL_VALUE_ADDRESS (&msym, symaddr);
+ gdbarch_elf_make_msymbol_special (gdbarch, sym, &msym);
+ symaddr = MSYMBOL_VALUE_RAW_ADDRESS (&msym);
+ }
+
+ /* BFD symbols are section relative. */
+ symaddr += sym->section->vma;
+ break;
+ }
+ }
+ do_cleanups (back_to);
+ }
+
+ return symaddr;
+}
+
+/* Lookup the value for a specific symbol from symbol table. Look up symbol
+ from ABFD. MATCH_SYM is a callback function to determine whether to pick
+ up a symbol. DATA is the input of this callback function. Return NULL
+ if symbol is not found. */
+
+static CORE_ADDR
+bfd_lookup_symbol_from_dyn_symtab (bfd *abfd,
+ int (*match_sym) (const asymbol *,
+ const void *),
+ const void *data)
+{
+ long storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
+ CORE_ADDR symaddr = 0;
+
+ if (storage_needed > 0)
+ {
+ unsigned int i;
+ asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
+ struct cleanup *back_to = make_cleanup (xfree, symbol_table);
+ unsigned int number_of_symbols =
+ bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
+
+ for (i = 0; i < number_of_symbols; i++)
+ {
+ asymbol *sym = *symbol_table++;
+
+ if (match_sym (sym, data))
+ {
+ /* BFD symbols are section relative. */
+ symaddr = sym->value + sym->section->vma;
+ break;
+ }
+ }
+ do_cleanups (back_to);
+ }
+ return symaddr;
+}
+
+/* Lookup the value for a specific symbol from symbol table and dynamic
+ symbol table. Look up symbol from ABFD. MATCH_SYM is a callback
+ function to determine whether to pick up a symbol. DATA is the
+ input of this callback function. Return NULL if symbol is not
+ found. */
+
+CORE_ADDR
+gdb_bfd_lookup_symbol (bfd *abfd,
+ int (*match_sym) (const asymbol *, const void *),
+ const void *data)
+{
+ CORE_ADDR symaddr = gdb_bfd_lookup_symbol_from_symtab (abfd, match_sym, data);
+
+ /* On FreeBSD, the dynamic linker is stripped by default. So we'll
+ have to check the dynamic string table too. */
+ if (symaddr == 0)
+ symaddr = bfd_lookup_symbol_from_dyn_symtab (abfd, match_sym, data);
+
+ return symaddr;
+}
+
+/* SO_LIST_HEAD may contain user-loaded object files that can be removed
+ out-of-band by the user. So upon notification of free_objfile remove
+ all references to any user-loaded file that is about to be freed. */
+
+static void
+remove_user_added_objfile (struct objfile *objfile)
+{
+ struct so_list *so;
+
+ if (objfile != 0 && objfile->flags & OBJF_USERLOADED)
+ {
+ for (so = so_list_head; so != NULL; so = so->next)
+ if (so->objfile == objfile)
+ so->objfile = NULL;
+ }
+}