1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1994, by Sun Microsytems, Inc.
24*0Sstevel@tonic-gate */
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate * Generic functions that know how to traverse elf sections in an object.
30*0Sstevel@tonic-gate * Also functions that know how to traverse records in a section.
31*0Sstevel@tonic-gate *
32*0Sstevel@tonic-gate */
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <sys/procfs.h>
39*0Sstevel@tonic-gate #include <sys/stat.h>
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #include "tnfctl_int.h"
42*0Sstevel@tonic-gate #include "dbg.h"
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate * _tnfctl_traverse_object() - traverses all of the elf sections in an object,
47*0Sstevel@tonic-gate * calling the supplied function on each.
48*0Sstevel@tonic-gate */
49*0Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_traverse_object(int objfd,uintptr_t addr,tnfctl_elf_search_t * search_info_p)50*0Sstevel@tonic-gate _tnfctl_traverse_object(int objfd, uintptr_t addr,
51*0Sstevel@tonic-gate tnfctl_elf_search_t *search_info_p)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate Elf *elf;
54*0Sstevel@tonic-gate GElf_Ehdr *ehdr, ehdr_obj;
55*0Sstevel@tonic-gate char *strs;
56*0Sstevel@tonic-gate GElf_Shdr *shdr, shdr_obj;
57*0Sstevel@tonic-gate Elf_Data *data;
58*0Sstevel@tonic-gate u_int idx;
59*0Sstevel@tonic-gate tnfctl_errcode_t prexstat = TNFCTL_ERR_NONE;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate DBG_TNF_PROBE_1(_tnfctl_traverse_object_1, "libtnfctl",
62*0Sstevel@tonic-gate "sunw%verbosity 3",
63*0Sstevel@tonic-gate tnf_opaque, obj_addr, addr);
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE)
66*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate /* open elf descriptor on the fd */
69*0Sstevel@tonic-gate elf = elf_begin(objfd, ELF_C_READ, NULL);
70*0Sstevel@tonic-gate if (elf == NULL || elf_kind(elf) != ELF_K_ELF) {
71*0Sstevel@tonic-gate DBG_TNF_PROBE_0(_tnfctl_traverse_object_2, "libtnfctl",
72*0Sstevel@tonic-gate "sunw%verbosity 3; sunw%debug 'not elf object'");
73*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate /* get the elf header */
76*0Sstevel@tonic-gate if ((ehdr = gelf_getehdr(elf, &ehdr_obj)) == NULL) {
77*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
78*0Sstevel@tonic-gate "_tnfctl_traverse_object: gelf_getehdr failed\n"));
79*0Sstevel@tonic-gate (void) elf_end(elf);
80*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate if ((ehdr->e_type != ET_EXEC) && (ehdr->e_type != ET_DYN)) {
83*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
84*0Sstevel@tonic-gate "_tnfctl_traverse_object: not an "
85*0Sstevel@tonic-gate "executable or a shared object\n"));
86*0Sstevel@tonic-gate (void) elf_end(elf);
87*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate /* if an executable file, the base address is 0 */
90*0Sstevel@tonic-gate if (ehdr->e_type == ET_EXEC)
91*0Sstevel@tonic-gate addr = 0;
92*0Sstevel@tonic-gate /* get a pointer to the elf header string table */
93*0Sstevel@tonic-gate strs = elf_strptr(elf, ehdr->e_shstrndx, NULL);
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate DBG_TNF_PROBE_1(_tnfctl_traverse_object_3, "libtnfctl",
96*0Sstevel@tonic-gate "sunw%verbosity 3",
97*0Sstevel@tonic-gate tnf_long, num_sections_found, ehdr->e_shnum);
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate for (idx = 1; idx < ehdr->e_shnum; idx++) {
100*0Sstevel@tonic-gate Elf_Scn *scn;
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate if ((scn = elf_getscn(elf, idx)) == NULL) {
103*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
104*0Sstevel@tonic-gate "_tnfctl_traverse_object: elf_getscn failed\n"));
105*0Sstevel@tonic-gate prexstat = TNFCTL_ERR_INTERNAL;
106*0Sstevel@tonic-gate break;
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate if ((shdr = gelf_getshdr(scn, &shdr_obj)) == NULL) {
109*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
110*0Sstevel@tonic-gate "_tnfctl_traverse_obj:gelf_getshdr failed\n"));
111*0Sstevel@tonic-gate prexstat = TNFCTL_ERR_INTERNAL;
112*0Sstevel@tonic-gate break;
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate if ((data = elf_getdata(scn, NULL)) == NULL) {
116*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
117*0Sstevel@tonic-gate "_tnfctl_traverse_obj:gelf_getdata failed\n"));
118*0Sstevel@tonic-gate prexstat = TNFCTL_ERR_INTERNAL;
119*0Sstevel@tonic-gate break;
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate /* call the supplied function */
122*0Sstevel@tonic-gate prexstat = search_info_p->section_func(elf,
123*0Sstevel@tonic-gate strs, scn, shdr, data, addr, search_info_p);
124*0Sstevel@tonic-gate if (prexstat)
125*0Sstevel@tonic-gate break;
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate (void) elf_end(elf);
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate return (prexstat);
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate } /* end _tnfctl_traverse_object */
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate * _tnfctl_traverse_rela() - this function traverses a .rela section calling the
137*0Sstevel@tonic-gate * supplied function on each relocation record.
138*0Sstevel@tonic-gate */
139*0Sstevel@tonic-gate /*ARGSUSED*/
140*0Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_traverse_rela(Elf * elf,char * strs,Elf_Scn * rel_scn,GElf_Shdr * rel_shdr,Elf_Data * rel_data,uintptr_t baseaddr,tnfctl_elf_search_t * search_info_p)141*0Sstevel@tonic-gate _tnfctl_traverse_rela(Elf * elf, char *strs, Elf_Scn * rel_scn,
142*0Sstevel@tonic-gate GElf_Shdr * rel_shdr, Elf_Data * rel_data, uintptr_t baseaddr,
143*0Sstevel@tonic-gate tnfctl_elf_search_t * search_info_p)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate Elf_Scn *sym_scn;
146*0Sstevel@tonic-gate GElf_Shdr *sym_shdr, sym_shdr_obj;
147*0Sstevel@tonic-gate Elf_Data *sym_data;
148*0Sstevel@tonic-gate Elf3264_Sym *sym_table;
149*0Sstevel@tonic-gate Elf_Scn *str_scn;
150*0Sstevel@tonic-gate GElf_Shdr *str_shdr, str_shdr_obj;
151*0Sstevel@tonic-gate Elf_Data *str_data;
152*0Sstevel@tonic-gate char *str_table;
153*0Sstevel@tonic-gate ulong_t nrels;
154*0Sstevel@tonic-gate uint_t i;
155*0Sstevel@tonic-gate boolean_t isrela;
156*0Sstevel@tonic-gate size_t rela_sz;
157*0Sstevel@tonic-gate char *ptr;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate DBG_TNF_PROBE_0(_tnfctl_traverse_rela_1, "libtnfctl",
160*0Sstevel@tonic-gate "sunw%verbosity 4");
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate /* bail if this isn't a rela (or rel) section */
163*0Sstevel@tonic-gate if (rel_shdr->sh_type == SHT_RELA) {
164*0Sstevel@tonic-gate isrela = B_TRUE;
165*0Sstevel@tonic-gate } else if (rel_shdr->sh_type == SHT_REL) {
166*0Sstevel@tonic-gate isrela = B_FALSE;
167*0Sstevel@tonic-gate } else
168*0Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /* find the symbol table section associated with this rela section */
171*0Sstevel@tonic-gate sym_scn = elf_getscn(elf, rel_shdr->sh_link);
172*0Sstevel@tonic-gate if (sym_scn == NULL) {
173*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
174*0Sstevel@tonic-gate "_tnfctl_traverse_rela:elf_getscn (sym) failed\n"));
175*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate sym_shdr = gelf_getshdr(sym_scn, &sym_shdr_obj);
178*0Sstevel@tonic-gate if (sym_shdr == NULL) {
179*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
180*0Sstevel@tonic-gate "_tnfctl_traverse_rela:gelf_getshdr (sym) failed\n"));
181*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate sym_data = elf_getdata(sym_scn, NULL);
184*0Sstevel@tonic-gate if (sym_data == NULL) {
185*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
186*0Sstevel@tonic-gate "_tnfctl_traverse_rela:elf_getdata (sym) failed\n"));
187*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate sym_table = (Elf3264_Sym *) sym_data->d_buf;
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /* find the string table associated with the symbol table */
192*0Sstevel@tonic-gate str_scn = elf_getscn(elf, sym_shdr->sh_link);
193*0Sstevel@tonic-gate if (str_scn == NULL) {
194*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
195*0Sstevel@tonic-gate "_tnfctl_traverse_rela:elf_getscn (str) failed\n"));
196*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate str_shdr = gelf_getshdr(str_scn, &str_shdr_obj);
199*0Sstevel@tonic-gate if (str_shdr == NULL) {
200*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
201*0Sstevel@tonic-gate "_tnfctl_traverse_rela:gelf_getshdr (str) failed\n"));
202*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate str_data = elf_getdata(str_scn, NULL);
205*0Sstevel@tonic-gate if (str_data == NULL) {
206*0Sstevel@tonic-gate DBG((void) fprintf(stderr,
207*0Sstevel@tonic-gate "_tnfctl_traverse_rela: elf_getdata (str) failed\n"));
208*0Sstevel@tonic-gate return (TNFCTL_ERR_INTERNAL);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate str_table = (char *) str_data->d_buf;
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate /* loop over each relocation record */
213*0Sstevel@tonic-gate nrels = rel_shdr->sh_size / rel_shdr->sh_entsize;
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gate DBG_TNF_PROBE_1(_tnfctl_traverse_rela_2, "libtnfctl",
216*0Sstevel@tonic-gate "sunw%verbosity 3",
217*0Sstevel@tonic-gate tnf_long, relocations_found, nrels);
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate ptr = rel_data->d_buf;
220*0Sstevel@tonic-gate rela_sz = (isrela) ? sizeof (Elf3264_Rela) : sizeof (Elf3264_Rel);
221*0Sstevel@tonic-gate for (i = 0; i < nrels; i++, ptr += rela_sz) {
222*0Sstevel@tonic-gate Elf3264_Word syminfo;
223*0Sstevel@tonic-gate Elf3264_Sym *sym;
224*0Sstevel@tonic-gate Elf3264_Addr offset;
225*0Sstevel@tonic-gate char *name;
226*0Sstevel@tonic-gate uintptr_t addr;
227*0Sstevel@tonic-gate tnfctl_errcode_t prexstat;
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate /* decode the r_info field of the relocation record */
230*0Sstevel@tonic-gate if (isrela) {
231*0Sstevel@tonic-gate Elf3264_Rela *rela_p;
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate /*LINTED pointer cast may result in improper alignment*/
234*0Sstevel@tonic-gate rela_p = (Elf3264_Rela *) ptr;
235*0Sstevel@tonic-gate syminfo = ELF3264_R_SYM(rela_p->r_info);
236*0Sstevel@tonic-gate offset = rela_p->r_offset;
237*0Sstevel@tonic-gate } else {
238*0Sstevel@tonic-gate Elf3264_Rel *rel_p;
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate /*LINTED pointer cast may result in improper alignment*/
241*0Sstevel@tonic-gate rel_p = (Elf3264_Rel *) ptr;
242*0Sstevel@tonic-gate syminfo = ELF3264_R_SYM(rel_p->r_info);
243*0Sstevel@tonic-gate offset = rel_p->r_offset;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate /* find the associated symbol table entry */
247*0Sstevel@tonic-gate if (!syminfo)
248*0Sstevel@tonic-gate continue;
249*0Sstevel@tonic-gate sym = sym_table + syminfo;
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate /* find the associated string table entry */
252*0Sstevel@tonic-gate if (!sym->st_name)
253*0Sstevel@tonic-gate continue;
254*0Sstevel@tonic-gate name = str_table + sym->st_name;
255*0Sstevel@tonic-gate addr = offset + baseaddr;
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate prexstat = search_info_p->record_func(name, addr, ptr,
258*0Sstevel@tonic-gate search_info_p);
259*0Sstevel@tonic-gate if (prexstat)
260*0Sstevel@tonic-gate break;
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate } /* end _tnfctl_traverse_rela */
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate /*
269*0Sstevel@tonic-gate * _tnfctl_traverse_dynsym() - this function traverses a dynsym section calling
270*0Sstevel@tonic-gate * the supplied function on each symbol.
271*0Sstevel@tonic-gate */
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate /*ARGSUSED*/
274*0Sstevel@tonic-gate tnfctl_errcode_t
_tnfctl_traverse_dynsym(Elf * elf,char * elfstrs,Elf_Scn * scn,GElf_Shdr * shdr,Elf_Data * data,uintptr_t baseaddr,tnfctl_elf_search_t * search_info_p)275*0Sstevel@tonic-gate _tnfctl_traverse_dynsym(Elf * elf,
276*0Sstevel@tonic-gate char *elfstrs,
277*0Sstevel@tonic-gate Elf_Scn * scn,
278*0Sstevel@tonic-gate GElf_Shdr * shdr,
279*0Sstevel@tonic-gate Elf_Data * data,
280*0Sstevel@tonic-gate uintptr_t baseaddr,
281*0Sstevel@tonic-gate tnfctl_elf_search_t * search_info_p)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate ulong_t nsyms;
284*0Sstevel@tonic-gate int i;
285*0Sstevel@tonic-gate char *strs;
286*0Sstevel@tonic-gate tnfctl_errcode_t prexstat;
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate Elf3264_Sym *syms;
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate /* bail if this isn't a dynsym section */
291*0Sstevel@tonic-gate if (shdr->sh_type != SHT_DYNSYM)
292*0Sstevel@tonic-gate return (TNFCTL_ERR_NONE);
293*0Sstevel@tonic-gate #if 0
294*0Sstevel@tonic-gate printf("### entering _tnfctl_traverse_dynsym...\n");
295*0Sstevel@tonic-gate #endif
296*0Sstevel@tonic-gate syms = data->d_buf;
297*0Sstevel@tonic-gate nsyms = shdr->sh_size / shdr->sh_entsize;
298*0Sstevel@tonic-gate strs = elf_strptr(elf, shdr->sh_link, 0);
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate DBG_TNF_PROBE_1(_tnfctl_traverse_dynsym_1, "libtnfctl",
301*0Sstevel@tonic-gate "sunw%verbosity 3",
302*0Sstevel@tonic-gate tnf_long, symbols_found, nsyms);
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gate for (i = 0; i < nsyms; i++) {
305*0Sstevel@tonic-gate Elf3264_Sym *sym = &syms[i];
306*0Sstevel@tonic-gate char *name;
307*0Sstevel@tonic-gate uintptr_t addr;
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate name = strs + sym->st_name;
310*0Sstevel@tonic-gate addr = baseaddr + sym->st_value;
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate #if 0
313*0Sstevel@tonic-gate if (name != 0)
314*0Sstevel@tonic-gate printf("_tnfctl_traverse_dynsym: name = %s\n", name);
315*0Sstevel@tonic-gate else
316*0Sstevel@tonic-gate printf("_tnfctl_traverse_dynsym: name is 0\n");
317*0Sstevel@tonic-gate #endif
318*0Sstevel@tonic-gate prexstat = search_info_p->record_func(name,
319*0Sstevel@tonic-gate addr, sym, search_info_p);
320*0Sstevel@tonic-gate if (prexstat)
321*0Sstevel@tonic-gate break;
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate #if 0
324*0Sstevel@tonic-gate printf("### leaving _tnfctl_traverse_dynsym...\n");
325*0Sstevel@tonic-gate #endif
326*0Sstevel@tonic-gate return (prexstat);
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gate } /* end _tnfctl_traverse_dynsym */
329