-
Notifications
You must be signed in to change notification settings - Fork 752
/
Psymtab.c
3554 lines (3066 loc) · 90.5 KB
/
Psymtab.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2016 Joyent, Inc.
* Copyright (c) 2013 by Delphix. All rights reserved.
* Copyright 2021 Oxide Computer Company
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <memory.h>
#include <errno.h>
#include <dirent.h>
#include <signal.h>
#include <limits.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/crc32.h>
#include "libproc.h"
#include "Pcontrol.h"
#include "Putil.h"
#include "Psymtab_machelf.h"
static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
static map_info_t *exec_map(struct ps_prochandle *);
static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
static map_info_t *object_name_to_map(struct ps_prochandle *,
Lmid_t, const char *);
static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
uintptr_t);
#ifdef _LP64
static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
uintptr_t);
#endif
static uint32_t psym_crc32[] = { CRC32_TABLE };
#define DATA_TYPES \
((1 << STT_OBJECT) | (1 << STT_FUNC) | \
(1 << STT_COMMON) | (1 << STT_TLS))
#define IS_DATA_TYPE(tp) (((1 << (tp)) & DATA_TYPES) != 0)
#define MA_RWX (MA_READ | MA_WRITE | MA_EXEC)
/*
* Minimum and maximum length of a build-id that we'll accept. Generally it's a
* 20 byte SHA1 and it's expected that the first byte (which is two ascii
* characters) indicates a directory and the remaining bytes become the file
* name. Therefore, our minimum length is at least 2 bytes (one for the
* directory and one for the name) and the max is a bit over the minimum -- 64,
* just in case folks do something odd. The string length is three times the max
* length. This accounts for the fact that each byte is two characters, a null
* terminator, and the directory '/' character.
*/
#define MINBUILDID 2
#define MAXBUILDID 64
#define BUILDID_STRLEN (3*MAXBUILDID)
#define BUILDID_NAME ".note.gnu.build-id"
#define DBGLINK_NAME ".gnu_debuglink"
typedef enum {
PRO_NATURAL,
PRO_BYADDR,
PRO_BYNAME
} pr_order_t;
static int
addr_cmp(const void *aa, const void *bb)
{
uintptr_t a = *((uintptr_t *)aa);
uintptr_t b = *((uintptr_t *)bb);
if (a > b)
return (1);
if (a < b)
return (-1);
return (0);
}
/*
* This function creates a list of addresses for a load object's sections.
* The list is in ascending address order and alternates start address
* then end address for each section we're interested in. The function
* returns a pointer to the list, which must be freed by the caller.
*/
static uintptr_t *
get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n)
{
uintptr_t a, addr, *addrs, last = 0;
uint_t i, naddrs = 0, unordered = 0;
if (P->status.pr_dmodel == PR_MODEL_ILP32) {
Elf32_Ehdr ehdr;
Elf32_Phdr phdr;
uint_t phnum;
if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0)
return (NULL);
addrs = malloc(sizeof (uintptr_t) * phnum * 2);
a = ehdr_start + ehdr.e_phoff;
for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
if (Pread(P, &phdr, sizeof (phdr), a) !=
sizeof (phdr)) {
free(addrs);
return (NULL);
}
if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
continue;
addr = phdr.p_vaddr;
if (ehdr.e_type == ET_DYN)
addr += ehdr_start;
if (last > addr)
unordered = 1;
addrs[naddrs++] = addr;
addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
}
#ifdef _LP64
} else {
Elf64_Ehdr ehdr;
Elf64_Phdr phdr;
uint_t phnum;
if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0)
return (NULL);
addrs = malloc(sizeof (uintptr_t) * phnum * 2);
a = ehdr_start + ehdr.e_phoff;
for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
if (Pread(P, &phdr, sizeof (phdr), a) !=
sizeof (phdr)) {
free(addrs);
return (NULL);
}
if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
continue;
addr = phdr.p_vaddr;
if (ehdr.e_type == ET_DYN)
addr += ehdr_start;
if (last > addr)
unordered = 1;
addrs[naddrs++] = addr;
addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
}
#endif
}
if (unordered)
qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
*n = naddrs;
return (addrs);
}
/*
* Allocation function for a new file_info_t
*/
file_info_t *
file_info_new(struct ps_prochandle *P, map_info_t *mptr)
{
file_info_t *fptr;
map_info_t *mp;
uintptr_t mstart, mend, sstart, send;
uint_t i;
if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
return (NULL);
list_insert_tail(&P->file_head, fptr);
(void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
mptr->map_file = fptr;
fptr->file_ref = 1;
fptr->file_fd = -1;
fptr->file_dbgfile = -1;
P->num_files++;
/*
* To figure out which map_info_t instances correspond to the mappings
* for this load object we try to obtain the start and end address
* for each section of our in-memory ELF image. If successful, we
* walk down the list of addresses and the list of map_info_t
* instances in lock step to correctly find the mappings that
* correspond to this load object.
*/
if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr,
&fptr->file_nsaddrs)) == NULL)
return (fptr);
mp = P->mappings;
i = 0;
while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) {
/* Calculate the start and end of the mapping and section */
mstart = mp->map_pmap.pr_vaddr;
mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size;
sstart = fptr->file_saddrs[i];
send = fptr->file_saddrs[i + 1];
if (mend <= sstart) {
/* This mapping is below the current section */
mp++;
} else if (mstart >= send) {
/* This mapping is above the current section */
i += 2;
} else {
/* This mapping overlaps the current section */
if (mp->map_file == NULL) {
dprintf("file_info_new: associating "
"segment at %p\n",
(void *)mp->map_pmap.pr_vaddr);
mp->map_file = fptr;
fptr->file_ref++;
} else {
dprintf("file_info_new: segment at %p "
"already associated with %s\n",
(void *)mp->map_pmap.pr_vaddr,
(mp == mptr ? "this file" :
mp->map_file->file_pname));
}
mp++;
}
}
return (fptr);
}
/*
* Deallocation function for a file_info_t
*/
static void
file_info_free(struct ps_prochandle *P, file_info_t *fptr)
{
if (--fptr->file_ref == 0) {
list_remove(&P->file_head, fptr);
if (fptr->file_symtab.sym_elf) {
(void) elf_end(fptr->file_symtab.sym_elf);
free(fptr->file_symtab.sym_elfmem);
}
if (fptr->file_symtab.sym_byname)
free(fptr->file_symtab.sym_byname);
if (fptr->file_symtab.sym_byaddr)
free(fptr->file_symtab.sym_byaddr);
if (fptr->file_dynsym.sym_elf) {
(void) elf_end(fptr->file_dynsym.sym_elf);
free(fptr->file_dynsym.sym_elfmem);
}
if (fptr->file_dynsym.sym_byname)
free(fptr->file_dynsym.sym_byname);
if (fptr->file_dynsym.sym_byaddr)
free(fptr->file_dynsym.sym_byaddr);
if (fptr->file_lo)
free(fptr->file_lo);
if (fptr->file_lname)
free(fptr->file_lname);
if (fptr->file_rname)
free(fptr->file_rname);
if (fptr->file_elf)
(void) elf_end(fptr->file_elf);
if (fptr->file_elfmem != NULL)
free(fptr->file_elfmem);
if (fptr->file_fd >= 0)
(void) close(fptr->file_fd);
if (fptr->file_dbgelf)
(void) elf_end(fptr->file_dbgelf);
if (fptr->file_dbgfile >= 0)
(void) close(fptr->file_dbgfile);
ctf_close(fptr->file_ctfp);
free(fptr->file_ctf_buf);
if (fptr->file_saddrs)
free(fptr->file_saddrs);
free(fptr);
P->num_files--;
}
}
/*
* Deallocation function for a map_info_t
*/
static void
map_info_free(struct ps_prochandle *P, map_info_t *mptr)
{
file_info_t *fptr;
if ((fptr = mptr->map_file) != NULL) {
if (fptr->file_map == mptr)
fptr->file_map = NULL;
file_info_free(P, fptr);
}
if (P->execname && mptr == P->map_exec) {
free(P->execname);
P->execname = NULL;
}
if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
free(P->auxv);
P->auxv = NULL;
P->nauxv = 0;
}
if (mptr == P->map_exec)
P->map_exec = NULL;
if (mptr == P->map_ldso)
P->map_ldso = NULL;
}
/*
* Call-back function for librtld_db to iterate through all of its shared
* libraries. We use this to get the load object names for the mappings.
*/
static int
map_iter(const rd_loadobj_t *lop, void *cd)
{
char buf[PATH_MAX];
struct ps_prochandle *P = cd;
map_info_t *mptr;
file_info_t *fptr;
dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
dprintf("map_iter: base address doesn't match any mapping\n");
return (1); /* Base address does not match any mapping */
}
if ((fptr = mptr->map_file) == NULL &&
(fptr = file_info_new(P, mptr)) == NULL) {
dprintf("map_iter: failed to allocate a new file_info_t\n");
return (1); /* Failed to allocate a new file_info_t */
}
if ((fptr->file_lo == NULL) &&
(fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
dprintf("map_iter: failed to allocate rd_loadobj_t\n");
file_info_free(P, fptr);
return (1); /* Failed to allocate rd_loadobj_t */
}
fptr->file_map = mptr;
*fptr->file_lo = *lop;
fptr->file_lo->rl_plt_base = fptr->file_plt_base;
fptr->file_lo->rl_plt_size = fptr->file_plt_size;
if (fptr->file_lname) {
free(fptr->file_lname);
fptr->file_lname = NULL;
fptr->file_lbase = NULL;
}
if (fptr->file_rname) {
free(fptr->file_rname);
fptr->file_rname = NULL;
fptr->file_rbase = NULL;
}
if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
if ((fptr->file_lname = strdup(buf)) != NULL)
fptr->file_lbase = basename(fptr->file_lname);
} else {
dprintf("map_iter: failed to read string at %p\n",
(void *)lop->rl_nameaddr);
}
if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
((fptr->file_rname = strdup(buf)) != NULL))
fptr->file_rbase = basename(fptr->file_rname);
dprintf("loaded rd object %s lmid %lx\n",
fptr->file_lname ? buf : "<NULL>", lop->rl_lmident);
return (1);
}
static void
map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
{
file_info_t *fptr;
char buf[PATH_MAX];
if ((fptr = mptr->map_file) == NULL &&
(fptr = file_info_new(P, mptr)) == NULL)
return; /* Failed to allocate a new file_info_t */
fptr->file_map = mptr;
if ((fptr->file_lo == NULL) &&
(fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
file_info_free(P, fptr);
return; /* Failed to allocate rd_loadobj_t */
}
(void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
fptr->file_lo->rl_bend =
mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
fptr->file_lo->rl_plt_base = fptr->file_plt_base;
fptr->file_lo->rl_plt_size = fptr->file_plt_size;
if ((fptr->file_lname == NULL) &&
(fptr->file_lname = strdup(lname)) != NULL)
fptr->file_lbase = basename(fptr->file_lname);
if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
((fptr->file_rname = strdup(buf)) != NULL))
fptr->file_rbase = basename(fptr->file_rname);
}
static void
load_static_maps(struct ps_prochandle *P)
{
map_info_t *mptr;
/*
* Construct the map for the a.out.
*/
if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
map_set(P, mptr, "a.out");
/*
* If the dynamic linker exists for this process,
* construct the map for it.
*/
if (Pgetauxval(P, AT_BASE) != -1L &&
(mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
map_set(P, mptr, "ld.so.1");
}
int
Preadmaps(struct ps_prochandle *P, prmap_t **Pmapp, ssize_t *nmapp)
{
return (P->ops.pop_read_maps(P, Pmapp, nmapp, P->data));
}
/*
* Go through all the address space mappings, validating or updating
* the information already gathered, or gathering new information.
*
* This function is only called when we suspect that the mappings have changed
* because this is the first time we're calling it or because of rtld activity.
*/
void
Pupdate_maps(struct ps_prochandle *P)
{
prmap_t *Pmap = NULL;
prmap_t *pmap;
ssize_t nmap;
int i;
uint_t oldmapcount;
map_info_t *newmap, *newp;
map_info_t *mptr;
if (P->info_valid || P->state == PS_UNDEAD)
return;
Preadauxvec(P);
if (Preadmaps(P, &Pmap, &nmap) != 0)
return;
if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
return;
/*
* We try to merge any file information we may have for existing
* mappings, to avoid having to rebuild the file info.
*/
mptr = P->mappings;
pmap = Pmap;
newp = newmap;
oldmapcount = P->map_count;
for (i = 0; i < nmap; i++, pmap++, newp++) {
if (oldmapcount == 0) {
/*
* We've exhausted all the old mappings. Every new
* mapping should be added.
*/
newp->map_pmap = *pmap;
} else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
pmap->pr_size == mptr->map_pmap.pr_size &&
pmap->pr_offset == mptr->map_pmap.pr_offset &&
(pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
(mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
/*
* This mapping matches exactly. Copy over the old
* mapping, taking care to get the latest flags.
* Make sure the associated file_info_t is updated
* appropriately.
*/
*newp = *mptr;
if (P->map_exec == mptr)
P->map_exec = newp;
if (P->map_ldso == mptr)
P->map_ldso = newp;
newp->map_pmap.pr_mflags = pmap->pr_mflags;
if (mptr->map_file != NULL &&
mptr->map_file->file_map == mptr)
mptr->map_file->file_map = newp;
oldmapcount--;
mptr++;
} else if (pmap->pr_vaddr + pmap->pr_size >
mptr->map_pmap.pr_vaddr) {
/*
* The old mapping doesn't exist any more, remove it
* from the list.
*/
map_info_free(P, mptr);
oldmapcount--;
i--;
newp--;
pmap--;
mptr++;
} else {
/*
* This is a new mapping, add it directly.
*/
newp->map_pmap = *pmap;
}
}
/*
* Free any old maps
*/
while (oldmapcount) {
map_info_free(P, mptr);
oldmapcount--;
mptr++;
}
free(Pmap);
if (P->mappings != NULL)
free(P->mappings);
P->mappings = newmap;
P->map_count = P->map_alloc = nmap;
P->info_valid = 1;
/*
* Consult librtld_db to get the load object
* names for all of the shared libraries.
*/
if (P->rap != NULL)
(void) rd_loadobj_iter(P->rap, map_iter, P);
}
/*
* Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
* forcibly cache all of the symbol tables associated with all object files.
*/
void
Pupdate_syms(struct ps_prochandle *P)
{
file_info_t *fptr;
Pupdate_maps(P);
for (fptr = list_head(&P->file_head); fptr != NULL;
fptr = list_next(&P->file_head, fptr)) {
Pbuild_file_symtab(P, fptr);
(void) Pbuild_file_ctf(P, fptr);
}
}
/*
* Return the librtld_db agent handle for the victim process.
* The handle will become invalid at the next successful exec() and the
* client (caller of proc_rd_agent()) must not use it beyond that point.
* If the process is already dead, we've already tried our best to
* create the agent during core file initialization.
*/
rd_agent_t *
Prd_agent(struct ps_prochandle *P)
{
if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
Pupdate_maps(P);
if (P->num_files == 0)
load_static_maps(P);
rd_log(_libproc_debug);
if ((P->rap = rd_new(P)) != NULL)
(void) rd_loadobj_iter(P->rap, map_iter, P);
}
return (P->rap);
}
/*
* Return the prmap_t structure containing 'addr', but only if it
* is in the dynamic linker's link map and is the text section.
*/
const prmap_t *
Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
{
map_info_t *mptr;
if (!P->info_valid)
Pupdate_maps(P);
if ((mptr = Paddr2mptr(P, addr)) != NULL) {
file_info_t *fptr = build_map_symtab(P, mptr);
const prmap_t *pmp = &mptr->map_pmap;
/*
* Assume that if rl_data_base is NULL, it means that no
* data section was found for this load object, and that
* a section must be text. Otherwise, a section will be
* text unless it ends above the start of the data
* section.
*/
if (fptr != NULL && fptr->file_lo != NULL &&
(fptr->file_lo->rl_data_base == (uintptr_t)NULL ||
pmp->pr_vaddr + pmp->pr_size <=
fptr->file_lo->rl_data_base))
return (pmp);
}
return (NULL);
}
/*
* Return the prmap_t structure containing 'addr' (no restrictions on
* the type of mapping).
*/
const prmap_t *
Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
{
map_info_t *mptr;
if (!P->info_valid)
Pupdate_maps(P);
if ((mptr = Paddr2mptr(P, addr)) != NULL)
return (&mptr->map_pmap);
return (NULL);
}
/*
* Convert a full or partial load object name to the prmap_t for its
* corresponding primary text mapping.
*/
const prmap_t *
Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
{
map_info_t *mptr;
if (name == PR_OBJ_EVERY)
return (NULL); /* A reasonable mistake */
if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
return (&mptr->map_pmap);
return (NULL);
}
const prmap_t *
Pname_to_map(struct ps_prochandle *P, const char *name)
{
return (Plmid_to_map(P, PR_LMID_EVERY, name));
}
const rd_loadobj_t *
Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
{
map_info_t *mptr;
if (!P->info_valid)
Pupdate_maps(P);
if ((mptr = Paddr2mptr(P, addr)) == NULL)
return (NULL);
/*
* By building the symbol table, we implicitly bring the PLT
* information up to date in the load object.
*/
(void) build_map_symtab(P, mptr);
return (mptr->map_file->file_lo);
}
const rd_loadobj_t *
Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
{
map_info_t *mptr;
if (name == PR_OBJ_EVERY)
return (NULL);
if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
return (NULL);
/*
* By building the symbol table, we implicitly bring the PLT
* information up to date in the load object.
*/
(void) build_map_symtab(P, mptr);
return (mptr->map_file->file_lo);
}
const rd_loadobj_t *
Pname_to_loadobj(struct ps_prochandle *P, const char *name)
{
return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
}
ctf_file_t *
Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
{
ctf_sect_t ctdata, symtab, strtab;
sym_tbl_t *symp;
int err;
if (fptr->file_ctfp != NULL)
return (fptr->file_ctfp);
Pbuild_file_symtab(P, fptr);
if (fptr->file_ctf_size == 0)
return (NULL);
symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
if (symp->sym_data_pri == NULL)
return (NULL);
/*
* The buffer may alread be allocated if this is a core file that
* contained CTF data for this file.
*/
if (fptr->file_ctf_buf == NULL) {
fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
if (fptr->file_ctf_buf == NULL) {
dprintf("failed to allocate ctf buffer\n");
return (NULL);
}
if (pread(fptr->file_fd, fptr->file_ctf_buf,
fptr->file_ctf_size, fptr->file_ctf_off) !=
fptr->file_ctf_size) {
free(fptr->file_ctf_buf);
fptr->file_ctf_buf = NULL;
dprintf("failed to read ctf data\n");
return (NULL);
}
}
ctdata.cts_name = ".SUNW_ctf";
ctdata.cts_type = SHT_PROGBITS;
ctdata.cts_flags = 0;
ctdata.cts_data = fptr->file_ctf_buf;
ctdata.cts_size = fptr->file_ctf_size;
ctdata.cts_entsize = 1;
ctdata.cts_offset = 0;
symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
symtab.cts_type = symp->sym_hdr_pri.sh_type;
symtab.cts_flags = symp->sym_hdr_pri.sh_flags;
symtab.cts_data = symp->sym_data_pri->d_buf;
symtab.cts_size = symp->sym_hdr_pri.sh_size;
symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize;
symtab.cts_offset = symp->sym_hdr_pri.sh_offset;
strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
strtab.cts_type = symp->sym_strhdr.sh_type;
strtab.cts_flags = symp->sym_strhdr.sh_flags;
strtab.cts_data = symp->sym_strs;
strtab.cts_size = symp->sym_strhdr.sh_size;
strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
strtab.cts_offset = symp->sym_strhdr.sh_offset;
fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
if (fptr->file_ctfp == NULL) {
dprintf("ctf_bufopen() failed, error code %d\n", err);
free(fptr->file_ctf_buf);
fptr->file_ctf_buf = NULL;
return (NULL);
}
dprintf("loaded %lu bytes of CTF data for %s\n",
(ulong_t)fptr->file_ctf_size, fptr->file_pname);
return (fptr->file_ctfp);
}
ctf_file_t *
Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
{
map_info_t *mptr;
file_info_t *fptr;
if (!P->info_valid)
Pupdate_maps(P);
if ((mptr = Paddr2mptr(P, addr)) == NULL ||
(fptr = mptr->map_file) == NULL)
return (NULL);
return (Pbuild_file_ctf(P, fptr));
}
ctf_file_t *
Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
{
map_info_t *mptr;
file_info_t *fptr = NULL;
if (name == PR_OBJ_EVERY)
return (NULL);
/*
* While most idle files are all ELF objects, not all of them have
* mapping information available. There's nothing which would make
* sense to fake up for ET_REL. Instead, if we're being asked for their
* executable object and we know that the information is valid and they
* only have a single file, we jump straight to that file pointer.
*/
if (P->state == PS_IDLE && name == PR_OBJ_EXEC && P->info_valid == 1 &&
P->num_files == 1 && P->mappings == NULL) {
fptr = list_head(&P->file_head);
}
if (fptr == NULL) {
if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
(fptr = mptr->map_file) == NULL)
return (NULL);
}
return (Pbuild_file_ctf(P, fptr));
}
ctf_file_t *
Pname_to_ctf(struct ps_prochandle *P, const char *name)
{
return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
}
void
Preadauxvec(struct ps_prochandle *P)
{
if (P->auxv != NULL) {
free(P->auxv);
P->auxv = NULL;
P->nauxv = 0;
}
P->ops.pop_read_aux(P, &P->auxv, &P->nauxv, P->data);
}
/*
* Return a requested element from the process's aux vector.
* Return -1 on failure (this is adequate for our purposes).
*/
long
Pgetauxval(struct ps_prochandle *P, int type)
{
auxv_t *auxv;
if (P->auxv == NULL)
Preadauxvec(P);
if (P->auxv == NULL)
return (-1);
for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
if (auxv->a_type == type)
return (auxv->a_un.a_val);
}
return (-1);
}
/*
* Return a pointer to our internal copy of the process's aux vector.
* The caller should not hold on to this pointer across any libproc calls.
*/
const auxv_t *
Pgetauxvec(struct ps_prochandle *P)
{
static const auxv_t empty = { AT_NULL, 0L };
if (P->auxv == NULL)
Preadauxvec(P);
if (P->auxv == NULL)
return (&empty);
return (P->auxv);
}
/*
* Return 1 if the given mapping corresponds to the given file_info_t's
* load object; return 0 otherwise.
*/
static int
is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr)
{
prmap_t *pmap = &mptr->map_pmap;
rd_loadobj_t *lop = fptr->file_lo;
uint_t i;
uintptr_t mstart, mend, sstart, send;
/*
* We can get for free the start address of the text and data
* sections of the load object. Start by seeing if the mapping
* encloses either of these.
*/
if ((pmap->pr_vaddr <= lop->rl_base &&
lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
(pmap->pr_vaddr <= lop->rl_data_base &&
lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))
return (1);
/*
* It's still possible that this mapping correponds to the load
* object. Consider the example of a mapping whose start and end
* addresses correspond to those of the load object's text section.
* If the mapping splits, e.g. as a result of a segment demotion,
* then although both mappings are still backed by the same section,
* only one will be seen to enclose that section's start address.
* Thus, to be rigorous, we ask not whether this mapping encloses
* the start of a section, but whether there exists a section that
* overlaps this mapping.
*
* If we don't already have the section addresses, and we successfully
* get them, then we cache them in case we come here again.
*/
if (fptr->file_saddrs == NULL &&
(fptr->file_saddrs = get_saddrs(P,
fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL)
return (0);
mstart = mptr->map_pmap.pr_vaddr;
mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
for (i = 0; i < fptr->file_nsaddrs; i += 2) {
/* Does this section overlap the mapping? */
sstart = fptr->file_saddrs[i];
send = fptr->file_saddrs[i + 1];
if (!(mend <= sstart || mstart >= send))
return (1);
}
return (0);
}
/*
* Find or build the symbol table for the given mapping.
*/
static file_info_t *
build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
{
prmap_t *pmap = &mptr->map_pmap;
file_info_t *fptr;
if ((fptr = mptr->map_file) != NULL) {
Pbuild_file_symtab(P, fptr);
return (fptr);
}
if (pmap->pr_mapname[0] == '\0')
return (NULL);
/*