198948Sobrien/* Handle shared libraries for GDB, the GNU Debugger. 219370Spst 3130809Smarcel Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 4130809Smarcel 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. 5130809Smarcel 698948Sobrien This file is part of GDB. 719370Spst 898948Sobrien This program is free software; you can redistribute it and/or modify 998948Sobrien it under the terms of the GNU General Public License as published by 1098948Sobrien the Free Software Foundation; either version 2 of the License, or 1198948Sobrien (at your option) any later version. 1219370Spst 1398948Sobrien This program is distributed in the hope that it will be useful, 1498948Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1598948Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1698948Sobrien GNU General Public License for more details. 1719370Spst 1898948Sobrien You should have received a copy of the GNU General Public License 1998948Sobrien along with this program; if not, write to the Free Software 2098948Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2198948Sobrien Boston, MA 02111-1307, USA. */ 2219370Spst 2319370Spst#include "defs.h" 2419370Spst 2519370Spst#include <sys/types.h> 2698948Sobrien#include <fcntl.h> 2719370Spst#include "gdb_string.h" 2819370Spst#include "symtab.h" 2919370Spst#include "bfd.h" 3019370Spst#include "symfile.h" 3119370Spst#include "objfiles.h" 3219370Spst#include "gdbcore.h" 3319370Spst#include "command.h" 3419370Spst#include "target.h" 3519370Spst#include "frame.h" 3698948Sobrien#include "gdb_regex.h" 3719370Spst#include "inferior.h" 3819370Spst#include "environ.h" 3919370Spst#include "language.h" 4019370Spst#include "gdbcmd.h" 4198948Sobrien#include "completer.h" 4298948Sobrien#include "filenames.h" /* for DOSish file names */ 43130809Smarcel#include "exec.h" 4498948Sobrien#include "solist.h" 45130809Smarcel#include "readline/readline.h" 4619370Spst 4798948Sobrien/* external data declarations */ 4819370Spst 4998948Sobrien/* FIXME: gdbarch needs to control this variable */ 5098948Sobrienstruct target_so_ops *current_target_so_ops; 5119370Spst 5219370Spst/* local data declarations */ 5319370Spst 5498948Sobrienstatic struct so_list *so_list_head; /* List of known shared objects */ 5519370Spst 5698948Sobrienstatic int solib_cleanup_queued = 0; /* make_run_cleanup called */ 5719370Spst 5898948Sobrien/* Local function prototypes */ 5919370Spst 60130809Smarcelstatic void do_clear_solib (void *); 6119370Spst 6298948Sobrien/* If non-zero, this is a prefix that will be added to the front of the name 6398948Sobrien shared libraries with an absolute filename for loading. */ 6498948Sobrienstatic char *solib_absolute_prefix = NULL; 6519370Spst 6698948Sobrien/* If non-empty, this is a search path for loading non-absolute shared library 6798948Sobrien symbol files. This takes precedence over the environment variables PATH 6898948Sobrien and LD_LIBRARY_PATH. */ 6998948Sobrienstatic char *solib_search_path = NULL; 7019370Spst 7198948Sobrien/* 7219370Spst 7398948Sobrien GLOBAL FUNCTION 7446289Sdfr 7598948Sobrien solib_open -- Find a shared library file and open it. 7619370Spst 7798948Sobrien SYNOPSIS 7819370Spst 7998948Sobrien int solib_open (char *in_patname, char **found_pathname); 8046289Sdfr 8198948Sobrien DESCRIPTION 8246289Sdfr 8398948Sobrien Global variable SOLIB_ABSOLUTE_PREFIX is used as a prefix directory 8498948Sobrien to search for shared libraries if they have an absolute path. 8519370Spst 8698948Sobrien Global variable SOLIB_SEARCH_PATH is used as a prefix directory 8798948Sobrien (or set of directories, as in LD_LIBRARY_PATH) to search for all 8898948Sobrien shared libraries if not found in SOLIB_ABSOLUTE_PREFIX. 8919370Spst 90130809Smarcel Search algorithm: 91130809Smarcel * If there is a solib_absolute_prefix and path is absolute: 92130809Smarcel * Search for solib_absolute_prefix/path. 93130809Smarcel * else 94130809Smarcel * Look for it literally (unmodified). 9598948Sobrien * Look in SOLIB_SEARCH_PATH. 96130809Smarcel * If available, use target defined search function. 97130809Smarcel * If solib_absolute_prefix is NOT set, perform the following two searches: 98130809Smarcel * Look in inferior's $PATH. 99130809Smarcel * Look in inferior's $LD_LIBRARY_PATH. 100130809Smarcel * 101130809Smarcel * The last check avoids doing this search when targetting remote 102130809Smarcel * machines since solib_absolute_prefix will almost always be set. 10319370Spst 10498948Sobrien RETURNS 10519370Spst 10698948Sobrien file handle for opened solib, or -1 for failure. */ 10719370Spst 10898948Sobrienint 10998948Sobriensolib_open (char *in_pathname, char **found_pathname) 11098948Sobrien{ 11198948Sobrien int found_file = -1; 11298948Sobrien char *temp_pathname = NULL; 11398948Sobrien char *p = in_pathname; 11419370Spst 11598948Sobrien while (*p && !IS_DIR_SEPARATOR (*p)) 11698948Sobrien p++; 11739645Sdfr 11898948Sobrien if (*p) 11998948Sobrien { 12098948Sobrien if (! IS_ABSOLUTE_PATH (in_pathname) || solib_absolute_prefix == NULL) 12198948Sobrien temp_pathname = in_pathname; 12298948Sobrien else 12398948Sobrien { 12498948Sobrien int prefix_len = strlen (solib_absolute_prefix); 12539645Sdfr 12698948Sobrien /* Remove trailing slashes from absolute prefix. */ 12798948Sobrien while (prefix_len > 0 12898948Sobrien && IS_DIR_SEPARATOR (solib_absolute_prefix[prefix_len - 1])) 12998948Sobrien prefix_len--; 13019370Spst 13198948Sobrien /* Cat the prefixed pathname together. */ 13298948Sobrien temp_pathname = alloca (prefix_len + strlen (in_pathname) + 1); 13398948Sobrien strncpy (temp_pathname, solib_absolute_prefix, prefix_len); 13498948Sobrien temp_pathname[prefix_len] = '\0'; 13598948Sobrien strcat (temp_pathname, in_pathname); 13698948Sobrien } 13719370Spst 13898948Sobrien /* Now see if we can open it. */ 13998948Sobrien found_file = open (temp_pathname, O_RDONLY, 0); 14098948Sobrien } 14119370Spst 14298948Sobrien /* If the search in solib_absolute_prefix failed, and the path name is 14398948Sobrien absolute at this point, make it relative. (openp will try and open the 14498948Sobrien file according to its absolute path otherwise, which is not what we want.) 14598948Sobrien Affects subsequent searches for this solib. */ 14698948Sobrien if (found_file < 0 && IS_ABSOLUTE_PATH (in_pathname)) 14798948Sobrien { 14898948Sobrien /* First, get rid of any drive letters etc. */ 14998948Sobrien while (!IS_DIR_SEPARATOR (*in_pathname)) 15098948Sobrien in_pathname++; 15119370Spst 15298948Sobrien /* Next, get rid of all leading dir separators. */ 15398948Sobrien while (IS_DIR_SEPARATOR (*in_pathname)) 15498948Sobrien in_pathname++; 15598948Sobrien } 15698948Sobrien 157130809Smarcel /* If not found, search the solib_search_path (if any). */ 15898948Sobrien if (found_file < 0 && solib_search_path != NULL) 15998948Sobrien found_file = openp (solib_search_path, 16098948Sobrien 1, in_pathname, O_RDONLY, 0, &temp_pathname); 16198948Sobrien 16298948Sobrien /* If not found, next search the solib_search_path (if any) for the basename 16398948Sobrien only (ignoring the path). This is to allow reading solibs from a path 16498948Sobrien that differs from the opened path. */ 16598948Sobrien if (found_file < 0 && solib_search_path != NULL) 16698948Sobrien found_file = openp (solib_search_path, 16798948Sobrien 1, lbasename (in_pathname), O_RDONLY, 0, 16898948Sobrien &temp_pathname); 16919370Spst 170130809Smarcel /* If not found, try to use target supplied solib search method */ 171130809Smarcel if (found_file < 0 && TARGET_SO_FIND_AND_OPEN_SOLIB != NULL) 172130809Smarcel found_file = TARGET_SO_FIND_AND_OPEN_SOLIB 173130809Smarcel (in_pathname, O_RDONLY, &temp_pathname); 174130809Smarcel 17598948Sobrien /* If not found, next search the inferior's $PATH environment variable. */ 176130809Smarcel if (found_file < 0 && solib_absolute_prefix == NULL) 17798948Sobrien found_file = openp (get_in_environ (inferior_environ, "PATH"), 17898948Sobrien 1, in_pathname, O_RDONLY, 0, &temp_pathname); 17919370Spst 18098948Sobrien /* If not found, next search the inferior's $LD_LIBRARY_PATH 18198948Sobrien environment variable. */ 182130809Smarcel if (found_file < 0 && solib_absolute_prefix == NULL) 18398948Sobrien found_file = openp (get_in_environ (inferior_environ, "LD_LIBRARY_PATH"), 18498948Sobrien 1, in_pathname, O_RDONLY, 0, &temp_pathname); 18519370Spst 18698948Sobrien /* Done. If not found, tough luck. Return found_file and 18798948Sobrien (optionally) found_pathname. */ 18898948Sobrien if (found_pathname != NULL && temp_pathname != NULL) 18998948Sobrien *found_pathname = xstrdup (temp_pathname); 19098948Sobrien return found_file; 19198948Sobrien} 19219370Spst 19319370Spst 19419370Spst/* 19519370Spst 19698948Sobrien LOCAL FUNCTION 19719370Spst 19898948Sobrien solib_map_sections -- open bfd and build sections for shared lib 19919370Spst 20098948Sobrien SYNOPSIS 20119370Spst 20298948Sobrien static int solib_map_sections (struct so_list *so) 20319370Spst 20498948Sobrien DESCRIPTION 20519370Spst 20698948Sobrien Given a pointer to one of the shared objects in our list 20798948Sobrien of mapped objects, use the recorded name to open a bfd 20898948Sobrien descriptor for the object, build a section table, and then 20998948Sobrien relocate all the section addresses by the base address at 21098948Sobrien which the shared object was mapped. 21119370Spst 21298948Sobrien FIXMES 21319370Spst 21498948Sobrien In most (all?) cases the shared object file name recorded in the 21598948Sobrien dynamic linkage tables will be a fully qualified pathname. For 21698948Sobrien cases where it isn't, do we really mimic the systems search 21798948Sobrien mechanism correctly in the below code (particularly the tilde 21898948Sobrien expansion stuff?). 21919370Spst */ 22019370Spst 22146289Sdfrstatic int 222130809Smarcelsolib_map_sections (void *arg) 22319370Spst{ 22446289Sdfr struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */ 22519370Spst char *filename; 22619370Spst char *scratch_pathname; 22719370Spst int scratch_chan; 22819370Spst struct section_table *p; 22919370Spst struct cleanup *old_chain; 23019370Spst bfd *abfd; 23146289Sdfr 23298948Sobrien filename = tilde_expand (so->so_name); 23346289Sdfr 23498948Sobrien old_chain = make_cleanup (xfree, filename); 23598948Sobrien scratch_chan = solib_open (filename, &scratch_pathname); 23646289Sdfr 23719370Spst if (scratch_chan < 0) 23819370Spst { 23919370Spst perror_with_name (filename); 24019370Spst } 24198948Sobrien 24219370Spst /* Leave scratch_pathname allocated. abfd->name will point to it. */ 24319370Spst abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan); 24419370Spst if (!abfd) 24519370Spst { 24619370Spst close (scratch_chan); 24719370Spst error ("Could not open `%s' as an executable file: %s", 24819370Spst scratch_pathname, bfd_errmsg (bfd_get_error ())); 24919370Spst } 25098948Sobrien 25119370Spst /* Leave bfd open, core_xfer_memory and "info files" need it. */ 25298948Sobrien so->abfd = abfd; 253130809Smarcel bfd_set_cacheable (abfd, 1); 25419370Spst 25598948Sobrien /* copy full path name into so_name, so that later symbol_file_add 25698948Sobrien can find it */ 25798948Sobrien if (strlen (scratch_pathname) >= SO_NAME_MAX_PATH_SIZE) 25898948Sobrien error ("Full path name length of shared library exceeds SO_NAME_MAX_PATH_SIZE in so_list structure."); 25919370Spst strcpy (so->so_name, scratch_pathname); 26019370Spst 26119370Spst if (!bfd_check_format (abfd, bfd_object)) 26219370Spst { 26319370Spst error ("\"%s\": not in executable format: %s.", 26419370Spst scratch_pathname, bfd_errmsg (bfd_get_error ())); 26519370Spst } 26698948Sobrien if (build_section_table (abfd, &so->sections, &so->sections_end)) 26719370Spst { 26898948Sobrien error ("Can't find the file sections in `%s': %s", 26919370Spst bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); 27019370Spst } 27119370Spst 27298948Sobrien for (p = so->sections; p < so->sections_end; p++) 27319370Spst { 27419370Spst /* Relocate the section binding addresses as recorded in the shared 27598948Sobrien object's file by the base address to which the object was actually 27698948Sobrien mapped. */ 27798948Sobrien TARGET_SO_RELOCATE_SECTION_ADDRESSES (so, p); 278130809Smarcel if (strcmp (p->the_bfd_section->name, ".text") == 0) 27919370Spst { 28098948Sobrien so->textsection = p; 28119370Spst } 28219370Spst } 28319370Spst 28419370Spst /* Free the file names, close the file now. */ 28519370Spst do_cleanups (old_chain); 28646289Sdfr 28746289Sdfr return (1); 28819370Spst} 28919370Spst 29098948Sobrien/* LOCAL FUNCTION 29119370Spst 29298948Sobrien free_so --- free a `struct so_list' object 29319370Spst 29498948Sobrien SYNOPSIS 29519370Spst 29698948Sobrien void free_so (struct so_list *so) 29719370Spst 29898948Sobrien DESCRIPTION 29919370Spst 30098948Sobrien Free the storage associated with the `struct so_list' object SO. 30198948Sobrien If we have opened a BFD for SO, close it. 30219370Spst 30398948Sobrien The caller is responsible for removing SO from whatever list it is 30498948Sobrien a member of. If we have placed SO's sections in some target's 30598948Sobrien section table, the caller is responsible for removing them. 30619370Spst 30798948Sobrien This function doesn't mess with objfiles at all. If there is an 30898948Sobrien objfile associated with SO that needs to be removed, the caller is 30998948Sobrien responsible for taking care of that. */ 31019370Spst 31198948Sobrienvoid 31298948Sobrienfree_so (struct so_list *so) 31319370Spst{ 31498948Sobrien char *bfd_filename = 0; 31519370Spst 31698948Sobrien if (so->sections) 31798948Sobrien xfree (so->sections); 31898948Sobrien 31998948Sobrien if (so->abfd) 32019370Spst { 32198948Sobrien bfd_filename = bfd_get_filename (so->abfd); 32298948Sobrien if (! bfd_close (so->abfd)) 32398948Sobrien warning ("cannot close \"%s\": %s", 32498948Sobrien bfd_filename, bfd_errmsg (bfd_get_error ())); 32519370Spst } 32619370Spst 32798948Sobrien if (bfd_filename) 32898948Sobrien xfree (bfd_filename); 32919370Spst 33098948Sobrien TARGET_SO_FREE_SO (so); 33119370Spst 33298948Sobrien xfree (so); 33319370Spst} 33419370Spst 33519370Spst 33698948Sobrien/* A small stub to get us past the arg-passing pinhole of catch_errors. */ 33719370Spst 33898948Sobrienstatic int 339130809Smarcelsymbol_add_stub (void *arg) 34019370Spst{ 341130809Smarcel struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */ 34298948Sobrien struct section_addr_info *sap; 34319370Spst 34498948Sobrien /* Have we already loaded this shared object? */ 34598948Sobrien ALL_OBJFILES (so->objfile) 34619370Spst { 34798948Sobrien if (strcmp (so->objfile->name, so->so_name) == 0) 34898948Sobrien return 1; 34919370Spst } 35035533Sdfr 35198948Sobrien sap = build_section_addr_info_from_section_table (so->sections, 35298948Sobrien so->sections_end); 35335533Sdfr 35498948Sobrien so->objfile = symbol_file_add (so->so_name, so->from_tty, 35598948Sobrien sap, 0, OBJF_SHARED); 35698948Sobrien free_section_addr_info (sap); 35735533Sdfr 35898948Sobrien return (1); 35919370Spst} 36019370Spst 36119370Spst 36298948Sobrien/* LOCAL FUNCTION 36319370Spst 36498948Sobrien update_solib_list --- synchronize GDB's shared object list with inferior's 36519370Spst 36698948Sobrien SYNOPSIS 36719370Spst 36898948Sobrien void update_solib_list (int from_tty, struct target_ops *TARGET) 36919370Spst 37098948Sobrien Extract the list of currently loaded shared objects from the 37198948Sobrien inferior, and compare it with the list of shared objects currently 37298948Sobrien in GDB's so_list_head list. Edit so_list_head to bring it in sync 37398948Sobrien with the inferior's new list. 37419370Spst 37598948Sobrien If we notice that the inferior has unloaded some shared objects, 37698948Sobrien free any symbolic info GDB had read about those shared objects. 37719370Spst 37898948Sobrien Don't load symbolic info for any new shared objects; just add them 37998948Sobrien to the list, and leave their symbols_loaded flag clear. 38019370Spst 38198948Sobrien If FROM_TTY is non-null, feel free to print messages about what 38298948Sobrien we're doing. 38319370Spst 38498948Sobrien If TARGET is non-null, add the sections of all new shared objects 38598948Sobrien to TARGET's section table. Note that this doesn't remove any 38698948Sobrien sections for shared objects that have been unloaded, and it 38798948Sobrien doesn't check to see if the new shared objects are already present in 38898948Sobrien the section table. But we only use this for core files and 38998948Sobrien processes we've just attached to, so that's okay. */ 39019370Spst 391130809Smarcelstatic void 39298948Sobrienupdate_solib_list (int from_tty, struct target_ops *target) 39319370Spst{ 39498948Sobrien struct so_list *inferior = TARGET_SO_CURRENT_SOS (); 39598948Sobrien struct so_list *gdb, **gdb_link; 39619370Spst 39798948Sobrien /* If we are attaching to a running process for which we 39898948Sobrien have not opened a symbol file, we may be able to get its 39998948Sobrien symbols now! */ 40098948Sobrien if (attach_flag && 40198948Sobrien symfile_objfile == NULL) 402130809Smarcel catch_errors (TARGET_SO_OPEN_SYMBOL_FILE_OBJECT, &from_tty, 40398948Sobrien "Error reading attached process's symbol file.\n", 40498948Sobrien RETURN_MASK_ALL); 40519370Spst 40698948Sobrien /* Since this function might actually add some elements to the 40798948Sobrien so_list_head list, arrange for it to be cleaned up when 40898948Sobrien appropriate. */ 40998948Sobrien if (!solib_cleanup_queued) 41019370Spst { 41198948Sobrien make_run_cleanup (do_clear_solib, NULL); 41298948Sobrien solib_cleanup_queued = 1; 41319370Spst } 41419370Spst 41598948Sobrien /* GDB and the inferior's dynamic linker each maintain their own 41698948Sobrien list of currently loaded shared objects; we want to bring the 41798948Sobrien former in sync with the latter. Scan both lists, seeing which 41898948Sobrien shared objects appear where. There are three cases: 41919370Spst 42098948Sobrien - A shared object appears on both lists. This means that GDB 42198948Sobrien knows about it already, and it's still loaded in the inferior. 42298948Sobrien Nothing needs to happen. 42319370Spst 42498948Sobrien - A shared object appears only on GDB's list. This means that 42598948Sobrien the inferior has unloaded it. We should remove the shared 42698948Sobrien object from GDB's tables. 42719370Spst 42898948Sobrien - A shared object appears only on the inferior's list. This 42998948Sobrien means that it's just been loaded. We should add it to GDB's 43098948Sobrien tables. 43119370Spst 43298948Sobrien So we walk GDB's list, checking each entry to see if it appears 43398948Sobrien in the inferior's list too. If it does, no action is needed, and 43498948Sobrien we remove it from the inferior's list. If it doesn't, the 43598948Sobrien inferior has unloaded it, and we remove it from GDB's list. By 43698948Sobrien the time we're done walking GDB's list, the inferior's list 43798948Sobrien contains only the new shared objects, which we then add. */ 43819370Spst 43998948Sobrien gdb = so_list_head; 44098948Sobrien gdb_link = &so_list_head; 44198948Sobrien while (gdb) 44219370Spst { 44398948Sobrien struct so_list *i = inferior; 44498948Sobrien struct so_list **i_link = &inferior; 44519370Spst 44698948Sobrien /* Check to see whether the shared object *gdb also appears in 44798948Sobrien the inferior's current list. */ 44898948Sobrien while (i) 44919370Spst { 45098948Sobrien if (! strcmp (gdb->so_original_name, i->so_original_name)) 45198948Sobrien break; 45219370Spst 45398948Sobrien i_link = &i->next; 45498948Sobrien i = *i_link; 45519370Spst } 45619370Spst 45798948Sobrien /* If the shared object appears on the inferior's list too, then 45898948Sobrien it's still loaded, so we don't need to do anything. Delete 45998948Sobrien it from the inferior's list, and leave it on GDB's list. */ 46098948Sobrien if (i) 46146289Sdfr { 46298948Sobrien *i_link = i->next; 46398948Sobrien free_so (i); 46498948Sobrien gdb_link = &gdb->next; 46598948Sobrien gdb = *gdb_link; 46646289Sdfr } 46746289Sdfr 46898948Sobrien /* If it's not on the inferior's list, remove it from GDB's tables. */ 46998948Sobrien else 47098948Sobrien { 47198948Sobrien *gdb_link = gdb->next; 47219370Spst 47398948Sobrien /* Unless the user loaded it explicitly, free SO's objfile. */ 47498948Sobrien if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)) 47598948Sobrien free_objfile (gdb->objfile); 47619370Spst 47798948Sobrien /* Some targets' section tables might be referring to 47898948Sobrien sections from so->abfd; remove them. */ 47998948Sobrien remove_target_sections (gdb->abfd); 48019370Spst 48198948Sobrien free_so (gdb); 48298948Sobrien gdb = *gdb_link; 48319370Spst } 48419370Spst } 48519370Spst 48698948Sobrien /* Now the inferior's list contains only shared objects that don't 48798948Sobrien appear in GDB's list --- those that are newly loaded. Add them 48898948Sobrien to GDB's shared object list. */ 48998948Sobrien if (inferior) 49019370Spst { 49198948Sobrien struct so_list *i; 49219370Spst 49398948Sobrien /* Add the new shared objects to GDB's list. */ 49498948Sobrien *gdb_link = inferior; 49519370Spst 49698948Sobrien /* Fill in the rest of each of the `struct so_list' nodes. */ 49798948Sobrien for (i = inferior; i; i = i->next) 49819370Spst { 49998948Sobrien i->from_tty = from_tty; 50039645Sdfr 50198948Sobrien /* Fill in the rest of the `struct so_list' node. */ 50298948Sobrien catch_errors (solib_map_sections, i, 50398948Sobrien "Error while mapping shared library sections:\n", 50498948Sobrien RETURN_MASK_ALL); 50539645Sdfr 50698948Sobrien /* If requested, add the shared object's sections to the TARGET's 50798948Sobrien section table. Do this immediately after mapping the object so 50898948Sobrien that later nodes in the list can query this object, as is needed 50998948Sobrien in solib-osf.c. */ 51098948Sobrien if (target) 51119370Spst { 51298948Sobrien int count = (i->sections_end - i->sections); 51398948Sobrien if (count > 0) 51439645Sdfr { 51598948Sobrien int space = target_resize_to_sections (target, count); 51698948Sobrien memcpy (target->to_sections + space, 51798948Sobrien i->sections, 51898948Sobrien count * sizeof (i->sections[0])); 51939645Sdfr } 52019370Spst } 52119370Spst } 52219370Spst } 52398948Sobrien} 52446289Sdfr 52539645Sdfr 52698948Sobrien/* GLOBAL FUNCTION 52719370Spst 52898948Sobrien solib_add -- read in symbol info for newly added shared libraries 52919370Spst 53098948Sobrien SYNOPSIS 53146289Sdfr 53298948Sobrien void solib_add (char *pattern, int from_tty, struct target_ops 53398948Sobrien *TARGET, int readsyms) 53446289Sdfr 53598948Sobrien DESCRIPTION 53646289Sdfr 53798948Sobrien Read in symbolic information for any shared objects whose names 53898948Sobrien match PATTERN. (If we've already read a shared object's symbol 53998948Sobrien info, leave it alone.) If PATTERN is zero, read them all. 54019370Spst 54198948Sobrien If READSYMS is 0, defer reading symbolic information until later 54298948Sobrien but still do any needed low level processing. 54319370Spst 54498948Sobrien FROM_TTY and TARGET are as described for update_solib_list, above. */ 54598948Sobrien 54698948Sobrienvoid 54798948Sobriensolib_add (char *pattern, int from_tty, struct target_ops *target, int readsyms) 54819370Spst{ 54998948Sobrien struct so_list *gdb; 55019370Spst 55198948Sobrien if (pattern) 55219370Spst { 55398948Sobrien char *re_err = re_comp (pattern); 55498948Sobrien 55598948Sobrien if (re_err) 55698948Sobrien error ("Invalid regexp: %s", re_err); 55719370Spst } 55819370Spst 55998948Sobrien update_solib_list (from_tty, target); 56019370Spst 56198948Sobrien /* Walk the list of currently loaded shared libraries, and read 56298948Sobrien symbols for any that match the pattern --- or any whose symbols 56398948Sobrien aren't already loaded, if no pattern was given. */ 56498948Sobrien { 56598948Sobrien int any_matches = 0; 56698948Sobrien int loaded_any_symbols = 0; 56719370Spst 56898948Sobrien for (gdb = so_list_head; gdb; gdb = gdb->next) 56998948Sobrien if (! pattern || re_exec (gdb->so_name)) 57019370Spst { 57198948Sobrien any_matches = 1; 57219370Spst 57398948Sobrien if (gdb->symbols_loaded) 57419370Spst { 57598948Sobrien if (from_tty) 57698948Sobrien printf_unfiltered ("Symbols already loaded for %s\n", 57798948Sobrien gdb->so_name); 57819370Spst } 57998948Sobrien else if (readsyms) 58019370Spst { 58198948Sobrien if (catch_errors 58298948Sobrien (symbol_add_stub, gdb, 58398948Sobrien "Error while reading shared library symbols:\n", 58498948Sobrien RETURN_MASK_ALL)) 58519370Spst { 58698948Sobrien if (from_tty) 58798948Sobrien printf_unfiltered ("Loaded symbols for %s\n", 58898948Sobrien gdb->so_name); 58998948Sobrien gdb->symbols_loaded = 1; 59098948Sobrien loaded_any_symbols = 1; 59119370Spst } 59219370Spst } 59319370Spst } 59419370Spst 59598948Sobrien if (from_tty && pattern && ! any_matches) 59698948Sobrien printf_unfiltered 59798948Sobrien ("No loaded shared libraries match the pattern `%s'.\n", pattern); 59819370Spst 59998948Sobrien if (loaded_any_symbols) 60098948Sobrien { 60198948Sobrien /* Getting new symbols may change our opinion about what is 60298948Sobrien frameless. */ 60398948Sobrien reinit_frame_cache (); 60498948Sobrien 60598948Sobrien TARGET_SO_SPECIAL_SYMBOL_HANDLING (); 60698948Sobrien } 60798948Sobrien } 60819370Spst} 60919370Spst 61098948Sobrien 61119370Spst/* 61219370Spst 61398948Sobrien LOCAL FUNCTION 61419370Spst 61598948Sobrien info_sharedlibrary_command -- code for "info sharedlibrary" 61619370Spst 61798948Sobrien SYNOPSIS 61819370Spst 61998948Sobrien static void info_sharedlibrary_command () 62019370Spst 62198948Sobrien DESCRIPTION 62219370Spst 62398948Sobrien Walk through the shared library list and print information 62498948Sobrien about each attached library. 62598948Sobrien */ 62619370Spst 62719370Spststatic void 62898948Sobrieninfo_sharedlibrary_command (char *ignore, int from_tty) 62919370Spst{ 630130809Smarcel struct so_list *so = NULL; /* link map state variable */ 63119370Spst int header_done = 0; 63246289Sdfr int addr_width; 63346289Sdfr char *addr_fmt; 63446289Sdfr 63598948Sobrien if (TARGET_PTR_BIT == 32) 63619370Spst { 63798948Sobrien addr_width = 8 + 4; 63898948Sobrien addr_fmt = "08l"; 63919370Spst } 64098948Sobrien else if (TARGET_PTR_BIT == 64) 64198948Sobrien { 64298948Sobrien addr_width = 16 + 4; 64398948Sobrien addr_fmt = "016l"; 64498948Sobrien } 64598948Sobrien else 64698948Sobrien { 64798948Sobrien internal_error (__FILE__, __LINE__, 64898948Sobrien "TARGET_PTR_BIT returned unknown size %d", 64998948Sobrien TARGET_PTR_BIT); 65098948Sobrien } 65146289Sdfr 65298948Sobrien update_solib_list (from_tty, 0); 65346289Sdfr 65498948Sobrien for (so = so_list_head; so; so = so->next) 65519370Spst { 65698948Sobrien if (so->so_name[0]) 65719370Spst { 65819370Spst if (!header_done) 65919370Spst { 66098948Sobrien printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width, "From", 66198948Sobrien addr_width, "To", "Syms Read", 66298948Sobrien "Shared Object Library"); 66319370Spst header_done++; 66419370Spst } 66546289Sdfr 66646289Sdfr printf_unfiltered ("%-*s", addr_width, 66798948Sobrien so->textsection != NULL 668130809Smarcel ? local_hex_string_custom ( 66998948Sobrien (LONGEST) so->textsection->addr, 67098948Sobrien addr_fmt) 67198948Sobrien : ""); 67246289Sdfr printf_unfiltered ("%-*s", addr_width, 67398948Sobrien so->textsection != NULL 674130809Smarcel ? local_hex_string_custom ( 67598948Sobrien (LONGEST) so->textsection->endaddr, 67698948Sobrien addr_fmt) 67798948Sobrien : ""); 67898948Sobrien printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No"); 67998948Sobrien printf_unfiltered ("%s\n", so->so_name); 68019370Spst } 68119370Spst } 68219370Spst if (so_list_head == NULL) 68319370Spst { 68498948Sobrien printf_unfiltered ("No shared libraries loaded at this time.\n"); 68519370Spst } 68619370Spst} 68719370Spst 68819370Spst/* 68919370Spst 69098948Sobrien GLOBAL FUNCTION 69119370Spst 69298948Sobrien solib_address -- check to see if an address is in a shared lib 69319370Spst 69498948Sobrien SYNOPSIS 69519370Spst 69698948Sobrien char * solib_address (CORE_ADDR address) 69719370Spst 69898948Sobrien DESCRIPTION 69919370Spst 70098948Sobrien Provides a hook for other gdb routines to discover whether or 70198948Sobrien not a particular address is within the mapped address space of 70298948Sobrien a shared library. 70319370Spst 70498948Sobrien For example, this routine is called at one point to disable 70598948Sobrien breakpoints which are in shared libraries that are not currently 70698948Sobrien mapped in. 70719370Spst */ 70819370Spst 70919370Spstchar * 71098948Sobriensolib_address (CORE_ADDR address) 71119370Spst{ 712130809Smarcel struct so_list *so = 0; /* link map state variable */ 71398948Sobrien 71498948Sobrien for (so = so_list_head; so; so = so->next) 71519370Spst { 71698948Sobrien struct section_table *p; 71798948Sobrien 71898948Sobrien for (p = so->sections; p < so->sections_end; p++) 71919370Spst { 72098948Sobrien if (p->addr <= address && address < p->endaddr) 72119370Spst return (so->so_name); 72219370Spst } 72319370Spst } 72498948Sobrien 72519370Spst return (0); 72619370Spst} 72719370Spst 72819370Spst/* Called by free_all_symtabs */ 72919370Spst 73098948Sobrienvoid 73198948Sobrienclear_solib (void) 73219370Spst{ 73398948Sobrien /* This function is expected to handle ELF shared libraries. It is 73498948Sobrien also used on Solaris, which can run either ELF or a.out binaries 73598948Sobrien (for compatibility with SunOS 4), both of which can use shared 73698948Sobrien libraries. So we don't know whether we have an ELF executable or 73798948Sobrien an a.out executable until the user chooses an executable file. 73898948Sobrien 73998948Sobrien ELF shared libraries don't get mapped into the address space 74098948Sobrien until after the program starts, so we'd better not try to insert 74198948Sobrien breakpoints in them immediately. We have to wait until the 74298948Sobrien dynamic linker has loaded them; we'll hit a bp_shlib_event 74398948Sobrien breakpoint (look for calls to create_solib_event_breakpoint) when 74498948Sobrien it's ready. 74598948Sobrien 74698948Sobrien SunOS shared libraries seem to be different --- they're present 74798948Sobrien as soon as the process begins execution, so there's no need to 74898948Sobrien put off inserting breakpoints. There's also nowhere to put a 74998948Sobrien bp_shlib_event breakpoint, so if we put it off, we'll never get 75098948Sobrien around to it. 75198948Sobrien 75298948Sobrien So: disable breakpoints only if we're using ELF shared libs. */ 75398948Sobrien if (exec_bfd != NULL 75498948Sobrien && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour) 75598948Sobrien disable_breakpoints_in_shlibs (1); 75698948Sobrien 75719370Spst while (so_list_head) 75819370Spst { 75998948Sobrien struct so_list *so = so_list_head; 76098948Sobrien so_list_head = so->next; 76198948Sobrien if (so->abfd) 76298948Sobrien remove_target_sections (so->abfd); 76398948Sobrien free_so (so); 76419370Spst } 76598948Sobrien 76698948Sobrien TARGET_SO_CLEAR_SOLIB (); 76719370Spst} 76819370Spst 76946289Sdfrstatic void 770130809Smarceldo_clear_solib (void *dummy) 77146289Sdfr{ 77246289Sdfr solib_cleanup_queued = 0; 77346289Sdfr clear_solib (); 77446289Sdfr} 77546289Sdfr 77698948Sobrien/* GLOBAL FUNCTION 77746289Sdfr 77898948Sobrien solib_create_inferior_hook -- shared library startup support 77946289Sdfr 78098948Sobrien SYNOPSIS 78146289Sdfr 78298948Sobrien void solib_create_inferior_hook() 78346289Sdfr 78498948Sobrien DESCRIPTION 78519370Spst 78698948Sobrien When gdb starts up the inferior, it nurses it along (through the 78798948Sobrien shell) until it is ready to execute it's first instruction. At this 78898948Sobrien point, this function gets called via expansion of the macro 78998948Sobrien SOLIB_CREATE_INFERIOR_HOOK. */ 79019370Spst 79198948Sobrienvoid 79298948Sobriensolib_create_inferior_hook (void) 79319370Spst{ 79498948Sobrien TARGET_SO_SOLIB_CREATE_INFERIOR_HOOK (); 79519370Spst} 79619370Spst 79798948Sobrien/* GLOBAL FUNCTION 79819370Spst 79998948Sobrien in_solib_dynsym_resolve_code -- check to see if an address is in 80098948Sobrien dynamic loader's dynamic symbol 80198948Sobrien resolution code 80219370Spst 80398948Sobrien SYNOPSIS 80419370Spst 80598948Sobrien int in_solib_dynsym_resolve_code (CORE_ADDR pc) 80619370Spst 80798948Sobrien DESCRIPTION 80819370Spst 80998948Sobrien Determine if PC is in the dynamic linker's symbol resolution 81098948Sobrien code. Return 1 if so, 0 otherwise. 81119370Spst*/ 81219370Spst 81398948Sobrienint 81498948Sobrienin_solib_dynsym_resolve_code (CORE_ADDR pc) 81519370Spst{ 81698948Sobrien return TARGET_SO_IN_DYNSYM_RESOLVE_CODE (pc); 81719370Spst} 81819370Spst 81919370Spst/* 82019370Spst 82198948Sobrien LOCAL FUNCTION 82219370Spst 82398948Sobrien sharedlibrary_command -- handle command to explicitly add library 82419370Spst 82598948Sobrien SYNOPSIS 82619370Spst 82798948Sobrien static void sharedlibrary_command (char *args, int from_tty) 82819370Spst 82998948Sobrien DESCRIPTION 83019370Spst 83198948Sobrien */ 83219370Spst 83319370Spststatic void 83498948Sobriensharedlibrary_command (char *args, int from_tty) 83519370Spst{ 83698948Sobrien dont_repeat (); 83798948Sobrien solib_add (args, from_tty, (struct target_ops *) 0, 1); 83819370Spst} 83919370Spst 84098948Sobrien/* LOCAL FUNCTION 84119370Spst 84298948Sobrien no_shared_libraries -- handle command to explicitly discard symbols 84398948Sobrien from shared libraries. 84419370Spst 84598948Sobrien DESCRIPTION 84619370Spst 84798948Sobrien Implements the command "nosharedlibrary", which discards symbols 84898948Sobrien that have been auto-loaded from shared libraries. Symbols from 84998948Sobrien shared libraries that were added by explicit request of the user 85098948Sobrien are not discarded. Also called from remote.c. */ 85119370Spst 85298948Sobrienvoid 85398948Sobrienno_shared_libraries (char *ignored, int from_tty) 85419370Spst{ 85598948Sobrien objfile_purge_solibs (); 85698948Sobrien do_clear_solib (NULL); 85719370Spst} 85819370Spst 859130809Smarcelstatic void 860130809Smarcelreload_shared_libraries (char *ignored, int from_tty) 861130809Smarcel{ 862130809Smarcel no_shared_libraries (NULL, from_tty); 863130809Smarcel solib_add (NULL, from_tty, NULL, auto_solib_add); 864130809Smarcel} 865130809Smarcel 866130809Smarcelextern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */ 867130809Smarcel 86819370Spstvoid 86998948Sobrien_initialize_solib (void) 87019370Spst{ 87198948Sobrien struct cmd_list_element *c; 87219370Spst 87319370Spst add_com ("sharedlibrary", class_files, sharedlibrary_command, 87419370Spst "Load shared object library symbols for files matching REGEXP."); 87598948Sobrien add_info ("sharedlibrary", info_sharedlibrary_command, 87619370Spst "Status of loaded shared object libraries."); 87798948Sobrien add_com ("nosharedlibrary", class_files, no_shared_libraries, 87898948Sobrien "Unload all shared object library symbols."); 87919370Spst 88019370Spst add_show_from_set 88198948Sobrien (add_set_cmd ("auto-solib-add", class_support, var_boolean, 88219370Spst (char *) &auto_solib_add, 88319370Spst "Set autoloading of shared library symbols.\n\ 88498948SobrienIf \"on\", symbols from all shared object libraries will be loaded\n\ 88598948Sobrienautomatically when the inferior begins execution, when the dynamic linker\n\ 88698948Sobrieninforms gdb that a new library has been loaded, or when attaching to the\n\ 88798948Sobrieninferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'.", 88819370Spst &setlist), 88919370Spst &showlist); 89019370Spst 89198948Sobrien c = add_set_cmd ("solib-absolute-prefix", class_support, var_filename, 89298948Sobrien (char *) &solib_absolute_prefix, 89398948Sobrien "Set prefix for loading absolute shared library symbol files.\n\ 89446289SdfrFor other (relative) files, you can add values using `set solib-search-path'.", 89598948Sobrien &setlist); 89698948Sobrien add_show_from_set (c, &showlist); 897130809Smarcel set_cmd_cfunc (c, reload_shared_libraries); 898130809Smarcel set_cmd_completer (c, filename_completer); 89998948Sobrien 900130809Smarcel /* Set the default value of "solib-absolute-prefix" from the sysroot, if 901130809Smarcel one is set. */ 902130809Smarcel solib_absolute_prefix = xstrdup (gdb_sysroot); 903130809Smarcel 90498948Sobrien c = add_set_cmd ("solib-search-path", class_support, var_string, 90598948Sobrien (char *) &solib_search_path, 90698948Sobrien "Set the search path for loading non-absolute shared library symbol files.\n\ 90746289SdfrThis takes precedence over the environment variables PATH and LD_LIBRARY_PATH.", 90898948Sobrien &setlist); 90998948Sobrien add_show_from_set (c, &showlist); 910130809Smarcel set_cmd_cfunc (c, reload_shared_libraries); 911130809Smarcel set_cmd_completer (c, filename_completer); 91219370Spst} 913