Make read_program_header return a gdb::byte_vector
[deliverable/binutils-gdb.git] / gdb / solib-svr4.c
CommitLineData
ab31aa69 1/* Handle SVR4 shared libraries for GDB, the GNU Debugger.
2f4950cd 2
e2882c85 3 Copyright (C) 1990-2018 Free Software Foundation, Inc.
13437d4b
KB
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
13437d4b
KB
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
13437d4b 19
13437d4b
KB
20#include "defs.h"
21
13437d4b 22#include "elf/external.h"
21479ded 23#include "elf/common.h"
f7856c8f 24#include "elf/mips.h"
13437d4b
KB
25
26#include "symtab.h"
27#include "bfd.h"
28#include "symfile.h"
29#include "objfiles.h"
30#include "gdbcore.h"
13437d4b 31#include "target.h"
13437d4b 32#include "inferior.h"
45741a9c 33#include "infrun.h"
fb14de7b 34#include "regcache.h"
2020b7ab 35#include "gdbthread.h"
76727919 36#include "observable.h"
13437d4b
KB
37
38#include "solist.h"
bba93f6c 39#include "solib.h"
13437d4b
KB
40#include "solib-svr4.h"
41
2f4950cd 42#include "bfd-target.h"
cc10cae3 43#include "elf-bfd.h"
2f4950cd 44#include "exec.h"
8d4e36ba 45#include "auxv.h"
695c3173 46#include "gdb_bfd.h"
f9e14852 47#include "probe.h"
2f4950cd 48
e5e2b9ff 49static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
d5a921c9 50static int svr4_have_link_map_offsets (void);
9f2982ff 51static void svr4_relocate_main_executable (void);
f9e14852 52static void svr4_free_library_list (void *p_list);
1c4dcb57 53
13437d4b
KB
54/* On SVR4 systems, a list of symbols in the dynamic linker where
55 GDB can try to place a breakpoint to monitor shared library
56 events.
57
58 If none of these symbols are found, or other errors occur, then
59 SVR4 systems will fall back to using a symbol as the "startup
60 mapping complete" breakpoint address. */
61
bc043ef3 62static const char * const solib_break_names[] =
13437d4b
KB
63{
64 "r_debug_state",
65 "_r_debug_state",
66 "_dl_debug_state",
67 "rtld_db_dlactivity",
4c7dcb84 68 "__dl_rtld_db_dlactivity",
1f72e589 69 "_rtld_debug_state",
4c0122c8 70
13437d4b
KB
71 NULL
72};
13437d4b 73
bc043ef3 74static const char * const bkpt_names[] =
13437d4b 75{
13437d4b 76 "_start",
ad3dcc5c 77 "__start",
13437d4b
KB
78 "main",
79 NULL
80};
13437d4b 81
bc043ef3 82static const char * const main_name_list[] =
13437d4b
KB
83{
84 "main_$main",
85 NULL
86};
87
f9e14852
GB
88/* What to do when a probe stop occurs. */
89
90enum probe_action
91{
92 /* Something went seriously wrong. Stop using probes and
93 revert to using the older interface. */
94 PROBES_INTERFACE_FAILED,
95
96 /* No action is required. The shared object list is still
97 valid. */
98 DO_NOTHING,
99
100 /* The shared object list should be reloaded entirely. */
101 FULL_RELOAD,
102
103 /* Attempt to incrementally update the shared object list. If
104 the update fails or is not possible, fall back to reloading
105 the list in full. */
106 UPDATE_OR_RELOAD,
107};
108
109/* A probe's name and its associated action. */
110
111struct probe_info
112{
113 /* The name of the probe. */
114 const char *name;
115
116 /* What to do when a probe stop occurs. */
117 enum probe_action action;
118};
119
120/* A list of named probes and their associated actions. If all
121 probes are present in the dynamic linker then the probes-based
122 interface will be used. */
123
124static const struct probe_info probe_info[] =
125{
126 { "init_start", DO_NOTHING },
127 { "init_complete", FULL_RELOAD },
128 { "map_start", DO_NOTHING },
129 { "map_failed", DO_NOTHING },
130 { "reloc_complete", UPDATE_OR_RELOAD },
131 { "unmap_start", DO_NOTHING },
132 { "unmap_complete", FULL_RELOAD },
133};
134
135#define NUM_PROBES ARRAY_SIZE (probe_info)
136
4d7b2d5b
JB
137/* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
138 the same shared library. */
139
140static int
141svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
142{
143 if (strcmp (gdb_so_name, inferior_so_name) == 0)
144 return 1;
145
146 /* On Solaris, when starting inferior we think that dynamic linker is
d989b283
PP
147 /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
148 contains /lib/ld.so.1. Sometimes one file is a link to another, but
4d7b2d5b
JB
149 sometimes they have identical content, but are not linked to each
150 other. We don't restrict this check for Solaris, but the chances
151 of running into this situation elsewhere are very low. */
152 if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
153 && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
154 return 1;
155
156 /* Similarly, we observed the same issue with sparc64, but with
157 different locations. */
158 if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
159 && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
160 return 1;
161
162 return 0;
163}
164
165static int
166svr4_same (struct so_list *gdb, struct so_list *inferior)
167{
168 return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
169}
170
a7961323 171static std::unique_ptr<lm_info_svr4>
3957565a 172lm_info_read (CORE_ADDR lm_addr)
13437d4b 173{
4b188b9f 174 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
a7961323 175 std::unique_ptr<lm_info_svr4> lm_info;
3957565a 176
a7961323 177 gdb::byte_vector lm (lmo->link_map_size);
3957565a 178
a7961323
TT
179 if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
180 warning (_("Error reading shared library list entry at %s"),
181 paddress (target_gdbarch (), lm_addr));
3957565a
JK
182 else
183 {
f5656ead 184 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
13437d4b 185
a7961323 186 lm_info.reset (new lm_info_svr4);
3957565a
JK
187 lm_info->lm_addr = lm_addr;
188
189 lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
190 ptr_type);
191 lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
192 lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
193 ptr_type);
194 lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
195 ptr_type);
196 lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
197 ptr_type);
198 }
199
3957565a 200 return lm_info;
13437d4b
KB
201}
202
cc10cae3 203static int
b23518f0 204has_lm_dynamic_from_link_map (void)
cc10cae3
AO
205{
206 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
207
cfaefc65 208 return lmo->l_ld_offset >= 0;
cc10cae3
AO
209}
210
cc10cae3 211static CORE_ADDR
f65ce5fb 212lm_addr_check (const struct so_list *so, bfd *abfd)
cc10cae3 213{
d0e449a1
SM
214 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
215
216 if (!li->l_addr_p)
cc10cae3
AO
217 {
218 struct bfd_section *dyninfo_sect;
28f34a8f 219 CORE_ADDR l_addr, l_dynaddr, dynaddr;
cc10cae3 220
d0e449a1 221 l_addr = li->l_addr_inferior;
cc10cae3 222
b23518f0 223 if (! abfd || ! has_lm_dynamic_from_link_map ())
cc10cae3
AO
224 goto set_addr;
225
d0e449a1 226 l_dynaddr = li->l_ld;
cc10cae3
AO
227
228 dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
229 if (dyninfo_sect == NULL)
230 goto set_addr;
231
232 dynaddr = bfd_section_vma (abfd, dyninfo_sect);
233
234 if (dynaddr + l_addr != l_dynaddr)
235 {
28f34a8f 236 CORE_ADDR align = 0x1000;
4e1fc9c9 237 CORE_ADDR minpagesize = align;
28f34a8f 238
cc10cae3
AO
239 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
240 {
241 Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
242 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
243 int i;
244
245 align = 1;
246
247 for (i = 0; i < ehdr->e_phnum; i++)
248 if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
249 align = phdr[i].p_align;
4e1fc9c9
JK
250
251 minpagesize = get_elf_backend_data (abfd)->minpagesize;
cc10cae3
AO
252 }
253
254 /* Turn it into a mask. */
255 align--;
256
257 /* If the changes match the alignment requirements, we
258 assume we're using a core file that was generated by the
259 same binary, just prelinked with a different base offset.
260 If it doesn't match, we may have a different binary, the
261 same binary with the dynamic table loaded at an unrelated
262 location, or anything, really. To avoid regressions,
263 don't adjust the base offset in the latter case, although
264 odds are that, if things really changed, debugging won't
5c0d192f
JK
265 quite work.
266
267 One could expect more the condition
268 ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
269 but the one below is relaxed for PPC. The PPC kernel supports
270 either 4k or 64k page sizes. To be prepared for 64k pages,
271 PPC ELF files are built using an alignment requirement of 64k.
272 However, when running on a kernel supporting 4k pages, the memory
273 mapping of the library may not actually happen on a 64k boundary!
274
275 (In the usual case where (l_addr & align) == 0, this check is
4e1fc9c9
JK
276 equivalent to the possibly expected check above.)
277
278 Even on PPC it must be zero-aligned at least for MINPAGESIZE. */
5c0d192f 279
02835898
JK
280 l_addr = l_dynaddr - dynaddr;
281
4e1fc9c9
JK
282 if ((l_addr & (minpagesize - 1)) == 0
283 && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
cc10cae3 284 {
701ed6dc 285 if (info_verbose)
ccf26247
JK
286 printf_unfiltered (_("Using PIC (Position Independent Code) "
287 "prelink displacement %s for \"%s\".\n"),
f5656ead 288 paddress (target_gdbarch (), l_addr),
ccf26247 289 so->so_name);
cc10cae3 290 }
79d4c408 291 else
02835898
JK
292 {
293 /* There is no way to verify the library file matches. prelink
294 can during prelinking of an unprelinked file (or unprelinking
295 of a prelinked file) shift the DYNAMIC segment by arbitrary
296 offset without any page size alignment. There is no way to
297 find out the ELF header and/or Program Headers for a limited
298 verification if it they match. One could do a verification
299 of the DYNAMIC segment. Still the found address is the best
300 one GDB could find. */
301
302 warning (_(".dynamic section for \"%s\" "
303 "is not at the expected address "
304 "(wrong library or version mismatch?)"), so->so_name);
305 }
cc10cae3
AO
306 }
307
308 set_addr:
d0e449a1
SM
309 li->l_addr = l_addr;
310 li->l_addr_p = 1;
cc10cae3
AO
311 }
312
d0e449a1 313 return li->l_addr;
cc10cae3
AO
314}
315
6c95b8df 316/* Per pspace SVR4 specific data. */
13437d4b 317
1a816a87
PA
318struct svr4_info
319{
c378eb4e 320 CORE_ADDR debug_base; /* Base of dynamic linker structures. */
1a816a87
PA
321
322 /* Validity flag for debug_loader_offset. */
323 int debug_loader_offset_p;
324
325 /* Load address for the dynamic linker, inferred. */
326 CORE_ADDR debug_loader_offset;
327
328 /* Name of the dynamic linker, valid if debug_loader_offset_p. */
329 char *debug_loader_name;
330
331 /* Load map address for the main executable. */
332 CORE_ADDR main_lm_addr;
1a816a87 333
6c95b8df
PA
334 CORE_ADDR interp_text_sect_low;
335 CORE_ADDR interp_text_sect_high;
336 CORE_ADDR interp_plt_sect_low;
337 CORE_ADDR interp_plt_sect_high;
f9e14852
GB
338
339 /* Nonzero if the list of objects was last obtained from the target
340 via qXfer:libraries-svr4:read. */
341 int using_xfer;
342
343 /* Table of struct probe_and_action instances, used by the
344 probes-based interface to map breakpoint addresses to probes
345 and their associated actions. Lookup is performed using
935676c9 346 probe_and_action->prob->address. */
f9e14852
GB
347 htab_t probes_table;
348
349 /* List of objects loaded into the inferior, used by the probes-
350 based interface. */
351 struct so_list *solib_list;
6c95b8df 352};
1a816a87 353
6c95b8df
PA
354/* Per-program-space data key. */
355static const struct program_space_data *solib_svr4_pspace_data;
1a816a87 356
f9e14852
GB
357/* Free the probes table. */
358
359static void
360free_probes_table (struct svr4_info *info)
361{
362 if (info->probes_table == NULL)
363 return;
364
365 htab_delete (info->probes_table);
366 info->probes_table = NULL;
367}
368
369/* Free the solib list. */
370
371static void
372free_solib_list (struct svr4_info *info)
373{
374 svr4_free_library_list (&info->solib_list);
375 info->solib_list = NULL;
376}
377
6c95b8df
PA
378static void
379svr4_pspace_data_cleanup (struct program_space *pspace, void *arg)
1a816a87 380{
19ba03f4 381 struct svr4_info *info = (struct svr4_info *) arg;
f9e14852
GB
382
383 free_probes_table (info);
384 free_solib_list (info);
385
6c95b8df 386 xfree (info);
1a816a87
PA
387}
388
6c95b8df
PA
389/* Get the current svr4 data. If none is found yet, add it now. This
390 function always returns a valid object. */
34439770 391
6c95b8df
PA
392static struct svr4_info *
393get_svr4_info (void)
1a816a87 394{
6c95b8df 395 struct svr4_info *info;
1a816a87 396
19ba03f4
SM
397 info = (struct svr4_info *) program_space_data (current_program_space,
398 solib_svr4_pspace_data);
6c95b8df
PA
399 if (info != NULL)
400 return info;
34439770 401
41bf6aca 402 info = XCNEW (struct svr4_info);
6c95b8df
PA
403 set_program_space_data (current_program_space, solib_svr4_pspace_data, info);
404 return info;
1a816a87 405}
93a57060 406
13437d4b
KB
407/* Local function prototypes */
408
bc043ef3 409static int match_main (const char *);
13437d4b 410
97ec2c2f 411/* Read program header TYPE from inferior memory. The header is found
17658d46 412 by scanning the OS auxiliary vector.
97ec2c2f 413
09919ac2
JK
414 If TYPE == -1, return the program headers instead of the contents of
415 one program header.
416
17658d46
SM
417 Return vector of bytes holding the program header contents, or an empty
418 optional on failure. If successful and P_ARCH_SIZE is non-NULL, the target
419 architecture size (32-bit or 64-bit) is returned to *P_ARCH_SIZE. Likewise,
420 the base address of the section is returned in *BASE_ADDR. */
97ec2c2f 421
17658d46
SM
422static gdb::optional<gdb::byte_vector>
423read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
97ec2c2f 424{
f5656ead 425 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
43136979 426 CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
97ec2c2f
UW
427 int arch_size, sect_size;
428 CORE_ADDR sect_addr;
43136979 429 int pt_phdr_p = 0;
97ec2c2f
UW
430
431 /* Get required auxv elements from target. */
8b88a78e 432 if (target_auxv_search (current_top_target (), AT_PHDR, &at_phdr) <= 0)
17658d46 433 return {};
8b88a78e 434 if (target_auxv_search (current_top_target (), AT_PHENT, &at_phent) <= 0)
17658d46 435 return {};
8b88a78e 436 if (target_auxv_search (current_top_target (), AT_PHNUM, &at_phnum) <= 0)
17658d46 437 return {};
97ec2c2f 438 if (!at_phdr || !at_phnum)
17658d46 439 return {};
97ec2c2f
UW
440
441 /* Determine ELF architecture type. */
442 if (at_phent == sizeof (Elf32_External_Phdr))
443 arch_size = 32;
444 else if (at_phent == sizeof (Elf64_External_Phdr))
445 arch_size = 64;
446 else
17658d46 447 return {};
97ec2c2f 448
09919ac2
JK
449 /* Find the requested segment. */
450 if (type == -1)
451 {
452 sect_addr = at_phdr;
453 sect_size = at_phent * at_phnum;
454 }
455 else if (arch_size == 32)
97ec2c2f
UW
456 {
457 Elf32_External_Phdr phdr;
458 int i;
459
460 /* Search for requested PHDR. */
461 for (i = 0; i < at_phnum; i++)
462 {
43136979
AR
463 int p_type;
464
97ec2c2f
UW
465 if (target_read_memory (at_phdr + i * sizeof (phdr),
466 (gdb_byte *)&phdr, sizeof (phdr)))
17658d46 467 return {};
97ec2c2f 468
43136979
AR
469 p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
470 4, byte_order);
471
472 if (p_type == PT_PHDR)
473 {
474 pt_phdr_p = 1;
475 pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
476 4, byte_order);
477 }
478
479 if (p_type == type)
97ec2c2f
UW
480 break;
481 }
482
483 if (i == at_phnum)
17658d46 484 return {};
97ec2c2f
UW
485
486 /* Retrieve address and size. */
e17a4113
UW
487 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
488 4, byte_order);
489 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
490 4, byte_order);
97ec2c2f
UW
491 }
492 else
493 {
494 Elf64_External_Phdr phdr;
495 int i;
496
497 /* Search for requested PHDR. */
498 for (i = 0; i < at_phnum; i++)
499 {
43136979
AR
500 int p_type;
501
97ec2c2f
UW
502 if (target_read_memory (at_phdr + i * sizeof (phdr),
503 (gdb_byte *)&phdr, sizeof (phdr)))
17658d46 504 return {};
97ec2c2f 505
43136979
AR
506 p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
507 4, byte_order);
508
509 if (p_type == PT_PHDR)
510 {
511 pt_phdr_p = 1;
512 pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
513 8, byte_order);
514 }
515
516 if (p_type == type)
97ec2c2f
UW
517 break;
518 }
519
520 if (i == at_phnum)
17658d46 521 return {};
97ec2c2f
UW
522
523 /* Retrieve address and size. */
e17a4113
UW
524 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
525 8, byte_order);
526 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
527 8, byte_order);
97ec2c2f
UW
528 }
529
43136979
AR
530 /* PT_PHDR is optional, but we really need it
531 for PIE to make this work in general. */
532
533 if (pt_phdr_p)
534 {
535 /* at_phdr is real address in memory. pt_phdr is what pheader says it is.
536 Relocation offset is the difference between the two. */
537 sect_addr = sect_addr + (at_phdr - pt_phdr);
538 }
539
97ec2c2f 540 /* Read in requested program header. */
17658d46
SM
541 gdb::byte_vector buf (sect_size);
542 if (target_read_memory (sect_addr, buf.data (), sect_size))
543 return {};
97ec2c2f
UW
544
545 if (p_arch_size)
546 *p_arch_size = arch_size;
a738da3a
MF
547 if (base_addr)
548 *base_addr = sect_addr;
97ec2c2f
UW
549
550 return buf;
551}
552
553
554/* Return program interpreter string. */
17658d46 555static gdb::optional<gdb::byte_vector>
97ec2c2f
UW
556find_program_interpreter (void)
557{
97ec2c2f
UW
558 /* If we have an exec_bfd, use its section table. */
559 if (exec_bfd
560 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
561 {
562 struct bfd_section *interp_sect;
563
564 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
565 if (interp_sect != NULL)
566 {
97ec2c2f
UW
567 int sect_size = bfd_section_size (exec_bfd, interp_sect);
568
17658d46
SM
569 gdb::byte_vector buf (sect_size);
570 bfd_get_section_contents (exec_bfd, interp_sect, buf.data (), 0,
571 sect_size);
572 return buf;
97ec2c2f
UW
573 }
574 }
575
17658d46
SM
576 /* If we didn't find it, use the target auxiliary vector. */
577 return read_program_header (PT_INTERP, NULL, NULL);
97ec2c2f
UW
578}
579
580
b6d7a4bf
SM
581/* Scan for DESIRED_DYNTAG in .dynamic section of ABFD. If DESIRED_DYNTAG is
582 found, 1 is returned and the corresponding PTR is set. */
3a40aaa0
UW
583
584static int
a738da3a
MF
585scan_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr,
586 CORE_ADDR *ptr_addr)
3a40aaa0
UW
587{
588 int arch_size, step, sect_size;
b6d7a4bf 589 long current_dyntag;
b381ea14 590 CORE_ADDR dyn_ptr, dyn_addr;
65728c26 591 gdb_byte *bufend, *bufstart, *buf;
3a40aaa0
UW
592 Elf32_External_Dyn *x_dynp_32;
593 Elf64_External_Dyn *x_dynp_64;
594 struct bfd_section *sect;
61f0d762 595 struct target_section *target_section;
3a40aaa0
UW
596
597 if (abfd == NULL)
598 return 0;
0763ab81
PA
599
600 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
601 return 0;
602
3a40aaa0
UW
603 arch_size = bfd_get_arch_size (abfd);
604 if (arch_size == -1)
0763ab81 605 return 0;
3a40aaa0
UW
606
607 /* Find the start address of the .dynamic section. */
608 sect = bfd_get_section_by_name (abfd, ".dynamic");
609 if (sect == NULL)
610 return 0;
61f0d762
JK
611
612 for (target_section = current_target_sections->sections;
613 target_section < current_target_sections->sections_end;
614 target_section++)
615 if (sect == target_section->the_bfd_section)
616 break;
b381ea14
JK
617 if (target_section < current_target_sections->sections_end)
618 dyn_addr = target_section->addr;
619 else
620 {
621 /* ABFD may come from OBJFILE acting only as a symbol file without being
622 loaded into the target (see add_symbol_file_command). This case is
623 such fallback to the file VMA address without the possibility of
624 having the section relocated to its actual in-memory address. */
625
626 dyn_addr = bfd_section_vma (abfd, sect);
627 }
3a40aaa0 628
65728c26
DJ
629 /* Read in .dynamic from the BFD. We will get the actual value
630 from memory later. */
3a40aaa0 631 sect_size = bfd_section_size (abfd, sect);
224c3ddb 632 buf = bufstart = (gdb_byte *) alloca (sect_size);
65728c26
DJ
633 if (!bfd_get_section_contents (abfd, sect,
634 buf, 0, sect_size))
635 return 0;
3a40aaa0
UW
636
637 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
638 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
639 : sizeof (Elf64_External_Dyn);
640 for (bufend = buf + sect_size;
641 buf < bufend;
642 buf += step)
643 {
644 if (arch_size == 32)
645 {
646 x_dynp_32 = (Elf32_External_Dyn *) buf;
b6d7a4bf 647 current_dyntag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
3a40aaa0
UW
648 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
649 }
65728c26 650 else
3a40aaa0
UW
651 {
652 x_dynp_64 = (Elf64_External_Dyn *) buf;
b6d7a4bf 653 current_dyntag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
3a40aaa0
UW
654 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
655 }
b6d7a4bf 656 if (current_dyntag == DT_NULL)
3a40aaa0 657 return 0;
b6d7a4bf 658 if (current_dyntag == desired_dyntag)
3a40aaa0 659 {
65728c26
DJ
660 /* If requested, try to read the runtime value of this .dynamic
661 entry. */
3a40aaa0 662 if (ptr)
65728c26 663 {
b6da22b0 664 struct type *ptr_type;
65728c26 665 gdb_byte ptr_buf[8];
a738da3a 666 CORE_ADDR ptr_addr_1;
65728c26 667
f5656ead 668 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
a738da3a
MF
669 ptr_addr_1 = dyn_addr + (buf - bufstart) + arch_size / 8;
670 if (target_read_memory (ptr_addr_1, ptr_buf, arch_size / 8) == 0)
b6da22b0 671 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
65728c26 672 *ptr = dyn_ptr;
a738da3a
MF
673 if (ptr_addr)
674 *ptr_addr = dyn_addr + (buf - bufstart);
65728c26
DJ
675 }
676 return 1;
3a40aaa0
UW
677 }
678 }
679
680 return 0;
681}
682
b6d7a4bf
SM
683/* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable,
684 found by consulting the OS auxillary vector. If DESIRED_DYNTAG is found, 1
685 is returned and the corresponding PTR is set. */
97ec2c2f
UW
686
687static int
a738da3a
MF
688scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr,
689 CORE_ADDR *ptr_addr)
97ec2c2f 690{
f5656ead 691 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
17658d46 692 int arch_size, step;
b6d7a4bf 693 long current_dyntag;
97ec2c2f 694 CORE_ADDR dyn_ptr;
a738da3a 695 CORE_ADDR base_addr;
97ec2c2f
UW
696
697 /* Read in .dynamic section. */
17658d46
SM
698 gdb::optional<gdb::byte_vector> ph_data
699 = read_program_header (PT_DYNAMIC, &arch_size, &base_addr);
700 if (!ph_data)
97ec2c2f
UW
701 return 0;
702
703 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
704 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
705 : sizeof (Elf64_External_Dyn);
17658d46
SM
706 for (gdb_byte *buf = ph_data->data (), *bufend = buf + ph_data->size ();
707 buf < bufend; buf += step)
97ec2c2f
UW
708 {
709 if (arch_size == 32)
710 {
711 Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
433759f7 712
b6d7a4bf 713 current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
e17a4113
UW
714 4, byte_order);
715 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
716 4, byte_order);
97ec2c2f
UW
717 }
718 else
719 {
720 Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
433759f7 721
b6d7a4bf 722 current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
e17a4113
UW
723 8, byte_order);
724 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
725 8, byte_order);
97ec2c2f 726 }
b6d7a4bf 727 if (current_dyntag == DT_NULL)
97ec2c2f
UW
728 break;
729
b6d7a4bf 730 if (current_dyntag == desired_dyntag)
97ec2c2f
UW
731 {
732 if (ptr)
733 *ptr = dyn_ptr;
734
a738da3a 735 if (ptr_addr)
17658d46 736 *ptr_addr = base_addr + buf - ph_data->data ();
a738da3a 737
97ec2c2f
UW
738 return 1;
739 }
740 }
741
97ec2c2f
UW
742 return 0;
743}
744
7f86f058
PA
745/* Locate the base address of dynamic linker structs for SVR4 elf
746 targets.
13437d4b
KB
747
748 For SVR4 elf targets the address of the dynamic linker's runtime
749 structure is contained within the dynamic info section in the
750 executable file. The dynamic section is also mapped into the
751 inferior address space. Because the runtime loader fills in the
752 real address before starting the inferior, we have to read in the
753 dynamic info section from the inferior address space.
754 If there are any errors while trying to find the address, we
7f86f058 755 silently return 0, otherwise the found address is returned. */
13437d4b
KB
756
757static CORE_ADDR
758elf_locate_base (void)
759{
3b7344d5 760 struct bound_minimal_symbol msymbol;
a738da3a 761 CORE_ADDR dyn_ptr, dyn_ptr_addr;
13437d4b 762
65728c26
DJ
763 /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
764 instead of DT_DEBUG, although they sometimes contain an unused
765 DT_DEBUG. */
a738da3a
MF
766 if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr, NULL)
767 || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL))
3a40aaa0 768 {
f5656ead 769 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
3a40aaa0 770 gdb_byte *pbuf;
b6da22b0 771 int pbuf_size = TYPE_LENGTH (ptr_type);
433759f7 772
224c3ddb 773 pbuf = (gdb_byte *) alloca (pbuf_size);
3a40aaa0
UW
774 /* DT_MIPS_RLD_MAP contains a pointer to the address
775 of the dynamic link structure. */
776 if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
e499d0f1 777 return 0;
b6da22b0 778 return extract_typed_address (pbuf, ptr_type);
e499d0f1
DJ
779 }
780
a738da3a
MF
781 /* Then check DT_MIPS_RLD_MAP_REL. MIPS executables now use this form
782 because of needing to support PIE. DT_MIPS_RLD_MAP will also exist
783 in non-PIE. */
784 if (scan_dyntag (DT_MIPS_RLD_MAP_REL, exec_bfd, &dyn_ptr, &dyn_ptr_addr)
785 || scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr))
786 {
787 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
788 gdb_byte *pbuf;
789 int pbuf_size = TYPE_LENGTH (ptr_type);
790
224c3ddb 791 pbuf = (gdb_byte *) alloca (pbuf_size);
a738da3a
MF
792 /* DT_MIPS_RLD_MAP_REL contains an offset from the address of the
793 DT slot to the address of the dynamic link structure. */
794 if (target_read_memory (dyn_ptr + dyn_ptr_addr, pbuf, pbuf_size))
795 return 0;
796 return extract_typed_address (pbuf, ptr_type);
797 }
798
65728c26 799 /* Find DT_DEBUG. */
a738da3a
MF
800 if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr, NULL)
801 || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL))
65728c26
DJ
802 return dyn_ptr;
803
3a40aaa0
UW
804 /* This may be a static executable. Look for the symbol
805 conventionally named _r_debug, as a last resort. */
806 msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
3b7344d5 807 if (msymbol.minsym != NULL)
77e371c0 808 return BMSYMBOL_VALUE_ADDRESS (msymbol);
13437d4b
KB
809
810 /* DT_DEBUG entry not found. */
811 return 0;
812}
813
7f86f058 814/* Locate the base address of dynamic linker structs.
13437d4b
KB
815
816 For both the SunOS and SVR4 shared library implementations, if the
817 inferior executable has been linked dynamically, there is a single
818 address somewhere in the inferior's data space which is the key to
819 locating all of the dynamic linker's runtime structures. This
820 address is the value of the debug base symbol. The job of this
821 function is to find and return that address, or to return 0 if there
822 is no such address (the executable is statically linked for example).
823
824 For SunOS, the job is almost trivial, since the dynamic linker and
825 all of it's structures are statically linked to the executable at
826 link time. Thus the symbol for the address we are looking for has
827 already been added to the minimal symbol table for the executable's
828 objfile at the time the symbol file's symbols were read, and all we
829 have to do is look it up there. Note that we explicitly do NOT want
830 to find the copies in the shared library.
831
832 The SVR4 version is a bit more complicated because the address
833 is contained somewhere in the dynamic info section. We have to go
834 to a lot more work to discover the address of the debug base symbol.
835 Because of this complexity, we cache the value we find and return that
836 value on subsequent invocations. Note there is no copy in the
7f86f058 837 executable symbol tables. */
13437d4b
KB
838
839static CORE_ADDR
1a816a87 840locate_base (struct svr4_info *info)
13437d4b 841{
13437d4b
KB
842 /* Check to see if we have a currently valid address, and if so, avoid
843 doing all this work again and just return the cached address. If
844 we have no cached address, try to locate it in the dynamic info
d5a921c9
KB
845 section for ELF executables. There's no point in doing any of this
846 though if we don't have some link map offsets to work with. */
13437d4b 847
1a816a87 848 if (info->debug_base == 0 && svr4_have_link_map_offsets ())
0763ab81 849 info->debug_base = elf_locate_base ();
1a816a87 850 return info->debug_base;
13437d4b
KB
851}
852
e4cd0d6a 853/* Find the first element in the inferior's dynamic link map, and
6f992fbf
JB
854 return its address in the inferior. Return zero if the address
855 could not be determined.
13437d4b 856
e4cd0d6a
MK
857 FIXME: Perhaps we should validate the info somehow, perhaps by
858 checking r_version for a known version number, or r_state for
859 RT_CONSISTENT. */
13437d4b
KB
860
861static CORE_ADDR
1a816a87 862solib_svr4_r_map (struct svr4_info *info)
13437d4b 863{
4b188b9f 864 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
f5656ead 865 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
08597104 866 CORE_ADDR addr = 0;
13437d4b 867
492d29ea 868 TRY
08597104
JB
869 {
870 addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset,
871 ptr_type);
872 }
492d29ea
PA
873 CATCH (ex, RETURN_MASK_ERROR)
874 {
875 exception_print (gdb_stderr, ex);
876 }
877 END_CATCH
878
08597104 879 return addr;
e4cd0d6a 880}
13437d4b 881
7cd25cfc
DJ
882/* Find r_brk from the inferior's debug base. */
883
884static CORE_ADDR
1a816a87 885solib_svr4_r_brk (struct svr4_info *info)
7cd25cfc
DJ
886{
887 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
f5656ead 888 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
7cd25cfc 889
1a816a87
PA
890 return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
891 ptr_type);
7cd25cfc
DJ
892}
893
e4cd0d6a
MK
894/* Find the link map for the dynamic linker (if it is not in the
895 normal list of loaded shared objects). */
13437d4b 896
e4cd0d6a 897static CORE_ADDR
1a816a87 898solib_svr4_r_ldsomap (struct svr4_info *info)
e4cd0d6a
MK
899{
900 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
f5656ead
TT
901 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
902 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
416f679e
SDJ
903 ULONGEST version = 0;
904
905 TRY
906 {
907 /* Check version, and return zero if `struct r_debug' doesn't have
908 the r_ldsomap member. */
909 version
910 = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
911 lmo->r_version_size, byte_order);
912 }
913 CATCH (ex, RETURN_MASK_ERROR)
914 {
915 exception_print (gdb_stderr, ex);
916 }
917 END_CATCH
13437d4b 918
e4cd0d6a
MK
919 if (version < 2 || lmo->r_ldsomap_offset == -1)
920 return 0;
13437d4b 921
1a816a87 922 return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
b6da22b0 923 ptr_type);
13437d4b
KB
924}
925
de18c1d8
JM
926/* On Solaris systems with some versions of the dynamic linker,
927 ld.so's l_name pointer points to the SONAME in the string table
928 rather than into writable memory. So that GDB can find shared
929 libraries when loading a core file generated by gcore, ensure that
930 memory areas containing the l_name string are saved in the core
931 file. */
932
933static int
934svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
935{
936 struct svr4_info *info;
937 CORE_ADDR ldsomap;
74de0234 938 CORE_ADDR name_lm;
de18c1d8
JM
939
940 info = get_svr4_info ();
941
942 info->debug_base = 0;
943 locate_base (info);
944 if (!info->debug_base)
945 return 0;
946
947 ldsomap = solib_svr4_r_ldsomap (info);
948 if (!ldsomap)
949 return 0;
950
a7961323 951 std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);
d0e449a1 952 name_lm = li != NULL ? li->l_name : 0;
de18c1d8 953
74de0234 954 return (name_lm >= vaddr && name_lm < vaddr + size);
de18c1d8
JM
955}
956
bf469271 957/* See solist.h. */
13437d4b
KB
958
959static int
bf469271 960open_symbol_file_object (int from_tty)
13437d4b
KB
961{
962 CORE_ADDR lm, l_name;
e83e4e24 963 gdb::unique_xmalloc_ptr<char> filename;
13437d4b 964 int errcode;
4b188b9f 965 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
f5656ead 966 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
b6da22b0 967 int l_name_size = TYPE_LENGTH (ptr_type);
a7961323 968 gdb::byte_vector l_name_buf (l_name_size);
6c95b8df 969 struct svr4_info *info = get_svr4_info ();
ecf45d2c
SL
970 symfile_add_flags add_flags = 0;
971
972 if (from_tty)
973 add_flags |= SYMFILE_VERBOSE;
13437d4b
KB
974
975 if (symfile_objfile)
9e2f0ad4 976 if (!query (_("Attempt to reload symbols from process? ")))
a7961323 977 return 0;
13437d4b 978
7cd25cfc 979 /* Always locate the debug struct, in case it has moved. */
1a816a87
PA
980 info->debug_base = 0;
981 if (locate_base (info) == 0)
a7961323 982 return 0; /* failed somehow... */
13437d4b
KB
983
984 /* First link map member should be the executable. */
1a816a87 985 lm = solib_svr4_r_map (info);
e4cd0d6a 986 if (lm == 0)
a7961323 987 return 0; /* failed somehow... */
13437d4b
KB
988
989 /* Read address of name from target memory to GDB. */
a7961323 990 read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
13437d4b 991
cfaefc65 992 /* Convert the address to host format. */
a7961323 993 l_name = extract_typed_address (l_name_buf.data (), ptr_type);
13437d4b 994
13437d4b 995 if (l_name == 0)
a7961323 996 return 0; /* No filename. */
13437d4b
KB
997
998 /* Now fetch the filename from target memory. */
999 target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
1000
1001 if (errcode)
1002 {
8a3fe4f8 1003 warning (_("failed to read exec filename from attached file: %s"),
13437d4b
KB
1004 safe_strerror (errcode));
1005 return 0;
1006 }
1007
13437d4b 1008 /* Have a pathname: read the symbol file. */
e83e4e24 1009 symbol_file_add_main (filename.get (), add_flags);
13437d4b
KB
1010
1011 return 1;
1012}
13437d4b 1013
2268b414
JK
1014/* Data exchange structure for the XML parser as returned by
1015 svr4_current_sos_via_xfer_libraries. */
1016
1017struct svr4_library_list
1018{
1019 struct so_list *head, **tailp;
1020
1021 /* Inferior address of struct link_map used for the main executable. It is
1022 NULL if not known. */
1023 CORE_ADDR main_lm;
1024};
1025
93f2a35e
JK
1026/* Implementation for target_so_ops.free_so. */
1027
1028static void
1029svr4_free_so (struct so_list *so)
1030{
76e75227
SM
1031 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1032
1033 delete li;
93f2a35e
JK
1034}
1035
0892cb63
DE
1036/* Implement target_so_ops.clear_so. */
1037
1038static void
1039svr4_clear_so (struct so_list *so)
1040{
d0e449a1
SM
1041 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1042
1043 if (li != NULL)
1044 li->l_addr_p = 0;
0892cb63
DE
1045}
1046
93f2a35e
JK
1047/* Free so_list built so far (called via cleanup). */
1048
1049static void
1050svr4_free_library_list (void *p_list)
1051{
1052 struct so_list *list = *(struct so_list **) p_list;
1053
1054 while (list != NULL)
1055 {
1056 struct so_list *next = list->next;
1057
3756ef7e 1058 free_so (list);
93f2a35e
JK
1059 list = next;
1060 }
1061}
1062
f9e14852
GB
1063/* Copy library list. */
1064
1065static struct so_list *
1066svr4_copy_library_list (struct so_list *src)
1067{
1068 struct so_list *dst = NULL;
1069 struct so_list **link = &dst;
1070
1071 while (src != NULL)
1072 {
fe978cb0 1073 struct so_list *newobj;
f9e14852 1074
8d749320 1075 newobj = XNEW (struct so_list);
fe978cb0 1076 memcpy (newobj, src, sizeof (struct so_list));
f9e14852 1077
76e75227
SM
1078 lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
1079 newobj->lm_info = new lm_info_svr4 (*src_li);
f9e14852 1080
fe978cb0
PA
1081 newobj->next = NULL;
1082 *link = newobj;
1083 link = &newobj->next;
f9e14852
GB
1084
1085 src = src->next;
1086 }
1087
1088 return dst;
1089}
1090
2268b414
JK
1091#ifdef HAVE_LIBEXPAT
1092
1093#include "xml-support.h"
1094
1095/* Handle the start of a <library> element. Note: new elements are added
1096 at the tail of the list, keeping the list in order. */
1097
1098static void
1099library_list_start_library (struct gdb_xml_parser *parser,
1100 const struct gdb_xml_element *element,
4d0fdd9b
SM
1101 void *user_data,
1102 std::vector<gdb_xml_value> &attributes)
2268b414 1103{
19ba03f4
SM
1104 struct svr4_library_list *list = (struct svr4_library_list *) user_data;
1105 const char *name
4d0fdd9b 1106 = (const char *) xml_find_attribute (attributes, "name")->value.get ();
19ba03f4 1107 ULONGEST *lmp
4d0fdd9b 1108 = (ULONGEST *) xml_find_attribute (attributes, "lm")->value.get ();
19ba03f4 1109 ULONGEST *l_addrp
4d0fdd9b 1110 = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get ();
19ba03f4 1111 ULONGEST *l_ldp
4d0fdd9b 1112 = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
2268b414
JK
1113 struct so_list *new_elem;
1114
41bf6aca 1115 new_elem = XCNEW (struct so_list);
76e75227 1116 lm_info_svr4 *li = new lm_info_svr4;
d0e449a1
SM
1117 new_elem->lm_info = li;
1118 li->lm_addr = *lmp;
1119 li->l_addr_inferior = *l_addrp;
1120 li->l_ld = *l_ldp;
2268b414
JK
1121
1122 strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
1123 new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
1124 strcpy (new_elem->so_original_name, new_elem->so_name);
1125
1126 *list->tailp = new_elem;
1127 list->tailp = &new_elem->next;
1128}
1129
1130/* Handle the start of a <library-list-svr4> element. */
1131
1132static void
1133svr4_library_list_start_list (struct gdb_xml_parser *parser,
1134 const struct gdb_xml_element *element,
4d0fdd9b
SM
1135 void *user_data,
1136 std::vector<gdb_xml_value> &attributes)
2268b414 1137{
19ba03f4
SM
1138 struct svr4_library_list *list = (struct svr4_library_list *) user_data;
1139 const char *version
4d0fdd9b 1140 = (const char *) xml_find_attribute (attributes, "version")->value.get ();
2268b414
JK
1141 struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");
1142
1143 if (strcmp (version, "1.0") != 0)
1144 gdb_xml_error (parser,
1145 _("SVR4 Library list has unsupported version \"%s\""),
1146 version);
1147
1148 if (main_lm)
4d0fdd9b 1149 list->main_lm = *(ULONGEST *) main_lm->value.get ();
2268b414
JK
1150}
1151
1152/* The allowed elements and attributes for an XML library list.
1153 The root element is a <library-list>. */
1154
1155static const struct gdb_xml_attribute svr4_library_attributes[] =
1156{
1157 { "name", GDB_XML_AF_NONE, NULL, NULL },
1158 { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1159 { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1160 { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1161 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1162};
1163
1164static const struct gdb_xml_element svr4_library_list_children[] =
1165{
1166 {
1167 "library", svr4_library_attributes, NULL,
1168 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
1169 library_list_start_library, NULL
1170 },
1171 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1172};
1173
1174static const struct gdb_xml_attribute svr4_library_list_attributes[] =
1175{
1176 { "version", GDB_XML_AF_NONE, NULL, NULL },
1177 { "main-lm", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
1178 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1179};
1180
1181static const struct gdb_xml_element svr4_library_list_elements[] =
1182{
1183 { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children,
1184 GDB_XML_EF_NONE, svr4_library_list_start_list, NULL },
1185 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1186};
1187
2268b414
JK
1188/* Parse qXfer:libraries:read packet into *SO_LIST_RETURN. Return 1 if
1189
1190 Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
1191 case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
1192 empty, caller is responsible for freeing all its entries. */
1193
1194static int
1195svr4_parse_libraries (const char *document, struct svr4_library_list *list)
1196{
1197 struct cleanup *back_to = make_cleanup (svr4_free_library_list,
1198 &list->head);
1199
1200 memset (list, 0, sizeof (*list));
1201 list->tailp = &list->head;
2eca4a8d 1202 if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
2268b414
JK
1203 svr4_library_list_elements, document, list) == 0)
1204 {
1205 /* Parsed successfully, keep the result. */
1206 discard_cleanups (back_to);
1207 return 1;
1208 }
1209
1210 do_cleanups (back_to);
1211 return 0;
1212}
1213
f9e14852 1214/* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.
2268b414
JK
1215
1216 Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
1217 case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
f9e14852
GB
1218 empty, caller is responsible for freeing all its entries.
1219
1220 Note that ANNEX must be NULL if the remote does not explicitly allow
1221 qXfer:libraries-svr4:read packets with non-empty annexes. Support for
1222 this can be checked using target_augmented_libraries_svr4_read (). */
2268b414
JK
1223
1224static int
f9e14852
GB
1225svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
1226 const char *annex)
2268b414 1227{
f9e14852
GB
1228 gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());
1229
2268b414 1230 /* Fetch the list of shared libraries. */
9018be22 1231 gdb::optional<gdb::char_vector> svr4_library_document
8b88a78e 1232 = target_read_stralloc (current_top_target (), TARGET_OBJECT_LIBRARIES_SVR4,
b7b030ad 1233 annex);
9018be22 1234 if (!svr4_library_document)
2268b414
JK
1235 return 0;
1236
9018be22 1237 return svr4_parse_libraries (svr4_library_document->data (), list);
2268b414
JK
1238}
1239
1240#else
1241
1242static int
f9e14852
GB
1243svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
1244 const char *annex)
2268b414
JK
1245{
1246 return 0;
1247}
1248
1249#endif
1250
34439770
DJ
1251/* If no shared library information is available from the dynamic
1252 linker, build a fallback list from other sources. */
1253
1254static struct so_list *
1255svr4_default_sos (void)
1256{
6c95b8df 1257 struct svr4_info *info = get_svr4_info ();
fe978cb0 1258 struct so_list *newobj;
1a816a87 1259
8e5c319d
JK
1260 if (!info->debug_loader_offset_p)
1261 return NULL;
34439770 1262
fe978cb0 1263 newobj = XCNEW (struct so_list);
76e75227 1264 lm_info_svr4 *li = new lm_info_svr4;
d0e449a1 1265 newobj->lm_info = li;
34439770 1266
3957565a 1267 /* Nothing will ever check the other fields if we set l_addr_p. */
d0e449a1
SM
1268 li->l_addr = info->debug_loader_offset;
1269 li->l_addr_p = 1;
34439770 1270
fe978cb0
PA
1271 strncpy (newobj->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
1272 newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1273 strcpy (newobj->so_original_name, newobj->so_name);
34439770 1274
fe978cb0 1275 return newobj;
34439770
DJ
1276}
1277
f9e14852
GB
1278/* Read the whole inferior libraries chain starting at address LM.
1279 Expect the first entry in the chain's previous entry to be PREV_LM.
1280 Add the entries to the tail referenced by LINK_PTR_PTR. Ignore the
1281 first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
1282 to it. Returns nonzero upon success. If zero is returned the
1283 entries stored to LINK_PTR_PTR are still valid although they may
1284 represent only part of the inferior library list. */
13437d4b 1285
f9e14852
GB
1286static int
1287svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
1288 struct so_list ***link_ptr_ptr, int ignore_first)
13437d4b 1289{
c725e7b6 1290 CORE_ADDR first_l_name = 0;
f9e14852 1291 CORE_ADDR next_lm;
13437d4b 1292
cb08cc53 1293 for (; lm != 0; prev_lm = lm, lm = next_lm)
13437d4b 1294 {
cb08cc53 1295 int errcode;
e83e4e24 1296 gdb::unique_xmalloc_ptr<char> buffer;
13437d4b 1297
b3bc8453 1298 so_list_up newobj (XCNEW (struct so_list));
13437d4b 1299
a7961323 1300 lm_info_svr4 *li = lm_info_read (lm).release ();
d0e449a1
SM
1301 newobj->lm_info = li;
1302 if (li == NULL)
b3bc8453 1303 return 0;
13437d4b 1304
d0e449a1 1305 next_lm = li->l_next;
492928e4 1306
d0e449a1 1307 if (li->l_prev != prev_lm)
492928e4 1308 {
2268b414 1309 warning (_("Corrupted shared library list: %s != %s"),
f5656ead 1310 paddress (target_gdbarch (), prev_lm),
d0e449a1 1311 paddress (target_gdbarch (), li->l_prev));
f9e14852 1312 return 0;
492928e4 1313 }
13437d4b
KB
1314
1315 /* For SVR4 versions, the first entry in the link map is for the
1316 inferior executable, so we must ignore it. For some versions of
1317 SVR4, it has no name. For others (Solaris 2.3 for example), it
1318 does have a name, so we can no longer use a missing name to
c378eb4e 1319 decide when to ignore it. */
d0e449a1 1320 if (ignore_first && li->l_prev == 0)
93a57060 1321 {
cb08cc53
JK
1322 struct svr4_info *info = get_svr4_info ();
1323
d0e449a1
SM
1324 first_l_name = li->l_name;
1325 info->main_lm_addr = li->lm_addr;
cb08cc53 1326 continue;
93a57060 1327 }
13437d4b 1328
cb08cc53 1329 /* Extract this shared object's name. */
d0e449a1
SM
1330 target_read_string (li->l_name, &buffer, SO_NAME_MAX_PATH_SIZE - 1,
1331 &errcode);
cb08cc53
JK
1332 if (errcode != 0)
1333 {
7d760051
UW
1334 /* If this entry's l_name address matches that of the
1335 inferior executable, then this is not a normal shared
1336 object, but (most likely) a vDSO. In this case, silently
1337 skip it; otherwise emit a warning. */
d0e449a1 1338 if (first_l_name == 0 || li->l_name != first_l_name)
7d760051
UW
1339 warning (_("Can't read pathname for load map: %s."),
1340 safe_strerror (errcode));
cb08cc53 1341 continue;
13437d4b
KB
1342 }
1343
e83e4e24 1344 strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
fe978cb0
PA
1345 newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1346 strcpy (newobj->so_original_name, newobj->so_name);
492928e4 1347
cb08cc53
JK
1348 /* If this entry has no name, or its name matches the name
1349 for the main executable, don't include it in the list. */
fe978cb0 1350 if (! newobj->so_name[0] || match_main (newobj->so_name))
b3bc8453 1351 continue;
e4cd0d6a 1352
fe978cb0 1353 newobj->next = 0;
b3bc8453
TT
1354 /* Don't free it now. */
1355 **link_ptr_ptr = newobj.release ();
1356 *link_ptr_ptr = &(**link_ptr_ptr)->next;
13437d4b 1357 }
f9e14852
GB
1358
1359 return 1;
cb08cc53
JK
1360}
1361
f9e14852
GB
1362/* Read the full list of currently loaded shared objects directly
1363 from the inferior, without referring to any libraries read and
1364 stored by the probes interface. Handle special cases relating
1365 to the first elements of the list. */
cb08cc53
JK
1366
1367static struct so_list *
f9e14852 1368svr4_current_sos_direct (struct svr4_info *info)
cb08cc53
JK
1369{
1370 CORE_ADDR lm;
1371 struct so_list *head = NULL;
1372 struct so_list **link_ptr = &head;
cb08cc53
JK
1373 struct cleanup *back_to;
1374 int ignore_first;
2268b414
JK
1375 struct svr4_library_list library_list;
1376
0c5bf5a9
JK
1377 /* Fall back to manual examination of the target if the packet is not
1378 supported or gdbserver failed to find DT_DEBUG. gdb.server/solib-list.exp
1379 tests a case where gdbserver cannot find the shared libraries list while
1380 GDB itself is able to find it via SYMFILE_OBJFILE.
1381
1382 Unfortunately statically linked inferiors will also fall back through this
1383 suboptimal code path. */
1384
f9e14852
GB
1385 info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list,
1386 NULL);
1387 if (info->using_xfer)
2268b414
JK
1388 {
1389 if (library_list.main_lm)
f9e14852 1390 info->main_lm_addr = library_list.main_lm;
2268b414
JK
1391
1392 return library_list.head ? library_list.head : svr4_default_sos ();
1393 }
cb08cc53 1394
cb08cc53
JK
1395 /* Always locate the debug struct, in case it has moved. */
1396 info->debug_base = 0;
1397 locate_base (info);
1398
1399 /* If we can't find the dynamic linker's base structure, this
1400 must not be a dynamically linked executable. Hmm. */
1401 if (! info->debug_base)
1402 return svr4_default_sos ();
1403
1404 /* Assume that everything is a library if the dynamic loader was loaded
1405 late by a static executable. */
1406 if (exec_bfd && bfd_get_section_by_name (exec_bfd, ".dynamic") == NULL)
1407 ignore_first = 0;
1408 else
1409 ignore_first = 1;
1410
1411 back_to = make_cleanup (svr4_free_library_list, &head);
1412
1413 /* Walk the inferior's link map list, and build our list of
1414 `struct so_list' nodes. */
1415 lm = solib_svr4_r_map (info);
1416 if (lm)
f9e14852 1417 svr4_read_so_list (lm, 0, &link_ptr, ignore_first);
cb08cc53
JK
1418
1419 /* On Solaris, the dynamic linker is not in the normal list of
1420 shared objects, so make sure we pick it up too. Having
1421 symbol information for the dynamic linker is quite crucial
1422 for skipping dynamic linker resolver code. */
1423 lm = solib_svr4_r_ldsomap (info);
1424 if (lm)
f9e14852 1425 svr4_read_so_list (lm, 0, &link_ptr, 0);
cb08cc53
JK
1426
1427 discard_cleanups (back_to);
13437d4b 1428
34439770
DJ
1429 if (head == NULL)
1430 return svr4_default_sos ();
1431
13437d4b
KB
1432 return head;
1433}
1434
8b9a549d
PA
1435/* Implement the main part of the "current_sos" target_so_ops
1436 method. */
f9e14852
GB
1437
1438static struct so_list *
8b9a549d 1439svr4_current_sos_1 (void)
f9e14852
GB
1440{
1441 struct svr4_info *info = get_svr4_info ();
1442
1443 /* If the solib list has been read and stored by the probes
1444 interface then we return a copy of the stored list. */
1445 if (info->solib_list != NULL)
1446 return svr4_copy_library_list (info->solib_list);
1447
1448 /* Otherwise obtain the solib list directly from the inferior. */
1449 return svr4_current_sos_direct (info);
1450}
1451
8b9a549d
PA
1452/* Implement the "current_sos" target_so_ops method. */
1453
1454static struct so_list *
1455svr4_current_sos (void)
1456{
1457 struct so_list *so_head = svr4_current_sos_1 ();
1458 struct mem_range vsyscall_range;
1459
1460 /* Filter out the vDSO module, if present. Its symbol file would
1461 not be found on disk. The vDSO/vsyscall's OBJFILE is instead
1462 managed by symfile-mem.c:add_vsyscall_page. */
1463 if (gdbarch_vsyscall_range (target_gdbarch (), &vsyscall_range)
1464 && vsyscall_range.length != 0)
1465 {
1466 struct so_list **sop;
1467
1468 sop = &so_head;
1469 while (*sop != NULL)
1470 {
1471 struct so_list *so = *sop;
1472
1473 /* We can't simply match the vDSO by starting address alone,
1474 because lm_info->l_addr_inferior (and also l_addr) do not
1475 necessarily represent the real starting address of the
1476 ELF if the vDSO's ELF itself is "prelinked". The l_ld
1477 field (the ".dynamic" section of the shared object)
1478 always points at the absolute/resolved address though.
1479 So check whether that address is inside the vDSO's
1480 mapping instead.
1481
1482 E.g., on Linux 3.16 (x86_64) the vDSO is a regular
1483 0-based ELF, and we see:
1484
1485 (gdb) info auxv
1486 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffb000
1487 (gdb) p/x *_r_debug.r_map.l_next
1488 $1 = {l_addr = 0x7ffff7ffb000, ..., l_ld = 0x7ffff7ffb318, ...}
1489
1490 And on Linux 2.6.32 (x86_64) we see:
1491
1492 (gdb) info auxv
1493 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffe000
1494 (gdb) p/x *_r_debug.r_map.l_next
1495 $5 = {l_addr = 0x7ffff88fe000, ..., l_ld = 0x7ffff7ffe580, ... }
1496
1497 Dumping that vDSO shows:
1498
1499 (gdb) info proc mappings
1500 0x7ffff7ffe000 0x7ffff7fff000 0x1000 0 [vdso]
1501 (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000
1502 # readelf -Wa vdso.bin
1503 [...]
1504 Entry point address: 0xffffffffff700700
1505 [...]
1506 Section Headers:
1507 [Nr] Name Type Address Off Size
1508 [ 0] NULL 0000000000000000 000000 000000
1509 [ 1] .hash HASH ffffffffff700120 000120 000038
1510 [ 2] .dynsym DYNSYM ffffffffff700158 000158 0000d8
1511 [...]
1512 [ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
1513 */
d0e449a1
SM
1514
1515 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1516
1517 if (address_in_mem_range (li->l_ld, &vsyscall_range))
8b9a549d
PA
1518 {
1519 *sop = so->next;
1520 free_so (so);
1521 break;
1522 }
1523
1524 sop = &so->next;
1525 }
1526 }
1527
1528 return so_head;
1529}
1530
93a57060 1531/* Get the address of the link_map for a given OBJFILE. */
bc4a16ae
EZ
1532
1533CORE_ADDR
1534svr4_fetch_objfile_link_map (struct objfile *objfile)
1535{
93a57060 1536 struct so_list *so;
6c95b8df 1537 struct svr4_info *info = get_svr4_info ();
bc4a16ae 1538
93a57060 1539 /* Cause svr4_current_sos() to be run if it hasn't been already. */
1a816a87 1540 if (info->main_lm_addr == 0)
e696b3ad 1541 solib_add (NULL, 0, auto_solib_add);
bc4a16ae 1542
93a57060
DJ
1543 /* svr4_current_sos() will set main_lm_addr for the main executable. */
1544 if (objfile == symfile_objfile)
1a816a87 1545 return info->main_lm_addr;
93a57060
DJ
1546
1547 /* The other link map addresses may be found by examining the list
1548 of shared libraries. */
1549 for (so = master_so_list (); so; so = so->next)
1550 if (so->objfile == objfile)
d0e449a1
SM
1551 {
1552 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1553
1554 return li->lm_addr;
1555 }
93a57060
DJ
1556
1557 /* Not found! */
bc4a16ae
EZ
1558 return 0;
1559}
13437d4b
KB
1560
1561/* On some systems, the only way to recognize the link map entry for
1562 the main executable file is by looking at its name. Return
1563 non-zero iff SONAME matches one of the known main executable names. */
1564
1565static int
bc043ef3 1566match_main (const char *soname)
13437d4b 1567{
bc043ef3 1568 const char * const *mainp;
13437d4b
KB
1569
1570 for (mainp = main_name_list; *mainp != NULL; mainp++)
1571 {
1572 if (strcmp (soname, *mainp) == 0)
1573 return (1);
1574 }
1575
1576 return (0);
1577}
1578
13437d4b
KB
1579/* Return 1 if PC lies in the dynamic symbol resolution code of the
1580 SVR4 run time loader. */
13437d4b 1581
7d522c90 1582int
d7fa2ae2 1583svr4_in_dynsym_resolve_code (CORE_ADDR pc)
13437d4b 1584{
6c95b8df
PA
1585 struct svr4_info *info = get_svr4_info ();
1586
1587 return ((pc >= info->interp_text_sect_low
1588 && pc < info->interp_text_sect_high)
1589 || (pc >= info->interp_plt_sect_low
1590 && pc < info->interp_plt_sect_high)
3e5d3a5a 1591 || in_plt_section (pc)
0875794a 1592 || in_gnu_ifunc_stub (pc));
13437d4b 1593}
13437d4b 1594
2f4950cd
AC
1595/* Given an executable's ABFD and target, compute the entry-point
1596 address. */
1597
1598static CORE_ADDR
1599exec_entry_point (struct bfd *abfd, struct target_ops *targ)
1600{
8c2b9656
YQ
1601 CORE_ADDR addr;
1602
2f4950cd
AC
1603 /* KevinB wrote ... for most targets, the address returned by
1604 bfd_get_start_address() is the entry point for the start
1605 function. But, for some targets, bfd_get_start_address() returns
1606 the address of a function descriptor from which the entry point
1607 address may be extracted. This address is extracted by
1608 gdbarch_convert_from_func_ptr_addr(). The method
1609 gdbarch_convert_from_func_ptr_addr() is the merely the identify
1610 function for targets which don't use function descriptors. */
8c2b9656 1611 addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2f4950cd
AC
1612 bfd_get_start_address (abfd),
1613 targ);
8c2b9656 1614 return gdbarch_addr_bits_remove (target_gdbarch (), addr);
2f4950cd 1615}
13437d4b 1616
f9e14852
GB
1617/* A probe and its associated action. */
1618
1619struct probe_and_action
1620{
1621 /* The probe. */
935676c9 1622 probe *prob;
f9e14852 1623
729662a5
TT
1624 /* The relocated address of the probe. */
1625 CORE_ADDR address;
1626
f9e14852
GB
1627 /* The action. */
1628 enum probe_action action;
1629};
1630
1631/* Returns a hash code for the probe_and_action referenced by p. */
1632
1633static hashval_t
1634hash_probe_and_action (const void *p)
1635{
19ba03f4 1636 const struct probe_and_action *pa = (const struct probe_and_action *) p;
f9e14852 1637
729662a5 1638 return (hashval_t) pa->address;
f9e14852
GB
1639}
1640
1641/* Returns non-zero if the probe_and_actions referenced by p1 and p2
1642 are equal. */
1643
1644static int
1645equal_probe_and_action (const void *p1, const void *p2)
1646{
19ba03f4
SM
1647 const struct probe_and_action *pa1 = (const struct probe_and_action *) p1;
1648 const struct probe_and_action *pa2 = (const struct probe_and_action *) p2;
f9e14852 1649
729662a5 1650 return pa1->address == pa2->address;
f9e14852
GB
1651}
1652
1653/* Register a solib event probe and its associated action in the
1654 probes table. */
1655
1656static void
935676c9 1657register_solib_event_probe (probe *prob, CORE_ADDR address,
729662a5 1658 enum probe_action action)
f9e14852
GB
1659{
1660 struct svr4_info *info = get_svr4_info ();
1661 struct probe_and_action lookup, *pa;
1662 void **slot;
1663
1664 /* Create the probes table, if necessary. */
1665 if (info->probes_table == NULL)
1666 info->probes_table = htab_create_alloc (1, hash_probe_and_action,
1667 equal_probe_and_action,
1668 xfree, xcalloc, xfree);
1669
935676c9 1670 lookup.prob = prob;
729662a5 1671 lookup.address = address;
f9e14852
GB
1672 slot = htab_find_slot (info->probes_table, &lookup, INSERT);
1673 gdb_assert (*slot == HTAB_EMPTY_ENTRY);
1674
1675 pa = XCNEW (struct probe_and_action);
935676c9 1676 pa->prob = prob;
729662a5 1677 pa->address = address;
f9e14852
GB
1678 pa->action = action;
1679
1680 *slot = pa;
1681}
1682
1683/* Get the solib event probe at the specified location, and the
1684 action associated with it. Returns NULL if no solib event probe
1685 was found. */
1686
1687static struct probe_and_action *
1688solib_event_probe_at (struct svr4_info *info, CORE_ADDR address)
1689{
f9e14852
GB
1690 struct probe_and_action lookup;
1691 void **slot;
1692
729662a5 1693 lookup.address = address;
f9e14852
GB
1694 slot = htab_find_slot (info->probes_table, &lookup, NO_INSERT);
1695
1696 if (slot == NULL)
1697 return NULL;
1698
1699 return (struct probe_and_action *) *slot;
1700}
1701
1702/* Decide what action to take when the specified solib event probe is
1703 hit. */
1704
1705static enum probe_action
1706solib_event_probe_action (struct probe_and_action *pa)
1707{
1708 enum probe_action action;
73c6b475 1709 unsigned probe_argc = 0;
08a6411c 1710 struct frame_info *frame = get_current_frame ();
f9e14852
GB
1711
1712 action = pa->action;
1713 if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED)
1714 return action;
1715
1716 gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);
1717
1718 /* Check that an appropriate number of arguments has been supplied.
1719 We expect:
1720 arg0: Lmid_t lmid (mandatory)
1721 arg1: struct r_debug *debug_base (mandatory)
1722 arg2: struct link_map *new (optional, for incremental updates) */
3bd7e5b7
SDJ
1723 TRY
1724 {
935676c9 1725 probe_argc = pa->prob->get_argument_count (frame);
3bd7e5b7
SDJ
1726 }
1727 CATCH (ex, RETURN_MASK_ERROR)
1728 {
1729 exception_print (gdb_stderr, ex);
1730 probe_argc = 0;
1731 }
1732 END_CATCH
1733
935676c9
SDJ
1734 /* If get_argument_count throws an exception, probe_argc will be set
1735 to zero. However, if pa->prob does not have arguments, then
1736 get_argument_count will succeed but probe_argc will also be zero.
1737 Both cases happen because of different things, but they are
1738 treated equally here: action will be set to
3bd7e5b7 1739 PROBES_INTERFACE_FAILED. */
f9e14852
GB
1740 if (probe_argc == 2)
1741 action = FULL_RELOAD;
1742 else if (probe_argc < 2)
1743 action = PROBES_INTERFACE_FAILED;
1744
1745 return action;
1746}
1747
1748/* Populate the shared object list by reading the entire list of
1749 shared objects from the inferior. Handle special cases relating
1750 to the first elements of the list. Returns nonzero on success. */
1751
1752static int
1753solist_update_full (struct svr4_info *info)
1754{
1755 free_solib_list (info);
1756 info->solib_list = svr4_current_sos_direct (info);
1757
1758 return 1;
1759}
1760
1761/* Update the shared object list starting from the link-map entry
1762 passed by the linker in the probe's third argument. Returns
1763 nonzero if the list was successfully updated, or zero to indicate
1764 failure. */
1765
1766static int
1767solist_update_incremental (struct svr4_info *info, CORE_ADDR lm)
1768{
1769 struct so_list *tail;
1770 CORE_ADDR prev_lm;
1771
1772 /* svr4_current_sos_direct contains logic to handle a number of
1773 special cases relating to the first elements of the list. To
1774 avoid duplicating this logic we defer to solist_update_full
1775 if the list is empty. */
1776 if (info->solib_list == NULL)
1777 return 0;
1778
1779 /* Fall back to a full update if we are using a remote target
1780 that does not support incremental transfers. */
1781 if (info->using_xfer && !target_augmented_libraries_svr4_read ())
1782 return 0;
1783
1784 /* Walk to the end of the list. */
1785 for (tail = info->solib_list; tail->next != NULL; tail = tail->next)
1786 /* Nothing. */;
d0e449a1
SM
1787
1788 lm_info_svr4 *li = (lm_info_svr4 *) tail->lm_info;
1789 prev_lm = li->lm_addr;
f9e14852
GB
1790
1791 /* Read the new objects. */
1792 if (info->using_xfer)
1793 {
1794 struct svr4_library_list library_list;
1795 char annex[64];
1796
1797 xsnprintf (annex, sizeof (annex), "start=%s;prev=%s",
1798 phex_nz (lm, sizeof (lm)),
1799 phex_nz (prev_lm, sizeof (prev_lm)));
1800 if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
1801 return 0;
1802
1803 tail->next = library_list.head;
1804 }
1805 else
1806 {
1807 struct so_list **link = &tail->next;
1808
1809 /* IGNORE_FIRST may safely be set to zero here because the
1810 above check and deferral to solist_update_full ensures
1811 that this call to svr4_read_so_list will never see the
1812 first element. */
1813 if (!svr4_read_so_list (lm, prev_lm, &link, 0))
1814 return 0;
1815 }
1816
1817 return 1;
1818}
1819
1820/* Disable the probes-based linker interface and revert to the
1821 original interface. We don't reset the breakpoints as the
1822 ones set up for the probes-based interface are adequate. */
1823
1824static void
1825disable_probes_interface_cleanup (void *arg)
1826{
1827 struct svr4_info *info = get_svr4_info ();
1828
1829 warning (_("Probes-based dynamic linker interface failed.\n"
1830 "Reverting to original interface.\n"));
1831
1832 free_probes_table (info);
1833 free_solib_list (info);
1834}
1835
1836/* Update the solib list as appropriate when using the
1837 probes-based linker interface. Do nothing if using the
1838 standard interface. */
1839
1840static void
1841svr4_handle_solib_event (void)
1842{
1843 struct svr4_info *info = get_svr4_info ();
1844 struct probe_and_action *pa;
1845 enum probe_action action;
06424eac 1846 struct cleanup *old_chain;
ad1c917a 1847 struct value *val = NULL;
f9e14852 1848 CORE_ADDR pc, debug_base, lm = 0;
08a6411c 1849 struct frame_info *frame = get_current_frame ();
f9e14852
GB
1850
1851 /* Do nothing if not using the probes interface. */
1852 if (info->probes_table == NULL)
1853 return;
1854
1855 /* If anything goes wrong we revert to the original linker
1856 interface. */
1857 old_chain = make_cleanup (disable_probes_interface_cleanup, NULL);
1858
1859 pc = regcache_read_pc (get_current_regcache ());
1860 pa = solib_event_probe_at (info, pc);
1861 if (pa == NULL)
1862 {
1863 do_cleanups (old_chain);
1864 return;
1865 }
1866
1867 action = solib_event_probe_action (pa);
1868 if (action == PROBES_INTERFACE_FAILED)
1869 {
1870 do_cleanups (old_chain);
1871 return;
1872 }
1873
1874 if (action == DO_NOTHING)
1875 {
1876 discard_cleanups (old_chain);
1877 return;
1878 }
1879
935676c9 1880 /* evaluate_argument looks up symbols in the dynamic linker
f9e14852
GB
1881 using find_pc_section. find_pc_section is accelerated by a cache
1882 called the section map. The section map is invalidated every
1883 time a shared library is loaded or unloaded, and if the inferior
1884 is generating a lot of shared library events then the section map
1885 will be updated every time svr4_handle_solib_event is called.
1886 We called find_pc_section in svr4_create_solib_event_breakpoints,
1887 so we can guarantee that the dynamic linker's sections are in the
1888 section map. We can therefore inhibit section map updates across
935676c9 1889 these calls to evaluate_argument and save a lot of time. */
06424eac
TT
1890 {
1891 scoped_restore inhibit_updates
1892 = inhibit_section_map_updates (current_program_space);
f9e14852 1893
06424eac
TT
1894 TRY
1895 {
1896 val = pa->prob->evaluate_argument (1, frame);
1897 }
1898 CATCH (ex, RETURN_MASK_ERROR)
1899 {
1900 exception_print (gdb_stderr, ex);
1901 val = NULL;
1902 }
1903 END_CATCH
f9e14852 1904
06424eac
TT
1905 if (val == NULL)
1906 {
1907 do_cleanups (old_chain);
1908 return;
1909 }
f9e14852 1910
06424eac
TT
1911 debug_base = value_as_address (val);
1912 if (debug_base == 0)
1913 {
1914 do_cleanups (old_chain);
1915 return;
1916 }
f9e14852 1917
06424eac
TT
1918 /* Always locate the debug struct, in case it moved. */
1919 info->debug_base = 0;
1920 if (locate_base (info) == 0)
1921 {
1922 do_cleanups (old_chain);
1923 return;
1924 }
3bd7e5b7 1925
06424eac
TT
1926 /* GDB does not currently support libraries loaded via dlmopen
1927 into namespaces other than the initial one. We must ignore
1928 any namespace other than the initial namespace here until
1929 support for this is added to GDB. */
1930 if (debug_base != info->debug_base)
1931 action = DO_NOTHING;
f9e14852 1932
06424eac
TT
1933 if (action == UPDATE_OR_RELOAD)
1934 {
1935 TRY
1936 {
1937 val = pa->prob->evaluate_argument (2, frame);
1938 }
1939 CATCH (ex, RETURN_MASK_ERROR)
1940 {
1941 exception_print (gdb_stderr, ex);
1942 do_cleanups (old_chain);
1943 return;
1944 }
1945 END_CATCH
1946
1947 if (val != NULL)
1948 lm = value_as_address (val);
1949
1950 if (lm == 0)
1951 action = FULL_RELOAD;
1952 }
f9e14852 1953
06424eac
TT
1954 /* Resume section map updates. Closing the scope is
1955 sufficient. */
1956 }
f9e14852
GB
1957
1958 if (action == UPDATE_OR_RELOAD)
1959 {
1960 if (!solist_update_incremental (info, lm))
1961 action = FULL_RELOAD;
1962 }
1963
1964 if (action == FULL_RELOAD)
1965 {
1966 if (!solist_update_full (info))
1967 {
1968 do_cleanups (old_chain);
1969 return;
1970 }
1971 }
1972
1973 discard_cleanups (old_chain);
1974}
1975
1976/* Helper function for svr4_update_solib_event_breakpoints. */
1977
1978static int
1979svr4_update_solib_event_breakpoint (struct breakpoint *b, void *arg)
1980{
1981 struct bp_location *loc;
1982
1983 if (b->type != bp_shlib_event)
1984 {
1985 /* Continue iterating. */
1986 return 0;
1987 }
1988
1989 for (loc = b->loc; loc != NULL; loc = loc->next)
1990 {
1991 struct svr4_info *info;
1992 struct probe_and_action *pa;
1993
19ba03f4
SM
1994 info = ((struct svr4_info *)
1995 program_space_data (loc->pspace, solib_svr4_pspace_data));
f9e14852
GB
1996 if (info == NULL || info->probes_table == NULL)
1997 continue;
1998
1999 pa = solib_event_probe_at (info, loc->address);
2000 if (pa == NULL)
2001 continue;
2002
2003 if (pa->action == DO_NOTHING)
2004 {
2005 if (b->enable_state == bp_disabled && stop_on_solib_events)
2006 enable_breakpoint (b);
2007 else if (b->enable_state == bp_enabled && !stop_on_solib_events)
2008 disable_breakpoint (b);
2009 }
2010
2011 break;
2012 }
2013
2014 /* Continue iterating. */
2015 return 0;
2016}
2017
2018/* Enable or disable optional solib event breakpoints as appropriate.
2019 Called whenever stop_on_solib_events is changed. */
2020
2021static void
2022svr4_update_solib_event_breakpoints (void)
2023{
2024 iterate_over_breakpoints (svr4_update_solib_event_breakpoint, NULL);
2025}
2026
2027/* Create and register solib event breakpoints. PROBES is an array
2028 of NUM_PROBES elements, each of which is vector of probes. A
2029 solib event breakpoint will be created and registered for each
2030 probe. */
2031
2032static void
2033svr4_create_probe_breakpoints (struct gdbarch *gdbarch,
45461e0d 2034 const std::vector<probe *> *probes,
729662a5 2035 struct objfile *objfile)
f9e14852 2036{
45461e0d 2037 for (int i = 0; i < NUM_PROBES; i++)
f9e14852
GB
2038 {
2039 enum probe_action action = probe_info[i].action;
f9e14852 2040
45461e0d 2041 for (probe *p : probes[i])
f9e14852 2042 {
935676c9 2043 CORE_ADDR address = p->get_relocated_address (objfile);
729662a5
TT
2044
2045 create_solib_event_breakpoint (gdbarch, address);
45461e0d 2046 register_solib_event_probe (p, address, action);
f9e14852
GB
2047 }
2048 }
2049
2050 svr4_update_solib_event_breakpoints ();
2051}
2052
2053/* Both the SunOS and the SVR4 dynamic linkers call a marker function
2054 before and after mapping and unmapping shared libraries. The sole
2055 purpose of this method is to allow debuggers to set a breakpoint so
2056 they can track these changes.
2057
2058 Some versions of the glibc dynamic linker contain named probes
2059 to allow more fine grained stopping. Given the address of the
2060 original marker function, this function attempts to find these
2061 probes, and if found, sets breakpoints on those instead. If the
2062 probes aren't found, a single breakpoint is set on the original
2063 marker function. */
2064
2065static void
2066svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
2067 CORE_ADDR address)
2068{
2069 struct obj_section *os;
2070
2071 os = find_pc_section (address);
2072 if (os != NULL)
2073 {
2074 int with_prefix;
2075
2076 for (with_prefix = 0; with_prefix <= 1; with_prefix++)
2077 {
45461e0d 2078 std::vector<probe *> probes[NUM_PROBES];
f9e14852 2079 int all_probes_found = 1;
25f9533e 2080 int checked_can_use_probe_arguments = 0;
f9e14852 2081
45461e0d 2082 for (int i = 0; i < NUM_PROBES; i++)
f9e14852
GB
2083 {
2084 const char *name = probe_info[i].name;
935676c9 2085 probe *p;
f9e14852
GB
2086 char buf[32];
2087
2088 /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4
2089 shipped with an early version of the probes code in
2090 which the probes' names were prefixed with "rtld_"
2091 and the "map_failed" probe did not exist. The
2092 locations of the probes are otherwise the same, so
2093 we check for probes with prefixed names if probes
2094 with unprefixed names are not present. */
2095 if (with_prefix)
2096 {
2097 xsnprintf (buf, sizeof (buf), "rtld_%s", name);
2098 name = buf;
2099 }
2100
2101 probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);
2102
2103 /* The "map_failed" probe did not exist in early
2104 versions of the probes code in which the probes'
2105 names were prefixed with "rtld_". */
2106 if (strcmp (name, "rtld_map_failed") == 0)
2107 continue;
2108
45461e0d 2109 if (probes[i].empty ())
f9e14852
GB
2110 {
2111 all_probes_found = 0;
2112 break;
2113 }
25f9533e
SDJ
2114
2115 /* Ensure probe arguments can be evaluated. */
2116 if (!checked_can_use_probe_arguments)
2117 {
45461e0d 2118 p = probes[i][0];
935676c9 2119 if (!p->can_evaluate_arguments ())
25f9533e
SDJ
2120 {
2121 all_probes_found = 0;
2122 break;
2123 }
2124 checked_can_use_probe_arguments = 1;
2125 }
f9e14852
GB
2126 }
2127
2128 if (all_probes_found)
729662a5 2129 svr4_create_probe_breakpoints (gdbarch, probes, os->objfile);
f9e14852 2130
f9e14852
GB
2131 if (all_probes_found)
2132 return;
2133 }
2134 }
2135
2136 create_solib_event_breakpoint (gdbarch, address);
2137}
2138
cb457ae2
YQ
2139/* Helper function for gdb_bfd_lookup_symbol. */
2140
2141static int
3953f15c 2142cmp_name_and_sec_flags (const asymbol *sym, const void *data)
cb457ae2
YQ
2143{
2144 return (strcmp (sym->name, (const char *) data) == 0
2145 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0);
2146}
7f86f058 2147/* Arrange for dynamic linker to hit breakpoint.
13437d4b
KB
2148
2149 Both the SunOS and the SVR4 dynamic linkers have, as part of their
2150 debugger interface, support for arranging for the inferior to hit
2151 a breakpoint after mapping in the shared libraries. This function
2152 enables that breakpoint.
2153
2154 For SunOS, there is a special flag location (in_debugger) which we
2155 set to 1. When the dynamic linker sees this flag set, it will set
2156 a breakpoint at a location known only to itself, after saving the
2157 original contents of that place and the breakpoint address itself,
2158 in it's own internal structures. When we resume the inferior, it
2159 will eventually take a SIGTRAP when it runs into the breakpoint.
2160 We handle this (in a different place) by restoring the contents of
2161 the breakpointed location (which is only known after it stops),
2162 chasing around to locate the shared libraries that have been
2163 loaded, then resuming.
2164
2165 For SVR4, the debugger interface structure contains a member (r_brk)
2166 which is statically initialized at the time the shared library is
2167 built, to the offset of a function (_r_debug_state) which is guaran-
2168 teed to be called once before mapping in a library, and again when
2169 the mapping is complete. At the time we are examining this member,
2170 it contains only the unrelocated offset of the function, so we have
2171 to do our own relocation. Later, when the dynamic linker actually
2172 runs, it relocates r_brk to be the actual address of _r_debug_state().
2173
2174 The debugger interface structure also contains an enumeration which
2175 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
2176 depending upon whether or not the library is being mapped or unmapped,
7f86f058 2177 and then set to RT_CONSISTENT after the library is mapped/unmapped. */
13437d4b
KB
2178
2179static int
268a4a75 2180enable_break (struct svr4_info *info, int from_tty)
13437d4b 2181{
3b7344d5 2182 struct bound_minimal_symbol msymbol;
bc043ef3 2183 const char * const *bkpt_namep;
13437d4b 2184 asection *interp_sect;
7cd25cfc 2185 CORE_ADDR sym_addr;
13437d4b 2186
6c95b8df
PA
2187 info->interp_text_sect_low = info->interp_text_sect_high = 0;
2188 info->interp_plt_sect_low = info->interp_plt_sect_high = 0;
13437d4b 2189
7cd25cfc
DJ
2190 /* If we already have a shared library list in the target, and
2191 r_debug contains r_brk, set the breakpoint there - this should
2192 mean r_brk has already been relocated. Assume the dynamic linker
2193 is the object containing r_brk. */
2194
e696b3ad 2195 solib_add (NULL, from_tty, auto_solib_add);
7cd25cfc 2196 sym_addr = 0;
1a816a87
PA
2197 if (info->debug_base && solib_svr4_r_map (info) != 0)
2198 sym_addr = solib_svr4_r_brk (info);
7cd25cfc
DJ
2199
2200 if (sym_addr != 0)
2201 {
2202 struct obj_section *os;
2203
b36ec657 2204 sym_addr = gdbarch_addr_bits_remove
8b88a78e
PA
2205 (target_gdbarch (),
2206 gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2207 sym_addr,
2208 current_top_target ()));
b36ec657 2209
48379de6
DE
2210 /* On at least some versions of Solaris there's a dynamic relocation
2211 on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
2212 we get control before the dynamic linker has self-relocated.
2213 Check if SYM_ADDR is in a known section, if it is assume we can
2214 trust its value. This is just a heuristic though, it could go away
2215 or be replaced if it's getting in the way.
2216
2217 On ARM we need to know whether the ISA of rtld_db_dlactivity (or
2218 however it's spelled in your particular system) is ARM or Thumb.
2219 That knowledge is encoded in the address, if it's Thumb the low bit
2220 is 1. However, we've stripped that info above and it's not clear
2221 what all the consequences are of passing a non-addr_bits_remove'd
f9e14852 2222 address to svr4_create_solib_event_breakpoints. The call to
48379de6
DE
2223 find_pc_section verifies we know about the address and have some
2224 hope of computing the right kind of breakpoint to use (via
2225 symbol info). It does mean that GDB needs to be pointed at a
2226 non-stripped version of the dynamic linker in order to obtain
2227 information it already knows about. Sigh. */
2228
7cd25cfc
DJ
2229 os = find_pc_section (sym_addr);
2230 if (os != NULL)
2231 {
2232 /* Record the relocated start and end address of the dynamic linker
2233 text and plt section for svr4_in_dynsym_resolve_code. */
2234 bfd *tmp_bfd;
2235 CORE_ADDR load_addr;
2236
2237 tmp_bfd = os->objfile->obfd;
2238 load_addr = ANOFFSET (os->objfile->section_offsets,
e03e6279 2239 SECT_OFF_TEXT (os->objfile));
7cd25cfc
DJ
2240
2241 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
2242 if (interp_sect)
2243 {
6c95b8df 2244 info->interp_text_sect_low =
7cd25cfc 2245 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
6c95b8df
PA
2246 info->interp_text_sect_high =
2247 info->interp_text_sect_low
2248 + bfd_section_size (tmp_bfd, interp_sect);
7cd25cfc
DJ
2249 }
2250 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
2251 if (interp_sect)
2252 {
6c95b8df 2253 info->interp_plt_sect_low =
7cd25cfc 2254 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
6c95b8df
PA
2255 info->interp_plt_sect_high =
2256 info->interp_plt_sect_low
2257 + bfd_section_size (tmp_bfd, interp_sect);
7cd25cfc
DJ
2258 }
2259
f9e14852 2260 svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
7cd25cfc
DJ
2261 return 1;
2262 }
2263 }
2264
97ec2c2f 2265 /* Find the program interpreter; if not found, warn the user and drop
13437d4b 2266 into the old breakpoint at symbol code. */
17658d46
SM
2267 gdb::optional<gdb::byte_vector> interp_name_holder
2268 = find_program_interpreter ();
2269 if (interp_name_holder)
13437d4b 2270 {
17658d46 2271 const char *interp_name = (const char *) interp_name_holder->data ();
8ad2fcde
KB
2272 CORE_ADDR load_addr = 0;
2273 int load_addr_found = 0;
2ec9a4f8 2274 int loader_found_in_list = 0;
f8766ec1 2275 struct so_list *so;
2f4950cd 2276 struct target_ops *tmp_bfd_target;
13437d4b 2277
7cd25cfc 2278 sym_addr = 0;
13437d4b
KB
2279
2280 /* Now we need to figure out where the dynamic linker was
2281 loaded so that we can load its symbols and place a breakpoint
2282 in the dynamic linker itself.
2283
2284 This address is stored on the stack. However, I've been unable
2285 to find any magic formula to find it for Solaris (appears to
2286 be trivial on GNU/Linux). Therefore, we have to try an alternate
2287 mechanism to find the dynamic linker's base address. */
e4f7b8c8 2288
192b62ce 2289 gdb_bfd_ref_ptr tmp_bfd;
492d29ea 2290 TRY
f1838a98 2291 {
97ec2c2f 2292 tmp_bfd = solib_bfd_open (interp_name);
f1838a98 2293 }
492d29ea
PA
2294 CATCH (ex, RETURN_MASK_ALL)
2295 {
2296 }
2297 END_CATCH
2298
13437d4b
KB
2299 if (tmp_bfd == NULL)
2300 goto bkpt_at_symbol;
2301
2f4950cd 2302 /* Now convert the TMP_BFD into a target. That way target, as
192b62ce
TT
2303 well as BFD operations can be used. target_bfd_reopen
2304 acquires its own reference. */
2305 tmp_bfd_target = target_bfd_reopen (tmp_bfd.get ());
2f4950cd 2306
f8766ec1
KB
2307 /* On a running target, we can get the dynamic linker's base
2308 address from the shared library table. */
f8766ec1
KB
2309 so = master_so_list ();
2310 while (so)
8ad2fcde 2311 {
97ec2c2f 2312 if (svr4_same_1 (interp_name, so->so_original_name))
8ad2fcde
KB
2313 {
2314 load_addr_found = 1;
2ec9a4f8 2315 loader_found_in_list = 1;
192b62ce 2316 load_addr = lm_addr_check (so, tmp_bfd.get ());
8ad2fcde
KB
2317 break;
2318 }
f8766ec1 2319 so = so->next;
8ad2fcde
KB
2320 }
2321
8d4e36ba
JB
2322 /* If we were not able to find the base address of the loader
2323 from our so_list, then try using the AT_BASE auxilliary entry. */
2324 if (!load_addr_found)
8b88a78e 2325 if (target_auxv_search (current_top_target (), AT_BASE, &load_addr) > 0)
ad3a0e5b 2326 {
f5656ead 2327 int addr_bit = gdbarch_addr_bit (target_gdbarch ());
ad3a0e5b
JK
2328
2329 /* Ensure LOAD_ADDR has proper sign in its possible upper bits so
2330 that `+ load_addr' will overflow CORE_ADDR width not creating
2331 invalid addresses like 0x101234567 for 32bit inferiors on 64bit
2332 GDB. */
2333
d182d057 2334 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
ad3a0e5b 2335 {
d182d057 2336 CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit;
192b62ce 2337 CORE_ADDR tmp_entry_point = exec_entry_point (tmp_bfd.get (),
ad3a0e5b
JK
2338 tmp_bfd_target);
2339
2340 gdb_assert (load_addr < space_size);
2341
2342 /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
2343 64bit ld.so with 32bit executable, it should not happen. */
2344
2345 if (tmp_entry_point < space_size
2346 && tmp_entry_point + load_addr >= space_size)
2347 load_addr -= space_size;
2348 }
2349
2350 load_addr_found = 1;
2351 }
8d4e36ba 2352
8ad2fcde
KB
2353 /* Otherwise we find the dynamic linker's base address by examining
2354 the current pc (which should point at the entry point for the
8d4e36ba
JB
2355 dynamic linker) and subtracting the offset of the entry point.
2356
2357 This is more fragile than the previous approaches, but is a good
2358 fallback method because it has actually been working well in
2359 most cases. */
8ad2fcde 2360 if (!load_addr_found)
fb14de7b 2361 {
c2250ad1 2362 struct regcache *regcache
f5656ead 2363 = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
433759f7 2364
fb14de7b 2365 load_addr = (regcache_read_pc (regcache)
192b62ce 2366 - exec_entry_point (tmp_bfd.get (), tmp_bfd_target));
fb14de7b 2367 }
2ec9a4f8
DJ
2368
2369 if (!loader_found_in_list)
34439770 2370 {
1a816a87
PA
2371 info->debug_loader_name = xstrdup (interp_name);
2372 info->debug_loader_offset_p = 1;
2373 info->debug_loader_offset = load_addr;
e696b3ad 2374 solib_add (NULL, from_tty, auto_solib_add);
34439770 2375 }
13437d4b
KB
2376
2377 /* Record the relocated start and end address of the dynamic linker
d7fa2ae2 2378 text and plt section for svr4_in_dynsym_resolve_code. */
192b62ce 2379 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
13437d4b
KB
2380 if (interp_sect)
2381 {
6c95b8df 2382 info->interp_text_sect_low =
192b62ce 2383 bfd_section_vma (tmp_bfd.get (), interp_sect) + load_addr;
6c95b8df
PA
2384 info->interp_text_sect_high =
2385 info->interp_text_sect_low
192b62ce 2386 + bfd_section_size (tmp_bfd.get (), interp_sect);
13437d4b 2387 }
192b62ce 2388 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
13437d4b
KB
2389 if (interp_sect)
2390 {
6c95b8df 2391 info->interp_plt_sect_low =
192b62ce 2392 bfd_section_vma (tmp_bfd.get (), interp_sect) + load_addr;
6c95b8df
PA
2393 info->interp_plt_sect_high =
2394 info->interp_plt_sect_low
192b62ce 2395 + bfd_section_size (tmp_bfd.get (), interp_sect);
13437d4b
KB
2396 }
2397
2398 /* Now try to set a breakpoint in the dynamic linker. */
2399 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
2400 {
192b62ce
TT
2401 sym_addr = gdb_bfd_lookup_symbol (tmp_bfd.get (),
2402 cmp_name_and_sec_flags,
3953f15c 2403 *bkpt_namep);
13437d4b
KB
2404 if (sym_addr != 0)
2405 break;
2406 }
2407
2bbe3cc1
DJ
2408 if (sym_addr != 0)
2409 /* Convert 'sym_addr' from a function pointer to an address.
2410 Because we pass tmp_bfd_target instead of the current
2411 target, this will always produce an unrelocated value. */
f5656ead 2412 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
2413 sym_addr,
2414 tmp_bfd_target);
2415
695c3173
TT
2416 /* We're done with both the temporary bfd and target. Closing
2417 the target closes the underlying bfd, because it holds the
2418 only remaining reference. */
460014f5 2419 target_close (tmp_bfd_target);
13437d4b
KB
2420
2421 if (sym_addr != 0)
2422 {
f9e14852
GB
2423 svr4_create_solib_event_breakpoints (target_gdbarch (),
2424 load_addr + sym_addr);
13437d4b
KB
2425 return 1;
2426 }
2427
2428 /* For whatever reason we couldn't set a breakpoint in the dynamic
2429 linker. Warn and drop into the old code. */
2430 bkpt_at_symbol:
82d03102
PG
2431 warning (_("Unable to find dynamic linker breakpoint function.\n"
2432 "GDB will be unable to debug shared library initializers\n"
2433 "and track explicitly loaded dynamic code."));
13437d4b 2434 }
13437d4b 2435
e499d0f1
DJ
2436 /* Scan through the lists of symbols, trying to look up the symbol and
2437 set a breakpoint there. Terminate loop when we/if we succeed. */
2438
2439 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
2440 {
2441 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
3b7344d5 2442 if ((msymbol.minsym != NULL)
77e371c0 2443 && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
e499d0f1 2444 {
77e371c0 2445 sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
f5656ead 2446 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
de64a9ac 2447 sym_addr,
8b88a78e 2448 current_top_target ());
f9e14852 2449 svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
e499d0f1
DJ
2450 return 1;
2451 }
2452 }
13437d4b 2453
17658d46 2454 if (interp_name_holder && !current_inferior ()->attach_flag)
13437d4b 2455 {
c6490bf2 2456 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
13437d4b 2457 {
c6490bf2 2458 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
3b7344d5 2459 if ((msymbol.minsym != NULL)
77e371c0 2460 && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
c6490bf2 2461 {
77e371c0 2462 sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
f5656ead 2463 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
c6490bf2 2464 sym_addr,
8b88a78e 2465 current_top_target ());
f9e14852 2466 svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
c6490bf2
KB
2467 return 1;
2468 }
13437d4b
KB
2469 }
2470 }
542c95c2 2471 return 0;
13437d4b
KB
2472}
2473
09919ac2
JK
2474/* Read the ELF program headers from ABFD. Return the contents and
2475 set *PHDRS_SIZE to the size of the program headers. */
e2a44558 2476
09919ac2
JK
2477static gdb_byte *
2478read_program_headers_from_bfd (bfd *abfd, int *phdrs_size)
e2a44558 2479{
09919ac2
JK
2480 Elf_Internal_Ehdr *ehdr;
2481 gdb_byte *buf;
e2a44558 2482
09919ac2 2483 ehdr = elf_elfheader (abfd);
b8040f19 2484
09919ac2
JK
2485 *phdrs_size = ehdr->e_phnum * ehdr->e_phentsize;
2486 if (*phdrs_size == 0)
2487 return NULL;
2488
224c3ddb 2489 buf = (gdb_byte *) xmalloc (*phdrs_size);
09919ac2
JK
2490 if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0
2491 || bfd_bread (buf, *phdrs_size, abfd) != *phdrs_size)
2492 {
2493 xfree (buf);
2494 return NULL;
2495 }
2496
2497 return buf;
b8040f19
JK
2498}
2499
01c30d6e
JK
2500/* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
2501 exec_bfd. Otherwise return 0.
2502
2503 We relocate all of the sections by the same amount. This
c378eb4e 2504 behavior is mandated by recent editions of the System V ABI.
b8040f19
JK
2505 According to the System V Application Binary Interface,
2506 Edition 4.1, page 5-5:
2507
2508 ... Though the system chooses virtual addresses for
2509 individual processes, it maintains the segments' relative
2510 positions. Because position-independent code uses relative
2511 addressesing between segments, the difference between
2512 virtual addresses in memory must match the difference
2513 between virtual addresses in the file. The difference
2514 between the virtual address of any segment in memory and
2515 the corresponding virtual address in the file is thus a
2516 single constant value for any one executable or shared
2517 object in a given process. This difference is the base
2518 address. One use of the base address is to relocate the
2519 memory image of the program during dynamic linking.
2520
2521 The same language also appears in Edition 4.0 of the System V
09919ac2
JK
2522 ABI and is left unspecified in some of the earlier editions.
2523
2524 Decide if the objfile needs to be relocated. As indicated above, we will
2525 only be here when execution is stopped. But during attachment PC can be at
2526 arbitrary address therefore regcache_read_pc can be misleading (contrary to
2527 the auxv AT_ENTRY value). Moreover for executable with interpreter section
2528 regcache_read_pc would point to the interpreter and not the main executable.
2529
2530 So, to summarize, relocations are necessary when the start address obtained
2531 from the executable is different from the address in auxv AT_ENTRY entry.
d989b283 2532
09919ac2
JK
2533 [ The astute reader will note that we also test to make sure that
2534 the executable in question has the DYNAMIC flag set. It is my
2535 opinion that this test is unnecessary (undesirable even). It
2536 was added to avoid inadvertent relocation of an executable
2537 whose e_type member in the ELF header is not ET_DYN. There may
2538 be a time in the future when it is desirable to do relocations
2539 on other types of files as well in which case this condition
2540 should either be removed or modified to accomodate the new file
2541 type. - Kevin, Nov 2000. ] */
b8040f19 2542
01c30d6e
JK
2543static int
2544svr4_exec_displacement (CORE_ADDR *displacementp)
b8040f19 2545{
41752192
JK
2546 /* ENTRY_POINT is a possible function descriptor - before
2547 a call to gdbarch_convert_from_func_ptr_addr. */
8f61baf8 2548 CORE_ADDR entry_point, exec_displacement;
b8040f19
JK
2549
2550 if (exec_bfd == NULL)
2551 return 0;
2552
09919ac2
JK
2553 /* Therefore for ELF it is ET_EXEC and not ET_DYN. Both shared libraries
2554 being executed themselves and PIE (Position Independent Executable)
2555 executables are ET_DYN. */
2556
2557 if ((bfd_get_file_flags (exec_bfd) & DYNAMIC) == 0)
2558 return 0;
2559
8b88a78e 2560 if (target_auxv_search (current_top_target (), AT_ENTRY, &entry_point) <= 0)
09919ac2
JK
2561 return 0;
2562
8f61baf8 2563 exec_displacement = entry_point - bfd_get_start_address (exec_bfd);
09919ac2 2564
8f61baf8 2565 /* Verify the EXEC_DISPLACEMENT candidate complies with the required page
09919ac2
JK
2566 alignment. It is cheaper than the program headers comparison below. */
2567
2568 if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
2569 {
2570 const struct elf_backend_data *elf = get_elf_backend_data (exec_bfd);
2571
2572 /* p_align of PT_LOAD segments does not specify any alignment but
2573 only congruency of addresses:
2574 p_offset % p_align == p_vaddr % p_align
2575 Kernel is free to load the executable with lower alignment. */
2576
8f61baf8 2577 if ((exec_displacement & (elf->minpagesize - 1)) != 0)
09919ac2
JK
2578 return 0;
2579 }
2580
2581 /* Verify that the auxilliary vector describes the same file as exec_bfd, by
2582 comparing their program headers. If the program headers in the auxilliary
2583 vector do not match the program headers in the executable, then we are
2584 looking at a different file than the one used by the kernel - for
2585 instance, "gdb program" connected to "gdbserver :PORT ld.so program". */
2586
2587 if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
2588 {
2589 /* Be optimistic and clear OK only if GDB was able to verify the headers
2590 really do not match. */
17658d46
SM
2591 int phdrs2_size, ok = 1;
2592 gdb_byte *buf2;
0a1e94c7 2593 int arch_size;
09919ac2 2594
17658d46
SM
2595 gdb::optional<gdb::byte_vector> phdrs_target
2596 = read_program_header (-1, &arch_size, NULL);
09919ac2 2597 buf2 = read_program_headers_from_bfd (exec_bfd, &phdrs2_size);
17658d46 2598 if (phdrs_target && buf2 != NULL)
0a1e94c7 2599 {
f5656ead 2600 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
0a1e94c7
JK
2601
2602 /* We are dealing with three different addresses. EXEC_BFD
2603 represents current address in on-disk file. target memory content
2604 may be different from EXEC_BFD as the file may have been prelinked
2605 to a different address after the executable has been loaded.
2606 Moreover the address of placement in target memory can be
3e43a32a
MS
2607 different from what the program headers in target memory say -
2608 this is the goal of PIE.
0a1e94c7
JK
2609
2610 Detected DISPLACEMENT covers both the offsets of PIE placement and
2611 possible new prelink performed after start of the program. Here
2612 relocate BUF and BUF2 just by the EXEC_BFD vs. target memory
2613 content offset for the verification purpose. */
2614
17658d46 2615 if (phdrs_target->size () != phdrs2_size
0a1e94c7
JK
2616 || bfd_get_arch_size (exec_bfd) != arch_size)
2617 ok = 0;
3e43a32a 2618 else if (arch_size == 32
17658d46
SM
2619 && phdrs_target->size () >= sizeof (Elf32_External_Phdr)
2620 && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
0a1e94c7
JK
2621 {
2622 Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
2623 Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
2624 CORE_ADDR displacement = 0;
2625 int i;
2626
2627 /* DISPLACEMENT could be found more easily by the difference of
2628 ehdr2->e_entry. But we haven't read the ehdr yet, and we
2629 already have enough information to compute that displacement
2630 with what we've read. */
2631
2632 for (i = 0; i < ehdr2->e_phnum; i++)
2633 if (phdr2[i].p_type == PT_LOAD)
2634 {
2635 Elf32_External_Phdr *phdrp;
2636 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2637 CORE_ADDR vaddr, paddr;
2638 CORE_ADDR displacement_vaddr = 0;
2639 CORE_ADDR displacement_paddr = 0;
2640
17658d46 2641 phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
0a1e94c7
JK
2642 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2643 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2644
2645 vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
2646 byte_order);
2647 displacement_vaddr = vaddr - phdr2[i].p_vaddr;
2648
2649 paddr = extract_unsigned_integer (buf_paddr_p, 4,
2650 byte_order);
2651 displacement_paddr = paddr - phdr2[i].p_paddr;
2652
2653 if (displacement_vaddr == displacement_paddr)
2654 displacement = displacement_vaddr;
2655
2656 break;
2657 }
2658
17658d46
SM
2659 /* Now compare program headers from the target and the binary
2660 with optional DISPLACEMENT. */
0a1e94c7 2661
17658d46
SM
2662 for (i = 0;
2663 i < phdrs_target->size () / sizeof (Elf32_External_Phdr);
2664 i++)
0a1e94c7
JK
2665 {
2666 Elf32_External_Phdr *phdrp;
2667 Elf32_External_Phdr *phdr2p;
2668 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2669 CORE_ADDR vaddr, paddr;
43b8e241 2670 asection *plt2_asect;
0a1e94c7 2671
17658d46 2672 phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
0a1e94c7
JK
2673 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2674 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2675 phdr2p = &((Elf32_External_Phdr *) buf2)[i];
2676
2677 /* PT_GNU_STACK is an exception by being never relocated by
2678 prelink as its addresses are always zero. */
2679
2680 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2681 continue;
2682
2683 /* Check also other adjustment combinations - PR 11786. */
2684
3e43a32a
MS
2685 vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
2686 byte_order);
0a1e94c7
JK
2687 vaddr -= displacement;
2688 store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr);
2689
3e43a32a
MS
2690 paddr = extract_unsigned_integer (buf_paddr_p, 4,
2691 byte_order);
0a1e94c7
JK
2692 paddr -= displacement;
2693 store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr);
2694
2695 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2696 continue;
2697
204b5331
DE
2698 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2699 CentOS-5 has problems with filesz, memsz as well.
be2d111a 2700 Strip also modifies memsz of PT_TLS.
204b5331 2701 See PR 11786. */
c44deb73
SM
2702 if (phdr2[i].p_type == PT_GNU_RELRO
2703 || phdr2[i].p_type == PT_TLS)
204b5331
DE
2704 {
2705 Elf32_External_Phdr tmp_phdr = *phdrp;
2706 Elf32_External_Phdr tmp_phdr2 = *phdr2p;
2707
2708 memset (tmp_phdr.p_filesz, 0, 4);
2709 memset (tmp_phdr.p_memsz, 0, 4);
2710 memset (tmp_phdr.p_flags, 0, 4);
2711 memset (tmp_phdr.p_align, 0, 4);
2712 memset (tmp_phdr2.p_filesz, 0, 4);
2713 memset (tmp_phdr2.p_memsz, 0, 4);
2714 memset (tmp_phdr2.p_flags, 0, 4);
2715 memset (tmp_phdr2.p_align, 0, 4);
2716
2717 if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
2718 == 0)
2719 continue;
2720 }
2721
43b8e241
JK
2722 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
2723 plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
2724 if (plt2_asect)
2725 {
2726 int content2;
2727 gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
2728 CORE_ADDR filesz;
2729
2730 content2 = (bfd_get_section_flags (exec_bfd, plt2_asect)
2731 & SEC_HAS_CONTENTS) != 0;
2732
2733 filesz = extract_unsigned_integer (buf_filesz_p, 4,
2734 byte_order);
2735
2736 /* PLT2_ASECT is from on-disk file (exec_bfd) while
2737 FILESZ is from the in-memory image. */
2738 if (content2)
2739 filesz += bfd_get_section_size (plt2_asect);
2740 else
2741 filesz -= bfd_get_section_size (plt2_asect);
2742
2743 store_unsigned_integer (buf_filesz_p, 4, byte_order,
2744 filesz);
2745
2746 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2747 continue;
2748 }
2749
0a1e94c7
JK
2750 ok = 0;
2751 break;
2752 }
2753 }
3e43a32a 2754 else if (arch_size == 64
17658d46
SM
2755 && phdrs_target->size () >= sizeof (Elf64_External_Phdr)
2756 && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
0a1e94c7
JK
2757 {
2758 Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
2759 Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
2760 CORE_ADDR displacement = 0;
2761 int i;
2762
2763 /* DISPLACEMENT could be found more easily by the difference of
2764 ehdr2->e_entry. But we haven't read the ehdr yet, and we
2765 already have enough information to compute that displacement
2766 with what we've read. */
2767
2768 for (i = 0; i < ehdr2->e_phnum; i++)
2769 if (phdr2[i].p_type == PT_LOAD)
2770 {
2771 Elf64_External_Phdr *phdrp;
2772 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2773 CORE_ADDR vaddr, paddr;
2774 CORE_ADDR displacement_vaddr = 0;
2775 CORE_ADDR displacement_paddr = 0;
2776
17658d46 2777 phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
0a1e94c7
JK
2778 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2779 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2780
2781 vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
2782 byte_order);
2783 displacement_vaddr = vaddr - phdr2[i].p_vaddr;
2784
2785 paddr = extract_unsigned_integer (buf_paddr_p, 8,
2786 byte_order);
2787 displacement_paddr = paddr - phdr2[i].p_paddr;
2788
2789 if (displacement_vaddr == displacement_paddr)
2790 displacement = displacement_vaddr;
2791
2792 break;
2793 }
2794
2795 /* Now compare BUF and BUF2 with optional DISPLACEMENT. */
2796
17658d46
SM
2797 for (i = 0;
2798 i < phdrs_target->size () / sizeof (Elf64_External_Phdr);
2799 i++)
0a1e94c7
JK
2800 {
2801 Elf64_External_Phdr *phdrp;
2802 Elf64_External_Phdr *phdr2p;
2803 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2804 CORE_ADDR vaddr, paddr;
43b8e241 2805 asection *plt2_asect;
0a1e94c7 2806
17658d46 2807 phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
0a1e94c7
JK
2808 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2809 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2810 phdr2p = &((Elf64_External_Phdr *) buf2)[i];
2811
2812 /* PT_GNU_STACK is an exception by being never relocated by
2813 prelink as its addresses are always zero. */
2814
2815 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2816 continue;
2817
2818 /* Check also other adjustment combinations - PR 11786. */
2819
3e43a32a
MS
2820 vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
2821 byte_order);
0a1e94c7
JK
2822 vaddr -= displacement;
2823 store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr);
2824
3e43a32a
MS
2825 paddr = extract_unsigned_integer (buf_paddr_p, 8,
2826 byte_order);
0a1e94c7
JK
2827 paddr -= displacement;
2828 store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr);
2829
2830 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2831 continue;
2832
204b5331
DE
2833 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2834 CentOS-5 has problems with filesz, memsz as well.
be2d111a 2835 Strip also modifies memsz of PT_TLS.
204b5331 2836 See PR 11786. */
c44deb73
SM
2837 if (phdr2[i].p_type == PT_GNU_RELRO
2838 || phdr2[i].p_type == PT_TLS)
204b5331
DE
2839 {
2840 Elf64_External_Phdr tmp_phdr = *phdrp;
2841 Elf64_External_Phdr tmp_phdr2 = *phdr2p;
2842
2843 memset (tmp_phdr.p_filesz, 0, 8);
2844 memset (tmp_phdr.p_memsz, 0, 8);
2845 memset (tmp_phdr.p_flags, 0, 4);
2846 memset (tmp_phdr.p_align, 0, 8);
2847 memset (tmp_phdr2.p_filesz, 0, 8);
2848 memset (tmp_phdr2.p_memsz, 0, 8);
2849 memset (tmp_phdr2.p_flags, 0, 4);
2850 memset (tmp_phdr2.p_align, 0, 8);
2851
2852 if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
2853 == 0)
2854 continue;
2855 }
2856
43b8e241
JK
2857 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
2858 plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
2859 if (plt2_asect)
2860 {
2861 int content2;
2862 gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
2863 CORE_ADDR filesz;
2864
2865 content2 = (bfd_get_section_flags (exec_bfd, plt2_asect)
2866 & SEC_HAS_CONTENTS) != 0;
2867
2868 filesz = extract_unsigned_integer (buf_filesz_p, 8,
2869 byte_order);
2870
2871 /* PLT2_ASECT is from on-disk file (exec_bfd) while
2872 FILESZ is from the in-memory image. */
2873 if (content2)
2874 filesz += bfd_get_section_size (plt2_asect);
2875 else
2876 filesz -= bfd_get_section_size (plt2_asect);
2877
2878 store_unsigned_integer (buf_filesz_p, 8, byte_order,
2879 filesz);
2880
2881 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2882 continue;
2883 }
2884
0a1e94c7
JK
2885 ok = 0;
2886 break;
2887 }
2888 }
2889 else
2890 ok = 0;
2891 }
09919ac2 2892
09919ac2
JK
2893 xfree (buf2);
2894
2895 if (!ok)
2896 return 0;
2897 }
b8040f19 2898
ccf26247
JK
2899 if (info_verbose)
2900 {
2901 /* It can be printed repeatedly as there is no easy way to check
2902 the executable symbols/file has been already relocated to
2903 displacement. */
2904
2905 printf_unfiltered (_("Using PIE (Position Independent Executable) "
2906 "displacement %s for \"%s\".\n"),
8f61baf8 2907 paddress (target_gdbarch (), exec_displacement),
ccf26247
JK
2908 bfd_get_filename (exec_bfd));
2909 }
2910
8f61baf8 2911 *displacementp = exec_displacement;
01c30d6e 2912 return 1;
b8040f19
JK
2913}
2914
2915/* Relocate the main executable. This function should be called upon
c378eb4e 2916 stopping the inferior process at the entry point to the program.
b8040f19
JK
2917 The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
2918 different, the main executable is relocated by the proper amount. */
2919
2920static void
2921svr4_relocate_main_executable (void)
2922{
01c30d6e
JK
2923 CORE_ADDR displacement;
2924
4e5799b6
JK
2925 /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS
2926 probably contains the offsets computed using the PIE displacement
2927 from the previous run, which of course are irrelevant for this run.
2928 So we need to determine the new PIE displacement and recompute the
2929 section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS
2930 already contains pre-computed offsets.
01c30d6e 2931
4e5799b6 2932 If we cannot compute the PIE displacement, either:
01c30d6e 2933
4e5799b6
JK
2934 - The executable is not PIE.
2935
2936 - SYMFILE_OBJFILE does not match the executable started in the target.
2937 This can happen for main executable symbols loaded at the host while
2938 `ld.so --ld-args main-executable' is loaded in the target.
2939
2940 Then we leave the section offsets untouched and use them as is for
2941 this run. Either:
2942
2943 - These section offsets were properly reset earlier, and thus
2944 already contain the correct values. This can happen for instance
2945 when reconnecting via the remote protocol to a target that supports
2946 the `qOffsets' packet.
2947
2948 - The section offsets were not reset earlier, and the best we can
c378eb4e 2949 hope is that the old offsets are still applicable to the new run. */
01c30d6e
JK
2950
2951 if (! svr4_exec_displacement (&displacement))
2952 return;
b8040f19 2953
01c30d6e
JK
2954 /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
2955 addresses. */
b8040f19
JK
2956
2957 if (symfile_objfile)
e2a44558 2958 {
e2a44558 2959 struct section_offsets *new_offsets;
b8040f19 2960 int i;
e2a44558 2961
224c3ddb
SM
2962 new_offsets = XALLOCAVEC (struct section_offsets,
2963 symfile_objfile->num_sections);
e2a44558 2964
b8040f19
JK
2965 for (i = 0; i < symfile_objfile->num_sections; i++)
2966 new_offsets->offsets[i] = displacement;
e2a44558 2967
b8040f19 2968 objfile_relocate (symfile_objfile, new_offsets);
e2a44558 2969 }
51bee8e9
JK
2970 else if (exec_bfd)
2971 {
2972 asection *asect;
2973
2974 for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
2975 exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
2976 (bfd_section_vma (exec_bfd, asect)
2977 + displacement));
2978 }
e2a44558
KB
2979}
2980
7f86f058 2981/* Implement the "create_inferior_hook" target_solib_ops method.
13437d4b
KB
2982
2983 For SVR4 executables, this first instruction is either the first
2984 instruction in the dynamic linker (for dynamically linked
2985 executables) or the instruction at "start" for statically linked
2986 executables. For dynamically linked executables, the system
2987 first exec's /lib/libc.so.N, which contains the dynamic linker,
2988 and starts it running. The dynamic linker maps in any needed
2989 shared libraries, maps in the actual user executable, and then
2990 jumps to "start" in the user executable.
2991
7f86f058
PA
2992 We can arrange to cooperate with the dynamic linker to discover the
2993 names of shared libraries that are dynamically linked, and the base
2994 addresses to which they are linked.
13437d4b
KB
2995
2996 This function is responsible for discovering those names and
2997 addresses, and saving sufficient information about them to allow
d2e5c99a 2998 their symbols to be read at a later time. */
13437d4b 2999
e2a44558 3000static void
268a4a75 3001svr4_solib_create_inferior_hook (int from_tty)
13437d4b 3002{
1a816a87
PA
3003 struct svr4_info *info;
3004
6c95b8df 3005 info = get_svr4_info ();
2020b7ab 3006
f9e14852
GB
3007 /* Clear the probes-based interface's state. */
3008 free_probes_table (info);
3009 free_solib_list (info);
3010
e2a44558 3011 /* Relocate the main executable if necessary. */
86e4bafc 3012 svr4_relocate_main_executable ();
e2a44558 3013
c91c8c16
PA
3014 /* No point setting a breakpoint in the dynamic linker if we can't
3015 hit it (e.g., a core file, or a trace file). */
3016 if (!target_has_execution)
3017 return;
3018
d5a921c9 3019 if (!svr4_have_link_map_offsets ())
513f5903 3020 return;
d5a921c9 3021
268a4a75 3022 if (!enable_break (info, from_tty))
542c95c2 3023 return;
13437d4b
KB
3024}
3025
3026static void
3027svr4_clear_solib (void)
3028{
6c95b8df
PA
3029 struct svr4_info *info;
3030
3031 info = get_svr4_info ();
3032 info->debug_base = 0;
3033 info->debug_loader_offset_p = 0;
3034 info->debug_loader_offset = 0;
3035 xfree (info->debug_loader_name);
3036 info->debug_loader_name = NULL;
13437d4b
KB
3037}
3038
6bb7be43
JB
3039/* Clear any bits of ADDR that wouldn't fit in a target-format
3040 data pointer. "Data pointer" here refers to whatever sort of
3041 address the dynamic linker uses to manage its sections. At the
3042 moment, we don't support shared libraries on any processors where
3043 code and data pointers are different sizes.
3044
3045 This isn't really the right solution. What we really need here is
3046 a way to do arithmetic on CORE_ADDR values that respects the
3047 natural pointer/address correspondence. (For example, on the MIPS,
3048 converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
3049 sign-extend the value. There, simply truncating the bits above
819844ad 3050 gdbarch_ptr_bit, as we do below, is no good.) This should probably
6bb7be43
JB
3051 be a new gdbarch method or something. */
3052static CORE_ADDR
3053svr4_truncate_ptr (CORE_ADDR addr)
3054{
f5656ead 3055 if (gdbarch_ptr_bit (target_gdbarch ()) == sizeof (CORE_ADDR) * 8)
6bb7be43
JB
3056 /* We don't need to truncate anything, and the bit twiddling below
3057 will fail due to overflow problems. */
3058 return addr;
3059 else
f5656ead 3060 return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
6bb7be43
JB
3061}
3062
3063
749499cb
KB
3064static void
3065svr4_relocate_section_addresses (struct so_list *so,
0542c86d 3066 struct target_section *sec)
749499cb 3067{
2b2848e2
DE
3068 bfd *abfd = sec->the_bfd_section->owner;
3069
3070 sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd));
3071 sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd));
749499cb 3072}
4b188b9f 3073\f
749499cb 3074
4b188b9f 3075/* Architecture-specific operations. */
6bb7be43 3076
4b188b9f
MK
3077/* Per-architecture data key. */
3078static struct gdbarch_data *solib_svr4_data;
e5e2b9ff 3079
4b188b9f 3080struct solib_svr4_ops
e5e2b9ff 3081{
4b188b9f
MK
3082 /* Return a description of the layout of `struct link_map'. */
3083 struct link_map_offsets *(*fetch_link_map_offsets)(void);
3084};
e5e2b9ff 3085
4b188b9f 3086/* Return a default for the architecture-specific operations. */
e5e2b9ff 3087
4b188b9f
MK
3088static void *
3089solib_svr4_init (struct obstack *obstack)
e5e2b9ff 3090{
4b188b9f 3091 struct solib_svr4_ops *ops;
e5e2b9ff 3092
4b188b9f 3093 ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops);
8d005789 3094 ops->fetch_link_map_offsets = NULL;
4b188b9f 3095 return ops;
e5e2b9ff
KB
3096}
3097
4b188b9f 3098/* Set the architecture-specific `struct link_map_offsets' fetcher for
7e3cb44c 3099 GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */
1c4dcb57 3100
21479ded 3101void
e5e2b9ff
KB
3102set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
3103 struct link_map_offsets *(*flmo) (void))
21479ded 3104{
19ba03f4
SM
3105 struct solib_svr4_ops *ops
3106 = (struct solib_svr4_ops *) gdbarch_data (gdbarch, solib_svr4_data);
4b188b9f
MK
3107
3108 ops->fetch_link_map_offsets = flmo;
7e3cb44c
UW
3109
3110 set_solib_ops (gdbarch, &svr4_so_ops);
21479ded
KB
3111}
3112
4b188b9f
MK
3113/* Fetch a link_map_offsets structure using the architecture-specific
3114 `struct link_map_offsets' fetcher. */
1c4dcb57 3115
4b188b9f
MK
3116static struct link_map_offsets *
3117svr4_fetch_link_map_offsets (void)
21479ded 3118{
19ba03f4
SM
3119 struct solib_svr4_ops *ops
3120 = (struct solib_svr4_ops *) gdbarch_data (target_gdbarch (),
3121 solib_svr4_data);
4b188b9f
MK
3122
3123 gdb_assert (ops->fetch_link_map_offsets);
3124 return ops->fetch_link_map_offsets ();
21479ded
KB
3125}
3126
4b188b9f
MK
3127/* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
3128
3129static int
3130svr4_have_link_map_offsets (void)
3131{
19ba03f4
SM
3132 struct solib_svr4_ops *ops
3133 = (struct solib_svr4_ops *) gdbarch_data (target_gdbarch (),
3134 solib_svr4_data);
433759f7 3135
4b188b9f
MK
3136 return (ops->fetch_link_map_offsets != NULL);
3137}
3138\f
3139
e4bbbda8
MK
3140/* Most OS'es that have SVR4-style ELF dynamic libraries define a
3141 `struct r_debug' and a `struct link_map' that are binary compatible
3142 with the origional SVR4 implementation. */
3143
3144/* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3145 for an ILP32 SVR4 system. */
d989b283 3146
e4bbbda8
MK
3147struct link_map_offsets *
3148svr4_ilp32_fetch_link_map_offsets (void)
3149{
3150 static struct link_map_offsets lmo;
3151 static struct link_map_offsets *lmp = NULL;
3152
3153 if (lmp == NULL)
3154 {
3155 lmp = &lmo;
3156
e4cd0d6a
MK
3157 lmo.r_version_offset = 0;
3158 lmo.r_version_size = 4;
e4bbbda8 3159 lmo.r_map_offset = 4;
7cd25cfc 3160 lmo.r_brk_offset = 8;
e4cd0d6a 3161 lmo.r_ldsomap_offset = 20;
e4bbbda8
MK
3162
3163 /* Everything we need is in the first 20 bytes. */
3164 lmo.link_map_size = 20;
3165 lmo.l_addr_offset = 0;
e4bbbda8 3166 lmo.l_name_offset = 4;
cc10cae3 3167 lmo.l_ld_offset = 8;
e4bbbda8 3168 lmo.l_next_offset = 12;
e4bbbda8 3169 lmo.l_prev_offset = 16;
e4bbbda8
MK
3170 }
3171
3172 return lmp;
3173}
3174
3175/* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3176 for an LP64 SVR4 system. */
d989b283 3177
e4bbbda8
MK
3178struct link_map_offsets *
3179svr4_lp64_fetch_link_map_offsets (void)
3180{
3181 static struct link_map_offsets lmo;
3182 static struct link_map_offsets *lmp = NULL;
3183
3184 if (lmp == NULL)
3185 {
3186 lmp = &lmo;
3187
e4cd0d6a
MK
3188 lmo.r_version_offset = 0;
3189 lmo.r_version_size = 4;
e4bbbda8 3190 lmo.r_map_offset = 8;
7cd25cfc 3191 lmo.r_brk_offset = 16;
e4cd0d6a 3192 lmo.r_ldsomap_offset = 40;
e4bbbda8
MK
3193
3194 /* Everything we need is in the first 40 bytes. */
3195 lmo.link_map_size = 40;
3196 lmo.l_addr_offset = 0;
e4bbbda8 3197 lmo.l_name_offset = 8;
cc10cae3 3198 lmo.l_ld_offset = 16;
e4bbbda8 3199 lmo.l_next_offset = 24;
e4bbbda8 3200 lmo.l_prev_offset = 32;
e4bbbda8
MK
3201 }
3202
3203 return lmp;
3204}
3205\f
3206
7d522c90 3207struct target_so_ops svr4_so_ops;
13437d4b 3208
c378eb4e 3209/* Lookup global symbol for ELF DSOs linked with -Bsymbolic. Those DSOs have a
3a40aaa0
UW
3210 different rule for symbol lookup. The lookup begins here in the DSO, not in
3211 the main executable. */
3212
d12307c1 3213static struct block_symbol
efad9b6a 3214elf_lookup_lib_symbol (struct objfile *objfile,
3a40aaa0 3215 const char *name,
21b556f4 3216 const domain_enum domain)
3a40aaa0 3217{
61f0d762
JK
3218 bfd *abfd;
3219
3220 if (objfile == symfile_objfile)
3221 abfd = exec_bfd;
3222 else
3223 {
3224 /* OBJFILE should have been passed as the non-debug one. */
3225 gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
3226
3227 abfd = objfile->obfd;
3228 }
3229
a738da3a 3230 if (abfd == NULL || scan_dyntag (DT_SYMBOLIC, abfd, NULL, NULL) != 1)
d12307c1 3231 return (struct block_symbol) {NULL, NULL};
3a40aaa0 3232
94af9270 3233 return lookup_global_symbol_from_objfile (objfile, name, domain);
3a40aaa0
UW
3234}
3235
13437d4b
KB
3236void
3237_initialize_svr4_solib (void)
3238{
4b188b9f 3239 solib_svr4_data = gdbarch_data_register_pre_init (solib_svr4_init);
6c95b8df 3240 solib_svr4_pspace_data
8e260fc0 3241 = register_program_space_data_with_cleanup (NULL, svr4_pspace_data_cleanup);
4b188b9f 3242
749499cb 3243 svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
13437d4b 3244 svr4_so_ops.free_so = svr4_free_so;
0892cb63 3245 svr4_so_ops.clear_so = svr4_clear_so;
13437d4b
KB
3246 svr4_so_ops.clear_solib = svr4_clear_solib;
3247 svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
13437d4b
KB
3248 svr4_so_ops.current_sos = svr4_current_sos;
3249 svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
d7fa2ae2 3250 svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
831a0c44 3251 svr4_so_ops.bfd_open = solib_bfd_open;
3a40aaa0 3252 svr4_so_ops.lookup_lib_global_symbol = elf_lookup_lib_symbol;
a7c02bc8 3253 svr4_so_ops.same = svr4_same;
de18c1d8 3254 svr4_so_ops.keep_data_in_core = svr4_keep_data_in_core;
f9e14852
GB
3255 svr4_so_ops.update_breakpoints = svr4_update_solib_event_breakpoints;
3256 svr4_so_ops.handle_event = svr4_handle_solib_event;
13437d4b 3257}
This page took 2.1361 seconds and 4 git commands to generate.