-
Notifications
You must be signed in to change notification settings - Fork 562
/
Copy pathsolib-svr4.c
3274 lines (2658 loc) · 99.8 KB
/
solib-svr4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Handle SVR4 shared libraries for GDB, the GNU Debugger.
Copyright (C) 1990-2020 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "elf/external.h"
#include "elf/common.h"
#include "elf/mips.h"
#include "symtab.h"
#include "bfd.h"
#include "symfile.h"
#include "objfiles.h"
#include "gdbcore.h"
#include "target.h"
#include "inferior.h"
#include "infrun.h"
#include "regcache.h"
#include "gdbthread.h"
#include "observable.h"
#include "solist.h"
#include "solib.h"
#include "solib-svr4.h"
#include "bfd-target.h"
#include "elf-bfd.h"
#include "exec.h"
#include "auxv.h"
#include "gdb_bfd.h"
#include "probe.h"
static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
static int svr4_have_link_map_offsets (void);
static void svr4_relocate_main_executable (void);
static void svr4_free_library_list (void *p_list);
static void probes_table_remove_objfile_probes (struct objfile *objfile);
static void svr4_iterate_over_objfiles_in_search_order (
struct gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype *cb,
void *cb_data, struct objfile *objfile);
/* On SVR4 systems, a list of symbols in the dynamic linker where
GDB can try to place a breakpoint to monitor shared library
events.
If none of these symbols are found, or other errors occur, then
SVR4 systems will fall back to using a symbol as the "startup
mapping complete" breakpoint address. */
static const char * const solib_break_names[] =
{
"r_debug_state",
"_r_debug_state",
"_dl_debug_state",
"rtld_db_dlactivity",
"__dl_rtld_db_dlactivity",
"_rtld_debug_state",
NULL
};
static const char * const bkpt_names[] =
{
"_start",
"__start",
"main",
NULL
};
static const char * const main_name_list[] =
{
"main_$main",
NULL
};
/* What to do when a probe stop occurs. */
enum probe_action
{
/* Something went seriously wrong. Stop using probes and
revert to using the older interface. */
PROBES_INTERFACE_FAILED,
/* No action is required. The shared object list is still
valid. */
DO_NOTHING,
/* The shared object list should be reloaded entirely. */
FULL_RELOAD,
/* Attempt to incrementally update the shared object list. If
the update fails or is not possible, fall back to reloading
the list in full. */
UPDATE_OR_RELOAD,
};
/* A probe's name and its associated action. */
struct probe_info
{
/* The name of the probe. */
const char *name;
/* What to do when a probe stop occurs. */
enum probe_action action;
};
/* A list of named probes and their associated actions. If all
probes are present in the dynamic linker then the probes-based
interface will be used. */
static const struct probe_info probe_info[] =
{
{ "init_start", DO_NOTHING },
{ "init_complete", FULL_RELOAD },
{ "map_start", DO_NOTHING },
{ "map_failed", DO_NOTHING },
{ "reloc_complete", UPDATE_OR_RELOAD },
{ "unmap_start", DO_NOTHING },
{ "unmap_complete", FULL_RELOAD },
};
#define NUM_PROBES ARRAY_SIZE (probe_info)
/* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
the same shared library. */
static int
svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
{
if (strcmp (gdb_so_name, inferior_so_name) == 0)
return 1;
/* On Solaris, when starting inferior we think that dynamic linker is
/usr/lib/ld.so.1, but later on, the table of loaded shared libraries
contains /lib/ld.so.1. Sometimes one file is a link to another, but
sometimes they have identical content, but are not linked to each
other. We don't restrict this check for Solaris, but the chances
of running into this situation elsewhere are very low. */
if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
&& strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
return 1;
/* Similarly, we observed the same issue with amd64 and sparcv9, but with
different locations. */
if (strcmp (gdb_so_name, "/usr/lib/amd64/ld.so.1") == 0
&& strcmp (inferior_so_name, "/lib/amd64/ld.so.1") == 0)
return 1;
if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
&& strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
return 1;
return 0;
}
static int
svr4_same (struct so_list *gdb, struct so_list *inferior)
{
return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
}
static std::unique_ptr<lm_info_svr4>
lm_info_read (CORE_ADDR lm_addr)
{
struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
std::unique_ptr<lm_info_svr4> lm_info;
gdb::byte_vector lm (lmo->link_map_size);
if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
warning (_("Error reading shared library list entry at %s"),
paddress (target_gdbarch (), lm_addr));
else
{
struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
lm_info.reset (new lm_info_svr4);
lm_info->lm_addr = lm_addr;
lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
ptr_type);
lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
ptr_type);
lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
ptr_type);
lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
ptr_type);
}
return lm_info;
}
static int
has_lm_dynamic_from_link_map (void)
{
struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
return lmo->l_ld_offset >= 0;
}
static CORE_ADDR
lm_addr_check (const struct so_list *so, bfd *abfd)
{
lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
if (!li->l_addr_p)
{
struct bfd_section *dyninfo_sect;
CORE_ADDR l_addr, l_dynaddr, dynaddr;
l_addr = li->l_addr_inferior;
if (! abfd || ! has_lm_dynamic_from_link_map ())
goto set_addr;
l_dynaddr = li->l_ld;
dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
if (dyninfo_sect == NULL)
goto set_addr;
dynaddr = bfd_section_vma (dyninfo_sect);
if (dynaddr + l_addr != l_dynaddr)
{
CORE_ADDR align = 0x1000;
CORE_ADDR minpagesize = align;
if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
{
Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
int i;
align = 1;
for (i = 0; i < ehdr->e_phnum; i++)
if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
align = phdr[i].p_align;
minpagesize = get_elf_backend_data (abfd)->minpagesize;
}
/* Turn it into a mask. */
align--;
/* If the changes match the alignment requirements, we
assume we're using a core file that was generated by the
same binary, just prelinked with a different base offset.
If it doesn't match, we may have a different binary, the
same binary with the dynamic table loaded at an unrelated
location, or anything, really. To avoid regressions,
don't adjust the base offset in the latter case, although
odds are that, if things really changed, debugging won't
quite work.
One could expect more the condition
((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
but the one below is relaxed for PPC. The PPC kernel supports
either 4k or 64k page sizes. To be prepared for 64k pages,
PPC ELF files are built using an alignment requirement of 64k.
However, when running on a kernel supporting 4k pages, the memory
mapping of the library may not actually happen on a 64k boundary!
(In the usual case where (l_addr & align) == 0, this check is
equivalent to the possibly expected check above.)
Even on PPC it must be zero-aligned at least for MINPAGESIZE. */
l_addr = l_dynaddr - dynaddr;
if ((l_addr & (minpagesize - 1)) == 0
&& (l_addr & align) == ((l_dynaddr - dynaddr) & align))
{
if (info_verbose)
printf_unfiltered (_("Using PIC (Position Independent Code) "
"prelink displacement %s for \"%s\".\n"),
paddress (target_gdbarch (), l_addr),
so->so_name);
}
else
{
/* There is no way to verify the library file matches. prelink
can during prelinking of an unprelinked file (or unprelinking
of a prelinked file) shift the DYNAMIC segment by arbitrary
offset without any page size alignment. There is no way to
find out the ELF header and/or Program Headers for a limited
verification if it they match. One could do a verification
of the DYNAMIC segment. Still the found address is the best
one GDB could find. */
warning (_(".dynamic section for \"%s\" "
"is not at the expected address "
"(wrong library or version mismatch?)"), so->so_name);
}
}
set_addr:
li->l_addr = l_addr;
li->l_addr_p = 1;
}
return li->l_addr;
}
/* Per pspace SVR4 specific data. */
struct svr4_info
{
svr4_info () = default;
~svr4_info ();
/* Base of dynamic linker structures. */
CORE_ADDR debug_base = 0;
/* Validity flag for debug_loader_offset. */
int debug_loader_offset_p = 0;
/* Load address for the dynamic linker, inferred. */
CORE_ADDR debug_loader_offset = 0;
/* Name of the dynamic linker, valid if debug_loader_offset_p. */
char *debug_loader_name = nullptr;
/* Load map address for the main executable. */
CORE_ADDR main_lm_addr = 0;
CORE_ADDR interp_text_sect_low = 0;
CORE_ADDR interp_text_sect_high = 0;
CORE_ADDR interp_plt_sect_low = 0;
CORE_ADDR interp_plt_sect_high = 0;
/* Nonzero if the list of objects was last obtained from the target
via qXfer:libraries-svr4:read. */
int using_xfer = 0;
/* Table of struct probe_and_action instances, used by the
probes-based interface to map breakpoint addresses to probes
and their associated actions. Lookup is performed using
probe_and_action->prob->address. */
htab_up probes_table;
/* List of objects loaded into the inferior, used by the probes-
based interface. */
struct so_list *solib_list = nullptr;
};
/* Per-program-space data key. */
static const struct program_space_key<svr4_info> solib_svr4_pspace_data;
/* Free the probes table. */
static void
free_probes_table (struct svr4_info *info)
{
info->probes_table.reset (nullptr);
}
/* Free the solib list. */
static void
free_solib_list (struct svr4_info *info)
{
svr4_free_library_list (&info->solib_list);
info->solib_list = NULL;
}
svr4_info::~svr4_info ()
{
free_solib_list (this);
}
/* Get the svr4 data for program space PSPACE. If none is found yet, add it now.
This function always returns a valid object. */
static struct svr4_info *
get_svr4_info (program_space *pspace)
{
struct svr4_info *info = solib_svr4_pspace_data.get (pspace);
if (info == NULL)
info = solib_svr4_pspace_data.emplace (pspace);
return info;
}
/* Local function prototypes */
static int match_main (const char *);
/* Read program header TYPE from inferior memory. The header is found
by scanning the OS auxiliary vector.
If TYPE == -1, return the program headers instead of the contents of
one program header.
Return vector of bytes holding the program header contents, or an empty
optional on failure. If successful and P_ARCH_SIZE is non-NULL, the target
architecture size (32-bit or 64-bit) is returned to *P_ARCH_SIZE. Likewise,
the base address of the section is returned in *BASE_ADDR. */
static gdb::optional<gdb::byte_vector>
read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
{
enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
int arch_size, sect_size;
CORE_ADDR sect_addr;
int pt_phdr_p = 0;
/* Get required auxv elements from target. */
if (target_auxv_search (current_top_target (), AT_PHDR, &at_phdr) <= 0)
return {};
if (target_auxv_search (current_top_target (), AT_PHENT, &at_phent) <= 0)
return {};
if (target_auxv_search (current_top_target (), AT_PHNUM, &at_phnum) <= 0)
return {};
if (!at_phdr || !at_phnum)
return {};
/* Determine ELF architecture type. */
if (at_phent == sizeof (Elf32_External_Phdr))
arch_size = 32;
else if (at_phent == sizeof (Elf64_External_Phdr))
arch_size = 64;
else
return {};
/* Find the requested segment. */
if (type == -1)
{
sect_addr = at_phdr;
sect_size = at_phent * at_phnum;
}
else if (arch_size == 32)
{
Elf32_External_Phdr phdr;
int i;
/* Search for requested PHDR. */
for (i = 0; i < at_phnum; i++)
{
int p_type;
if (target_read_memory (at_phdr + i * sizeof (phdr),
(gdb_byte *)&phdr, sizeof (phdr)))
return {};
p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
4, byte_order);
if (p_type == PT_PHDR)
{
pt_phdr_p = 1;
pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
4, byte_order);
}
if (p_type == type)
break;
}
if (i == at_phnum)
return {};
/* Retrieve address and size. */
sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
4, byte_order);
sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
4, byte_order);
}
else
{
Elf64_External_Phdr phdr;
int i;
/* Search for requested PHDR. */
for (i = 0; i < at_phnum; i++)
{
int p_type;
if (target_read_memory (at_phdr + i * sizeof (phdr),
(gdb_byte *)&phdr, sizeof (phdr)))
return {};
p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
4, byte_order);
if (p_type == PT_PHDR)
{
pt_phdr_p = 1;
pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
8, byte_order);
}
if (p_type == type)
break;
}
if (i == at_phnum)
return {};
/* Retrieve address and size. */
sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
8, byte_order);
sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
8, byte_order);
}
/* PT_PHDR is optional, but we really need it
for PIE to make this work in general. */
if (pt_phdr_p)
{
/* at_phdr is real address in memory. pt_phdr is what pheader says it is.
Relocation offset is the difference between the two. */
sect_addr = sect_addr + (at_phdr - pt_phdr);
}
/* Read in requested program header. */
gdb::byte_vector buf (sect_size);
if (target_read_memory (sect_addr, buf.data (), sect_size))
return {};
if (p_arch_size)
*p_arch_size = arch_size;
if (base_addr)
*base_addr = sect_addr;
return buf;
}
/* Return program interpreter string. */
static gdb::optional<gdb::byte_vector>
find_program_interpreter (void)
{
/* If we have an exec_bfd, use its section table. */
if (exec_bfd
&& bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
{
struct bfd_section *interp_sect;
interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
if (interp_sect != NULL)
{
int sect_size = bfd_section_size (interp_sect);
gdb::byte_vector buf (sect_size);
bfd_get_section_contents (exec_bfd, interp_sect, buf.data (), 0,
sect_size);
return buf;
}
}
/* If we didn't find it, use the target auxiliary vector. */
return read_program_header (PT_INTERP, NULL, NULL);
}
/* Scan for DESIRED_DYNTAG in .dynamic section of ABFD. If DESIRED_DYNTAG is
found, 1 is returned and the corresponding PTR is set. */
static int
scan_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr,
CORE_ADDR *ptr_addr)
{
int arch_size, step, sect_size;
long current_dyntag;
CORE_ADDR dyn_ptr, dyn_addr;
gdb_byte *bufend, *bufstart, *buf;
Elf32_External_Dyn *x_dynp_32;
Elf64_External_Dyn *x_dynp_64;
struct bfd_section *sect;
struct target_section *target_section;
if (abfd == NULL)
return 0;
if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
return 0;
arch_size = bfd_get_arch_size (abfd);
if (arch_size == -1)
return 0;
/* Find the start address of the .dynamic section. */
sect = bfd_get_section_by_name (abfd, ".dynamic");
if (sect == NULL)
return 0;
for (target_section = current_target_sections->sections;
target_section < current_target_sections->sections_end;
target_section++)
if (sect == target_section->the_bfd_section)
break;
if (target_section < current_target_sections->sections_end)
dyn_addr = target_section->addr;
else
{
/* ABFD may come from OBJFILE acting only as a symbol file without being
loaded into the target (see add_symbol_file_command). This case is
such fallback to the file VMA address without the possibility of
having the section relocated to its actual in-memory address. */
dyn_addr = bfd_section_vma (sect);
}
/* Read in .dynamic from the BFD. We will get the actual value
from memory later. */
sect_size = bfd_section_size (sect);
buf = bufstart = (gdb_byte *) alloca (sect_size);
if (!bfd_get_section_contents (abfd, sect,
buf, 0, sect_size))
return 0;
/* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
: sizeof (Elf64_External_Dyn);
for (bufend = buf + sect_size;
buf < bufend;
buf += step)
{
if (arch_size == 32)
{
x_dynp_32 = (Elf32_External_Dyn *) buf;
current_dyntag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
}
else
{
x_dynp_64 = (Elf64_External_Dyn *) buf;
current_dyntag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
}
if (current_dyntag == DT_NULL)
return 0;
if (current_dyntag == desired_dyntag)
{
/* If requested, try to read the runtime value of this .dynamic
entry. */
if (ptr)
{
struct type *ptr_type;
gdb_byte ptr_buf[8];
CORE_ADDR ptr_addr_1;
ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
ptr_addr_1 = dyn_addr + (buf - bufstart) + arch_size / 8;
if (target_read_memory (ptr_addr_1, ptr_buf, arch_size / 8) == 0)
dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
*ptr = dyn_ptr;
if (ptr_addr)
*ptr_addr = dyn_addr + (buf - bufstart);
}
return 1;
}
}
return 0;
}
/* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable,
found by consulting the OS auxillary vector. If DESIRED_DYNTAG is found, 1
is returned and the corresponding PTR is set. */
static int
scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr,
CORE_ADDR *ptr_addr)
{
enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
int arch_size, step;
long current_dyntag;
CORE_ADDR dyn_ptr;
CORE_ADDR base_addr;
/* Read in .dynamic section. */
gdb::optional<gdb::byte_vector> ph_data
= read_program_header (PT_DYNAMIC, &arch_size, &base_addr);
if (!ph_data)
return 0;
/* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
: sizeof (Elf64_External_Dyn);
for (gdb_byte *buf = ph_data->data (), *bufend = buf + ph_data->size ();
buf < bufend; buf += step)
{
if (arch_size == 32)
{
Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
4, byte_order);
dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
4, byte_order);
}
else
{
Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
8, byte_order);
dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
8, byte_order);
}
if (current_dyntag == DT_NULL)
break;
if (current_dyntag == desired_dyntag)
{
if (ptr)
*ptr = dyn_ptr;
if (ptr_addr)
*ptr_addr = base_addr + buf - ph_data->data ();
return 1;
}
}
return 0;
}
/* Locate the base address of dynamic linker structs for SVR4 elf
targets.
For SVR4 elf targets the address of the dynamic linker's runtime
structure is contained within the dynamic info section in the
executable file. The dynamic section is also mapped into the
inferior address space. Because the runtime loader fills in the
real address before starting the inferior, we have to read in the
dynamic info section from the inferior address space.
If there are any errors while trying to find the address, we
silently return 0, otherwise the found address is returned. */
static CORE_ADDR
elf_locate_base (void)
{
struct bound_minimal_symbol msymbol;
CORE_ADDR dyn_ptr, dyn_ptr_addr;
/* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
instead of DT_DEBUG, although they sometimes contain an unused
DT_DEBUG. */
if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr, NULL)
|| scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL))
{
struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
gdb_byte *pbuf;
int pbuf_size = TYPE_LENGTH (ptr_type);
pbuf = (gdb_byte *) alloca (pbuf_size);
/* DT_MIPS_RLD_MAP contains a pointer to the address
of the dynamic link structure. */
if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
return 0;
return extract_typed_address (pbuf, ptr_type);
}
/* Then check DT_MIPS_RLD_MAP_REL. MIPS executables now use this form
because of needing to support PIE. DT_MIPS_RLD_MAP will also exist
in non-PIE. */
if (scan_dyntag (DT_MIPS_RLD_MAP_REL, exec_bfd, &dyn_ptr, &dyn_ptr_addr)
|| scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr))
{
struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
gdb_byte *pbuf;
int pbuf_size = TYPE_LENGTH (ptr_type);
pbuf = (gdb_byte *) alloca (pbuf_size);
/* DT_MIPS_RLD_MAP_REL contains an offset from the address of the
DT slot to the address of the dynamic link structure. */
if (target_read_memory (dyn_ptr + dyn_ptr_addr, pbuf, pbuf_size))
return 0;
return extract_typed_address (pbuf, ptr_type);
}
/* Find DT_DEBUG. */
if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr, NULL)
|| scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL))
return dyn_ptr;
/* This may be a static executable. Look for the symbol
conventionally named _r_debug, as a last resort. */
msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
if (msymbol.minsym != NULL)
return BMSYMBOL_VALUE_ADDRESS (msymbol);
/* DT_DEBUG entry not found. */
return 0;
}
/* Locate the base address of dynamic linker structs.
For both the SunOS and SVR4 shared library implementations, if the
inferior executable has been linked dynamically, there is a single
address somewhere in the inferior's data space which is the key to
locating all of the dynamic linker's runtime structures. This
address is the value of the debug base symbol. The job of this
function is to find and return that address, or to return 0 if there
is no such address (the executable is statically linked for example).
For SunOS, the job is almost trivial, since the dynamic linker and
all of it's structures are statically linked to the executable at
link time. Thus the symbol for the address we are looking for has
already been added to the minimal symbol table for the executable's
objfile at the time the symbol file's symbols were read, and all we
have to do is look it up there. Note that we explicitly do NOT want
to find the copies in the shared library.
The SVR4 version is a bit more complicated because the address
is contained somewhere in the dynamic info section. We have to go
to a lot more work to discover the address of the debug base symbol.
Because of this complexity, we cache the value we find and return that
value on subsequent invocations. Note there is no copy in the
executable symbol tables. */
static CORE_ADDR
locate_base (struct svr4_info *info)
{
/* Check to see if we have a currently valid address, and if so, avoid
doing all this work again and just return the cached address. If
we have no cached address, try to locate it in the dynamic info
section for ELF executables. There's no point in doing any of this
though if we don't have some link map offsets to work with. */
if (info->debug_base == 0 && svr4_have_link_map_offsets ())
info->debug_base = elf_locate_base ();
return info->debug_base;
}
/* Find the first element in the inferior's dynamic link map, and
return its address in the inferior. Return zero if the address
could not be determined.
FIXME: Perhaps we should validate the info somehow, perhaps by
checking r_version for a known version number, or r_state for
RT_CONSISTENT. */
static CORE_ADDR
solib_svr4_r_map (struct svr4_info *info)
{
struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
CORE_ADDR addr = 0;
try
{
addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset,
ptr_type);
}
catch (const gdb_exception_error &ex)
{
exception_print (gdb_stderr, ex);
}
return addr;
}
/* Find r_brk from the inferior's debug base. */
static CORE_ADDR
solib_svr4_r_brk (struct svr4_info *info)
{
struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
ptr_type);
}
/* Find the link map for the dynamic linker (if it is not in the
normal list of loaded shared objects). */
static CORE_ADDR
solib_svr4_r_ldsomap (struct svr4_info *info)
{
struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
enum bfd_endian byte_order = type_byte_order (ptr_type);
ULONGEST version = 0;
try
{
/* Check version, and return zero if `struct r_debug' doesn't have
the r_ldsomap member. */
version
= read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
lmo->r_version_size, byte_order);
}
catch (const gdb_exception_error &ex)
{
exception_print (gdb_stderr, ex);
}
if (version < 2 || lmo->r_ldsomap_offset == -1)
return 0;
return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
ptr_type);
}
/* On Solaris systems with some versions of the dynamic linker,
ld.so's l_name pointer points to the SONAME in the string table
rather than into writable memory. So that GDB can find shared
libraries when loading a core file generated by gcore, ensure that
memory areas containing the l_name string are saved in the core
file. */
static int
svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
{
struct svr4_info *info;
CORE_ADDR ldsomap;
CORE_ADDR name_lm;
info = get_svr4_info (current_program_space);
info->debug_base = 0;
locate_base (info);
if (!info->debug_base)
return 0;
ldsomap = solib_svr4_r_ldsomap (info);
if (!ldsomap)
return 0;
std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);
name_lm = li != NULL ? li->l_name : 0;
return (name_lm >= vaddr && name_lm < vaddr + size);
}
/* See solist.h. */
static int
open_symbol_file_object (int from_tty)
{
CORE_ADDR lm, l_name;
struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
int l_name_size = TYPE_LENGTH (ptr_type);
gdb::byte_vector l_name_buf (l_name_size);
struct svr4_info *info = get_svr4_info (current_program_space);
symfile_add_flags add_flags = 0;
if (from_tty)
add_flags |= SYMFILE_VERBOSE;
if (symfile_objfile)
if (!query (_("Attempt to reload symbols from process? ")))
return 0;
/* Always locate the debug struct, in case it has moved. */
info->debug_base = 0;
if (locate_base (info) == 0)
return 0; /* failed somehow... */
/* First link map member should be the executable. */
lm = solib_svr4_r_map (info);
if (lm == 0)
return 0; /* failed somehow... */
/* Read address of name from target memory to GDB. */
read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
/* Convert the address to host format. */
l_name = extract_typed_address (l_name_buf.data (), ptr_type);
if (l_name == 0)
return 0; /* No filename. */
/* Now fetch the filename from target memory. */
gdb::unique_xmalloc_ptr<char> filename
= target_read_string (l_name, SO_NAME_MAX_PATH_SIZE - 1);
if (filename == nullptr)
{
warning (_("failed to read exec filename from attached file"));
return 0;