Remove unnecessary function prototypes.
[external/binutils.git] / gdb / solib-dsbt.c
1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2    Copyright (C) 2010-2017 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 3 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, see <http://www.gnu.org/licenses/>.  */
18
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "gdbcore.h"
23 #include "solib.h"
24 #include "solist.h"
25 #include "objfiles.h"
26 #include "symtab.h"
27 #include "language.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "elf-bfd.h"
31 #include "gdb_bfd.h"
32
33 #define GOT_MODULE_OFFSET 4
34
35 /* Flag which indicates whether internal debug messages should be printed.  */
36 static unsigned int solib_dsbt_debug = 0;
37
38 /* TIC6X pointers are four bytes wide.  */
39 enum { TIC6X_PTR_SIZE = 4 };
40
41 /* Representation of loadmap and related structs for the TIC6X DSBT.  */
42
43 /* External versions; the size and alignment of the fields should be
44    the same as those on the target.  When loaded, the placement of
45    the bits in each field will be the same as on the target.  */
46 typedef gdb_byte ext_Elf32_Half[2];
47 typedef gdb_byte ext_Elf32_Addr[4];
48 typedef gdb_byte ext_Elf32_Word[4];
49
50 struct ext_elf32_dsbt_loadseg
51 {
52   /* Core address to which the segment is mapped.  */
53   ext_Elf32_Addr addr;
54   /* VMA recorded in the program header.  */
55   ext_Elf32_Addr p_vaddr;
56   /* Size of this segment in memory.  */
57   ext_Elf32_Word p_memsz;
58 };
59
60 struct ext_elf32_dsbt_loadmap {
61   /* Protocol version number, must be zero.  */
62   ext_Elf32_Word version;
63   /* A pointer to the DSBT table; the DSBT size and the index of this
64      module.  */
65   ext_Elf32_Word dsbt_table_ptr;
66   ext_Elf32_Word dsbt_size;
67   ext_Elf32_Word dsbt_index;
68   /* Number of segments in this map.  */
69   ext_Elf32_Word nsegs;
70   /* The actual memory map.  */
71   struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
72 };
73
74 /* Internal versions; the types are GDB types and the data in each
75    of the fields is (or will be) decoded from the external struct
76    for ease of consumption.  */
77 struct int_elf32_dsbt_loadseg
78 {
79   /* Core address to which the segment is mapped.  */
80   CORE_ADDR addr;
81   /* VMA recorded in the program header.  */
82   CORE_ADDR p_vaddr;
83   /* Size of this segment in memory.  */
84   long p_memsz;
85 };
86
87 struct int_elf32_dsbt_loadmap
88 {
89   /* Protocol version number, must be zero.  */
90   int version;
91   CORE_ADDR dsbt_table_ptr;
92   /* A pointer to the DSBT table; the DSBT size and the index of this
93      module.  */
94   int dsbt_size, dsbt_index;
95   /* Number of segments in this map.  */
96   int nsegs;
97   /* The actual memory map.  */
98   struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
99 };
100
101 /* External link_map and elf32_dsbt_loadaddr struct definitions.  */
102
103 typedef gdb_byte ext_ptr[4];
104
105 struct ext_elf32_dsbt_loadaddr
106 {
107   ext_ptr map;                  /* struct elf32_dsbt_loadmap *map; */
108 };
109
110 struct ext_link_map
111 {
112   struct ext_elf32_dsbt_loadaddr l_addr;
113
114   /* Absolute file name object was found in.  */
115   ext_ptr l_name;               /* char *l_name; */
116
117   /* Dynamic section of the shared object.  */
118   ext_ptr l_ld;                 /* ElfW(Dyn) *l_ld; */
119
120   /* Chain of loaded objects.  */
121   ext_ptr l_next, l_prev;       /* struct link_map *l_next, *l_prev; */
122 };
123
124 /* Link map info to include in an allocated so_list entry */
125
126 struct lm_info_dsbt : public lm_info_base
127 {
128   ~lm_info_dsbt ()
129   {
130     xfree (this->map);
131   }
132
133   /* The loadmap, digested into an easier to use form.  */
134   int_elf32_dsbt_loadmap *map = NULL;
135 };
136
137 /* Per pspace dsbt specific data.  */
138
139 struct dsbt_info
140 {
141   /* The load map, got value, etc. are not available from the chain
142      of loaded shared objects.  ``main_executable_lm_info'' provides
143      a way to get at this information so that it doesn't need to be
144      frequently recomputed.  Initialized by dsbt_relocate_main_executable.  */
145   struct lm_info_dsbt *main_executable_lm_info;
146
147   /* Load maps for the main executable and the interpreter.  These are obtained
148      from ptrace.  They are the starting point for getting into the program,
149      and are required to find the solib list with the individual load maps for
150      each module.  */
151   struct int_elf32_dsbt_loadmap *exec_loadmap;
152   struct int_elf32_dsbt_loadmap *interp_loadmap;
153
154   /* Cached value for lm_base, below.  */
155   CORE_ADDR lm_base_cache;
156
157   /* Link map address for main module.  */
158   CORE_ADDR main_lm_addr;
159
160   CORE_ADDR interp_text_sect_low;
161   CORE_ADDR interp_text_sect_high;
162   CORE_ADDR interp_plt_sect_low;
163   CORE_ADDR interp_plt_sect_high;
164 };
165
166 /* Per-program-space data key.  */
167 static const struct program_space_data *solib_dsbt_pspace_data;
168
169 static void
170 dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
171 {
172   xfree (arg);
173 }
174
175 /* Get the current dsbt data.  If none is found yet, add it now.  This
176    function always returns a valid object.  */
177
178 static struct dsbt_info *
179 get_dsbt_info (void)
180 {
181   struct dsbt_info *info;
182
183   info = (struct dsbt_info *) program_space_data (current_program_space,
184                                                   solib_dsbt_pspace_data);
185   if (info != NULL)
186     return info;
187
188   info = XCNEW (struct dsbt_info);
189   set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
190
191   info->lm_base_cache = 0;
192   info->main_lm_addr = 0;
193
194   return info;
195 }
196
197
198 static void
199 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
200 {
201   int i;
202
203   if (map == NULL)
204     printf_filtered ("(null)\n");
205   else if (map->version != 0)
206     printf_filtered (_("Unsupported map version: %d\n"), map->version);
207   else
208     {
209       printf_filtered ("version %d\n", map->version);
210
211       for (i = 0; i < map->nsegs; i++)
212         printf_filtered ("%s:%s -> %s:%s\n",
213                          print_core_address (target_gdbarch (),
214                                              map->segs[i].p_vaddr),
215                          print_core_address (target_gdbarch (),
216                                              map->segs[i].p_vaddr
217                                              + map->segs[i].p_memsz),
218                          print_core_address (target_gdbarch (), map->segs[i].addr),
219                          print_core_address (target_gdbarch (), map->segs[i].addr
220                                              + map->segs[i].p_memsz));
221     }
222 }
223
224 /* Decode int_elf32_dsbt_loadmap from BUF.  */
225
226 static struct int_elf32_dsbt_loadmap *
227 decode_loadmap (gdb_byte *buf)
228 {
229   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
230   struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
231   struct int_elf32_dsbt_loadmap *int_ldmbuf;
232
233   int version, seg, nsegs;
234   int int_ldmbuf_size;
235
236   ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
237
238   /* Extract the version.  */
239   version = extract_unsigned_integer (ext_ldmbuf->version,
240                                       sizeof ext_ldmbuf->version,
241                                       byte_order);
242   if (version != 0)
243     {
244       /* We only handle version 0.  */
245       return NULL;
246     }
247
248   /* Extract the number of segments.  */
249   nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
250                                     sizeof ext_ldmbuf->nsegs,
251                                     byte_order);
252
253   if (nsegs <= 0)
254     return NULL;
255
256   /* Allocate space into which to put information extract from the
257      external loadsegs.  I.e, allocate the internal loadsegs.  */
258   int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
259                      + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
260   int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
261
262   /* Place extracted information in internal structs.  */
263   int_ldmbuf->version = version;
264   int_ldmbuf->nsegs = nsegs;
265   for (seg = 0; seg < nsegs; seg++)
266     {
267       int_ldmbuf->segs[seg].addr
268         = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
269                                     sizeof (ext_ldmbuf->segs[seg].addr),
270                                     byte_order);
271       int_ldmbuf->segs[seg].p_vaddr
272         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
273                                     sizeof (ext_ldmbuf->segs[seg].p_vaddr),
274                                     byte_order);
275       int_ldmbuf->segs[seg].p_memsz
276         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
277                                     sizeof (ext_ldmbuf->segs[seg].p_memsz),
278                                     byte_order);
279     }
280
281   xfree (ext_ldmbuf);
282   return int_ldmbuf;
283 }
284
285
286 static struct dsbt_info *get_dsbt_info (void);
287
288 /* Interrogate the Linux kernel to find out where the program was loaded.
289    There are two load maps; one for the executable and one for the
290    interpreter (only in the case of a dynamically linked executable).  */
291
292 static void
293 dsbt_get_initial_loadmaps (void)
294 {
295   gdb_byte *buf;
296   struct dsbt_info *info = get_dsbt_info ();
297
298   if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
299                               "exec", &buf))
300     {
301       info->exec_loadmap = NULL;
302       error (_("Error reading DSBT exec loadmap"));
303     }
304   info->exec_loadmap = decode_loadmap (buf);
305   if (solib_dsbt_debug)
306     dsbt_print_loadmap (info->exec_loadmap);
307
308   if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
309                               "interp", &buf))
310     {
311       info->interp_loadmap = NULL;
312       error (_("Error reading DSBT interp loadmap"));
313     }
314   info->interp_loadmap = decode_loadmap (buf);
315   if (solib_dsbt_debug)
316     dsbt_print_loadmap (info->interp_loadmap);
317 }
318
319 /* Given address LDMADDR, fetch and decode the loadmap at that address.
320    Return NULL if there is a problem reading the target memory or if
321    there doesn't appear to be a loadmap at the given address.  The
322    allocated space (representing the loadmap) returned by this
323    function may be freed via a single call to xfree.  */
324
325 static struct int_elf32_dsbt_loadmap *
326 fetch_loadmap (CORE_ADDR ldmaddr)
327 {
328   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
329   struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
330   struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
331   struct int_elf32_dsbt_loadmap *int_ldmbuf;
332   int ext_ldmbuf_size, int_ldmbuf_size;
333   int version, seg, nsegs;
334
335   /* Fetch initial portion of the loadmap.  */
336   if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
337                           sizeof ext_ldmbuf_partial))
338     {
339       /* Problem reading the target's memory.  */
340       return NULL;
341     }
342
343   /* Extract the version.  */
344   version = extract_unsigned_integer (ext_ldmbuf_partial.version,
345                                       sizeof ext_ldmbuf_partial.version,
346                                       byte_order);
347   if (version != 0)
348     {
349       /* We only handle version 0.  */
350       return NULL;
351     }
352
353   /* Extract the number of segments.  */
354   nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
355                                     sizeof ext_ldmbuf_partial.nsegs,
356                                     byte_order);
357
358   if (nsegs <= 0)
359     return NULL;
360
361   /* Allocate space for the complete (external) loadmap.  */
362   ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
363     + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
364   ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) xmalloc (ext_ldmbuf_size);
365
366   /* Copy over the portion of the loadmap that's already been read.  */
367   memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
368
369   /* Read the rest of the loadmap from the target.  */
370   if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
371                           (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
372                           ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
373     {
374       /* Couldn't read rest of the loadmap.  */
375       xfree (ext_ldmbuf);
376       return NULL;
377     }
378
379   /* Allocate space into which to put information extract from the
380      external loadsegs.  I.e, allocate the internal loadsegs.  */
381   int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
382     + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
383   int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
384
385   /* Place extracted information in internal structs.  */
386   int_ldmbuf->version = version;
387   int_ldmbuf->nsegs = nsegs;
388   for (seg = 0; seg < nsegs; seg++)
389     {
390       int_ldmbuf->segs[seg].addr
391         = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
392                                     sizeof (ext_ldmbuf->segs[seg].addr),
393                                     byte_order);
394       int_ldmbuf->segs[seg].p_vaddr
395         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
396                                     sizeof (ext_ldmbuf->segs[seg].p_vaddr),
397                                     byte_order);
398       int_ldmbuf->segs[seg].p_memsz
399         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
400                                     sizeof (ext_ldmbuf->segs[seg].p_memsz),
401                                     byte_order);
402     }
403
404   xfree (ext_ldmbuf);
405   return int_ldmbuf;
406 }
407
408 static void dsbt_relocate_main_executable (void);
409 static int enable_break (void);
410
411 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
412    returned and the corresponding PTR is set.  */
413
414 static int
415 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
416 {
417   int arch_size, step, sect_size;
418   long dyn_tag;
419   CORE_ADDR dyn_ptr, dyn_addr;
420   gdb_byte *bufend, *bufstart, *buf;
421   Elf32_External_Dyn *x_dynp_32;
422   Elf64_External_Dyn *x_dynp_64;
423   struct bfd_section *sect;
424   struct target_section *target_section;
425
426   if (abfd == NULL)
427     return 0;
428
429   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
430     return 0;
431
432   arch_size = bfd_get_arch_size (abfd);
433   if (arch_size == -1)
434     return 0;
435
436   /* Find the start address of the .dynamic section.  */
437   sect = bfd_get_section_by_name (abfd, ".dynamic");
438   if (sect == NULL)
439     return 0;
440
441   for (target_section = current_target_sections->sections;
442        target_section < current_target_sections->sections_end;
443        target_section++)
444     if (sect == target_section->the_bfd_section)
445       break;
446   if (target_section < current_target_sections->sections_end)
447     dyn_addr = target_section->addr;
448   else
449     {
450       /* ABFD may come from OBJFILE acting only as a symbol file without being
451          loaded into the target (see add_symbol_file_command).  This case is
452          such fallback to the file VMA address without the possibility of
453          having the section relocated to its actual in-memory address.  */
454
455       dyn_addr = bfd_section_vma (abfd, sect);
456     }
457
458   /* Read in .dynamic from the BFD.  We will get the actual value
459      from memory later.  */
460   sect_size = bfd_section_size (abfd, sect);
461   buf = bufstart = (gdb_byte *) alloca (sect_size);
462   if (!bfd_get_section_contents (abfd, sect,
463                                  buf, 0, sect_size))
464     return 0;
465
466   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
467   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
468                            : sizeof (Elf64_External_Dyn);
469   for (bufend = buf + sect_size;
470        buf < bufend;
471        buf += step)
472   {
473     if (arch_size == 32)
474       {
475         x_dynp_32 = (Elf32_External_Dyn *) buf;
476         dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
477         dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
478       }
479     else
480       {
481         x_dynp_64 = (Elf64_External_Dyn *) buf;
482         dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
483         dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
484       }
485      if (dyn_tag == DT_NULL)
486        return 0;
487      if (dyn_tag == dyntag)
488        {
489          /* If requested, try to read the runtime value of this .dynamic
490             entry.  */
491          if (ptr)
492            {
493              struct type *ptr_type;
494              gdb_byte ptr_buf[8];
495              CORE_ADDR ptr_addr;
496
497              ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
498              ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
499              if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
500                dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
501              *ptr = dyn_ptr;
502            }
503          return 1;
504        }
505   }
506
507   return 0;
508 }
509
510 /* If no open symbol file, attempt to locate and open the main symbol
511    file.
512
513    If FROM_TTYP dereferences to a non-zero integer, allow messages to
514    be printed.  This parameter is a pointer rather than an int because
515    open_symbol_file_object is called via catch_errors and
516    catch_errors requires a pointer argument. */
517
518 static int
519 open_symbol_file_object (void *from_ttyp)
520 {
521   /* Unimplemented.  */
522   return 0;
523 }
524
525 /* Given a loadmap and an address, return the displacement needed
526    to relocate the address.  */
527
528 static CORE_ADDR
529 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
530                        CORE_ADDR addr)
531 {
532   int seg;
533
534   for (seg = 0; seg < map->nsegs; seg++)
535     if (map->segs[seg].p_vaddr <= addr
536         && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
537       return map->segs[seg].addr - map->segs[seg].p_vaddr;
538
539   return 0;
540 }
541
542 /* Return the address from which the link map chain may be found.  On
543    DSBT, a pointer to the start of the link map will be located at the
544    word found at base of GOT + GOT_MODULE_OFFSET.
545
546    The base of GOT may be found in a number of ways.  Assuming that the
547    main executable has already been relocated,
548    1 The easiest way to find this value is to look up the address of
549    _GLOBAL_OFFSET_TABLE_.
550    2 The other way is to look for tag DT_PLTGOT, which contains the virtual
551    address of Global Offset Table.  .*/
552
553 static CORE_ADDR
554 lm_base (void)
555 {
556   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
557   struct bound_minimal_symbol got_sym;
558   CORE_ADDR addr;
559   gdb_byte buf[TIC6X_PTR_SIZE];
560   struct dsbt_info *info = get_dsbt_info ();
561
562   /* One of our assumptions is that the main executable has been relocated.
563      Bail out if this has not happened.  (Note that post_create_inferior
564      in infcmd.c will call solib_add prior to solib_create_inferior_hook.
565      If we allow this to happen, lm_base_cache will be initialized with
566      a bogus value.  */
567   if (info->main_executable_lm_info == 0)
568     return 0;
569
570   /* If we already have a cached value, return it.  */
571   if (info->lm_base_cache)
572     return info->lm_base_cache;
573
574   got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
575                                    symfile_objfile);
576
577   if (got_sym.minsym != 0)
578     {
579       addr = BMSYMBOL_VALUE_ADDRESS (got_sym);
580       if (solib_dsbt_debug)
581         fprintf_unfiltered (gdb_stdlog,
582                             "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
583                             (unsigned int) addr);
584     }
585   else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
586     {
587       struct int_elf32_dsbt_loadmap *ldm;
588
589       dsbt_get_initial_loadmaps ();
590       ldm = info->exec_loadmap;
591       addr += displacement_from_map (ldm, addr);
592       if (solib_dsbt_debug)
593         fprintf_unfiltered (gdb_stdlog,
594                             "lm_base: get addr %x by DT_PLTGOT.\n",
595                             (unsigned int) addr);
596     }
597   else
598     {
599       if (solib_dsbt_debug)
600         fprintf_unfiltered (gdb_stdlog,
601                             "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
602       return 0;
603     }
604   addr += GOT_MODULE_OFFSET;
605
606   if (solib_dsbt_debug)
607     fprintf_unfiltered (gdb_stdlog,
608                         "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
609                         GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
610
611   if (target_read_memory (addr, buf, sizeof buf) != 0)
612     return 0;
613   info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
614
615   if (solib_dsbt_debug)
616     fprintf_unfiltered (gdb_stdlog,
617                         "lm_base: lm_base_cache = %s\n",
618                         hex_string_custom (info->lm_base_cache, 8));
619
620   return info->lm_base_cache;
621 }
622
623
624 /* Build a list of `struct so_list' objects describing the shared
625    objects currently loaded in the inferior.  This list does not
626    include an entry for the main executable file.
627
628    Note that we only gather information directly available from the
629    inferior --- we don't examine any of the shared library files
630    themselves.  The declaration of `struct so_list' says which fields
631    we provide values for.  */
632
633 static struct so_list *
634 dsbt_current_sos (void)
635 {
636   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
637   CORE_ADDR lm_addr;
638   struct so_list *sos_head = NULL;
639   struct so_list **sos_next_ptr = &sos_head;
640   struct dsbt_info *info = get_dsbt_info ();
641
642   /* Make sure that the main executable has been relocated.  This is
643      required in order to find the address of the global offset table,
644      which in turn is used to find the link map info.  (See lm_base
645      for details.)
646
647      Note that the relocation of the main executable is also performed
648      by solib_create_inferior_hook, however, in the case of core
649      files, this hook is called too late in order to be of benefit to
650      solib_add.  solib_add eventually calls this function,
651      dsbt_current_sos, and also precedes the call to
652      solib_create_inferior_hook.   (See post_create_inferior in
653      infcmd.c.)  */
654   if (info->main_executable_lm_info == 0 && core_bfd != NULL)
655     dsbt_relocate_main_executable ();
656
657   /* Locate the address of the first link map struct.  */
658   lm_addr = lm_base ();
659
660   /* We have at least one link map entry.  Fetch the the lot of them,
661      building the solist chain.  */
662   while (lm_addr)
663     {
664       struct ext_link_map lm_buf;
665       ext_Elf32_Word indexword;
666       CORE_ADDR map_addr;
667       int dsbt_index;
668       int ret;
669
670       if (solib_dsbt_debug)
671         fprintf_unfiltered (gdb_stdlog,
672                             "current_sos: reading link_map entry at %s\n",
673                             hex_string_custom (lm_addr, 8));
674
675       ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
676       if (ret)
677         {
678           warning (_("dsbt_current_sos: Unable to read link map entry."
679                      "  Shared object chain may be incomplete."));
680           break;
681         }
682
683       /* Fetch the load map address.  */
684       map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
685                                            sizeof lm_buf.l_addr.map,
686                                            byte_order);
687
688       ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
689                                 sizeof indexword);
690       if (ret)
691         {
692           warning (_("dsbt_current_sos: Unable to read dsbt index."
693                      "  Shared object chain may be incomplete."));
694           break;
695         }
696       dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
697                                              byte_order);
698
699       /* If the DSBT index is zero, then we're looking at the entry
700          for the main executable.  By convention, we don't include
701          this in the list of shared objects.  */
702       if (dsbt_index != 0)
703         {
704           int errcode;
705           char *name_buf;
706           struct int_elf32_dsbt_loadmap *loadmap;
707           struct so_list *sop;
708           CORE_ADDR addr;
709
710           loadmap = fetch_loadmap (map_addr);
711           if (loadmap == NULL)
712             {
713               warning (_("dsbt_current_sos: Unable to fetch load map."
714                          "  Shared object chain may be incomplete."));
715               break;
716             }
717
718           sop = XCNEW (struct so_list);
719           lm_info_dsbt *li = new lm_info_dsbt;
720           sop->lm_info = li;
721           li->map = loadmap;
722           /* Fetch the name.  */
723           addr = extract_unsigned_integer (lm_buf.l_name,
724                                            sizeof (lm_buf.l_name),
725                                            byte_order);
726           target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
727                               &errcode);
728
729           if (errcode != 0)
730             warning (_("Can't read pathname for link map entry: %s."),
731                      safe_strerror (errcode));
732           else
733             {
734               if (solib_dsbt_debug)
735                 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
736                                     name_buf);
737
738               strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
739               sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
740               xfree (name_buf);
741               strcpy (sop->so_original_name, sop->so_name);
742             }
743
744           *sos_next_ptr = sop;
745           sos_next_ptr = &sop->next;
746         }
747       else
748         {
749           info->main_lm_addr = lm_addr;
750         }
751
752       lm_addr = extract_unsigned_integer (lm_buf.l_next,
753                                           sizeof (lm_buf.l_next), byte_order);
754     }
755
756   return sos_head;
757 }
758
759 /* Return 1 if PC lies in the dynamic symbol resolution code of the
760    run time loader.  */
761
762 static int
763 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
764 {
765   struct dsbt_info *info = get_dsbt_info ();
766
767   return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
768           || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
769           || in_plt_section (pc));
770 }
771
772 /* Print a warning about being unable to set the dynamic linker
773    breakpoint.  */
774
775 static void
776 enable_break_failure_warning (void)
777 {
778   warning (_("Unable to find dynamic linker breakpoint function.\n"
779              "GDB will be unable to debug shared library initializers\n"
780              "and track explicitly loaded dynamic code."));
781 }
782
783 /* Helper function for gdb_bfd_lookup_symbol.  */
784
785 static int
786 cmp_name (const asymbol *sym, const void *data)
787 {
788   return (strcmp (sym->name, (const char *) data) == 0);
789 }
790
791 /* The dynamic linkers has, as part of its debugger interface, support
792    for arranging for the inferior to hit a breakpoint after mapping in
793    the shared libraries.  This function enables that breakpoint.
794
795    On the TIC6X, using the shared library (DSBT), GDB can try to place
796    a breakpoint on '_dl_debug_state' to monitor the shared library
797    event.  */
798
799 static int
800 enable_break (void)
801 {
802   asection *interp_sect;
803   struct dsbt_info *info;
804
805   if (exec_bfd == NULL)
806     return 0;
807
808   if (!target_has_execution)
809     return 0;
810
811   info = get_dsbt_info ();
812
813   info->interp_text_sect_low = 0;
814   info->interp_text_sect_high = 0;
815   info->interp_plt_sect_low = 0;
816   info->interp_plt_sect_high = 0;
817
818   /* Find the .interp section; if not found, warn the user and drop
819      into the old breakpoint at symbol code.  */
820   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
821   if (interp_sect)
822     {
823       unsigned int interp_sect_size;
824       char *buf;
825       CORE_ADDR addr;
826       struct int_elf32_dsbt_loadmap *ldm;
827       int ret;
828
829       /* Read the contents of the .interp section into a local buffer;
830          the contents specify the dynamic linker this program uses.  */
831       interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
832       buf = (char *) alloca (interp_sect_size);
833       bfd_get_section_contents (exec_bfd, interp_sect,
834                                 buf, 0, interp_sect_size);
835
836       /* Now we need to figure out where the dynamic linker was
837          loaded so that we can load its symbols and place a breakpoint
838          in the dynamic linker itself.  */
839
840       gdb_bfd_ref_ptr tmp_bfd;
841       TRY
842         {
843           tmp_bfd = solib_bfd_open (buf);
844         }
845       CATCH (ex, RETURN_MASK_ALL)
846         {
847         }
848       END_CATCH
849
850       if (tmp_bfd == NULL)
851         {
852           enable_break_failure_warning ();
853           return 0;
854         }
855
856       dsbt_get_initial_loadmaps ();
857       ldm = info->interp_loadmap;
858
859       /* Record the relocated start and end address of the dynamic linker
860          text and plt section for dsbt_in_dynsym_resolve_code.  */
861       interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
862       if (interp_sect)
863         {
864           info->interp_text_sect_low
865             = bfd_section_vma (tmp_bfd.get (), interp_sect);
866           info->interp_text_sect_low
867             += displacement_from_map (ldm, info->interp_text_sect_low);
868           info->interp_text_sect_high
869             = info->interp_text_sect_low
870             + bfd_section_size (tmp_bfd.get (), interp_sect);
871         }
872       interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
873       if (interp_sect)
874         {
875           info->interp_plt_sect_low =
876             bfd_section_vma (tmp_bfd.get (), interp_sect);
877           info->interp_plt_sect_low
878             += displacement_from_map (ldm, info->interp_plt_sect_low);
879           info->interp_plt_sect_high =
880             info->interp_plt_sect_low + bfd_section_size (tmp_bfd.get (),
881                                                           interp_sect);
882         }
883
884       addr = gdb_bfd_lookup_symbol (tmp_bfd.get (), cmp_name,
885                                     "_dl_debug_state");
886       if (addr != 0)
887         {
888           if (solib_dsbt_debug)
889             fprintf_unfiltered (gdb_stdlog,
890                                 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
891                                 hex_string_custom (addr, 8));
892           addr += displacement_from_map (ldm, addr);
893
894           if (solib_dsbt_debug)
895             fprintf_unfiltered (gdb_stdlog,
896                                 "enable_break: _dl_debug_state (after relocation) = %s\n",
897                                 hex_string_custom (addr, 8));
898
899           /* Now (finally!) create the solib breakpoint.  */
900           create_solib_event_breakpoint (target_gdbarch (), addr);
901
902           ret = 1;
903         }
904       else
905         {
906           if (solib_dsbt_debug)
907             fprintf_unfiltered (gdb_stdlog,
908                                 "enable_break: _dl_debug_state is not found\n");
909           ret = 0;
910         }
911
912       /* We're done with the loadmap.  */
913       xfree (ldm);
914
915       return ret;
916     }
917
918   /* Tell the user we couldn't set a dynamic linker breakpoint.  */
919   enable_break_failure_warning ();
920
921   /* Failure return.  */
922   return 0;
923 }
924
925 static void
926 dsbt_relocate_main_executable (void)
927 {
928   struct int_elf32_dsbt_loadmap *ldm;
929   struct cleanup *old_chain;
930   struct section_offsets *new_offsets;
931   int changed;
932   struct obj_section *osect;
933   struct dsbt_info *info = get_dsbt_info ();
934
935   dsbt_get_initial_loadmaps ();
936   ldm = info->exec_loadmap;
937
938   delete info->main_executable_lm_info;
939   info->main_executable_lm_info = new lm_info_dsbt;
940   info->main_executable_lm_info->map = ldm;
941
942   new_offsets = XCNEWVEC (struct section_offsets,
943                           symfile_objfile->num_sections);
944   old_chain = make_cleanup (xfree, new_offsets);
945   changed = 0;
946
947   ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
948     {
949       CORE_ADDR orig_addr, addr, offset;
950       int osect_idx;
951       int seg;
952
953       osect_idx = osect - symfile_objfile->sections;
954
955       /* Current address of section.  */
956       addr = obj_section_addr (osect);
957       /* Offset from where this section started.  */
958       offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
959       /* Original address prior to any past relocations.  */
960       orig_addr = addr - offset;
961
962       for (seg = 0; seg < ldm->nsegs; seg++)
963         {
964           if (ldm->segs[seg].p_vaddr <= orig_addr
965               && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
966             {
967               new_offsets->offsets[osect_idx]
968                 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
969
970               if (new_offsets->offsets[osect_idx] != offset)
971                 changed = 1;
972               break;
973             }
974         }
975     }
976
977   if (changed)
978     objfile_relocate (symfile_objfile, new_offsets);
979
980   do_cleanups (old_chain);
981
982   /* Now that symfile_objfile has been relocated, we can compute the
983      GOT value and stash it away.  */
984 }
985
986 /* When gdb starts up the inferior, it nurses it along (through the
987    shell) until it is ready to execute it's first instruction.  At this
988    point, this function gets called via solib_create_inferior_hook.
989
990    For the DSBT shared library, the main executable needs to be relocated.
991    The shared library breakpoints also need to be enabled.  */
992
993 static void
994 dsbt_solib_create_inferior_hook (int from_tty)
995 {
996   /* Relocate main executable.  */
997   dsbt_relocate_main_executable ();
998
999   /* Enable shared library breakpoints.  */
1000   if (!enable_break ())
1001     {
1002       warning (_("shared library handler failed to enable breakpoint"));
1003       return;
1004     }
1005 }
1006
1007 static void
1008 dsbt_clear_solib (void)
1009 {
1010   struct dsbt_info *info = get_dsbt_info ();
1011
1012   info->lm_base_cache = 0;
1013   info->main_lm_addr = 0;
1014
1015   delete info->main_executable_lm_info;
1016   info->main_executable_lm_info = NULL;
1017 }
1018
1019 static void
1020 dsbt_free_so (struct so_list *so)
1021 {
1022   lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
1023
1024   delete li;
1025 }
1026
1027 static void
1028 dsbt_relocate_section_addresses (struct so_list *so,
1029                                  struct target_section *sec)
1030 {
1031   int seg;
1032   lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
1033   int_elf32_dsbt_loadmap *map = li->map;
1034
1035   for (seg = 0; seg < map->nsegs; seg++)
1036     {
1037       if (map->segs[seg].p_vaddr <= sec->addr
1038           && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1039         {
1040           CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1041
1042           sec->addr += displ;
1043           sec->endaddr += displ;
1044           break;
1045         }
1046     }
1047 }
1048 static void
1049 show_dsbt_debug (struct ui_file *file, int from_tty,
1050                  struct cmd_list_element *c, const char *value)
1051 {
1052   fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1053 }
1054
1055 struct target_so_ops dsbt_so_ops;
1056
1057 void
1058 _initialize_dsbt_solib (void)
1059 {
1060   solib_dsbt_pspace_data
1061     = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);
1062
1063   dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1064   dsbt_so_ops.free_so = dsbt_free_so;
1065   dsbt_so_ops.clear_solib = dsbt_clear_solib;
1066   dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1067   dsbt_so_ops.current_sos = dsbt_current_sos;
1068   dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1069   dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1070   dsbt_so_ops.bfd_open = solib_bfd_open;
1071
1072   /* Debug this file's internals.  */
1073   add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
1074                              &solib_dsbt_debug, _("\
1075 Set internal debugging of shared library code for DSBT ELF."), _("\
1076 Show internal debugging of shared library code for DSBT ELF."), _("\
1077 When non-zero, DSBT solib specific internal debugging is enabled."),
1078                              NULL,
1079                              show_dsbt_debug,
1080                              &setdebuglist, &showdebuglist);
1081 }