* breakpoint.c (breakpoint_thread_match break_command_1):
[deliverable/binutils-gdb.git] / gdb / osfsolib.c
1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* FIXME: Most of this code could be merged with solib.c by using
21 next_link_map_member and xfer_link_map_member in solib.c. */
22
23 #include "defs.h"
24
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <fcntl.h>
29
30 #include "symtab.h"
31 #include "bfd.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "gdbcore.h"
35 #include "command.h"
36 #include "target.h"
37 #include "frame.h"
38 #include "regex.h"
39 #include "inferior.h"
40 #include "language.h"
41
42 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
43
44 /* FIXME: This is a terrible hack for shared library support under OSF/1.
45 The main problem is that the needed definitions are not contained in
46 the system header files.
47 The ldr_* routines described in loader(3) would be the way to go here.
48 But they do not work for arbitrary target processes (as documented). */
49
50 #ifndef USE_LDR_ROUTINES
51 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
52
53 typedef struct
54 {
55 CORE_ADDR next;
56 CORE_ADDR previous;
57 CORE_ADDR unknown;
58 char *module_name;
59 CORE_ADDR modinfo_addr;
60 } ldr_module_info_t;
61
62 typedef struct
63 {
64 CORE_ADDR unknown1;
65 CORE_ADDR unknown2;
66 CORE_ADDR head;
67 CORE_ADDR tail;
68 } ldr_context_t;
69
70 static ldr_context_t ldr_context;
71 #else
72 #include <loader.h>
73 #endif
74
75 /* Define our own link_map structure.
76 This will help to share code with solib.c. */
77
78 struct link_map {
79 CORE_ADDR l_addr; /* address at which object mapped */
80 char *l_name; /* full name of loaded object */
81 ldr_module_info_t module_info; /* corresponding module info */
82 };
83
84 #define LM_ADDR(so) ((so) -> lm.l_addr)
85 #define LM_NAME(so) ((so) -> lm.l_name)
86
87 struct so_list {
88 struct so_list *next; /* next structure in linked list */
89 struct link_map lm; /* copy of link map from inferior */
90 struct link_map *lmaddr; /* addr in inferior lm was read from */
91 CORE_ADDR lmend; /* upper addr bound of mapped object */
92 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
93 char symbols_loaded; /* flag: symbols read in yet? */
94 char from_tty; /* flag: print msgs? */
95 struct objfile *objfile; /* objfile for loaded lib */
96 struct section_table *sections;
97 struct section_table *sections_end;
98 struct section_table *textsection;
99 bfd *abfd;
100 };
101
102 static struct so_list *so_list_head; /* List of known shared objects */
103
104 extern int
105 fdmatch PARAMS ((int, int)); /* In libiberty */
106
107 /* Local function prototypes */
108
109 static void
110 sharedlibrary_command PARAMS ((char *, int));
111
112 static void
113 info_sharedlibrary_command PARAMS ((char *, int));
114
115 static int
116 symbol_add_stub PARAMS ((char *));
117
118 static struct so_list *
119 find_solib PARAMS ((struct so_list *));
120
121 static struct link_map *
122 first_link_map_member PARAMS ((void));
123
124 static struct link_map *
125 next_link_map_member PARAMS ((struct so_list *));
126
127 static void
128 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
129
130 static void
131 solib_map_sections PARAMS ((struct so_list *));
132
133 void
134 _initialize_solib PARAMS ((void));
135
136 /*
137
138 LOCAL FUNCTION
139
140 solib_map_sections -- open bfd and build sections for shared lib
141
142 SYNOPSIS
143
144 static void solib_map_sections (struct so_list *so)
145
146 DESCRIPTION
147
148 Given a pointer to one of the shared objects in our list
149 of mapped objects, use the recorded name to open a bfd
150 descriptor for the object, build a section table, and then
151 relocate all the section addresses by the base address at
152 which the shared object was mapped.
153
154 FIXMES
155
156 In most (all?) cases the shared object file name recorded in the
157 dynamic linkage tables will be a fully qualified pathname. For
158 cases where it isn't, do we really mimic the systems search
159 mechanism correctly in the below code (particularly the tilde
160 expansion stuff?).
161 */
162
163 static void
164 solib_map_sections (so)
165 struct so_list *so;
166 {
167 char *filename;
168 char *scratch_pathname;
169 int scratch_chan;
170 struct section_table *p;
171 struct cleanup *old_chain;
172 bfd *abfd;
173
174 filename = tilde_expand (so -> so_name);
175 old_chain = make_cleanup (free, filename);
176
177 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
178 &scratch_pathname);
179 if (scratch_chan < 0)
180 {
181 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
182 O_RDONLY, 0, &scratch_pathname);
183 }
184 if (scratch_chan < 0)
185 {
186 perror_with_name (filename);
187 }
188 /* Leave scratch_pathname allocated. bfd->name will point to it. */
189
190 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
191 if (!abfd)
192 {
193 close (scratch_chan);
194 error ("Could not open `%s' as an executable file: %s",
195 scratch_pathname, bfd_errmsg (bfd_error));
196 }
197 /* Leave bfd open, core_xfer_memory and "info files" need it. */
198 so -> abfd = abfd;
199 abfd -> cacheable = true;
200
201 if (!bfd_check_format (abfd, bfd_object))
202 {
203 error ("\"%s\": not in executable format: %s.",
204 scratch_pathname, bfd_errmsg (bfd_error));
205 }
206 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
207 {
208 error ("Can't find the file sections in `%s': %s",
209 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_error));
210 }
211
212 for (p = so -> sections; p < so -> sections_end; p++)
213 {
214 /* Relocate the section binding addresses as recorded in the shared
215 object's file by the base address to which the object was actually
216 mapped. */
217 p -> addr += (CORE_ADDR) LM_ADDR (so);
218 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
219 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
220 if (STREQ (p -> sec_ptr -> name, ".text"))
221 {
222 so -> textsection = p;
223 }
224 }
225
226 /* Free the file names, close the file now. */
227 do_cleanups (old_chain);
228 }
229
230 /*
231
232 LOCAL FUNCTION
233
234 first_link_map_member -- locate first member in dynamic linker's map
235
236 SYNOPSIS
237
238 static struct link_map *first_link_map_member (void)
239
240 DESCRIPTION
241
242 Read in a copy of the first member in the inferior's dynamic
243 link map from the inferior's dynamic linker structures, and return
244 a pointer to the copy in our address space.
245 */
246
247 static struct link_map *
248 first_link_map_member ()
249 {
250 struct link_map *lm = NULL;
251 static struct link_map first_lm;
252
253 #ifdef USE_LDR_ROUTINES
254 ldr_module_t mod_id = LDR_NULL_MODULE;
255 size_t retsize;
256
257 if (ldr_next_module(inferior_pid, &mod_id) != 0
258 || mod_id == LDR_NULL_MODULE
259 || ldr_inq_module(inferior_pid, mod_id,
260 &first_lm.module_info, sizeof(ldr_module_info_t),
261 &retsize) != 0)
262 return lm;
263 #else
264 CORE_ADDR ldr_context_addr;
265
266 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
267 (char *) &ldr_context_addr,
268 sizeof (CORE_ADDR)) != 0
269 || target_read_memory (ldr_context_addr,
270 (char *) &ldr_context,
271 sizeof (ldr_context_t)) != 0
272 || target_read_memory ((CORE_ADDR) ldr_context.head,
273 (char *) &first_lm.module_info,
274 sizeof (ldr_module_info_t)) != 0)
275 return lm;
276 #endif
277
278 lm = &first_lm;
279
280 /* The first entry is for the main program and should be skipped. */
281 lm->l_name = NULL;
282
283 return lm;
284 }
285
286 static struct link_map *
287 next_link_map_member (so_list_ptr)
288 struct so_list *so_list_ptr;
289 {
290 struct link_map *lm = NULL;
291 static struct link_map next_lm;
292 #ifdef USE_LDR_ROUTINES
293 ldr_module_t mod_id = lm->module_info.lmi_modid;
294 size_t retsize;
295
296 if (ldr_next_module(inferior_pid, &mod_id) != 0
297 || mod_id == LDR_NULL_MODULE
298 || ldr_inq_module(inferior_pid, mod_id,
299 &next_lm.module_info, sizeof(ldr_module_info_t),
300 &retsize) != 0)
301 return lm;
302
303 lm = &next_lm;
304 lm->l_name = lm->module_info.lmi_name;
305 #else
306 CORE_ADDR ldr_context_addr;
307
308 /* Reread context in case ldr_context.tail was updated. */
309
310 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
311 (char *) &ldr_context_addr,
312 sizeof (CORE_ADDR)) != 0
313 || target_read_memory (ldr_context_addr,
314 (char *) &ldr_context,
315 sizeof (ldr_context_t)) != 0
316 || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
317 || target_read_memory (so_list_ptr->lm.module_info.next,
318 (char *) &next_lm.module_info,
319 sizeof (ldr_module_info_t)) != 0)
320 return lm;
321
322 lm = &next_lm;
323 lm->l_name = lm->module_info.module_name;
324 #endif
325 return lm;
326 }
327
328 static void
329 xfer_link_map_member (so_list_ptr, lm)
330 struct so_list *so_list_ptr;
331 struct link_map *lm;
332 {
333 so_list_ptr->lm = *lm;
334
335 /* OSF/1 has absolute addresses in shared libraries. */
336 LM_ADDR (so_list_ptr) = 0;
337
338 /* There is one entry that has no name (for the inferior executable)
339 since it is not a shared object. */
340 if (LM_NAME (so_list_ptr) != 0)
341 {
342
343 #ifdef USE_LDR_ROUTINES
344 int len = strlen (LM_NAME (so_list_ptr) + 1);
345
346 if (len > MAX_PATH_SIZE)
347 len = MAX_PATH_SIZE;
348 strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
349 #else
350 if (!target_read_string((CORE_ADDR) LM_NAME (so_list_ptr),
351 so_list_ptr->so_name, MAX_PATH_SIZE - 1))
352 error ("xfer_link_map_member: Can't read pathname for load map\n");
353 #endif
354 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = 0;
355
356 solib_map_sections (so_list_ptr);
357 }
358 }
359
360 /*
361
362 LOCAL FUNCTION
363
364 find_solib -- step through list of shared objects
365
366 SYNOPSIS
367
368 struct so_list *find_solib (struct so_list *so_list_ptr)
369
370 DESCRIPTION
371
372 This module contains the routine which finds the names of any
373 loaded "images" in the current process. The argument in must be
374 NULL on the first call, and then the returned value must be passed
375 in on subsequent calls. This provides the capability to "step" down
376 the list of loaded objects. On the last object, a NULL value is
377 returned.
378
379 The arg and return value are "struct link_map" pointers, as defined
380 in <link.h>.
381 */
382
383 static struct so_list *
384 find_solib (so_list_ptr)
385 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
386 {
387 struct so_list *so_list_next = NULL;
388 struct link_map *lm = NULL;
389 struct so_list *new;
390
391 if (so_list_ptr == NULL)
392 {
393 /* We are setting up for a new scan through the loaded images. */
394 if ((so_list_next = so_list_head) == NULL)
395 {
396 /* Find the first link map list member. */
397 lm = first_link_map_member ();
398 }
399 }
400 else
401 {
402 /* We have been called before, and are in the process of walking
403 the shared library list. Advance to the next shared object. */
404 lm = next_link_map_member (so_list_ptr);
405 so_list_next = so_list_ptr -> next;
406 }
407 if ((so_list_next == NULL) && (lm != NULL))
408 {
409 /* Get next link map structure from inferior image and build a local
410 abbreviated load_map structure */
411 new = (struct so_list *) xmalloc (sizeof (struct so_list));
412 memset ((char *) new, 0, sizeof (struct so_list));
413 new -> lmaddr = lm;
414 /* Add the new node as the next node in the list, or as the root
415 node if this is the first one. */
416 if (so_list_ptr != NULL)
417 {
418 so_list_ptr -> next = new;
419 }
420 else
421 {
422 so_list_head = new;
423 }
424 so_list_next = new;
425 xfer_link_map_member (new, lm);
426 }
427 return (so_list_next);
428 }
429
430 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
431
432 static int
433 symbol_add_stub (arg)
434 char *arg;
435 {
436 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
437
438 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
439 so -> textsection -> addr,
440 0, 0, 0);
441 return (1);
442 }
443
444 /*
445
446 GLOBAL FUNCTION
447
448 solib_add -- add a shared library file to the symtab and section list
449
450 SYNOPSIS
451
452 void solib_add (char *arg_string, int from_tty,
453 struct target_ops *target)
454
455 DESCRIPTION
456
457 */
458
459 void
460 solib_add (arg_string, from_tty, target)
461 char *arg_string;
462 int from_tty;
463 struct target_ops *target;
464 {
465 register struct so_list *so = NULL; /* link map state variable */
466
467 /* Last shared library that we read. */
468 struct so_list *so_last = NULL;
469
470 char *re_err;
471 int count;
472 int old;
473
474 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
475 {
476 error ("Invalid regexp: %s", re_err);
477 }
478
479
480 /* Add the shared library sections to the section table of the
481 specified target, if any. We have to do this before reading the
482 symbol files as symbol_file_add calls reinit_frame_cache and
483 creating a new frame might access memory in the shared library. */
484 if (target)
485 {
486 /* Count how many new section_table entries there are. */
487 so = NULL;
488 count = 0;
489 while ((so = find_solib (so)) != NULL)
490 {
491 if (so -> so_name[0])
492 {
493 count += so -> sections_end - so -> sections;
494 }
495 }
496
497 if (count)
498 {
499 /* Reallocate the target's section table including the new size. */
500 if (target -> to_sections)
501 {
502 old = target -> to_sections_end - target -> to_sections;
503 target -> to_sections = (struct section_table *)
504 xrealloc ((char *)target -> to_sections,
505 (sizeof (struct section_table)) * (count + old));
506 }
507 else
508 {
509 old = 0;
510 target -> to_sections = (struct section_table *)
511 xmalloc ((sizeof (struct section_table)) * count);
512 }
513 target -> to_sections_end = target -> to_sections + (count + old);
514
515 /* Add these section table entries to the target's table. */
516 while ((so = find_solib (so)) != NULL)
517 {
518 if (so -> so_name[0])
519 {
520 count = so -> sections_end - so -> sections;
521 memcpy ((char *) (target -> to_sections + old),
522 so -> sections,
523 (sizeof (struct section_table)) * count);
524 old += count;
525 }
526 }
527 }
528 }
529
530 /* Now add the symbol files. */
531 so = NULL;
532 while ((so = find_solib (so)) != NULL)
533 {
534 if (so -> so_name[0] && re_exec (so -> so_name))
535 {
536 so -> from_tty = from_tty;
537 if (so -> symbols_loaded)
538 {
539 if (from_tty)
540 {
541 printf ("Symbols already loaded for %s\n", so -> so_name);
542 }
543 }
544 else if (catch_errors
545 (symbol_add_stub, (char *) so,
546 "Error while reading shared library symbols:\n",
547 RETURN_MASK_ALL))
548 {
549 so_last = so;
550 so -> symbols_loaded = 1;
551 }
552 }
553 }
554 }
555
556 /*
557
558 LOCAL FUNCTION
559
560 info_sharedlibrary_command -- code for "info sharedlibrary"
561
562 SYNOPSIS
563
564 static void info_sharedlibrary_command ()
565
566 DESCRIPTION
567
568 Walk through the shared library list and print information
569 about each attached library.
570 */
571
572 static void
573 info_sharedlibrary_command (ignore, from_tty)
574 char *ignore;
575 int from_tty;
576 {
577 register struct so_list *so = NULL; /* link map state variable */
578 int header_done = 0;
579
580 if (exec_bfd == NULL)
581 {
582 printf ("No exec file.\n");
583 return;
584 }
585 while ((so = find_solib (so)) != NULL)
586 {
587 if (so -> so_name[0])
588 {
589 unsigned long txt_start = 0;
590 unsigned long txt_end = 0;
591
592 if (!header_done)
593 {
594 printf("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
595 "Shared Object Library");
596 header_done++;
597 }
598 if (so -> textsection)
599 {
600 txt_start = (unsigned long) so -> textsection -> addr;
601 txt_end = (unsigned long) so -> textsection -> endaddr;
602 }
603 printf ("%-20s", local_hex_string_custom (txt_start, "08l"));
604 printf ("%-20s", local_hex_string_custom (txt_end, "08l"));
605 printf ("%-12s", so -> symbols_loaded ? "Yes" : "No");
606 printf ("%s\n", so -> so_name);
607 }
608 }
609 if (so_list_head == NULL)
610 {
611 printf ("No shared libraries loaded at this time.\n");
612 }
613 }
614
615 /*
616
617 GLOBAL FUNCTION
618
619 solib_address -- check to see if an address is in a shared lib
620
621 SYNOPSIS
622
623 int solib_address (CORE_ADDR address)
624
625 DESCRIPTION
626
627 Provides a hook for other gdb routines to discover whether or
628 not a particular address is within the mapped address space of
629 a shared library. Any address between the base mapping address
630 and the first address beyond the end of the last mapping, is
631 considered to be within the shared library address space, for
632 our purposes.
633
634 For example, this routine is called at one point to disable
635 breakpoints which are in shared libraries that are not currently
636 mapped in.
637 */
638
639 int
640 solib_address (address)
641 CORE_ADDR address;
642 {
643 register struct so_list *so = 0; /* link map state variable */
644
645 while ((so = find_solib (so)) != NULL)
646 {
647 if (so -> so_name[0] && so -> textsection)
648 {
649 if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
650 (address < (CORE_ADDR) so -> textsection -> endaddr))
651 {
652 return (1);
653 }
654 }
655 }
656 return (0);
657 }
658
659 /* Called by free_all_symtabs */
660
661 void
662 clear_solib()
663 {
664 struct so_list *next;
665 char *bfd_filename;
666
667 while (so_list_head)
668 {
669 if (so_list_head -> sections)
670 {
671 free ((PTR)so_list_head -> sections);
672 }
673 if (so_list_head -> abfd)
674 {
675 bfd_filename = bfd_get_filename (so_list_head -> abfd);
676 bfd_close (so_list_head -> abfd);
677 }
678 else
679 /* This happens for the executable on SVR4. */
680 bfd_filename = NULL;
681
682 next = so_list_head -> next;
683 if (bfd_filename)
684 free ((PTR)bfd_filename);
685 free ((PTR)so_list_head);
686 so_list_head = next;
687 }
688 }
689
690 /*
691
692 GLOBAL FUNCTION
693
694 solib_create_inferior_hook -- shared library startup support
695
696 SYNOPSIS
697
698 void solib_create_inferior_hook()
699
700 DESCRIPTION
701
702 When gdb starts up the inferior, it nurses it along (through the
703 shell) until it is ready to execute it's first instruction. At this
704 point, this function gets called via expansion of the macro
705 SOLIB_CREATE_INFERIOR_HOOK.
706 For a statically bound executable, this first instruction is the
707 one at "_start", or a similar text label. No further processing is
708 needed in that case.
709 For a dynamically bound executable, this first instruction is somewhere
710 in the rld, and the actual user executable is not yet mapped in.
711 We continue the inferior again, rld then maps in the actual user
712 executable and any needed shared libraries and then sends
713 itself a SIGTRAP.
714 At that point we discover the names of all shared libraries and
715 read their symbols in.
716
717 FIXME
718
719 This code does not properly handle hitting breakpoints which the
720 user might have set in the rld itself. Proper handling would have
721 to check if the SIGTRAP happened due to a kill call.
722
723 Also, what if child has exit()ed? Must exit loop somehow.
724 */
725
726 void
727 solib_create_inferior_hook()
728 {
729
730 /* Nothing to do for statically bound executables. */
731
732 if (symfile_objfile == 0 || symfile_objfile->ei.entry_file_lowpc == stop_pc)
733 return;
734
735 /* Now run the target. It will eventually get a SIGTRAP, at
736 which point all of the libraries will have been mapped in and we
737 can go groveling around in the rld structures to find
738 out what we need to know about them. */
739
740 clear_proceed_status ();
741 stop_soon_quietly = 1;
742 stop_signal = 0;
743 do
744 {
745 target_resume (-1, 0, stop_signal);
746 wait_for_inferior ();
747 }
748 while (stop_signal != SIGTRAP);
749
750 /* solib_add will call reinit_frame_cache via symbol_file_add.
751 But we are stopped in the runtime loader and we do not have symbols
752 for the runtime loader. So heuristic_proc_start will be called
753 and will put out an annoying warning.
754 Resetting stop_soon_quietly after symbol loading suppresses
755 the warning. */
756 solib_add ((char *) 0, 0, (struct target_ops *) 0);
757 stop_soon_quietly = 0;
758 }
759
760
761 /*
762
763 LOCAL FUNCTION
764
765 sharedlibrary_command -- handle command to explicitly add library
766
767 SYNOPSIS
768
769 static void sharedlibrary_command (char *args, int from_tty)
770
771 DESCRIPTION
772
773 */
774
775 static void
776 sharedlibrary_command (args, from_tty)
777 char *args;
778 int from_tty;
779 {
780 dont_repeat ();
781 solib_add (args, from_tty, (struct target_ops *) 0);
782 }
783
784 void
785 _initialize_solib()
786 {
787
788 add_com ("sharedlibrary", class_files, sharedlibrary_command,
789 "Load shared object library symbols for files matching REGEXP.");
790 add_info ("sharedlibrary", info_sharedlibrary_command,
791 "Status of loaded shared object libraries.");
792 }
This page took 0.048042 seconds and 5 git commands to generate.