1 /* Copyright (C) 1990 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <sys/types.h>
25 #include <sys/param.h>
38 extern char *getenv();
41 ** local data declarations
43 #define MAX_PATH_SIZE 256
45 struct link_map inferior_lm; /* inferior link map */
46 struct link_map *inferior_lm_add;
48 char inferior_so_name[MAX_PATH_SIZE]; /* Shared Object Library Name */
49 struct so_list *next; /* Next Structure */
50 char symbols_loaded; /* Flag: loaded? */
51 char from_tty; /* Flag: print msgs? */
53 struct section_table *sections;
54 struct section_table *sections_end;
57 static struct so_list *so_list_head = 0;
60 ** Build a section map for a shared library, record its text size in
61 ** the so_list structure and set up the text section of the shared lib.
64 solib_map_sections(so)
68 char *scratch_pathname;
70 struct section_table *p;
72 filename = tilde_expand (so->inferior_so_name);
73 make_cleanup (free, filename);
75 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
78 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename, O_RDONLY, 0,
81 perror_with_name (filename);
83 so->so_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
85 error ("Could not open `%s' as an executable file: %s",
86 scratch_pathname, bfd_errmsg (bfd_error));
87 if (!bfd_check_format (so->so_bfd, bfd_object))
88 error ("\"%s\": not in executable format: %s.",
89 scratch_pathname, bfd_errmsg (bfd_error));
90 if (build_section_table (so->so_bfd, &so->sections, &so->sections_end))
91 error ("Can't find the file sections in `%s': %s",
92 exec_bfd->filename, bfd_errmsg (bfd_error));
94 for (p = so->sections; p < so->sections_end; p++)
96 if (strcmp (bfd_section_name (so->so_bfd, p->sec_ptr), ".text") == 0)
98 /* Determine length of text section and relocate it. */
99 so->ld_text = p->endaddr - p->addr;
100 p->addr += (CORE_ADDR)so->inferior_lm.lm_addr;
101 p->endaddr += (CORE_ADDR)so->inferior_lm.lm_addr;
104 /* All other sections are ignored for now. */
105 p->addr = p->endaddr = 0;
109 /*=======================================================================*/
115 ** This module contains the routine which finds the names of any loaded
116 ** "images" in the current process. The argument in must be NULL on the
117 ** first call, and then the returned value must be passed in on
118 ** subsequent calls. This provides the capability to "step" down the
119 ** list of loaded objects. On the last object, a NULL value is returned.
120 ** The arg and return value are "struct link_map" pointers, as defined
123 ** NOTE: This only works under SunOS4.0.
126 struct so_list *find_solib(so_list_ptr)
127 struct so_list *so_list_ptr; /* so_list_head position ptr */
129 struct so_list *so_list_next = 0;
130 struct link_map *inferior_lm = 0;
131 struct link_dynamic inferior_dynamic_cpy;
132 struct link_dynamic_2 inferior_ld_2_cpy;
137 if (!(so_list_next = so_list_head)) {
138 i = lookup_misc_func ("_DYNAMIC");
140 read_memory(misc_function_vector[i].address,
141 &inferior_dynamic_cpy,
142 sizeof(struct link_dynamic));
143 if (inferior_dynamic_cpy.ld_version == 3) {
144 read_memory((CORE_ADDR)inferior_dynamic_cpy.ld_un.ld_2,
146 sizeof(struct link_dynamic_2));
147 inferior_lm = inferior_ld_2_cpy.ld_loaded;
153 ** Advance to next local abbreviated load_map structure
155 if (!(inferior_lm = so_list_ptr->inferior_lm.lm_next)) {
156 /* See if any were added, but be quiet if we can't read
157 from the target any more. */
160 status = target_read_memory (
161 (CORE_ADDR)so_list_ptr->inferior_lm_add,
162 (char *)&so_list_ptr->inferior_lm,
163 sizeof(struct link_map));
165 inferior_lm = so_list_ptr->inferior_lm.lm_next;
169 so_list_next = so_list_ptr->next;
171 if ((!so_list_next) && inferior_lm) {
173 ** Get Next LM Structure from inferior image and build
174 ** an local abbreviated load_map structure
176 new = (struct so_list *) xmalloc(sizeof(struct so_list));
177 new->inferior_lm_add = inferior_lm;
178 read_memory((CORE_ADDR)inferior_lm,
180 sizeof(struct link_map));
182 read_memory((CORE_ADDR)new->inferior_lm.lm_name,
183 new->inferior_so_name,
185 new->inferior_so_name[MAX_PATH_SIZE - 1] = 0;
186 /* Zero everything after the first terminating null */
187 strncpy(new->inferior_so_name, new->inferior_so_name, MAX_PATH_SIZE);
190 /* This doesn't work for core files, so instead get ld_text
191 using solib_map_sections (below). */
192 read_memory((CORE_ADDR)new->inferior_lm.lm_ld,
193 &inferior_dynamic_cpy,
194 sizeof(struct link_dynamic));
195 read_memory((CORE_ADDR)inferior_dynamic_cpy.ld_un.ld_2,
197 sizeof(struct link_dynamic_2));
198 new->ld_text = inferior_ld_2_cpy.ld_text;
202 new->symbols_loaded = 0;
204 new->sections = NULL;
206 so_list_ptr->next = new;
210 solib_map_sections (new);
214 return(so_list_next);
217 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
220 symbol_add_stub (arg)
223 register struct so_list *so = (struct so_list *)arg; /* catch_errs bogon */
225 symbol_file_add (so->inferior_so_name, so->from_tty,
226 (unsigned int)so->inferior_lm.lm_addr, 0);
230 /* The real work of adding a shared library file to the symtab and
234 solib_add (arg_string, from_tty, target)
237 struct target_ops *target;
239 register struct so_list *so = 0; /* link map state variable */
242 struct section_table *sec;
246 else if (val = (char *) re_comp (arg_string)) {
247 error ("Invalid regexp: %s", val);
250 /* Getting new symbols may change our opinion about what is
252 reinit_frame_cache ();
255 printf_filtered ("Shared libraries");
257 printf_filtered (" matching regular expresion \"%s\"", arg_string);
258 printf_filtered (":\n");
263 while (so = find_solib(so)) {
264 if (re_exec(so->inferior_so_name)) {
265 if (so->symbols_loaded) {
267 printf("Symbols already loaded for %s\n", so->inferior_so_name);
269 so->symbols_loaded = 1;
270 so->from_tty = from_tty;
271 catch_errors (symbol_add_stub, (char *)so,
272 "Error while reading shared library symbols:\n");
277 /* Now add the shared library sections to the section table of the
278 specified target, if any. */
280 /* Count how many new section_table entries there are. */
283 while (0 != (so = find_solib (so))) {
284 count += so->sections_end - so->sections;
288 /* Reallocate the target's section table including the new size. */
289 if (target->sections) {
290 old = target->sections_end - target->sections;
291 target->sections = (struct section_table *)
292 realloc ((char *)target->sections,
293 (sizeof (struct section_table)) * (count + old));
296 target->sections = (struct section_table *)
297 malloc ((sizeof (struct section_table)) * count);
299 target->sections_end = target->sections + (count + old);
301 /* Add these section table entries to the target's table. */
302 while (0 != (so = find_solib (so))) {
303 count = so->sections_end - so->sections;
304 bcopy (so->sections, (char *)(target->sections + old),
305 (sizeof (struct section_table)) * count);
312 /*=======================================================================*/
314 static void solib_info()
316 register struct so_list *so = 0; /* link map state variable */
318 while (so = find_solib(so)) {
319 if (so == so_list_head) {
320 printf(" Address Range Syms Read Shared Object Library\n");
322 printf(" 0x%08x - 0x%08x %s %s\n",
323 so->inferior_lm.lm_addr,
324 so->inferior_lm.lm_addr + so->ld_text - 1,
325 (so->symbols_loaded ? "Yes" : "No "),
326 so->inferior_so_name);
329 printf("No shared libraries loaded at this time.\n");
334 ** Called by Insert Breakpoint to see if Address is Shared Library Address
337 solib_address(address)
340 register struct so_list *so = 0; /* link map state variable */
342 while (so = find_solib(so)) {
343 if ((address >= (CORE_ADDR) so->inferior_lm.lm_addr) &&
344 (address < (CORE_ADDR) so->inferior_lm.lm_addr + so->ld_text))
351 ** Called by free_all_symtabs
356 struct so_list *next;
358 while (so_list_head) {
359 if (so_list_head->sections)
360 free (so_list_head->sections);
361 if (so_list_head->so_bfd)
362 bfd_close (so_list_head->so_bfd);
363 next = so_list_head->next;
369 /* Called by child_create_inferior when the inferior is stopped at its
370 first instruction. */
373 solib_create_inferior_hook()
375 struct link_dynamic inferior_dynamic_cpy;
376 CORE_ADDR inferior_debug_addr;
377 struct ld_debug inferior_debug_cpy;
379 CORE_ADDR in_debugger_addr;
380 CORE_ADDR breakpoint_addr;
383 /* FIXME: We should look around in the executable code to find _DYNAMIC,
384 if it isn't in the symbol table. It's not that hard to find...
385 Then we can debug stripped executables using shared library symbols. */
386 i = lookup_misc_func ("_DYNAMIC");
387 if (i < 0) /* Can't find shared lib ptr. */
389 if (misc_function_vector[i].address == 0) /* statically linked program */
392 /* Get link_dynamic structure */
393 j = target_read_memory(misc_function_vector[i].address,
394 &inferior_dynamic_cpy,
395 sizeof(struct link_dynamic));
396 if (j) /* unreadable */
399 /* Calc address of debugger interface structure */
400 inferior_debug_addr = (CORE_ADDR)inferior_dynamic_cpy.ldd;
401 /* Calc address of `in_debugger' member of debugger interface structure */
402 in_debugger_addr = inferior_debug_addr + (CORE_ADDR)((char *)&inferior_debug_cpy.ldd_in_debugger - (char *)&inferior_debug_cpy);
403 /* Write a value of 1 to this member. */
405 write_memory(in_debugger_addr, &in_debugger, sizeof(in_debugger));
407 /* Now run the target. Seeing `in_debugger' set, it will set a
408 breakpoint at some convenient place, remember the original contents
409 of that place, and eventually take a SIGTRAP when it runs into the
410 breakpoint. We handle this by restoring the contents of the
411 breakpointed location (which is only known after it stops),
412 chasing around to locate the shared libraries that have been
413 loaded, then resuming. */
415 clear_proceed_status ();
416 stop_soon_quietly = 1;
417 target_resume (0, 0);
418 wait_for_inferior ();
419 while (stop_signal != SIGTRAP)
421 /* FIXME, what if child has exit()ed? Must exit loop somehow */
422 target_resume (0, stop_signal);
423 wait_for_inferior ();
425 stop_soon_quietly = 0;
427 /* Set `in_debugger' to zero now. WHY, is this needed? */
429 write_memory(in_debugger_addr, &in_debugger, sizeof(in_debugger));
430 read_memory(inferior_debug_addr, &inferior_debug_cpy, sizeof(inferior_debug_cpy));
431 /* FIXME: maybe we should add the common symbols from the ldd_cp chain
432 * to the misc_function_vector ?
434 breakpoint_addr = (CORE_ADDR)inferior_debug_cpy.ldd_bp_addr;
435 if (stop_pc - DECR_PC_AFTER_BREAK == breakpoint_addr)
437 write_memory(breakpoint_addr, &inferior_debug_cpy.ldd_bp_inst, sizeof(inferior_debug_cpy.ldd_bp_inst));
438 if (DECR_PC_AFTER_BREAK)
440 stop_pc -= DECR_PC_AFTER_BREAK;
441 write_register (PC_REGNUM, stop_pc);
444 solib_add ((char *)0, 0, (struct target_ops *)0);
448 sharedlibrary_command (args, from_tty)
450 solib_add (args, from_tty, (struct target_ops *)0);
457 add_com("sharedlibrary", class_files, sharedlibrary_command,
458 "Load shared object library symbols for files matching REGEXP.");
459 add_info("sharedlibrary", solib_info,
460 "Status of loaded shared object libraries");