Update
[gdb.git] / gdb / solib-irix.c
blob14f9fc24f6533f11ce346e682724b8ea1bdef865
1 /* Shared library support for IRIX.
2 Copyright (C) 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2004,
3 2007, 2008 Free Software Foundation, Inc.
5 This file was created using portions of irix5-nat.c originally
6 contributed to GDB by Ian Lance Taylor.
8 This file is part of GDB.
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 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "defs.h"
25 #include "symtab.h"
26 #include "bfd.h"
27 /* FIXME: ezannoni/2004-02-13 Verify that the include below is
28 really needed. */
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdbcore.h"
32 #include "target.h"
33 #include "inferior.h"
35 #include "solist.h"
36 #include "solib.h"
37 #include "solib-irix.h"
40 /* Link map info to include in an allocate so_list entry. Unlike some
41 of the other solib backends, this (Irix) backend chooses to decode
42 the link map info obtained from the target and store it as (mostly)
43 CORE_ADDRs which need no further decoding. This is more convenient
44 because there are three different link map formats to worry about.
45 We use a single routine (fetch_lm_info) to read (and decode) the target
46 specific link map data. */
48 struct lm_info
50 CORE_ADDR addr; /* address of obj_info or obj_list
51 struct on target (from which the
52 following information is obtained). */
53 CORE_ADDR next; /* address of next item in list. */
54 CORE_ADDR reloc_offset; /* amount to relocate by */
55 CORE_ADDR pathname_addr; /* address of pathname */
56 int pathname_len; /* length of pathname */
59 /* It's not desirable to use the system header files to obtain the
60 structure of the obj_list or obj_info structs. Therefore, we use a
61 platform neutral representation which has been derived from the IRIX
62 header files. */
64 typedef struct
66 gdb_byte b[4];
68 gdb_int32_bytes;
69 typedef struct
71 gdb_byte b[8];
73 gdb_int64_bytes;
75 /* The "old" obj_list struct. This is used with old (o32) binaries.
76 The ``data'' member points at a much larger and more complicated
77 struct which we will only refer to by offsets. See
78 fetch_lm_info(). */
80 struct irix_obj_list
82 gdb_int32_bytes data;
83 gdb_int32_bytes next;
84 gdb_int32_bytes prev;
87 /* The ELF32 and ELF64 versions of the above struct. The oi_magic value
88 corresponds to the ``data'' value in the "old" struct. When this value
89 is 0xffffffff, the data will be in one of the following formats. The
90 ``oi_size'' field is used to decide which one we actually have. */
92 struct irix_elf32_obj_info
94 gdb_int32_bytes oi_magic;
95 gdb_int32_bytes oi_size;
96 gdb_int32_bytes oi_next;
97 gdb_int32_bytes oi_prev;
98 gdb_int32_bytes oi_ehdr;
99 gdb_int32_bytes oi_orig_ehdr;
100 gdb_int32_bytes oi_pathname;
101 gdb_int32_bytes oi_pathname_len;
104 struct irix_elf64_obj_info
106 gdb_int32_bytes oi_magic;
107 gdb_int32_bytes oi_size;
108 gdb_int64_bytes oi_next;
109 gdb_int64_bytes oi_prev;
110 gdb_int64_bytes oi_ehdr;
111 gdb_int64_bytes oi_orig_ehdr;
112 gdb_int64_bytes oi_pathname;
113 gdb_int32_bytes oi_pathname_len;
114 gdb_int32_bytes padding;
117 /* Union of all of the above (plus a split out magic field). */
119 union irix_obj_info
121 gdb_int32_bytes magic;
122 struct irix_obj_list ol32;
123 struct irix_elf32_obj_info oi32;
124 struct irix_elf64_obj_info oi64;
127 /* MIPS sign extends its 32 bit addresses. We could conceivably use
128 extract_typed_address here, but to do so, we'd have to construct an
129 appropriate type. Calling extract_signed_integer seems simpler. */
131 static CORE_ADDR
132 extract_mips_address (void *addr, int len)
134 return extract_signed_integer (addr, len);
137 /* Fetch and return the link map data associated with ADDR. Note that
138 this routine automatically determines which (of three) link map
139 formats is in use by the target. */
141 struct lm_info
142 fetch_lm_info (CORE_ADDR addr)
144 struct lm_info li;
145 union irix_obj_info buf;
147 li.addr = addr;
149 /* The smallest region that we'll need is for buf.ol32. We'll read
150 that first. We'll read more of the buffer later if we have to deal
151 with one of the other cases. (We don't want to incur a memory error
152 if we were to read a larger region that generates an error due to
153 being at the end of a page or the like.) */
154 read_memory (addr, (char *) &buf, sizeof (buf.ol32));
156 if (extract_unsigned_integer (buf.magic.b, sizeof (buf.magic)) != 0xffffffff)
158 /* Use buf.ol32... */
159 char obj_buf[432];
160 CORE_ADDR obj_addr = extract_mips_address (&buf.ol32.data,
161 sizeof (buf.ol32.data));
162 li.next = extract_mips_address (&buf.ol32.next, sizeof (buf.ol32.next));
164 read_memory (obj_addr, obj_buf, sizeof (obj_buf));
166 li.pathname_addr = extract_mips_address (&obj_buf[236], 4);
167 li.pathname_len = 0; /* unknown */
168 li.reloc_offset = extract_mips_address (&obj_buf[196], 4)
169 - extract_mips_address (&obj_buf[248], 4);
172 else if (extract_unsigned_integer (buf.oi32.oi_size.b,
173 sizeof (buf.oi32.oi_size))
174 == sizeof (buf.oi32))
176 /* Use buf.oi32... */
178 /* Read rest of buffer. */
179 read_memory (addr + sizeof (buf.ol32),
180 ((char *) &buf) + sizeof (buf.ol32),
181 sizeof (buf.oi32) - sizeof (buf.ol32));
183 /* Fill in fields using buffer contents. */
184 li.next = extract_mips_address (&buf.oi32.oi_next,
185 sizeof (buf.oi32.oi_next));
186 li.reloc_offset = extract_mips_address (&buf.oi32.oi_ehdr,
187 sizeof (buf.oi32.oi_ehdr))
188 - extract_mips_address (&buf.oi32.oi_orig_ehdr,
189 sizeof (buf.oi32.oi_orig_ehdr));
190 li.pathname_addr = extract_mips_address (&buf.oi32.oi_pathname,
191 sizeof (buf.oi32.oi_pathname));
192 li.pathname_len = extract_unsigned_integer (buf.oi32.oi_pathname_len.b,
193 sizeof (buf.oi32.
194 oi_pathname_len));
196 else if (extract_unsigned_integer (buf.oi64.oi_size.b,
197 sizeof (buf.oi64.oi_size))
198 == sizeof (buf.oi64))
200 /* Use buf.oi64... */
202 /* Read rest of buffer. */
203 read_memory (addr + sizeof (buf.ol32),
204 ((char *) &buf) + sizeof (buf.ol32),
205 sizeof (buf.oi64) - sizeof (buf.ol32));
207 /* Fill in fields using buffer contents. */
208 li.next = extract_mips_address (&buf.oi64.oi_next,
209 sizeof (buf.oi64.oi_next));
210 li.reloc_offset = extract_mips_address (&buf.oi64.oi_ehdr,
211 sizeof (buf.oi64.oi_ehdr))
212 - extract_mips_address (&buf.oi64.oi_orig_ehdr,
213 sizeof (buf.oi64.oi_orig_ehdr));
214 li.pathname_addr = extract_mips_address (&buf.oi64.oi_pathname,
215 sizeof (buf.oi64.oi_pathname));
216 li.pathname_len = extract_unsigned_integer (buf.oi64.oi_pathname_len.b,
217 sizeof (buf.oi64.
218 oi_pathname_len));
220 else
222 error (_("Unable to fetch shared library obj_info or obj_list info."));
225 return li;
228 /* The symbol which starts off the list of shared libraries. */
229 #define DEBUG_BASE "__rld_obj_head"
231 static void *base_breakpoint;
233 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
237 LOCAL FUNCTION
239 locate_base -- locate the base address of dynamic linker structs
241 SYNOPSIS
243 CORE_ADDR locate_base (void)
245 DESCRIPTION
247 For both the SunOS and SVR4 shared library implementations, if the
248 inferior executable has been linked dynamically, there is a single
249 address somewhere in the inferior's data space which is the key to
250 locating all of the dynamic linker's runtime structures. This
251 address is the value of the symbol defined by the macro DEBUG_BASE.
252 The job of this function is to find and return that address, or to
253 return 0 if there is no such address (the executable is statically
254 linked for example).
256 For SunOS, the job is almost trivial, since the dynamic linker and
257 all of it's structures are statically linked to the executable at
258 link time. Thus the symbol for the address we are looking for has
259 already been added to the minimal symbol table for the executable's
260 objfile at the time the symbol file's symbols were read, and all we
261 have to do is look it up there. Note that we explicitly do NOT want
262 to find the copies in the shared library.
264 The SVR4 version is much more complicated because the dynamic linker
265 and it's structures are located in the shared C library, which gets
266 run as the executable's "interpreter" by the kernel. We have to go
267 to a lot more work to discover the address of DEBUG_BASE. Because
268 of this complexity, we cache the value we find and return that value
269 on subsequent invocations. Note there is no copy in the executable
270 symbol tables.
272 Irix 5 is basically like SunOS.
274 Note that we can assume nothing about the process state at the time
275 we need to find this address. We may be stopped on the first instruc-
276 tion of the interpreter (C shared library), the first instruction of
277 the executable itself, or somewhere else entirely (if we attached
278 to the process for example).
282 static CORE_ADDR
283 locate_base (void)
285 struct minimal_symbol *msymbol;
286 CORE_ADDR address = 0;
288 msymbol = lookup_minimal_symbol (DEBUG_BASE, NULL, symfile_objfile);
289 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
291 address = SYMBOL_VALUE_ADDRESS (msymbol);
293 return (address);
298 LOCAL FUNCTION
300 disable_break -- remove the "mapping changed" breakpoint
302 SYNOPSIS
304 static int disable_break ()
306 DESCRIPTION
308 Removes the breakpoint that gets hit when the dynamic linker
309 completes a mapping change.
313 static int
314 disable_break (void)
316 int status = 1;
319 /* Note that breakpoint address and original contents are in our address
320 space, so we just need to write the original contents back. */
322 if (deprecated_remove_raw_breakpoint (base_breakpoint) != 0)
324 status = 0;
327 base_breakpoint = NULL;
329 /* Note that it is possible that we have stopped at a location that
330 is different from the location where we inserted our breakpoint.
331 On mips-irix, we can actually land in __dbx_init(), so we should
332 not check the PC against our breakpoint address here. See procfs.c
333 for more details. */
335 return (status);
340 LOCAL FUNCTION
342 enable_break -- arrange for dynamic linker to hit breakpoint
344 SYNOPSIS
346 int enable_break (void)
348 DESCRIPTION
350 This functions inserts a breakpoint at the entry point of the
351 main executable, where all shared libraries are mapped in.
354 static int
355 enable_break (void)
357 if (symfile_objfile != NULL)
359 base_breakpoint
360 = deprecated_insert_raw_breakpoint (entry_point_address ());
362 if (base_breakpoint != NULL)
363 return 1;
366 return 0;
371 LOCAL FUNCTION
373 irix_solib_create_inferior_hook -- shared library startup support
375 SYNOPSIS
377 void solib_create_inferior_hook ()
379 DESCRIPTION
381 When gdb starts up the inferior, it nurses it along (through the
382 shell) until it is ready to execute it's first instruction. At this
383 point, this function gets called via expansion of the macro
384 SOLIB_CREATE_INFERIOR_HOOK.
386 For SunOS executables, this first instruction is typically the
387 one at "_start", or a similar text label, regardless of whether
388 the executable is statically or dynamically linked. The runtime
389 startup code takes care of dynamically linking in any shared
390 libraries, once gdb allows the inferior to continue.
392 For SVR4 executables, this first instruction is either the first
393 instruction in the dynamic linker (for dynamically linked
394 executables) or the instruction at "start" for statically linked
395 executables. For dynamically linked executables, the system
396 first exec's /lib/libc.so.N, which contains the dynamic linker,
397 and starts it running. The dynamic linker maps in any needed
398 shared libraries, maps in the actual user executable, and then
399 jumps to "start" in the user executable.
401 For both SunOS shared libraries, and SVR4 shared libraries, we
402 can arrange to cooperate with the dynamic linker to discover the
403 names of shared libraries that are dynamically linked, and the
404 base addresses to which they are linked.
406 This function is responsible for discovering those names and
407 addresses, and saving sufficient information about them to allow
408 their symbols to be read at a later time.
410 FIXME
412 Between enable_break() and disable_break(), this code does not
413 properly handle hitting breakpoints which the user might have
414 set in the startup code or in the dynamic linker itself. Proper
415 handling will probably have to wait until the implementation is
416 changed to use the "breakpoint handler function" method.
418 Also, what if child has exit()ed? Must exit loop somehow.
421 static void
422 irix_solib_create_inferior_hook (void)
424 if (!enable_break ())
426 warning (_("shared library handler failed to enable breakpoint"));
427 return;
430 /* Now run the target. It will eventually hit the breakpoint, at
431 which point all of the libraries will have been mapped in and we
432 can go groveling around in the dynamic linker structures to find
433 out what we need to know about them. */
435 clear_proceed_status ();
436 stop_soon = STOP_QUIETLY;
437 stop_signal = TARGET_SIGNAL_0;
440 target_resume (pid_to_ptid (-1), 0, stop_signal);
441 wait_for_inferior (0);
443 while (stop_signal != TARGET_SIGNAL_TRAP);
445 /* We are now either at the "mapping complete" breakpoint (or somewhere
446 else, a condition we aren't prepared to deal with anyway), so adjust
447 the PC as necessary after a breakpoint, disable the breakpoint, and
448 add any shared libraries that were mapped in. */
450 if (!disable_break ())
452 warning (_("shared library handler failed to disable breakpoint"));
455 /* solib_add will call reinit_frame_cache.
456 But we are stopped in the startup code and we might not have symbols
457 for the startup code, so heuristic_proc_start could be called
458 and will put out an annoying warning.
459 Delaying the resetting of stop_soon until after symbol loading
460 suppresses the warning. */
461 solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
462 stop_soon = NO_STOP_QUIETLY;
465 /* LOCAL FUNCTION
467 current_sos -- build a list of currently loaded shared objects
469 SYNOPSIS
471 struct so_list *current_sos ()
473 DESCRIPTION
475 Build a list of `struct so_list' objects describing the shared
476 objects currently loaded in the inferior. This list does not
477 include an entry for the main executable file.
479 Note that we only gather information directly available from the
480 inferior --- we don't examine any of the shared library files
481 themselves. The declaration of `struct so_list' says which fields
482 we provide values for. */
484 static struct so_list *
485 irix_current_sos (void)
487 CORE_ADDR lma;
488 char addr_buf[8];
489 struct so_list *head = 0;
490 struct so_list **link_ptr = &head;
491 int is_first = 1;
492 struct lm_info lm;
494 /* Make sure we've looked up the inferior's dynamic linker's base
495 structure. */
496 if (!debug_base)
498 debug_base = locate_base ();
500 /* If we can't find the dynamic linker's base structure, this
501 must not be a dynamically linked executable. Hmm. */
502 if (!debug_base)
503 return 0;
506 read_memory (debug_base,
507 addr_buf,
508 gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT);
509 lma = extract_mips_address (addr_buf,
510 gdbarch_addr_bit (current_gdbarch)
511 / TARGET_CHAR_BIT);
513 while (lma)
515 lm = fetch_lm_info (lma);
516 if (!is_first)
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);
525 memset (new, 0, sizeof (*new));
527 new->lm_info = xmalloc (sizeof (struct lm_info));
528 make_cleanup (xfree, new->lm_info);
530 *new->lm_info = lm;
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;
537 if (name_size >= SO_NAME_MAX_PATH_SIZE)
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);
545 target_read_string (lm.pathname_addr, &name_buf,
546 name_size, &errcode);
547 if (errcode != 0)
548 warning (_("Can't read pathname for load map: %s."),
549 safe_strerror (errcode));
550 else
552 strncpy (new->so_name, name_buf, name_size);
553 new->so_name[name_size] = '\0';
554 xfree (name_buf);
555 strcpy (new->so_original_name, new->so_name);
558 new->next = 0;
559 *link_ptr = new;
560 link_ptr = &new->next;
562 discard_cleanups (old_chain);
564 is_first = 0;
565 lma = lm.next;
568 return head;
573 LOCAL FUNCTION
575 irix_open_symbol_file_object
577 SYNOPSIS
579 void irix_open_symbol_file_object (void *from_tty)
581 DESCRIPTION
583 If no open symbol file, attempt to locate and open the main symbol
584 file. On IRIX, this is the first link map entry. If its name is
585 here, we can open it. Useful when attaching to a process without
586 first loading its symbol file.
588 If FROM_TTYP dereferences to a non-zero integer, allow messages to
589 be printed. This parameter is a pointer rather than an int because
590 open_symbol_file_object() is called via catch_errors() and
591 catch_errors() requires a pointer argument. */
593 static int
594 irix_open_symbol_file_object (void *from_ttyp)
596 CORE_ADDR lma;
597 char addr_buf[8];
598 struct lm_info lm;
599 struct cleanup *cleanups;
600 int errcode;
601 int from_tty = *(int *) from_ttyp;
602 char *filename;
604 if (symfile_objfile)
605 if (!query ("Attempt to reload symbols from process? "))
606 return 0;
608 if ((debug_base = locate_base ()) == 0)
609 return 0; /* failed somehow... */
611 /* First link map member should be the executable. */
612 read_memory (debug_base,
613 addr_buf,
614 gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT);
615 lma = extract_mips_address (addr_buf,
616 gdbarch_addr_bit (current_gdbarch)
617 / TARGET_CHAR_BIT);
618 if (lma == 0)
619 return 0; /* failed somehow... */
621 lm = fetch_lm_info (lma);
623 if (lm.pathname_addr == 0)
624 return 0; /* No filename. */
626 /* Now fetch the filename from target memory. */
627 target_read_string (lm.pathname_addr, &filename, SO_NAME_MAX_PATH_SIZE - 1,
628 &errcode);
630 if (errcode)
632 warning (_("failed to read exec filename from attached file: %s"),
633 safe_strerror (errcode));
634 return 0;
637 cleanups = make_cleanup (xfree, filename);
638 /* Have a pathname: read the symbol file. */
639 symbol_file_add_main (filename, from_tty);
641 do_cleanups (cleanups);
643 return 1;
649 LOCAL FUNCTION
651 irix_special_symbol_handling -- additional shared library symbol handling
653 SYNOPSIS
655 void irix_special_symbol_handling ()
657 DESCRIPTION
659 Once the symbols from a shared object have been loaded in the usual
660 way, we are called to do any system specific symbol handling that
661 is needed.
663 For SunOS4, this consisted of grunging around in the dynamic
664 linkers structures to find symbol definitions for "common" symbols
665 and adding them to the minimal symbol table for the runtime common
666 objfile.
668 However, for IRIX, there's nothing to do.
672 static void
673 irix_special_symbol_handling (void)
677 /* Using the solist entry SO, relocate the addresses in SEC. */
679 static void
680 irix_relocate_section_addresses (struct so_list *so,
681 struct section_table *sec)
683 sec->addr += so->lm_info->reloc_offset;
684 sec->endaddr += so->lm_info->reloc_offset;
687 /* Free the lm_info struct. */
689 static void
690 irix_free_so (struct so_list *so)
692 xfree (so->lm_info);
695 /* Clear backend specific state. */
697 static void
698 irix_clear_solib (void)
700 debug_base = 0;
703 /* Return 1 if PC lies in the dynamic symbol resolution code of the
704 run time loader. */
705 static int
706 irix_in_dynsym_resolve_code (CORE_ADDR pc)
708 return 0;
711 struct target_so_ops irix_so_ops;
713 void
714 _initialize_irix_solib (void)
716 irix_so_ops.relocate_section_addresses = irix_relocate_section_addresses;
717 irix_so_ops.free_so = irix_free_so;
718 irix_so_ops.clear_solib = irix_clear_solib;
719 irix_so_ops.solib_create_inferior_hook = irix_solib_create_inferior_hook;
720 irix_so_ops.special_symbol_handling = irix_special_symbol_handling;
721 irix_so_ops.current_sos = irix_current_sos;
722 irix_so_ops.open_symbol_file_object = irix_open_symbol_file_object;
723 irix_so_ops.in_dynsym_resolve_code = irix_in_dynsym_resolve_code;