17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5ab9a77c7Sahl * Common Development and Distribution License (the "License").
6ab9a77c7Sahl * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21ab9a77c7Sahl
227c478bd9Sstevel@tonic-gate /*
230a9ca722SRobert Harris * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
24936dcb54SRobert Mustacchi * Copyright 2016 Joyent, Inc.
252a12f85aSJeremy Jones * Copyright (c) 2013 by Delphix. All rights reserved.
26*e8c318c6SRobert Mustacchi * Copyright 2023 Oxide Computer Company
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
299acbbeafSnn35248 #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <stddef.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <strings.h>
387c478bd9Sstevel@tonic-gate #include <memory.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <dirent.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <libgen.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
47e68846a8SRobert Mustacchi #include <sys/crc32.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #include "libproc.h"
507c478bd9Sstevel@tonic-gate #include "Pcontrol.h"
517c478bd9Sstevel@tonic-gate #include "Putil.h"
52d51e9074Sab196087 #include "Psymtab_machelf.h"
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static file_info_t *build_map_symtab(struct ps_prochandle *, map_info_t *);
557c478bd9Sstevel@tonic-gate static map_info_t *exec_map(struct ps_prochandle *);
567c478bd9Sstevel@tonic-gate static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
577c478bd9Sstevel@tonic-gate static map_info_t *object_name_to_map(struct ps_prochandle *,
587c478bd9Sstevel@tonic-gate Lmid_t, const char *);
597c478bd9Sstevel@tonic-gate static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
6030da1432Sahl static int read_ehdr32(struct ps_prochandle *, Elf32_Ehdr *, uint_t *,
6130da1432Sahl uintptr_t);
627c478bd9Sstevel@tonic-gate #ifdef _LP64
6330da1432Sahl static int read_ehdr64(struct ps_prochandle *, Elf64_Ehdr *, uint_t *,
6430da1432Sahl uintptr_t);
657c478bd9Sstevel@tonic-gate #endif
66e68846a8SRobert Mustacchi static uint32_t psym_crc32[] = { CRC32_TABLE };
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #define DATA_TYPES \
697c478bd9Sstevel@tonic-gate ((1 << STT_OBJECT) | (1 << STT_FUNC) | \
707c478bd9Sstevel@tonic-gate (1 << STT_COMMON) | (1 << STT_TLS))
717c478bd9Sstevel@tonic-gate #define IS_DATA_TYPE(tp) (((1 << (tp)) & DATA_TYPES) != 0)
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #define MA_RWX (MA_READ | MA_WRITE | MA_EXEC)
747c478bd9Sstevel@tonic-gate
758d12444dSRobert Mustacchi /*
768d12444dSRobert Mustacchi * Minimum and maximum length of a build-id that we'll accept. Generally it's a
778d12444dSRobert Mustacchi * 20 byte SHA1 and it's expected that the first byte (which is two ascii
788d12444dSRobert Mustacchi * characters) indicates a directory and the remaining bytes become the file
798d12444dSRobert Mustacchi * name. Therefore, our minimum length is at least 2 bytes (one for the
808d12444dSRobert Mustacchi * directory and one for the name) and the max is a bit over the minimum -- 64,
818d12444dSRobert Mustacchi * just in case folks do something odd. The string length is three times the max
828d12444dSRobert Mustacchi * length. This accounts for the fact that each byte is two characters, a null
838d12444dSRobert Mustacchi * terminator, and the directory '/' character.
848d12444dSRobert Mustacchi */
858d12444dSRobert Mustacchi #define MINBUILDID 2
868d12444dSRobert Mustacchi #define MAXBUILDID 64
878d12444dSRobert Mustacchi #define BUILDID_STRLEN (3*MAXBUILDID)
888d12444dSRobert Mustacchi #define BUILDID_NAME ".note.gnu.build-id"
898d12444dSRobert Mustacchi #define DBGLINK_NAME ".gnu_debuglink"
908d12444dSRobert Mustacchi
917c478bd9Sstevel@tonic-gate typedef enum {
927c478bd9Sstevel@tonic-gate PRO_NATURAL,
937c478bd9Sstevel@tonic-gate PRO_BYADDR,
947c478bd9Sstevel@tonic-gate PRO_BYNAME
957c478bd9Sstevel@tonic-gate } pr_order_t;
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate static int
addr_cmp(const void * aa,const void * bb)987c478bd9Sstevel@tonic-gate addr_cmp(const void *aa, const void *bb)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate uintptr_t a = *((uintptr_t *)aa);
1017c478bd9Sstevel@tonic-gate uintptr_t b = *((uintptr_t *)bb);
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if (a > b)
1047c478bd9Sstevel@tonic-gate return (1);
1057c478bd9Sstevel@tonic-gate if (a < b)
1067c478bd9Sstevel@tonic-gate return (-1);
1077c478bd9Sstevel@tonic-gate return (0);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*
11112e72dbbSrh87107 * This function creates a list of addresses for a load object's sections.
11212e72dbbSrh87107 * The list is in ascending address order and alternates start address
11312e72dbbSrh87107 * then end address for each section we're interested in. The function
11412e72dbbSrh87107 * returns a pointer to the list, which must be freed by the caller.
11512e72dbbSrh87107 */
11612e72dbbSrh87107 static uintptr_t *
get_saddrs(struct ps_prochandle * P,uintptr_t ehdr_start,uint_t * n)11712e72dbbSrh87107 get_saddrs(struct ps_prochandle *P, uintptr_t ehdr_start, uint_t *n)
11812e72dbbSrh87107 {
11912e72dbbSrh87107 uintptr_t a, addr, *addrs, last = 0;
12012e72dbbSrh87107 uint_t i, naddrs = 0, unordered = 0;
12112e72dbbSrh87107
12212e72dbbSrh87107 if (P->status.pr_dmodel == PR_MODEL_ILP32) {
12312e72dbbSrh87107 Elf32_Ehdr ehdr;
12412e72dbbSrh87107 Elf32_Phdr phdr;
12512e72dbbSrh87107 uint_t phnum;
12612e72dbbSrh87107
12712e72dbbSrh87107 if (read_ehdr32(P, &ehdr, &phnum, ehdr_start) != 0)
12812e72dbbSrh87107 return (NULL);
12912e72dbbSrh87107
13012e72dbbSrh87107 addrs = malloc(sizeof (uintptr_t) * phnum * 2);
13112e72dbbSrh87107 a = ehdr_start + ehdr.e_phoff;
13212e72dbbSrh87107 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
13312e72dbbSrh87107 if (Pread(P, &phdr, sizeof (phdr), a) !=
13412e72dbbSrh87107 sizeof (phdr)) {
13512e72dbbSrh87107 free(addrs);
13612e72dbbSrh87107 return (NULL);
13712e72dbbSrh87107 }
13812e72dbbSrh87107 if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
13912e72dbbSrh87107 continue;
14012e72dbbSrh87107
14112e72dbbSrh87107 addr = phdr.p_vaddr;
14212e72dbbSrh87107 if (ehdr.e_type == ET_DYN)
14312e72dbbSrh87107 addr += ehdr_start;
14412e72dbbSrh87107 if (last > addr)
14512e72dbbSrh87107 unordered = 1;
14612e72dbbSrh87107 addrs[naddrs++] = addr;
14712e72dbbSrh87107 addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
14812e72dbbSrh87107 }
14912e72dbbSrh87107 #ifdef _LP64
15012e72dbbSrh87107 } else {
15112e72dbbSrh87107 Elf64_Ehdr ehdr;
15212e72dbbSrh87107 Elf64_Phdr phdr;
15312e72dbbSrh87107 uint_t phnum;
15412e72dbbSrh87107
15512e72dbbSrh87107 if (read_ehdr64(P, &ehdr, &phnum, ehdr_start) != 0)
15612e72dbbSrh87107 return (NULL);
15712e72dbbSrh87107
15812e72dbbSrh87107 addrs = malloc(sizeof (uintptr_t) * phnum * 2);
15912e72dbbSrh87107 a = ehdr_start + ehdr.e_phoff;
16012e72dbbSrh87107 for (i = 0; i < phnum; i++, a += ehdr.e_phentsize) {
16112e72dbbSrh87107 if (Pread(P, &phdr, sizeof (phdr), a) !=
16212e72dbbSrh87107 sizeof (phdr)) {
16312e72dbbSrh87107 free(addrs);
16412e72dbbSrh87107 return (NULL);
16512e72dbbSrh87107 }
16612e72dbbSrh87107 if (phdr.p_type != PT_LOAD || phdr.p_memsz == 0)
16712e72dbbSrh87107 continue;
16812e72dbbSrh87107
16912e72dbbSrh87107 addr = phdr.p_vaddr;
17012e72dbbSrh87107 if (ehdr.e_type == ET_DYN)
17112e72dbbSrh87107 addr += ehdr_start;
17212e72dbbSrh87107 if (last > addr)
17312e72dbbSrh87107 unordered = 1;
17412e72dbbSrh87107 addrs[naddrs++] = addr;
17512e72dbbSrh87107 addrs[naddrs++] = last = addr + phdr.p_memsz - 1;
17612e72dbbSrh87107 }
17712e72dbbSrh87107 #endif
17812e72dbbSrh87107 }
17912e72dbbSrh87107
18012e72dbbSrh87107 if (unordered)
18112e72dbbSrh87107 qsort(addrs, naddrs, sizeof (uintptr_t), addr_cmp);
18212e72dbbSrh87107
18312e72dbbSrh87107 *n = naddrs;
18412e72dbbSrh87107 return (addrs);
18512e72dbbSrh87107 }
18612e72dbbSrh87107
18712e72dbbSrh87107 /*
1887c478bd9Sstevel@tonic-gate * Allocation function for a new file_info_t
1897c478bd9Sstevel@tonic-gate */
190d7755b5aSrh87107 file_info_t *
file_info_new(struct ps_prochandle * P,map_info_t * mptr)1917c478bd9Sstevel@tonic-gate file_info_new(struct ps_prochandle *P, map_info_t *mptr)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate file_info_t *fptr;
1947c478bd9Sstevel@tonic-gate map_info_t *mp;
195d7755b5aSrh87107 uintptr_t mstart, mend, sstart, send;
196d7755b5aSrh87107 uint_t i;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if ((fptr = calloc(1, sizeof (file_info_t))) == NULL)
1997c478bd9Sstevel@tonic-gate return (NULL);
2007c478bd9Sstevel@tonic-gate
20150d4d24eSRobert Mustacchi list_insert_tail(&P->file_head, fptr);
2027c478bd9Sstevel@tonic-gate (void) strcpy(fptr->file_pname, mptr->map_pmap.pr_mapname);
2037c478bd9Sstevel@tonic-gate mptr->map_file = fptr;
2047c478bd9Sstevel@tonic-gate fptr->file_ref = 1;
2057c478bd9Sstevel@tonic-gate fptr->file_fd = -1;
206e68846a8SRobert Mustacchi fptr->file_dbgfile = -1;
2077c478bd9Sstevel@tonic-gate P->num_files++;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate * To figure out which map_info_t instances correspond to the mappings
21112e72dbbSrh87107 * for this load object we try to obtain the start and end address
21212e72dbbSrh87107 * for each section of our in-memory ELF image. If successful, we
2137c478bd9Sstevel@tonic-gate * walk down the list of addresses and the list of map_info_t
2147c478bd9Sstevel@tonic-gate * instances in lock step to correctly find the mappings that
2157c478bd9Sstevel@tonic-gate * correspond to this load object.
2167c478bd9Sstevel@tonic-gate */
21712e72dbbSrh87107 if ((fptr->file_saddrs = get_saddrs(P, mptr->map_pmap.pr_vaddr,
21812e72dbbSrh87107 &fptr->file_nsaddrs)) == NULL)
2197c478bd9Sstevel@tonic-gate return (fptr);
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate mp = P->mappings;
222d7755b5aSrh87107 i = 0;
223d7755b5aSrh87107 while (mp < P->mappings + P->map_count && i < fptr->file_nsaddrs) {
224d7755b5aSrh87107
225d7755b5aSrh87107 /* Calculate the start and end of the mapping and section */
226d7755b5aSrh87107 mstart = mp->map_pmap.pr_vaddr;
227d7755b5aSrh87107 mend = mp->map_pmap.pr_vaddr + mp->map_pmap.pr_size;
228d7755b5aSrh87107 sstart = fptr->file_saddrs[i];
229d7755b5aSrh87107 send = fptr->file_saddrs[i + 1];
230d7755b5aSrh87107
231d7755b5aSrh87107 if (mend <= sstart) {
232d7755b5aSrh87107 /* This mapping is below the current section */
233d7755b5aSrh87107 mp++;
234d7755b5aSrh87107 } else if (mstart >= send) {
235d7755b5aSrh87107 /* This mapping is above the current section */
236d7755b5aSrh87107 i += 2;
237d7755b5aSrh87107 } else {
238d7755b5aSrh87107 /* This mapping overlaps the current section */
239d7755b5aSrh87107 if (mp->map_file == NULL) {
240d7755b5aSrh87107 dprintf("file_info_new: associating "
241d7755b5aSrh87107 "segment at %p\n",
242d7755b5aSrh87107 (void *)mp->map_pmap.pr_vaddr);
2437c478bd9Sstevel@tonic-gate mp->map_file = fptr;
2447c478bd9Sstevel@tonic-gate fptr->file_ref++;
2457c478bd9Sstevel@tonic-gate } else {
246d7755b5aSrh87107 dprintf("file_info_new: segment at %p "
247d7755b5aSrh87107 "already associated with %s\n",
248d7755b5aSrh87107 (void *)mp->map_pmap.pr_vaddr,
249d7755b5aSrh87107 (mp == mptr ? "this file" :
250d7755b5aSrh87107 mp->map_file->file_pname));
251d7755b5aSrh87107 }
2527c478bd9Sstevel@tonic-gate mp++;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate return (fptr);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate * Deallocation function for a file_info_t
2617c478bd9Sstevel@tonic-gate */
2627c478bd9Sstevel@tonic-gate static void
file_info_free(struct ps_prochandle * P,file_info_t * fptr)2637c478bd9Sstevel@tonic-gate file_info_free(struct ps_prochandle *P, file_info_t *fptr)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate if (--fptr->file_ref == 0) {
26650d4d24eSRobert Mustacchi list_remove(&P->file_head, fptr);
2677c478bd9Sstevel@tonic-gate if (fptr->file_symtab.sym_elf) {
2687c478bd9Sstevel@tonic-gate (void) elf_end(fptr->file_symtab.sym_elf);
2697c478bd9Sstevel@tonic-gate free(fptr->file_symtab.sym_elfmem);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate if (fptr->file_symtab.sym_byname)
2727c478bd9Sstevel@tonic-gate free(fptr->file_symtab.sym_byname);
2737c478bd9Sstevel@tonic-gate if (fptr->file_symtab.sym_byaddr)
2747c478bd9Sstevel@tonic-gate free(fptr->file_symtab.sym_byaddr);
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate if (fptr->file_dynsym.sym_elf) {
2777c478bd9Sstevel@tonic-gate (void) elf_end(fptr->file_dynsym.sym_elf);
2787c478bd9Sstevel@tonic-gate free(fptr->file_dynsym.sym_elfmem);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate if (fptr->file_dynsym.sym_byname)
2817c478bd9Sstevel@tonic-gate free(fptr->file_dynsym.sym_byname);
2827c478bd9Sstevel@tonic-gate if (fptr->file_dynsym.sym_byaddr)
2837c478bd9Sstevel@tonic-gate free(fptr->file_dynsym.sym_byaddr);
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate if (fptr->file_lo)
2867c478bd9Sstevel@tonic-gate free(fptr->file_lo);
2877c478bd9Sstevel@tonic-gate if (fptr->file_lname)
2887c478bd9Sstevel@tonic-gate free(fptr->file_lname);
289186f7fbfSEdward Pilatowicz if (fptr->file_rname)
290186f7fbfSEdward Pilatowicz free(fptr->file_rname);
2917c478bd9Sstevel@tonic-gate if (fptr->file_elf)
2927c478bd9Sstevel@tonic-gate (void) elf_end(fptr->file_elf);
2937c478bd9Sstevel@tonic-gate if (fptr->file_elfmem != NULL)
2947c478bd9Sstevel@tonic-gate free(fptr->file_elfmem);
2957c478bd9Sstevel@tonic-gate if (fptr->file_fd >= 0)
2967c478bd9Sstevel@tonic-gate (void) close(fptr->file_fd);
297e68846a8SRobert Mustacchi if (fptr->file_dbgelf)
298e68846a8SRobert Mustacchi (void) elf_end(fptr->file_dbgelf);
299e68846a8SRobert Mustacchi if (fptr->file_dbgfile >= 0)
300e68846a8SRobert Mustacchi (void) close(fptr->file_dbgfile);
3017c478bd9Sstevel@tonic-gate ctf_close(fptr->file_ctfp);
3027c478bd9Sstevel@tonic-gate free(fptr->file_ctf_buf);
30312e72dbbSrh87107 if (fptr->file_saddrs)
30412e72dbbSrh87107 free(fptr->file_saddrs);
3057c478bd9Sstevel@tonic-gate free(fptr);
3067c478bd9Sstevel@tonic-gate P->num_files--;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate * Deallocation function for a map_info_t
3127c478bd9Sstevel@tonic-gate */
3137c478bd9Sstevel@tonic-gate static void
map_info_free(struct ps_prochandle * P,map_info_t * mptr)3147c478bd9Sstevel@tonic-gate map_info_free(struct ps_prochandle *P, map_info_t *mptr)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate file_info_t *fptr;
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL) {
3197c478bd9Sstevel@tonic-gate if (fptr->file_map == mptr)
3207c478bd9Sstevel@tonic-gate fptr->file_map = NULL;
3217c478bd9Sstevel@tonic-gate file_info_free(P, fptr);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate if (P->execname && mptr == P->map_exec) {
3247c478bd9Sstevel@tonic-gate free(P->execname);
3257c478bd9Sstevel@tonic-gate P->execname = NULL;
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate if (P->auxv && (mptr == P->map_exec || mptr == P->map_ldso)) {
3287c478bd9Sstevel@tonic-gate free(P->auxv);
3297c478bd9Sstevel@tonic-gate P->auxv = NULL;
3307c478bd9Sstevel@tonic-gate P->nauxv = 0;
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate if (mptr == P->map_exec)
3337c478bd9Sstevel@tonic-gate P->map_exec = NULL;
3347c478bd9Sstevel@tonic-gate if (mptr == P->map_ldso)
3357c478bd9Sstevel@tonic-gate P->map_ldso = NULL;
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate * Call-back function for librtld_db to iterate through all of its shared
3407c478bd9Sstevel@tonic-gate * libraries. We use this to get the load object names for the mappings.
3417c478bd9Sstevel@tonic-gate */
3427c478bd9Sstevel@tonic-gate static int
map_iter(const rd_loadobj_t * lop,void * cd)3437c478bd9Sstevel@tonic-gate map_iter(const rd_loadobj_t *lop, void *cd)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate char buf[PATH_MAX];
3467c478bd9Sstevel@tonic-gate struct ps_prochandle *P = cd;
3477c478bd9Sstevel@tonic-gate map_info_t *mptr;
3487c478bd9Sstevel@tonic-gate file_info_t *fptr;
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate dprintf("encountered rd object at %p\n", (void *)lop->rl_base);
3517c478bd9Sstevel@tonic-gate
3529acbbeafSnn35248 if ((mptr = Paddr2mptr(P, lop->rl_base)) == NULL) {
3539acbbeafSnn35248 dprintf("map_iter: base address doesn't match any mapping\n");
3547c478bd9Sstevel@tonic-gate return (1); /* Base address does not match any mapping */
3559acbbeafSnn35248 }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL &&
3589acbbeafSnn35248 (fptr = file_info_new(P, mptr)) == NULL) {
3599acbbeafSnn35248 dprintf("map_iter: failed to allocate a new file_info_t\n");
3607c478bd9Sstevel@tonic-gate return (1); /* Failed to allocate a new file_info_t */
3619acbbeafSnn35248 }
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate if ((fptr->file_lo == NULL) &&
3647c478bd9Sstevel@tonic-gate (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
3659acbbeafSnn35248 dprintf("map_iter: failed to allocate rd_loadobj_t\n");
3667c478bd9Sstevel@tonic-gate file_info_free(P, fptr);
3677c478bd9Sstevel@tonic-gate return (1); /* Failed to allocate rd_loadobj_t */
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate fptr->file_map = mptr;
3717c478bd9Sstevel@tonic-gate *fptr->file_lo = *lop;
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base;
3747c478bd9Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size;
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate if (fptr->file_lname) {
3777c478bd9Sstevel@tonic-gate free(fptr->file_lname);
3787c478bd9Sstevel@tonic-gate fptr->file_lname = NULL;
379186f7fbfSEdward Pilatowicz fptr->file_lbase = NULL;
380186f7fbfSEdward Pilatowicz }
381186f7fbfSEdward Pilatowicz if (fptr->file_rname) {
382186f7fbfSEdward Pilatowicz free(fptr->file_rname);
383186f7fbfSEdward Pilatowicz fptr->file_rname = NULL;
384186f7fbfSEdward Pilatowicz fptr->file_rbase = NULL;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate if (Pread_string(P, buf, sizeof (buf), lop->rl_nameaddr) > 0) {
3887c478bd9Sstevel@tonic-gate if ((fptr->file_lname = strdup(buf)) != NULL)
3897c478bd9Sstevel@tonic-gate fptr->file_lbase = basename(fptr->file_lname);
3909acbbeafSnn35248 } else {
3919acbbeafSnn35248 dprintf("map_iter: failed to read string at %p\n",
3929acbbeafSnn35248 (void *)lop->rl_nameaddr);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate
395186f7fbfSEdward Pilatowicz if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
396186f7fbfSEdward Pilatowicz ((fptr->file_rname = strdup(buf)) != NULL))
397186f7fbfSEdward Pilatowicz fptr->file_rbase = basename(fptr->file_rname);
398186f7fbfSEdward Pilatowicz
3997c478bd9Sstevel@tonic-gate dprintf("loaded rd object %s lmid %lx\n",
400186f7fbfSEdward Pilatowicz fptr->file_lname ? buf : "<NULL>", lop->rl_lmident);
4017c478bd9Sstevel@tonic-gate return (1);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate static void
map_set(struct ps_prochandle * P,map_info_t * mptr,const char * lname)4057c478bd9Sstevel@tonic-gate map_set(struct ps_prochandle *P, map_info_t *mptr, const char *lname)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate file_info_t *fptr;
4084e4a6245SEdward Pilatowicz char buf[PATH_MAX];
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL &&
4117c478bd9Sstevel@tonic-gate (fptr = file_info_new(P, mptr)) == NULL)
4127c478bd9Sstevel@tonic-gate return; /* Failed to allocate a new file_info_t */
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate fptr->file_map = mptr;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate if ((fptr->file_lo == NULL) &&
4177c478bd9Sstevel@tonic-gate (fptr->file_lo = malloc(sizeof (rd_loadobj_t))) == NULL) {
4187c478bd9Sstevel@tonic-gate file_info_free(P, fptr);
4197c478bd9Sstevel@tonic-gate return; /* Failed to allocate rd_loadobj_t */
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate (void) memset(fptr->file_lo, 0, sizeof (rd_loadobj_t));
4237c478bd9Sstevel@tonic-gate fptr->file_lo->rl_base = mptr->map_pmap.pr_vaddr;
4247c478bd9Sstevel@tonic-gate fptr->file_lo->rl_bend =
4257c478bd9Sstevel@tonic-gate mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base;
4287c478bd9Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size;
4297c478bd9Sstevel@tonic-gate
430186f7fbfSEdward Pilatowicz if ((fptr->file_lname == NULL) &&
4319acbbeafSnn35248 (fptr->file_lname = strdup(lname)) != NULL)
4327c478bd9Sstevel@tonic-gate fptr->file_lbase = basename(fptr->file_lname);
433186f7fbfSEdward Pilatowicz
4344e4a6245SEdward Pilatowicz if ((Pfindmap(P, mptr, buf, sizeof (buf)) != NULL) &&
4354e4a6245SEdward Pilatowicz ((fptr->file_rname = strdup(buf)) != NULL))
4364e4a6245SEdward Pilatowicz fptr->file_rbase = basename(fptr->file_rname);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate static void
load_static_maps(struct ps_prochandle * P)4407c478bd9Sstevel@tonic-gate load_static_maps(struct ps_prochandle *P)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate map_info_t *mptr;
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate * Construct the map for the a.out.
4467c478bd9Sstevel@tonic-gate */
4477c478bd9Sstevel@tonic-gate if ((mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_EXEC)) != NULL)
4487c478bd9Sstevel@tonic-gate map_set(P, mptr, "a.out");
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate * If the dynamic linker exists for this process,
4527c478bd9Sstevel@tonic-gate * construct the map for it.
4537c478bd9Sstevel@tonic-gate */
4547c478bd9Sstevel@tonic-gate if (Pgetauxval(P, AT_BASE) != -1L &&
4557c478bd9Sstevel@tonic-gate (mptr = object_name_to_map(P, PR_LMID_EVERY, PR_OBJ_LDSO)) != NULL)
4567c478bd9Sstevel@tonic-gate map_set(P, mptr, "ld.so.1");
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate
4592a12f85aSJeremy Jones int
Preadmaps(struct ps_prochandle * P,prmap_t ** Pmapp,ssize_t * nmapp)4602a12f85aSJeremy Jones Preadmaps(struct ps_prochandle *P, prmap_t **Pmapp, ssize_t *nmapp)
4612a12f85aSJeremy Jones {
4622a12f85aSJeremy Jones return (P->ops.pop_read_maps(P, Pmapp, nmapp, P->data));
4632a12f85aSJeremy Jones }
4642a12f85aSJeremy Jones
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * Go through all the address space mappings, validating or updating
4677c478bd9Sstevel@tonic-gate * the information already gathered, or gathering new information.
4687c478bd9Sstevel@tonic-gate *
4697c478bd9Sstevel@tonic-gate * This function is only called when we suspect that the mappings have changed
4707c478bd9Sstevel@tonic-gate * because this is the first time we're calling it or because of rtld activity.
4717c478bd9Sstevel@tonic-gate */
4727c478bd9Sstevel@tonic-gate void
Pupdate_maps(struct ps_prochandle * P)4737c478bd9Sstevel@tonic-gate Pupdate_maps(struct ps_prochandle *P)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate prmap_t *Pmap = NULL;
4767c478bd9Sstevel@tonic-gate prmap_t *pmap;
4777c478bd9Sstevel@tonic-gate ssize_t nmap;
4787c478bd9Sstevel@tonic-gate int i;
4797c478bd9Sstevel@tonic-gate uint_t oldmapcount;
4807c478bd9Sstevel@tonic-gate map_info_t *newmap, *newp;
4817c478bd9Sstevel@tonic-gate map_info_t *mptr;
4827c478bd9Sstevel@tonic-gate
483a1b5e537Sbmc if (P->info_valid || P->state == PS_UNDEAD)
4847c478bd9Sstevel@tonic-gate return;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate Preadauxvec(P);
4877c478bd9Sstevel@tonic-gate
4882a12f85aSJeremy Jones if (Preadmaps(P, &Pmap, &nmap) != 0)
4897c478bd9Sstevel@tonic-gate return;
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate if ((newmap = calloc(1, nmap * sizeof (map_info_t))) == NULL)
4927c478bd9Sstevel@tonic-gate return;
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate * We try to merge any file information we may have for existing
4967c478bd9Sstevel@tonic-gate * mappings, to avoid having to rebuild the file info.
4977c478bd9Sstevel@tonic-gate */
4987c478bd9Sstevel@tonic-gate mptr = P->mappings;
4997c478bd9Sstevel@tonic-gate pmap = Pmap;
5007c478bd9Sstevel@tonic-gate newp = newmap;
5017c478bd9Sstevel@tonic-gate oldmapcount = P->map_count;
5027c478bd9Sstevel@tonic-gate for (i = 0; i < nmap; i++, pmap++, newp++) {
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate if (oldmapcount == 0) {
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate * We've exhausted all the old mappings. Every new
5077c478bd9Sstevel@tonic-gate * mapping should be added.
5087c478bd9Sstevel@tonic-gate */
5097c478bd9Sstevel@tonic-gate newp->map_pmap = *pmap;
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate } else if (pmap->pr_vaddr == mptr->map_pmap.pr_vaddr &&
5127c478bd9Sstevel@tonic-gate pmap->pr_size == mptr->map_pmap.pr_size &&
5137c478bd9Sstevel@tonic-gate pmap->pr_offset == mptr->map_pmap.pr_offset &&
5147c478bd9Sstevel@tonic-gate (pmap->pr_mflags & ~(MA_BREAK | MA_STACK)) ==
5157c478bd9Sstevel@tonic-gate (mptr->map_pmap.pr_mflags & ~(MA_BREAK | MA_STACK)) &&
5167c478bd9Sstevel@tonic-gate pmap->pr_pagesize == mptr->map_pmap.pr_pagesize &&
5177c478bd9Sstevel@tonic-gate pmap->pr_shmid == mptr->map_pmap.pr_shmid &&
5187c478bd9Sstevel@tonic-gate strcmp(pmap->pr_mapname, mptr->map_pmap.pr_mapname) == 0) {
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate * This mapping matches exactly. Copy over the old
5227c478bd9Sstevel@tonic-gate * mapping, taking care to get the latest flags.
5237c478bd9Sstevel@tonic-gate * Make sure the associated file_info_t is updated
5247c478bd9Sstevel@tonic-gate * appropriately.
5257c478bd9Sstevel@tonic-gate */
5267c478bd9Sstevel@tonic-gate *newp = *mptr;
5277c478bd9Sstevel@tonic-gate if (P->map_exec == mptr)
5287c478bd9Sstevel@tonic-gate P->map_exec = newp;
5297c478bd9Sstevel@tonic-gate if (P->map_ldso == mptr)
5307c478bd9Sstevel@tonic-gate P->map_ldso = newp;
5317c478bd9Sstevel@tonic-gate newp->map_pmap.pr_mflags = pmap->pr_mflags;
5327c478bd9Sstevel@tonic-gate if (mptr->map_file != NULL &&
5337c478bd9Sstevel@tonic-gate mptr->map_file->file_map == mptr)
5347c478bd9Sstevel@tonic-gate mptr->map_file->file_map = newp;
5357c478bd9Sstevel@tonic-gate oldmapcount--;
5367c478bd9Sstevel@tonic-gate mptr++;
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate } else if (pmap->pr_vaddr + pmap->pr_size >
5397c478bd9Sstevel@tonic-gate mptr->map_pmap.pr_vaddr) {
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate * The old mapping doesn't exist any more, remove it
5437c478bd9Sstevel@tonic-gate * from the list.
5447c478bd9Sstevel@tonic-gate */
5457c478bd9Sstevel@tonic-gate map_info_free(P, mptr);
5467c478bd9Sstevel@tonic-gate oldmapcount--;
5477c478bd9Sstevel@tonic-gate i--;
5487c478bd9Sstevel@tonic-gate newp--;
5497c478bd9Sstevel@tonic-gate pmap--;
5507c478bd9Sstevel@tonic-gate mptr++;
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate } else {
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate * This is a new mapping, add it directly.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate newp->map_pmap = *pmap;
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate * Free any old maps
5637c478bd9Sstevel@tonic-gate */
5647c478bd9Sstevel@tonic-gate while (oldmapcount) {
5657c478bd9Sstevel@tonic-gate map_info_free(P, mptr);
5667c478bd9Sstevel@tonic-gate oldmapcount--;
5677c478bd9Sstevel@tonic-gate mptr++;
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate free(Pmap);
5717c478bd9Sstevel@tonic-gate if (P->mappings != NULL)
5727c478bd9Sstevel@tonic-gate free(P->mappings);
5737c478bd9Sstevel@tonic-gate P->mappings = newmap;
5747c478bd9Sstevel@tonic-gate P->map_count = P->map_alloc = nmap;
5757c478bd9Sstevel@tonic-gate P->info_valid = 1;
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate * Consult librtld_db to get the load object
5797c478bd9Sstevel@tonic-gate * names for all of the shared libraries.
5807c478bd9Sstevel@tonic-gate */
5817c478bd9Sstevel@tonic-gate if (P->rap != NULL)
5827c478bd9Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate * Update all of the mappings and rtld_db as if by Pupdate_maps(), and then
5877c478bd9Sstevel@tonic-gate * forcibly cache all of the symbol tables associated with all object files.
5887c478bd9Sstevel@tonic-gate */
5897c478bd9Sstevel@tonic-gate void
Pupdate_syms(struct ps_prochandle * P)5907c478bd9Sstevel@tonic-gate Pupdate_syms(struct ps_prochandle *P)
5917c478bd9Sstevel@tonic-gate {
5924bfb3cb6Srh87107 file_info_t *fptr;
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate Pupdate_maps(P);
5957c478bd9Sstevel@tonic-gate
59650d4d24eSRobert Mustacchi for (fptr = list_head(&P->file_head); fptr != NULL;
59750d4d24eSRobert Mustacchi fptr = list_next(&P->file_head, fptr)) {
5987c478bd9Sstevel@tonic-gate Pbuild_file_symtab(P, fptr);
5997c478bd9Sstevel@tonic-gate (void) Pbuild_file_ctf(P, fptr);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate * Return the librtld_db agent handle for the victim process.
6057c478bd9Sstevel@tonic-gate * The handle will become invalid at the next successful exec() and the
6067c478bd9Sstevel@tonic-gate * client (caller of proc_rd_agent()) must not use it beyond that point.
6077c478bd9Sstevel@tonic-gate * If the process is already dead, we've already tried our best to
6087c478bd9Sstevel@tonic-gate * create the agent during core file initialization.
6097c478bd9Sstevel@tonic-gate */
6107c478bd9Sstevel@tonic-gate rd_agent_t *
Prd_agent(struct ps_prochandle * P)6117c478bd9Sstevel@tonic-gate Prd_agent(struct ps_prochandle *P)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate if (P->rap == NULL && P->state != PS_DEAD && P->state != PS_IDLE) {
6147c478bd9Sstevel@tonic-gate Pupdate_maps(P);
6157c478bd9Sstevel@tonic-gate if (P->num_files == 0)
6167c478bd9Sstevel@tonic-gate load_static_maps(P);
6177c478bd9Sstevel@tonic-gate rd_log(_libproc_debug);
6187c478bd9Sstevel@tonic-gate if ((P->rap = rd_new(P)) != NULL)
6197c478bd9Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P);
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate return (P->rap);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate * Return the prmap_t structure containing 'addr', but only if it
6267c478bd9Sstevel@tonic-gate * is in the dynamic linker's link map and is the text section.
6277c478bd9Sstevel@tonic-gate */
6287c478bd9Sstevel@tonic-gate const prmap_t *
Paddr_to_text_map(struct ps_prochandle * P,uintptr_t addr)6297c478bd9Sstevel@tonic-gate Paddr_to_text_map(struct ps_prochandle *P, uintptr_t addr)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate map_info_t *mptr;
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate if (!P->info_valid)
6347c478bd9Sstevel@tonic-gate Pupdate_maps(P);
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL) {
6377c478bd9Sstevel@tonic-gate file_info_t *fptr = build_map_symtab(P, mptr);
6387c478bd9Sstevel@tonic-gate const prmap_t *pmp = &mptr->map_pmap;
6397c478bd9Sstevel@tonic-gate
640d7755b5aSrh87107 /*
641d7755b5aSrh87107 * Assume that if rl_data_base is NULL, it means that no
642d7755b5aSrh87107 * data section was found for this load object, and that
643d7755b5aSrh87107 * a section must be text. Otherwise, a section will be
644d7755b5aSrh87107 * text unless it ends above the start of the data
645d7755b5aSrh87107 * section.
646d7755b5aSrh87107 */
6477c478bd9Sstevel@tonic-gate if (fptr != NULL && fptr->file_lo != NULL &&
648c75682cdSToomas Soome (fptr->file_lo->rl_data_base == (uintptr_t)NULL ||
6490a9ca722SRobert Harris pmp->pr_vaddr + pmp->pr_size <=
650d7755b5aSrh87107 fptr->file_lo->rl_data_base))
6517c478bd9Sstevel@tonic-gate return (pmp);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate return (NULL);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate * Return the prmap_t structure containing 'addr' (no restrictions on
6597c478bd9Sstevel@tonic-gate * the type of mapping).
6607c478bd9Sstevel@tonic-gate */
6617c478bd9Sstevel@tonic-gate const prmap_t *
Paddr_to_map(struct ps_prochandle * P,uintptr_t addr)6627c478bd9Sstevel@tonic-gate Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate map_info_t *mptr;
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate if (!P->info_valid)
6677c478bd9Sstevel@tonic-gate Pupdate_maps(P);
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL)
6707c478bd9Sstevel@tonic-gate return (&mptr->map_pmap);
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate return (NULL);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate * Convert a full or partial load object name to the prmap_t for its
6777c478bd9Sstevel@tonic-gate * corresponding primary text mapping.
6787c478bd9Sstevel@tonic-gate */
6797c478bd9Sstevel@tonic-gate const prmap_t *
Plmid_to_map(struct ps_prochandle * P,Lmid_t lmid,const char * name)6807c478bd9Sstevel@tonic-gate Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate map_info_t *mptr;
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate if (name == PR_OBJ_EVERY)
6857c478bd9Sstevel@tonic-gate return (NULL); /* A reasonable mistake */
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
6887c478bd9Sstevel@tonic-gate return (&mptr->map_pmap);
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate return (NULL);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate const prmap_t *
Pname_to_map(struct ps_prochandle * P,const char * name)6947c478bd9Sstevel@tonic-gate Pname_to_map(struct ps_prochandle *P, const char *name)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate return (Plmid_to_map(P, PR_LMID_EVERY, name));
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate
6997c478bd9Sstevel@tonic-gate const rd_loadobj_t *
Paddr_to_loadobj(struct ps_prochandle * P,uintptr_t addr)7007c478bd9Sstevel@tonic-gate Paddr_to_loadobj(struct ps_prochandle *P, uintptr_t addr)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate map_info_t *mptr;
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate if (!P->info_valid)
7057c478bd9Sstevel@tonic-gate Pupdate_maps(P);
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL)
7087c478bd9Sstevel@tonic-gate return (NULL);
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate * By building the symbol table, we implicitly bring the PLT
7127c478bd9Sstevel@tonic-gate * information up to date in the load object.
7137c478bd9Sstevel@tonic-gate */
7147c478bd9Sstevel@tonic-gate (void) build_map_symtab(P, mptr);
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate return (mptr->map_file->file_lo);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate const rd_loadobj_t *
Plmid_to_loadobj(struct ps_prochandle * P,Lmid_t lmid,const char * name)7207c478bd9Sstevel@tonic-gate Plmid_to_loadobj(struct ps_prochandle *P, Lmid_t lmid, const char *name)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate map_info_t *mptr;
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate if (name == PR_OBJ_EVERY)
7257c478bd9Sstevel@tonic-gate return (NULL);
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) == NULL)
7287c478bd9Sstevel@tonic-gate return (NULL);
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate * By building the symbol table, we implicitly bring the PLT
7327c478bd9Sstevel@tonic-gate * information up to date in the load object.
7337c478bd9Sstevel@tonic-gate */
7347c478bd9Sstevel@tonic-gate (void) build_map_symtab(P, mptr);
7357c478bd9Sstevel@tonic-gate
7367c478bd9Sstevel@tonic-gate return (mptr->map_file->file_lo);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate const rd_loadobj_t *
Pname_to_loadobj(struct ps_prochandle * P,const char * name)7407c478bd9Sstevel@tonic-gate Pname_to_loadobj(struct ps_prochandle *P, const char *name)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate return (Plmid_to_loadobj(P, PR_LMID_EVERY, name));
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate ctf_file_t *
Pbuild_file_ctf(struct ps_prochandle * P,file_info_t * fptr)7467c478bd9Sstevel@tonic-gate Pbuild_file_ctf(struct ps_prochandle *P, file_info_t *fptr)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate ctf_sect_t ctdata, symtab, strtab;
7497c478bd9Sstevel@tonic-gate sym_tbl_t *symp;
7507c478bd9Sstevel@tonic-gate int err;
7517c478bd9Sstevel@tonic-gate
7527c478bd9Sstevel@tonic-gate if (fptr->file_ctfp != NULL)
7537c478bd9Sstevel@tonic-gate return (fptr->file_ctfp);
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate Pbuild_file_symtab(P, fptr);
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate if (fptr->file_ctf_size == 0)
7587c478bd9Sstevel@tonic-gate return (NULL);
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate symp = fptr->file_ctf_dyn ? &fptr->file_dynsym : &fptr->file_symtab;
761d51e9074Sab196087 if (symp->sym_data_pri == NULL)
7627c478bd9Sstevel@tonic-gate return (NULL);
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate * The buffer may alread be allocated if this is a core file that
7667c478bd9Sstevel@tonic-gate * contained CTF data for this file.
7677c478bd9Sstevel@tonic-gate */
7687c478bd9Sstevel@tonic-gate if (fptr->file_ctf_buf == NULL) {
7697c478bd9Sstevel@tonic-gate fptr->file_ctf_buf = malloc(fptr->file_ctf_size);
7707c478bd9Sstevel@tonic-gate if (fptr->file_ctf_buf == NULL) {
7717c478bd9Sstevel@tonic-gate dprintf("failed to allocate ctf buffer\n");
7727c478bd9Sstevel@tonic-gate return (NULL);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate
7757c478bd9Sstevel@tonic-gate if (pread(fptr->file_fd, fptr->file_ctf_buf,
7767c478bd9Sstevel@tonic-gate fptr->file_ctf_size, fptr->file_ctf_off) !=
7777c478bd9Sstevel@tonic-gate fptr->file_ctf_size) {
7787c478bd9Sstevel@tonic-gate free(fptr->file_ctf_buf);
7797c478bd9Sstevel@tonic-gate fptr->file_ctf_buf = NULL;
7807c478bd9Sstevel@tonic-gate dprintf("failed to read ctf data\n");
7817c478bd9Sstevel@tonic-gate return (NULL);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate ctdata.cts_name = ".SUNW_ctf";
7867c478bd9Sstevel@tonic-gate ctdata.cts_type = SHT_PROGBITS;
7877c478bd9Sstevel@tonic-gate ctdata.cts_flags = 0;
7887c478bd9Sstevel@tonic-gate ctdata.cts_data = fptr->file_ctf_buf;
7897c478bd9Sstevel@tonic-gate ctdata.cts_size = fptr->file_ctf_size;
7907c478bd9Sstevel@tonic-gate ctdata.cts_entsize = 1;
7917c478bd9Sstevel@tonic-gate ctdata.cts_offset = 0;
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate symtab.cts_name = fptr->file_ctf_dyn ? ".dynsym" : ".symtab";
794d51e9074Sab196087 symtab.cts_type = symp->sym_hdr_pri.sh_type;
795d51e9074Sab196087 symtab.cts_flags = symp->sym_hdr_pri.sh_flags;
796d51e9074Sab196087 symtab.cts_data = symp->sym_data_pri->d_buf;
797d51e9074Sab196087 symtab.cts_size = symp->sym_hdr_pri.sh_size;
798d51e9074Sab196087 symtab.cts_entsize = symp->sym_hdr_pri.sh_entsize;
799d51e9074Sab196087 symtab.cts_offset = symp->sym_hdr_pri.sh_offset;
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate strtab.cts_name = fptr->file_ctf_dyn ? ".dynstr" : ".strtab";
8027c478bd9Sstevel@tonic-gate strtab.cts_type = symp->sym_strhdr.sh_type;
8037c478bd9Sstevel@tonic-gate strtab.cts_flags = symp->sym_strhdr.sh_flags;
8047c478bd9Sstevel@tonic-gate strtab.cts_data = symp->sym_strs;
8057c478bd9Sstevel@tonic-gate strtab.cts_size = symp->sym_strhdr.sh_size;
8067c478bd9Sstevel@tonic-gate strtab.cts_entsize = symp->sym_strhdr.sh_entsize;
8077c478bd9Sstevel@tonic-gate strtab.cts_offset = symp->sym_strhdr.sh_offset;
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate fptr->file_ctfp = ctf_bufopen(&ctdata, &symtab, &strtab, &err);
8107c478bd9Sstevel@tonic-gate if (fptr->file_ctfp == NULL) {
811327644c7SKrishnendu Sadhukhan - Sun Microsystems dprintf("ctf_bufopen() failed, error code %d\n", err);
8127c478bd9Sstevel@tonic-gate free(fptr->file_ctf_buf);
8137c478bd9Sstevel@tonic-gate fptr->file_ctf_buf = NULL;
8147c478bd9Sstevel@tonic-gate return (NULL);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate
8177c478bd9Sstevel@tonic-gate dprintf("loaded %lu bytes of CTF data for %s\n",
8187c478bd9Sstevel@tonic-gate (ulong_t)fptr->file_ctf_size, fptr->file_pname);
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate return (fptr->file_ctfp);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate ctf_file_t *
Paddr_to_ctf(struct ps_prochandle * P,uintptr_t addr)8247c478bd9Sstevel@tonic-gate Paddr_to_ctf(struct ps_prochandle *P, uintptr_t addr)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate map_info_t *mptr;
8277c478bd9Sstevel@tonic-gate file_info_t *fptr;
8287c478bd9Sstevel@tonic-gate
8297c478bd9Sstevel@tonic-gate if (!P->info_valid)
8307c478bd9Sstevel@tonic-gate Pupdate_maps(P);
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL ||
8337c478bd9Sstevel@tonic-gate (fptr = mptr->map_file) == NULL)
8347c478bd9Sstevel@tonic-gate return (NULL);
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate return (Pbuild_file_ctf(P, fptr));
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate ctf_file_t *
Plmid_to_ctf(struct ps_prochandle * P,Lmid_t lmid,const char * name)8407c478bd9Sstevel@tonic-gate Plmid_to_ctf(struct ps_prochandle *P, Lmid_t lmid, const char *name)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate map_info_t *mptr;
843936dcb54SRobert Mustacchi file_info_t *fptr = NULL;
8447c478bd9Sstevel@tonic-gate
8457c478bd9Sstevel@tonic-gate if (name == PR_OBJ_EVERY)
8467c478bd9Sstevel@tonic-gate return (NULL);
8477c478bd9Sstevel@tonic-gate
848936dcb54SRobert Mustacchi /*
849936dcb54SRobert Mustacchi * While most idle files are all ELF objects, not all of them have
850936dcb54SRobert Mustacchi * mapping information available. There's nothing which would make
851936dcb54SRobert Mustacchi * sense to fake up for ET_REL. Instead, if we're being asked for their
852936dcb54SRobert Mustacchi * executable object and we know that the information is valid and they
853936dcb54SRobert Mustacchi * only have a single file, we jump straight to that file pointer.
854936dcb54SRobert Mustacchi */
855936dcb54SRobert Mustacchi if (P->state == PS_IDLE && name == PR_OBJ_EXEC && P->info_valid == 1 &&
856936dcb54SRobert Mustacchi P->num_files == 1 && P->mappings == NULL) {
85750d4d24eSRobert Mustacchi fptr = list_head(&P->file_head);
858936dcb54SRobert Mustacchi }
859936dcb54SRobert Mustacchi
860936dcb54SRobert Mustacchi if (fptr == NULL) {
8617c478bd9Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, name)) == NULL ||
8627c478bd9Sstevel@tonic-gate (fptr = mptr->map_file) == NULL)
8637c478bd9Sstevel@tonic-gate return (NULL);
864936dcb54SRobert Mustacchi }
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate return (Pbuild_file_ctf(P, fptr));
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate
8697c478bd9Sstevel@tonic-gate ctf_file_t *
Pname_to_ctf(struct ps_prochandle * P,const char * name)8707c478bd9Sstevel@tonic-gate Pname_to_ctf(struct ps_prochandle *P, const char *name)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate return (Plmid_to_ctf(P, PR_LMID_EVERY, name));
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate void
Preadauxvec(struct ps_prochandle * P)8767c478bd9Sstevel@tonic-gate Preadauxvec(struct ps_prochandle *P)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate if (P->auxv != NULL) {
8797c478bd9Sstevel@tonic-gate free(P->auxv);
8807c478bd9Sstevel@tonic-gate P->auxv = NULL;
8817c478bd9Sstevel@tonic-gate P->nauxv = 0;
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate
8842a12f85aSJeremy Jones P->ops.pop_read_aux(P, &P->auxv, &P->nauxv, P->data);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate /*
8887c478bd9Sstevel@tonic-gate * Return a requested element from the process's aux vector.
8897c478bd9Sstevel@tonic-gate * Return -1 on failure (this is adequate for our purposes).
8907c478bd9Sstevel@tonic-gate */
8917c478bd9Sstevel@tonic-gate long
Pgetauxval(struct ps_prochandle * P,int type)8927c478bd9Sstevel@tonic-gate Pgetauxval(struct ps_prochandle *P, int type)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate auxv_t *auxv;
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate if (P->auxv == NULL)
8977c478bd9Sstevel@tonic-gate Preadauxvec(P);
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate if (P->auxv == NULL)
9007c478bd9Sstevel@tonic-gate return (-1);
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate for (auxv = P->auxv; auxv->a_type != AT_NULL; auxv++) {
9037c478bd9Sstevel@tonic-gate if (auxv->a_type == type)
9047c478bd9Sstevel@tonic-gate return (auxv->a_un.a_val);
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate
9077c478bd9Sstevel@tonic-gate return (-1);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate * Return a pointer to our internal copy of the process's aux vector.
9127c478bd9Sstevel@tonic-gate * The caller should not hold on to this pointer across any libproc calls.
9137c478bd9Sstevel@tonic-gate */
9147c478bd9Sstevel@tonic-gate const auxv_t *
Pgetauxvec(struct ps_prochandle * P)9157c478bd9Sstevel@tonic-gate Pgetauxvec(struct ps_prochandle *P)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate static const auxv_t empty = { AT_NULL, 0L };
9187c478bd9Sstevel@tonic-gate
9197c478bd9Sstevel@tonic-gate if (P->auxv == NULL)
9207c478bd9Sstevel@tonic-gate Preadauxvec(P);
9217c478bd9Sstevel@tonic-gate
9227c478bd9Sstevel@tonic-gate if (P->auxv == NULL)
9237c478bd9Sstevel@tonic-gate return (&empty);
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate return (P->auxv);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate
9287c478bd9Sstevel@tonic-gate /*
92912e72dbbSrh87107 * Return 1 if the given mapping corresponds to the given file_info_t's
93012e72dbbSrh87107 * load object; return 0 otherwise.
93112e72dbbSrh87107 */
93212e72dbbSrh87107 static int
is_mapping_in_file(struct ps_prochandle * P,map_info_t * mptr,file_info_t * fptr)93312e72dbbSrh87107 is_mapping_in_file(struct ps_prochandle *P, map_info_t *mptr, file_info_t *fptr)
93412e72dbbSrh87107 {
93512e72dbbSrh87107 prmap_t *pmap = &mptr->map_pmap;
93612e72dbbSrh87107 rd_loadobj_t *lop = fptr->file_lo;
93712e72dbbSrh87107 uint_t i;
938d7755b5aSrh87107 uintptr_t mstart, mend, sstart, send;
93912e72dbbSrh87107
94012e72dbbSrh87107 /*
94112e72dbbSrh87107 * We can get for free the start address of the text and data
94212e72dbbSrh87107 * sections of the load object. Start by seeing if the mapping
94312e72dbbSrh87107 * encloses either of these.
94412e72dbbSrh87107 */
94512e72dbbSrh87107 if ((pmap->pr_vaddr <= lop->rl_base &&
94612e72dbbSrh87107 lop->rl_base < pmap->pr_vaddr + pmap->pr_size) ||
94712e72dbbSrh87107 (pmap->pr_vaddr <= lop->rl_data_base &&
94812e72dbbSrh87107 lop->rl_data_base < pmap->pr_vaddr + pmap->pr_size))
94912e72dbbSrh87107 return (1);
95012e72dbbSrh87107
95112e72dbbSrh87107 /*
95212e72dbbSrh87107 * It's still possible that this mapping correponds to the load
95312e72dbbSrh87107 * object. Consider the example of a mapping whose start and end
95412e72dbbSrh87107 * addresses correspond to those of the load object's text section.
95512e72dbbSrh87107 * If the mapping splits, e.g. as a result of a segment demotion,
95612e72dbbSrh87107 * then although both mappings are still backed by the same section,
95712e72dbbSrh87107 * only one will be seen to enclose that section's start address.
95812e72dbbSrh87107 * Thus, to be rigorous, we ask not whether this mapping encloses
95912e72dbbSrh87107 * the start of a section, but whether there exists a section that
960d7755b5aSrh87107 * overlaps this mapping.
96112e72dbbSrh87107 *
96212e72dbbSrh87107 * If we don't already have the section addresses, and we successfully
96312e72dbbSrh87107 * get them, then we cache them in case we come here again.
96412e72dbbSrh87107 */
96512e72dbbSrh87107 if (fptr->file_saddrs == NULL &&
96612e72dbbSrh87107 (fptr->file_saddrs = get_saddrs(P,
96712e72dbbSrh87107 fptr->file_map->map_pmap.pr_vaddr, &fptr->file_nsaddrs)) == NULL)
96812e72dbbSrh87107 return (0);
969d7755b5aSrh87107
970d7755b5aSrh87107 mstart = mptr->map_pmap.pr_vaddr;
971d7755b5aSrh87107 mend = mptr->map_pmap.pr_vaddr + mptr->map_pmap.pr_size;
97212e72dbbSrh87107 for (i = 0; i < fptr->file_nsaddrs; i += 2) {
973d7755b5aSrh87107 /* Does this section overlap the mapping? */
974d7755b5aSrh87107 sstart = fptr->file_saddrs[i];
975d7755b5aSrh87107 send = fptr->file_saddrs[i + 1];
976d7755b5aSrh87107 if (!(mend <= sstart || mstart >= send))
97712e72dbbSrh87107 return (1);
97812e72dbbSrh87107 }
97912e72dbbSrh87107
98012e72dbbSrh87107 return (0);
98112e72dbbSrh87107 }
98212e72dbbSrh87107
98312e72dbbSrh87107 /*
9847c478bd9Sstevel@tonic-gate * Find or build the symbol table for the given mapping.
9857c478bd9Sstevel@tonic-gate */
9867c478bd9Sstevel@tonic-gate static file_info_t *
build_map_symtab(struct ps_prochandle * P,map_info_t * mptr)9877c478bd9Sstevel@tonic-gate build_map_symtab(struct ps_prochandle *P, map_info_t *mptr)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate prmap_t *pmap = &mptr->map_pmap;
9907c478bd9Sstevel@tonic-gate file_info_t *fptr;
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL) {
9937c478bd9Sstevel@tonic-gate Pbuild_file_symtab(P, fptr);
9947c478bd9Sstevel@tonic-gate return (fptr);
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate
9977c478bd9Sstevel@tonic-gate if (pmap->pr_mapname[0] == '\0')
9987c478bd9Sstevel@tonic-gate return (NULL);
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate /*
10017c478bd9Sstevel@tonic-gate * Attempt to find a matching file.
10027c478bd9Sstevel@tonic-gate * (A file can be mapped at several different addresses.)
10037c478bd9Sstevel@tonic-gate */
100450d4d24eSRobert Mustacchi for (fptr = list_head(&P->file_head); fptr != NULL;
100550d4d24eSRobert Mustacchi fptr = list_next(&P->file_head, fptr)) {
10067c478bd9Sstevel@tonic-gate if (strcmp(fptr->file_pname, pmap->pr_mapname) == 0 &&
100712e72dbbSrh87107 fptr->file_lo && is_mapping_in_file(P, mptr, fptr)) {
10087c478bd9Sstevel@tonic-gate mptr->map_file = fptr;
10097c478bd9Sstevel@tonic-gate fptr->file_ref++;
10107c478bd9Sstevel@tonic-gate Pbuild_file_symtab(P, fptr);
10117c478bd9Sstevel@tonic-gate return (fptr);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate /*
10167c478bd9Sstevel@tonic-gate * If we need to create a new file_info structure, iterate
10177c478bd9Sstevel@tonic-gate * through the load objects in order to attempt to connect
10187c478bd9Sstevel@tonic-gate * this new file with its primary text mapping. We again
10197c478bd9Sstevel@tonic-gate * need to handle ld.so as a special case because we need
10207c478bd9Sstevel@tonic-gate * to be able to bootstrap librtld_db.
10217c478bd9Sstevel@tonic-gate */
10227c478bd9Sstevel@tonic-gate if ((fptr = file_info_new(P, mptr)) == NULL)
10237c478bd9Sstevel@tonic-gate return (NULL);
10247c478bd9Sstevel@tonic-gate
10257c478bd9Sstevel@tonic-gate if (P->map_ldso != mptr) {
10267c478bd9Sstevel@tonic-gate if (P->rap != NULL)
10277c478bd9Sstevel@tonic-gate (void) rd_loadobj_iter(P->rap, map_iter, P);
10287c478bd9Sstevel@tonic-gate else
10297c478bd9Sstevel@tonic-gate (void) Prd_agent(P);
10307c478bd9Sstevel@tonic-gate } else {
10317c478bd9Sstevel@tonic-gate fptr->file_map = mptr;
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate
10347c478bd9Sstevel@tonic-gate /*
10357c478bd9Sstevel@tonic-gate * If librtld_db wasn't able to help us connect the file to a primary
10367c478bd9Sstevel@tonic-gate * text mapping, set file_map to the current mapping because we require
10377c478bd9Sstevel@tonic-gate * fptr->file_map to be set in Pbuild_file_symtab. librtld_db may be
10387c478bd9Sstevel@tonic-gate * unaware of what's going on in the rare case that a legitimate ELF
10397c478bd9Sstevel@tonic-gate * file has been mmap(2)ed into the process address space *without*
10409acbbeafSnn35248 * the use of dlopen(3x).
10417c478bd9Sstevel@tonic-gate */
10427c478bd9Sstevel@tonic-gate if (fptr->file_map == NULL)
10437c478bd9Sstevel@tonic-gate fptr->file_map = mptr;
10447c478bd9Sstevel@tonic-gate
10457c478bd9Sstevel@tonic-gate Pbuild_file_symtab(P, fptr);
10467c478bd9Sstevel@tonic-gate
10477c478bd9Sstevel@tonic-gate return (fptr);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate
10507c478bd9Sstevel@tonic-gate static int
read_ehdr32(struct ps_prochandle * P,Elf32_Ehdr * ehdr,uint_t * phnum,uintptr_t addr)105130da1432Sahl read_ehdr32(struct ps_prochandle *P, Elf32_Ehdr *ehdr, uint_t *phnum,
105230da1432Sahl uintptr_t addr)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate if (Pread(P, ehdr, sizeof (*ehdr), addr) != sizeof (*ehdr))
10557c478bd9Sstevel@tonic-gate return (-1);
10567c478bd9Sstevel@tonic-gate
10577c478bd9Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
10587c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
10597c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
10607c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
10617c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
10627c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
10637c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
10647c478bd9Sstevel@tonic-gate #else
10657c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
10667c478bd9Sstevel@tonic-gate #endif
10677c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_VERSION] != EV_CURRENT)
10687c478bd9Sstevel@tonic-gate return (-1);
10697c478bd9Sstevel@tonic-gate
107030da1432Sahl if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
107130da1432Sahl Elf32_Shdr shdr0;
107230da1432Sahl
107330da1432Sahl if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
107430da1432Sahl Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
107530da1432Sahl sizeof (shdr0))
107630da1432Sahl return (-1);
107730da1432Sahl
107830da1432Sahl if (shdr0.sh_info != 0)
107930da1432Sahl *phnum = shdr0.sh_info;
108030da1432Sahl }
108130da1432Sahl
10827c478bd9Sstevel@tonic-gate return (0);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate
10857c478bd9Sstevel@tonic-gate static int
read_dynamic_phdr32(struct ps_prochandle * P,const Elf32_Ehdr * ehdr,uint_t phnum,Elf32_Phdr * phdr,uintptr_t addr)10867c478bd9Sstevel@tonic-gate read_dynamic_phdr32(struct ps_prochandle *P, const Elf32_Ehdr *ehdr,
108730da1432Sahl uint_t phnum, Elf32_Phdr *phdr, uintptr_t addr)
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate uint_t i;
10907c478bd9Sstevel@tonic-gate
109130da1432Sahl for (i = 0; i < phnum; i++) {
10927c478bd9Sstevel@tonic-gate uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
10937c478bd9Sstevel@tonic-gate if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
10947c478bd9Sstevel@tonic-gate return (-1);
10957c478bd9Sstevel@tonic-gate
10967c478bd9Sstevel@tonic-gate if (phdr->p_type == PT_DYNAMIC)
10977c478bd9Sstevel@tonic-gate return (0);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate
11007c478bd9Sstevel@tonic-gate return (-1);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate #ifdef _LP64
11047c478bd9Sstevel@tonic-gate static int
read_ehdr64(struct ps_prochandle * P,Elf64_Ehdr * ehdr,uint_t * phnum,uintptr_t addr)110530da1432Sahl read_ehdr64(struct ps_prochandle *P, Elf64_Ehdr *ehdr, uint_t *phnum,
110630da1432Sahl uintptr_t addr)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate if (Pread(P, ehdr, sizeof (Elf64_Ehdr), addr) != sizeof (Elf64_Ehdr))
11097c478bd9Sstevel@tonic-gate return (-1);
11107c478bd9Sstevel@tonic-gate
11117c478bd9Sstevel@tonic-gate if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
11127c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
11137c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
11147c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
11157c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
11167c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
11177c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2MSB ||
11187c478bd9Sstevel@tonic-gate #else
11197c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
11207c478bd9Sstevel@tonic-gate #endif
11217c478bd9Sstevel@tonic-gate ehdr->e_ident[EI_VERSION] != EV_CURRENT)
11227c478bd9Sstevel@tonic-gate return (-1);
11237c478bd9Sstevel@tonic-gate
112430da1432Sahl if ((*phnum = ehdr->e_phnum) == PN_XNUM) {
112530da1432Sahl Elf64_Shdr shdr0;
112630da1432Sahl
112730da1432Sahl if (ehdr->e_shoff == 0 || ehdr->e_shentsize < sizeof (shdr0) ||
112830da1432Sahl Pread(P, &shdr0, sizeof (shdr0), addr + ehdr->e_shoff) !=
112930da1432Sahl sizeof (shdr0))
113030da1432Sahl return (-1);
113130da1432Sahl
113230da1432Sahl if (shdr0.sh_info != 0)
113330da1432Sahl *phnum = shdr0.sh_info;
113430da1432Sahl }
113530da1432Sahl
11367c478bd9Sstevel@tonic-gate return (0);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate
11397c478bd9Sstevel@tonic-gate static int
read_dynamic_phdr64(struct ps_prochandle * P,const Elf64_Ehdr * ehdr,uint_t phnum,Elf64_Phdr * phdr,uintptr_t addr)11407c478bd9Sstevel@tonic-gate read_dynamic_phdr64(struct ps_prochandle *P, const Elf64_Ehdr *ehdr,
114130da1432Sahl uint_t phnum, Elf64_Phdr *phdr, uintptr_t addr)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate uint_t i;
11447c478bd9Sstevel@tonic-gate
114530da1432Sahl for (i = 0; i < phnum; i++) {
11467c478bd9Sstevel@tonic-gate uintptr_t a = addr + ehdr->e_phoff + i * ehdr->e_phentsize;
11477c478bd9Sstevel@tonic-gate if (Pread(P, phdr, sizeof (*phdr), a) != sizeof (*phdr))
11487c478bd9Sstevel@tonic-gate return (-1);
11497c478bd9Sstevel@tonic-gate
11507c478bd9Sstevel@tonic-gate if (phdr->p_type == PT_DYNAMIC)
11517c478bd9Sstevel@tonic-gate return (0);
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate
11547c478bd9Sstevel@tonic-gate return (-1);
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate #endif /* _LP64 */
11577c478bd9Sstevel@tonic-gate
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate * The text segment for each load object contains the elf header and
11607c478bd9Sstevel@tonic-gate * program headers. We can use this information to determine if the
11617c478bd9Sstevel@tonic-gate * file that corresponds to the load object is the same file that
11627c478bd9Sstevel@tonic-gate * was loaded into the process's address space. There can be a discrepency
11637c478bd9Sstevel@tonic-gate * if a file is recompiled after the process is started or if the target
11647c478bd9Sstevel@tonic-gate * represents a core file from a differently configured system -- two
11657c478bd9Sstevel@tonic-gate * common examples. The DT_CHECKSUM entry in the dynamic section
11667c478bd9Sstevel@tonic-gate * provides an easy method of comparison. It is important to note that
11677c478bd9Sstevel@tonic-gate * the dynamic section usually lives in the data segment, but the meta
11687c478bd9Sstevel@tonic-gate * data we use to find the dynamic section lives in the text segment so
11697c478bd9Sstevel@tonic-gate * if either of those segments is absent we can't proceed.
11707c478bd9Sstevel@tonic-gate *
11717c478bd9Sstevel@tonic-gate * We're looking through the elf file for several items: the symbol tables
11727c478bd9Sstevel@tonic-gate * (both dynsym and symtab), the procedure linkage table (PLT) base,
11737c478bd9Sstevel@tonic-gate * size, and relocation base, and the CTF information. Most of this can
11747c478bd9Sstevel@tonic-gate * be recovered from the loaded image of the file itself, the exceptions
11757c478bd9Sstevel@tonic-gate * being the symtab and CTF data.
11767c478bd9Sstevel@tonic-gate *
11777c478bd9Sstevel@tonic-gate * First we try to open the file that we think corresponds to the load
11787c478bd9Sstevel@tonic-gate * object, if the DT_CHECKSUM values match, we're all set, and can simply
11797c478bd9Sstevel@tonic-gate * recover all the information we need from the file. If the values of
11807c478bd9Sstevel@tonic-gate * DT_CHECKSUM don't match, or if we can't access the file for whatever
11817c478bd9Sstevel@tonic-gate * reasaon, we fake up a elf file to use in its stead. If we can't read
11827c478bd9Sstevel@tonic-gate * the elf data in the process's address space, we fall back to using
11837c478bd9Sstevel@tonic-gate * the file even though it may give inaccurate information.
11847c478bd9Sstevel@tonic-gate *
11857c478bd9Sstevel@tonic-gate * The elf file that we fake up has to consist of sections for the
11867c478bd9Sstevel@tonic-gate * dynsym, the PLT and the dynamic section. Note that in the case of a
11877c478bd9Sstevel@tonic-gate * core file, we'll get the CTF data in the file_info_t later on from
11887c478bd9Sstevel@tonic-gate * a section embedded the core file (if it's present).
11897c478bd9Sstevel@tonic-gate *
11907c478bd9Sstevel@tonic-gate * file_differs() conservatively looks for mismatched files, identifying
11917c478bd9Sstevel@tonic-gate * a match when there is any ambiguity (since that's the legacy behavior).
11927c478bd9Sstevel@tonic-gate */
11937c478bd9Sstevel@tonic-gate static int
file_differs(struct ps_prochandle * P,Elf * elf,file_info_t * fptr)11947c478bd9Sstevel@tonic-gate file_differs(struct ps_prochandle *P, Elf *elf, file_info_t *fptr)
11957c478bd9Sstevel@tonic-gate {
11967c478bd9Sstevel@tonic-gate Elf_Scn *scn;
11977c478bd9Sstevel@tonic-gate GElf_Shdr shdr;
11987c478bd9Sstevel@tonic-gate GElf_Dyn dyn;
11997c478bd9Sstevel@tonic-gate Elf_Data *data;
12007c478bd9Sstevel@tonic-gate uint_t i, ndyn;
12017c478bd9Sstevel@tonic-gate GElf_Xword cksum;
12027c478bd9Sstevel@tonic-gate uintptr_t addr;
12037c478bd9Sstevel@tonic-gate
12047c478bd9Sstevel@tonic-gate if (fptr->file_map == NULL)
12057c478bd9Sstevel@tonic-gate return (0);
12067c478bd9Sstevel@tonic-gate
12077c478bd9Sstevel@tonic-gate if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
12087c478bd9Sstevel@tonic-gate (CC_CONTENT_TEXT | CC_CONTENT_DATA))
12097c478bd9Sstevel@tonic-gate return (0);
12107c478bd9Sstevel@tonic-gate
12117c478bd9Sstevel@tonic-gate /*
12127c478bd9Sstevel@tonic-gate * First, we find the checksum value in the elf file.
12137c478bd9Sstevel@tonic-gate */
12147c478bd9Sstevel@tonic-gate scn = NULL;
12157c478bd9Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != NULL) {
12167c478bd9Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) != NULL &&
12177c478bd9Sstevel@tonic-gate shdr.sh_type == SHT_DYNAMIC)
12187c478bd9Sstevel@tonic-gate goto found_shdr;
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate return (0);
12217c478bd9Sstevel@tonic-gate
12227c478bd9Sstevel@tonic-gate found_shdr:
12237c478bd9Sstevel@tonic-gate if ((data = elf_getdata(scn, NULL)) == NULL)
12247c478bd9Sstevel@tonic-gate return (0);
12257c478bd9Sstevel@tonic-gate
12267c478bd9Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32)
12277c478bd9Sstevel@tonic-gate ndyn = shdr.sh_size / sizeof (Elf32_Dyn);
12287c478bd9Sstevel@tonic-gate #ifdef _LP64
12297c478bd9Sstevel@tonic-gate else if (P->status.pr_dmodel == PR_MODEL_LP64)
12307c478bd9Sstevel@tonic-gate ndyn = shdr.sh_size / sizeof (Elf64_Dyn);
12317c478bd9Sstevel@tonic-gate #endif
12327c478bd9Sstevel@tonic-gate else
12337c478bd9Sstevel@tonic-gate return (0);
12347c478bd9Sstevel@tonic-gate
12357c478bd9Sstevel@tonic-gate for (i = 0; i < ndyn; i++) {
12367c478bd9Sstevel@tonic-gate if (gelf_getdyn(data, i, &dyn) != NULL &&
12377c478bd9Sstevel@tonic-gate dyn.d_tag == DT_CHECKSUM)
12387c478bd9Sstevel@tonic-gate goto found_cksum;
12397c478bd9Sstevel@tonic-gate }
12409acbbeafSnn35248
12419acbbeafSnn35248 /*
12429acbbeafSnn35248 * The in-memory ELF has no DT_CHECKSUM section, but we will report it
12439acbbeafSnn35248 * as matching the file anyhow.
12449acbbeafSnn35248 */
12457c478bd9Sstevel@tonic-gate return (0);
12467c478bd9Sstevel@tonic-gate
12477c478bd9Sstevel@tonic-gate found_cksum:
12487c478bd9Sstevel@tonic-gate cksum = dyn.d_un.d_val;
12497c478bd9Sstevel@tonic-gate dprintf("elf cksum value is %llx\n", (u_longlong_t)cksum);
12507c478bd9Sstevel@tonic-gate
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate * Get the base of the text mapping that corresponds to this file.
12537c478bd9Sstevel@tonic-gate */
12547c478bd9Sstevel@tonic-gate addr = fptr->file_map->map_pmap.pr_vaddr;
12557c478bd9Sstevel@tonic-gate
12567c478bd9Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) {
12577c478bd9Sstevel@tonic-gate Elf32_Ehdr ehdr;
12587c478bd9Sstevel@tonic-gate Elf32_Phdr phdr;
12597c478bd9Sstevel@tonic-gate Elf32_Dyn dync, *dynp;
126030da1432Sahl uint_t phnum, i;
12617c478bd9Sstevel@tonic-gate
126230da1432Sahl if (read_ehdr32(P, &ehdr, &phnum, addr) != 0 ||
126330da1432Sahl read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
12647c478bd9Sstevel@tonic-gate return (0);
12657c478bd9Sstevel@tonic-gate
12667c478bd9Sstevel@tonic-gate if (ehdr.e_type == ET_DYN)
12677c478bd9Sstevel@tonic-gate phdr.p_vaddr += addr;
12687c478bd9Sstevel@tonic-gate if ((dynp = malloc(phdr.p_filesz)) == NULL)
12697c478bd9Sstevel@tonic-gate return (0);
12707c478bd9Sstevel@tonic-gate dync.d_tag = DT_NULL;
12717c478bd9Sstevel@tonic-gate if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
12727c478bd9Sstevel@tonic-gate phdr.p_filesz) {
12737c478bd9Sstevel@tonic-gate free(dynp);
12747c478bd9Sstevel@tonic-gate return (0);
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate
12777c478bd9Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf32_Dyn); i++) {
12787c478bd9Sstevel@tonic-gate if (dynp[i].d_tag == DT_CHECKSUM)
12797c478bd9Sstevel@tonic-gate dync = dynp[i];
12807c478bd9Sstevel@tonic-gate }
12817c478bd9Sstevel@tonic-gate
12827c478bd9Sstevel@tonic-gate free(dynp);
12837c478bd9Sstevel@tonic-gate
12847c478bd9Sstevel@tonic-gate if (dync.d_tag != DT_CHECKSUM)
12857c478bd9Sstevel@tonic-gate return (0);
12867c478bd9Sstevel@tonic-gate
12877c478bd9Sstevel@tonic-gate dprintf("image cksum value is %llx\n",
12887c478bd9Sstevel@tonic-gate (u_longlong_t)dync.d_un.d_val);
12897c478bd9Sstevel@tonic-gate return (dync.d_un.d_val != cksum);
12907c478bd9Sstevel@tonic-gate #ifdef _LP64
12917c478bd9Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_LP64) {
12927c478bd9Sstevel@tonic-gate Elf64_Ehdr ehdr;
12937c478bd9Sstevel@tonic-gate Elf64_Phdr phdr;
12947c478bd9Sstevel@tonic-gate Elf64_Dyn dync, *dynp;
129530da1432Sahl uint_t phnum, i;
12967c478bd9Sstevel@tonic-gate
129730da1432Sahl if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
129830da1432Sahl read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
12997c478bd9Sstevel@tonic-gate return (0);
13007c478bd9Sstevel@tonic-gate
13017c478bd9Sstevel@tonic-gate if (ehdr.e_type == ET_DYN)
13027c478bd9Sstevel@tonic-gate phdr.p_vaddr += addr;
13037c478bd9Sstevel@tonic-gate if ((dynp = malloc(phdr.p_filesz)) == NULL)
13047c478bd9Sstevel@tonic-gate return (0);
13057c478bd9Sstevel@tonic-gate dync.d_tag = DT_NULL;
13067c478bd9Sstevel@tonic-gate if (Pread(P, dynp, phdr.p_filesz, phdr.p_vaddr) !=
13077c478bd9Sstevel@tonic-gate phdr.p_filesz) {
13087c478bd9Sstevel@tonic-gate free(dynp);
13097c478bd9Sstevel@tonic-gate return (0);
13107c478bd9Sstevel@tonic-gate }
13117c478bd9Sstevel@tonic-gate
13127c478bd9Sstevel@tonic-gate for (i = 0; i < phdr.p_filesz / sizeof (Elf64_Dyn); i++) {
13137c478bd9Sstevel@tonic-gate if (dynp[i].d_tag == DT_CHECKSUM)
13147c478bd9Sstevel@tonic-gate dync = dynp[i];
13157c478bd9Sstevel@tonic-gate }
13167c478bd9Sstevel@tonic-gate
13177c478bd9Sstevel@tonic-gate free(dynp);
13187c478bd9Sstevel@tonic-gate
13197c478bd9Sstevel@tonic-gate if (dync.d_tag != DT_CHECKSUM)
13207c478bd9Sstevel@tonic-gate return (0);
13217c478bd9Sstevel@tonic-gate
13227c478bd9Sstevel@tonic-gate dprintf("image cksum value is %llx\n",
13237c478bd9Sstevel@tonic-gate (u_longlong_t)dync.d_un.d_val);
13247c478bd9Sstevel@tonic-gate return (dync.d_un.d_val != cksum);
13257c478bd9Sstevel@tonic-gate #endif /* _LP64 */
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate
13287c478bd9Sstevel@tonic-gate return (0);
13297c478bd9Sstevel@tonic-gate }
13307c478bd9Sstevel@tonic-gate
1331d51e9074Sab196087 /*
1332d51e9074Sab196087 * Read data from the specified process and construct an in memory
1333d51e9074Sab196087 * image of an ELF file that represents it well enough to let
1334d51e9074Sab196087 * us probe it for information.
1335d51e9074Sab196087 */
13367c478bd9Sstevel@tonic-gate static Elf *
fake_elf(struct ps_prochandle * P,file_info_t * fptr)13377c478bd9Sstevel@tonic-gate fake_elf(struct ps_prochandle *P, file_info_t *fptr)
13387c478bd9Sstevel@tonic-gate {
13397c478bd9Sstevel@tonic-gate Elf *elf;
1340d51e9074Sab196087 uintptr_t addr;
1341d51e9074Sab196087 uint_t phnum;
13427c478bd9Sstevel@tonic-gate
13437c478bd9Sstevel@tonic-gate if (fptr->file_map == NULL)
13447c478bd9Sstevel@tonic-gate return (NULL);
13457c478bd9Sstevel@tonic-gate
13467c478bd9Sstevel@tonic-gate if ((Pcontent(P) & (CC_CONTENT_TEXT | CC_CONTENT_DATA)) !=
13477c478bd9Sstevel@tonic-gate (CC_CONTENT_TEXT | CC_CONTENT_DATA))
13487c478bd9Sstevel@tonic-gate return (NULL);
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate addr = fptr->file_map->map_pmap.pr_vaddr;
13517c478bd9Sstevel@tonic-gate
13527c478bd9Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1353d51e9074Sab196087 Elf32_Ehdr ehdr;
13547c478bd9Sstevel@tonic-gate Elf32_Phdr phdr;
13557c478bd9Sstevel@tonic-gate
13569acbbeafSnn35248 if ((read_ehdr32(P, &ehdr, &phnum, addr) != 0) ||
135730da1432Sahl read_dynamic_phdr32(P, &ehdr, phnum, &phdr, addr) != 0)
13587c478bd9Sstevel@tonic-gate return (NULL);
13597c478bd9Sstevel@tonic-gate
1360d51e9074Sab196087 elf = fake_elf32(P, fptr, addr, &ehdr, phnum, &phdr);
13617c478bd9Sstevel@tonic-gate #ifdef _LP64
1362d51e9074Sab196087 } else {
1363d51e9074Sab196087 Elf64_Ehdr ehdr;
13647c478bd9Sstevel@tonic-gate Elf64_Phdr phdr;
13657c478bd9Sstevel@tonic-gate
136630da1432Sahl if (read_ehdr64(P, &ehdr, &phnum, addr) != 0 ||
136730da1432Sahl read_dynamic_phdr64(P, &ehdr, phnum, &phdr, addr) != 0)
13687c478bd9Sstevel@tonic-gate return (NULL);
13697c478bd9Sstevel@tonic-gate
1370d51e9074Sab196087 elf = fake_elf64(P, fptr, addr, &ehdr, phnum, &phdr);
1371d51e9074Sab196087 #endif
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate
13747c478bd9Sstevel@tonic-gate return (elf);
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate
13777c478bd9Sstevel@tonic-gate /*
13787c478bd9Sstevel@tonic-gate * We wouldn't need these if qsort(3C) took an argument for the callback...
13797c478bd9Sstevel@tonic-gate */
13807c478bd9Sstevel@tonic-gate static mutex_t sort_mtx = DEFAULTMUTEX;
13817c478bd9Sstevel@tonic-gate static char *sort_strs;
13827c478bd9Sstevel@tonic-gate static GElf_Sym *sort_syms;
13837c478bd9Sstevel@tonic-gate
13847c478bd9Sstevel@tonic-gate int
byaddr_cmp_common(GElf_Sym * a,char * aname,GElf_Sym * b,char * bname)13857c478bd9Sstevel@tonic-gate byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate if (a->st_value < b->st_value)
13887c478bd9Sstevel@tonic-gate return (-1);
13897c478bd9Sstevel@tonic-gate if (a->st_value > b->st_value)
13907c478bd9Sstevel@tonic-gate return (1);
13917c478bd9Sstevel@tonic-gate
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate * Prefer the function to the non-function.
13947c478bd9Sstevel@tonic-gate */
13957c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
13967c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
13977c478bd9Sstevel@tonic-gate return (-1);
13987c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
13997c478bd9Sstevel@tonic-gate return (1);
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate
14027c478bd9Sstevel@tonic-gate /*
14037c478bd9Sstevel@tonic-gate * Prefer the weak or strong global symbol to the local symbol.
14047c478bd9Sstevel@tonic-gate */
14057c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
14067c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
14077c478bd9Sstevel@tonic-gate return (-1);
14087c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
14097c478bd9Sstevel@tonic-gate return (1);
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate
14127c478bd9Sstevel@tonic-gate /*
1413ab9a77c7Sahl * Prefer the symbol that doesn't begin with a '$' since compilers and
1414ab9a77c7Sahl * other symbol generators often use it as a prefix.
1415ab9a77c7Sahl */
1416ab9a77c7Sahl if (*bname == '$')
1417ab9a77c7Sahl return (-1);
1418ab9a77c7Sahl if (*aname == '$')
1419ab9a77c7Sahl return (1);
1420ab9a77c7Sahl
1421ab9a77c7Sahl /*
14227c478bd9Sstevel@tonic-gate * Prefer the name with fewer leading underscores in the name.
14237c478bd9Sstevel@tonic-gate */
14247c478bd9Sstevel@tonic-gate while (*aname == '_' && *bname == '_') {
14257c478bd9Sstevel@tonic-gate aname++;
14267c478bd9Sstevel@tonic-gate bname++;
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate
14297c478bd9Sstevel@tonic-gate if (*bname == '_')
14307c478bd9Sstevel@tonic-gate return (-1);
14317c478bd9Sstevel@tonic-gate if (*aname == '_')
14327c478bd9Sstevel@tonic-gate return (1);
14337c478bd9Sstevel@tonic-gate
14347c478bd9Sstevel@tonic-gate /*
14357c478bd9Sstevel@tonic-gate * Prefer the symbol with the smaller size.
14367c478bd9Sstevel@tonic-gate */
14377c478bd9Sstevel@tonic-gate if (a->st_size < b->st_size)
14387c478bd9Sstevel@tonic-gate return (-1);
14397c478bd9Sstevel@tonic-gate if (a->st_size > b->st_size)
14407c478bd9Sstevel@tonic-gate return (1);
14417c478bd9Sstevel@tonic-gate
14427c478bd9Sstevel@tonic-gate /*
14437c478bd9Sstevel@tonic-gate * All other factors being equal, fall back to lexicographic order.
14447c478bd9Sstevel@tonic-gate */
14457c478bd9Sstevel@tonic-gate return (strcmp(aname, bname));
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate
14487c478bd9Sstevel@tonic-gate static int
byaddr_cmp(const void * aa,const void * bb)14497c478bd9Sstevel@tonic-gate byaddr_cmp(const void *aa, const void *bb)
14507c478bd9Sstevel@tonic-gate {
14517c478bd9Sstevel@tonic-gate GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14527c478bd9Sstevel@tonic-gate GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14537c478bd9Sstevel@tonic-gate char *aname = sort_strs + a->st_name;
14547c478bd9Sstevel@tonic-gate char *bname = sort_strs + b->st_name;
14557c478bd9Sstevel@tonic-gate
14567c478bd9Sstevel@tonic-gate return (byaddr_cmp_common(a, aname, b, bname));
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate
14597c478bd9Sstevel@tonic-gate static int
byname_cmp(const void * aa,const void * bb)14607c478bd9Sstevel@tonic-gate byname_cmp(const void *aa, const void *bb)
14617c478bd9Sstevel@tonic-gate {
14627c478bd9Sstevel@tonic-gate GElf_Sym *a = &sort_syms[*(uint_t *)aa];
14637c478bd9Sstevel@tonic-gate GElf_Sym *b = &sort_syms[*(uint_t *)bb];
14647c478bd9Sstevel@tonic-gate char *aname = sort_strs + a->st_name;
14657c478bd9Sstevel@tonic-gate char *bname = sort_strs + b->st_name;
14667c478bd9Sstevel@tonic-gate
14677c478bd9Sstevel@tonic-gate return (strcmp(aname, bname));
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate
1470d51e9074Sab196087 /*
1471d51e9074Sab196087 * Given a symbol index, look up the corresponding symbol from the
1472d51e9074Sab196087 * given symbol table.
1473d51e9074Sab196087 *
1474d51e9074Sab196087 * This function allows the caller to treat the symbol table as a single
1475d51e9074Sab196087 * logical entity even though there may be 2 actual ELF symbol tables
1476d51e9074Sab196087 * involved. See the comments in Pcontrol.h for details.
1477d51e9074Sab196087 */
1478d51e9074Sab196087 static GElf_Sym *
symtab_getsym(sym_tbl_t * symtab,int ndx,GElf_Sym * dst)1479d51e9074Sab196087 symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst)
1480d51e9074Sab196087 {
1481d51e9074Sab196087 /* If index is in range of primary symtab, look it up there */
1482d51e9074Sab196087 if (ndx >= symtab->sym_symn_aux) {
1483d51e9074Sab196087 return (gelf_getsym(symtab->sym_data_pri,
1484d51e9074Sab196087 ndx - symtab->sym_symn_aux, dst));
1485d51e9074Sab196087 }
1486d51e9074Sab196087
1487d51e9074Sab196087 /* Not in primary: Look it up in the auxiliary symtab */
1488d51e9074Sab196087 return (gelf_getsym(symtab->sym_data_aux, ndx, dst));
1489d51e9074Sab196087 }
1490d51e9074Sab196087
14917c478bd9Sstevel@tonic-gate void
optimize_symtab(sym_tbl_t * symtab)14927c478bd9Sstevel@tonic-gate optimize_symtab(sym_tbl_t *symtab)
14937c478bd9Sstevel@tonic-gate {
14947c478bd9Sstevel@tonic-gate GElf_Sym *symp, *syms;
14957c478bd9Sstevel@tonic-gate uint_t i, *indexa, *indexb;
14967c478bd9Sstevel@tonic-gate size_t symn, strsz, count;
14977c478bd9Sstevel@tonic-gate
1498d51e9074Sab196087 if (symtab == NULL || symtab->sym_data_pri == NULL ||
14997c478bd9Sstevel@tonic-gate symtab->sym_byaddr != NULL)
15007c478bd9Sstevel@tonic-gate return;
15017c478bd9Sstevel@tonic-gate
15027c478bd9Sstevel@tonic-gate symn = symtab->sym_symn;
15037c478bd9Sstevel@tonic-gate strsz = symtab->sym_strsz;
15047c478bd9Sstevel@tonic-gate
15057c478bd9Sstevel@tonic-gate symp = syms = malloc(sizeof (GElf_Sym) * symn);
1506d51e9074Sab196087 if (symp == NULL) {
1507d51e9074Sab196087 dprintf("optimize_symtab: failed to malloc symbol array");
1508d51e9074Sab196087 return;
1509d51e9074Sab196087 }
15107c478bd9Sstevel@tonic-gate
15117c478bd9Sstevel@tonic-gate /*
15127c478bd9Sstevel@tonic-gate * First record all the symbols into a table and count up the ones
15137c478bd9Sstevel@tonic-gate * that we're interested in. We mark symbols as invalid by setting
15147c478bd9Sstevel@tonic-gate * the st_name to an illegal value.
15157c478bd9Sstevel@tonic-gate */
15167c478bd9Sstevel@tonic-gate for (i = 0, count = 0; i < symn; i++, symp++) {
1517d51e9074Sab196087 if (symtab_getsym(symtab, i, symp) != NULL &&
15187c478bd9Sstevel@tonic-gate symp->st_name < strsz &&
15197c478bd9Sstevel@tonic-gate IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
15207c478bd9Sstevel@tonic-gate count++;
15217c478bd9Sstevel@tonic-gate else
15227c478bd9Sstevel@tonic-gate symp->st_name = strsz;
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate
15257c478bd9Sstevel@tonic-gate /*
15267c478bd9Sstevel@tonic-gate * Allocate sufficient space for both tables and populate them
15277c478bd9Sstevel@tonic-gate * with the same symbols we just counted.
15287c478bd9Sstevel@tonic-gate */
15297c478bd9Sstevel@tonic-gate symtab->sym_count = count;
15307c478bd9Sstevel@tonic-gate indexa = symtab->sym_byaddr = calloc(sizeof (uint_t), count);
15317c478bd9Sstevel@tonic-gate indexb = symtab->sym_byname = calloc(sizeof (uint_t), count);
1532d51e9074Sab196087 if (indexa == NULL || indexb == NULL) {
1533d51e9074Sab196087 dprintf(
1534d51e9074Sab196087 "optimize_symtab: failed to malloc symbol index arrays");
1535d51e9074Sab196087 symtab->sym_count = 0;
1536d51e9074Sab196087 if (indexa != NULL) { /* First alloc succeeded. Free it */
1537d51e9074Sab196087 free(indexa);
1538d51e9074Sab196087 symtab->sym_byaddr = NULL;
1539d51e9074Sab196087 }
1540d51e9074Sab196087 free(syms);
1541d51e9074Sab196087 return;
1542d51e9074Sab196087 }
15437c478bd9Sstevel@tonic-gate for (i = 0, symp = syms; i < symn; i++, symp++) {
15447c478bd9Sstevel@tonic-gate if (symp->st_name < strsz)
15457c478bd9Sstevel@tonic-gate *indexa++ = *indexb++ = i;
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate
15487c478bd9Sstevel@tonic-gate /*
1549d7755b5aSrh87107 * Sort the two tables according to the appropriate criteria,
1550d7755b5aSrh87107 * unless the user has overridden this behaviour.
1551d7755b5aSrh87107 *
1552d7755b5aSrh87107 * An example where we might not sort the tables is the relatively
1553d7755b5aSrh87107 * unusual case of a process with very large symbol tables in which
1554d7755b5aSrh87107 * we perform few lookups. In such a case the total time would be
1555d7755b5aSrh87107 * dominated by the sort. It is difficult to determine a priori
1556d7755b5aSrh87107 * how many lookups an arbitrary client will perform, and
1557d7755b5aSrh87107 * hence whether the symbol tables should be sorted. We therefore
1558d7755b5aSrh87107 * sort the tables by default, but provide the user with a
1559d7755b5aSrh87107 * "chicken switch" in the form of the LIBPROC_NO_QSORT
1560d7755b5aSrh87107 * environment variable.
15617c478bd9Sstevel@tonic-gate */
1562d7755b5aSrh87107 if (!_libproc_no_qsort) {
15637c478bd9Sstevel@tonic-gate (void) mutex_lock(&sort_mtx);
15647c478bd9Sstevel@tonic-gate sort_strs = symtab->sym_strs;
15657c478bd9Sstevel@tonic-gate sort_syms = syms;
15667c478bd9Sstevel@tonic-gate
15677c478bd9Sstevel@tonic-gate qsort(symtab->sym_byaddr, count, sizeof (uint_t), byaddr_cmp);
15687c478bd9Sstevel@tonic-gate qsort(symtab->sym_byname, count, sizeof (uint_t), byname_cmp);
15697c478bd9Sstevel@tonic-gate
15707c478bd9Sstevel@tonic-gate sort_strs = NULL;
15717c478bd9Sstevel@tonic-gate sort_syms = NULL;
15727c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sort_mtx);
1573d7755b5aSrh87107 }
15747c478bd9Sstevel@tonic-gate
15757c478bd9Sstevel@tonic-gate free(syms);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate
1578d9452f23SEdward Pilatowicz
1579d9452f23SEdward Pilatowicz static Elf *
build_fake_elf(struct ps_prochandle * P,file_info_t * fptr,GElf_Ehdr * ehdr,size_t * nshdrs,Elf_Data ** shdata)1580d9452f23SEdward Pilatowicz build_fake_elf(struct ps_prochandle *P, file_info_t *fptr, GElf_Ehdr *ehdr,
1581d9452f23SEdward Pilatowicz size_t *nshdrs, Elf_Data **shdata)
1582d9452f23SEdward Pilatowicz {
1583d9452f23SEdward Pilatowicz size_t shstrndx;
1584d9452f23SEdward Pilatowicz Elf_Scn *scn;
1585d9452f23SEdward Pilatowicz Elf *elf;
1586d9452f23SEdward Pilatowicz
1587d9452f23SEdward Pilatowicz if ((elf = fake_elf(P, fptr)) == NULL ||
1588d9452f23SEdward Pilatowicz elf_kind(elf) != ELF_K_ELF ||
1589d9452f23SEdward Pilatowicz gelf_getehdr(elf, ehdr) == NULL ||
1590d9452f23SEdward Pilatowicz elf_getshdrnum(elf, nshdrs) == -1 ||
1591d9452f23SEdward Pilatowicz elf_getshdrstrndx(elf, &shstrndx) == -1 ||
1592d9452f23SEdward Pilatowicz (scn = elf_getscn(elf, shstrndx)) == NULL ||
1593d9452f23SEdward Pilatowicz (*shdata = elf_getdata(scn, NULL)) == NULL) {
1594d9452f23SEdward Pilatowicz if (elf != NULL)
1595d9452f23SEdward Pilatowicz (void) elf_end(elf);
1596d9452f23SEdward Pilatowicz dprintf("failed to fake up ELF file\n");
1597d9452f23SEdward Pilatowicz return (NULL);
1598d9452f23SEdward Pilatowicz }
1599d9452f23SEdward Pilatowicz
1600d9452f23SEdward Pilatowicz return (elf);
1601d9452f23SEdward Pilatowicz }
1602d9452f23SEdward Pilatowicz
16037c478bd9Sstevel@tonic-gate /*
1604e68846a8SRobert Mustacchi * Try and find the file described by path in the file system and validate that
16058d12444dSRobert Mustacchi * it matches our CRC before we try and process it for symbol information. If we
16068d12444dSRobert Mustacchi * instead have an ELF data section, then that means we're checking a build-id
16078d12444dSRobert Mustacchi * section instead. In that case we just need to find and bcmp the corresponding
16088d12444dSRobert Mustacchi * section.
1609e68846a8SRobert Mustacchi *
16108d12444dSRobert Mustacchi * Before we validate if it's a valid CRC or data section, we check to ensure
16118d12444dSRobert Mustacchi * that it's a normal file and not anything else.
1612e68846a8SRobert Mustacchi */
1613e68846a8SRobert Mustacchi static boolean_t
build_alt_debug(file_info_t * fptr,const char * path,uint32_t crc,Elf_Data * data)16148d12444dSRobert Mustacchi build_alt_debug(file_info_t *fptr, const char *path, uint32_t crc,
16158d12444dSRobert Mustacchi Elf_Data *data)
1616e68846a8SRobert Mustacchi {
1617e68846a8SRobert Mustacchi int fd;
1618e68846a8SRobert Mustacchi struct stat st;
1619e68846a8SRobert Mustacchi Elf *elf;
1620e68846a8SRobert Mustacchi Elf_Scn *scn;
1621e68846a8SRobert Mustacchi GElf_Shdr symshdr, strshdr;
1622e68846a8SRobert Mustacchi Elf_Data *symdata, *strdata;
16238d12444dSRobert Mustacchi boolean_t valid;
1624e68846a8SRobert Mustacchi uint32_t c = -1U;
1625e68846a8SRobert Mustacchi
1626e68846a8SRobert Mustacchi if ((fd = open(path, O_RDONLY)) < 0)
1627e68846a8SRobert Mustacchi return (B_FALSE);
1628e68846a8SRobert Mustacchi
1629e68846a8SRobert Mustacchi if (fstat(fd, &st) != 0) {
1630e68846a8SRobert Mustacchi (void) close(fd);
1631e68846a8SRobert Mustacchi return (B_FALSE);
1632e68846a8SRobert Mustacchi }
1633e68846a8SRobert Mustacchi
1634e68846a8SRobert Mustacchi if (S_ISREG(st.st_mode) == 0) {
1635e68846a8SRobert Mustacchi (void) close(fd);
1636e68846a8SRobert Mustacchi return (B_FALSE);
1637e68846a8SRobert Mustacchi }
1638e68846a8SRobert Mustacchi
16398d12444dSRobert Mustacchi /*
16408d12444dSRobert Mustacchi * Only check the CRC if we've come here through a GNU debug link
16418d12444dSRobert Mustacchi * section as opposed to the build id. This is indicated by having the
16428d12444dSRobert Mustacchi * value of data be NULL.
16438d12444dSRobert Mustacchi */
16448d12444dSRobert Mustacchi if (data == NULL) {
1645e68846a8SRobert Mustacchi for (;;) {
1646e68846a8SRobert Mustacchi char buf[4096];
1647e68846a8SRobert Mustacchi ssize_t ret = read(fd, buf, sizeof (buf));
1648e68846a8SRobert Mustacchi if (ret == -1) {
1649e68846a8SRobert Mustacchi if (ret == EINTR)
1650e68846a8SRobert Mustacchi continue;
1651e68846a8SRobert Mustacchi (void) close(fd);
1652e68846a8SRobert Mustacchi return (B_FALSE);
1653e68846a8SRobert Mustacchi }
1654e68846a8SRobert Mustacchi if (ret == 0) {
1655e68846a8SRobert Mustacchi c = ~c;
1656e68846a8SRobert Mustacchi if (c != crc) {
1657e68846a8SRobert Mustacchi dprintf("crc mismatch, found: 0x%x "
1658e68846a8SRobert Mustacchi "expected 0x%x\n", c, crc);
1659e68846a8SRobert Mustacchi (void) close(fd);
1660e68846a8SRobert Mustacchi return (B_FALSE);
1661e68846a8SRobert Mustacchi }
1662e68846a8SRobert Mustacchi break;
1663e68846a8SRobert Mustacchi }
1664e68846a8SRobert Mustacchi CRC32(c, buf, ret, c, psym_crc32);
1665e68846a8SRobert Mustacchi }
16668d12444dSRobert Mustacchi }
1667e68846a8SRobert Mustacchi
1668e68846a8SRobert Mustacchi elf = elf_begin(fd, ELF_C_READ, NULL);
1669e68846a8SRobert Mustacchi if (elf == NULL) {
1670e68846a8SRobert Mustacchi (void) close(fd);
1671e68846a8SRobert Mustacchi return (B_FALSE);
1672e68846a8SRobert Mustacchi }
1673e68846a8SRobert Mustacchi
1674e68846a8SRobert Mustacchi if (elf_kind(elf) != ELF_K_ELF) {
1675e68846a8SRobert Mustacchi goto fail;
1676e68846a8SRobert Mustacchi }
1677e68846a8SRobert Mustacchi
1678e68846a8SRobert Mustacchi /*
16798d12444dSRobert Mustacchi * If we have a data section, that indicates we have a build-id which
16808d12444dSRobert Mustacchi * means we need to find the corresponding build-id section and compare
16818d12444dSRobert Mustacchi * it.
16828d12444dSRobert Mustacchi */
16838d12444dSRobert Mustacchi scn = NULL;
16848d12444dSRobert Mustacchi valid = B_FALSE;
16858d12444dSRobert Mustacchi for (scn = elf_nextscn(elf, scn); data != NULL && scn != NULL;
16868d12444dSRobert Mustacchi scn = elf_nextscn(elf, scn)) {
16878d12444dSRobert Mustacchi GElf_Shdr hdr;
16888d12444dSRobert Mustacchi Elf_Data *ntdata;
16898d12444dSRobert Mustacchi
16908d12444dSRobert Mustacchi if (gelf_getshdr(scn, &hdr) == NULL)
16918d12444dSRobert Mustacchi goto fail;
16928d12444dSRobert Mustacchi
16938d12444dSRobert Mustacchi if (hdr.sh_type != SHT_NOTE)
16948d12444dSRobert Mustacchi continue;
16958d12444dSRobert Mustacchi
16968d12444dSRobert Mustacchi if ((ntdata = elf_getdata(scn, NULL)) == NULL)
16978d12444dSRobert Mustacchi goto fail;
16988d12444dSRobert Mustacchi
16998d12444dSRobert Mustacchi /*
17008d12444dSRobert Mustacchi * First verify the data section sizes are equal, then the
17018d12444dSRobert Mustacchi * section name. If that's all true, then we can just do a bcmp.
17028d12444dSRobert Mustacchi */
17038d12444dSRobert Mustacchi if (data->d_size != ntdata->d_size)
17048d12444dSRobert Mustacchi continue;
17058d12444dSRobert Mustacchi
17068d12444dSRobert Mustacchi dprintf("found corresponding section in alternate file\n");
17078d12444dSRobert Mustacchi if (bcmp(ntdata->d_buf, data->d_buf, data->d_size) != 0)
17088d12444dSRobert Mustacchi goto fail;
17098d12444dSRobert Mustacchi
17108d12444dSRobert Mustacchi valid = B_TRUE;
17118d12444dSRobert Mustacchi break;
17128d12444dSRobert Mustacchi }
17138d12444dSRobert Mustacchi if (data != NULL && valid == B_FALSE) {
17148d12444dSRobert Mustacchi dprintf("failed to find a matching %s section in %s\n",
17158d12444dSRobert Mustacchi BUILDID_NAME, path);
17168d12444dSRobert Mustacchi goto fail;
17178d12444dSRobert Mustacchi }
17188d12444dSRobert Mustacchi
17198d12444dSRobert Mustacchi
17208d12444dSRobert Mustacchi /*
1721e68846a8SRobert Mustacchi * Do two passes, first see if we have a symbol header, then see if we
1722e68846a8SRobert Mustacchi * can find the corresponding linked string table.
1723e68846a8SRobert Mustacchi */
1724e68846a8SRobert Mustacchi scn = NULL;
1725e68846a8SRobert Mustacchi for (scn = elf_nextscn(elf, scn); scn != NULL;
1726e68846a8SRobert Mustacchi scn = elf_nextscn(elf, scn)) {
1727e68846a8SRobert Mustacchi
1728e68846a8SRobert Mustacchi if (gelf_getshdr(scn, &symshdr) == NULL)
1729e68846a8SRobert Mustacchi goto fail;
1730e68846a8SRobert Mustacchi
1731e68846a8SRobert Mustacchi if (symshdr.sh_type != SHT_SYMTAB)
1732e68846a8SRobert Mustacchi continue;
1733e68846a8SRobert Mustacchi
1734e68846a8SRobert Mustacchi if ((symdata = elf_getdata(scn, NULL)) == NULL)
1735e68846a8SRobert Mustacchi goto fail;
1736e68846a8SRobert Mustacchi
1737e68846a8SRobert Mustacchi break;
1738e68846a8SRobert Mustacchi }
1739e68846a8SRobert Mustacchi if (scn == NULL)
1740e68846a8SRobert Mustacchi goto fail;
1741e68846a8SRobert Mustacchi
1742e68846a8SRobert Mustacchi if ((scn = elf_getscn(elf, symshdr.sh_link)) == NULL)
1743e68846a8SRobert Mustacchi goto fail;
1744e68846a8SRobert Mustacchi
1745e68846a8SRobert Mustacchi if (gelf_getshdr(scn, &strshdr) == NULL)
1746e68846a8SRobert Mustacchi goto fail;
1747e68846a8SRobert Mustacchi
1748e68846a8SRobert Mustacchi if ((strdata = elf_getdata(scn, NULL)) == NULL)
1749e68846a8SRobert Mustacchi goto fail;
1750e68846a8SRobert Mustacchi
1751e68846a8SRobert Mustacchi fptr->file_symtab.sym_data_pri = symdata;
1752e68846a8SRobert Mustacchi fptr->file_symtab.sym_symn += symshdr.sh_size / symshdr.sh_entsize;
1753e68846a8SRobert Mustacchi fptr->file_symtab.sym_strs = strdata->d_buf;
1754e68846a8SRobert Mustacchi fptr->file_symtab.sym_strsz = strdata->d_size;
1755e68846a8SRobert Mustacchi fptr->file_symtab.sym_hdr_pri = symshdr;
1756e68846a8SRobert Mustacchi fptr->file_symtab.sym_strhdr = strshdr;
1757e68846a8SRobert Mustacchi
1758e68846a8SRobert Mustacchi dprintf("successfully loaded additional debug symbols for %s from %s\n",
1759e68846a8SRobert Mustacchi fptr->file_rname, path);
1760e68846a8SRobert Mustacchi
1761e68846a8SRobert Mustacchi fptr->file_dbgfile = fd;
1762e68846a8SRobert Mustacchi fptr->file_dbgelf = elf;
1763e68846a8SRobert Mustacchi return (B_TRUE);
1764e68846a8SRobert Mustacchi fail:
1765e68846a8SRobert Mustacchi (void) elf_end(elf);
1766e68846a8SRobert Mustacchi (void) close(fd);
1767e68846a8SRobert Mustacchi return (B_FALSE);
1768e68846a8SRobert Mustacchi }
1769e68846a8SRobert Mustacchi
1770e68846a8SRobert Mustacchi /*
1771e68846a8SRobert Mustacchi * We're here because the object in question has no symbol information, that's a
1772e68846a8SRobert Mustacchi * bit unfortunate. However, we've found that there's a .gnu_debuglink sitting
1773e68846a8SRobert Mustacchi * around. By convention that means that given the current location of the
1774e68846a8SRobert Mustacchi * object on disk, and the debug name that we found in the binary we need to
1775e68846a8SRobert Mustacchi * search the following locations for a matching file.
1776e68846a8SRobert Mustacchi *
1777e68846a8SRobert Mustacchi * <dirname>/.debug/<debug-name>
1778e68846a8SRobert Mustacchi * /usr/lib/debug/<dirname>/<debug-name>
1779e68846a8SRobert Mustacchi *
1780e68846a8SRobert Mustacchi * In the future, we should consider supporting looking in the prefix's
17818d12444dSRobert Mustacchi * lib/debug directory for a matching object or supporting an arbitrary user
17828d12444dSRobert Mustacchi * defined set of places to look.
1783e68846a8SRobert Mustacchi */
1784e68846a8SRobert Mustacchi static void
find_alt_debuglink(file_info_t * fptr,const char * name,uint32_t crc)17858d12444dSRobert Mustacchi find_alt_debuglink(file_info_t *fptr, const char *name, uint32_t crc)
1786e68846a8SRobert Mustacchi {
1787e68846a8SRobert Mustacchi boolean_t r;
1788e68846a8SRobert Mustacchi char *dup = NULL, *path = NULL, *dname;
1789e68846a8SRobert Mustacchi
1790e68846a8SRobert Mustacchi dprintf("find_alt_debug: looking for %s, crc 0x%x\n", name, crc);
1791e68846a8SRobert Mustacchi if (fptr->file_rname == NULL) {
1792e68846a8SRobert Mustacchi dprintf("find_alt_debug: encountered null file_rname\n");
1793e68846a8SRobert Mustacchi return;
1794e68846a8SRobert Mustacchi }
1795e68846a8SRobert Mustacchi
1796e68846a8SRobert Mustacchi dup = strdup(fptr->file_rname);
1797e68846a8SRobert Mustacchi if (dup == NULL)
1798e68846a8SRobert Mustacchi return;
1799e68846a8SRobert Mustacchi
1800e68846a8SRobert Mustacchi dname = dirname(dup);
1801e68846a8SRobert Mustacchi if (asprintf(&path, "%s/.debug/%s", dname, name) != -1) {
1802e68846a8SRobert Mustacchi dprintf("attempting to load alternate debug information "
1803e68846a8SRobert Mustacchi "from %s\n", path);
18048d12444dSRobert Mustacchi r = build_alt_debug(fptr, path, crc, NULL);
1805e68846a8SRobert Mustacchi free(path);
1806e68846a8SRobert Mustacchi if (r == B_TRUE)
1807e68846a8SRobert Mustacchi goto out;
1808e68846a8SRobert Mustacchi }
1809e68846a8SRobert Mustacchi
1810e68846a8SRobert Mustacchi if (asprintf(&path, "/usr/lib/debug/%s/%s", dname, name) != -1) {
1811e68846a8SRobert Mustacchi dprintf("attempting to load alternate debug information "
1812e68846a8SRobert Mustacchi "from %s\n", path);
18138d12444dSRobert Mustacchi r = build_alt_debug(fptr, path, crc, NULL);
1814e68846a8SRobert Mustacchi free(path);
1815e68846a8SRobert Mustacchi if (r == B_TRUE)
1816e68846a8SRobert Mustacchi goto out;
1817e68846a8SRobert Mustacchi }
1818e68846a8SRobert Mustacchi out:
1819e68846a8SRobert Mustacchi free(dup);
1820e68846a8SRobert Mustacchi }
1821e68846a8SRobert Mustacchi
1822e68846a8SRobert Mustacchi /*
18237c478bd9Sstevel@tonic-gate * Build the symbol table for the given mapped file.
18247c478bd9Sstevel@tonic-gate */
18257c478bd9Sstevel@tonic-gate void
Pbuild_file_symtab(struct ps_prochandle * P,file_info_t * fptr)18267c478bd9Sstevel@tonic-gate Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
18277c478bd9Sstevel@tonic-gate {
18287c478bd9Sstevel@tonic-gate char objectfile[PATH_MAX];
18297c478bd9Sstevel@tonic-gate uint_t i;
18307c478bd9Sstevel@tonic-gate
18317c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr;
18327c478bd9Sstevel@tonic-gate GElf_Sym s;
18337c478bd9Sstevel@tonic-gate
18347c478bd9Sstevel@tonic-gate Elf_Data *shdata;
18357c478bd9Sstevel@tonic-gate Elf_Scn *scn;
18367c478bd9Sstevel@tonic-gate Elf *elf;
183730da1432Sahl size_t nshdrs, shstrndx;
18387c478bd9Sstevel@tonic-gate
18397c478bd9Sstevel@tonic-gate struct {
18407c478bd9Sstevel@tonic-gate GElf_Shdr c_shdr;
18417c478bd9Sstevel@tonic-gate Elf_Data *c_data;
18427c478bd9Sstevel@tonic-gate const char *c_name;
1843e68846a8SRobert Mustacchi } *cp, *cache = NULL, *dyn = NULL, *plt = NULL, *ctf = NULL,
18448d12444dSRobert Mustacchi *dbglink = NULL, *buildid = NULL;
18457c478bd9Sstevel@tonic-gate
18467c478bd9Sstevel@tonic-gate if (fptr->file_init)
18477c478bd9Sstevel@tonic-gate return; /* We've already processed this file */
18487c478bd9Sstevel@tonic-gate
18497c478bd9Sstevel@tonic-gate /*
18507c478bd9Sstevel@tonic-gate * Mark the file_info struct as having the symbol table initialized
18517c478bd9Sstevel@tonic-gate * even if we fail below. We tried once; we don't try again.
18527c478bd9Sstevel@tonic-gate */
18537c478bd9Sstevel@tonic-gate fptr->file_init = 1;
18547c478bd9Sstevel@tonic-gate
18557c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) {
18567c478bd9Sstevel@tonic-gate dprintf("libproc ELF version is more recent than libelf\n");
18577c478bd9Sstevel@tonic-gate return;
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate
18607c478bd9Sstevel@tonic-gate if (P->state == PS_DEAD || P->state == PS_IDLE) {
1861186f7fbfSEdward Pilatowicz char *name;
18627c478bd9Sstevel@tonic-gate /*
18637c478bd9Sstevel@tonic-gate * If we're a not live, we can't open files from the /proc
18647c478bd9Sstevel@tonic-gate * object directory; we have only the mapping and file names
18657c478bd9Sstevel@tonic-gate * to guide us. We prefer the file_lname, but need to handle
18667c478bd9Sstevel@tonic-gate * the case of it being NULL in order to bootstrap: we first
18677c478bd9Sstevel@tonic-gate * come here during rd_new() when the only information we have
18687c478bd9Sstevel@tonic-gate * is interpreter name associated with the AT_BASE mapping.
1869186f7fbfSEdward Pilatowicz *
1870186f7fbfSEdward Pilatowicz * Also, if the zone associated with the core file seems
1871186f7fbfSEdward Pilatowicz * to exists on this machine we'll try to open the object
1872186f7fbfSEdward Pilatowicz * file within the zone.
18737c478bd9Sstevel@tonic-gate */
1874186f7fbfSEdward Pilatowicz if (fptr->file_rname != NULL)
1875186f7fbfSEdward Pilatowicz name = fptr->file_rname;
1876186f7fbfSEdward Pilatowicz else if (fptr->file_lname != NULL)
1877186f7fbfSEdward Pilatowicz name = fptr->file_lname;
1878186f7fbfSEdward Pilatowicz else
1879186f7fbfSEdward Pilatowicz name = fptr->file_pname;
1880186f7fbfSEdward Pilatowicz (void) strlcpy(objectfile, name, sizeof (objectfile));
18817c478bd9Sstevel@tonic-gate } else {
18827c478bd9Sstevel@tonic-gate (void) snprintf(objectfile, sizeof (objectfile),
18839acbbeafSnn35248 "%s/%d/object/%s",
18849acbbeafSnn35248 procfs_path, (int)P->pid, fptr->file_pname);
18857c478bd9Sstevel@tonic-gate }
18867c478bd9Sstevel@tonic-gate
18877c478bd9Sstevel@tonic-gate /*
18887c478bd9Sstevel@tonic-gate * Open the object file, create the elf file, and then get the elf
18897c478bd9Sstevel@tonic-gate * header and .shstrtab data buffer so we can process sections by
18907c478bd9Sstevel@tonic-gate * name. If anything goes wrong try to fake up an elf file from
18917c478bd9Sstevel@tonic-gate * the in-core elf image.
18927c478bd9Sstevel@tonic-gate */
1893d9452f23SEdward Pilatowicz
18942a12f85aSJeremy Jones if (_libproc_incore_elf || (P->flags & INCORE)) {
1895d9452f23SEdward Pilatowicz dprintf("Pbuild_file_symtab: using in-core data for: %s\n",
1896d9452f23SEdward Pilatowicz fptr->file_pname);
1897d9452f23SEdward Pilatowicz
1898d9452f23SEdward Pilatowicz if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1899d9452f23SEdward Pilatowicz NULL)
1900d9452f23SEdward Pilatowicz return;
1901d9452f23SEdward Pilatowicz
1902d9452f23SEdward Pilatowicz } else if ((fptr->file_fd = open(objectfile, O_RDONLY)) < 0) {
19037c478bd9Sstevel@tonic-gate dprintf("Pbuild_file_symtab: failed to open %s: %s\n",
19047c478bd9Sstevel@tonic-gate objectfile, strerror(errno));
19057c478bd9Sstevel@tonic-gate
1906d9452f23SEdward Pilatowicz if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1907d9452f23SEdward Pilatowicz NULL)
19087c478bd9Sstevel@tonic-gate return;
19097c478bd9Sstevel@tonic-gate
19107c478bd9Sstevel@tonic-gate } else if ((elf = elf_begin(fptr->file_fd, ELF_C_READ, NULL)) == NULL ||
19117c478bd9Sstevel@tonic-gate elf_kind(elf) != ELF_K_ELF ||
19127c478bd9Sstevel@tonic-gate gelf_getehdr(elf, &ehdr) == NULL ||
191362b628a6SAli Bahrami elf_getshdrnum(elf, &nshdrs) == -1 ||
191462b628a6SAli Bahrami elf_getshdrstrndx(elf, &shstrndx) == -1 ||
191530da1432Sahl (scn = elf_getscn(elf, shstrndx)) == NULL ||
19167c478bd9Sstevel@tonic-gate (shdata = elf_getdata(scn, NULL)) == NULL) {
19179acbbeafSnn35248 int err = elf_errno();
19189acbbeafSnn35248
19197c478bd9Sstevel@tonic-gate dprintf("failed to process ELF file %s: %s\n",
19209acbbeafSnn35248 objectfile, (err == 0) ? "<null>" : elf_errmsg(err));
1921d9452f23SEdward Pilatowicz (void) elf_end(elf);
19227c478bd9Sstevel@tonic-gate
1923d9452f23SEdward Pilatowicz if ((elf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata)) ==
1924d9452f23SEdward Pilatowicz NULL)
1925d9452f23SEdward Pilatowicz return;
19267c478bd9Sstevel@tonic-gate
19277c478bd9Sstevel@tonic-gate } else if (file_differs(P, elf, fptr)) {
19287c478bd9Sstevel@tonic-gate Elf *newelf;
19297c478bd9Sstevel@tonic-gate
19307c478bd9Sstevel@tonic-gate /*
19317c478bd9Sstevel@tonic-gate * Before we get too excited about this elf file, we'll check
19327c478bd9Sstevel@tonic-gate * its checksum value against the value we have in memory. If
19337c478bd9Sstevel@tonic-gate * they don't agree, we try to fake up a new elf file and
19347c478bd9Sstevel@tonic-gate * proceed with that instead.
19357c478bd9Sstevel@tonic-gate */
19367c478bd9Sstevel@tonic-gate dprintf("ELF file %s (%lx) doesn't match in-core image\n",
19377c478bd9Sstevel@tonic-gate fptr->file_pname,
19387c478bd9Sstevel@tonic-gate (ulong_t)fptr->file_map->map_pmap.pr_vaddr);
19397c478bd9Sstevel@tonic-gate
1940d9452f23SEdward Pilatowicz if ((newelf = build_fake_elf(P, fptr, &ehdr, &nshdrs, &shdata))
1941d9452f23SEdward Pilatowicz != NULL) {
19427c478bd9Sstevel@tonic-gate (void) elf_end(elf);
19437c478bd9Sstevel@tonic-gate elf = newelf;
19447c478bd9Sstevel@tonic-gate dprintf("switched to faked up ELF file\n");
19458f68126cSBryan Cantrill
19468f68126cSBryan Cantrill /*
19478f68126cSBryan Cantrill * Check to see if the file that we just discovered
19488f68126cSBryan Cantrill * to be an imposter matches the execname that was
19498f68126cSBryan Cantrill * determined by Pfindexec(). If it does, we (clearly)
19508f68126cSBryan Cantrill * don't have the right binary, and we zero out
19518f68126cSBryan Cantrill * execname before anyone gets hurt.
19528f68126cSBryan Cantrill */
19538f68126cSBryan Cantrill if (fptr->file_rname != NULL && P->execname != NULL &&
19548f68126cSBryan Cantrill strcmp(fptr->file_rname, P->execname) == 0) {
19558f68126cSBryan Cantrill dprintf("file/in-core image mismatch was "
19568f68126cSBryan Cantrill "on P->execname; discarding\n");
19578f68126cSBryan Cantrill free(P->execname);
19588f68126cSBryan Cantrill P->execname = NULL;
19598f68126cSBryan Cantrill }
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate }
19627c478bd9Sstevel@tonic-gate
196330da1432Sahl if ((cache = malloc(nshdrs * sizeof (*cache))) == NULL) {
19647c478bd9Sstevel@tonic-gate dprintf("failed to malloc section cache for %s\n", objectfile);
19657c478bd9Sstevel@tonic-gate goto bad;
19667c478bd9Sstevel@tonic-gate }
19677c478bd9Sstevel@tonic-gate
19687c478bd9Sstevel@tonic-gate dprintf("processing ELF file %s\n", objectfile);
19697c478bd9Sstevel@tonic-gate fptr->file_class = ehdr.e_ident[EI_CLASS];
19707c478bd9Sstevel@tonic-gate fptr->file_etype = ehdr.e_type;
19717c478bd9Sstevel@tonic-gate fptr->file_elf = elf;
1972f957ecc1Svb160487 fptr->file_shstrs = shdata->d_buf;
1973f957ecc1Svb160487 fptr->file_shstrsz = shdata->d_size;
19747c478bd9Sstevel@tonic-gate
19757c478bd9Sstevel@tonic-gate /*
19767c478bd9Sstevel@tonic-gate * Iterate through each section, caching its section header, data
19777c478bd9Sstevel@tonic-gate * pointer, and name. We use this for handling sh_link values below.
19787c478bd9Sstevel@tonic-gate */
19797c478bd9Sstevel@tonic-gate for (cp = cache + 1, scn = NULL; scn = elf_nextscn(elf, scn); cp++) {
19809acbbeafSnn35248 if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
19819acbbeafSnn35248 dprintf("Pbuild_file_symtab: Failed to get section "
19829acbbeafSnn35248 "header\n");
19837c478bd9Sstevel@tonic-gate goto bad; /* Failed to get section header */
19849acbbeafSnn35248 }
19857c478bd9Sstevel@tonic-gate
19869acbbeafSnn35248 if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
19879acbbeafSnn35248 dprintf("Pbuild_file_symtab: Failed to get section "
19889acbbeafSnn35248 "data\n");
19897c478bd9Sstevel@tonic-gate goto bad; /* Failed to get section data */
19909acbbeafSnn35248 }
19917c478bd9Sstevel@tonic-gate
19929acbbeafSnn35248 if (cp->c_shdr.sh_name >= shdata->d_size) {
19939acbbeafSnn35248 dprintf("Pbuild_file_symtab: corrupt section name");
19947c478bd9Sstevel@tonic-gate goto bad; /* Corrupt section name */
19959acbbeafSnn35248 }
19967c478bd9Sstevel@tonic-gate
19977c478bd9Sstevel@tonic-gate cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
19987c478bd9Sstevel@tonic-gate }
19997c478bd9Sstevel@tonic-gate
20007c478bd9Sstevel@tonic-gate /*
20017c478bd9Sstevel@tonic-gate * Now iterate through the section cache in order to locate info
2002d51e9074Sab196087 * for the .symtab, .dynsym, .SUNW_ldynsym, .dynamic, .plt,
2003d51e9074Sab196087 * and .SUNW_ctf sections:
20047c478bd9Sstevel@tonic-gate */
200530da1432Sahl for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
20067c478bd9Sstevel@tonic-gate GElf_Shdr *shp = &cp->c_shdr;
20077c478bd9Sstevel@tonic-gate
20087c478bd9Sstevel@tonic-gate if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
20097c478bd9Sstevel@tonic-gate sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
20107c478bd9Sstevel@tonic-gate &fptr->file_symtab : &fptr->file_dynsym;
20117c478bd9Sstevel@tonic-gate /*
20127c478bd9Sstevel@tonic-gate * It's possible that the we already got the symbol
20137c478bd9Sstevel@tonic-gate * table from the core file itself. Either the file
20147c478bd9Sstevel@tonic-gate * differs in which case our faked up elf file will
20157c478bd9Sstevel@tonic-gate * only contain the dynsym (not the symtab) or the
20167c478bd9Sstevel@tonic-gate * file matches in which case we'll just be replacing
20177c478bd9Sstevel@tonic-gate * the symbol table we pulled out of the core file
20187c478bd9Sstevel@tonic-gate * with an equivalent one. In either case, this
20197c478bd9Sstevel@tonic-gate * check isn't essential, but it's a good idea.
20207c478bd9Sstevel@tonic-gate */
2021d51e9074Sab196087 if (symp->sym_data_pri == NULL) {
20229acbbeafSnn35248 dprintf("Symbol table found for %s\n",
20239acbbeafSnn35248 objectfile);
2024d51e9074Sab196087 symp->sym_data_pri = cp->c_data;
2025d51e9074Sab196087 symp->sym_symn +=
2026d51e9074Sab196087 shp->sh_size / shp->sh_entsize;
20277c478bd9Sstevel@tonic-gate symp->sym_strs =
20287c478bd9Sstevel@tonic-gate cache[shp->sh_link].c_data->d_buf;
20297c478bd9Sstevel@tonic-gate symp->sym_strsz =
20307c478bd9Sstevel@tonic-gate cache[shp->sh_link].c_data->d_size;
2031d51e9074Sab196087 symp->sym_hdr_pri = cp->c_shdr;
20327c478bd9Sstevel@tonic-gate symp->sym_strhdr = cache[shp->sh_link].c_shdr;
20339acbbeafSnn35248 } else {
20349acbbeafSnn35248 dprintf("Symbol table already there for %s\n",
20359acbbeafSnn35248 objectfile);
20367c478bd9Sstevel@tonic-gate }
2037d51e9074Sab196087 } else if (shp->sh_type == SHT_SUNW_LDYNSYM) {
2038d51e9074Sab196087 /* .SUNW_ldynsym section is auxiliary to .dynsym */
2039d51e9074Sab196087 if (fptr->file_dynsym.sym_data_aux == NULL) {
2040d51e9074Sab196087 dprintf(".SUNW_ldynsym symbol table"
2041d51e9074Sab196087 " found for %s\n", objectfile);
2042d51e9074Sab196087 fptr->file_dynsym.sym_data_aux = cp->c_data;
2043d51e9074Sab196087 fptr->file_dynsym.sym_symn_aux =
2044d51e9074Sab196087 shp->sh_size / shp->sh_entsize;
2045d51e9074Sab196087 fptr->file_dynsym.sym_symn +=
2046d51e9074Sab196087 fptr->file_dynsym.sym_symn_aux;
2047d51e9074Sab196087 fptr->file_dynsym.sym_hdr_aux = cp->c_shdr;
2048d51e9074Sab196087 } else {
2049d51e9074Sab196087 dprintf(".SUNW_ldynsym symbol table already"
2050d51e9074Sab196087 " there for %s\n", objectfile);
2051d51e9074Sab196087 }
20527c478bd9Sstevel@tonic-gate } else if (shp->sh_type == SHT_DYNAMIC) {
20537c478bd9Sstevel@tonic-gate dyn = cp;
20547c478bd9Sstevel@tonic-gate } else if (strcmp(cp->c_name, ".plt") == 0) {
20557c478bd9Sstevel@tonic-gate plt = cp;
20567c478bd9Sstevel@tonic-gate } else if (strcmp(cp->c_name, ".SUNW_ctf") == 0) {
20577c478bd9Sstevel@tonic-gate /*
20587c478bd9Sstevel@tonic-gate * Skip over bogus CTF sections so they don't come back
20597c478bd9Sstevel@tonic-gate * to haunt us later.
20607c478bd9Sstevel@tonic-gate */
20617c478bd9Sstevel@tonic-gate if (shp->sh_link == 0 ||
206230da1432Sahl shp->sh_link >= nshdrs ||
20637c478bd9Sstevel@tonic-gate (cache[shp->sh_link].c_shdr.sh_type != SHT_DYNSYM &&
20647c478bd9Sstevel@tonic-gate cache[shp->sh_link].c_shdr.sh_type != SHT_SYMTAB)) {
20657c478bd9Sstevel@tonic-gate dprintf("Bad sh_link %d for "
20667c478bd9Sstevel@tonic-gate "CTF\n", shp->sh_link);
20677c478bd9Sstevel@tonic-gate continue;
20687c478bd9Sstevel@tonic-gate }
20697c478bd9Sstevel@tonic-gate ctf = cp;
20708d12444dSRobert Mustacchi } else if (strcmp(cp->c_name, BUILDID_NAME) == 0) {
20718d12444dSRobert Mustacchi dprintf("Found a %s section for %s\n", BUILDID_NAME,
20728d12444dSRobert Mustacchi fptr->file_rname);
20738d12444dSRobert Mustacchi /* The ElfXX_Nhdr is 32/64-bit neutral */
20748d12444dSRobert Mustacchi if (cp->c_shdr.sh_type == SHT_NOTE &&
20758d12444dSRobert Mustacchi cp->c_data->d_buf != NULL &&
20768d12444dSRobert Mustacchi cp->c_data->d_size >= sizeof (Elf32_Nhdr)) {
20778d12444dSRobert Mustacchi Elf32_Nhdr *hdr = cp->c_data->d_buf;
20788d12444dSRobert Mustacchi if (hdr->n_type != 3)
20798d12444dSRobert Mustacchi continue;
20808d12444dSRobert Mustacchi if (hdr->n_namesz != 4)
20818d12444dSRobert Mustacchi continue;
20828d12444dSRobert Mustacchi if (hdr->n_descsz < MINBUILDID)
20838d12444dSRobert Mustacchi continue;
20848d12444dSRobert Mustacchi /* Set a reasonable upper bound */
20858d12444dSRobert Mustacchi if (hdr->n_descsz > MAXBUILDID) {
20868d12444dSRobert Mustacchi dprintf("Skipped %s as too large "
20878d12444dSRobert Mustacchi "(%ld)\n", BUILDID_NAME,
20888d12444dSRobert Mustacchi (unsigned long)hdr->n_descsz);
20898d12444dSRobert Mustacchi continue;
20908d12444dSRobert Mustacchi }
20918d12444dSRobert Mustacchi
20928d12444dSRobert Mustacchi if (cp->c_data->d_size < sizeof (hdr) +
20938d12444dSRobert Mustacchi hdr->n_namesz + hdr->n_descsz)
20948d12444dSRobert Mustacchi continue;
20958d12444dSRobert Mustacchi buildid = cp;
20968d12444dSRobert Mustacchi }
20978d12444dSRobert Mustacchi } else if (strcmp(cp->c_name, DBGLINK_NAME) == 0) {
20988d12444dSRobert Mustacchi dprintf("found %s section for %s\n", DBGLINK_NAME,
2099e68846a8SRobert Mustacchi fptr->file_rname);
2100e68846a8SRobert Mustacchi /*
2101e68846a8SRobert Mustacchi * Let's make sure of a few things before we do this.
2102e68846a8SRobert Mustacchi */
2103e68846a8SRobert Mustacchi if (cp->c_shdr.sh_type == SHT_PROGBITS &&
21048d12444dSRobert Mustacchi cp->c_data->d_buf != NULL &&
21058d12444dSRobert Mustacchi cp->c_data->d_size) {
2106e68846a8SRobert Mustacchi dbglink = cp;
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate }
2109e68846a8SRobert Mustacchi }
2110e68846a8SRobert Mustacchi
2111e68846a8SRobert Mustacchi /*
21128d12444dSRobert Mustacchi * If we haven't found any symbol table information and we have found
21138d12444dSRobert Mustacchi * either a .note.gnu.build-id or a .gnu_debuglink, it's time to try and
21148d12444dSRobert Mustacchi * figure out where we might find this. Originally, GNU used the
21158d12444dSRobert Mustacchi * .gnu_debuglink solely, but then they added a .note.gnu.build-id. The
21168d12444dSRobert Mustacchi * build-id is some size, usually 16 or 20 bytes, often a SHA1 sum of
21178d12444dSRobert Mustacchi * parts of the original file. This is maintained across all versions of
21188d12444dSRobert Mustacchi * the subsequent file.
21198d12444dSRobert Mustacchi *
21208d12444dSRobert Mustacchi * For the .note.gnu.build-id, we're going to check a few things before
21218d12444dSRobert Mustacchi * using it, first that the name is 4 bytes, and is GNU and that the
21228d12444dSRobert Mustacchi * type is 3, which they say is the build-id identifier.
21238d12444dSRobert Mustacchi *
21248d12444dSRobert Mustacchi * To verify that the elf data for the .gnu_debuglink seems somewhat
21258d12444dSRobert Mustacchi * sane, eg. the elf data should be a string, so we want to verify we
21268d12444dSRobert Mustacchi * have a null-terminator.
2127e68846a8SRobert Mustacchi */
21288d12444dSRobert Mustacchi if (fptr->file_symtab.sym_data_pri == NULL && buildid != NULL) {
21298d12444dSRobert Mustacchi int i, bo;
21308d12444dSRobert Mustacchi uint8_t *dp;
21318d12444dSRobert Mustacchi char buf[BUILDID_STRLEN], *path;
21328d12444dSRobert Mustacchi Elf32_Nhdr *hdr = buildid->c_data->d_buf;
21338d12444dSRobert Mustacchi
21348d12444dSRobert Mustacchi /*
21358d12444dSRobert Mustacchi * This was checked for validity when assigning the buildid
21368d12444dSRobert Mustacchi * variable.
21378d12444dSRobert Mustacchi */
21388d12444dSRobert Mustacchi bzero(buf, sizeof (buf));
21398d12444dSRobert Mustacchi dp = (uint8_t *)((uintptr_t)hdr + sizeof (*hdr) +
21408d12444dSRobert Mustacchi hdr->n_namesz);
21418d12444dSRobert Mustacchi for (i = 0, bo = 0; i < hdr->n_descsz; i++, bo += 2, dp++) {
21428d12444dSRobert Mustacchi assert(sizeof (buf) - bo > 0);
21438d12444dSRobert Mustacchi
21448d12444dSRobert Mustacchi /*
21458d12444dSRobert Mustacchi * Recall that the build-id is structured as a series of
21468d12444dSRobert Mustacchi * bytes. However, the first two characters are supposed
21478d12444dSRobert Mustacchi * to represent a directory. Hence, once we reach offset
21488d12444dSRobert Mustacchi * two, we insert a '/' character.
21498d12444dSRobert Mustacchi */
21508d12444dSRobert Mustacchi if (bo == 2) {
21518d12444dSRobert Mustacchi buf[bo] = '/';
21528d12444dSRobert Mustacchi bo++;
21538d12444dSRobert Mustacchi }
21548d12444dSRobert Mustacchi (void) snprintf(buf + bo, sizeof (buf) - bo, "%2x",
21558d12444dSRobert Mustacchi *dp);
21568d12444dSRobert Mustacchi }
21578d12444dSRobert Mustacchi
21588d12444dSRobert Mustacchi if (asprintf(&path, "/usr/lib/debug/.build-id/%s.debug",
21598d12444dSRobert Mustacchi buf) != -1) {
21608d12444dSRobert Mustacchi boolean_t r;
21618d12444dSRobert Mustacchi dprintf("attempting to find build id alternate debug "
21628d12444dSRobert Mustacchi "file at %s\n", path);
21638d12444dSRobert Mustacchi r = build_alt_debug(fptr, path, 0, buildid->c_data);
21648d12444dSRobert Mustacchi dprintf("attempt %s\n", r == B_TRUE ?
21658d12444dSRobert Mustacchi "succeeded" : "failed");
21668d12444dSRobert Mustacchi free(path);
21678d12444dSRobert Mustacchi } else {
21688d12444dSRobert Mustacchi dprintf("failed to construct build id path: %s\n",
21698d12444dSRobert Mustacchi strerror(errno));
21708d12444dSRobert Mustacchi }
21718d12444dSRobert Mustacchi }
21728d12444dSRobert Mustacchi
2173e68846a8SRobert Mustacchi if (fptr->file_symtab.sym_data_pri == NULL && dbglink != NULL) {
2174e68846a8SRobert Mustacchi char *c = dbglink->c_data->d_buf;
2175e68846a8SRobert Mustacchi size_t i;
2176e68846a8SRobert Mustacchi boolean_t found = B_FALSE;
2177e68846a8SRobert Mustacchi Elf_Data *ed = dbglink->c_data;
2178e68846a8SRobert Mustacchi uint32_t crc;
2179e68846a8SRobert Mustacchi
2180e68846a8SRobert Mustacchi for (i = 0; i < ed->d_size; i++) {
2181e68846a8SRobert Mustacchi if (c[i] == '\0') {
2182e68846a8SRobert Mustacchi uintptr_t off;
2183e68846a8SRobert Mustacchi dprintf("got .gnu_debuglink terminator at "
2184e68846a8SRobert Mustacchi "offset %lu\n", (unsigned long)i);
2185e68846a8SRobert Mustacchi /*
2186e68846a8SRobert Mustacchi * After the null terminator, there should be
2187e68846a8SRobert Mustacchi * padding, followed by a 4 byte CRC of the
2188e68846a8SRobert Mustacchi * file. If we don't see this, we're going to
2189e68846a8SRobert Mustacchi * assume this is bogus.
2190e68846a8SRobert Mustacchi */
2191e68846a8SRobert Mustacchi if ((i % sizeof (uint32_t)) == 0) {
2192e68846a8SRobert Mustacchi i += 4;
2193e68846a8SRobert Mustacchi } else {
2194e68846a8SRobert Mustacchi i += sizeof (uint32_t) -
2195e68846a8SRobert Mustacchi (i % sizeof (uint32_t));
2196e68846a8SRobert Mustacchi }
2197e68846a8SRobert Mustacchi if (i + sizeof (uint32_t) ==
2198e68846a8SRobert Mustacchi dbglink->c_data->d_size) {
2199e68846a8SRobert Mustacchi found = B_TRUE;
2200e68846a8SRobert Mustacchi off = (uintptr_t)ed->d_buf + i;
2201e68846a8SRobert Mustacchi crc = *(uint32_t *)off;
2202e68846a8SRobert Mustacchi } else {
2203e68846a8SRobert Mustacchi dprintf(".gnu_debuglink size mismatch, "
2204e68846a8SRobert Mustacchi "expected: %lu, found: %lu\n",
2205e68846a8SRobert Mustacchi (unsigned long)i,
2206e68846a8SRobert Mustacchi (unsigned long)ed->d_size);
2207e68846a8SRobert Mustacchi }
2208e68846a8SRobert Mustacchi break;
2209e68846a8SRobert Mustacchi }
2210e68846a8SRobert Mustacchi }
2211e68846a8SRobert Mustacchi
2212e68846a8SRobert Mustacchi if (found == B_TRUE)
22138d12444dSRobert Mustacchi find_alt_debuglink(fptr, dbglink->c_data->d_buf, crc);
2214e68846a8SRobert Mustacchi }
22157c478bd9Sstevel@tonic-gate
22167c478bd9Sstevel@tonic-gate /*
22177c478bd9Sstevel@tonic-gate * At this point, we've found all the symbol tables we're ever going
22187c478bd9Sstevel@tonic-gate * to find: the ones in the loop above and possibly the symtab that
22197c478bd9Sstevel@tonic-gate * was included in the core file. Before we perform any lookups, we
22207c478bd9Sstevel@tonic-gate * create sorted versions to optimize for lookups.
22217c478bd9Sstevel@tonic-gate */
22227c478bd9Sstevel@tonic-gate optimize_symtab(&fptr->file_symtab);
22237c478bd9Sstevel@tonic-gate optimize_symtab(&fptr->file_dynsym);
22247c478bd9Sstevel@tonic-gate
22257c478bd9Sstevel@tonic-gate /*
22267c478bd9Sstevel@tonic-gate * Fill in the base address of the text mapping for shared libraries.
22277c478bd9Sstevel@tonic-gate * This allows us to translate symbols before librtld_db is ready.
22287c478bd9Sstevel@tonic-gate */
22297c478bd9Sstevel@tonic-gate if (fptr->file_etype == ET_DYN) {
22307c478bd9Sstevel@tonic-gate fptr->file_dyn_base = fptr->file_map->map_pmap.pr_vaddr -
22317c478bd9Sstevel@tonic-gate fptr->file_map->map_pmap.pr_offset;
22329acbbeafSnn35248 dprintf("setting file_dyn_base for %s to %lx\n",
22339acbbeafSnn35248 objectfile, (long)fptr->file_dyn_base);
22347c478bd9Sstevel@tonic-gate }
22357c478bd9Sstevel@tonic-gate
22367c478bd9Sstevel@tonic-gate /*
22377c478bd9Sstevel@tonic-gate * Record the CTF section information in the file info structure.
22387c478bd9Sstevel@tonic-gate */
22397c478bd9Sstevel@tonic-gate if (ctf != NULL) {
22407c478bd9Sstevel@tonic-gate fptr->file_ctf_off = ctf->c_shdr.sh_offset;
22417c478bd9Sstevel@tonic-gate fptr->file_ctf_size = ctf->c_shdr.sh_size;
22427c478bd9Sstevel@tonic-gate if (ctf->c_shdr.sh_link != 0 &&
22437c478bd9Sstevel@tonic-gate cache[ctf->c_shdr.sh_link].c_shdr.sh_type == SHT_DYNSYM)
22447c478bd9Sstevel@tonic-gate fptr->file_ctf_dyn = 1;
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate
22477c478bd9Sstevel@tonic-gate if (fptr->file_lo == NULL)
22487c478bd9Sstevel@tonic-gate goto done; /* Nothing else to do if no load object info */
22497c478bd9Sstevel@tonic-gate
22507c478bd9Sstevel@tonic-gate /*
22517c478bd9Sstevel@tonic-gate * If the object is a shared library and we have a different rl_base
22527c478bd9Sstevel@tonic-gate * value, reset file_dyn_base according to librtld_db's information.
22537c478bd9Sstevel@tonic-gate */
22547c478bd9Sstevel@tonic-gate if (fptr->file_etype == ET_DYN &&
22557c478bd9Sstevel@tonic-gate fptr->file_lo->rl_base != fptr->file_dyn_base) {
22569acbbeafSnn35248 dprintf("resetting file_dyn_base for %s to %lx\n",
22579acbbeafSnn35248 objectfile, (long)fptr->file_lo->rl_base);
22587c478bd9Sstevel@tonic-gate fptr->file_dyn_base = fptr->file_lo->rl_base;
22597c478bd9Sstevel@tonic-gate }
22607c478bd9Sstevel@tonic-gate
22617c478bd9Sstevel@tonic-gate /*
22627c478bd9Sstevel@tonic-gate * Fill in the PLT information for this file if a PLT symbol is found.
22637c478bd9Sstevel@tonic-gate */
22647c478bd9Sstevel@tonic-gate if (sym_by_name(&fptr->file_dynsym, "_PROCEDURE_LINKAGE_TABLE_", &s,
22657c478bd9Sstevel@tonic-gate NULL) != NULL) {
22667c478bd9Sstevel@tonic-gate fptr->file_plt_base = s.st_value + fptr->file_dyn_base;
22677c478bd9Sstevel@tonic-gate fptr->file_plt_size = (plt != NULL) ? plt->c_shdr.sh_size : 0;
22687c478bd9Sstevel@tonic-gate
22697c478bd9Sstevel@tonic-gate /*
22707c478bd9Sstevel@tonic-gate * Bring the load object up to date; it is the only way the
22717c478bd9Sstevel@tonic-gate * user has to access the PLT data. The PLT information in the
22727c478bd9Sstevel@tonic-gate * rd_loadobj_t is not set in the call to map_iter() (the
22737c478bd9Sstevel@tonic-gate * callback for rd_loadobj_iter) where we set file_lo.
22747c478bd9Sstevel@tonic-gate */
22757c478bd9Sstevel@tonic-gate fptr->file_lo->rl_plt_base = fptr->file_plt_base;
22767c478bd9Sstevel@tonic-gate fptr->file_lo->rl_plt_size = fptr->file_plt_size;
22777c478bd9Sstevel@tonic-gate
22787c478bd9Sstevel@tonic-gate dprintf("PLT found at %p, size = %lu\n",
22797c478bd9Sstevel@tonic-gate (void *)fptr->file_plt_base, (ulong_t)fptr->file_plt_size);
22807c478bd9Sstevel@tonic-gate }
22817c478bd9Sstevel@tonic-gate
22827c478bd9Sstevel@tonic-gate /*
22837c478bd9Sstevel@tonic-gate * Fill in the PLT information.
22847c478bd9Sstevel@tonic-gate */
22857c478bd9Sstevel@tonic-gate if (dyn != NULL) {
22867c478bd9Sstevel@tonic-gate uintptr_t dynaddr = dyn->c_shdr.sh_addr + fptr->file_dyn_base;
22877c478bd9Sstevel@tonic-gate size_t ndyn = dyn->c_shdr.sh_size / dyn->c_shdr.sh_entsize;
22887c478bd9Sstevel@tonic-gate GElf_Dyn d;
22897c478bd9Sstevel@tonic-gate
22907c478bd9Sstevel@tonic-gate for (i = 0; i < ndyn; i++) {
2291d9452f23SEdward Pilatowicz if (gelf_getdyn(dyn->c_data, i, &d) == NULL)
2292d9452f23SEdward Pilatowicz continue;
2293d9452f23SEdward Pilatowicz
2294d9452f23SEdward Pilatowicz switch (d.d_tag) {
2295d9452f23SEdward Pilatowicz case DT_JMPREL:
22969acbbeafSnn35248 dprintf("DT_JMPREL is %p\n",
22979acbbeafSnn35248 (void *)(uintptr_t)d.d_un.d_ptr);
22987c478bd9Sstevel@tonic-gate fptr->file_jmp_rel =
22997c478bd9Sstevel@tonic-gate d.d_un.d_ptr + fptr->file_dyn_base;
23007c478bd9Sstevel@tonic-gate break;
2301d9452f23SEdward Pilatowicz case DT_STRTAB:
2302d9452f23SEdward Pilatowicz dprintf("DT_STRTAB is %p\n",
2303d9452f23SEdward Pilatowicz (void *)(uintptr_t)d.d_un.d_ptr);
2304d9452f23SEdward Pilatowicz break;
2305d9452f23SEdward Pilatowicz case DT_PLTGOT:
2306d9452f23SEdward Pilatowicz dprintf("DT_PLTGOT is %p\n",
2307d9452f23SEdward Pilatowicz (void *)(uintptr_t)d.d_un.d_ptr);
2308d9452f23SEdward Pilatowicz break;
2309d9452f23SEdward Pilatowicz case DT_SUNW_SYMTAB:
2310d9452f23SEdward Pilatowicz dprintf("DT_SUNW_SYMTAB is %p\n",
2311d9452f23SEdward Pilatowicz (void *)(uintptr_t)d.d_un.d_ptr);
2312d9452f23SEdward Pilatowicz break;
2313d9452f23SEdward Pilatowicz case DT_SYMTAB:
2314d9452f23SEdward Pilatowicz dprintf("DT_SYMTAB is %p\n",
2315d9452f23SEdward Pilatowicz (void *)(uintptr_t)d.d_un.d_ptr);
2316d9452f23SEdward Pilatowicz break;
2317d9452f23SEdward Pilatowicz case DT_HASH:
2318d9452f23SEdward Pilatowicz dprintf("DT_HASH is %p\n",
2319d9452f23SEdward Pilatowicz (void *)(uintptr_t)d.d_un.d_ptr);
2320d9452f23SEdward Pilatowicz break;
23217c478bd9Sstevel@tonic-gate }
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate
23247c478bd9Sstevel@tonic-gate dprintf("_DYNAMIC found at %p, %lu entries, DT_JMPREL = %p\n",
23257c478bd9Sstevel@tonic-gate (void *)dynaddr, (ulong_t)ndyn, (void *)fptr->file_jmp_rel);
23267c478bd9Sstevel@tonic-gate }
23277c478bd9Sstevel@tonic-gate
23287c478bd9Sstevel@tonic-gate done:
23297c478bd9Sstevel@tonic-gate free(cache);
23307c478bd9Sstevel@tonic-gate return;
23317c478bd9Sstevel@tonic-gate
23327c478bd9Sstevel@tonic-gate bad:
23337c478bd9Sstevel@tonic-gate if (cache != NULL)
23347c478bd9Sstevel@tonic-gate free(cache);
23357c478bd9Sstevel@tonic-gate
23367c478bd9Sstevel@tonic-gate (void) elf_end(elf);
23377c478bd9Sstevel@tonic-gate fptr->file_elf = NULL;
23387c478bd9Sstevel@tonic-gate if (fptr->file_elfmem != NULL) {
23397c478bd9Sstevel@tonic-gate free(fptr->file_elfmem);
23407c478bd9Sstevel@tonic-gate fptr->file_elfmem = NULL;
23417c478bd9Sstevel@tonic-gate }
23427c478bd9Sstevel@tonic-gate (void) close(fptr->file_fd);
2343e68846a8SRobert Mustacchi if (fptr->file_dbgelf != NULL)
2344e68846a8SRobert Mustacchi (void) elf_end(fptr->file_dbgelf);
2345e68846a8SRobert Mustacchi fptr->file_dbgelf = NULL;
2346e68846a8SRobert Mustacchi if (fptr->file_dbgfile >= 0)
2347e68846a8SRobert Mustacchi (void) close(fptr->file_dbgfile);
23487c478bd9Sstevel@tonic-gate fptr->file_fd = -1;
2349e68846a8SRobert Mustacchi fptr->file_dbgfile = -1;
23507c478bd9Sstevel@tonic-gate }
23517c478bd9Sstevel@tonic-gate
23527c478bd9Sstevel@tonic-gate /*
23537c478bd9Sstevel@tonic-gate * Given a process virtual address, return the map_info_t containing it.
23547c478bd9Sstevel@tonic-gate * If none found, return NULL.
23557c478bd9Sstevel@tonic-gate */
23567c478bd9Sstevel@tonic-gate map_info_t *
Paddr2mptr(struct ps_prochandle * P,uintptr_t addr)23577c478bd9Sstevel@tonic-gate Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
23587c478bd9Sstevel@tonic-gate {
23597c478bd9Sstevel@tonic-gate int lo = 0;
23607c478bd9Sstevel@tonic-gate int hi = P->map_count - 1;
23617c478bd9Sstevel@tonic-gate int mid;
23627c478bd9Sstevel@tonic-gate map_info_t *mp;
23637c478bd9Sstevel@tonic-gate
23647c478bd9Sstevel@tonic-gate while (lo <= hi) {
23657c478bd9Sstevel@tonic-gate
23667c478bd9Sstevel@tonic-gate mid = (lo + hi) / 2;
23677c478bd9Sstevel@tonic-gate mp = &P->mappings[mid];
23687c478bd9Sstevel@tonic-gate
23697c478bd9Sstevel@tonic-gate /* check that addr is in [vaddr, vaddr + size) */
23707c478bd9Sstevel@tonic-gate if ((addr - mp->map_pmap.pr_vaddr) < mp->map_pmap.pr_size)
23717c478bd9Sstevel@tonic-gate return (mp);
23727c478bd9Sstevel@tonic-gate
23737c478bd9Sstevel@tonic-gate if (addr < mp->map_pmap.pr_vaddr)
23747c478bd9Sstevel@tonic-gate hi = mid - 1;
23757c478bd9Sstevel@tonic-gate else
23767c478bd9Sstevel@tonic-gate lo = mid + 1;
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate
23797c478bd9Sstevel@tonic-gate return (NULL);
23807c478bd9Sstevel@tonic-gate }
23817c478bd9Sstevel@tonic-gate
23827c478bd9Sstevel@tonic-gate /*
23837c478bd9Sstevel@tonic-gate * Return the map_info_t for the executable file.
23847c478bd9Sstevel@tonic-gate * If not found, return NULL.
23857c478bd9Sstevel@tonic-gate */
23867c478bd9Sstevel@tonic-gate static map_info_t *
exec_map(struct ps_prochandle * P)23877c478bd9Sstevel@tonic-gate exec_map(struct ps_prochandle *P)
23887c478bd9Sstevel@tonic-gate {
23897c478bd9Sstevel@tonic-gate uint_t i;
23907c478bd9Sstevel@tonic-gate map_info_t *mptr;
23917c478bd9Sstevel@tonic-gate map_info_t *mold = NULL;
23927c478bd9Sstevel@tonic-gate file_info_t *fptr;
23937c478bd9Sstevel@tonic-gate uintptr_t base;
23947c478bd9Sstevel@tonic-gate
23957c478bd9Sstevel@tonic-gate for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
23967c478bd9Sstevel@tonic-gate if (mptr->map_pmap.pr_mapname[0] == '\0')
23977c478bd9Sstevel@tonic-gate continue;
23987c478bd9Sstevel@tonic-gate if (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) {
23997c478bd9Sstevel@tonic-gate if ((fptr = mptr->map_file) != NULL &&
24007c478bd9Sstevel@tonic-gate fptr->file_lo != NULL) {
24017c478bd9Sstevel@tonic-gate base = fptr->file_lo->rl_base;
24027c478bd9Sstevel@tonic-gate if (base >= mptr->map_pmap.pr_vaddr &&
24037c478bd9Sstevel@tonic-gate base < mptr->map_pmap.pr_vaddr +
24047c478bd9Sstevel@tonic-gate mptr->map_pmap.pr_size) /* text space */
24057c478bd9Sstevel@tonic-gate return (mptr);
24067c478bd9Sstevel@tonic-gate mold = mptr; /* must be the data */
24077c478bd9Sstevel@tonic-gate continue;
24087c478bd9Sstevel@tonic-gate }
24097c478bd9Sstevel@tonic-gate /* This is a poor way to test for text space */
24107c478bd9Sstevel@tonic-gate if (!(mptr->map_pmap.pr_mflags & MA_EXEC) ||
24117c478bd9Sstevel@tonic-gate (mptr->map_pmap.pr_mflags & MA_WRITE)) {
24127c478bd9Sstevel@tonic-gate mold = mptr;
24137c478bd9Sstevel@tonic-gate continue;
24147c478bd9Sstevel@tonic-gate }
24157c478bd9Sstevel@tonic-gate return (mptr);
24167c478bd9Sstevel@tonic-gate }
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate
24197c478bd9Sstevel@tonic-gate return (mold);
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate
24227c478bd9Sstevel@tonic-gate /*
24237c478bd9Sstevel@tonic-gate * Given a shared object name, return the map_info_t for it. If no matching
24247c478bd9Sstevel@tonic-gate * object is found, return NULL. Normally, the link maps contain the full
24257c478bd9Sstevel@tonic-gate * object pathname, e.g. /usr/lib/libc.so.1. We allow the object name to
24267c478bd9Sstevel@tonic-gate * take one of the following forms:
24277c478bd9Sstevel@tonic-gate *
24287c478bd9Sstevel@tonic-gate * 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
24297c478bd9Sstevel@tonic-gate * 2. An exact basename match: "libc.so.1"
24307c478bd9Sstevel@tonic-gate * 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
24317c478bd9Sstevel@tonic-gate * 4. The literal string "a.out" is an alias for the executable mapping
24327c478bd9Sstevel@tonic-gate *
24337c478bd9Sstevel@tonic-gate * The third case is a convenience for callers and may not be necessary.
24347c478bd9Sstevel@tonic-gate *
24357c478bd9Sstevel@tonic-gate * As the exact same object name may be loaded on different link maps (see
24367c478bd9Sstevel@tonic-gate * dlmopen(3DL)), we also allow the caller to resolve the object name by
24377c478bd9Sstevel@tonic-gate * specifying a particular link map id. If lmid is PR_LMID_EVERY, the
24387c478bd9Sstevel@tonic-gate * first matching name will be returned, regardless of the link map id.
24397c478bd9Sstevel@tonic-gate */
24407c478bd9Sstevel@tonic-gate static map_info_t *
object_to_map(struct ps_prochandle * P,Lmid_t lmid,const char * objname)24417c478bd9Sstevel@tonic-gate object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
24427c478bd9Sstevel@tonic-gate {
24437c478bd9Sstevel@tonic-gate map_info_t *mp;
24447c478bd9Sstevel@tonic-gate file_info_t *fp;
24457c478bd9Sstevel@tonic-gate size_t objlen;
24467c478bd9Sstevel@tonic-gate uint_t i;
24477c478bd9Sstevel@tonic-gate
24487c478bd9Sstevel@tonic-gate /*
24499acbbeafSnn35248 * If we have no rtld_db, then always treat a request as one for all
24509acbbeafSnn35248 * link maps.
24519acbbeafSnn35248 */
24529acbbeafSnn35248 if (P->rap == NULL)
24539acbbeafSnn35248 lmid = PR_LMID_EVERY;
24549acbbeafSnn35248
24559acbbeafSnn35248 /*
24567c478bd9Sstevel@tonic-gate * First pass: look for exact matches of the entire pathname or
24577c478bd9Sstevel@tonic-gate * basename (cases 1 and 2 above):
24587c478bd9Sstevel@tonic-gate */
24597c478bd9Sstevel@tonic-gate for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
24607c478bd9Sstevel@tonic-gate
24617c478bd9Sstevel@tonic-gate if (mp->map_pmap.pr_mapname[0] == '\0' ||
2462186f7fbfSEdward Pilatowicz (fp = mp->map_file) == NULL ||
2463186f7fbfSEdward Pilatowicz ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
24647c478bd9Sstevel@tonic-gate continue;
24657c478bd9Sstevel@tonic-gate
24667c478bd9Sstevel@tonic-gate if (lmid != PR_LMID_EVERY &&
24677c478bd9Sstevel@tonic-gate (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
24687c478bd9Sstevel@tonic-gate continue;
24697c478bd9Sstevel@tonic-gate
24707c478bd9Sstevel@tonic-gate /*
24717c478bd9Sstevel@tonic-gate * If we match, return the primary text mapping; otherwise
24727c478bd9Sstevel@tonic-gate * just return the mapping we matched.
24737c478bd9Sstevel@tonic-gate */
2474186f7fbfSEdward Pilatowicz if ((fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) ||
2475186f7fbfSEdward Pilatowicz (fp->file_rbase && strcmp(fp->file_rbase, objname) == 0) ||
2476186f7fbfSEdward Pilatowicz (fp->file_lname && strcmp(fp->file_lname, objname) == 0) ||
2477186f7fbfSEdward Pilatowicz (fp->file_rname && strcmp(fp->file_rname, objname) == 0))
24787c478bd9Sstevel@tonic-gate return (fp->file_map ? fp->file_map : mp);
24797c478bd9Sstevel@tonic-gate }
24807c478bd9Sstevel@tonic-gate
24817c478bd9Sstevel@tonic-gate objlen = strlen(objname);
24827c478bd9Sstevel@tonic-gate
24837c478bd9Sstevel@tonic-gate /*
24847c478bd9Sstevel@tonic-gate * Second pass: look for partial matches (case 3 above):
24857c478bd9Sstevel@tonic-gate */
24867c478bd9Sstevel@tonic-gate for (i = 0, mp = P->mappings; i < P->map_count; i++, mp++) {
24877c478bd9Sstevel@tonic-gate
24887c478bd9Sstevel@tonic-gate if (mp->map_pmap.pr_mapname[0] == '\0' ||
2489186f7fbfSEdward Pilatowicz (fp = mp->map_file) == NULL ||
2490186f7fbfSEdward Pilatowicz ((fp->file_lname == NULL) && (fp->file_rname == NULL)))
24917c478bd9Sstevel@tonic-gate continue;
24927c478bd9Sstevel@tonic-gate
24937c478bd9Sstevel@tonic-gate if (lmid != PR_LMID_EVERY &&
24947c478bd9Sstevel@tonic-gate (fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
24957c478bd9Sstevel@tonic-gate continue;
24967c478bd9Sstevel@tonic-gate
24977c478bd9Sstevel@tonic-gate /*
24987c478bd9Sstevel@tonic-gate * If we match, return the primary text mapping; otherwise
24997c478bd9Sstevel@tonic-gate * just return the mapping we matched.
25007c478bd9Sstevel@tonic-gate */
2501186f7fbfSEdward Pilatowicz if ((fp->file_lbase != NULL) &&
2502186f7fbfSEdward Pilatowicz (strncmp(fp->file_lbase, objname, objlen) == 0) &&
2503186f7fbfSEdward Pilatowicz (fp->file_lbase[objlen] == '.'))
2504186f7fbfSEdward Pilatowicz return (fp->file_map ? fp->file_map : mp);
2505186f7fbfSEdward Pilatowicz if ((fp->file_rbase != NULL) &&
2506186f7fbfSEdward Pilatowicz (strncmp(fp->file_rbase, objname, objlen) == 0) &&
2507186f7fbfSEdward Pilatowicz (fp->file_rbase[objlen] == '.'))
25087c478bd9Sstevel@tonic-gate return (fp->file_map ? fp->file_map : mp);
25097c478bd9Sstevel@tonic-gate }
25107c478bd9Sstevel@tonic-gate
25117c478bd9Sstevel@tonic-gate /*
25127c478bd9Sstevel@tonic-gate * One last check: we allow "a.out" to always alias the executable,
25137c478bd9Sstevel@tonic-gate * assuming this name was not in use for something else.
25147c478bd9Sstevel@tonic-gate */
25157c478bd9Sstevel@tonic-gate if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
25167c478bd9Sstevel@tonic-gate (strcmp(objname, "a.out") == 0))
25177c478bd9Sstevel@tonic-gate return (P->map_exec);
25187c478bd9Sstevel@tonic-gate
25197c478bd9Sstevel@tonic-gate return (NULL);
25207c478bd9Sstevel@tonic-gate }
25217c478bd9Sstevel@tonic-gate
25227c478bd9Sstevel@tonic-gate static map_info_t *
object_name_to_map(struct ps_prochandle * P,Lmid_t lmid,const char * name)25237c478bd9Sstevel@tonic-gate object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate map_info_t *mptr;
25267c478bd9Sstevel@tonic-gate
25277c478bd9Sstevel@tonic-gate if (!P->info_valid)
25287c478bd9Sstevel@tonic-gate Pupdate_maps(P);
25297c478bd9Sstevel@tonic-gate
25307c478bd9Sstevel@tonic-gate if (P->map_exec == NULL && ((mptr = Paddr2mptr(P,
25317c478bd9Sstevel@tonic-gate Pgetauxval(P, AT_ENTRY))) != NULL || (mptr = exec_map(P)) != NULL))
25327c478bd9Sstevel@tonic-gate P->map_exec = mptr;
25337c478bd9Sstevel@tonic-gate
25347c478bd9Sstevel@tonic-gate if (P->map_ldso == NULL && (mptr = Paddr2mptr(P,
25357c478bd9Sstevel@tonic-gate Pgetauxval(P, AT_BASE))) != NULL)
25367c478bd9Sstevel@tonic-gate P->map_ldso = mptr;
25377c478bd9Sstevel@tonic-gate
25387c478bd9Sstevel@tonic-gate if (name == PR_OBJ_EXEC)
25397c478bd9Sstevel@tonic-gate mptr = P->map_exec;
25407c478bd9Sstevel@tonic-gate else if (name == PR_OBJ_LDSO)
25417c478bd9Sstevel@tonic-gate mptr = P->map_ldso;
2542b45b146bSjhaslam else if (Prd_agent(P) != NULL || P->state == PS_IDLE)
25439acbbeafSnn35248 mptr = object_to_map(P, lmid, name);
2544b45b146bSjhaslam else
2545b45b146bSjhaslam mptr = NULL;
25467c478bd9Sstevel@tonic-gate
25477c478bd9Sstevel@tonic-gate return (mptr);
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate
25507c478bd9Sstevel@tonic-gate /*
25517c478bd9Sstevel@tonic-gate * When two symbols are found by address, decide which one is to be preferred.
25527c478bd9Sstevel@tonic-gate */
25537c478bd9Sstevel@tonic-gate static GElf_Sym *
sym_prefer(GElf_Sym * sym1,char * name1,GElf_Sym * sym2,char * name2)25547c478bd9Sstevel@tonic-gate sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
25557c478bd9Sstevel@tonic-gate {
25567c478bd9Sstevel@tonic-gate /*
25577c478bd9Sstevel@tonic-gate * Prefer the non-NULL symbol.
25587c478bd9Sstevel@tonic-gate */
25597c478bd9Sstevel@tonic-gate if (sym1 == NULL)
25607c478bd9Sstevel@tonic-gate return (sym2);
25617c478bd9Sstevel@tonic-gate if (sym2 == NULL)
25627c478bd9Sstevel@tonic-gate return (sym1);
25637c478bd9Sstevel@tonic-gate
25647c478bd9Sstevel@tonic-gate /*
25657c478bd9Sstevel@tonic-gate * Defer to the sort ordering...
25667c478bd9Sstevel@tonic-gate */
25677c478bd9Sstevel@tonic-gate return (byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2);
25687c478bd9Sstevel@tonic-gate }
25697c478bd9Sstevel@tonic-gate
25707c478bd9Sstevel@tonic-gate /*
2571d7755b5aSrh87107 * Use a binary search to do the work of sym_by_addr().
25727c478bd9Sstevel@tonic-gate */
25737c478bd9Sstevel@tonic-gate static GElf_Sym *
sym_by_addr_binary(sym_tbl_t * symtab,GElf_Addr addr,GElf_Sym * symp,uint_t * idp)2574d7755b5aSrh87107 sym_by_addr_binary(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp,
2575d7755b5aSrh87107 uint_t *idp)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate GElf_Sym sym, osym;
25787c478bd9Sstevel@tonic-gate uint_t i, oid, *byaddr = symtab->sym_byaddr;
25797c478bd9Sstevel@tonic-gate int min, max, mid, omid, found = 0;
25807c478bd9Sstevel@tonic-gate
2581d51e9074Sab196087 if (symtab->sym_data_pri == NULL || symtab->sym_count == 0)
25827c478bd9Sstevel@tonic-gate return (NULL);
25837c478bd9Sstevel@tonic-gate
25847c478bd9Sstevel@tonic-gate min = 0;
25857c478bd9Sstevel@tonic-gate max = symtab->sym_count - 1;
25867c478bd9Sstevel@tonic-gate osym.st_value = 0;
25877c478bd9Sstevel@tonic-gate
25887c478bd9Sstevel@tonic-gate /*
25897c478bd9Sstevel@tonic-gate * We can't return when we've found a match, we have to continue
25907c478bd9Sstevel@tonic-gate * searching for the closest matching symbol.
25917c478bd9Sstevel@tonic-gate */
25927c478bd9Sstevel@tonic-gate while (min <= max) {
25937c478bd9Sstevel@tonic-gate mid = (max + min) / 2;
25947c478bd9Sstevel@tonic-gate
25957c478bd9Sstevel@tonic-gate i = byaddr[mid];
2596d51e9074Sab196087 (void) symtab_getsym(symtab, i, &sym);
25977c478bd9Sstevel@tonic-gate
25987c478bd9Sstevel@tonic-gate if (addr >= sym.st_value &&
25997c478bd9Sstevel@tonic-gate addr < sym.st_value + sym.st_size &&
26007c478bd9Sstevel@tonic-gate (!found || sym.st_value > osym.st_value)) {
26017c478bd9Sstevel@tonic-gate osym = sym;
26027c478bd9Sstevel@tonic-gate omid = mid;
26037c478bd9Sstevel@tonic-gate oid = i;
26047c478bd9Sstevel@tonic-gate found = 1;
26057c478bd9Sstevel@tonic-gate }
26067c478bd9Sstevel@tonic-gate
26077c478bd9Sstevel@tonic-gate if (addr < sym.st_value)
26087c478bd9Sstevel@tonic-gate max = mid - 1;
26097c478bd9Sstevel@tonic-gate else
26107c478bd9Sstevel@tonic-gate min = mid + 1;
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate
26137c478bd9Sstevel@tonic-gate if (!found)
26147c478bd9Sstevel@tonic-gate return (NULL);
26157c478bd9Sstevel@tonic-gate
26167c478bd9Sstevel@tonic-gate /*
26177c478bd9Sstevel@tonic-gate * There may be many symbols with identical values so we walk
26187c478bd9Sstevel@tonic-gate * backward in the byaddr table to find the best match.
26197c478bd9Sstevel@tonic-gate */
26207c478bd9Sstevel@tonic-gate do {
26217c478bd9Sstevel@tonic-gate sym = osym;
26227c478bd9Sstevel@tonic-gate i = oid;
26237c478bd9Sstevel@tonic-gate
26247c478bd9Sstevel@tonic-gate if (omid == 0)
26257c478bd9Sstevel@tonic-gate break;
26267c478bd9Sstevel@tonic-gate
26277c478bd9Sstevel@tonic-gate oid = byaddr[--omid];
2628d51e9074Sab196087 (void) symtab_getsym(symtab, oid, &osym);
26297c478bd9Sstevel@tonic-gate } while (addr >= osym.st_value &&
26307c478bd9Sstevel@tonic-gate addr < sym.st_value + osym.st_size &&
26317c478bd9Sstevel@tonic-gate osym.st_value == sym.st_value);
26327c478bd9Sstevel@tonic-gate
26337c478bd9Sstevel@tonic-gate *symp = sym;
26347c478bd9Sstevel@tonic-gate if (idp != NULL)
26357c478bd9Sstevel@tonic-gate *idp = i;
26367c478bd9Sstevel@tonic-gate return (symp);
26377c478bd9Sstevel@tonic-gate }
26387c478bd9Sstevel@tonic-gate
26397c478bd9Sstevel@tonic-gate /*
2640d7755b5aSrh87107 * Use a linear search to do the work of sym_by_addr().
26417c478bd9Sstevel@tonic-gate */
26427c478bd9Sstevel@tonic-gate static GElf_Sym *
sym_by_addr_linear(sym_tbl_t * symtab,GElf_Addr addr,GElf_Sym * symbolp,uint_t * idp)2643d7755b5aSrh87107 sym_by_addr_linear(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symbolp,
2644d7755b5aSrh87107 uint_t *idp)
2645d7755b5aSrh87107 {
2646d7755b5aSrh87107 size_t symn = symtab->sym_symn;
2647d7755b5aSrh87107 char *strs = symtab->sym_strs;
2648d7755b5aSrh87107 GElf_Sym sym, *symp = NULL;
2649d7755b5aSrh87107 GElf_Sym osym, *osymp = NULL;
2650d7755b5aSrh87107 int i, id;
2651d7755b5aSrh87107
2652d7755b5aSrh87107 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2653d7755b5aSrh87107 return (NULL);
2654d7755b5aSrh87107
2655d7755b5aSrh87107 for (i = 0; i < symn; i++) {
2656d7755b5aSrh87107 if ((symp = symtab_getsym(symtab, i, &sym)) != NULL) {
2657d7755b5aSrh87107 if (addr >= sym.st_value &&
2658d7755b5aSrh87107 addr < sym.st_value + sym.st_size) {
2659d7755b5aSrh87107 if (osymp)
2660d7755b5aSrh87107 symp = sym_prefer(
2661d7755b5aSrh87107 symp, strs + symp->st_name,
2662d7755b5aSrh87107 osymp, strs + osymp->st_name);
2663d7755b5aSrh87107 if (symp != osymp) {
2664d7755b5aSrh87107 osym = sym;
2665d7755b5aSrh87107 osymp = &osym;
2666d7755b5aSrh87107 id = i;
2667d7755b5aSrh87107 }
2668d7755b5aSrh87107 }
2669d7755b5aSrh87107 }
2670d7755b5aSrh87107 }
2671d7755b5aSrh87107 if (osymp) {
2672d7755b5aSrh87107 *symbolp = osym;
2673d7755b5aSrh87107 if (idp)
2674d7755b5aSrh87107 *idp = id;
2675d7755b5aSrh87107 return (symbolp);
2676d7755b5aSrh87107 }
2677d7755b5aSrh87107 return (NULL);
2678d7755b5aSrh87107 }
2679d7755b5aSrh87107
2680d7755b5aSrh87107 /*
2681d7755b5aSrh87107 * Look up a symbol by address in the specified symbol table.
2682d7755b5aSrh87107 * Adjustment to 'addr' must already have been made for the
2683d7755b5aSrh87107 * offset of the symbol if this is a dynamic library symbol table.
2684d7755b5aSrh87107 *
2685d7755b5aSrh87107 * Use a linear or a binary search depending on whether or not we
2686d7755b5aSrh87107 * chose to sort the table in optimize_symtab().
2687d7755b5aSrh87107 */
2688d7755b5aSrh87107 static GElf_Sym *
sym_by_addr(sym_tbl_t * symtab,GElf_Addr addr,GElf_Sym * symp,uint_t * idp)2689d7755b5aSrh87107 sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
2690d7755b5aSrh87107 {
2691d7755b5aSrh87107 if (_libproc_no_qsort) {
2692d7755b5aSrh87107 return (sym_by_addr_linear(symtab, addr, symp, idp));
2693d7755b5aSrh87107 } else {
2694d7755b5aSrh87107 return (sym_by_addr_binary(symtab, addr, symp, idp));
2695d7755b5aSrh87107 }
2696d7755b5aSrh87107 }
2697d7755b5aSrh87107
2698d7755b5aSrh87107 /*
2699d7755b5aSrh87107 * Use a binary search to do the work of sym_by_name().
2700d7755b5aSrh87107 */
2701d7755b5aSrh87107 static GElf_Sym *
sym_by_name_binary(sym_tbl_t * symtab,const char * name,GElf_Sym * symp,uint_t * idp)2702d7755b5aSrh87107 sym_by_name_binary(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2703d7755b5aSrh87107 uint_t *idp)
27047c478bd9Sstevel@tonic-gate {
27057c478bd9Sstevel@tonic-gate char *strs = symtab->sym_strs;
27067c478bd9Sstevel@tonic-gate uint_t i, *byname = symtab->sym_byname;
27077c478bd9Sstevel@tonic-gate int min, mid, max, cmp;
27087c478bd9Sstevel@tonic-gate
2709d51e9074Sab196087 if (symtab->sym_data_pri == NULL || strs == NULL ||
2710d51e9074Sab196087 symtab->sym_count == 0)
27117c478bd9Sstevel@tonic-gate return (NULL);
27127c478bd9Sstevel@tonic-gate
27137c478bd9Sstevel@tonic-gate min = 0;
27147c478bd9Sstevel@tonic-gate max = symtab->sym_count - 1;
27157c478bd9Sstevel@tonic-gate
27167c478bd9Sstevel@tonic-gate while (min <= max) {
27177c478bd9Sstevel@tonic-gate mid = (max + min) / 2;
27187c478bd9Sstevel@tonic-gate
27197c478bd9Sstevel@tonic-gate i = byname[mid];
2720d51e9074Sab196087 (void) symtab_getsym(symtab, i, symp);
27217c478bd9Sstevel@tonic-gate
27227c478bd9Sstevel@tonic-gate if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
27237c478bd9Sstevel@tonic-gate if (idp != NULL)
27247c478bd9Sstevel@tonic-gate *idp = i;
27257c478bd9Sstevel@tonic-gate return (symp);
27267c478bd9Sstevel@tonic-gate }
27277c478bd9Sstevel@tonic-gate
27287c478bd9Sstevel@tonic-gate if (cmp < 0)
27297c478bd9Sstevel@tonic-gate max = mid - 1;
27307c478bd9Sstevel@tonic-gate else
27317c478bd9Sstevel@tonic-gate min = mid + 1;
27327c478bd9Sstevel@tonic-gate }
27337c478bd9Sstevel@tonic-gate
27347c478bd9Sstevel@tonic-gate return (NULL);
27357c478bd9Sstevel@tonic-gate }
27367c478bd9Sstevel@tonic-gate
27377c478bd9Sstevel@tonic-gate /*
2738d7755b5aSrh87107 * Use a linear search to do the work of sym_by_name().
2739d7755b5aSrh87107 */
2740d7755b5aSrh87107 static GElf_Sym *
sym_by_name_linear(sym_tbl_t * symtab,const char * name,GElf_Sym * symp,uint_t * idp)2741d7755b5aSrh87107 sym_by_name_linear(sym_tbl_t *symtab, const char *name, GElf_Sym *symp,
2742d7755b5aSrh87107 uint_t *idp)
2743d7755b5aSrh87107 {
2744d7755b5aSrh87107 size_t symn = symtab->sym_symn;
2745d7755b5aSrh87107 char *strs = symtab->sym_strs;
2746d7755b5aSrh87107 int i;
2747d7755b5aSrh87107
2748d7755b5aSrh87107 if (symtab->sym_data_pri == NULL || symn == 0 || strs == NULL)
2749d7755b5aSrh87107 return (NULL);
2750d7755b5aSrh87107
2751d7755b5aSrh87107 for (i = 0; i < symn; i++) {
2752d7755b5aSrh87107 if (symtab_getsym(symtab, i, symp) &&
2753d7755b5aSrh87107 strcmp(name, strs + symp->st_name) == 0) {
2754d7755b5aSrh87107 if (idp)
2755d7755b5aSrh87107 *idp = i;
2756d7755b5aSrh87107 return (symp);
2757d7755b5aSrh87107 }
2758d7755b5aSrh87107 }
2759d7755b5aSrh87107
2760d7755b5aSrh87107 return (NULL);
2761d7755b5aSrh87107 }
2762d7755b5aSrh87107
2763d7755b5aSrh87107 /*
2764d7755b5aSrh87107 * Look up a symbol by name in the specified symbol table.
2765d7755b5aSrh87107 *
2766d7755b5aSrh87107 * Use a linear or a binary search depending on whether or not we
2767d7755b5aSrh87107 * chose to sort the table in optimize_symtab().
2768d7755b5aSrh87107 */
2769d7755b5aSrh87107 static GElf_Sym *
sym_by_name(sym_tbl_t * symtab,const char * name,GElf_Sym * symp,uint_t * idp)2770d7755b5aSrh87107 sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
2771d7755b5aSrh87107 {
2772d7755b5aSrh87107 if (_libproc_no_qsort) {
2773d7755b5aSrh87107 return (sym_by_name_linear(symtab, name, symp, idp));
2774d7755b5aSrh87107 } else {
2775d7755b5aSrh87107 return (sym_by_name_binary(symtab, name, symp, idp));
2776d7755b5aSrh87107 }
2777d7755b5aSrh87107 }
2778d7755b5aSrh87107
2779d7755b5aSrh87107 /*
27807c478bd9Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose
27817c478bd9Sstevel@tonic-gate * value to value+size contain the address specified by addr.
27827c478bd9Sstevel@tonic-gate * Return values are:
27837c478bd9Sstevel@tonic-gate * sym_name_buffer containing the symbol name
27847c478bd9Sstevel@tonic-gate * GElf_Sym symbol table entry
27857c478bd9Sstevel@tonic-gate * prsyminfo_t ancillary symbol information
27867c478bd9Sstevel@tonic-gate * Returns 0 on success, -1 on failure.
27877c478bd9Sstevel@tonic-gate */
2788186f7fbfSEdward Pilatowicz static int
i_Pxlookup_by_addr(struct ps_prochandle * P,int lmresolve,uintptr_t addr,char * sym_name_buffer,size_t bufsize,GElf_Sym * symbolp,prsyminfo_t * sip)2789186f7fbfSEdward Pilatowicz i_Pxlookup_by_addr(
27907c478bd9Sstevel@tonic-gate struct ps_prochandle *P,
2791186f7fbfSEdward Pilatowicz int lmresolve, /* use resolve linker object names */
27927c478bd9Sstevel@tonic-gate uintptr_t addr, /* process address being sought */
27937c478bd9Sstevel@tonic-gate char *sym_name_buffer, /* buffer for the symbol name */
27947c478bd9Sstevel@tonic-gate size_t bufsize, /* size of sym_name_buffer */
27957c478bd9Sstevel@tonic-gate GElf_Sym *symbolp, /* returned symbol table entry */
27967c478bd9Sstevel@tonic-gate prsyminfo_t *sip) /* returned symbol info */
27977c478bd9Sstevel@tonic-gate {
27987c478bd9Sstevel@tonic-gate GElf_Sym *symp;
27997c478bd9Sstevel@tonic-gate char *name;
28007c478bd9Sstevel@tonic-gate GElf_Sym sym1, *sym1p = NULL;
28017c478bd9Sstevel@tonic-gate GElf_Sym sym2, *sym2p = NULL;
28027c478bd9Sstevel@tonic-gate char *name1 = NULL;
28037c478bd9Sstevel@tonic-gate char *name2 = NULL;
28047c478bd9Sstevel@tonic-gate uint_t i1;
28057c478bd9Sstevel@tonic-gate uint_t i2;
28067c478bd9Sstevel@tonic-gate map_info_t *mptr;
28077c478bd9Sstevel@tonic-gate file_info_t *fptr;
28087c478bd9Sstevel@tonic-gate
28097c478bd9Sstevel@tonic-gate (void) Prd_agent(P);
28107c478bd9Sstevel@tonic-gate
28117c478bd9Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) == NULL || /* no such address */
28127c478bd9Sstevel@tonic-gate (fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
28137c478bd9Sstevel@tonic-gate fptr->file_elf == NULL) /* not an ELF file */
28147c478bd9Sstevel@tonic-gate return (-1);
28157c478bd9Sstevel@tonic-gate
28167c478bd9Sstevel@tonic-gate /*
28177c478bd9Sstevel@tonic-gate * Adjust the address by the load object base address in
28187c478bd9Sstevel@tonic-gate * case the address turns out to be in a shared library.
28197c478bd9Sstevel@tonic-gate */
28207c478bd9Sstevel@tonic-gate addr -= fptr->file_dyn_base;
28217c478bd9Sstevel@tonic-gate
28227c478bd9Sstevel@tonic-gate /*
28237c478bd9Sstevel@tonic-gate * Search both symbol tables, symtab first, then dynsym.
28247c478bd9Sstevel@tonic-gate */
28257c478bd9Sstevel@tonic-gate if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
28267c478bd9Sstevel@tonic-gate name1 = fptr->file_symtab.sym_strs + sym1.st_name;
28277c478bd9Sstevel@tonic-gate if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
28287c478bd9Sstevel@tonic-gate name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
28297c478bd9Sstevel@tonic-gate
28307c478bd9Sstevel@tonic-gate if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
28317c478bd9Sstevel@tonic-gate return (-1);
28327c478bd9Sstevel@tonic-gate
28337c478bd9Sstevel@tonic-gate name = (symp == sym1p) ? name1 : name2;
28347c478bd9Sstevel@tonic-gate if (bufsize > 0) {
28357c478bd9Sstevel@tonic-gate (void) strncpy(sym_name_buffer, name, bufsize);
28367c478bd9Sstevel@tonic-gate sym_name_buffer[bufsize - 1] = '\0';
28377c478bd9Sstevel@tonic-gate }
28387c478bd9Sstevel@tonic-gate
28397c478bd9Sstevel@tonic-gate *symbolp = *symp;
28407c478bd9Sstevel@tonic-gate if (sip != NULL) {
28417c478bd9Sstevel@tonic-gate sip->prs_name = bufsize == 0 ? NULL : sym_name_buffer;
2842186f7fbfSEdward Pilatowicz if (lmresolve && (fptr->file_rname != NULL))
2843186f7fbfSEdward Pilatowicz sip->prs_object = fptr->file_rbase;
2844186f7fbfSEdward Pilatowicz else
28457c478bd9Sstevel@tonic-gate sip->prs_object = fptr->file_lbase;
28467c478bd9Sstevel@tonic-gate sip->prs_id = (symp == sym1p) ? i1 : i2;
28477c478bd9Sstevel@tonic-gate sip->prs_table = (symp == sym1p) ? PR_SYMTAB : PR_DYNSYM;
28487c478bd9Sstevel@tonic-gate sip->prs_lmid = (fptr->file_lo == NULL) ? LM_ID_BASE :
28497c478bd9Sstevel@tonic-gate fptr->file_lo->rl_lmident;
28507c478bd9Sstevel@tonic-gate }
28517c478bd9Sstevel@tonic-gate
28527c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
28537c478bd9Sstevel@tonic-gate symbolp->st_value += fptr->file_dyn_base;
28547c478bd9Sstevel@tonic-gate
28557c478bd9Sstevel@tonic-gate return (0);
28567c478bd9Sstevel@tonic-gate }
28577c478bd9Sstevel@tonic-gate
28587c478bd9Sstevel@tonic-gate int
Pxlookup_by_addr(struct ps_prochandle * P,uintptr_t addr,char * buf,size_t bufsize,GElf_Sym * symp,prsyminfo_t * sip)2859186f7fbfSEdward Pilatowicz Pxlookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2860186f7fbfSEdward Pilatowicz size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
28617c478bd9Sstevel@tonic-gate {
2862186f7fbfSEdward Pilatowicz return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, bufsize, symp, sip));
2863186f7fbfSEdward Pilatowicz }
2864186f7fbfSEdward Pilatowicz
2865186f7fbfSEdward Pilatowicz int
Pxlookup_by_addr_resolved(struct ps_prochandle * P,uintptr_t addr,char * buf,size_t bufsize,GElf_Sym * symp,prsyminfo_t * sip)2866186f7fbfSEdward Pilatowicz Pxlookup_by_addr_resolved(struct ps_prochandle *P, uintptr_t addr, char *buf,
2867186f7fbfSEdward Pilatowicz size_t bufsize, GElf_Sym *symp, prsyminfo_t *sip)
2868186f7fbfSEdward Pilatowicz {
2869186f7fbfSEdward Pilatowicz return (i_Pxlookup_by_addr(P, B_TRUE, addr, buf, bufsize, symp, sip));
2870186f7fbfSEdward Pilatowicz }
2871186f7fbfSEdward Pilatowicz
2872186f7fbfSEdward Pilatowicz int
Plookup_by_addr(struct ps_prochandle * P,uintptr_t addr,char * buf,size_t size,GElf_Sym * symp)2873186f7fbfSEdward Pilatowicz Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, char *buf,
2874186f7fbfSEdward Pilatowicz size_t size, GElf_Sym *symp)
2875186f7fbfSEdward Pilatowicz {
2876186f7fbfSEdward Pilatowicz return (i_Pxlookup_by_addr(P, B_FALSE, addr, buf, size, symp, NULL));
28777c478bd9Sstevel@tonic-gate }
28787c478bd9Sstevel@tonic-gate
28797c478bd9Sstevel@tonic-gate /*
28807c478bd9Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose name matches the
28817c478bd9Sstevel@tonic-gate * specified name and whose object and link map optionally match the specified
28827c478bd9Sstevel@tonic-gate * parameters. On success, the function returns 0 and fills in the GElf_Sym
28837c478bd9Sstevel@tonic-gate * symbol table entry. On failure, -1 is returned.
28847c478bd9Sstevel@tonic-gate */
28857c478bd9Sstevel@tonic-gate int
Pxlookup_by_name(struct ps_prochandle * P,Lmid_t lmid,const char * oname,const char * sname,GElf_Sym * symp,prsyminfo_t * sip)28867c478bd9Sstevel@tonic-gate Pxlookup_by_name(
28877c478bd9Sstevel@tonic-gate struct ps_prochandle *P,
28887c478bd9Sstevel@tonic-gate Lmid_t lmid, /* link map to match, or -1 for any */
28897c478bd9Sstevel@tonic-gate const char *oname, /* load object name */
28907c478bd9Sstevel@tonic-gate const char *sname, /* symbol name */
28917c478bd9Sstevel@tonic-gate GElf_Sym *symp, /* returned symbol table entry */
28927c478bd9Sstevel@tonic-gate prsyminfo_t *sip) /* returned symbol info */
28937c478bd9Sstevel@tonic-gate {
28947c478bd9Sstevel@tonic-gate map_info_t *mptr;
28957c478bd9Sstevel@tonic-gate file_info_t *fptr;
28967c478bd9Sstevel@tonic-gate int cnt;
28977c478bd9Sstevel@tonic-gate
28987c478bd9Sstevel@tonic-gate GElf_Sym sym;
28997c478bd9Sstevel@tonic-gate prsyminfo_t si;
29007c478bd9Sstevel@tonic-gate int rv = -1;
29017c478bd9Sstevel@tonic-gate uint_t id;
29027c478bd9Sstevel@tonic-gate
29037c478bd9Sstevel@tonic-gate if (oname == PR_OBJ_EVERY) {
29047c478bd9Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */
29057c478bd9Sstevel@tonic-gate (void) Prd_agent(P);
29067c478bd9Sstevel@tonic-gate cnt = P->num_files;
290750d4d24eSRobert Mustacchi fptr = list_head(&P->file_head);
29087c478bd9Sstevel@tonic-gate } else {
29097c478bd9Sstevel@tonic-gate cnt = 1;
29107c478bd9Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, oname)) == NULL ||
29117c478bd9Sstevel@tonic-gate (fptr = build_map_symtab(P, mptr)) == NULL)
29127c478bd9Sstevel@tonic-gate return (-1);
29137c478bd9Sstevel@tonic-gate }
29147c478bd9Sstevel@tonic-gate
29157c478bd9Sstevel@tonic-gate /*
29167c478bd9Sstevel@tonic-gate * Iterate through the loaded object files and look for the symbol
29177c478bd9Sstevel@tonic-gate * name in the .symtab and .dynsym of each. If we encounter a match
29187c478bd9Sstevel@tonic-gate * with SHN_UNDEF, keep looking in hopes of finding a better match.
29197c478bd9Sstevel@tonic-gate * This means that a name such as "puts" will match the puts function
29207c478bd9Sstevel@tonic-gate * in libc instead of matching the puts PLT entry in the a.out file.
29217c478bd9Sstevel@tonic-gate */
292250d4d24eSRobert Mustacchi for (; cnt > 0; cnt--, fptr = list_next(&P->file_head, fptr)) {
29237c478bd9Sstevel@tonic-gate Pbuild_file_symtab(P, fptr);
29247c478bd9Sstevel@tonic-gate
29257c478bd9Sstevel@tonic-gate if (fptr->file_elf == NULL)
29267c478bd9Sstevel@tonic-gate continue;
29277c478bd9Sstevel@tonic-gate
29287c478bd9Sstevel@tonic-gate if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
29297c478bd9Sstevel@tonic-gate lmid != fptr->file_lo->rl_lmident)
29307c478bd9Sstevel@tonic-gate continue;
29317c478bd9Sstevel@tonic-gate
2932d51e9074Sab196087 if (fptr->file_symtab.sym_data_pri != NULL &&
29337c478bd9Sstevel@tonic-gate sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
29347c478bd9Sstevel@tonic-gate if (sip != NULL) {
29357c478bd9Sstevel@tonic-gate sip->prs_id = id;
29367c478bd9Sstevel@tonic-gate sip->prs_table = PR_SYMTAB;
29377c478bd9Sstevel@tonic-gate sip->prs_object = oname;
29387c478bd9Sstevel@tonic-gate sip->prs_name = sname;
29397c478bd9Sstevel@tonic-gate sip->prs_lmid = fptr->file_lo == NULL ?
29407c478bd9Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident;
29417c478bd9Sstevel@tonic-gate }
2942d51e9074Sab196087 } else if (fptr->file_dynsym.sym_data_pri != NULL &&
29437c478bd9Sstevel@tonic-gate sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
29447c478bd9Sstevel@tonic-gate if (sip != NULL) {
29457c478bd9Sstevel@tonic-gate sip->prs_id = id;
29467c478bd9Sstevel@tonic-gate sip->prs_table = PR_DYNSYM;
29477c478bd9Sstevel@tonic-gate sip->prs_object = oname;
29487c478bd9Sstevel@tonic-gate sip->prs_name = sname;
29497c478bd9Sstevel@tonic-gate sip->prs_lmid = fptr->file_lo == NULL ?
29507c478bd9Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident;
29517c478bd9Sstevel@tonic-gate }
29527c478bd9Sstevel@tonic-gate } else {
29537c478bd9Sstevel@tonic-gate continue;
29547c478bd9Sstevel@tonic-gate }
29557c478bd9Sstevel@tonic-gate
29567c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(symp->st_info) != STT_TLS)
29577c478bd9Sstevel@tonic-gate symp->st_value += fptr->file_dyn_base;
29587c478bd9Sstevel@tonic-gate
29597c478bd9Sstevel@tonic-gate if (symp->st_shndx != SHN_UNDEF)
29607c478bd9Sstevel@tonic-gate return (0);
29617c478bd9Sstevel@tonic-gate
29627c478bd9Sstevel@tonic-gate if (rv != 0) {
29637c478bd9Sstevel@tonic-gate if (sip != NULL)
29647c478bd9Sstevel@tonic-gate si = *sip;
29657c478bd9Sstevel@tonic-gate sym = *symp;
29667c478bd9Sstevel@tonic-gate rv = 0;
29677c478bd9Sstevel@tonic-gate }
29687c478bd9Sstevel@tonic-gate }
29697c478bd9Sstevel@tonic-gate
29707c478bd9Sstevel@tonic-gate if (rv == 0) {
29717c478bd9Sstevel@tonic-gate if (sip != NULL)
29727c478bd9Sstevel@tonic-gate *sip = si;
29737c478bd9Sstevel@tonic-gate *symp = sym;
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate
29767c478bd9Sstevel@tonic-gate return (rv);
29777c478bd9Sstevel@tonic-gate }
29787c478bd9Sstevel@tonic-gate
29797c478bd9Sstevel@tonic-gate /*
29807c478bd9Sstevel@tonic-gate * Search the process symbol tables looking for a symbol whose name matches the
29817c478bd9Sstevel@tonic-gate * specified name, but without any restriction on the link map id.
29827c478bd9Sstevel@tonic-gate */
29837c478bd9Sstevel@tonic-gate int
Plookup_by_name(struct ps_prochandle * P,const char * object,const char * symbol,GElf_Sym * symp)29847c478bd9Sstevel@tonic-gate Plookup_by_name(struct ps_prochandle *P, const char *object,
29857c478bd9Sstevel@tonic-gate const char *symbol, GElf_Sym *symp)
29867c478bd9Sstevel@tonic-gate {
29877c478bd9Sstevel@tonic-gate return (Pxlookup_by_name(P, PR_LMID_EVERY, object, symbol, symp, NULL));
29887c478bd9Sstevel@tonic-gate }
29897c478bd9Sstevel@tonic-gate
29907c478bd9Sstevel@tonic-gate /*
29917c478bd9Sstevel@tonic-gate * Iterate over the process's address space mappings.
29927c478bd9Sstevel@tonic-gate */
2993186f7fbfSEdward Pilatowicz static int
i_Pmapping_iter(struct ps_prochandle * P,boolean_t lmresolve,proc_map_f * func,void * cd)2994186f7fbfSEdward Pilatowicz i_Pmapping_iter(struct ps_prochandle *P, boolean_t lmresolve,
2995186f7fbfSEdward Pilatowicz proc_map_f *func, void *cd)
29967c478bd9Sstevel@tonic-gate {
29977c478bd9Sstevel@tonic-gate map_info_t *mptr;
29987c478bd9Sstevel@tonic-gate file_info_t *fptr;
29997c478bd9Sstevel@tonic-gate char *object_name;
30007c478bd9Sstevel@tonic-gate int rc = 0;
30017c478bd9Sstevel@tonic-gate int i;
30027c478bd9Sstevel@tonic-gate
30037c478bd9Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */
30047c478bd9Sstevel@tonic-gate (void) Prd_agent(P);
30057c478bd9Sstevel@tonic-gate
30067c478bd9Sstevel@tonic-gate for (i = 0, mptr = P->mappings; i < P->map_count; i++, mptr++) {
30077c478bd9Sstevel@tonic-gate if ((fptr = mptr->map_file) == NULL)
30087c478bd9Sstevel@tonic-gate object_name = NULL;
3009186f7fbfSEdward Pilatowicz else if (lmresolve && (fptr->file_rname != NULL))
3010186f7fbfSEdward Pilatowicz object_name = fptr->file_rname;
30117c478bd9Sstevel@tonic-gate else
30127c478bd9Sstevel@tonic-gate object_name = fptr->file_lname;
30137c478bd9Sstevel@tonic-gate if ((rc = func(cd, &mptr->map_pmap, object_name)) != 0)
30147c478bd9Sstevel@tonic-gate return (rc);
30157c478bd9Sstevel@tonic-gate }
30167c478bd9Sstevel@tonic-gate return (0);
30177c478bd9Sstevel@tonic-gate }
30187c478bd9Sstevel@tonic-gate
3019186f7fbfSEdward Pilatowicz int
Pmapping_iter(struct ps_prochandle * P,proc_map_f * func,void * cd)3020186f7fbfSEdward Pilatowicz Pmapping_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
3021186f7fbfSEdward Pilatowicz {
3022186f7fbfSEdward Pilatowicz return (i_Pmapping_iter(P, B_FALSE, func, cd));
3023186f7fbfSEdward Pilatowicz }
3024186f7fbfSEdward Pilatowicz
3025186f7fbfSEdward Pilatowicz int
Pmapping_iter_resolved(struct ps_prochandle * P,proc_map_f * func,void * cd)3026186f7fbfSEdward Pilatowicz Pmapping_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
3027186f7fbfSEdward Pilatowicz {
3028186f7fbfSEdward Pilatowicz return (i_Pmapping_iter(P, B_TRUE, func, cd));
3029186f7fbfSEdward Pilatowicz }
3030186f7fbfSEdward Pilatowicz
30317c478bd9Sstevel@tonic-gate /*
30327c478bd9Sstevel@tonic-gate * Iterate over the process's mapped objects.
30337c478bd9Sstevel@tonic-gate */
3034186f7fbfSEdward Pilatowicz static int
i_Pobject_iter(struct ps_prochandle * P,boolean_t lmresolve,proc_map_f * func,void * cd)3035186f7fbfSEdward Pilatowicz i_Pobject_iter(struct ps_prochandle *P, boolean_t lmresolve,
3036186f7fbfSEdward Pilatowicz proc_map_f *func, void *cd)
30377c478bd9Sstevel@tonic-gate {
30387c478bd9Sstevel@tonic-gate map_info_t *mptr;
30397c478bd9Sstevel@tonic-gate file_info_t *fptr;
30407c478bd9Sstevel@tonic-gate int rc = 0;
30417c478bd9Sstevel@tonic-gate
30427c478bd9Sstevel@tonic-gate (void) Prd_agent(P); /* create file_info_t's for all the mappings */
30437c478bd9Sstevel@tonic-gate Pupdate_maps(P);
30447c478bd9Sstevel@tonic-gate
304550d4d24eSRobert Mustacchi for (fptr = list_head(&P->file_head); fptr != NULL;
304650d4d24eSRobert Mustacchi fptr = list_next(&P->file_head, fptr)) {
3047186f7fbfSEdward Pilatowicz const char *lname;
30487c478bd9Sstevel@tonic-gate
3049186f7fbfSEdward Pilatowicz if (lmresolve && (fptr->file_rname != NULL))
3050186f7fbfSEdward Pilatowicz lname = fptr->file_rname;
3051186f7fbfSEdward Pilatowicz else if (fptr->file_lname != NULL)
3052186f7fbfSEdward Pilatowicz lname = fptr->file_lname;
3053186f7fbfSEdward Pilatowicz else
3054186f7fbfSEdward Pilatowicz lname = "";
30557c478bd9Sstevel@tonic-gate
30567c478bd9Sstevel@tonic-gate if ((mptr = fptr->file_map) == NULL)
30577c478bd9Sstevel@tonic-gate continue;
30587c478bd9Sstevel@tonic-gate
30597c478bd9Sstevel@tonic-gate if ((rc = func(cd, &mptr->map_pmap, lname)) != 0)
30607c478bd9Sstevel@tonic-gate return (rc);
30618cb74972SJonathan Haslam
30628cb74972SJonathan Haslam if (!P->info_valid)
30638cb74972SJonathan Haslam Pupdate_maps(P);
30647c478bd9Sstevel@tonic-gate }
30657c478bd9Sstevel@tonic-gate return (0);
30667c478bd9Sstevel@tonic-gate }
30677c478bd9Sstevel@tonic-gate
3068186f7fbfSEdward Pilatowicz int
Pobject_iter(struct ps_prochandle * P,proc_map_f * func,void * cd)3069186f7fbfSEdward Pilatowicz Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
3070186f7fbfSEdward Pilatowicz {
3071186f7fbfSEdward Pilatowicz return (i_Pobject_iter(P, B_FALSE, func, cd));
3072186f7fbfSEdward Pilatowicz }
3073186f7fbfSEdward Pilatowicz
3074186f7fbfSEdward Pilatowicz int
Pobject_iter_resolved(struct ps_prochandle * P,proc_map_f * func,void * cd)3075186f7fbfSEdward Pilatowicz Pobject_iter_resolved(struct ps_prochandle *P, proc_map_f *func, void *cd)
3076186f7fbfSEdward Pilatowicz {
3077186f7fbfSEdward Pilatowicz return (i_Pobject_iter(P, B_TRUE, func, cd));
3078186f7fbfSEdward Pilatowicz }
3079186f7fbfSEdward Pilatowicz
3080186f7fbfSEdward Pilatowicz static char *
i_Pobjname(struct ps_prochandle * P,boolean_t lmresolve,uintptr_t addr,char * buffer,size_t bufsize)3081186f7fbfSEdward Pilatowicz i_Pobjname(struct ps_prochandle *P, boolean_t lmresolve, uintptr_t addr,
30827c478bd9Sstevel@tonic-gate char *buffer, size_t bufsize)
30837c478bd9Sstevel@tonic-gate {
30847c478bd9Sstevel@tonic-gate map_info_t *mptr;
30857c478bd9Sstevel@tonic-gate file_info_t *fptr;
30867c478bd9Sstevel@tonic-gate
30877c478bd9Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */
30887c478bd9Sstevel@tonic-gate (void) Prd_agent(P);
30897c478bd9Sstevel@tonic-gate
3090186f7fbfSEdward Pilatowicz if ((mptr = Paddr2mptr(P, addr)) == NULL)
3091186f7fbfSEdward Pilatowicz return (NULL);
3092186f7fbfSEdward Pilatowicz
3093186f7fbfSEdward Pilatowicz if (!lmresolve) {
3094186f7fbfSEdward Pilatowicz if (((fptr = mptr->map_file) == NULL) ||
3095186f7fbfSEdward Pilatowicz (fptr->file_lname == NULL))
3096186f7fbfSEdward Pilatowicz return (NULL);
3097186f7fbfSEdward Pilatowicz (void) strlcpy(buffer, fptr->file_lname, bufsize);
30987c478bd9Sstevel@tonic-gate return (buffer);
30997c478bd9Sstevel@tonic-gate }
3100186f7fbfSEdward Pilatowicz
3101186f7fbfSEdward Pilatowicz /* Check for a cached copy of the resolved path */
3102186f7fbfSEdward Pilatowicz if (Pfindmap(P, mptr, buffer, bufsize) != NULL)
3103186f7fbfSEdward Pilatowicz return (buffer);
3104186f7fbfSEdward Pilatowicz
31057c478bd9Sstevel@tonic-gate return (NULL);
31067c478bd9Sstevel@tonic-gate }
31077c478bd9Sstevel@tonic-gate
31087c478bd9Sstevel@tonic-gate /*
3109186f7fbfSEdward Pilatowicz * Given a virtual address, return the name of the underlying
3110186f7fbfSEdward Pilatowicz * mapped object (file) as provided by the dynamic linker.
3111186f7fbfSEdward Pilatowicz * Return NULL if we can't find any name information for the object.
3112186f7fbfSEdward Pilatowicz */
3113186f7fbfSEdward Pilatowicz char *
Pobjname(struct ps_prochandle * P,uintptr_t addr,char * buffer,size_t bufsize)3114186f7fbfSEdward Pilatowicz Pobjname(struct ps_prochandle *P, uintptr_t addr,
3115186f7fbfSEdward Pilatowicz char *buffer, size_t bufsize)
3116186f7fbfSEdward Pilatowicz {
3117186f7fbfSEdward Pilatowicz return (i_Pobjname(P, B_FALSE, addr, buffer, bufsize));
3118186f7fbfSEdward Pilatowicz }
3119186f7fbfSEdward Pilatowicz
3120186f7fbfSEdward Pilatowicz /*
3121186f7fbfSEdward Pilatowicz * Given a virtual address, try to return a filesystem path to the
3122186f7fbfSEdward Pilatowicz * underlying mapped object (file). If we're in the global zone,
3123186f7fbfSEdward Pilatowicz * this path could resolve to an object in another zone. If we're
3124186f7fbfSEdward Pilatowicz * unable return a valid filesystem path, we'll fall back to providing
3125186f7fbfSEdward Pilatowicz * the mapped object (file) name provided by the dynamic linker in
3126186f7fbfSEdward Pilatowicz * the target process (ie, the object reported by Pobjname()).
3127186f7fbfSEdward Pilatowicz */
3128186f7fbfSEdward Pilatowicz char *
Pobjname_resolved(struct ps_prochandle * P,uintptr_t addr,char * buffer,size_t bufsize)3129186f7fbfSEdward Pilatowicz Pobjname_resolved(struct ps_prochandle *P, uintptr_t addr,
3130186f7fbfSEdward Pilatowicz char *buffer, size_t bufsize)
3131186f7fbfSEdward Pilatowicz {
3132186f7fbfSEdward Pilatowicz return (i_Pobjname(P, B_TRUE, addr, buffer, bufsize));
3133186f7fbfSEdward Pilatowicz }
3134186f7fbfSEdward Pilatowicz
3135186f7fbfSEdward Pilatowicz /*
31367c478bd9Sstevel@tonic-gate * Given a virtual address, return the link map id of the underlying mapped
31377c478bd9Sstevel@tonic-gate * object (file), as provided by the dynamic linker. Return -1 on failure.
31387c478bd9Sstevel@tonic-gate */
31397c478bd9Sstevel@tonic-gate int
Plmid(struct ps_prochandle * P,uintptr_t addr,Lmid_t * lmidp)31407c478bd9Sstevel@tonic-gate Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
31417c478bd9Sstevel@tonic-gate {
31427c478bd9Sstevel@tonic-gate map_info_t *mptr;
31437c478bd9Sstevel@tonic-gate file_info_t *fptr;
31447c478bd9Sstevel@tonic-gate
31457c478bd9Sstevel@tonic-gate /* create all the file_info_t's for all the mappings */
31467c478bd9Sstevel@tonic-gate (void) Prd_agent(P);
31477c478bd9Sstevel@tonic-gate
31487c478bd9Sstevel@tonic-gate if ((mptr = Paddr2mptr(P, addr)) != NULL &&
31497c478bd9Sstevel@tonic-gate (fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
31507c478bd9Sstevel@tonic-gate *lmidp = fptr->file_lo->rl_lmident;
31517c478bd9Sstevel@tonic-gate return (0);
31527c478bd9Sstevel@tonic-gate }
31537c478bd9Sstevel@tonic-gate
31547c478bd9Sstevel@tonic-gate return (-1);
31557c478bd9Sstevel@tonic-gate }
31567c478bd9Sstevel@tonic-gate
31577c478bd9Sstevel@tonic-gate /*
31587c478bd9Sstevel@tonic-gate * Given an object name and optional lmid, iterate over the object's symbols.
31597c478bd9Sstevel@tonic-gate * If which == PR_SYMTAB, search the normal symbol table.
31607c478bd9Sstevel@tonic-gate * If which == PR_DYNSYM, search the dynamic symbol table.
31617c478bd9Sstevel@tonic-gate */
31627c478bd9Sstevel@tonic-gate static int
Psymbol_iter_com(struct ps_prochandle * P,Lmid_t lmid,const char * object_name,int which,int mask,pr_order_t order,proc_xsym_f * func,void * cd)31637c478bd9Sstevel@tonic-gate Psymbol_iter_com(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
31647c478bd9Sstevel@tonic-gate int which, int mask, pr_order_t order, proc_xsym_f *func, void *cd)
31657c478bd9Sstevel@tonic-gate {
316662b628a6SAli Bahrami #if STT_NUM != (STT_TLS + 1)
31677e16fca0SAli Bahrami #error "STT_NUM has grown. update Psymbol_iter_com()"
31687e16fca0SAli Bahrami #endif
31697e16fca0SAli Bahrami
31707c478bd9Sstevel@tonic-gate GElf_Sym sym;
3171f957ecc1Svb160487 GElf_Shdr shdr;
31727c478bd9Sstevel@tonic-gate map_info_t *mptr;
31737c478bd9Sstevel@tonic-gate file_info_t *fptr;
31747c478bd9Sstevel@tonic-gate sym_tbl_t *symtab;
31757c478bd9Sstevel@tonic-gate size_t symn;
31767c478bd9Sstevel@tonic-gate const char *strs;
31777c478bd9Sstevel@tonic-gate size_t strsz;
31787c478bd9Sstevel@tonic-gate prsyminfo_t si;
31797c478bd9Sstevel@tonic-gate int rv;
31807c478bd9Sstevel@tonic-gate uint_t *map, i, count, ndx;
31817c478bd9Sstevel@tonic-gate
31827c478bd9Sstevel@tonic-gate if ((mptr = object_name_to_map(P, lmid, object_name)) == NULL)
31837c478bd9Sstevel@tonic-gate return (-1);
31847c478bd9Sstevel@tonic-gate
31857c478bd9Sstevel@tonic-gate if ((fptr = build_map_symtab(P, mptr)) == NULL || /* no mapped file */
31867c478bd9Sstevel@tonic-gate fptr->file_elf == NULL) /* not an ELF file */
31877c478bd9Sstevel@tonic-gate return (-1);
31887c478bd9Sstevel@tonic-gate
31897c478bd9Sstevel@tonic-gate /*
31907c478bd9Sstevel@tonic-gate * Search the specified symbol table.
31917c478bd9Sstevel@tonic-gate */
31927c478bd9Sstevel@tonic-gate switch (which) {
31937c478bd9Sstevel@tonic-gate case PR_SYMTAB:
31947c478bd9Sstevel@tonic-gate symtab = &fptr->file_symtab;
31957c478bd9Sstevel@tonic-gate si.prs_table = PR_SYMTAB;
31967c478bd9Sstevel@tonic-gate break;
31977c478bd9Sstevel@tonic-gate case PR_DYNSYM:
31987c478bd9Sstevel@tonic-gate symtab = &fptr->file_dynsym;
31997c478bd9Sstevel@tonic-gate si.prs_table = PR_DYNSYM;
32007c478bd9Sstevel@tonic-gate break;
32017c478bd9Sstevel@tonic-gate default:
32027c478bd9Sstevel@tonic-gate return (-1);
32037c478bd9Sstevel@tonic-gate }
32047c478bd9Sstevel@tonic-gate
32057c478bd9Sstevel@tonic-gate si.prs_object = object_name;
32067c478bd9Sstevel@tonic-gate si.prs_lmid = fptr->file_lo == NULL ?
32077c478bd9Sstevel@tonic-gate LM_ID_BASE : fptr->file_lo->rl_lmident;
32087c478bd9Sstevel@tonic-gate
32097c478bd9Sstevel@tonic-gate symn = symtab->sym_symn;
32107c478bd9Sstevel@tonic-gate strs = symtab->sym_strs;
32117c478bd9Sstevel@tonic-gate strsz = symtab->sym_strsz;
32127c478bd9Sstevel@tonic-gate
32137c478bd9Sstevel@tonic-gate switch (order) {
32147c478bd9Sstevel@tonic-gate case PRO_NATURAL:
32157c478bd9Sstevel@tonic-gate map = NULL;
32167c478bd9Sstevel@tonic-gate count = symn;
32177c478bd9Sstevel@tonic-gate break;
32187c478bd9Sstevel@tonic-gate case PRO_BYNAME:
32197c478bd9Sstevel@tonic-gate map = symtab->sym_byname;
32207c478bd9Sstevel@tonic-gate count = symtab->sym_count;
32217c478bd9Sstevel@tonic-gate break;
32227c478bd9Sstevel@tonic-gate case PRO_BYADDR:
32237c478bd9Sstevel@tonic-gate map = symtab->sym_byaddr;
32247c478bd9Sstevel@tonic-gate count = symtab->sym_count;
32257c478bd9Sstevel@tonic-gate break;
32267c478bd9Sstevel@tonic-gate default:
32277c478bd9Sstevel@tonic-gate return (-1);
32287c478bd9Sstevel@tonic-gate }
32297c478bd9Sstevel@tonic-gate
3230d51e9074Sab196087 if (symtab->sym_data_pri == NULL || strs == NULL || count == 0)
3231d51e9074Sab196087 return (-1);
3232d51e9074Sab196087
32337c478bd9Sstevel@tonic-gate rv = 0;
32347c478bd9Sstevel@tonic-gate
32357c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) {
32367c478bd9Sstevel@tonic-gate ndx = map == NULL ? i : map[i];
3237d51e9074Sab196087 if (symtab_getsym(symtab, ndx, &sym) != NULL) {
32387c478bd9Sstevel@tonic-gate uint_t s_bind, s_type, type;
32397c478bd9Sstevel@tonic-gate
32407c478bd9Sstevel@tonic-gate if (sym.st_name >= strsz) /* invalid st_name */
32417c478bd9Sstevel@tonic-gate continue;
32427c478bd9Sstevel@tonic-gate
32437c478bd9Sstevel@tonic-gate s_bind = GELF_ST_BIND(sym.st_info);
32447c478bd9Sstevel@tonic-gate s_type = GELF_ST_TYPE(sym.st_info);
32457c478bd9Sstevel@tonic-gate
32467c478bd9Sstevel@tonic-gate /*
32477c478bd9Sstevel@tonic-gate * In case you haven't already guessed, this relies on
32487c478bd9Sstevel@tonic-gate * the bitmask used in <libproc.h> for encoding symbol
32497c478bd9Sstevel@tonic-gate * type and binding matching the order of STB and STT
32507e16fca0SAli Bahrami * constants in <sys/elf.h>. Changes to ELF must
32517e16fca0SAli Bahrami * maintain binary compatibility, so I think this is
32527c478bd9Sstevel@tonic-gate * reasonably fair game.
32537c478bd9Sstevel@tonic-gate */
325462b628a6SAli Bahrami if (s_bind < STB_NUM && s_type < STT_NUM) {
32557c478bd9Sstevel@tonic-gate type = (1 << (s_type + 8)) | (1 << s_bind);
32567c478bd9Sstevel@tonic-gate if ((type & ~mask) != 0)
32577c478bd9Sstevel@tonic-gate continue;
32587c478bd9Sstevel@tonic-gate } else
32597c478bd9Sstevel@tonic-gate continue; /* Invalid type or binding */
32607c478bd9Sstevel@tonic-gate
32617c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
32627c478bd9Sstevel@tonic-gate sym.st_value += fptr->file_dyn_base;
32637c478bd9Sstevel@tonic-gate
32647c478bd9Sstevel@tonic-gate si.prs_name = strs + sym.st_name;
3265f957ecc1Svb160487
3266f957ecc1Svb160487 /*
3267f957ecc1Svb160487 * If symbol's type is STT_SECTION, then try to lookup
3268f957ecc1Svb160487 * the name of the corresponding section.
3269f957ecc1Svb160487 */
3270f957ecc1Svb160487 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
3271f957ecc1Svb160487 fptr->file_shstrs != NULL &&
3272f957ecc1Svb160487 gelf_getshdr(elf_getscn(fptr->file_elf,
3273f957ecc1Svb160487 sym.st_shndx), &shdr) != NULL &&
3274f957ecc1Svb160487 shdr.sh_name != 0 &&
3275f957ecc1Svb160487 shdr.sh_name < fptr->file_shstrsz)
3276f957ecc1Svb160487 si.prs_name = fptr->file_shstrs + shdr.sh_name;
3277f957ecc1Svb160487
32787c478bd9Sstevel@tonic-gate si.prs_id = ndx;
3279f957ecc1Svb160487 if ((rv = func(cd, &sym, si.prs_name, &si)) != 0)
32807c478bd9Sstevel@tonic-gate break;
32817c478bd9Sstevel@tonic-gate }
32827c478bd9Sstevel@tonic-gate }
32837c478bd9Sstevel@tonic-gate
32847c478bd9Sstevel@tonic-gate return (rv);
32857c478bd9Sstevel@tonic-gate }
32867c478bd9Sstevel@tonic-gate
32877c478bd9Sstevel@tonic-gate int
Pxsymbol_iter(struct ps_prochandle * P,Lmid_t lmid,const char * object_name,int which,int mask,proc_xsym_f * func,void * cd)32887c478bd9Sstevel@tonic-gate Pxsymbol_iter(struct ps_prochandle *P, Lmid_t lmid, const char *object_name,
32897c478bd9Sstevel@tonic-gate int which, int mask, proc_xsym_f *func, void *cd)
32907c478bd9Sstevel@tonic-gate {
32917c478bd9Sstevel@tonic-gate return (Psymbol_iter_com(P, lmid, object_name, which, mask,
32927c478bd9Sstevel@tonic-gate PRO_NATURAL, func, cd));
32937c478bd9Sstevel@tonic-gate }
32947c478bd9Sstevel@tonic-gate
32957c478bd9Sstevel@tonic-gate int
Psymbol_iter_by_lmid(struct ps_prochandle * P,Lmid_t lmid,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)32967c478bd9Sstevel@tonic-gate Psymbol_iter_by_lmid(struct ps_prochandle *P, Lmid_t lmid,
32977c478bd9Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
32987c478bd9Sstevel@tonic-gate {
32997c478bd9Sstevel@tonic-gate return (Psymbol_iter_com(P, lmid, object_name, which, mask,
330078d83021SToomas Soome PRO_NATURAL, (proc_xsym_f *)(uintptr_t)func, cd));
33017c478bd9Sstevel@tonic-gate }
33027c478bd9Sstevel@tonic-gate
33037c478bd9Sstevel@tonic-gate int
Psymbol_iter(struct ps_prochandle * P,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)33047c478bd9Sstevel@tonic-gate Psymbol_iter(struct ps_prochandle *P,
33057c478bd9Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
33067c478bd9Sstevel@tonic-gate {
33077c478bd9Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
330878d83021SToomas Soome PRO_NATURAL, (proc_xsym_f *)(uintptr_t)func, cd));
33097c478bd9Sstevel@tonic-gate }
33107c478bd9Sstevel@tonic-gate
33117c478bd9Sstevel@tonic-gate int
Psymbol_iter_by_addr(struct ps_prochandle * P,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)33127c478bd9Sstevel@tonic-gate Psymbol_iter_by_addr(struct ps_prochandle *P,
33137c478bd9Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
33147c478bd9Sstevel@tonic-gate {
33157c478bd9Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
331678d83021SToomas Soome PRO_BYADDR, (proc_xsym_f *)(uintptr_t)func, cd));
33177c478bd9Sstevel@tonic-gate }
33187c478bd9Sstevel@tonic-gate
33197c478bd9Sstevel@tonic-gate int
Psymbol_iter_by_name(struct ps_prochandle * P,const char * object_name,int which,int mask,proc_sym_f * func,void * cd)33207c478bd9Sstevel@tonic-gate Psymbol_iter_by_name(struct ps_prochandle *P,
33217c478bd9Sstevel@tonic-gate const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
33227c478bd9Sstevel@tonic-gate {
33237c478bd9Sstevel@tonic-gate return (Psymbol_iter_com(P, PR_LMID_EVERY, object_name, which, mask,
332478d83021SToomas Soome PRO_BYNAME, (proc_xsym_f *)(uintptr_t)func, cd));
33257c478bd9Sstevel@tonic-gate }
33267c478bd9Sstevel@tonic-gate
33277c478bd9Sstevel@tonic-gate /*
33282a12f85aSJeremy Jones * Get the platform string.
33297c478bd9Sstevel@tonic-gate */
33307c478bd9Sstevel@tonic-gate char *
Pplatform(struct ps_prochandle * P,char * s,size_t n)33317c478bd9Sstevel@tonic-gate Pplatform(struct ps_prochandle *P, char *s, size_t n)
33327c478bd9Sstevel@tonic-gate {
33332a12f85aSJeremy Jones return (P->ops.pop_platform(P, s, n, P->data));
33347c478bd9Sstevel@tonic-gate }
33357c478bd9Sstevel@tonic-gate
33367c478bd9Sstevel@tonic-gate /*
33372a12f85aSJeremy Jones * Get the uname(2) information.
33387c478bd9Sstevel@tonic-gate */
33397c478bd9Sstevel@tonic-gate int
Puname(struct ps_prochandle * P,struct utsname * u)33407c478bd9Sstevel@tonic-gate Puname(struct ps_prochandle *P, struct utsname *u)
33417c478bd9Sstevel@tonic-gate {
33422a12f85aSJeremy Jones return (P->ops.pop_uname(P, u, P->data));
33437c478bd9Sstevel@tonic-gate }
33447c478bd9Sstevel@tonic-gate
33457c478bd9Sstevel@tonic-gate /*
33467c478bd9Sstevel@tonic-gate * Called from Pcreate(), Pgrab(), and Pfgrab_core() to initialize
33477c478bd9Sstevel@tonic-gate * the symbol table heads in the new ps_prochandle.
33487c478bd9Sstevel@tonic-gate */
33497c478bd9Sstevel@tonic-gate void
Pinitsym(struct ps_prochandle * P)33507c478bd9Sstevel@tonic-gate Pinitsym(struct ps_prochandle *P)
33517c478bd9Sstevel@tonic-gate {
33527c478bd9Sstevel@tonic-gate P->num_files = 0;
335350d4d24eSRobert Mustacchi list_create(&P->file_head, sizeof (file_info_t),
335450d4d24eSRobert Mustacchi offsetof(file_info_t, file_list));
33557c478bd9Sstevel@tonic-gate }
33567c478bd9Sstevel@tonic-gate
33577c478bd9Sstevel@tonic-gate /*
33587c478bd9Sstevel@tonic-gate * Called from Prelease() to destroy the symbol tables.
33597c478bd9Sstevel@tonic-gate * Must be called by the client after an exec() in the victim process.
33607c478bd9Sstevel@tonic-gate */
33617c478bd9Sstevel@tonic-gate void
Preset_maps(struct ps_prochandle * P)33627c478bd9Sstevel@tonic-gate Preset_maps(struct ps_prochandle *P)
33637c478bd9Sstevel@tonic-gate {
33647c478bd9Sstevel@tonic-gate int i;
33657c478bd9Sstevel@tonic-gate
33667c478bd9Sstevel@tonic-gate if (P->rap != NULL) {
33677c478bd9Sstevel@tonic-gate rd_delete(P->rap);
33687c478bd9Sstevel@tonic-gate P->rap = NULL;
33697c478bd9Sstevel@tonic-gate }
33707c478bd9Sstevel@tonic-gate
33717c478bd9Sstevel@tonic-gate if (P->execname != NULL) {
33727c478bd9Sstevel@tonic-gate free(P->execname);
33737c478bd9Sstevel@tonic-gate P->execname = NULL;
33747c478bd9Sstevel@tonic-gate }
33757c478bd9Sstevel@tonic-gate
33767c478bd9Sstevel@tonic-gate if (P->auxv != NULL) {
33777c478bd9Sstevel@tonic-gate free(P->auxv);
33787c478bd9Sstevel@tonic-gate P->auxv = NULL;
33797c478bd9Sstevel@tonic-gate P->nauxv = 0;
33807c478bd9Sstevel@tonic-gate }
33817c478bd9Sstevel@tonic-gate
33827c478bd9Sstevel@tonic-gate for (i = 0; i < P->map_count; i++)
33837c478bd9Sstevel@tonic-gate map_info_free(P, &P->mappings[i]);
33847c478bd9Sstevel@tonic-gate
33857c478bd9Sstevel@tonic-gate if (P->mappings != NULL) {
33867c478bd9Sstevel@tonic-gate free(P->mappings);
33877c478bd9Sstevel@tonic-gate P->mappings = NULL;
33887c478bd9Sstevel@tonic-gate }
33897c478bd9Sstevel@tonic-gate P->map_count = P->map_alloc = 0;
33907c478bd9Sstevel@tonic-gate
33917c478bd9Sstevel@tonic-gate P->info_valid = 0;
33927c478bd9Sstevel@tonic-gate }
33937c478bd9Sstevel@tonic-gate
33947c478bd9Sstevel@tonic-gate typedef struct getenv_data {
33957c478bd9Sstevel@tonic-gate char *buf;
33967c478bd9Sstevel@tonic-gate size_t bufsize;
33977c478bd9Sstevel@tonic-gate const char *search;
33987c478bd9Sstevel@tonic-gate size_t searchlen;
33997c478bd9Sstevel@tonic-gate } getenv_data_t;
34007c478bd9Sstevel@tonic-gate
34017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34027c478bd9Sstevel@tonic-gate static int
getenv_func(void * data,struct ps_prochandle * P,uintptr_t addr,const char * nameval)34037c478bd9Sstevel@tonic-gate getenv_func(void *data, struct ps_prochandle *P, uintptr_t addr,
34047c478bd9Sstevel@tonic-gate const char *nameval)
34057c478bd9Sstevel@tonic-gate {
34067c478bd9Sstevel@tonic-gate getenv_data_t *d = data;
34077c478bd9Sstevel@tonic-gate size_t len;
34087c478bd9Sstevel@tonic-gate
34097c478bd9Sstevel@tonic-gate if (nameval == NULL)
34107c478bd9Sstevel@tonic-gate return (0);
34117c478bd9Sstevel@tonic-gate
34127c478bd9Sstevel@tonic-gate if (d->searchlen < strlen(nameval) &&
34137c478bd9Sstevel@tonic-gate strncmp(nameval, d->search, d->searchlen) == 0 &&
34147c478bd9Sstevel@tonic-gate nameval[d->searchlen] == '=') {
34157c478bd9Sstevel@tonic-gate len = MIN(strlen(nameval), d->bufsize - 1);
34167c478bd9Sstevel@tonic-gate (void) strncpy(d->buf, nameval, len);
34177c478bd9Sstevel@tonic-gate d->buf[len] = '\0';
34187c478bd9Sstevel@tonic-gate return (1);
34197c478bd9Sstevel@tonic-gate }
34207c478bd9Sstevel@tonic-gate
34217c478bd9Sstevel@tonic-gate return (0);
34227c478bd9Sstevel@tonic-gate }
34237c478bd9Sstevel@tonic-gate
34247c478bd9Sstevel@tonic-gate char *
Pgetenv(struct ps_prochandle * P,const char * name,char * buf,size_t buflen)34257c478bd9Sstevel@tonic-gate Pgetenv(struct ps_prochandle *P, const char *name, char *buf, size_t buflen)
34267c478bd9Sstevel@tonic-gate {
34277c478bd9Sstevel@tonic-gate getenv_data_t d;
34287c478bd9Sstevel@tonic-gate
34297c478bd9Sstevel@tonic-gate d.buf = buf;
34307c478bd9Sstevel@tonic-gate d.bufsize = buflen;
34317c478bd9Sstevel@tonic-gate d.search = name;
34327c478bd9Sstevel@tonic-gate d.searchlen = strlen(name);
34337c478bd9Sstevel@tonic-gate
34347c478bd9Sstevel@tonic-gate if (Penv_iter(P, getenv_func, &d) == 1) {
34357c478bd9Sstevel@tonic-gate char *equals = strchr(d.buf, '=');
34367c478bd9Sstevel@tonic-gate
34377c478bd9Sstevel@tonic-gate if (equals != NULL) {
34387c478bd9Sstevel@tonic-gate (void) memmove(d.buf, equals + 1,
34397c478bd9Sstevel@tonic-gate d.buf + buflen - equals - 1);
34407c478bd9Sstevel@tonic-gate d.buf[d.buf + buflen - equals] = '\0';
34417c478bd9Sstevel@tonic-gate
34427c478bd9Sstevel@tonic-gate return (buf);
34437c478bd9Sstevel@tonic-gate }
34447c478bd9Sstevel@tonic-gate }
34457c478bd9Sstevel@tonic-gate
34467c478bd9Sstevel@tonic-gate return (NULL);
34477c478bd9Sstevel@tonic-gate }
34487c478bd9Sstevel@tonic-gate
34497c478bd9Sstevel@tonic-gate /* number of argument or environment pointers to read all at once */
34507c478bd9Sstevel@tonic-gate #define NARG 100
34517c478bd9Sstevel@tonic-gate
34527c478bd9Sstevel@tonic-gate int
Penv_iter(struct ps_prochandle * P,proc_env_f * func,void * data)34537c478bd9Sstevel@tonic-gate Penv_iter(struct ps_prochandle *P, proc_env_f *func, void *data)
34547c478bd9Sstevel@tonic-gate {
34557c478bd9Sstevel@tonic-gate const psinfo_t *psp;
34567c478bd9Sstevel@tonic-gate uintptr_t envpoff;
34577c478bd9Sstevel@tonic-gate GElf_Sym sym;
34587c478bd9Sstevel@tonic-gate int ret;
34597c478bd9Sstevel@tonic-gate char *buf, *nameval;
34607c478bd9Sstevel@tonic-gate size_t buflen;
34617c478bd9Sstevel@tonic-gate
34627c478bd9Sstevel@tonic-gate int nenv = NARG;
34637c478bd9Sstevel@tonic-gate long envp[NARG];
34647c478bd9Sstevel@tonic-gate
34657c478bd9Sstevel@tonic-gate /*
34667c478bd9Sstevel@tonic-gate * Attempt to find the "_environ" variable in the process.
34677c478bd9Sstevel@tonic-gate * Failing that, use the original value provided by Ppsinfo().
3468*e8c318c6SRobert Mustacchi *
3469*e8c318c6SRobert Mustacchi * The "_environ" variable is initialized by the CRT. We use a rough
3470*e8c318c6SRobert Mustacchi * heuristic to try and figure out if we have started running before the
3471*e8c318c6SRobert Mustacchi * CRT has executed by checking if the _environ pointer points to NULL
3472*e8c318c6SRobert Mustacchi * or not. Once initialized, it will never point to NULL absent an
3473*e8c318c6SRobert Mustacchi * application manipulating it directly, libc does not do so, even if
3474*e8c318c6SRobert Mustacchi * one calls clearenv(). There is a rare chance that an application is
3475*e8c318c6SRobert Mustacchi * messing with the _environ pointer directly; however, in practice that
3476*e8c318c6SRobert Mustacchi * is much rarer than this case and if someone is, libc is unlikely to
3477*e8c318c6SRobert Mustacchi * have a good day.
3478*e8c318c6SRobert Mustacchi *
3479*e8c318c6SRobert Mustacchi * While it's tempting to look towards libc variables such as
3480*e8c318c6SRobert Mustacchi * initenv_done and related, we have to remember that we're here because
3481*e8c318c6SRobert Mustacchi * we haven't actually called libc_init() or even loaded it!
34827c478bd9Sstevel@tonic-gate */
34837c478bd9Sstevel@tonic-gate if ((psp = Ppsinfo(P)) == NULL)
34847c478bd9Sstevel@tonic-gate return (-1);
34857c478bd9Sstevel@tonic-gate
34867c478bd9Sstevel@tonic-gate envpoff = psp->pr_envp; /* Default if no _environ found */
34877c478bd9Sstevel@tonic-gate
34887c478bd9Sstevel@tonic-gate if (Plookup_by_name(P, PR_OBJ_EXEC, "_environ", &sym) == 0) {
34897c478bd9Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
34907c478bd9Sstevel@tonic-gate if (Pread(P, &envpoff, sizeof (envpoff),
34917c478bd9Sstevel@tonic-gate sym.st_value) != sizeof (envpoff))
34927c478bd9Sstevel@tonic-gate envpoff = psp->pr_envp;
34937c478bd9Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
34947c478bd9Sstevel@tonic-gate uint32_t envpoff32;
34957c478bd9Sstevel@tonic-gate
34967c478bd9Sstevel@tonic-gate if (Pread(P, &envpoff32, sizeof (envpoff32),
34977c478bd9Sstevel@tonic-gate sym.st_value) != sizeof (envpoff32))
34987c478bd9Sstevel@tonic-gate envpoff = psp->pr_envp;
34997c478bd9Sstevel@tonic-gate else
35007c478bd9Sstevel@tonic-gate envpoff = envpoff32;
35017c478bd9Sstevel@tonic-gate }
3502*e8c318c6SRobert Mustacchi
3503*e8c318c6SRobert Mustacchi if (envpoff == 0) {
3504*e8c318c6SRobert Mustacchi envpoff = psp->pr_envp;
3505*e8c318c6SRobert Mustacchi }
35067c478bd9Sstevel@tonic-gate }
35077c478bd9Sstevel@tonic-gate
35087c478bd9Sstevel@tonic-gate buflen = 128;
35097c478bd9Sstevel@tonic-gate buf = malloc(buflen);
35107c478bd9Sstevel@tonic-gate
35117c478bd9Sstevel@tonic-gate ret = 0;
35127c478bd9Sstevel@tonic-gate for (;;) {
35137c478bd9Sstevel@tonic-gate uintptr_t envoff;
35147c478bd9Sstevel@tonic-gate
35157c478bd9Sstevel@tonic-gate if (nenv == NARG) {
35167c478bd9Sstevel@tonic-gate (void) memset(envp, 0, sizeof (envp));
35177c478bd9Sstevel@tonic-gate if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
35187c478bd9Sstevel@tonic-gate if (Pread(P, envp,
35197c478bd9Sstevel@tonic-gate sizeof (envp), envpoff) <= 0) {
35207c478bd9Sstevel@tonic-gate ret = -1;
35217c478bd9Sstevel@tonic-gate break;
35227c478bd9Sstevel@tonic-gate }
35237c478bd9Sstevel@tonic-gate } else if (P->status.pr_dmodel == PR_MODEL_ILP32) {
35247c478bd9Sstevel@tonic-gate uint32_t e32[NARG];
35257c478bd9Sstevel@tonic-gate int i;
35267c478bd9Sstevel@tonic-gate
35277c478bd9Sstevel@tonic-gate (void) memset(e32, 0, sizeof (e32));
35287c478bd9Sstevel@tonic-gate if (Pread(P, e32, sizeof (e32), envpoff) <= 0) {
35297c478bd9Sstevel@tonic-gate ret = -1;
35307c478bd9Sstevel@tonic-gate break;
35317c478bd9Sstevel@tonic-gate }
35327c478bd9Sstevel@tonic-gate for (i = 0; i < NARG; i++)
35337c478bd9Sstevel@tonic-gate envp[i] = e32[i];
35347c478bd9Sstevel@tonic-gate }
35357c478bd9Sstevel@tonic-gate nenv = 0;
35367c478bd9Sstevel@tonic-gate }
35377c478bd9Sstevel@tonic-gate
3538c75682cdSToomas Soome if ((envoff = envp[nenv++]) == (uintptr_t)NULL)
35397c478bd9Sstevel@tonic-gate break;
35407c478bd9Sstevel@tonic-gate
35417c478bd9Sstevel@tonic-gate /*
35427c478bd9Sstevel@tonic-gate * Attempt to read the string from the process.
35437c478bd9Sstevel@tonic-gate */
35447c478bd9Sstevel@tonic-gate again:
35457c478bd9Sstevel@tonic-gate ret = Pread_string(P, buf, buflen, envoff);
35467c478bd9Sstevel@tonic-gate
35477c478bd9Sstevel@tonic-gate if (ret <= 0) {
35487c478bd9Sstevel@tonic-gate nameval = NULL;
35497c478bd9Sstevel@tonic-gate } else if (ret == buflen - 1) {
35507c478bd9Sstevel@tonic-gate free(buf);
35517c478bd9Sstevel@tonic-gate /*
35527c478bd9Sstevel@tonic-gate * Bail if we have a corrupted environment
35537c478bd9Sstevel@tonic-gate */
35547c478bd9Sstevel@tonic-gate if (buflen >= ARG_MAX)
35557c478bd9Sstevel@tonic-gate return (-1);
35567c478bd9Sstevel@tonic-gate buflen *= 2;
35577c478bd9Sstevel@tonic-gate buf = malloc(buflen);
35587c478bd9Sstevel@tonic-gate goto again;
35597c478bd9Sstevel@tonic-gate } else {
35607c478bd9Sstevel@tonic-gate nameval = buf;
35617c478bd9Sstevel@tonic-gate }
35627c478bd9Sstevel@tonic-gate
35637c478bd9Sstevel@tonic-gate if ((ret = func(data, P, envoff, nameval)) != 0)
35647c478bd9Sstevel@tonic-gate break;
35657c478bd9Sstevel@tonic-gate
35667c478bd9Sstevel@tonic-gate envpoff += (P->status.pr_dmodel == PR_MODEL_LP64)? 8 : 4;
35677c478bd9Sstevel@tonic-gate }
35687c478bd9Sstevel@tonic-gate
35697c478bd9Sstevel@tonic-gate free(buf);
35707c478bd9Sstevel@tonic-gate
35717c478bd9Sstevel@tonic-gate return (ret);
35727c478bd9Sstevel@tonic-gate }
3573