summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/ksyms_snapshot.c
blob: ee46eb7937ad08b64c8f52b985d4634a61406ab5 (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
/*
 * 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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

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

#include <sys/ksyms.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>

static const char ksyms_shstrtab[] = "\0.symtab\0.strtab\0.shstrtab\0";

#define	KSHDR_NULL	0
#define	KSHDR_SYMTAB	1
#define	KSHDR_STRTAB	2
#define	KSHDR_SHSTRTAB	3
#define	KSHDR_NUM	4

typedef struct ksyms_header {
	Ehdr	elf_hdr;		/* Elf file header */
	Phdr	text_phdr;		/* text program header */
	Phdr	data_phdr;		/* data program header */
	Shdr	shdr[KSHDR_NUM];	/* section headers */
	char	shstrings[sizeof (ksyms_shstrtab)];	/* shstrtab strings */
} ksyms_header_t;

#define	KW_HEADER	0x1
#define	KW_LOCALS	0x2
#define	KW_GLOBALS	0x4
#define	KW_STRINGS	0x8

typedef struct ksyms_walkinfo {
	void	(*kw_emit)(const void *, void *, size_t);
	char	*kw_target;
	ssize_t	kw_resid;
	ssize_t kw_totalsize;
	int	kw_actions;
	size_t	kw_size[KW_STRINGS + 1];
} ksyms_walkinfo_t;

krwlock_t ksyms_lock;
vmem_t *ksyms_arena;

static void
ksyms_emit(ksyms_walkinfo_t *kwp, void *src, size_t size, int action)
{
	if (kwp->kw_actions & action) {
		if ((kwp->kw_resid -= size) >= 0)
			kwp->kw_emit(src, kwp->kw_target, size);
		kwp->kw_totalsize += size;
	}
	kwp->kw_size[action] += size;
}

/*ARGSUSED*/
static void
ksyms_walk_one(void *arg, void *base, size_t size)
{
	ksyms_walkinfo_t *kwp = arg;
	Shdr *symhdr = base;
	Shdr *strhdr = symhdr + symhdr->sh_link;
	size_t symsize = symhdr->sh_entsize;
	size_t nsyms = symhdr->sh_size / symsize;
	char *strings = (char *)strhdr->sh_addr;
	int i;

	for (i = 1; i < nsyms; i++) {
		Sym *sym = (Sym *)(symhdr->sh_addr + i * symsize);
		Sym tmp = *sym;
		char *name = strings + sym->st_name;
		tmp.st_name = kwp->kw_size[KW_STRINGS];
		tmp.st_shndx = SHN_ABS;
		ksyms_emit(kwp, &tmp, sizeof (Sym),
		    ELF_ST_BIND(sym->st_info) == STB_LOCAL ?
		    KW_LOCALS : KW_GLOBALS);
		ksyms_emit(kwp, name, strlen(name) + 1, KW_STRINGS);
	}
}

static ssize_t
ksyms_walk(ksyms_walkinfo_t *kwp, void *target, ssize_t resid,
	void (*emit)(const void *, void *, size_t), void *src, int actions)
{
	Sym tmp;

	bzero(kwp, sizeof (ksyms_walkinfo_t));
	kwp->kw_emit = emit;
	kwp->kw_target = target;
	kwp->kw_resid = resid;
	kwp->kw_actions = actions;

	ksyms_emit(kwp, src, sizeof (ksyms_header_t), KW_HEADER);
	/*
	 * The first symbol table entry is all zeroes; it's unused
	 * because index 0 marks the end of symbol hash chains.
	 */
	bzero(&tmp, sizeof (Sym));
	ksyms_emit(kwp, &tmp, sizeof (Sym), KW_LOCALS);
	ksyms_emit(kwp, &tmp, 1, KW_STRINGS);
	vmem_walk(ksyms_arena, VMEM_ALLOC, ksyms_walk_one, kwp);
	return (kwp->kw_totalsize);
}

size_t
ksyms_snapshot(void (*emit)(const void *, void *, size_t),
	void *buf, size_t len)
{
	ksyms_walkinfo_t kw;
	ksyms_header_t hdr;
	ssize_t size = 0, bufsize = len;
	Shdr *shp;

	rw_enter(&ksyms_lock, RW_READER);

	/*
	 * Compute the size of the header, locals, globals, and strings.
	 */
	(void) ksyms_walk(&kw, NULL, 0, NULL, NULL,
	    KW_HEADER | KW_LOCALS | KW_GLOBALS | KW_STRINGS);

	/*
	 * Construct the ELF header.
	 */
	bzero(&hdr, sizeof (hdr));

	hdr.elf_hdr = ((struct module *)modules.mod_mp)->hdr;
	hdr.elf_hdr.e_phoff = offsetof(ksyms_header_t, text_phdr);
	hdr.elf_hdr.e_shoff = offsetof(ksyms_header_t, shdr);
	hdr.elf_hdr.e_phnum = 2;
	hdr.elf_hdr.e_shnum = KSHDR_NUM;
	hdr.elf_hdr.e_shstrndx = KSHDR_SHSTRTAB;

	hdr.text_phdr.p_type = PT_LOAD;
	hdr.text_phdr.p_vaddr = (Addr)s_text;
	hdr.text_phdr.p_memsz = (Word)(e_text - s_text);
	hdr.text_phdr.p_flags = PF_R | PF_X;

	hdr.data_phdr.p_type = PT_LOAD;
	hdr.data_phdr.p_vaddr = (Addr)s_data;
	hdr.data_phdr.p_memsz = (Word)(e_data - s_data);
	hdr.data_phdr.p_flags = PF_R | PF_W | PF_X;

	shp = &hdr.shdr[KSHDR_SYMTAB];
	shp->sh_name = 1;	/* ksyms_shstrtab[1] = ".symtab" */
	shp->sh_type = SHT_SYMTAB;
	shp->sh_offset = kw.kw_size[KW_HEADER];
	shp->sh_size = kw.kw_size[KW_LOCALS] + kw.kw_size[KW_GLOBALS];
	shp->sh_link = KSHDR_STRTAB;
	shp->sh_info = kw.kw_size[KW_LOCALS] / sizeof (Sym);
	shp->sh_addralign = sizeof (Addr);
	shp->sh_entsize = sizeof (Sym);

	shp = &hdr.shdr[KSHDR_STRTAB];
	shp->sh_name = 9;	/* ksyms_shstrtab[9] = ".strtab" */
	shp->sh_type = SHT_STRTAB;
	shp->sh_offset = kw.kw_size[KW_HEADER] +
	    kw.kw_size[KW_LOCALS] + kw.kw_size[KW_GLOBALS];
	shp->sh_size = kw.kw_size[KW_STRINGS];
	shp->sh_addralign = 1;

	shp = &hdr.shdr[KSHDR_SHSTRTAB];
	shp->sh_name = 17;	/* ksyms_shstrtab[17] = ".shstrtab" */
	shp->sh_type = SHT_STRTAB;
	shp->sh_offset = offsetof(ksyms_header_t, shstrings);
	shp->sh_size = sizeof (ksyms_shstrtab);
	shp->sh_addralign = 1;

	bcopy(ksyms_shstrtab, hdr.shstrings, sizeof (ksyms_shstrtab));

	/*
	 * Emit the symbol table.
	 */
	size += ksyms_walk(&kw, buf, (bufsize - size), emit, &hdr,
					    KW_HEADER);
	size += ksyms_walk(&kw, buf, (bufsize - size), emit,
					    NULL, KW_LOCALS);
	size += ksyms_walk(&kw, buf, (bufsize - size), emit,
					    NULL, KW_GLOBALS);
	size += ksyms_walk(&kw, buf, (bufsize - size), emit, NULL,
					    KW_STRINGS);

	rw_exit(&ksyms_lock);

	return ((size_t)size);
}