]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/solib-irix.c
2003-04-08 Elena Zannoni <ezannoni@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / solib-irix.c
CommitLineData
dabbe2c0
KB
1/* Shared library support for IRIX.
2 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file was created using portions of irix5-nat.c originally
6 contributed to GDB by Ian Lance Taylor.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25#include "defs.h"
26
27#include "symtab.h"
28#include "bfd.h"
29#include "symfile.h"
30#include "objfiles.h"
31#include "gdbcore.h"
32#include "target.h"
33#include "inferior.h"
34
35#include "solist.h"
36
37/* Link map info to include in an allocate so_list entry. Unlike some
38 of the other solib backends, this (Irix) backend chooses to decode
39 the link map info obtained from the target and store it as (mostly)
40 CORE_ADDRs which need no further decoding. This is more convenient
41 because there are three different link map formats to worry about.
42 We use a single routine (fetch_lm_info) to read (and decode) the target
43 specific link map data. */
44
45struct lm_info
46{
47 CORE_ADDR addr; /* address of obj_info or obj_list
48 struct on target (from which the
49 following information is obtained). */
50 CORE_ADDR next; /* address of next item in list. */
51 CORE_ADDR reloc_offset; /* amount to relocate by */
52 CORE_ADDR pathname_addr; /* address of pathname */
53 int pathname_len; /* length of pathname */
54};
55
56/* It's not desirable to use the system header files to obtain the
57 structure of the obj_list or obj_info structs. Therefore, we use a
58 platform neutral representation which has been derived from the IRIX
59 header files. */
60
61typedef struct
62{
63 char b[4];
64}
65gdb_int32_bytes;
66typedef struct
67{
68 char b[8];
69}
70gdb_int64_bytes;
71
72/* The "old" obj_list struct. This is used with old (o32) binaries.
73 The ``data'' member points at a much larger and more complicated
74 struct which we will only refer to by offsets. See
75 fetch_lm_info(). */
76
77struct irix_obj_list
78{
79 gdb_int32_bytes data;
80 gdb_int32_bytes next;
81 gdb_int32_bytes prev;
82};
83
84/* The ELF32 and ELF64 versions of the above struct. The oi_magic value
85 corresponds to the ``data'' value in the "old" struct. When this value
86 is 0xffffffff, the data will be in one of the following formats. The
87 ``oi_size'' field is used to decide which one we actually have. */
88
89struct irix_elf32_obj_info
90{
91 gdb_int32_bytes oi_magic;
92 gdb_int32_bytes oi_size;
93 gdb_int32_bytes oi_next;
94 gdb_int32_bytes oi_prev;
95 gdb_int32_bytes oi_ehdr;
96 gdb_int32_bytes oi_orig_ehdr;
97 gdb_int32_bytes oi_pathname;
98 gdb_int32_bytes oi_pathname_len;
99};
100
101struct irix_elf64_obj_info
102{
103 gdb_int32_bytes oi_magic;
104 gdb_int32_bytes oi_size;
105 gdb_int64_bytes oi_next;
106 gdb_int64_bytes oi_prev;
107 gdb_int64_bytes oi_ehdr;
108 gdb_int64_bytes oi_orig_ehdr;
109 gdb_int64_bytes oi_pathname;
110 gdb_int32_bytes oi_pathname_len;
111 gdb_int32_bytes padding;
112};
113
114/* Union of all of the above (plus a split out magic field). */
115
116union irix_obj_info
117{
118 gdb_int32_bytes magic;
119 struct irix_obj_list ol32;
120 struct irix_elf32_obj_info oi32;
121 struct irix_elf64_obj_info oi64;
122};
123
124/* MIPS sign extends its 32 bit addresses. We could conceivably use
125 extract_typed_address here, but to do so, we'd have to construct an
126 appropriate type. Calling extract_signed_integer or
127 extract_address seems simpler. */
128
129static CORE_ADDR
130extract_mips_address (void *addr, int len)
131{
132 if (len <= 32)
133 return extract_signed_integer (addr, len);
134 else
135 return extract_address (addr, len);
136}
137
138/* Fetch and return the link map data associated with ADDR. Note that
139 this routine automatically determines which (of three) link map
140 formats is in use by the target. */
141
142struct lm_info
143fetch_lm_info (CORE_ADDR addr)
144{
145 struct lm_info li;
146 union irix_obj_info buf;
147
148 li.addr = addr;
149
150 /* The smallest region that we'll need is for buf.ol32. We'll read
151 that first. We'll read more of the buffer later if we have to deal
152 with one of the other cases. (We don't want to incur a memory error
153 if we were to read a larger region that generates an error due to
154 being at the end of a page or the like.) */
155 read_memory (addr, (char *) &buf, sizeof (buf.ol32));
156
157 if (extract_unsigned_integer (&buf.magic, sizeof (buf.magic)) != 0xffffffff)
158 {
159 /* Use buf.ol32... */
160 char obj_buf[432];
161 CORE_ADDR obj_addr = extract_mips_address (&buf.ol32.data,
162 sizeof (buf.ol32.data));
163 li.next = extract_mips_address (&buf.ol32.next, sizeof (buf.ol32.next));
164
165 read_memory (obj_addr, obj_buf, sizeof (obj_buf));
166
167 li.pathname_addr = extract_mips_address (&obj_buf[236], 4);
168 li.pathname_len = 0; /* unknown */
169 li.reloc_offset = extract_mips_address (&obj_buf[196], 4)
170 - extract_mips_address (&obj_buf[248], 4);
171
172 }
173 else if (extract_unsigned_integer (&buf.oi32.oi_size,
174 sizeof (buf.oi32.oi_size))
175 == sizeof (buf.oi32))
176 {
177 /* Use buf.oi32... */
178
179 /* Read rest of buffer. */
180 read_memory (addr + sizeof (buf.ol32),
181 ((char *) &buf) + sizeof (buf.ol32),
182 sizeof (buf.oi32) - sizeof (buf.ol32));
183
184 /* Fill in fields using buffer contents. */
185 li.next = extract_mips_address (&buf.oi32.oi_next,
186 sizeof (buf.oi32.oi_next));
187 li.reloc_offset = extract_mips_address (&buf.oi32.oi_ehdr,
188 sizeof (buf.oi32.oi_ehdr))
189 - extract_mips_address (&buf.oi32.oi_orig_ehdr,
190 sizeof (buf.oi32.oi_orig_ehdr));
191 li.pathname_addr = extract_mips_address (&buf.oi32.oi_pathname,
192 sizeof (buf.oi32.oi_pathname));
193 li.pathname_len = extract_unsigned_integer (&buf.oi32.oi_pathname_len,
194 sizeof (buf.oi32.
195 oi_pathname_len));
196 }
197 else if (extract_unsigned_integer (&buf.oi64.oi_size,
198 sizeof (buf.oi64.oi_size))
199 == sizeof (buf.oi64))
200 {
201 /* Use buf.oi64... */
202
203 /* Read rest of buffer. */
204 read_memory (addr + sizeof (buf.ol32),
205 ((char *) &buf) + sizeof (buf.ol32),
206 sizeof (buf.oi64) - sizeof (buf.ol32));
207
208 /* Fill in fields using buffer contents. */
209 li.next = extract_mips_address (&buf.oi64.oi_next,
210 sizeof (buf.oi64.oi_next));
211 li.reloc_offset = extract_mips_address (&buf.oi64.oi_ehdr,
212 sizeof (buf.oi64.oi_ehdr))
213 - extract_mips_address (&buf.oi64.oi_orig_ehdr,
214 sizeof (buf.oi64.oi_orig_ehdr));
215 li.pathname_addr = extract_mips_address (&buf.oi64.oi_pathname,
216 sizeof (buf.oi64.oi_pathname));
217 li.pathname_len = extract_unsigned_integer (&buf.oi64.oi_pathname_len,
218 sizeof (buf.oi64.
219 oi_pathname_len));
220 }
221 else
222 {
223 error ("Unable to fetch shared library obj_info or obj_list info.");
224 }
225
226 return li;
227}
228
229/* The symbol which starts off the list of shared libraries. */
230#define DEBUG_BASE "__rld_obj_head"
231
232char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
233
234static CORE_ADDR debug_base; /* Base of dynamic linker structures */
235static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
236
237/*
238
239 LOCAL FUNCTION
240
241 locate_base -- locate the base address of dynamic linker structs
242
243 SYNOPSIS
244
245 CORE_ADDR locate_base (void)
246
247 DESCRIPTION
248
249 For both the SunOS and SVR4 shared library implementations, if the
250 inferior executable has been linked dynamically, there is a single
251 address somewhere in the inferior's data space which is the key to
252 locating all of the dynamic linker's runtime structures. This
253 address is the value of the symbol defined by the macro DEBUG_BASE.
254 The job of this function is to find and return that address, or to
255 return 0 if there is no such address (the executable is statically
256 linked for example).
257
258 For SunOS, the job is almost trivial, since the dynamic linker and
259 all of it's structures are statically linked to the executable at
260 link time. Thus the symbol for the address we are looking for has
261 already been added to the minimal symbol table for the executable's
262 objfile at the time the symbol file's symbols were read, and all we
263 have to do is look it up there. Note that we explicitly do NOT want
264 to find the copies in the shared library.
265
266 The SVR4 version is much more complicated because the dynamic linker
267 and it's structures are located in the shared C library, which gets
268 run as the executable's "interpreter" by the kernel. We have to go
269 to a lot more work to discover the address of DEBUG_BASE. Because
270 of this complexity, we cache the value we find and return that value
271 on subsequent invocations. Note there is no copy in the executable
272 symbol tables.
273
274 Irix 5 is basically like SunOS.
275
276 Note that we can assume nothing about the process state at the time
277 we need to find this address. We may be stopped on the first instruc-
278 tion of the interpreter (C shared library), the first instruction of
279 the executable itself, or somewhere else entirely (if we attached
280 to the process for example).
281
282 */
283
284static CORE_ADDR
285locate_base (void)
286{
287 struct minimal_symbol *msymbol;
288 CORE_ADDR address = 0;
289
290 msymbol = lookup_minimal_symbol (DEBUG_BASE, NULL, symfile_objfile);
291 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
292 {
293 address = SYMBOL_VALUE_ADDRESS (msymbol);
294 }
295 return (address);
296}
297
298/*
299
300 LOCAL FUNCTION
301
302 disable_break -- remove the "mapping changed" breakpoint
303
304 SYNOPSIS
305
306 static int disable_break ()
307
308 DESCRIPTION
309
310 Removes the breakpoint that gets hit when the dynamic linker
311 completes a mapping change.
312
313 */
314
315static int
316disable_break (void)
317{
318 int status = 1;
319
320
321 /* Note that breakpoint address and original contents are in our address
322 space, so we just need to write the original contents back. */
323
324 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
325 {
326 status = 0;
327 }
328
329 /* For the SVR4 version, we always know the breakpoint address. For the
330 SunOS version we don't know it until the above code is executed.
331 Grumble if we are stopped anywhere besides the breakpoint address. */
332
333 if (stop_pc != breakpoint_addr)
334 {
335 warning
336 ("stopped at unknown breakpoint while handling shared libraries");
337 }
338
339 return (status);
340}
341
342/*
343
344 LOCAL FUNCTION
345
346 enable_break -- arrange for dynamic linker to hit breakpoint
347
348 SYNOPSIS
349
350 int enable_break (void)
351
352 DESCRIPTION
353
354 This functions inserts a breakpoint at the entry point of the
355 main executable, where all shared libraries are mapped in.
356 */
357
358static int
359enable_break (void)
360{
361 if (symfile_objfile != NULL
362 && target_insert_breakpoint (symfile_objfile->ei.entry_point,
363 shadow_contents) == 0)
364 {
365 breakpoint_addr = symfile_objfile->ei.entry_point;
366 return 1;
367 }
368
369 return 0;
370}
371
372/*
373
374 LOCAL FUNCTION
375
376 irix_solib_create_inferior_hook -- shared library startup support
377
378 SYNOPSIS
379
380 void solib_create_inferior_hook()
381
382 DESCRIPTION
383
384 When gdb starts up the inferior, it nurses it along (through the
385 shell) until it is ready to execute it's first instruction. At this
386 point, this function gets called via expansion of the macro
387 SOLIB_CREATE_INFERIOR_HOOK.
388
389 For SunOS executables, this first instruction is typically the
390 one at "_start", or a similar text label, regardless of whether
391 the executable is statically or dynamically linked. The runtime
392 startup code takes care of dynamically linking in any shared
393 libraries, once gdb allows the inferior to continue.
394
395 For SVR4 executables, this first instruction is either the first
396 instruction in the dynamic linker (for dynamically linked
397 executables) or the instruction at "start" for statically linked
398 executables. For dynamically linked executables, the system
399 first exec's /lib/libc.so.N, which contains the dynamic linker,
400 and starts it running. The dynamic linker maps in any needed
401 shared libraries, maps in the actual user executable, and then
402 jumps to "start" in the user executable.
403
404 For both SunOS shared libraries, and SVR4 shared libraries, we
405 can arrange to cooperate with the dynamic linker to discover the
406 names of shared libraries that are dynamically linked, and the
407 base addresses to which they are linked.
408
409 This function is responsible for discovering those names and
410 addresses, and saving sufficient information about them to allow
411 their symbols to be read at a later time.
412
413 FIXME
414
415 Between enable_break() and disable_break(), this code does not
416 properly handle hitting breakpoints which the user might have
417 set in the startup code or in the dynamic linker itself. Proper
418 handling will probably have to wait until the implementation is
419 changed to use the "breakpoint handler function" method.
420
421 Also, what if child has exit()ed? Must exit loop somehow.
422 */
423
424static void
425irix_solib_create_inferior_hook (void)
426{
427 if (!enable_break ())
428 {
429 warning ("shared library handler failed to enable breakpoint");
430 return;
431 }
432
433 /* Now run the target. It will eventually hit the breakpoint, at
434 which point all of the libraries will have been mapped in and we
435 can go groveling around in the dynamic linker structures to find
436 out what we need to know about them. */
437
438 clear_proceed_status ();
c0236d92 439 stop_soon = STOP_QUIETLY;
dabbe2c0
KB
440 stop_signal = TARGET_SIGNAL_0;
441 do
442 {
443 target_resume (pid_to_ptid (-1), 0, stop_signal);
444 wait_for_inferior ();
445 }
446 while (stop_signal != TARGET_SIGNAL_TRAP);
447
448 /* We are now either at the "mapping complete" breakpoint (or somewhere
449 else, a condition we aren't prepared to deal with anyway), so adjust
450 the PC as necessary after a breakpoint, disable the breakpoint, and
451 add any shared libraries that were mapped in. */
452
453 if (!disable_break ())
454 {
455 warning ("shared library handler failed to disable breakpoint");
456 }
457
458 /* solib_add will call reinit_frame_cache.
459 But we are stopped in the startup code and we might not have symbols
460 for the startup code, so heuristic_proc_start could be called
461 and will put out an annoying warning.
c0236d92 462 Delaying the resetting of stop_soon until after symbol loading
dabbe2c0
KB
463 suppresses the warning. */
464 solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
c0236d92 465 stop_soon = NO_STOP_QUIETLY;
dabbe2c0
KB
466 re_enable_breakpoints_in_shlibs ();
467}
468
469/* LOCAL FUNCTION
470
471 current_sos -- build a list of currently loaded shared objects
472
473 SYNOPSIS
474
475 struct so_list *current_sos ()
476
477 DESCRIPTION
478
479 Build a list of `struct so_list' objects describing the shared
480 objects currently loaded in the inferior. This list does not
481 include an entry for the main executable file.
482
483 Note that we only gather information directly available from the
484 inferior --- we don't examine any of the shared library files
485 themselves. The declaration of `struct so_list' says which fields
486 we provide values for. */
487
488static struct so_list *
489irix_current_sos (void)
490{
491 CORE_ADDR lma;
492 char addr_buf[8];
493 struct so_list *head = 0;
494 struct so_list **link_ptr = &head;
495 int is_first = 1;
496 struct lm_info lm;
497
498 /* Make sure we've looked up the inferior's dynamic linker's base
499 structure. */
500 if (!debug_base)
501 {
502 debug_base = locate_base ();
503
504 /* If we can't find the dynamic linker's base structure, this
505 must not be a dynamically linked executable. Hmm. */
506 if (!debug_base)
507 return 0;
508 }
509
510 read_memory (debug_base, addr_buf, TARGET_ADDR_BIT / TARGET_CHAR_BIT);
511 lma = extract_mips_address (addr_buf, TARGET_ADDR_BIT / TARGET_CHAR_BIT);
512
513 while (lma)
514 {
515 lm = fetch_lm_info (lma);
516 if (!is_first)
517 {
518 int errcode;
519 char *name_buf;
520 int name_size;
521 struct so_list *new
522 = (struct so_list *) xmalloc (sizeof (struct so_list));
523 struct cleanup *old_chain = make_cleanup (xfree, new);
524
525 memset (new, 0, sizeof (*new));
526
527 new->lm_info = xmalloc (sizeof (struct lm_info));
528 make_cleanup (xfree, new->lm_info);
529
530 *new->lm_info = lm;
531
532 /* Extract this shared object's name. */
533 name_size = lm.pathname_len;
534 if (name_size == 0)
535 name_size = SO_NAME_MAX_PATH_SIZE - 1;
536
537 if (name_size >= SO_NAME_MAX_PATH_SIZE)
538 {
539 name_size = SO_NAME_MAX_PATH_SIZE - 1;
540 warning
541 ("current_sos: truncating name of %d characters to only %d characters",
542 lm.pathname_len, name_size);
543 }
544
545 target_read_string (lm.pathname_addr, &name_buf,
546 name_size, &errcode);
547 if (errcode != 0)
548 {
549 warning ("current_sos: Can't read pathname for load map: %s\n",
550 safe_strerror (errcode));
551 }
552 else
553 {
554 strncpy (new->so_name, name_buf, name_size);
555 new->so_name[name_size] = '\0';
556 xfree (name_buf);
557 strcpy (new->so_original_name, new->so_name);
558 }
559
560 new->next = 0;
561 *link_ptr = new;
562 link_ptr = &new->next;
563
564 discard_cleanups (old_chain);
565 }
566 is_first = 0;
567 lma = lm.next;
568 }
569
570 return head;
571}
572
573/*
574
575 LOCAL FUNCTION
576
577 irix_open_symbol_file_object
578
579 SYNOPSIS
580
581 void irix_open_symbol_file_object (void *from_tty)
582
583 DESCRIPTION
584
585 If no open symbol file, attempt to locate and open the main symbol
586 file. On IRIX, this is the first link map entry. If its name is
587 here, we can open it. Useful when attaching to a process without
588 first loading its symbol file.
589
590 If FROM_TTYP dereferences to a non-zero integer, allow messages to
591 be printed. This parameter is a pointer rather than an int because
592 open_symbol_file_object() is called via catch_errors() and
593 catch_errors() requires a pointer argument. */
594
595static int
596irix_open_symbol_file_object (void *from_ttyp)
597{
598 CORE_ADDR lma;
599 char addr_buf[8];
600 struct lm_info lm;
601 struct cleanup *cleanups;
602 int errcode;
603 int from_tty = *(int *) from_ttyp;
604 char *filename;
605
606 if (symfile_objfile)
607 if (!query ("Attempt to reload symbols from process? "))
608 return 0;
609
610 if ((debug_base = locate_base ()) == 0)
611 return 0; /* failed somehow... */
612
613 /* First link map member should be the executable. */
614 read_memory (debug_base, addr_buf, TARGET_ADDR_BIT / TARGET_CHAR_BIT);
615 lma = extract_mips_address (addr_buf, TARGET_ADDR_BIT / TARGET_CHAR_BIT);
616 if (lma == 0)
617 return 0; /* failed somehow... */
618
619 lm = fetch_lm_info (lma);
620
621 if (lm.pathname_addr == 0)
622 return 0; /* No filename. */
623
624 /* Now fetch the filename from target memory. */
625 target_read_string (lm.pathname_addr, &filename, SO_NAME_MAX_PATH_SIZE - 1,
626 &errcode);
627
628 if (errcode)
629 {
630 warning ("failed to read exec filename from attached file: %s",
631 safe_strerror (errcode));
632 return 0;
633 }
634
635 cleanups = make_cleanup (xfree, filename);
636 /* Have a pathname: read the symbol file. */
637 symbol_file_add_main (filename, from_tty);
638
639 do_cleanups (cleanups);
640
641 return 1;
642}
643
644
645/*
646
647 LOCAL FUNCTION
648
649 irix_special_symbol_handling -- additional shared library symbol handling
650
651 SYNOPSIS
652
653 void irix_special_symbol_handling ()
654
655 DESCRIPTION
656
657 Once the symbols from a shared object have been loaded in the usual
658 way, we are called to do any system specific symbol handling that
659 is needed.
660
661 For SunOS4, this consisted of grunging around in the dynamic
662 linkers structures to find symbol definitions for "common" symbols
663 and adding them to the minimal symbol table for the runtime common
664 objfile.
665
666 However, for IRIX, there's nothing to do.
667
668 */
669
670static void
671irix_special_symbol_handling (void)
672{
673}
674
675/* Using the solist entry SO, relocate the addresses in SEC. */
676
677static void
678irix_relocate_section_addresses (struct so_list *so,
679 struct section_table *sec)
680{
681 sec->addr += so->lm_info->reloc_offset;
682 sec->endaddr += so->lm_info->reloc_offset;
683}
684
685/* Free the lm_info struct. */
686
687static void
688irix_free_so (struct so_list *so)
689{
690 xfree (so->lm_info);
691}
692
693/* Clear backend specific state. */
694
695static void
696irix_clear_solib (void)
697{
698 debug_base = 0;
699}
700
701/* Return 1 if PC lies in the dynamic symbol resolution code of the
702 run time loader. */
703static int
704irix_in_dynsym_resolve_code (CORE_ADDR pc)
705{
706 return 0;
707}
708
709static struct target_so_ops irix_so_ops;
710
711void
712_initialize_irix_solib (void)
713{
714 irix_so_ops.relocate_section_addresses = irix_relocate_section_addresses;
715 irix_so_ops.free_so = irix_free_so;
716 irix_so_ops.clear_solib = irix_clear_solib;
717 irix_so_ops.solib_create_inferior_hook = irix_solib_create_inferior_hook;
718 irix_so_ops.special_symbol_handling = irix_special_symbol_handling;
719 irix_so_ops.current_sos = irix_current_sos;
720 irix_so_ops.open_symbol_file_object = irix_open_symbol_file_object;
721 irix_so_ops.in_dynsym_resolve_code = irix_in_dynsym_resolve_code;
722
723 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
724 current_target_so_ops = &irix_so_ops;
725}