1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2016 Joyent, Inc.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 */
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <fcntl.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <memory.h>
38 #include <errno.h>
39 #include <dirent.h>
40 #include <signal.h>
41 #include <limits.h>
42 #include <libgen.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/sysmacros.h>
46 #include <sys/crc32.h>
47
48 #include "libproc.h"
49 #include "Pcontrol.h"
50 #include "Putil.h"
51 #include "Psymtab_machelf.h"
52
53 static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
54 static map_info_t *exec_map(struct ps_prochandle *);
55 static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
56 static map_info_t *object_name_to_map(struct ps_prochandle *,
57 Lmid_t, const char *);
58 static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
59 static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
60 uintptr_t);
61 #ifdef _LP64
62 static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
63 uintptr_t);
64 #endif
65 static uint32_t psym_crc32[] = { CRC32_TABLE };
66
67 #define DATA_TYPES \
68 ((1 << STT_OBJECT) | (1 << STT_FUNC) | \
69 (1 << STT_COMMON) | (1 << STT_TLS))
70 #define IS_DATA_TYPE(tp) (((1 << (tp)) & DATA_TYPES) != 0)
71
72 #define MA_RWX (MA_READ | MA_WRITE | MA_EXEC)
73
74 /*
75 * Minimum and maximum length of a build-id that we'll accept. Generally it's a
76 * 20 byte SHA1 and it's expected that the first byte (which is two ascii
77 * characters) indicates a directory and the remaining bytes become the file
78 * name. Therefore, our minimum length is at least 2 bytes (one for the
79 * directory and one for the name) and the max is a bit over the minimum -- 64,
80 * just in case folks do something odd. The string length is three times the max
81 * length. This accounts for the fact that each byte is two characters, a null
82 * terminator, and the directory '/' character.
83 */
84 #define MINBUILDID 2
85 #define MAXBUILDID 64
86 #define BUILDID_STRLEN (3*MAXBUILDID)
87 #define BUILDID_NAME ".note.gnu.build-id"
88 #define DBGLINK_NAME ".gnu_debuglink"
89
90 typedef enum {
91 PRO_NATURAL,
92 PRO_BYADDR,
93 PRO_BYNAME
94 } pr_order_t;
95
96 static int
97 addr_cmp(const void *aa, const void *bb)
98 {
99 uintptr_t a = *((uintptr_t *)aa);
100 uintptr_t b = *((uintptr_t *)bb);
101
102 if (a > b)
103 return (1);
104 if (a < b)
105 return (-1);
106 return (0);
107 }
108
109 /*
110 * This function creates a list of addresses for a load object's sections.
111 * The list is in ascending address order and alternates start address
112 * then end address for each section we're interested in. The function
113 * returns a pointer to the list, which must be freed by the caller.
114 */
115 static uintptr_t *
116 get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n)
117 {
118 uintptr_t a, addr, *addrs, last = 0;
119 uint_t i, naddrs = 0, unordered = 0;
120
121 if (P->status.pr_dmodel == PR_MODEL_ILP32) {
122 Elf32_Ehdr ehdr;
123 Elf32_Phdr phdr;
124 uint_t phnum;
125
126 if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0)
127 return (NULL);
128
129 addrs = malloc(sizeof (uintptr_t) * phnum * 2);
130 a = ehdr_start + ehdr.e_phoff;
131 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
132 if (Pread(P, &phdr, sizeof (phdr), a) !=
133 sizeof (phdr)) {
134 free(addrs);
135 return (NULL);
136 }
137 if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
138 continue;
139
140 addr = phdr.p_vaddr;
141 if (ehdr.e_type == ET_DYN)
142 addr += ehdr_start;
143 if (last > addr)
144 unordered = 1;
145 addrs[naddrs++] = addr;
146 addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
147 }
148 #ifdef _LP64
149 } else {
150 Elf64_Ehdr ehdr;
151 Elf64_Phdr phdr;
152 uint_t phnum;
153
154 if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0)
155 return (NULL);
156
157 addrs = malloc(sizeof (uintptr_t) * phnum * 2);
158 a = ehdr_start + ehdr.e_phoff;
159 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
160 if (Pread(P, &phdr, sizeof (phdr), a) !=
161 sizeof (phdr)) {
162 free(addrs);
163 return (NULL);
164 }
165 if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
166 continue;
167
168 addr = phdr.p_vaddr;
169 if (ehdr.e_type == ET_DYN)
170 addr += ehdr_start;
171 if (last > addr)
172 unordered = 1;
173 addrs[naddrs++] = addr;
174 addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
175 }
176 #endif
177 }
178
179 if (unordered)
180 qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
181
182 *n = naddrs;
183 return (addrs);
184 }
185
186 /*
187 * Allocation function for a new file_info_t
188 */
189 file_info_t *
190 file_info_new(struct ps_prochandle *P, map_info_t *mptr)
191 {
192 file_info_t *fptr;
193 map_info_t *mp;
194 uintptr_t mstart, mend, sstart, send;
195 uint_t i;
196
197 if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
198 return (NULL);
199
200 list_link(fptr, &P->file_head);
201 (void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
202 mptr->map_file = fptr;
203 fptr->file_ref = 1;
204 fptr->file_fd = -1;
205 fptr->file_dbgfile = -1;
206 P->num_files++;
207
208 /*
209 * To figure out which map_info_t instances correspond to the mappings
210 * for this load object we try to obtain the start and end address
211 * for each section of our in-memory ELF image. If successful, we
212 * walk down the list of addresses and the list of map_info_t
213 * instances in lock step to correctly find the mappings that
214 * correspond to this load object.
215 */
216 if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr,
217 &fptr->file_nsaddrs)) == NULL)
218 return (fptr);
219
220 mp = P->mappings;
221 i = 0;
222 while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) {
223
224 /* Calculate the start and end of the mapping and section */
225 mstart = mp->map_pmap.pr_vaddr;
226 mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size;
227 sstart = fptr->file_saddrs[i];
228 send = fptr->file_saddrs[i + 1];
229
230 if (mend <= sstart) {
231 /* This mapping is below the current section */
232 mp++;
233 } else if (mstart >= send) {
234 /* This mapping is above the current section */
235 i += 2;
236 } else {
237 /* This mapping overlaps the current section */
238 if (mp->map_file == NULL) {
239 dprintf("file_info_new: associating "
240 "segment at %p\n",
241 (void *)mp->map_pmap.pr_vaddr);
242 mp->map_file = fptr;
243 fptr->file_ref++;
244 } else {
245 dprintf("file_info_new: segment at %p "
246 "already associated with %s\n",
247 (void *)mp->map_pmap.pr_vaddr,
248 (mp == mptr ? "this file" :
249 mp->map_file->file_pname));
250 }
251 mp++;
252 }
253 }
254
255 return (fptr);
256 }
257
258 /*
259 * Deallocation function for a file_info_t
260 */
261 static void
262 file_info_free(struct ps_prochandle *P, file_info_t *fptr)
263 {
264 if (--fptr->file_ref == 0) {
265 list_unlink(fptr);
266 if (fptr->file_symtab.sym_elf) {
267 (void) elf_end(fptr->file_symtab.sym_elf);
268 free(fptr->file_symtab.sym_elfmem);
269 }
270 if (fptr->file_symtab.sym_byname)
271 free(fptr->file_symtab.sym_byname);
272 if (fptr->file_symtab.sym_byaddr)
273 free(fptr->file_symtab.sym_byaddr);
274
275 if (fptr->file_dynsym.sym_elf) {
276 (void) elf_end(fptr->file_dynsym.sym_elf);
277 free(fptr->file_dynsym.sym_elfmem);
278 }
279 if (fptr->file_dynsym.sym_byname)
280 free(fptr->file_dynsym.sym_byname);
281 if (fptr->file_dynsym.sym_byaddr)
282 free(fptr->file_dynsym.sym_byaddr);
283
284 if (fptr->file_lo)
285 free(fptr->file_lo);
286 if (fptr->file_lname)
287 free(fptr->file_lname);
288 if (fptr->file_rname)
289 free(fptr->file_rname);
290 if (fptr->file_elf)
291 (void) elf_end(fptr->file_elf);
292 if (fptr->file_elfmem != NULL)
293 free(fptr->file_elfmem);
294 if (fptr->file_fd >= 0)
295 (void) close(fptr->file_fd);
296 if (fptr->file_dbgelf)
297 (void) elf_end(fptr->file_dbgelf);
298 if (fptr->file_dbgfile >= 0)
299 (void) close(fptr->file_dbgfile);
300 if (fptr->file_ctfp) {
301 ctf_close(fptr->file_ctfp);
302 free(fptr->file_ctf_buf);
303 }
304 if (fptr->file_saddrs)
305 free(fptr->file_saddrs);
306 free(fptr);
307 P->num_files--;
308 }
309 }
310
311 /*
312 * Deallocation function for a map_info_t
313 */
314 static void
315 map_info_free(struct ps_prochandle *P, map_info_t *mptr)
316 {
317 file_info_t *fptr;
318
319 if ((fptr = mptr->map_file) != NULL) {
320 if (fptr->file_map == mptr)
321 fptr->file_map = NULL;
322 file_info_free(P, fptr);
323 }
324 if (P->execname && mptr == P->map_exec) {
325 free(P->execname);
326 P->execname = NULL;
327 }
328 if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
329 free(P->auxv);
330 P->auxv = NULL;
331 P->nauxv = 0;
332 }
333 if (mptr == P->map_exec)
334 P->map_exec = NULL;
335 if (mptr == P->map_ldso)
336 P->map_ldso = NULL;
337 }
338
339 /*
340 * Call-back function for librtld_db to iterate through all of its shared
341 * libraries. We use this to get the load object names for the mappings.
342 */
343 static int
344 map_iter(const rd_loadobj_t *lop, void *cd)
345 {
346 char buf[PATH_MAX];
347 struct ps_prochandle *P = cd;
348 map_info_t *mptr;
349 file_info_t *fptr;
350
351 dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
352
353 if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
354 dprintf("map_iter: base address doesn't match any mapping\n");
355 return (1); /* Base address does not match any mapping */
356 }
357
358 if ((fptr = mptr->map_file) == NULL &&
359 (fptr = file_info_new(P, mptr)) == NULL) {
360 dprintf("map_iter: failed to allocate a new file_info_t\n");
361 return (1); /* Failed to allocate a new file_info_t */
362 }
363
364 if ((fptr->file_lo == NULL) &&
365 (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
366 dprintf("map_iter: failed to allocate rd_loadobj_t\n");
367 file_info_free(P, fptr);
368 return (1); /* Failed to allocate rd_loadobj_t */
369 }
370
371 fptr->file_map = mptr;
372 *fptr->file_lo = *lop;
373
374 fptr->file_lo->rl_plt_base = fptr->file_plt_base;
375 fptr->file_lo->rl_plt_size = fptr->file_plt_size;
376
377 if (fptr->file_lname) {
378 free(fptr->file_lname);
379 fptr->file_lname = NULL;
380 fptr->file_lbase = NULL;
381 }
382 if (fptr->file_rname) {
383 free(fptr->file_rname);
384 fptr->file_rname = NULL;
385 fptr->file_rbase = NULL;
386 }
387
388 if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
389 if ((fptr->file_lname = strdup(buf)) != NULL)
390 fptr->file_lbase = basename(fptr->file_lname);
391 } else {
392 dprintf("map_iter: failed to read string at %p\n",
393 (void *)lop->rl_nameaddr);
394 }
395
396 if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
397 ((fptr->file_rname = strdup(buf)) != NULL))
398 fptr->file_rbase = basename(fptr->file_rname);
399
400 dprintf("loaded rd object %s lmid %lx\n",
401 fptr->file_lname ? buf : "<NULL>", lop->rl_lmident);
402 return (1);
403 }
404
405 static void
406 map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
407 {
408 file_info_t *fptr;
409 char buf[PATH_MAX];
410
411 if ((fptr = mptr->map_file) == NULL &&
412 (fptr = file_info_new(P, mptr)) == NULL)
413 return; /* Failed to allocate a new file_info_t */
414
415 fptr->file_map = mptr;
416
417 if ((fptr->file_lo == NULL) &&
418 (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
419 file_info_free(P, fptr);
420 return; /* Failed to allocate rd_loadobj_t */
421 }
422
423 (void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
424 fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
425 fptr->file_lo->rl_bend =
426 mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
427
428 fptr->file_lo->rl_plt_base = fptr->file_plt_base;
429 fptr->file_lo->rl_plt_size = fptr->file_plt_size;
430
431 if ((fptr->file_lname == NULL) &&
432 (fptr->file_lname = strdup(lname)) != NULL)
433 fptr->file_lbase = basename(fptr->file_lname);
434
435 if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
436 ((fptr->file_rname = strdup(buf)) != NULL))
437 fptr->file_rbase = basename(fptr->file_rname);
438 }
439
440 static void
441 load_static_maps(struct ps_prochandle *P)
442 {
443 map_info_t *mptr;
444
445 /*
446 * Construct the map for the a.out.
447 */
448 if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
449 map_set(P, mptr, "a.out");
450
451 /*
452 * If the dynamic linker exists for this process,
453 * construct the map for it.
454 */
455 if (Pgetauxval(P, AT_BASE) != -1L &&
456 (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
457 map_set(P, mptr, "ld.so.1");
458 }
459
460 int
461 Preadmaps(struct ps_prochandle *P, prmap_t **Pmapp, ssize_t *nmapp)
462 {
463 return (P->ops.pop_read_maps(P, Pmapp, nmapp, P->data));
464 }
465
466 /*
467 * Go through all the address space mappings, validating or updating
468 * the information already gathered, or gathering new information.
469 *
470 * This function is only called when we suspect that the mappings have changed
471 * because this is the first time we're calling it or because of rtld activity.
472 */
473 void
474 Pupdate_maps(struct ps_prochandle *P)
475 {
476 prmap_t *Pmap = NULL;
477 prmap_t *pmap;
478 ssize_t nmap;
479 int i;
480 uint_t oldmapcount;
481 map_info_t *newmap, *newp;
482 map_info_t *mptr;
483
484 if (P->info_valid || P->state == PS_UNDEAD)
485 return;
486
487 Preadauxvec(P);
488
489 if (Preadmaps(P, &Pmap, &nmap) != 0)
490 return;
491
492 if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
493 return;
494
495 /*
496 * We try to merge any file information we may have for existing
497 * mappings, to avoid having to rebuild the file info.
498 */
499 mptr = P->mappings;
500 pmap = Pmap;
501 newp = newmap;
502 oldmapcount = P->map_count;
503 for (i = 0; i < nmap; i++, pmap++, newp++) {
504
505 if (oldmapcount == 0) {
506 /*
507 * We've exhausted all the old mappings. Every new
508 * mapping should be added.
509 */
510 newp->map_pmap = *pmap;
511
512 } else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
513 pmap->pr_size == mptr->map_pmap.pr_size &&
514 pmap->pr_offset == mptr->map_pmap.pr_offset &&
515 (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
516 (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
517 pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
518 pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
519 strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
520
521 /*
522 * This mapping matches exactly. Copy over the old
523 * mapping, taking care to get the latest flags.
524 * Make sure the associated file_info_t is updated
525 * appropriately.
526 */
527 *newp = *mptr;
528 if (P->map_exec == mptr)
529 P->map_exec = newp;
530 if (P->map_ldso == mptr)
531 P->map_ldso = newp;
532 newp->map_pmap.pr_mflags = pmap->pr_mflags;
533 if (mptr->map_file != NULL &&
534 mptr->map_file->file_map == mptr)
535 mptr->map_file->file_map = newp;
536 oldmapcount--;
537 mptr++;
538
539 } else if (pmap->pr_vaddr + pmap->pr_size >
540 mptr->map_pmap.pr_vaddr) {
541
542 /*
543 * The old mapping doesn't exist any more, remove it
544 * from the list.
545 */
546 map_info_free(P, mptr);
547 oldmapcount--;
548 i--;
549 newp--;
550 pmap--;
551 mptr++;
552
553 } else {
554
555 /*
556 * This is a new mapping, add it directly.
557 */
558 newp->map_pmap = *pmap;
559 }
560 }
561
562 /*
563 * Free any old maps
564 */
565 while (oldmapcount) {
566 map_info_free(P, mptr);
567 oldmapcount--;
568 mptr++;
569 }
570
571 free(Pmap);
572 if (P->mappings != NULL)
573 free(P->mappings);
574 P->mappings = newmap;
575 P->map_count = P->map_alloc = nmap;
576 P->info_valid = 1;
577
578 /*
579 * Consult librtld_db to get the load object
580 * names for all of the shared libraries.
581 */
582 if (P->rap != NULL)
583 (void) rd_loadobj_iter(P->rap, map_iter, P);
584 }
585
586 /*
587 * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
588 * forcibly cache all of the symbol tables associated with all object files.
589 */
590 void
591 Pupdate_syms(struct ps_prochandle *P)
592 {
593 file_info_t *fptr;
594 int i;
595
596 Pupdate_maps(P);
597
598 for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
599 i++, fptr = list_next(fptr)) {
600 Pbuild_file_symtab(P, fptr);
601 (void) Pbuild_file_ctf(P, fptr);
602 }
603 }
604
605 /*
606 * Return the librtld_db agent handle for the victim process.
607 * The handle will become invalid at the next successful exec() and the
608 * client (caller of proc_rd_agent()) must not use it beyond that point.
609 * If the process is already dead, we've already tried our best to
610 * create the agent during core file initialization.
611 */
612 rd_agent_t *
613 Prd_agent(struct ps_prochandle *P)
614 {
615 if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
616 Pupdate_maps(P);
617 if (P->num_files == 0)
618 load_static_maps(P);
619 rd_log(_libproc_debug);
620 if ((P->rap = rd_new(P)) != NULL)
621 (void) rd_loadobj_iter(P->rap, map_iter, P);
622 }
623 return (P->rap);
624 }
625
626 /*
627 * Return the prmap_t structure containing 'addr', but only if it
628 * is in the dynamic linker's link map and is the text section.
629 */
630 const prmap_t *
631 Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
632 {
633 map_info_t *mptr;
634
635 if (!P->info_valid)
636 Pupdate_maps(P);
637
638 if ((mptr = Paddr2mptr(P, addr)) != NULL) {
639 file_info_t *fptr = build_map_symtab(P, mptr);
640 const prmap_t *pmp = &mptr->map_pmap;
641
642 /*
643 * Assume that if rl_data_base is NULL, it means that no
644 * data section was found for this load object, and that
645 * a section must be text. Otherwise, a section will be
646 * text unless it ends above the start of the data
647 * section.
648 */
649 if (fptr != NULL && fptr->file_lo != NULL &&
650 (fptr->file_lo->rl_data_base == NULL ||
651 pmp->pr_vaddr + pmp->pr_size <=
652 fptr->file_lo->rl_data_base))
653 return (pmp);
654 }
655
656 return (NULL);
657 }
658
659 /*
660 * Return the prmap_t structure containing 'addr' (no restrictions on
661 * the type of mapping).
662 */
663 const prmap_t *
664 Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
665 {
666 map_info_t *mptr;
667
668 if (!P->info_valid)
669 Pupdate_maps(P);
670
671 if ((mptr = Paddr2mptr(P, addr)) != NULL)
672 return (&mptr->map_pmap);
673
674 return (NULL);
675 }
676
677 /*
678 * Convert a full or partial load object name to the prmap_t for its
679 * corresponding primary text mapping.
680 */
681 const prmap_t *
682 Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
683 {
684 map_info_t *mptr;
685
686 if (name == PR_OBJ_EVERY)
687 return (NULL); /* A reasonable mistake */
688
689 if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
690 return (&mptr->map_pmap);
691
692 return (NULL);
693 }
694
695 const prmap_t *
696 Pname_to_map(struct ps_prochandle *P, const char *name)
697 {
698 return (Plmid_to_map(P, PR_LMID_EVERY, name));
699 }
700
701 const rd_loadobj_t *
702 Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
703 {
704 map_info_t *mptr;
705
706 if (!P->info_valid)
707 Pupdate_maps(P);
708
709 if ((mptr = Paddr2mptr(P, addr)) == NULL)
710 return (NULL);
711
712 /*
713 * By building the symbol table, we implicitly bring the PLT
714 * information up to date in the load object.
715 */
716 (void) build_map_symtab(P, mptr);
717
718 return (mptr->map_file->file_lo);
719 }
720
721 const rd_loadobj_t *
722 Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
723 {
724 map_info_t *mptr;
725
726 if (name == PR_OBJ_EVERY)
727 return (NULL);
728
729 if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
730 return (NULL);
731
732 /*
733 * By building the symbol table, we implicitly bring the PLT
734 * information up to date in the load object.
735 */
736 (void) build_map_symtab(P, mptr);
737
738 return (mptr->map_file->file_lo);
739 }
740
741 const rd_loadobj_t *
742 Pname_to_loadobj(struct ps_prochandle *P, const char *name)
743 {
744 return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
745 }
746
747 /*
748 * We've been given a file_info_t which doesn't have any CTF. However, it may
749 * have information that's in a format that we could convert if on the fly.
750 * We'll first try to convert the alternate debug file, if present, and then
751 * move onto the default file. The reason we prefer the alternate debug file is
752 * that if both exist, then it likely has any usable debugging information.
753 */
754 static ctf_file_t *
755 Pconvert_file_ctf(file_info_t *fptr)
756 {
757 int err;
758 ctf_file_t *fp;
759 char errmsg[1024];
760
761 /*
762 * Provide an opt in.
763 */
764 if (getenv("LIBPROC_CTFCONVERT") == NULL)
765 return (NULL);
766
767 /*
768 * If we've already attempted to call this, then that's it. No reason to
769 * pretend we'll be more successful again another time.
770 */
771 if (fptr->file_cvt == B_TRUE)
772 return (NULL);
773 fptr->file_cvt = B_TRUE;
774
775 fp = NULL;
776 if (fptr->file_dbgelf != NULL) {
777 fp = ctf_elfconvert(fptr->file_fd, fptr->file_dbgelf, NULL, 1,
778 0, &err, errmsg, sizeof (errmsg));
779 if (fp == NULL) {
780 dprintf("failed to convert %s: %s\n", fptr->file_pname,
781 err == ECTF_CONVBKERR ? errmsg : ctf_errmsg(err));
782 }
783 }
784 if (fp == NULL) {
785 fp = ctf_elfconvert(fptr->file_fd, fptr->file_elf, NULL, 1,
786 0, &err, errmsg, sizeof (errmsg));
787 if (fp == NULL) {
788 dprintf("failed to convert %s: %s\n", fptr->file_pname,
789 err == ECTF_CONVBKERR ? errmsg : ctf_errmsg(err));
790 }
791 }
792 if (fp != NULL) {
793 fptr->file_ctfp = fp;
794 }
795
796 return (NULL);
797 }
798
799 ctf_file_t *
800 Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
801 {
802 ctf_sect_t ctdata, symtab, strtab;
803 sym_tbl_t *symp;
804 int err;
805
806 if (fptr->file_ctfp != NULL)
807 return (fptr->file_ctfp);
808
809 Pbuild_file_symtab(P, fptr);
810
811 if (fptr->file_ctf_size == 0) {
812 return (Pconvert_file_ctf(fptr));
813 }
814
815 symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
816 if (symp->sym_data_pri == NULL)
817 return (NULL);
818
819 /*
820 * The buffer may alread be allocated if this is a core file that
821 * contained CTF data for this file.
822 */
823 if (fptr->file_ctf_buf == NULL) {
824 fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
825 if (fptr->file_ctf_buf == NULL) {
826 dprintf("failed to allocate ctf buffer\n");
827 return (NULL);
828 }
829
830 if (pread(fptr->file_fd, fptr->file_ctf_buf,
831 fptr->file_ctf_size, fptr->file_ctf_off) !=
832 fptr->file_ctf_size) {
833 free(fptr->file_ctf_buf);
834 fptr->file_ctf_buf = NULL;
835 dprintf("failed to read ctf data\n");
836 return (NULL);
837 }
838 }
839
840 ctdata.cts_name = ".SUNW_ctf";
841 ctdata.cts_type = SHT_PROGBITS;
842 ctdata.cts_flags = 0;
843 ctdata.cts_data = fptr->file_ctf_buf;
844 ctdata.cts_size = fptr->file_ctf_size;
845 ctdata.cts_entsize = 1;
846 ctdata.cts_offset = 0;
847
848 symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
849 symtab.cts_type = symp->sym_hdr_pri.sh_type;
850 symtab.cts_flags = symp->sym_hdr_pri.sh_flags;
851 symtab.cts_data = symp->sym_data_pri->d_buf;
852 symtab.cts_size = symp->sym_hdr_pri.sh_size;
853 symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize;
854 symtab.cts_offset = symp->sym_hdr_pri.sh_offset;
855
856 strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
857 strtab.cts_type = symp->sym_strhdr.sh_type;
858 strtab.cts_flags = symp->sym_strhdr.sh_flags;
859 strtab.cts_data = symp->sym_strs;
860 strtab.cts_size = symp->sym_strhdr.sh_size;
861 strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
862 strtab.cts_offset = symp->sym_strhdr.sh_offset;
863
864 fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
865 if (fptr->file_ctfp == NULL) {
866 dprintf("ctf_bufopen() failed, error code %d\n", err);
867 free(fptr->file_ctf_buf);
868 fptr->file_ctf_buf = NULL;
869 return (NULL);
870 }
871
872 dprintf("loaded %lu bytes of CTF data for %s\n",
873 (ulong_t)fptr->file_ctf_size, fptr->file_pname);
874
875 return (fptr->file_ctfp);
876 }
877
878 ctf_file_t *
879 Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
880 {
881 map_info_t *mptr;
882 file_info_t *fptr;
883
884 if (!P->info_valid)
885 Pupdate_maps(P);
886
887 if ((mptr = Paddr2mptr(P, addr)) == NULL ||
888 (fptr = mptr->map_file) == NULL)
889 return (NULL);
890
891 return (Pbuild_file_ctf(P, fptr));
892 }
893
894 ctf_file_t *
895 Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
896 {
897 map_info_t *mptr;
898 file_info_t *fptr = NULL;
899
900 if (name == PR_OBJ_EVERY)
901 return (NULL);
902
903 /*
904 * While most idle files are all ELF objects, not all of them have
905 * mapping information available. There's nothing which would make
906 * sense to fake up for ET_REL. Instead, if we're being asked for their
907 * executable object and we know that the information is valid and they
908 * only have a single file, we jump straight to that file pointer.
909 */
910 if (P->state == PS_IDLE && name == PR_OBJ_EXEC && P->info_valid == 1 &&
911 P->num_files == 1 && P->mappings == NULL) {
912 fptr = list_next(&P->file_head);
913 }
914
915 if (fptr == NULL) {
916 if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
917 (fptr = mptr->map_file) == NULL)
918 return (NULL);
919 }
920
921 return (Pbuild_file_ctf(P, fptr));
922 }
923
924 ctf_file_t *
925 Pname_to_ctf(struct ps_prochandle *P, const char *name)
926 {
927 return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
928 }
929
930 void
931 Preadauxvec(struct ps_prochandle *P)
932 {
933 if (P->auxv != NULL) {
934 free(P->auxv);
935 P->auxv = NULL;
936 P->nauxv = 0;
937 }
938
939 P->ops.pop_read_aux(P, &P->auxv, &P->nauxv, P->data);
940 }
941
942 /*
943 * Return a requested element from the process's aux vector.
944 * Return -1 on failure (this is adequate for our purposes).
945 */
946 long
947 Pgetauxval(struct ps_prochandle *P, int type)
948 {
949 auxv_t *auxv;
950
951 if (P->auxv == NULL)
952 Preadauxvec(P);
953
954 if (P->auxv == NULL)
955 return (-1);
956
957 for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
958 if (auxv->a_type == type)
959 return (auxv->a_un.a_val);
960 }
961
962 return (-1);
963 }
964
965 /*
966 * Return a pointer to our internal copy of the process's aux vector.
967 * The caller should not hold on to this pointer across any libproc calls.
968 */
969 const auxv_t *
970 Pgetauxvec(struct ps_prochandle *P)
971 {
972 static const auxv_t empty = { AT_NULL, 0L };
973
974 if (P->auxv == NULL)
975 Preadauxvec(P);
976
977 if (P->auxv == NULL)
978 return (&empty);
979
980 return (P->auxv);
981 }
982
983 /*
984 * Return 1 if the given mapping corresponds to the given file_info_t's
985 * load object; return 0 otherwise.
986 */
987 static int
988 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr)
989 {
990 prmap_t *pmap = &mptr->map_pmap;
991 rd_loadobj_t *lop = fptr->file_lo;
992 uint_t i;
993 uintptr_t mstart, mend, sstart, send;
994
995 /*
996 * We can get for free the start address of the text and data
997 * sections of the load object. Start by seeing if the mapping
998 * encloses either of these.
999 */
1000 if ((pmap->pr_vaddr <= lop->rl_base &&
1001 lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
1002 (pmap->pr_vaddr <= lop->rl_data_base &&
1003 lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))
1004 return (1);
1005
1006 /*
1007 * It's still possible that this mapping correponds to the load
1008 * object. Consider the example of a mapping whose start and end
1009 * addresses correspond to those of the load object's text section.
1010 * If the mapping splits, e.g. as a result of a segment demotion,
1011 * then although both mappings are still backed by the same section,
1012 * only one will be seen to enclose that section's start address.
1013 * Thus, to be rigorous, we ask not whether this mapping encloses
1014 * the start of a section, but whether there exists a section that
1015 * overlaps this mapping.
1016 *
1017 * If we don't already have the section addresses, and we successfully
1018 * get them, then we cache them in case we come here again.
1019 */
1020 if (fptr->file_saddrs == NULL &&
1021 (fptr->file_saddrs = get_saddrs(P,
1022 fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL)
1023 return (0);
1024
1025 mstart = mptr->map_pmap.pr_vaddr;
1026 mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
1027 for (i = 0; i < fptr->file_nsaddrs; i += 2) {
1028 /* Does this section overlap the mapping? */
1029 sstart = fptr->file_saddrs[i];
1030 send = fptr->file_saddrs[i + 1];
1031 if (!(mend <= sstart || mstart >= send))
1032 return (1);
1033 }
1034
1035 return (0);
1036 }
1037
1038 /*
1039 * Find or build the symbol table for the given mapping.
1040 */
1041 static file_info_t *
1042 build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
1043 {
1044 prmap_t *pmap = &mptr->map_pmap;
1045 file_info_t *fptr;
1046 uint_t i;
1047
1048 if ((fptr = mptr->map_file) != NULL) {
1049 Pbuild_file_symtab(P, fptr);
1050 return (fptr);
1051 }
1052
1053 if (pmap->pr_mapname[0] == '\0')
1054 return (NULL);
1055
1056 /*
1057 * Attempt to find a matching file.
1058 * (A file can be mapped at several different addresses.)
1059 */
1060 for (i = 0, fptr = list_next(&P->file_head); i < P->num_files;
1061 i++, fptr = list_next(fptr)) {
1062 if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 &&
1063 fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) {
1064 mptr->map_file = fptr;
1065 fptr->file_ref++;
1066 Pbuild_file_symtab(P, fptr);
1067 return (fptr);
1068 }
1069 }
1070
1071 /*
1072 * If we need to create a new file_info structure, iterate
1073 * through the load objects in order to attempt to connect
1074 * this new file with its primary text mapping. We again
1075 * need to handle ld.so as a special case because we need
1076 * to be able to bootstrap librtld_db.
1077 */
1078 if ((fptr = file_info_new(P, mptr)) == NULL)
1079 return (NULL);
1080
1081 if (P->map_ldso != mptr) {
1082 if (P->rap != NULL)
1083 (void) rd_loadobj_iter(P->rap, map_iter, P);
1084 else
1085 (void) Prd_agent(P);
1086 } else {
1087 fptr->file_map = mptr;
1088 }
1089
1090 /*
1091 * If librtld_db wasn't able to help us connect the file to a primary
1092 * text mapping, set file_map to the current mapping because we require
1093 * fptr->file_map to be set in Pbuild_file_symtab. librtld_db may be
1094 * unaware of what's going on in the rare case that a legitimate ELF
1095 * file has been mmap(2)ed into the process address space *without*
1096 * the use of dlopen(3x).
1097 */
1098 if (fptr->file_map == NULL)
1099 fptr->file_map = mptr;
1100
1101 Pbuild_file_symtab(P, fptr);
1102
1103 return (fptr);
1104 }
1105
1106 static int
1107 read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum,
1108 uintptr_t addr)
1109 {
1110 if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr))
1111 return (-1);
1112
1113 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1114 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1115 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1116 ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
1117 ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
1118 #ifdef _BIG_ENDIAN
1119 ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
1120 #else
1121 ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
1122 #endif
1123 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1124 return (-1);
1125
1126 if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
1127 Elf32_Shdr shdr0;
1128
1129 if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
1130 Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
1131 sizeof (shdr0))
1132 return (-1);
1133
1134 if (shdr0.sh_info != 0)
1135 *phnum = shdr0.sh_info;
1136 }
1137
1138 return (0);
1139 }
1140
1141 static int
1142 read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr,
1143 uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr)
1144 {
1145 uint_t i;
1146
1147 for (i = 0; i < phnum; i++) {
1148 uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
1149 if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
1150 return (-1);
1151
1152 if (phdr->p_type == PT_DYNAMIC)
1153 return (0);
1154 }
1155
1156 return (-1);
1157 }
1158
1159 #ifdef _LP64
1160 static int
1161 read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum,
1162 uintptr_t addr)
1163 {
1164 if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr))
1165 return (-1);
1166
1167 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1168 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1169 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1170 ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
1171 ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
1172 #ifdef _BIG_ENDIAN
1173 ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
1174 #else
1175 ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
1176 #endif
1177 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1178 return (-1);
1179
1180 if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
1181 Elf64_Shdr shdr0;
1182
1183 if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
1184 Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
1185 sizeof (shdr0))
1186 return (-1);
1187
1188 if (shdr0.sh_info != 0)
1189 *phnum = shdr0.sh_info;
1190 }
1191
1192 return (0);
1193 }
1194
1195 static int
1196 read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr,
1197 uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr)
1198 {
1199 uint_t i;
1200
1201 for (i = 0; i < phnum; i++) {
1202 uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
1203 if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
1204 return (-1);
1205
1206 if (phdr->p_type == PT_DYNAMIC)
1207 return (0);
1208 }
1209
1210 return (-1);
1211 }
1212 #endif /* _LP64 */
1213
1214 /*
1215 * The text segment for each load object contains the elf header and
1216 * program headers. We can use this information to determine if the
1217 * file that corresponds to the load object is the same file that
1218 * was loaded into the process's address space. There can be a discrepency
1219 * if a file is recompiled after the process is started or if the target
1220 * represents a core file from a differently configured system -- two
1221 * common examples. The DT_CHECKSUM entry in the dynamic section
1222 * provides an easy method of comparison. It is important to note that
1223 * the dynamic section usually lives in the data segment, but the meta
1224 * data we use to find the dynamic section lives in the text segment so
1225 * if either of those segments is absent we can't proceed.
1226 *
1227 * We're looking through the elf file for several items: the symbol tables
1228 * (both dynsym and symtab), the procedure linkage table (PLT) base,
1229 * size, and relocation base, and the CTF information. Most of this can
1230 * be recovered from the loaded image of the file itself, the exceptions
1231 * being the symtab and CTF data.
1232 *
1233 * First we try to open the file that we think corresponds to the load
1234 * object, if the DT_CHECKSUM values match, we're all set, and can simply
1235 * recover all the information we need from the file. If the values of
1236 * DT_CHECKSUM don't match, or if we can't access the file for whatever
1237 * reasaon, we fake up a elf file to use in its stead. If we can't read
1238 * the elf data in the process's address space, we fall back to using
1239 * the file even though it may give inaccurate information.
1240 *
1241 * The elf file that we fake up has to consist of sections for the
1242 * dynsym, the PLT and the dynamic section. Note that in the case of a
1243 * core file, we'll get the CTF data in the file_info_t later on from
1244 * a section embedded the core file (if it's present).
1245 *
1246 * file_differs() conservatively looks for mismatched files, identifying
1247 * a match when there is any ambiguity (since that's the legacy behavior).
1248 */
1249 static int
1250 file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr)
1251 {
1252 Elf_Scn *scn;
1253 GElf_Shdr shdr;
1254 GElf_Dyn dyn;
1255 Elf_Data *data;
1256 uint_t i, ndyn;
1257 GElf_Xword cksum;
1258 uintptr_t addr;
1259
1260 if (fptr->file_map == NULL)
1261 return (0);
1262
1263 if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
1264 (CC_CONTENT_TEXT | CC_CONTENT_DATA))
1265 return (0);
1266
1267 /*
1268 * First, we find the checksum value in the elf file.
1269 */
1270 scn = NULL;
1271 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1272 if (gelf_getshdr(scn, &shdr) != NULL &&
1273 shdr.sh_type == SHT_DYNAMIC)
1274 goto found_shdr;
1275 }
1276 return (0);
1277
1278 found_shdr:
1279 if ((data = elf_getdata(scn, NULL)) == NULL)
1280 return (0);
1281
1282 if (P->status.pr_dmodel == PR_MODEL_ILP32)
1283 ndyn = shdr.sh_size / sizeof (Elf32_Dyn);
1284 #ifdef _LP64
1285 else if (P->status.pr_dmodel == PR_MODEL_LP64)
1286 ndyn = shdr.sh_size / sizeof (Elf64_Dyn);
1287 #endif
1288 else
1289 return (0);
1290
1291 for (i = 0; i < ndyn; i++) {
1292 if (gelf_getdyn(data, i, &dyn) != NULL &&
1293 dyn.d_tag == DT_CHECKSUM)
1294 goto found_cksum;
1295 }
1296
1297 /*
1298 * The in-memory ELF has no DT_CHECKSUM section, but we will report it
1299 * as matching the file anyhow.
1300 */
1301 return (0);
1302
1303 found_cksum:
1304 cksum = dyn.d_un.d_val;
1305 dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum);
1306
1307 /*
1308 * Get the base of the text mapping that corresponds to this file.
1309 */
1310 addr = fptr->file_map->map_pmap.pr_vaddr;
1311
1312 if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1313 Elf32_Ehdr ehdr;
1314 Elf32_Phdr phdr;
1315 Elf32_Dyn dync, *dynp;
1316 uint_t phnum, i;
1317
1318 if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 ||
1319 read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
1320 return (0);
1321
1322 if (ehdr.e_type == ET_DYN)
1323 phdr.p_vaddr += addr;
1324 if ((dynp = malloc(phdr.p_filesz)) == NULL)
1325 return (0);
1326 dync.d_tag = DT_NULL;
1327 if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
1328 phdr.p_filesz) {
1329 free(dynp);
1330 return (0);
1331 }
1332
1333 for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
1334 if (dynp[i].d_tag == DT_CHECKSUM)
1335 dync = dynp[i];
1336 }
1337
1338 free(dynp);
1339
1340 if (dync.d_tag != DT_CHECKSUM)
1341 return (0);
1342
1343 dprintf("image cksum value is %llx\n",
1344 (u_longlong_t)dync.d_un.d_val);
1345 return (dync.d_un.d_val != cksum);
1346 #ifdef _LP64
1347 } else if (P->status.pr_dmodel == PR_MODEL_LP64) {
1348 Elf64_Ehdr ehdr;
1349 Elf64_Phdr phdr;
1350 Elf64_Dyn dync, *dynp;
1351 uint_t phnum, i;
1352
1353 if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
1354 read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
1355 return (0);
1356
1357 if (ehdr.e_type == ET_DYN)
1358 phdr.p_vaddr += addr;
1359 if ((dynp = malloc(phdr.p_filesz)) == NULL)
1360 return (0);
1361 dync.d_tag = DT_NULL;
1362 if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
1363 phdr.p_filesz) {
1364 free(dynp);
1365 return (0);
1366 }
1367
1368 for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
1369 if (dynp[i].d_tag == DT_CHECKSUM)
1370 dync = dynp[i];
1371 }
1372
1373 free(dynp);
1374
1375 if (dync.d_tag != DT_CHECKSUM)
1376 return (0);
1377
1378 dprintf("image cksum value is %llx\n",
1379 (u_longlong_t)dync.d_un.d_val);
1380 return (dync.d_un.d_val != cksum);
1381 #endif /* _LP64 */
1382 }
1383
1384 return (0);
1385 }
1386
1387 /*
1388 * Read data from the specified process and construct an in memory
1389 * image of an ELF file that represents it well enough to let
1390 * us probe it for information.
1391 */
1392 static Elf *
1393 fake_elf(struct ps_prochandle *P, file_info_t *fptr)
1394 {
1395 Elf *elf;
1396 uintptr_t addr;
1397 uint_t phnum;
1398
1399 if (fptr->file_map == NULL)
1400 return (NULL);
1401
1402 if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
1403 (CC_CONTENT_TEXT | CC_CONTENT_DATA))
1404 return (NULL);
1405
1406 addr = fptr->file_map->map_pmap.pr_vaddr;
1407
1408 if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1409 Elf32_Ehdr ehdr;
1410 Elf32_Phdr phdr;
1411
1412 if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) ||
1413 read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
1414 return (NULL);
1415
1416 elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr);
1417 #ifdef _LP64
1418 } else {
1419 Elf64_Ehdr ehdr;
1420 Elf64_Phdr phdr;
1421
1422 if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
1423 read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
1424 return (NULL);
1425
1426 elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr);
1427 #endif
1428 }
1429
1430 return (elf);
1431 }
1432
1433 /*
1434 * We wouldn't need these if qsort(3C) took an argument for the callback...
1435 */
1436 static mutex_t sort_mtx = DEFAULTMUTEX;
1437 static char *sort_strs;
1438 static GElf_Sym *sort_syms;
1439
1440 int
1441 byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
1442 {
1443 if (a->st_value < b->st_value)
1444 return (-1);
1445 if (a->st_value > b->st_value)
1446 return (1);
1447
1448 /*
1449 * Prefer the function to the non-function.
1450 */
1451 if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
1452 if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
1453 return (-1);
1454 if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
1455 return (1);
1456 }
1457
1458 /*
1459 * Prefer the weak or strong global symbol to the local symbol.
1460 */
1461 if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
1462 if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
1463 return (-1);
1464 if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
1465 return (1);
1466 }
1467
1468 /*
1469 * Prefer the symbol that doesn't begin with a '$' since compilers and
1470 * other symbol generators often use it as a prefix.
1471 */
1472 if (*bname == '$')
1473 return (-1);
1474 if (*aname == '$')
1475 return (1);
1476
1477 /*
1478 * Prefer the name with fewer leading underscores in the name.
1479 */
1480 while (*aname == '_' && *bname == '_') {
1481 aname++;
1482 bname++;
1483 }
1484
1485 if (*bname == '_')
1486 return (-1);
1487 if (*aname == '_')
1488 return (1);
1489
1490 /*
1491 * Prefer the symbol with the smaller size.
1492 */
1493 if (a->st_size < b->st_size)
1494 return (-1);
1495 if (a->st_size > b->st_size)
1496 return (1);
1497
1498 /*
1499 * All other factors being equal, fall back to lexicographic order.
1500 */
1501 return (strcmp(aname, bname));
1502 }
1503
1504 static int
1505 byaddr_cmp(const void *aa, const void *bb)
1506 {
1507 GElf_Sym *a = &sort_syms[*(uint_t *)aa];
1508 GElf_Sym *b = &sort_syms[*(uint_t *)bb];
1509 char *aname = sort_strs + a->st_name;
1510 char *bname = sort_strs + b->st_name;
1511
1512 return (byaddr_cmp_common(a, aname, b, bname));
1513 }
1514
1515 static int
1516 byname_cmp(const void *aa, const void *bb)
1517 {
1518 GElf_Sym *a = &sort_syms[*(uint_t *)aa];
1519 GElf_Sym *b = &sort_syms[*(uint_t *)bb];
1520 char *aname = sort_strs + a->st_name;
1521 char *bname = sort_strs + b->st_name;
1522
1523 return (strcmp(aname, bname));
1524 }
1525
1526 /*
1527 * Given a symbol index, look up the corresponding symbol from the
1528 * given symbol table.
1529 *
1530 * This function allows the caller to treat the symbol table as a single
1531 * logical entity even though there may be 2 actual ELF symbol tables
1532 * involved. See the comments in Pcontrol.h for details.
1533 */
1534 static GElf_Sym *
1535 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst)
1536 {
1537 /* If index is in range of primary symtab, look it up there */
1538 if (ndx >= symtab->sym_symn_aux) {
1539 return (gelf_getsym(symtab->sym_data_pri,
1540 ndx - symtab->sym_symn_aux, dst));
1541 }
1542
1543 /* Not in primary: Look it up in the auxiliary symtab */
1544 return (gelf_getsym(symtab->sym_data_aux, ndx, dst));
1545 }
1546
1547 void
1548 optimize_symtab(sym_tbl_t *symtab)
1549 {
1550 GElf_Sym *symp, *syms;
1551 uint_t i, *indexa, *indexb;
1552 size_t symn, strsz, count;
1553
1554 if (symtab == NULL || symtab->sym_data_pri == NULL ||
1555 symtab->sym_byaddr != NULL)
1556 return;
1557
1558 symn = symtab->sym_symn;
1559 strsz = symtab->sym_strsz;
1560
1561 symp = syms = malloc(sizeof (GElf_Sym) * symn);
1562 if (symp == NULL) {
1563 dprintf("optimize_symtab: failed to malloc symbol array");
1564 return;
1565 }
1566
1567 /*
1568 * First record all the symbols into a table and count up the ones
1569 * that we're interested in. We mark symbols as invalid by setting
1570 * the st_name to an illegal value.
1571 */
1572 for (i = 0, count = 0; i < symn; i++, symp++) {
1573 if (symtab_getsym(symtab, i, symp) != NULL &&
1574 symp->st_name < strsz &&
1575 IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
1576 count++;
1577 else
1578 symp->st_name = strsz;
1579 }
1580
1581 /*
1582 * Allocate sufficient space for both tables and populate them
1583 * with the same symbols we just counted.
1584 */
1585 symtab->sym_count = count;
1586 indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count);
1587 indexb = symtab->sym_byname = calloc(sizeof (uint_t), count);
1588 if (indexa == NULL || indexb == NULL) {
1589 dprintf(
1590 "optimize_symtab: failed to malloc symbol index arrays");
1591 symtab->sym_count = 0;
1592 if (indexa != NULL) { /* First alloc succeeded. Free it */
1593 free(indexa);
1594 symtab->sym_byaddr = NULL;
1595 }
1596 free(syms);
1597 return;
1598 }
1599 for (i = 0, symp = syms; i < symn; i++, symp++) {
1600 if (symp->st_name < strsz)
1601 *indexa++ = *indexb++ = i;
1602 }
1603
1604 /*
1605 * Sort the two tables according to the appropriate criteria,
1606 * unless the user has overridden this behaviour.
1607 *
1608 * An example where we might not sort the tables is the relatively
1609 * unusual case of a process with very large symbol tables in which
1610 * we perform few lookups. In such a case the total time would be
1611 * dominated by the sort. It is difficult to determine a priori
1612 * how many lookups an arbitrary client will perform, and
1613 * hence whether the symbol tables should be sorted. We therefore
1614 * sort the tables by default, but provide the user with a
1615 * "chicken switch" in the form of the LIBPROC_NO_QSORT
1616 * environment variable.
1617 */
1618 if (!_libproc_no_qsort) {
1619 (void) mutex_lock(&sort_mtx);
1620 sort_strs = symtab->sym_strs;
1621 sort_syms = syms;
1622
1623 qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp);
1624 qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp);
1625
1626 sort_strs = NULL;
1627 sort_syms = NULL;
1628 (void) mutex_unlock(&sort_mtx);
1629 }
1630
1631 free(syms);
1632 }
1633
1634
1635 static Elf *
1636 build_fake_elf(struct ps_prochandle *P, file_info_t *fptr, GElf_Ehdr *ehdr,
1637 size_t *nshdrs, Elf_Data **shdata)
1638 {
1639 size_t shstrndx;
1640 Elf_Scn *scn;
1641 Elf *elf;
1642
1643 if ((elf = fake_elf(P, fptr)) == NULL ||
1644 elf_kind(elf) != ELF_K_ELF ||
1645 gelf_getehdr(elf, ehdr) == NULL ||
1646 elf_getshdrnum(elf, nshdrs) == -1 ||
1647 elf_getshdrstrndx(elf, &shstrndx) == -1 ||
1648 (scn = elf_getscn(elf, shstrndx)) == NULL ||
1649 (*shdata = elf_getdata(scn, NULL)) == NULL) {
1650 if (elf != NULL)
1651 (void) elf_end(elf);
1652 dprintf("failed to fake up ELF file\n");
1653 return (NULL);
1654 }
1655
1656 return (elf);
1657 }
1658
1659 /*
1660 * Try and find the file described by path in the file system and validate that
1661 * it matches our CRC before we try and process it for symbol information. If we
1662 * instead have an ELF data section, then that means we're checking a build-id
1663 * section instead. In that case we just need to find and bcmp the corresponding
1664 * section.
1665 *
1666 * Before we validate if it's a valid CRC or data section, we check to ensure
1667 * that it's a normal file and not anything else.
1668 */
1669 static boolean_t
1670 build_alt_debug(file_info_t *fptr, const char *path, uint32_t crc,
1671 Elf_Data *data)
1672 {
1673 int fd;
1674 struct stat st;
1675 Elf *elf;
1676 Elf_Scn *scn;
1677 GElf_Shdr symshdr, strshdr;
1678 Elf_Data *symdata, *strdata;
1679 boolean_t valid;
1680 uint32_t c = -1U;
1681
1682 if ((fd = open(path, O_RDONLY)) < 0)
1683 return (B_FALSE);
1684
1685 if (fstat(fd, &st) != 0) {
1686 (void) close(fd);
1687 return (B_FALSE);
1688 }
1689
1690 if (S_ISREG(st.st_mode) == 0) {
1691 (void) close(fd);
1692 return (B_FALSE);
1693 }
1694
1695 /*
1696 * Only check the CRC if we've come here through a GNU debug link
1697 * section as opposed to the build id. This is indicated by having the
1698 * value of data be NULL.
1699 */
1700 if (data == NULL) {
1701 for (;;) {
1702 char buf[4096];
1703 ssize_t ret = read(fd, buf, sizeof (buf));
1704 if (ret == -1) {
1705 if (ret == EINTR)
1706 continue;
1707 (void) close(fd);
1708 return (B_FALSE);
1709 }
1710 if (ret == 0) {
1711 c = ~c;
1712 if (c != crc) {
1713 dprintf("crc mismatch, found: 0x%x "
1714 "expected 0x%x\n", c, crc);
1715 (void) close(fd);
1716 return (B_FALSE);
1717 }
1718 break;
1719 }
1720 CRC32(c, buf, ret, c, psym_crc32);
1721 }
1722 }
1723
1724 elf = elf_begin(fd, ELF_C_READ, NULL);
1725 if (elf == NULL) {
1726 (void) close(fd);
1727 return (B_FALSE);
1728 }
1729
1730 if (elf_kind(elf) != ELF_K_ELF) {
1731 goto fail;
1732 }
1733
1734 /*
1735 * If we have a data section, that indicates we have a build-id which
1736 * means we need to find the corresponding build-id section and compare
1737 * it.
1738 */
1739 scn = NULL;
1740 valid = B_FALSE;
1741 for (scn = elf_nextscn(elf, scn); data != NULL && scn != NULL;
1742 scn = elf_nextscn(elf, scn)) {
1743 GElf_Shdr hdr;
1744 Elf_Data *ntdata;
1745
1746 if (gelf_getshdr(scn, &hdr) == NULL)
1747 goto fail;
1748
1749 if (hdr.sh_type != SHT_NOTE)
1750 continue;
1751
1752 if ((ntdata = elf_getdata(scn, NULL)) == NULL)
1753 goto fail;
1754
1755 /*
1756 * First verify the data section sizes are equal, then the
1757 * section name. If that's all true, then we can just do a bcmp.
1758 */
1759 if (data->d_size != ntdata->d_size)
1760 continue;
1761
1762 dprintf("found corresponding section in alternate file\n");
1763 if (bcmp(ntdata->d_buf, data->d_buf, data->d_size) != 0)
1764 goto fail;
1765
1766 valid = B_TRUE;
1767 break;
1768 }
1769 if (data != NULL && valid == B_FALSE) {
1770 dprintf("failed to find a matching %s section in %s\n",
1771 BUILDID_NAME, path);
1772 goto fail;
1773 }
1774
1775
1776 /*
1777 * Do two passes, first see if we have a symbol header, then see if we
1778 * can find the corresponding linked string table.
1779 */
1780 scn = NULL;
1781 for (scn = elf_nextscn(elf, scn); scn != NULL;
1782 scn = elf_nextscn(elf, scn)) {
1783
1784 if (gelf_getshdr(scn, &symshdr) == NULL)
1785 goto fail;
1786
1787 if (symshdr.sh_type != SHT_SYMTAB)
1788 continue;
1789
1790 if ((symdata = elf_getdata(scn, NULL)) == NULL)
1791 goto fail;
1792
1793 break;
1794 }
1795 if (scn == NULL)
1796 goto fail;
1797
1798 if ((scn = elf_getscn(elf, symshdr.sh_link)) == NULL)
1799 goto fail;
1800
1801 if (gelf_getshdr(scn, &strshdr) == NULL)
1802 goto fail;
1803
1804 if ((strdata = elf_getdata(scn, NULL)) == NULL)
1805 goto fail;
1806
1807 fptr->file_symtab.sym_data_pri = symdata;
1808 fptr->file_symtab.sym_symn += symshdr.sh_size / symshdr.sh_entsize;
1809 fptr->file_symtab.sym_strs = strdata->d_buf;
1810 fptr->file_symtab.sym_strsz = strdata->d_size;
1811 fptr->file_symtab.sym_hdr_pri = symshdr;
1812 fptr->file_symtab.sym_strhdr = strshdr;
1813
1814 dprintf("successfully loaded additional debug symbols for %s from %s\n",
1815 fptr->file_rname, path);
1816
1817 fptr->file_dbgfile = fd;
1818 fptr->file_dbgelf = elf;
1819 return (B_TRUE);
1820 fail:
1821 (void) elf_end(elf);
1822 (void) close(fd);
1823 return (B_FALSE);
1824 }
1825
1826 /*
1827 * We're here because the object in question has no symbol information, that's a
1828 * bit unfortunate. However, we've found that there's a .gnu_debuglink sitting
1829 * around. By convention that means that given the current location of the
1830 * object on disk, and the debug name that we found in the binary we need to
1831 * search the following locations for a matching file.
1832 *
1833 * <dirname>/.debug/<debug-name>
1834 * /usr/lib/debug/<dirname>/<debug-name>
1835 *
1836 * In the future, we should consider supporting looking in the prefix's
1837 * lib/debug directory for a matching object or supporting an arbitrary user
1838 * defined set of places to look.
1839 */
1840 static void
1841 find_alt_debuglink(file_info_t *fptr, const char *name, uint32_t crc)
1842 {
1843 boolean_t r;
1844 char *dup = NULL, *path = NULL, *dname;
1845
1846 dprintf("find_alt_debug: looking for %s, crc 0x%x\n", name, crc);
1847 if (fptr->file_rname == NULL) {
1848 dprintf("find_alt_debug: encountered null file_rname\n");
1849 return;
1850 }
1851
1852 dup = strdup(fptr->file_rname);
1853 if (dup == NULL)
1854 return;
1855
1856 dname = dirname(dup);
1857 if (asprintf(&path, "%s/.debug/%s", dname, name) != -1) {
1858 dprintf("attempting to load alternate debug information "
1859 "from %s\n", path);
1860 r = build_alt_debug(fptr, path, crc, NULL);
1861 free(path);
1862 if (r == B_TRUE)
1863 goto out;
1864 }
1865
1866 if (asprintf(&path, "/usr/lib/debug/%s/%s", dname, name) != -1) {
1867 dprintf("attempting to load alternate debug information "
1868 "from %s\n", path);
1869 r = build_alt_debug(fptr, path, crc, NULL);
1870 free(path);
1871 if (r == B_TRUE)
1872 goto out;
1873 }
1874 out:
1875 free(dup);
1876 }
1877
1878 /*
1879 * Build the symbol table for the given mapped file.
1880 */
1881 void
1882 Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
1883 {
1884 char objectfile[PATH_MAX];
1885 uint_t i;
1886
1887 GElf_Ehdr ehdr;
1888 GElf_Sym s;
1889
1890 Elf_Data *shdata;
1891 Elf_Scn *scn;
1892 Elf *elf;
1893 size_t nshdrs, shstrndx;
1894
1895 struct {
1896 GElf_Shdr c_shdr;
1897 Elf_Data *c_data;
1898 const char *c_name;
1899 } *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL,
1900 *dbglink = NULL, *buildid = NULL;
1901
1902 if (fptr->file_init)
1903 return; /* We've already processed this file */
1904
1905 /*
1906 * Mark the file_info struct as having the symbol table initialized
1907 * even if we fail below. We tried once; we don't try again.
1908 */
1909 fptr->file_init = 1;
1910
1911 if (elf_version(EV_CURRENT) == EV_NONE) {
1912 dprintf("libproc ELF version is more recent than libelf\n");
1913 return;
1914 }
1915
1916 if (P->state == PS_DEAD || P->state == PS_IDLE) {
1917 char *name;
1918 /*
1919 * If we're a not live, we can't open files from the /proc
1920 * object directory; we have only the mapping and file names
1921 * to guide us. We prefer the file_lname, but need to handle
1922 * the case of it being NULL in order to bootstrap: we first
1923 * come here during rd_new() when the only information we have
1924 * is interpreter name associated with the AT_BASE mapping.
1925 *
1926 * Also, if the zone associated with the core file seems
1927 * to exists on this machine we'll try to open the object
1928 * file within the zone.
1929 */
1930 if (fptr->file_rname != NULL)
1931 name = fptr->file_rname;
1932 else if (fptr->file_lname != NULL)
1933 name = fptr->file_lname;
1934 else
1935 name = fptr->file_pname;
1936 (void) strlcpy(objectfile, name, sizeof (objectfile));
1937 } else {
1938 (void) snprintf(objectfile, sizeof (objectfile),
1939 "%s/%d/object/%s",
1940 procfs_path, (int)P->pid, fptr->file_pname);
1941 }
1942
1943 /*
1944 * Open the object file, create the elf file, and then get the elf
1945 * header and .shstrtab data buffer so we can process sections by
1946 * name. If anything goes wrong try to fake up an elf file from
1947 * the in-core elf image.
1948 */
1949
1950 if (_libproc_incore_elf || (P->flags & INCORE)) {
1951 dprintf("Pbuild_file_symtab: using in-core data for: %s\n",
1952 fptr->file_pname);
1953
1954 if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1955 NULL)
1956 return;
1957
1958 } else if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) {
1959 dprintf("Pbuild_file_symtab: failed to open %s: %s\n",
1960 objectfile, strerror(errno));
1961
1962 if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1963 NULL)
1964 return;
1965
1966 } else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL ||
1967 elf_kind(elf) != ELF_K_ELF ||
1968 gelf_getehdr(elf, &ehdr) == NULL ||
1969 elf_getshdrnum(elf, &nshdrs) == -1 ||
1970 elf_getshdrstrndx(elf, &shstrndx) == -1 ||
1971 (scn = elf_getscn(elf, shstrndx)) == NULL ||
1972 (shdata = elf_getdata(scn, NULL)) == NULL) {
1973 int err = elf_errno();
1974
1975 dprintf("failed to process ELF file %s: %s\n",
1976 objectfile, (err == 0) ? "<null>" : elf_errmsg(err));
1977 (void) elf_end(elf);
1978
1979 if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1980 NULL)
1981 return;
1982
1983 } else if (file_differs(P, elf, fptr)) {
1984 Elf *newelf;
1985
1986 /*
1987 * Before we get too excited about this elf file, we'll check
1988 * its checksum value against the value we have in memory. If
1989 * they don't agree, we try to fake up a new elf file and
1990 * proceed with that instead.
1991 */
1992 dprintf("ELF file %s (%lx) doesn't match in-core image\n",
1993 fptr->file_pname,
1994 (ulong_t)fptr->file_map->map_pmap.pr_vaddr);
1995
1996 if ((newelf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata))
1997 != NULL) {
1998 (void) elf_end(elf);
1999 elf = newelf;
2000 dprintf("switched to faked up ELF file\n");
2001
2002 /*
2003 * Check to see if the file that we just discovered
2004 * to be an imposter matches the execname that was
2005 * determined by Pfindexec(). If it does, we (clearly)
2006 * don't have the right binary, and we zero out
2007 * execname before anyone gets hurt.
2008 */
2009 if (fptr->file_rname != NULL && P->execname != NULL &&
2010 strcmp(fptr->file_rname, P->execname) == 0) {
2011 dprintf("file/in-core image mismatch was "
2012 "on P->execname; discarding\n");
2013 free(P->execname);
2014 P->execname = NULL;
2015 }
2016 }
2017 }
2018
2019 if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) {
2020 dprintf("failed to malloc section cache for %s\n", objectfile);
2021 goto bad;
2022 }
2023
2024 dprintf("processing ELF file %s\n", objectfile);
2025 fptr->file_class = ehdr.e_ident[EI_CLASS];
2026 fptr->file_etype = ehdr.e_type;
2027 fptr->file_elf = elf;
2028 fptr->file_shstrs = shdata->d_buf;
2029 fptr->file_shstrsz = shdata->d_size;
2030
2031 /*
2032 * Iterate through each section, caching its section header, data
2033 * pointer, and name. We use this for handling sh_link values below.
2034 */
2035 for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) {
2036 if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
2037 dprintf("Pbuild_file_symtab: Failed to get section "
2038 "header\n");
2039 goto bad; /* Failed to get section header */
2040 }
2041
2042 if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
2043 dprintf("Pbuild_file_symtab: Failed to get section "
2044 "data\n");
2045 goto bad; /* Failed to get section data */
2046 }
2047
2048 if (cp->c_shdr.sh_name >= shdata->d_size) {
2049 dprintf("Pbuild_file_symtab: corrupt section name");
2050 goto bad; /* Corrupt section name */
2051 }
2052
2053 cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
2054 }
2055
2056 /*
2057 * Now iterate through the section cache in order to locate info
2058 * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt,
2059 * and .SUNW_ctf sections:
2060 */
2061 for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
2062 GElf_Shdr *shp = &cp->c_shdr;
2063
2064 if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
2065 sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
2066 &fptr->file_symtab : &fptr->file_dynsym;
2067 /*
2068 * It's possible that the we already got the symbol
2069 * table from the core file itself. Either the file
2070 * differs in which case our faked up elf file will
2071 * only contain the dynsym (not the symtab) or the
2072 * file matches in which case we'll just be replacing
2073 * the symbol table we pulled out of the core file
2074 * with an equivalent one. In either case, this
2075 * check isn't essential, but it's a good idea.
2076 */
2077 if (symp->sym_data_pri == NULL) {
2078 dprintf("Symbol table found for %s\n",
2079 objectfile);
2080 symp->sym_data_pri = cp->c_data;
2081 symp->sym_symn +=
2082 shp->sh_size / shp->sh_entsize;
2083 symp->sym_strs =
2084 cache[shp->sh_link].c_data->d_buf;
2085 symp->sym_strsz =
2086 cache[shp->sh_link].c_data->d_size;
2087 symp->sym_hdr_pri = cp->c_shdr;
2088 symp->sym_strhdr = cache[shp->sh_link].c_shdr;
2089 } else {
2090 dprintf("Symbol table already there for %s\n",
2091 objectfile);
2092 }
2093 } else if (shp->sh_type == SHT_SUNW_LDYNSYM) {
2094 /* .SUNW_ldynsym section is auxiliary to .dynsym */
2095 if (fptr->file_dynsym.sym_data_aux == NULL) {
2096 dprintf(".SUNW_ldynsym symbol table"
2097 " found for %s\n", objectfile);
2098 fptr->file_dynsym.sym_data_aux = cp->c_data;
2099 fptr->file_dynsym.sym_symn_aux =
2100 shp->sh_size / shp->sh_entsize;
2101 fptr->file_dynsym.sym_symn +=
2102 fptr->file_dynsym.sym_symn_aux;
2103 fptr->file_dynsym.sym_hdr_aux = cp->c_shdr;
2104 } else {
2105 dprintf(".SUNW_ldynsym symbol table already"
2106 " there for %s\n", objectfile);
2107 }
2108 } else if (shp->sh_type == SHT_DYNAMIC) {
2109 dyn = cp;
2110 } else if (strcmp(cp->c_name, ".plt") == 0) {
2111 plt = cp;
2112 } else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) {
2113 /*
2114 * Skip over bogus CTF sections so they don't come back
2115 * to haunt us later.
2116 */
2117 if (shp->sh_link == 0 ||
2118 shp->sh_link >= nshdrs ||
2119 (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM &&
2120 cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) {
2121 dprintf("Bad sh_link %d for "
2122 "CTF\n", shp->sh_link);
2123 continue;
2124 }
2125 ctf = cp;
2126 } else if (strcmp(cp->c_name, BUILDID_NAME) == 0) {
2127 dprintf("Found a %s section for %s\n", BUILDID_NAME,
2128 fptr->file_rname);
2129 /* The ElfXX_Nhdr is 32/64-bit neutral */
2130 if (cp->c_shdr.sh_type == SHT_NOTE &&
2131 cp->c_data->d_buf != NULL &&
2132 cp->c_data->d_size >= sizeof (Elf32_Nhdr)) {
2133 Elf32_Nhdr *hdr = cp->c_data->d_buf;
2134 if (hdr->n_type != 3)
2135 continue;
2136 if (hdr->n_namesz != 4)
2137 continue;
2138 if (hdr->n_descsz < MINBUILDID)
2139 continue;
2140 /* Set a reasonable upper bound */
2141 if (hdr->n_descsz > MAXBUILDID) {
2142 dprintf("Skipped %s as too large "
2143 "(%ld)\n", BUILDID_NAME,
2144 (unsigned long)hdr->n_descsz);
2145 continue;
2146 }
2147
2148 if (cp->c_data->d_size < sizeof (hdr) +
2149 hdr->n_namesz + hdr->n_descsz)
2150 continue;
2151 buildid = cp;
2152 }
2153 } else if (strcmp(cp->c_name, DBGLINK_NAME) == 0) {
2154 dprintf("found %s section for %s\n", DBGLINK_NAME,
2155 fptr->file_rname);
2156 /*
2157 * Let's make sure of a few things before we do this.
2158 */
2159 if (cp->c_shdr.sh_type == SHT_PROGBITS &&
2160 cp->c_data->d_buf != NULL &&
2161 cp->c_data->d_size) {
2162 dbglink = cp;
2163 }
2164 }
2165 }
2166
2167 /*
2168 * If we haven't found any symbol table information and we have found
2169 * either a .note.gnu.build-id or a .gnu_debuglink, it's time to try and
2170 * figure out where we might find this. Originally, GNU used the
2171 * .gnu_debuglink solely, but then they added a .note.gnu.build-id. The
2172 * build-id is some size, usually 16 or 20 bytes, often a SHA1 sum of
2173 * the old, but not present file. All that you have to do to compare
2174 * things is see if the sections are less, in theory saving you from
2175 * doing lots of expensive I/O.
2176 *
2177 * For the .note.gnu.build-id, we're going to check a few things before
2178 * using it, first that the name is 4 bytes, and is GNU and that the
2179 * type is 3, which they say is the build-id identifier.
2180 *
2181 * To verify that the elf data for the .gnu_debuglink seems somewhat
2182 * sane, eg. the elf data should be a string, so we want to verify we
2183 * have a null-terminator.
2184 */
2185 if (fptr->file_symtab.sym_data_pri == NULL && buildid != NULL) {
2186 int i, bo;
2187 uint8_t *dp;
2188 char buf[BUILDID_STRLEN], *path;
2189 Elf32_Nhdr *hdr = buildid->c_data->d_buf;
2190
2191 /*
2192 * This was checked for validity when assigning the buildid
2193 * variable.
2194 */
2195 bzero(buf, sizeof (buf));
2196 dp = (uint8_t *)((uintptr_t)hdr + sizeof (*hdr) +
2197 hdr->n_namesz);
2198 for (i = 0, bo = 0; i < hdr->n_descsz; i++, bo += 2, dp++) {
2199 assert(sizeof (buf) - bo > 0);
2200
2201 /*
2202 * Recall that the build-id is structured as a series of
2203 * bytes. However, the first two characters are supposed
2204 * to represent a directory. Hence, once we reach offset
2205 * two, we insert a '/' character.
2206 */
2207 if (bo == 2) {
2208 buf[bo] = '/';
2209 bo++;
2210 }
2211 (void) snprintf(buf + bo, sizeof (buf) - bo, "%2x",
2212 *dp);
2213 }
2214
2215 if (asprintf(&path, "/usr/lib/debug/.build-id/%s.debug",
2216 buf) != -1) {
2217 boolean_t r;
2218 dprintf("attempting to find build id alternate debug "
2219 "file at %s\n", path);
2220 r = build_alt_debug(fptr, path, 0, buildid->c_data);
2221 dprintf("attempt %s\n", r == B_TRUE ?
2222 "succeeded" : "failed");
2223 free(path);
2224 } else {
2225 dprintf("failed to construct build id path: %s\n",
2226 strerror(errno));
2227 }
2228 }
2229
2230 if (fptr->file_symtab.sym_data_pri == NULL && dbglink != NULL) {
2231 char *c = dbglink->c_data->d_buf;
2232 size_t i;
2233 boolean_t found = B_FALSE;
2234 Elf_Data *ed = dbglink->c_data;
2235 uint32_t crc;
2236
2237 for (i = 0; i < ed->d_size; i++) {
2238 if (c[i] == '\0') {
2239 uintptr_t off;
2240 dprintf("got .gnu_debuglink terminator at "
2241 "offset %lu\n", (unsigned long)i);
2242 /*
2243 * After the null terminator, there should be
2244 * padding, followed by a 4 byte CRC of the
2245 * file. If we don't see this, we're going to
2246 * assume this is bogus.
2247 */
2248 if ((i % sizeof (uint32_t)) == 0) {
2249 i += 4;
2250 } else {
2251 i += sizeof (uint32_t) -
2252 (i % sizeof (uint32_t));
2253 }
2254 if (i + sizeof (uint32_t) ==
2255 dbglink->c_data->d_size) {
2256 found = B_TRUE;
2257 off = (uintptr_t)ed->d_buf + i;
2258 crc = *(uint32_t *)off;
2259 } else {
2260 dprintf(".gnu_debuglink size mismatch, "
2261 "expected: %lu, found: %lu\n",
2262 (unsigned long)i,
2263 (unsigned long)ed->d_size);
2264 }
2265 break;
2266 }
2267 }
2268
2269 if (found == B_TRUE)
2270 find_alt_debuglink(fptr, dbglink->c_data->d_buf, crc);
2271 }
2272
2273 /*
2274 * At this point, we've found all the symbol tables we're ever going
2275 * to find: the ones in the loop above and possibly the symtab that
2276 * was included in the core file. Before we perform any lookups, we
2277 * create sorted versions to optimize for lookups.
2278 */
2279 optimize_symtab(&fptr->file_symtab);
2280 optimize_symtab(&fptr->file_dynsym);
2281
2282 /*
2283 * Fill in the base address of the text mapping for shared libraries.
2284 * This allows us to translate symbols before librtld_db is ready.
2285 */
2286 if (fptr->file_etype == ET_DYN) {
2287 fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr -
2288 fptr->file_map->map_pmap.pr_offset;
2289 dprintf("setting file_dyn_base for %s to %lx\n",
2290 objectfile, (long)fptr->file_dyn_base);
2291 }
2292
2293 /*
2294 * Record the CTF section information in the file info structure.
2295 */
2296 if (ctf != NULL) {
2297 fptr->file_ctf_off = ctf->c_shdr.sh_offset;
2298 fptr->file_ctf_size = ctf->c_shdr.sh_size;
2299 if (ctf->c_shdr.sh_link != 0 &&
2300 cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM)
2301 fptr->file_ctf_dyn = 1;
2302 }
2303
2304 if (fptr->file_lo == NULL)
2305 goto done; /* Nothing else to do if no load object info */
2306
2307 /*
2308 * If the object is a shared library and we have a different rl_base
2309 * value, reset file_dyn_base according to librtld_db's information.
2310 */
2311 if (fptr->file_etype == ET_DYN &&
2312 fptr->file_lo->rl_base != fptr->file_dyn_base) {
2313 dprintf("resetting file_dyn_base for %s to %lx\n",
2314 objectfile, (long)fptr->file_lo->rl_base);
2315 fptr->file_dyn_base = fptr->file_lo->rl_base;
2316 }
2317
2318 /*
2319 * Fill in the PLT information for this file if a PLT symbol is found.
2320 */
2321 if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s,
2322 NULL) != NULL) {
2323 fptr->file_plt_base = s.st_value + fptr->file_dyn_base;
2324 fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0;
2325
2326 /*
2327 * Bring the load object up to date; it is the only way the
2328 * user has to access the PLT data. The PLT information in the
2329 * rd_loadobj_t is not set in the call to map_iter() (the
2330 * callback for rd_loadobj_iter) where we set file_lo.
2331 */
2332 fptr->file_lo->rl_plt_base = fptr->file_plt_base;
2333 fptr->file_lo->rl_plt_size = fptr->file_plt_size;
2334
2335 dprintf("PLT found at %p, size = %lu\n",
2336 (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size);
2337 }
2338
2339 /*
2340 * Fill in the PLT information.
2341 */
2342 if (dyn != NULL) {
2343 uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base;
2344 size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize;
2345 GElf_Dyn d;
2346
2347 for (i = 0; i < ndyn; i++) {
2348 if (gelf_getdyn(dyn->c_data, i, &d) == NULL)
2349 continue;
2350
2351 switch (d.d_tag) {
2352 case DT_JMPREL:
2353 dprintf("DT_JMPREL is %p\n",
2354 (void *)(uintptr_t)d.d_un.d_ptr);
2355 fptr->file_jmp_rel =
2356 d.d_un.d_ptr + fptr->file_dyn_base;
2357 break;
2358 case DT_STRTAB:
2359 dprintf("DT_STRTAB is %p\n",
2360 (void *)(uintptr_t)d.d_un.d_ptr);
2361 break;
2362 case DT_PLTGOT:
2363 dprintf("DT_PLTGOT is %p\n",
2364 (void *)(uintptr_t)d.d_un.d_ptr);
2365 break;
2366 case DT_SUNW_SYMTAB:
2367 dprintf("DT_SUNW_SYMTAB is %p\n",
2368 (void *)(uintptr_t)d.d_un.d_ptr);
2369 break;
2370 case DT_SYMTAB:
2371 dprintf("DT_SYMTAB is %p\n",
2372 (void *)(uintptr_t)d.d_un.d_ptr);
2373 break;
2374 case DT_HASH:
2375 dprintf("DT_HASH is %p\n",
2376 (void *)(uintptr_t)d.d_un.d_ptr);
2377 break;
2378 }
2379 }
2380
2381 dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n",
2382 (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel);
2383 }
2384
2385 done:
2386 free(cache);
2387 return;
2388
2389 bad:
2390 if (cache != NULL)
2391 free(cache);
2392
2393 (void) elf_end(elf);
2394 fptr->file_elf = NULL;
2395 if (fptr->file_elfmem != NULL) {
2396 free(fptr->file_elfmem);
2397 fptr->file_elfmem = NULL;
2398 }
2399 (void) close(fptr->file_fd);
2400 if (fptr->file_dbgelf != NULL)
2401 (void) elf_end(fptr->file_dbgelf);
2402 fptr->file_dbgelf = NULL;
2403 if (fptr->file_dbgfile >= 0)
2404 (void) close(fptr->file_dbgfile);
2405 fptr->file_fd = -1;
2406 fptr->file_dbgfile = -1;
2407 }
2408
2409 /*
2410 * Given a process virtual address, return the map_info_t containing it.
2411 * If none found, return NULL.
2412 */
2413 map_info_t *
2414 Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
2415 {
2416 int lo = 0;
2417 int hi = P->map_count - 1;
2418 int mid;
2419 map_info_t *mp;
2420
2421 while (lo <= hi) {
2422
2423 mid = (lo + hi) / 2;
2424 mp = &P->mappings[mid];
2425
2426 /* check that addr is in [vaddr, vaddr + size) */
2427 if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size)
2428 return (mp);
2429
2430 if (addr < mp->map_pmap.pr_vaddr)
2431 hi = mid - 1;
2432 else
2433 lo = mid + 1;
2434 }
2435
2436 return (NULL);
2437 }
2438
2439 /*
2440 * Return the map_info_t for the executable file.
2441 * If not found, return NULL.
2442 */
2443 static map_info_t *
2444 exec_map(struct ps_prochandle *P)
2445 {
2446 uint_t i;
2447 map_info_t *mptr;
2448 map_info_t *mold = NULL;
2449 file_info_t *fptr;
2450 uintptr_t base;
2451
2452 for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
2453 if (mptr->map_pmap.pr_mapname[0] == '\0')
2454 continue;
2455 if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) {
2456 if ((fptr = mptr->map_file) != NULL &&
2457 fptr->file_lo != NULL) {
2458 base = fptr->file_lo->rl_base;
2459 if (base >= mptr->map_pmap.pr_vaddr &&
2460 base < mptr->map_pmap.pr_vaddr +
2461 mptr->map_pmap.pr_size) /* text space */
2462 return (mptr);
2463 mold = mptr; /* must be the data */
2464 continue;
2465 }
2466 /* This is a poor way to test for text space */
2467 if (!(mptr->map_pmap.pr_mflags & MA_EXEC) ||
2468 (mptr->map_pmap.pr_mflags & MA_WRITE)) {
2469 mold = mptr;
2470 continue;
2471 }
2472 return (mptr);
2473 }
2474 }
2475
2476 return (mold);
2477 }
2478
2479 /*
2480 * Given a shared object name, return the map_info_t for it. If no matching
2481 * object is found, return NULL. Normally, the link maps contain the full
2482 * object pathname, e.g. /usr/lib/libc.so.1. We allow the object name to
2483 * take one of the following forms:
2484 *
2485 * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
2486 * 2. An exact basename match: "libc.so.1"
2487 * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
2488 * 4. The literal string "a.out" is an alias for the executable mapping
2489 *
2490 * The third case is a convenience for callers and may not be necessary.
2491 *
2492 * As the exact same object name may be loaded on different link maps (see
2493 * dlmopen(3DL)), we also allow the caller to resolve the object name by
2494 * specifying a particular link map id. If lmid is PR_LMID_EVERY, the
2495 * first matching name will be returned, regardless of the link map id.
2496 */
2497 static map_info_t *
2498 object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
2499 {
2500 map_info_t *mp;
2501 file_info_t *fp;
2502 size_t objlen;
2503 uint_t i;
2504
2505 /*
2506 * If we have no rtld_db, then always treat a request as one for all
2507 * link maps.
2508 */
2509 if (P->rap == NULL)
2510 lmid = PR_LMID_EVERY;
2511
2512 /*
2513 * First pass: look for exact matches of the entire pathname or
2514 * basename (cases 1 and 2 above):
2515 */
2516 for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
2517
2518 if (mp->map_pmap.pr_mapname[0] == '\0' ||
2519 (fp = mp->map_file) == NULL ||
2520 ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
2521 continue;
2522
2523 if (lmid != PR_LMID_EVERY &&
2524 (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
2525 continue;
2526
2527 /*
2528 * If we match, return the primary text mapping; otherwise
2529 * just return the mapping we matched.
2530 */
2531 if ((fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) ||
2532 (fp->file_rbase && strcmp(fp->file_rbase, objname) == 0) ||
2533 (fp->file_lname && strcmp(fp->file_lname, objname) == 0) ||
2534 (fp->file_rname && strcmp(fp->file_rname, objname) == 0))
2535 return (fp->file_map ? fp->file_map : mp);
2536 }
2537
2538 objlen = strlen(objname);
2539
2540 /*
2541 * Second pass: look for partial matches (case 3 above):
2542 */
2543 for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
2544
2545 if (mp->map_pmap.pr_mapname[0] == '\0' ||
2546 (fp = mp->map_file) == NULL ||
2547 ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
2548 continue;
2549
2550 if (lmid != PR_LMID_EVERY &&
2551 (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
2552 continue;
2553
2554 /*
2555 * If we match, return the primary text mapping; otherwise
2556 * just return the mapping we matched.
2557 */
2558 if ((fp->file_lbase != NULL) &&
2559 (strncmp(fp->file_lbase, objname, objlen) == 0) &&
2560 (fp->file_lbase[objlen] == '.'))
2561 return (fp->file_map ? fp->file_map : mp);
2562 if ((fp->file_rbase != NULL) &&
2563 (strncmp(fp->file_rbase, objname, objlen) == 0) &&
2564 (fp->file_rbase[objlen] == '.'))
2565 return (fp->file_map ? fp->file_map : mp);
2566 }
2567
2568 /*
2569 * One last check: we allow "a.out" to always alias the executable,
2570 * assuming this name was not in use for something else.
2571 */
2572 if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
2573 (strcmp(objname, "a.out") == 0))
2574 return (P->map_exec);
2575
2576 return (NULL);
2577 }
2578
2579 static map_info_t *
2580 object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
2581 {
2582 map_info_t *mptr;
2583
2584 if (!P->info_valid)
2585 Pupdate_maps(P);
2586
2587 if (P->map_exec == NULL && ((mptr = Paddr2mptr(P,
2588 Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL))
2589 P->map_exec = mptr;
2590
2591 if (P->map_ldso == NULL && (mptr = Paddr2mptr(P,
2592 Pgetauxval(P, AT_BASE))) != NULL)
2593 P->map_ldso = mptr;
2594
2595 if (name == PR_OBJ_EXEC)
2596 mptr = P->map_exec;
2597 else if (name == PR_OBJ_LDSO)
2598 mptr = P->map_ldso;
2599 else if (Prd_agent(P) != NULL || P->state == PS_IDLE)
2600 mptr = object_to_map(P, lmid, name);
2601 else
2602 mptr = NULL;
2603
2604 return (mptr);
2605 }
2606
2607 /*
2608 * When two symbols are found by address, decide which one is to be preferred.
2609 */
2610 static GElf_Sym *
2611 sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
2612 {
2613 /*
2614 * Prefer the non-NULL symbol.
2615 */
2616 if (sym1 == NULL)
2617 return (sym2);
2618 if (sym2 == NULL)
2619 return (sym1);
2620
2621 /*
2622 * Defer to the sort ordering...
2623 */
2624 return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2);
2625 }
2626
2627 /*
2628 * Use a binary search to do the work of sym_by_addr().
2629 */
2630 static GElf_Sym *
2631 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp,
2632 uint_t *idp)
2633 {
2634 GElf_Sym sym, osym;
2635 uint_t i, oid, *byaddr = symtab->sym_byaddr;
2636 int min, max, mid, omid, found = 0;
2637
2638 if (symtab->sym_data_pri == NULL || symtab->sym_count == 0)
2639 return (NULL);
2640
2641 min = 0;
2642 max = symtab->sym_count - 1;
2643 osym.st_value = 0;
2644
2645 /*
2646 * We can't return when we've found a match, we have to continue
2647 * searching for the closest matching symbol.
2648 */
2649 while (min <= max) {
2650 mid = (max + min) / 2;
2651
2652 i = byaddr[mid];
2653 (void) symtab_getsym(symtab, i, &sym);
2654
2655 if (addr >= sym.st_value &&
2656 addr < sym.st_value + sym.st_size &&
2657 (!found || sym.st_value > osym.st_value)) {
2658 osym = sym;
2659 omid = mid;
2660 oid = i;
2661 found = 1;
2662 }
2663
2664 if (addr < sym.st_value)
2665 max = mid - 1;
2666 else
2667 min = mid + 1;
2668 }
2669
2670 if (!found)
2671 return (NULL);
2672
2673 /*
2674 * There may be many symbols with identical values so we walk
2675 * backward in the byaddr table to find the best match.
2676 */
2677 do {
2678 sym = osym;
2679 i = oid;
2680
2681 if (omid == 0)
2682 break;
2683
2684 oid = byaddr[--omid];
2685 (void) symtab_getsym(symtab, oid, &osym);
2686 } while (addr >= osym.st_value &&
2687 addr < sym.st_value + osym.st_size &&
2688 osym.st_value == sym.st_value);
2689
2690 *symp = sym;
2691 if (idp != NULL)
2692 *idp = i;
2693 return (symp);
2694 }
2695
2696 /*
2697 * Use a linear search to do the work of sym_by_addr().
2698 */
2699 static GElf_Sym *
2700 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp,
2701 uint_t *idp)
2702 {
2703 size_t symn = symtab->sym_symn;
2704 char *strs = symtab->sym_strs;
2705 GElf_Sym sym, *symp = NULL;
2706 GElf_Sym osym, *osymp = NULL;
2707 int i, id;
2708
2709 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2710 return (NULL);
2711
2712 for (i = 0; i < symn; i++) {
2713 if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) {
2714 if (addr >= sym.st_value &&
2715 addr < sym.st_value + sym.st_size) {
2716 if (osymp)
2717 symp = sym_prefer(
2718 symp, strs + symp->st_name,
2719 osymp, strs + osymp->st_name);
2720 if (symp != osymp) {
2721 osym = sym;
2722 osymp = &osym;
2723 id = i;
2724 }
2725 }
2726 }
2727 }
2728 if (osymp) {
2729 *symbolp = osym;
2730 if (idp)
2731 *idp = id;
2732 return (symbolp);
2733 }
2734 return (NULL);
2735 }
2736
2737 /*
2738 * Look up a symbol by address in the specified symbol table.
2739 * Adjustment to 'addr' must already have been made for the
2740 * offset of the symbol if this is a dynamic library symbol table.
2741 *
2742 * Use a linear or a binary search depending on whether or not we
2743 * chose to sort the table in optimize_symtab().
2744 */
2745 static GElf_Sym *
2746 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
2747 {
2748 if (_libproc_no_qsort) {
2749 return (sym_by_addr_linear(symtab, addr, symp, idp));
2750 } else {
2751 return (sym_by_addr_binary(symtab, addr, symp, idp));
2752 }
2753 }
2754
2755 /*
2756 * Use a binary search to do the work of sym_by_name().
2757 */
2758 static GElf_Sym *
2759 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2760 uint_t *idp)
2761 {
2762 char *strs = symtab->sym_strs;
2763 uint_t i, *byname = symtab->sym_byname;
2764 int min, mid, max, cmp;
2765
2766 if (symtab->sym_data_pri == NULL || strs == NULL ||
2767 symtab->sym_count == 0)
2768 return (NULL);
2769
2770 min = 0;
2771 max = symtab->sym_count - 1;
2772
2773 while (min <= max) {
2774 mid = (max + min) / 2;
2775
2776 i = byname[mid];
2777 (void) symtab_getsym(symtab, i, symp);
2778
2779 if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
2780 if (idp != NULL)
2781 *idp = i;
2782 return (symp);
2783 }
2784
2785 if (cmp < 0)
2786 max = mid - 1;
2787 else
2788 min = mid + 1;
2789 }
2790
2791 return (NULL);
2792 }
2793
2794 /*
2795 * Use a linear search to do the work of sym_by_name().
2796 */
2797 static GElf_Sym *
2798 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2799 uint_t *idp)
2800 {
2801 size_t symn = symtab->sym_symn;
2802 char *strs = symtab->sym_strs;
2803 int i;
2804
2805 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2806 return (NULL);
2807
2808 for (i = 0; i < symn; i++) {
2809 if (symtab_getsym(symtab, i, symp) &&
2810 strcmp(name, strs + symp->st_name) == 0) {
2811 if (idp)
2812 *idp = i;
2813 return (symp);
2814 }
2815 }
2816
2817 return (NULL);
2818 }
2819
2820 /*
2821 * Look up a symbol by name in the specified symbol table.
2822 *
2823 * Use a linear or a binary search depending on whether or not we
2824 * chose to sort the table in optimize_symtab().
2825 */
2826 static GElf_Sym *
2827 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
2828 {
2829 if (_libproc_no_qsort) {
2830 return (sym_by_name_linear(symtab, name, symp, idp));
2831 } else {
2832 return (sym_by_name_binary(symtab, name, symp, idp));
2833 }
2834 }
2835
2836 /*
2837 * Search the process symbol tables looking for a symbol whose
2838 * value to value+size contain the address specified by addr.
2839 * Return values are:
2840 * sym_name_buffer containing the symbol name
2841 * GElf_Sym symbol table entry
2842 * prsyminfo_t ancillary symbol information
2843 * Returns 0 on success, -1 on failure.
2844 */
2845 static int
2846 i_Pxlookup_by_addr(
2847 struct ps_prochandle *P,
2848 int lmresolve, /* use resolve linker object names */
2849 uintptr_t addr, /* process address being sought */
2850 char *sym_name_buffer, /* buffer for the symbol name */
2851 size_t bufsize, /* size of sym_name_buffer */
2852 GElf_Sym *symbolp, /* returned symbol table entry */
2853 prsyminfo_t *sip) /* returned symbol info */
2854 {
2855 GElf_Sym *symp;
2856 char *name;
2857 GElf_Sym sym1, *sym1p = NULL;
2858 GElf_Sym sym2, *sym2p = NULL;
2859 char *name1 = NULL;
2860 char *name2 = NULL;
2861 uint_t i1;
2862 uint_t i2;
2863 map_info_t *mptr;
2864 file_info_t *fptr;
2865
2866 (void) Prd_agent(P);
2867
2868 if ((mptr = Paddr2mptr(P, addr)) == NULL || /* no such address */
2869 (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
2870 fptr->file_elf == NULL) /* not an ELF file */
2871 return (-1);
2872
2873 /*
2874 * Adjust the address by the load object base address in
2875 * case the address turns out to be in a shared library.
2876 */
2877 addr -= fptr->file_dyn_base;
2878
2879 /*
2880 * Search both symbol tables, symtab first, then dynsym.
2881 */
2882 if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
2883 name1 = fptr->file_symtab.sym_strs + sym1.st_name;
2884 if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
2885 name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
2886
2887 if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
2888 return (-1);
2889
2890 name = (symp == sym1p) ? name1 : name2;
2891 if (bufsize > 0) {
2892 (void) strncpy(sym_name_buffer, name, bufsize);
2893 sym_name_buffer[bufsize - 1] = '\0';
2894 }
2895
2896 *symbolp = *symp;
2897 if (sip != NULL) {
2898 sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer;
2899 if (lmresolve && (fptr->file_rname != NULL))
2900 sip->prs_object = fptr->file_rbase;
2901 else
2902 sip->prs_object = fptr->file_lbase;
2903 sip->prs_id = (symp == sym1p) ? i1 : i2;
2904 sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM;
2905 sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE :
2906 fptr->file_lo->rl_lmident;
2907 }
2908
2909 if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
2910 symbolp->st_value += fptr->file_dyn_base;
2911
2912 return (0);
2913 }
2914
2915 int
2916 Pxlookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2917 size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
2918 {
2919 return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, bufsize, symp, sip));
2920 }
2921
2922 int
2923 Pxlookup_by_addr_resolved(struct ps_prochandle *P, uintptr_t addr, char *buf,
2924 size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
2925 {
2926 return (i_Pxlookup_by_addr(P, B_TRUE, addr, buf, bufsize, symp, sip));
2927 }
2928
2929 int
2930 Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2931 size_t size, GElf_Sym *symp)
2932 {
2933 return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, size, symp, NULL));
2934 }
2935
2936 /*
2937 * Search the process symbol tables looking for a symbol whose name matches the
2938 * specified name and whose object and link map optionally match the specified
2939 * parameters. On success, the function returns 0 and fills in the GElf_Sym
2940 * symbol table entry. On failure, -1 is returned.
2941 */
2942 int
2943 Pxlookup_by_name(
2944 struct ps_prochandle *P,
2945 Lmid_t lmid, /* link map to match, or -1 for any */
2946 const char *oname, /* load object name */
2947 const char *sname, /* symbol name */
2948 GElf_Sym *symp, /* returned symbol table entry */
2949 prsyminfo_t *sip) /* returned symbol info */
2950 {
2951 map_info_t *mptr;
2952 file_info_t *fptr;
2953 int cnt;
2954
2955 GElf_Sym sym;
2956 prsyminfo_t si;
2957 int rv = -1;
2958 uint_t id;
2959
2960 if (oname == PR_OBJ_EVERY) {
2961 /* create all the file_info_t's for all the mappings */
2962 (void) Prd_agent(P);
2963 cnt = P->num_files;
2964 fptr = list_next(&P->file_head);
2965 } else {
2966 cnt = 1;
2967 if ((mptr = object_name_to_map(P, lmid, oname)) == NULL ||
2968 (fptr = build_map_symtab(P, mptr)) == NULL)
2969 return (-1);
2970 }
2971
2972 /*
2973 * Iterate through the loaded object files and look for the symbol
2974 * name in the .symtab and .dynsym of each. If we encounter a match
2975 * with SHN_UNDEF, keep looking in hopes of finding a better match.
2976 * This means that a name such as "puts" will match the puts function
2977 * in libc instead of matching the puts PLT entry in the a.out file.
2978 */
2979 for (; cnt > 0; cnt--, fptr = list_next(fptr)) {
2980 Pbuild_file_symtab(P, fptr);
2981
2982 if (fptr->file_elf == NULL)
2983 continue;
2984
2985 if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
2986 lmid != fptr->file_lo->rl_lmident)
2987 continue;
2988
2989 if (fptr->file_symtab.sym_data_pri != NULL &&
2990 sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
2991 if (sip != NULL) {
2992 sip->prs_id = id;
2993 sip->prs_table = PR_SYMTAB;
2994 sip->prs_object = oname;
2995 sip->prs_name = sname;
2996 sip->prs_lmid = fptr->file_lo == NULL ?
2997 LM_ID_BASE : fptr->file_lo->rl_lmident;
2998 }
2999 } else if (fptr->file_dynsym.sym_data_pri != NULL &&
3000 sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
3001 if (sip != NULL) {
3002 sip->prs_id = id;
3003 sip->prs_table = PR_DYNSYM;
3004 sip->prs_object = oname;
3005 sip->prs_name = sname;
3006 sip->prs_lmid = fptr->file_lo == NULL ?
3007 LM_ID_BASE : fptr->file_lo->rl_lmident;
3008 }
3009 } else {
3010 continue;
3011 }
3012
3013 if (GELF_ST_TYPE(symp->st_info) != STT_TLS)
3014 symp->st_value += fptr->file_dyn_base;
3015
3016 if (symp->st_shndx != SHN_UNDEF)
3017 return (0);
3018
3019 if (rv != 0) {
3020 if (sip != NULL)
3021 si = *sip;
3022 sym = *symp;
3023 rv = 0;
3024 }
3025 }
3026
3027 if (rv == 0) {
3028 if (sip != NULL)
3029 *sip = si;
3030 *symp = sym;
3031 }
3032
3033 return (rv);
3034 }
3035
3036 /*
3037 * Search the process symbol tables looking for a symbol whose name matches the
3038 * specified name, but without any restriction on the link map id.
3039 */
3040 int
3041 Plookup_by_name(struct ps_prochandle *P, const char *object,
3042 const char *symbol, GElf_Sym *symp)
3043 {
3044 return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL));
3045 }
3046
3047 /*
3048 * Iterate over the process's address space mappings.
3049 */
3050 static int
3051 i_Pmapping_iter(struct ps_prochandle *P, boolean_t lmresolve,
3052 proc_map_f *func, void *cd)
3053 {
3054 map_info_t *mptr;
3055 file_info_t *fptr;
3056 char *object_name;
3057 int rc = 0;
3058 int i;
3059
3060 /* create all the file_info_t's for all the mappings */
3061 (void) Prd_agent(P);
3062
3063 for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
3064 if ((fptr = mptr->map_file) == NULL)
3065 object_name = NULL;
3066 else if (lmresolve && (fptr->file_rname != NULL))
3067 object_name = fptr->file_rname;
3068 else
3069 object_name = fptr->file_lname;
3070 if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0)
3071 return (rc);
3072 }
3073 return (0);
3074 }
3075
3076 int
3077 Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
3078 {
3079 return (i_Pmapping_iter(P, B_FALSE, func, cd));
3080 }
3081
3082 int
3083 Pmapping_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
3084 {
3085 return (i_Pmapping_iter(P, B_TRUE, func, cd));
3086 }
3087
3088 /*
3089 * Iterate over the process's mapped objects.
3090 */
3091 static int
3092 i_Pobject_iter(struct ps_prochandle *P, boolean_t lmresolve,
3093 proc_map_f *func, void *cd)
3094 {
3095 map_info_t *mptr;
3096 file_info_t *fptr;
3097 uint_t cnt;
3098 int rc = 0;
3099
3100 (void) Prd_agent(P); /* create file_info_t's for all the mappings */
3101 Pupdate_maps(P);
3102
3103 for (cnt = P->num_files, fptr = list_next(&P->file_head);
3104 cnt; cnt--, fptr = list_next(fptr)) {
3105 const char *lname;
3106
3107 if (lmresolve && (fptr->file_rname != NULL))
3108 lname = fptr->file_rname;
3109 else if (fptr->file_lname != NULL)
3110 lname = fptr->file_lname;
3111 else
3112 lname = "";
3113
3114 if ((mptr = fptr->file_map) == NULL)
3115 continue;
3116
3117 if ((rc = func(cd, &mptr->map_pmap, lname)) != 0)
3118 return (rc);
3119
3120 if (!P->info_valid)
3121 Pupdate_maps(P);
3122 }
3123 return (0);
3124 }
3125
3126 int
3127 Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
3128 {
3129 return (i_Pobject_iter(P, B_FALSE, func, cd));
3130 }
3131
3132 int
3133 Pobject_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
3134 {
3135 return (i_Pobject_iter(P, B_TRUE, func, cd));
3136 }
3137
3138 static char *
3139 i_Pobjname(struct ps_prochandle *P, boolean_t lmresolve, uintptr_t addr,
3140 char *buffer, size_t bufsize)
3141 {
3142 map_info_t *mptr;
3143 file_info_t *fptr;
3144
3145 /* create all the file_info_t's for all the mappings */
3146 (void) Prd_agent(P);
3147
3148 if ((mptr = Paddr2mptr(P, addr)) == NULL)
3149 return (NULL);
3150
3151 if (!lmresolve) {
3152 if (((fptr = mptr->map_file) == NULL) ||
3153 (fptr->file_lname == NULL))
3154 return (NULL);
3155 (void) strlcpy(buffer, fptr->file_lname, bufsize);
3156 return (buffer);
3157 }
3158
3159 /* Check for a cached copy of the resolved path */
3160 if (Pfindmap(P, mptr, buffer, bufsize) != NULL)
3161 return (buffer);
3162
3163 return (NULL);
3164 }
3165
3166 /*
3167 * Given a virtual address, return the name of the underlying
3168 * mapped object (file) as provided by the dynamic linker.
3169 * Return NULL if we can't find any name information for the object.
3170 */
3171 char *
3172 Pobjname(struct ps_prochandle *P, uintptr_t addr,
3173 char *buffer, size_t bufsize)
3174 {
3175 return (i_Pobjname(P, B_FALSE, addr, buffer, bufsize));
3176 }
3177
3178 /*
3179 * Given a virtual address, try to return a filesystem path to the
3180 * underlying mapped object (file). If we're in the global zone,
3181 * this path could resolve to an object in another zone. If we're
3182 * unable return a valid filesystem path, we'll fall back to providing
3183 * the mapped object (file) name provided by the dynamic linker in
3184 * the target process (ie, the object reported by Pobjname()).
3185 */
3186 char *
3187 Pobjname_resolved(struct ps_prochandle *P, uintptr_t addr,
3188 char *buffer, size_t bufsize)
3189 {
3190 return (i_Pobjname(P, B_TRUE, addr, buffer, bufsize));
3191 }
3192
3193 /*
3194 * Given a virtual address, return the link map id of the underlying mapped
3195 * object (file), as provided by the dynamic linker. Return -1 on failure.
3196 */
3197 int
3198 Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
3199 {
3200 map_info_t *mptr;
3201 file_info_t *fptr;
3202
3203 /* create all the file_info_t's for all the mappings */
3204 (void) Prd_agent(P);
3205
3206 if ((mptr = Paddr2mptr(P, addr)) != NULL &&
3207 (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
3208 *lmidp = fptr->file_lo->rl_lmident;
3209 return (0);
3210 }
3211
3212 return (-1);
3213 }
3214
3215 /*
3216 * Given an object name and optional lmid, iterate over the object's symbols.
3217 * If which == PR_SYMTAB, search the normal symbol table.
3218 * If which == PR_DYNSYM, search the dynamic symbol table.
3219 */
3220 static int
3221 Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
3222 int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd)
3223 {
3224 #if STT_NUM != (STT_TLS + 1)
3225 #error "STT_NUM has grown. update Psymbol_iter_com()"
3226 #endif
3227
3228 GElf_Sym sym;
3229 GElf_Shdr shdr;
3230 map_info_t *mptr;
3231 file_info_t *fptr;
3232 sym_tbl_t *symtab;
3233 size_t symn;
3234 const char *strs;
3235 size_t strsz;
3236 prsyminfo_t si;
3237 int rv;
3238 uint_t *map, i, count, ndx;
3239
3240 if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL)
3241 return (-1);
3242
3243 if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
3244 fptr->file_elf == NULL) /* not an ELF file */
3245 return (-1);
3246
3247 /*
3248 * Search the specified symbol table.
3249 */
3250 switch (which) {
3251 case PR_SYMTAB:
3252 symtab = &fptr->file_symtab;
3253 si.prs_table = PR_SYMTAB;
3254 break;
3255 case PR_DYNSYM:
3256 symtab = &fptr->file_dynsym;
3257 si.prs_table = PR_DYNSYM;
3258 break;
3259 default:
3260 return (-1);
3261 }
3262
3263 si.prs_object = object_name;
3264 si.prs_lmid = fptr->file_lo == NULL ?
3265 LM_ID_BASE : fptr->file_lo->rl_lmident;
3266
3267 symn = symtab->sym_symn;
3268 strs = symtab->sym_strs;
3269 strsz = symtab->sym_strsz;
3270
3271 switch (order) {
3272 case PRO_NATURAL:
3273 map = NULL;
3274 count = symn;
3275 break;
3276 case PRO_BYNAME:
3277 map = symtab->sym_byname;
3278 count = symtab->sym_count;
3279 break;
3280 case PRO_BYADDR:
3281 map = symtab->sym_byaddr;
3282 count = symtab->sym_count;
3283 break;
3284 default:
3285 return (-1);
3286 }
3287
3288 if (symtab->sym_data_pri == NULL || strs == NULL || count == 0)
3289 return (-1);
3290
3291 rv = 0;
3292
3293 for (i = 0; i < count; i++) {
3294 ndx = map == NULL ? i : map[i];
3295 if (symtab_getsym(symtab, ndx, &sym) != NULL) {
3296 uint_t s_bind, s_type, type;
3297
3298 if (sym.st_name >= strsz) /* invalid st_name */
3299 continue;
3300
3301 s_bind = GELF_ST_BIND(sym.st_info);
3302 s_type = GELF_ST_TYPE(sym.st_info);
3303
3304 /*
3305 * In case you haven't already guessed, this relies on
3306 * the bitmask used in <libproc.h> for encoding symbol
3307 * type and binding matching the order of STB and STT
3308 * constants in <sys/elf.h>. Changes to ELF must
3309 * maintain binary compatibility, so I think this is
3310 * reasonably fair game.
3311 */
3312 if (s_bind < STB_NUM && s_type < STT_NUM) {
3313 type = (1 << (s_type + 8)) | (1 << s_bind);
3314 if ((type & ~mask) != 0)
3315 continue;
3316 } else
3317 continue; /* Invalid type or binding */
3318
3319 if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
3320 sym.st_value += fptr->file_dyn_base;
3321
3322 si.prs_name = strs + sym.st_name;
3323
3324 /*
3325 * If symbol's type is STT_SECTION, then try to lookup
3326 * the name of the corresponding section.
3327 */
3328 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
3329 fptr->file_shstrs != NULL &&
3330 gelf_getshdr(elf_getscn(fptr->file_elf,
3331 sym.st_shndx), &shdr) != NULL &&
3332 shdr.sh_name != 0 &&
3333 shdr.sh_name < fptr->file_shstrsz)
3334 si.prs_name = fptr->file_shstrs + shdr.sh_name;
3335
3336 si.prs_id = ndx;
3337 if ((rv = func(cd, &sym, si.prs_name, &si)) != 0)
3338 break;
3339 }
3340 }
3341
3342 return (rv);
3343 }
3344
3345 int
3346 Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
3347 int which, int mask, proc_xsym_f *func, void *cd)
3348 {
3349 return (Psymbol_iter_com(P, lmid, object_name, which, mask,
3350 PRO_NATURAL, func, cd));
3351 }
3352
3353 int
3354 Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid,
3355 const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
3356 {
3357 return (Psymbol_iter_com(P, lmid, object_name, which, mask,
3358 PRO_NATURAL, (proc_xsym_f *)func, cd));
3359 }
3360
3361 int
3362 Psymbol_iter(struct ps_prochandle *P,
3363 const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
3364 {
3365 return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
3366 PRO_NATURAL, (proc_xsym_f *)func, cd));
3367 }
3368
3369 int
3370 Psymbol_iter_by_addr(struct ps_prochandle *P,
3371 const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
3372 {
3373 return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
3374 PRO_BYADDR, (proc_xsym_f *)func, cd));
3375 }
3376
3377 int
3378 Psymbol_iter_by_name(struct ps_prochandle *P,
3379 const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
3380 {
3381 return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
3382 PRO_BYNAME, (proc_xsym_f *)func, cd));
3383 }
3384
3385 /*
3386 * Get the platform string.
3387 */
3388 char *
3389 Pplatform(struct ps_prochandle *P, char *s, size_t n)
3390 {
3391 return (P->ops.pop_platform(P, s, n, P->data));
3392 }
3393
3394 /*
3395 * Get the uname(2) information.
3396 */
3397 int
3398 Puname(struct ps_prochandle *P, struct utsname *u)
3399 {
3400 return (P->ops.pop_uname(P, u, P->data));
3401 }
3402
3403 /*
3404 * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize
3405 * the symbol table heads in the new ps_prochandle.
3406 */
3407 void
3408 Pinitsym(struct ps_prochandle *P)
3409 {
3410 P->num_files = 0;
3411 list_link(&P->file_head, NULL);
3412 }
3413
3414 /*
3415 * Called from Prelease() to destroy the symbol tables.
3416 * Must be called by the client after an exec() in the victim process.
3417 */
3418 void
3419 Preset_maps(struct ps_prochandle *P)
3420 {
3421 int i;
3422
3423 if (P->rap != NULL) {
3424 rd_delete(P->rap);
3425 P->rap = NULL;
3426 }
3427
3428 if (P->execname != NULL) {
3429 free(P->execname);
3430 P->execname = NULL;
3431 }
3432
3433 if (P->auxv != NULL) {
3434 free(P->auxv);
3435 P->auxv = NULL;
3436 P->nauxv = 0;
3437 }
3438
3439 for (i = 0; i < P->map_count; i++)
3440 map_info_free(P, &P->mappings[i]);
3441
3442 if (P->mappings != NULL) {
3443 free(P->mappings);
3444 P->mappings = NULL;
3445 }
3446 P->map_count = P->map_alloc = 0;
3447
3448 P->info_valid = 0;
3449 }
3450
3451 typedef struct getenv_data {
3452 char *buf;
3453 size_t bufsize;
3454 const char *search;
3455 size_t searchlen;
3456 } getenv_data_t;
3457
3458 /*ARGSUSED*/
3459 static int
3460 getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr,
3461 const char *nameval)
3462 {
3463 getenv_data_t *d = data;
3464 size_t len;
3465
3466 if (nameval == NULL)
3467 return (0);
3468
3469 if (d->searchlen < strlen(nameval) &&
3470 strncmp(nameval, d->search, d->searchlen) == 0 &&
3471 nameval[d->searchlen] == '=') {
3472 len = MIN(strlen(nameval), d->bufsize - 1);
3473 (void) strncpy(d->buf, nameval, len);
3474 d->buf[len] = '\0';
3475 return (1);
3476 }
3477
3478 return (0);
3479 }
3480
3481 char *
3482 Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen)
3483 {
3484 getenv_data_t d;
3485
3486 d.buf = buf;
3487 d.bufsize = buflen;
3488 d.search = name;
3489 d.searchlen = strlen(name);
3490
3491 if (Penv_iter(P, getenv_func, &d) == 1) {
3492 char *equals = strchr(d.buf, '=');
3493
3494 if (equals != NULL) {
3495 (void) memmove(d.buf, equals + 1,
3496 d.buf + buflen - equals - 1);
3497 d.buf[d.buf + buflen - equals] = '\0';
3498
3499 return (buf);
3500 }
3501 }
3502
3503 return (NULL);
3504 }
3505
3506 /* number of argument or environment pointers to read all at once */
3507 #define NARG 100
3508
3509 int
3510 Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data)
3511 {
3512 const psinfo_t *psp;
3513 uintptr_t envpoff;
3514 GElf_Sym sym;
3515 int ret;
3516 char *buf, *nameval;
3517 size_t buflen;
3518
3519 int nenv = NARG;
3520 long envp[NARG];
3521
3522 /*
3523 * Attempt to find the "_environ" variable in the process.
3524 * Failing that, use the original value provided by Ppsinfo().
3525 */
3526 if ((psp = Ppsinfo(P)) == NULL)
3527 return (-1);
3528
3529 envpoff = psp->pr_envp; /* Default if no _environ found */
3530
3531 if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) {
3532 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
3533 if (Pread(P, &envpoff, sizeof (envpoff),
3534 sym.st_value) != sizeof (envpoff))
3535 envpoff = psp->pr_envp;
3536 } else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
3537 uint32_t envpoff32;
3538
3539 if (Pread(P, &envpoff32, sizeof (envpoff32),
3540 sym.st_value) != sizeof (envpoff32))
3541 envpoff = psp->pr_envp;
3542 else
3543 envpoff = envpoff32;
3544 }
3545 }
3546
3547 buflen = 128;
3548 buf = malloc(buflen);
3549
3550 ret = 0;
3551 for (;;) {
3552 uintptr_t envoff;
3553
3554 if (nenv == NARG) {
3555 (void) memset(envp, 0, sizeof (envp));
3556 if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
3557 if (Pread(P, envp,
3558 sizeof (envp), envpoff) <= 0) {
3559 ret = -1;
3560 break;
3561 }
3562 } else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
3563 uint32_t e32[NARG];
3564 int i;
3565
3566 (void) memset(e32, 0, sizeof (e32));
3567 if (Pread(P, e32, sizeof (e32), envpoff) <= 0) {
3568 ret = -1;
3569 break;
3570 }
3571 for (i = 0; i < NARG; i++)
3572 envp[i] = e32[i];
3573 }
3574 nenv = 0;
3575 }
3576
3577 if ((envoff = envp[nenv++]) == NULL)
3578 break;
3579
3580 /*
3581 * Attempt to read the string from the process.
3582 */
3583 again:
3584 ret = Pread_string(P, buf, buflen, envoff);
3585
3586 if (ret <= 0) {
3587 nameval = NULL;
3588 } else if (ret == buflen - 1) {
3589 free(buf);
3590 /*
3591 * Bail if we have a corrupted environment
3592 */
3593 if (buflen >= ARG_MAX)
3594 return (-1);
3595 buflen *= 2;
3596 buf = malloc(buflen);
3597 goto again;
3598 } else {
3599 nameval = buf;
3600 }
3601
3602 if ((ret = func(data, P, envoff, nameval)) != 0)
3603 break;
3604
3605 envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4;
3606 }
3607
3608 free(buf);
3609
3610 return (ret);
3611 }