xref: /freebsd/cddl/contrib/opensolaris/lib/libctf/common/ctf_lib.c (revision 2f1217877e155a8a7de97e74eb7e82096a5ef316)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32 #include <sys/zmod.h>
33 #include <ctf_impl.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #if defined(sun)
38 #include <dlfcn.h>
39 #else
40 #include <zlib.h>
41 #endif
42 #include <gelf.h>
43 
44 #if defined(sun)
45 #ifdef _LP64
46 static const char *_libctf_zlib = "/usr/lib/64/libz.so";
47 #else
48 static const char *_libctf_zlib = "/usr/lib/libz.so";
49 #endif
50 #endif
51 
52 static struct {
53 	int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
54 	const char *(*z_error)(int);
55 	void *z_dlp;
56 } zlib;
57 
58 static size_t _PAGESIZE;
59 static size_t _PAGEMASK;
60 
61 #if defined(sun)
62 #pragma init(_libctf_init)
63 #else
64 void    _libctf_init(void) __attribute__ ((constructor));
65 #endif
66 void
67 _libctf_init(void)
68 {
69 #if defined(sun)
70 	const char *p = getenv("LIBCTF_DECOMPRESSOR");
71 
72 	if (p != NULL)
73 		_libctf_zlib = p; /* use alternate decompression library */
74 #endif
75 
76 	_libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
77 
78 	_PAGESIZE = getpagesize();
79 	_PAGEMASK = ~(_PAGESIZE - 1);
80 }
81 
82 /*
83  * Attempt to dlopen the decompression library and locate the symbols of
84  * interest that we will need to call.  This information in cached so
85  * that multiple calls to ctf_bufopen() do not need to reopen the library.
86  */
87 void *
88 ctf_zopen(int *errp)
89 {
90 #if defined(sun)
91 	ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
92 
93 	if (zlib.z_dlp != NULL)
94 		return (zlib.z_dlp); /* library is already loaded */
95 
96 	if (access(_libctf_zlib, R_OK) == -1)
97 		return (ctf_set_open_errno(errp, ECTF_ZMISSING));
98 
99 	if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
100 		return (ctf_set_open_errno(errp, ECTF_ZINIT));
101 
102 	zlib.z_uncompress = (int (*)(uchar_t *, ulong_t *, const uchar_t *, ulong_t)) dlsym(zlib.z_dlp, "uncompress");
103 	zlib.z_error = (const char *(*)(int)) dlsym(zlib.z_dlp, "zError");
104 
105 	if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {
106 		(void) dlclose(zlib.z_dlp);
107 		bzero(&zlib, sizeof (zlib));
108 		return (ctf_set_open_errno(errp, ECTF_ZINIT));
109 	}
110 #else
111 	zlib.z_uncompress = uncompress;
112 	zlib.z_error = zError;
113 
114 	/* Dummy return variable as 'no error' */
115 	zlib.z_dlp = (void *) (uintptr_t) 1;
116 #endif
117 
118 	return (zlib.z_dlp);
119 }
120 
121 /*
122  * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
123  * which we then patch through to the functions in the decompression library.
124  */
125 int
126 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
127 {
128 	return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
129 }
130 
131 const char *
132 z_strerror(int err)
133 {
134 	return (zlib.z_error(err));
135 }
136 
137 /*
138  * Convert a 32-bit ELF file header into GElf.
139  */
140 static void
141 ehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
142 {
143 	bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
144 	dst->e_type = src->e_type;
145 	dst->e_machine = src->e_machine;
146 	dst->e_version = src->e_version;
147 	dst->e_entry = (Elf64_Addr)src->e_entry;
148 	dst->e_phoff = (Elf64_Off)src->e_phoff;
149 	dst->e_shoff = (Elf64_Off)src->e_shoff;
150 	dst->e_flags = src->e_flags;
151 	dst->e_ehsize = src->e_ehsize;
152 	dst->e_phentsize = src->e_phentsize;
153 	dst->e_phnum = src->e_phnum;
154 	dst->e_shentsize = src->e_shentsize;
155 	dst->e_shnum = src->e_shnum;
156 	dst->e_shstrndx = src->e_shstrndx;
157 }
158 
159 /*
160  * Convert a 32-bit ELF section header into GElf.
161  */
162 static void
163 shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
164 {
165 	dst->sh_name = src->sh_name;
166 	dst->sh_type = src->sh_type;
167 	dst->sh_flags = src->sh_flags;
168 	dst->sh_addr = src->sh_addr;
169 	dst->sh_offset = src->sh_offset;
170 	dst->sh_size = src->sh_size;
171 	dst->sh_link = src->sh_link;
172 	dst->sh_info = src->sh_info;
173 	dst->sh_addralign = src->sh_addralign;
174 	dst->sh_entsize = src->sh_entsize;
175 }
176 
177 /*
178  * In order to mmap a section from the ELF file, we must round down sh_offset
179  * to the previous page boundary, and mmap the surrounding page.  We store
180  * the pointer to the start of the actual section data back into sp->cts_data.
181  */
182 const void *
183 ctf_sect_mmap(ctf_sect_t *sp, int fd)
184 {
185 	size_t pageoff = sp->cts_offset & ~_PAGEMASK;
186 
187 	caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
188 	    MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
189 
190 	if (base != MAP_FAILED)
191 		sp->cts_data = base + pageoff;
192 
193 	return (base);
194 }
195 
196 /*
197  * Since sp->cts_data has the adjusted offset, we have to again round down
198  * to get the actual mmap address and round up to get the size.
199  */
200 void
201 ctf_sect_munmap(const ctf_sect_t *sp)
202 {
203 	uintptr_t addr = (uintptr_t)sp->cts_data;
204 	uintptr_t pageoff = addr & ~_PAGEMASK;
205 
206 	(void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
207 }
208 
209 /*
210  * Open the specified file descriptor and return a pointer to a CTF container.
211  * The file can be either an ELF file or raw CTF file.  The caller is
212  * responsible for closing the file descriptor when it is no longer needed.
213  */
214 ctf_file_t *
215 ctf_fdopen(int fd, int *errp)
216 {
217 	ctf_sect_t ctfsect, symsect, strsect;
218 	ctf_file_t *fp = NULL;
219 	size_t shstrndx, shnum;
220 
221 	struct stat64 st;
222 	ssize_t nbytes;
223 
224 	union {
225 		ctf_preamble_t ctf;
226 		Elf32_Ehdr e32;
227 		GElf_Ehdr e64;
228 	} hdr;
229 
230 	bzero(&ctfsect, sizeof (ctf_sect_t));
231 	bzero(&symsect, sizeof (ctf_sect_t));
232 	bzero(&strsect, sizeof (ctf_sect_t));
233 	bzero(&hdr.ctf, sizeof (hdr));
234 
235 	if (fstat64(fd, &st) == -1)
236 		return (ctf_set_open_errno(errp, errno));
237 
238 	if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
239 		return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
240 
241 	/*
242 	 * If we have read enough bytes to form a CTF header and the magic
243 	 * string matches, attempt to interpret the file as raw CTF.
244 	 */
245 	if (nbytes >= (ssize_t) sizeof (ctf_preamble_t) &&
246 	    hdr.ctf.ctp_magic == CTF_MAGIC) {
247 		if (hdr.ctf.ctp_version > CTF_VERSION)
248 			return (ctf_set_open_errno(errp, ECTF_CTFVERS));
249 
250 		ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
251 		    MAP_PRIVATE, fd, 0);
252 
253 		if (ctfsect.cts_data == MAP_FAILED)
254 			return (ctf_set_open_errno(errp, errno));
255 
256 		ctfsect.cts_name = _CTF_SECTION;
257 		ctfsect.cts_type = SHT_PROGBITS;
258 		ctfsect.cts_flags = SHF_ALLOC;
259 		ctfsect.cts_size = (size_t)st.st_size;
260 		ctfsect.cts_entsize = 1;
261 		ctfsect.cts_offset = 0;
262 
263 		if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
264 			ctf_sect_munmap(&ctfsect);
265 
266 		return (fp);
267 	}
268 
269 	/*
270 	 * If we have read enough bytes to form an ELF header and the magic
271 	 * string matches, attempt to interpret the file as an ELF file.  We
272 	 * do our own largefile ELF processing, and convert everything to
273 	 * GElf structures so that clients can operate on any data model.
274 	 */
275 	if (nbytes >= (ssize_t) sizeof (Elf32_Ehdr) &&
276 	    bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
277 #if BYTE_ORDER == _BIG_ENDIAN
278 		uchar_t order = ELFDATA2MSB;
279 #else
280 		uchar_t order = ELFDATA2LSB;
281 #endif
282 		GElf_Shdr *sp;
283 
284 		void *strs_map;
285 		size_t strs_mapsz, i;
286 		char *strs;
287 
288 		if (hdr.e32.e_ident[EI_DATA] != order)
289 			return (ctf_set_open_errno(errp, ECTF_ENDIAN));
290 		if (hdr.e32.e_version != EV_CURRENT)
291 			return (ctf_set_open_errno(errp, ECTF_ELFVERS));
292 
293 		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
294 			if (nbytes < (ssize_t) sizeof (GElf_Ehdr))
295 				return (ctf_set_open_errno(errp, ECTF_FMT));
296 		} else {
297 			Elf32_Ehdr e32 = hdr.e32;
298 			ehdr_to_gelf(&e32, &hdr.e64);
299 		}
300 
301 		shnum = hdr.e64.e_shnum;
302 		shstrndx = hdr.e64.e_shstrndx;
303 
304 		/* Extended ELF sections */
305 		if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {
306 			if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
307 				Elf32_Shdr x32;
308 
309 				if (pread64(fd, &x32, sizeof (x32),
310 				    hdr.e64.e_shoff) != sizeof (x32))
311 					return (ctf_set_open_errno(errp,
312 					    errno));
313 
314 				shnum = x32.sh_size;
315 				shstrndx = x32.sh_link;
316 			} else {
317 				Elf64_Shdr x64;
318 
319 				if (pread64(fd, &x64, sizeof (x64),
320 				    hdr.e64.e_shoff) != sizeof (x64))
321 					return (ctf_set_open_errno(errp,
322 					    errno));
323 
324 				shnum = x64.sh_size;
325 				shstrndx = x64.sh_link;
326 			}
327 		}
328 
329 		if (shstrndx >= shnum)
330 			return (ctf_set_open_errno(errp, ECTF_CORRUPT));
331 
332 		nbytes = sizeof (GElf_Shdr) * shnum;
333 
334 		if ((sp = malloc(nbytes)) == NULL)
335 			return (ctf_set_open_errno(errp, errno));
336 
337 		/*
338 		 * Read in and convert to GElf the array of Shdr structures
339 		 * from e_shoff so we can locate sections of interest.
340 		 */
341 		if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
342 			Elf32_Shdr *sp32;
343 
344 			nbytes = sizeof (Elf32_Shdr) * shnum;
345 
346 			if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
347 			    sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
348 				free(sp);
349 				return (ctf_set_open_errno(errp, errno));
350 			}
351 
352 			for (i = 0; i < shnum; i++)
353 				shdr_to_gelf(&sp32[i], &sp[i]);
354 
355 			free(sp32);
356 
357 		} else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
358 			free(sp);
359 			return (ctf_set_open_errno(errp, errno));
360 		}
361 
362 		/*
363 		 * Now mmap the section header strings section so that we can
364 		 * perform string comparison on the section names.
365 		 */
366 		strs_mapsz = sp[shstrndx].sh_size +
367 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
368 
369 		strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
370 		    fd, sp[shstrndx].sh_offset & _PAGEMASK);
371 
372 		strs = (char *)strs_map +
373 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
374 
375 		if (strs_map == MAP_FAILED) {
376 			free(sp);
377 			return (ctf_set_open_errno(errp, ECTF_MMAP));
378 		}
379 
380 		/*
381 		 * Iterate over the section header array looking for the CTF
382 		 * section and symbol table.  The strtab is linked to symtab.
383 		 */
384 		for (i = 0; i < shnum; i++) {
385 			const GElf_Shdr *shp = &sp[i];
386 			const GElf_Shdr *lhp = &sp[shp->sh_link];
387 
388 			if (shp->sh_link >= shnum)
389 				continue; /* corrupt sh_link field */
390 
391 			if (shp->sh_name >= sp[shstrndx].sh_size ||
392 			    lhp->sh_name >= sp[shstrndx].sh_size)
393 				continue; /* corrupt sh_name field */
394 
395 			if (shp->sh_type == SHT_PROGBITS &&
396 			    strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
397 				ctfsect.cts_name = strs + shp->sh_name;
398 				ctfsect.cts_type = shp->sh_type;
399 				ctfsect.cts_flags = shp->sh_flags;
400 				ctfsect.cts_size = shp->sh_size;
401 				ctfsect.cts_entsize = shp->sh_entsize;
402 				ctfsect.cts_offset = (off64_t)shp->sh_offset;
403 
404 			} else if (shp->sh_type == SHT_SYMTAB) {
405 				symsect.cts_name = strs + shp->sh_name;
406 				symsect.cts_type = shp->sh_type;
407 				symsect.cts_flags = shp->sh_flags;
408 				symsect.cts_size = shp->sh_size;
409 				symsect.cts_entsize = shp->sh_entsize;
410 				symsect.cts_offset = (off64_t)shp->sh_offset;
411 
412 				strsect.cts_name = strs + lhp->sh_name;
413 				strsect.cts_type = lhp->sh_type;
414 				strsect.cts_flags = lhp->sh_flags;
415 				strsect.cts_size = lhp->sh_size;
416 				strsect.cts_entsize = lhp->sh_entsize;
417 				strsect.cts_offset = (off64_t)lhp->sh_offset;
418 			}
419 		}
420 
421 		free(sp); /* free section header array */
422 
423 		if (ctfsect.cts_type == SHT_NULL) {
424 			(void) munmap(strs_map, strs_mapsz);
425 			return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
426 		}
427 
428 		/*
429 		 * Now mmap the CTF data, symtab, and strtab sections and
430 		 * call ctf_bufopen() to do the rest of the work.
431 		 */
432 		if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
433 			(void) munmap(strs_map, strs_mapsz);
434 			return (ctf_set_open_errno(errp, ECTF_MMAP));
435 		}
436 
437 		if (symsect.cts_type != SHT_NULL &&
438 		    strsect.cts_type != SHT_NULL) {
439 			if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
440 			    ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
441 				(void) ctf_set_open_errno(errp, ECTF_MMAP);
442 				goto bad; /* unmap all and abort */
443 			}
444 			fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
445 		} else
446 			fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
447 bad:
448 		if (fp == NULL) {
449 			ctf_sect_munmap(&ctfsect);
450 			ctf_sect_munmap(&symsect);
451 			ctf_sect_munmap(&strsect);
452 		} else
453 			fp->ctf_flags |= LCTF_MMAP;
454 
455 		(void) munmap(strs_map, strs_mapsz);
456 		return (fp);
457 	}
458 
459 	return (ctf_set_open_errno(errp, ECTF_FMT));
460 }
461 
462 /*
463  * Open the specified file and return a pointer to a CTF container.  The file
464  * can be either an ELF file or raw CTF file.  This is just a convenient
465  * wrapper around ctf_fdopen() for callers.
466  */
467 ctf_file_t *
468 ctf_open(const char *filename, int *errp)
469 {
470 	ctf_file_t *fp;
471 	int fd;
472 
473 	if ((fd = open64(filename, O_RDONLY)) == -1) {
474 		if (errp != NULL)
475 			*errp = errno;
476 		return (NULL);
477 	}
478 
479 	fp = ctf_fdopen(fd, errp);
480 	(void) close(fd);
481 	return (fp);
482 }
483 
484 /*
485  * Write the uncompressed CTF data stream to the specified file descriptor.
486  * This is useful for saving the results of dynamic CTF containers.
487  */
488 int
489 ctf_write(ctf_file_t *fp, int fd)
490 {
491 	const uchar_t *buf = fp->ctf_base;
492 	ssize_t resid = fp->ctf_size;
493 	ssize_t len;
494 
495 	while (resid != 0) {
496 		if ((len = write(fd, buf, resid)) <= 0)
497 			return (ctf_set_errno(fp, errno));
498 		resid -= len;
499 		buf += len;
500 	}
501 
502 	return (0);
503 }
504 
505 /*
506  * Set the CTF library client version to the specified version.  If version is
507  * zero, we just return the default library version number.
508  */
509 int
510 ctf_version(int version)
511 {
512 	if (version < 0) {
513 		errno = EINVAL;
514 		return (-1);
515 	}
516 
517 	if (version > 0) {
518 		if (version > CTF_VERSION) {
519 			errno = ENOTSUP;
520 			return (-1);
521 		}
522 		ctf_dprintf("ctf_version: client using version %d\n", version);
523 		_libctf_version = version;
524 	}
525 
526 	return (_libctf_version);
527 }
528