xref: /freebsd/cddl/contrib/opensolaris/lib/libctf/common/ctf_lib.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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 #ifdef illumos
38 #include <dlfcn.h>
39 #else
40 #include <zlib.h>
41 #endif
42 #include <gelf.h>
43 
44 #ifdef illumos
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 #ifdef illumos
62 #pragma init(_libctf_init)
63 #else
64 void    _libctf_init(void) __attribute__ ((constructor));
65 #endif
66 void
67 _libctf_init(void)
68 {
69 #ifdef illumos
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 #ifdef illumos
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, sizeof (hdr));
234 
235 	if (fstat64(fd, &st) == -1)
236 		return (ctf_set_open_errno(errp, errno));
237 
238 	if ((nbytes = pread64(fd, &hdr, 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 				free(sp32);
350 				return (ctf_set_open_errno(errp, errno));
351 			}
352 
353 			for (i = 0; i < shnum; i++)
354 				shdr_to_gelf(&sp32[i], &sp[i]);
355 
356 			free(sp32);
357 
358 		} else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
359 			free(sp);
360 			return (ctf_set_open_errno(errp, errno));
361 		}
362 
363 		/*
364 		 * Now mmap the section header strings section so that we can
365 		 * perform string comparison on the section names.
366 		 */
367 		strs_mapsz = sp[shstrndx].sh_size +
368 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
369 
370 		strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
371 		    fd, sp[shstrndx].sh_offset & _PAGEMASK);
372 
373 		strs = (char *)strs_map +
374 		    (sp[shstrndx].sh_offset & ~_PAGEMASK);
375 
376 		if (strs_map == MAP_FAILED) {
377 			free(sp);
378 			return (ctf_set_open_errno(errp, ECTF_MMAP));
379 		}
380 
381 		/*
382 		 * Iterate over the section header array looking for the CTF
383 		 * section and symbol table.  The strtab is linked to symtab.
384 		 */
385 		for (i = 0; i < shnum; i++) {
386 			const GElf_Shdr *shp = &sp[i];
387 			const GElf_Shdr *lhp = &sp[shp->sh_link];
388 
389 			if (shp->sh_link >= shnum)
390 				continue; /* corrupt sh_link field */
391 
392 			if (shp->sh_name >= sp[shstrndx].sh_size ||
393 			    lhp->sh_name >= sp[shstrndx].sh_size)
394 				continue; /* corrupt sh_name field */
395 
396 			if (shp->sh_type == SHT_PROGBITS &&
397 			    strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
398 				ctfsect.cts_name = strs + shp->sh_name;
399 				ctfsect.cts_type = shp->sh_type;
400 				ctfsect.cts_flags = shp->sh_flags;
401 				ctfsect.cts_size = shp->sh_size;
402 				ctfsect.cts_entsize = shp->sh_entsize;
403 				ctfsect.cts_offset = (off64_t)shp->sh_offset;
404 
405 			} else if (shp->sh_type == SHT_SYMTAB) {
406 				symsect.cts_name = strs + shp->sh_name;
407 				symsect.cts_type = shp->sh_type;
408 				symsect.cts_flags = shp->sh_flags;
409 				symsect.cts_size = shp->sh_size;
410 				symsect.cts_entsize = shp->sh_entsize;
411 				symsect.cts_offset = (off64_t)shp->sh_offset;
412 
413 				strsect.cts_name = strs + lhp->sh_name;
414 				strsect.cts_type = lhp->sh_type;
415 				strsect.cts_flags = lhp->sh_flags;
416 				strsect.cts_size = lhp->sh_size;
417 				strsect.cts_entsize = lhp->sh_entsize;
418 				strsect.cts_offset = (off64_t)lhp->sh_offset;
419 			}
420 		}
421 
422 		free(sp); /* free section header array */
423 
424 		if (ctfsect.cts_type == SHT_NULL) {
425 			(void) munmap(strs_map, strs_mapsz);
426 			return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
427 		}
428 
429 		/*
430 		 * Now mmap the CTF data, symtab, and strtab sections and
431 		 * call ctf_bufopen() to do the rest of the work.
432 		 */
433 		if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
434 			(void) munmap(strs_map, strs_mapsz);
435 			return (ctf_set_open_errno(errp, ECTF_MMAP));
436 		}
437 
438 		if (symsect.cts_type != SHT_NULL &&
439 		    strsect.cts_type != SHT_NULL) {
440 			if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
441 			    ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
442 				(void) ctf_set_open_errno(errp, ECTF_MMAP);
443 				goto bad; /* unmap all and abort */
444 			}
445 			fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
446 		} else
447 			fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
448 bad:
449 		if (fp == NULL) {
450 			ctf_sect_munmap(&ctfsect);
451 			ctf_sect_munmap(&symsect);
452 			ctf_sect_munmap(&strsect);
453 		} else
454 			fp->ctf_flags |= LCTF_MMAP;
455 
456 		(void) munmap(strs_map, strs_mapsz);
457 		return (fp);
458 	}
459 
460 	return (ctf_set_open_errno(errp, ECTF_FMT));
461 }
462 
463 /*
464  * Open the specified file and return a pointer to a CTF container.  The file
465  * can be either an ELF file or raw CTF file.  This is just a convenient
466  * wrapper around ctf_fdopen() for callers.
467  */
468 ctf_file_t *
469 ctf_open(const char *filename, int *errp)
470 {
471 	ctf_file_t *fp;
472 	int fd;
473 
474 	if ((fd = open64(filename, O_RDONLY)) == -1) {
475 		if (errp != NULL)
476 			*errp = errno;
477 		return (NULL);
478 	}
479 
480 	fp = ctf_fdopen(fd, errp);
481 	(void) close(fd);
482 	return (fp);
483 }
484 
485 /*
486  * Write the uncompressed CTF data stream to the specified file descriptor.
487  * This is useful for saving the results of dynamic CTF containers.
488  */
489 int
490 ctf_write(ctf_file_t *fp, int fd)
491 {
492 	const uchar_t *buf = fp->ctf_base;
493 	ssize_t resid = fp->ctf_size;
494 	ssize_t len;
495 
496 	while (resid != 0) {
497 		if ((len = write(fd, buf, resid)) <= 0)
498 			return (ctf_set_errno(fp, errno));
499 		resid -= len;
500 		buf += len;
501 	}
502 
503 	return (0);
504 }
505 
506 /*
507  * Set the CTF library client version to the specified version.  If version is
508  * zero, we just return the default library version number.
509  */
510 int
511 ctf_version(int version)
512 {
513 	if (version < 0) {
514 		errno = EINVAL;
515 		return (-1);
516 	}
517 
518 	if (version > 0) {
519 		if (version > CTF_VERSION) {
520 			errno = ENOTSUP;
521 			return (-1);
522 		}
523 		ctf_dprintf("ctf_version: client using version %d\n", version);
524 		_libctf_version = version;
525 	}
526 
527 	return (_libctf_version);
528 }
529