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