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