summaryrefslogtreecommitdiff
path: root/usr/src/lib/libtnfctl/traverse.c
blob: b2c38180b8ad08e69506b14df861f2cf04d94009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1994, by Sun Microsytems, Inc.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Generic functions that know how to traverse elf sections in an object.
 * Also functions that know how to traverse records in a section.
 *
 */

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/procfs.h>
#include <sys/stat.h>

#include "tnfctl_int.h"
#include "dbg.h"


/*
 * _tnfctl_traverse_object() - traverses all of the elf sections in an object,
 * calling the supplied function on each.
 */
tnfctl_errcode_t
_tnfctl_traverse_object(int objfd, uintptr_t addr,
			tnfctl_elf_search_t *search_info_p)
{
	Elf		*elf;
	GElf_Ehdr	*ehdr, ehdr_obj;
	char		*strs;
	GElf_Shdr	*shdr, shdr_obj;
	Elf_Data	*data;
	u_int		idx;
	tnfctl_errcode_t	prexstat = TNFCTL_ERR_NONE;

	DBG_TNF_PROBE_1(_tnfctl_traverse_object_1, "libtnfctl",
			"sunw%verbosity 3",
			tnf_opaque, obj_addr, addr);

	if (elf_version(EV_CURRENT) == EV_NONE)
		return (TNFCTL_ERR_INTERNAL);

	/* open elf descriptor on the fd */
	elf = elf_begin(objfd, ELF_C_READ, NULL);
	if (elf == NULL || elf_kind(elf) != ELF_K_ELF) {
		DBG_TNF_PROBE_0(_tnfctl_traverse_object_2, "libtnfctl",
			"sunw%verbosity 3; sunw%debug 'not elf object'");
		return (TNFCTL_ERR_INTERNAL);
	}
	/* get the elf header */
	if ((ehdr = gelf_getehdr(elf, &ehdr_obj)) == NULL) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_object: gelf_getehdr failed\n"));
		(void) elf_end(elf);
		return (TNFCTL_ERR_INTERNAL);
	}
	if ((ehdr->e_type != ET_EXEC) && (ehdr->e_type != ET_DYN)) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_object: not an "
			"executable or a shared object\n"));
		(void) elf_end(elf);
		return (TNFCTL_ERR_INTERNAL);
	}
	/* if an executable file, the base address is 0 */
	if (ehdr->e_type == ET_EXEC)
		addr = 0;
	/* get a pointer to the elf header string table */
	strs = elf_strptr(elf, ehdr->e_shstrndx, NULL);

	DBG_TNF_PROBE_1(_tnfctl_traverse_object_3, "libtnfctl",
			"sunw%verbosity 3",
			tnf_long, num_sections_found, ehdr->e_shnum);

	for (idx = 1; idx < ehdr->e_shnum; idx++) {
		Elf_Scn		*scn;

		if ((scn = elf_getscn(elf, idx)) == NULL) {
			DBG((void) fprintf(stderr,
			    "_tnfctl_traverse_object: elf_getscn failed\n"));
			prexstat = TNFCTL_ERR_INTERNAL;
			break;
		}
		if ((shdr = gelf_getshdr(scn, &shdr_obj)) == NULL) {
			DBG((void) fprintf(stderr,
				"_tnfctl_traverse_obj:gelf_getshdr failed\n"));
			prexstat = TNFCTL_ERR_INTERNAL;
			break;
		}

		if ((data = elf_getdata(scn, NULL)) == NULL) {
			DBG((void) fprintf(stderr,
				"_tnfctl_traverse_obj:gelf_getdata failed\n"));
			prexstat = TNFCTL_ERR_INTERNAL;
			break;
		}
		/* call the supplied function */
		prexstat = search_info_p->section_func(elf,
			strs, scn, shdr, data, addr, search_info_p);
		if (prexstat)
			break;
	}

	(void) elf_end(elf);

	return (prexstat);

}				/* end _tnfctl_traverse_object */


/*
 * _tnfctl_traverse_rela() - this function traverses a .rela section calling the
 * supplied function on each relocation record.
 */
/*ARGSUSED*/
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)
{
	Elf_Scn		*sym_scn;
	GElf_Shdr	*sym_shdr, sym_shdr_obj;
	Elf_Data	*sym_data;
	Elf3264_Sym	*sym_table;
	Elf_Scn		*str_scn;
	GElf_Shdr	*str_shdr, str_shdr_obj;
	Elf_Data	*str_data;
	char		*str_table;
	ulong_t		nrels;
	uint_t		i;
	boolean_t	isrela;
	size_t		rela_sz;
	char		*ptr;

	DBG_TNF_PROBE_0(_tnfctl_traverse_rela_1, "libtnfctl",
				"sunw%verbosity 4");

	/* bail if this isn't a rela (or rel) section */
	if (rel_shdr->sh_type == SHT_RELA) {
		isrela = B_TRUE;
	} else if (rel_shdr->sh_type == SHT_REL) {
		isrela = B_FALSE;
	} else
		return (TNFCTL_ERR_NONE);

	/* find the symbol table section associated with this rela section */
	sym_scn = elf_getscn(elf, rel_shdr->sh_link);
	if (sym_scn == NULL) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_rela:elf_getscn (sym) failed\n"));
		return (TNFCTL_ERR_INTERNAL);
	}
	sym_shdr = gelf_getshdr(sym_scn, &sym_shdr_obj);
	if (sym_shdr == NULL) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_rela:gelf_getshdr (sym) failed\n"));
		return (TNFCTL_ERR_INTERNAL);
	}
	sym_data = elf_getdata(sym_scn, NULL);
	if (sym_data == NULL) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_rela:elf_getdata (sym) failed\n"));
		return (TNFCTL_ERR_INTERNAL);
	}
	sym_table = (Elf3264_Sym *) sym_data->d_buf;

	/* find the string table associated with the symbol table */
	str_scn = elf_getscn(elf, sym_shdr->sh_link);
	if (str_scn == NULL) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_rela:elf_getscn (str) failed\n"));
		return (TNFCTL_ERR_INTERNAL);
	}
	str_shdr = gelf_getshdr(str_scn, &str_shdr_obj);
	if (str_shdr == NULL) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_rela:gelf_getshdr (str) failed\n"));
		return (TNFCTL_ERR_INTERNAL);
	}
	str_data = elf_getdata(str_scn, NULL);
	if (str_data == NULL) {
		DBG((void) fprintf(stderr,
			"_tnfctl_traverse_rela: elf_getdata (str) failed\n"));
		return (TNFCTL_ERR_INTERNAL);
	}
	str_table = (char *) str_data->d_buf;

	/* loop over each relocation record */
	nrels = rel_shdr->sh_size / rel_shdr->sh_entsize;

	DBG_TNF_PROBE_1(_tnfctl_traverse_rela_2, "libtnfctl",
			"sunw%verbosity 3",
			tnf_long, relocations_found, nrels);

	ptr = rel_data->d_buf;
	rela_sz = (isrela) ? sizeof (Elf3264_Rela) : sizeof (Elf3264_Rel);
	for (i = 0; i < nrels; i++, ptr += rela_sz) {
		Elf3264_Word	syminfo;
		Elf3264_Sym	*sym;
		Elf3264_Addr	offset;
		char		*name;
		uintptr_t	addr;
		tnfctl_errcode_t	prexstat;

		/* decode the r_info field of the relocation record */
		if (isrela) {
			Elf3264_Rela	 *rela_p;

			/*LINTED pointer cast may result in improper alignment*/
			rela_p = (Elf3264_Rela *) ptr;
			syminfo = ELF3264_R_SYM(rela_p->r_info);
			offset = rela_p->r_offset;
		} else {
			Elf3264_Rel	  *rel_p;

			/*LINTED pointer cast may result in improper alignment*/
			rel_p = (Elf3264_Rel *) ptr;
			syminfo = ELF3264_R_SYM(rel_p->r_info);
			offset = rel_p->r_offset;
		}

		/* find the associated symbol table entry */
		if (!syminfo)
			continue;
		sym = sym_table + syminfo;

		/* find the associated string table entry */
		if (!sym->st_name)
			continue;
		name = str_table + sym->st_name;
		addr = offset + baseaddr;

		prexstat = search_info_p->record_func(name, addr, ptr,
							search_info_p);
		if (prexstat)
			break;
	}

	return (TNFCTL_ERR_NONE);

}				/* end _tnfctl_traverse_rela */


/*
 * _tnfctl_traverse_dynsym() - this function traverses a dynsym section calling
 * the supplied function on each symbol.
 */

/*ARGSUSED*/
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)
{
	ulong_t		nsyms;
	int		i;
	char		*strs;
	tnfctl_errcode_t	prexstat;

	Elf3264_Sym	*syms;

	/* bail if this isn't a dynsym section */
	if (shdr->sh_type != SHT_DYNSYM)
		return (TNFCTL_ERR_NONE);
#if 0
	printf("### entering _tnfctl_traverse_dynsym...\n");
#endif
	syms = data->d_buf;
	nsyms = shdr->sh_size / shdr->sh_entsize;
	strs = elf_strptr(elf, shdr->sh_link, 0);

	DBG_TNF_PROBE_1(_tnfctl_traverse_dynsym_1, "libtnfctl",
			"sunw%verbosity 3",
			tnf_long, symbols_found, nsyms);

	for (i = 0; i < nsyms; i++) {
		Elf3264_Sym	*sym = &syms[i];
		char		*name;
		uintptr_t	addr;

		name = strs + sym->st_name;
		addr = baseaddr + sym->st_value;

#if 0
		if (name != 0)
			printf("_tnfctl_traverse_dynsym: name = %s\n", name);
		else
			printf("_tnfctl_traverse_dynsym: name is 0\n");
#endif
		prexstat = search_info_p->record_func(name,
			addr, sym, search_info_p);
		if (prexstat)
			break;
	}
#if 0
	printf("### leaving _tnfctl_traverse_dynsym...\n");
#endif
	return (prexstat);

}				/* end _tnfctl_traverse_dynsym */