Print this page
10812 ctf tools shouldn't add blank labels
10813 ctf symbol mapping needs work
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libctf/common/ctf_lib.c
+++ new/usr/src/lib/libctf/common/ctf_lib.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26 /*
27 - * Copyright (c) 2015, Joyent, Inc.
27 + * Copyright (c) 2019, Joyent, Inc.
28 28 */
29 29
30 30 #include <sys/types.h>
31 31 #include <sys/stat.h>
32 32 #include <sys/mman.h>
33 -#include <ctf_impl.h>
33 +#include <libctf_impl.h>
34 34 #include <unistd.h>
35 35 #include <fcntl.h>
36 36 #include <errno.h>
37 37 #include <dlfcn.h>
38 38 #include <gelf.h>
39 39 #include <zlib.h>
40 40 #include <sys/debug.h>
41 41
42 42 #ifdef _LP64
43 43 static const char *_libctf_zlib = "/usr/lib/64/libz.so.1";
44 44 #else
45 45 static const char *_libctf_zlib = "/usr/lib/libz.so.1";
46 46 #endif
47 47
48 48 static struct {
49 49 int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
50 50 int (*z_initcomp)(z_stream *, int, const char *, int);
51 51 int (*z_compress)(z_stream *, int);
52 52 int (*z_finicomp)(z_stream *);
53 53 const char *(*z_error)(int);
54 54 void *z_dlp;
55 55 } zlib;
56 56
57 57 static size_t _PAGESIZE;
58 58 static size_t _PAGEMASK;
59 59
60 60 static uint64_t ctf_phase = 0;
61 61
62 62 #define CTF_COMPRESS_CHUNK (64*1024)
63 63
64 64 typedef struct ctf_zdata {
65 65 void *czd_buf;
66 66 void *czd_next;
67 67 ctf_file_t *czd_ctfp;
68 68 size_t czd_allocsz;
69 69 z_stream czd_zstr;
70 70 } ctf_zdata_t;
71 71
72 72 #pragma init(_libctf_init)
73 73 void
74 74 _libctf_init(void)
75 75 {
76 76 const char *p = getenv("LIBCTF_DECOMPRESSOR");
77 77
78 78 if (p != NULL)
79 79 _libctf_zlib = p; /* use alternate decompression library */
80 80
81 81 _libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
82 82
83 83 _PAGESIZE = getpagesize();
84 84 _PAGEMASK = ~(_PAGESIZE - 1);
85 85 }
86 86
87 87 /*
88 88 * Attempt to dlopen the decompression library and locate the symbols of
89 89 * interest that we will need to call. This information in cached so
90 90 * that multiple calls to ctf_bufopen() do not need to reopen the library.
91 91 */
92 92 void *
93 93 ctf_zopen(int *errp)
94 94 {
95 95 ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
96 96
97 97 if (zlib.z_dlp != NULL)
98 98 return (zlib.z_dlp); /* library is already loaded */
99 99
100 100 if (access(_libctf_zlib, R_OK) == -1)
101 101 return (ctf_set_open_errno(errp, ECTF_ZMISSING));
102 102
103 103 if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
104 104 return (ctf_set_open_errno(errp, ECTF_ZINIT));
105 105
106 106 zlib.z_uncompress = (int (*)()) dlsym(zlib.z_dlp, "uncompress");
107 107 zlib.z_initcomp = (int (*)()) dlsym(zlib.z_dlp, "deflateInit_");
108 108 zlib.z_compress = (int (*)()) dlsym(zlib.z_dlp, "deflate");
109 109 zlib.z_finicomp = (int (*)()) dlsym(zlib.z_dlp, "deflateEnd");
110 110 zlib.z_error = (const char *(*)()) dlsym(zlib.z_dlp, "zError");
111 111
112 112 if (zlib.z_uncompress == NULL || zlib.z_error == NULL ||
113 113 zlib.z_initcomp == NULL|| zlib.z_compress == NULL ||
114 114 zlib.z_finicomp == NULL) {
115 115 (void) dlclose(zlib.z_dlp);
116 116 bzero(&zlib, sizeof (zlib));
117 117 return (ctf_set_open_errno(errp, ECTF_ZINIT));
118 118 }
119 119
120 120 return (zlib.z_dlp);
121 121 }
122 122
123 123 /*
124 124 * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
125 125 * which we then patch through to the functions in the decompression library.
126 126 */
127 127 int
128 128 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
129 129 {
130 130 return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
131 131 }
132 132
133 133 const char *
134 134 z_strerror(int err)
135 135 {
136 136 return (zlib.z_error(err));
137 137 }
138 138
139 139 static int
140 140 ctf_zdata_init(ctf_zdata_t *czd, ctf_file_t *fp)
141 141 {
142 142 ctf_header_t *cthp;
143 143
144 144 bzero(czd, sizeof (ctf_zdata_t));
145 145
146 146 czd->czd_allocsz = fp->ctf_size;
147 147 czd->czd_buf = ctf_data_alloc(czd->czd_allocsz);
148 148 if (czd->czd_buf == MAP_FAILED)
149 149 return (ctf_set_errno(fp, ENOMEM));
150 150
151 151 bcopy(fp->ctf_base, czd->czd_buf, sizeof (ctf_header_t));
152 152 czd->czd_ctfp = fp;
153 153 cthp = czd->czd_buf;
154 154 cthp->cth_flags |= CTF_F_COMPRESS;
155 155 czd->czd_next = (void *)((uintptr_t)czd->czd_buf +
156 156 sizeof (ctf_header_t));
157 157
158 158 if (zlib.z_initcomp(&czd->czd_zstr, Z_BEST_COMPRESSION,
159 159 ZLIB_VERSION, sizeof (z_stream)) != Z_OK)
160 160 return (ctf_set_errno(fp, ECTF_ZLIB));
161 161
162 162 return (0);
163 163 }
164 164
165 165 static int
166 166 ctf_zdata_grow(ctf_zdata_t *czd)
167 167 {
168 168 size_t off;
169 169 size_t newsz;
170 170 void *ndata;
171 171
172 172 off = (uintptr_t)czd->czd_next - (uintptr_t)czd->czd_buf;
173 173 newsz = czd->czd_allocsz + CTF_COMPRESS_CHUNK;
174 174 ndata = ctf_data_alloc(newsz);
175 175 if (ndata == MAP_FAILED) {
176 176 return (ctf_set_errno(czd->czd_ctfp, ENOMEM));
177 177 }
178 178
179 179 bcopy(czd->czd_buf, ndata, off);
180 180 ctf_data_free(czd->czd_buf, czd->czd_allocsz);
181 181 czd->czd_allocsz = newsz;
182 182 czd->czd_buf = ndata;
183 183 czd->czd_next = (void *)((uintptr_t)ndata + off);
184 184
185 185 czd->czd_zstr.next_out = (Bytef *)czd->czd_next;
186 186 czd->czd_zstr.avail_out = CTF_COMPRESS_CHUNK;
187 187 return (0);
188 188 }
189 189
190 190 static int
191 191 ctf_zdata_compress_buffer(ctf_zdata_t *czd, const void *buf, size_t bufsize)
192 192 {
193 193 int err;
194 194
195 195 czd->czd_zstr.next_out = czd->czd_next;
196 196 czd->czd_zstr.avail_out = czd->czd_allocsz -
197 197 ((uintptr_t)czd->czd_next - (uintptr_t)czd->czd_buf);
198 198 czd->czd_zstr.next_in = (Bytef *)buf;
199 199 czd->czd_zstr.avail_in = bufsize;
200 200
201 201 while (czd->czd_zstr.avail_in != 0) {
202 202 if (czd->czd_zstr.avail_out == 0) {
203 203 czd->czd_next = czd->czd_zstr.next_out;
204 204 if ((err = ctf_zdata_grow(czd)) != 0) {
205 205 return (err);
206 206 }
207 207 }
208 208
209 209 if ((err = zlib.z_compress(&czd->czd_zstr, Z_NO_FLUSH)) != Z_OK)
210 210 return (ctf_set_errno(czd->czd_ctfp, ECTF_ZLIB));
211 211 }
212 212 czd->czd_next = czd->czd_zstr.next_out;
213 213
214 214 return (0);
215 215 }
216 216
217 217 static int
218 218 ctf_zdata_flush(ctf_zdata_t *czd, boolean_t finish)
219 219 {
220 220 int err;
221 221 int flag = finish == B_TRUE ? Z_FINISH : Z_FULL_FLUSH;
222 222 int bret = finish == B_TRUE ? Z_STREAM_END : Z_BUF_ERROR;
223 223
224 224 for (;;) {
225 225 if (czd->czd_zstr.avail_out == 0) {
226 226 czd->czd_next = czd->czd_zstr.next_out;
227 227 if ((err = ctf_zdata_grow(czd)) != 0) {
228 228 return (err);
229 229 }
230 230 }
231 231
232 232 err = zlib.z_compress(&czd->czd_zstr, flag);
233 233 if (err == bret) {
234 234 break;
235 235 }
236 236 if (err != Z_OK)
237 237 return (ctf_set_errno(czd->czd_ctfp, ECTF_ZLIB));
238 238
239 239 }
240 240
241 241 czd->czd_next = czd->czd_zstr.next_out;
242 242
243 243 return (0);
244 244 }
245 245
246 246 static int
247 247 ctf_zdata_end(ctf_zdata_t *czd)
248 248 {
249 249 int ret;
250 250
251 251 if ((ret = ctf_zdata_flush(czd, B_TRUE)) != 0)
252 252 return (ret);
253 253
254 254 if ((ret = zlib.z_finicomp(&czd->czd_zstr)) != 0)
255 255 return (ctf_set_errno(czd->czd_ctfp, ECTF_ZLIB));
256 256
257 257 return (0);
258 258 }
259 259
260 260 static void
261 261 ctf_zdata_cleanup(ctf_zdata_t *czd)
262 262 {
263 263 ctf_data_free(czd->czd_buf, czd->czd_allocsz);
264 264 (void) zlib.z_finicomp(&czd->czd_zstr);
265 265 }
266 266
267 267 /*
268 268 * Compress our CTF data and return both the size of the compressed data and the
269 269 * size of the allocation. These may be different due to the nature of
270 270 * compression.
271 271 *
272 272 * In addition, we flush the compression between our two phases such that we
273 273 * maintain a different dictionary between the CTF data and the string section.
274 274 */
275 275 int
276 276 ctf_compress(ctf_file_t *fp, void **buf, size_t *allocsz, size_t *elfsize)
277 277 {
278 278 int err;
279 279 ctf_zdata_t czd;
280 280 ctf_header_t *cthp = (ctf_header_t *)fp->ctf_base;
281 281
282 282 if ((err = ctf_zdata_init(&czd, fp)) != 0)
283 283 return (err);
284 284
285 285 if ((err = ctf_zdata_compress_buffer(&czd, fp->ctf_buf,
286 286 cthp->cth_stroff)) != 0) {
287 287 ctf_zdata_cleanup(&czd);
288 288 return (err);
289 289 }
290 290
291 291 if ((err = ctf_zdata_flush(&czd, B_FALSE)) != 0) {
292 292 ctf_zdata_cleanup(&czd);
293 293 return (err);
294 294 }
295 295
296 296 if ((err = ctf_zdata_compress_buffer(&czd,
297 297 fp->ctf_buf + cthp->cth_stroff, cthp->cth_strlen)) != 0) {
298 298 ctf_zdata_cleanup(&czd);
299 299 return (err);
300 300 }
301 301
302 302 if ((err = ctf_zdata_end(&czd)) != 0) {
303 303 ctf_zdata_cleanup(&czd);
304 304 return (err);
305 305 }
306 306
307 307 *buf = czd.czd_buf;
308 308 *allocsz = czd.czd_allocsz;
309 309 *elfsize = (uintptr_t)czd.czd_next - (uintptr_t)czd.czd_buf;
310 310
311 311 return (0);
312 312 }
313 313
314 314 int
315 315 z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen)
316 316 {
317 317 z_stream zs;
318 318 int err;
319 319
320 320 bzero(&zs, sizeof (z_stream));
321 321 zs.next_in = (uchar_t *)src;
322 322 zs.avail_in = srclen;
323 323 zs.next_out = dst;
324 324 zs.avail_out = *dstlen;
325 325
326 326 if ((err = zlib.z_initcomp(&zs, Z_BEST_COMPRESSION, ZLIB_VERSION,
327 327 sizeof (z_stream))) != Z_OK)
328 328 return (err);
329 329
330 330 if ((err = zlib.z_compress(&zs, Z_FINISH)) != Z_STREAM_END) {
331 331 (void) zlib.z_finicomp(&zs);
332 332 return (err == Z_OK ? Z_BUF_ERROR : err);
333 333 }
334 334
335 335 *dstlen = zs.total_out;
336 336 return (zlib.z_finicomp(&zs));
337 337 }
338 338
339 339 /*
340 340 * Convert a 32-bit ELF file header into GElf.
341 341 */
342 342 static void
343 343 ehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
344 344 {
345 345 bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
346 346 dst->e_type = src->e_type;
347 347 dst->e_machine = src->e_machine;
348 348 dst->e_version = src->e_version;
349 349 dst->e_entry = (Elf64_Addr)src->e_entry;
350 350 dst->e_phoff = (Elf64_Off)src->e_phoff;
351 351 dst->e_shoff = (Elf64_Off)src->e_shoff;
352 352 dst->e_flags = src->e_flags;
353 353 dst->e_ehsize = src->e_ehsize;
354 354 dst->e_phentsize = src->e_phentsize;
355 355 dst->e_phnum = src->e_phnum;
356 356 dst->e_shentsize = src->e_shentsize;
357 357 dst->e_shnum = src->e_shnum;
358 358 dst->e_shstrndx = src->e_shstrndx;
359 359 }
360 360
361 361 /*
362 362 * Convert a 32-bit ELF section header into GElf.
363 363 */
364 364 static void
365 365 shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
366 366 {
367 367 dst->sh_name = src->sh_name;
368 368 dst->sh_type = src->sh_type;
369 369 dst->sh_flags = src->sh_flags;
370 370 dst->sh_addr = src->sh_addr;
371 371 dst->sh_offset = src->sh_offset;
372 372 dst->sh_size = src->sh_size;
373 373 dst->sh_link = src->sh_link;
374 374 dst->sh_info = src->sh_info;
375 375 dst->sh_addralign = src->sh_addralign;
376 376 dst->sh_entsize = src->sh_entsize;
377 377 }
378 378
379 379 /*
380 380 * In order to mmap a section from the ELF file, we must round down sh_offset
381 381 * to the previous page boundary, and mmap the surrounding page. We store
382 382 * the pointer to the start of the actual section data back into sp->cts_data.
383 383 */
384 384 const void *
385 385 ctf_sect_mmap(ctf_sect_t *sp, int fd)
386 386 {
387 387 size_t pageoff = sp->cts_offset & ~_PAGEMASK;
388 388
389 389 caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
390 390 MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
391 391
392 392 if (base != MAP_FAILED)
393 393 sp->cts_data = base + pageoff;
394 394
395 395 return (base);
396 396 }
397 397
398 398 /*
399 399 * Since sp->cts_data has the adjusted offset, we have to again round down
400 400 * to get the actual mmap address and round up to get the size.
401 401 */
402 402 void
403 403 ctf_sect_munmap(const ctf_sect_t *sp)
404 404 {
405 405 uintptr_t addr = (uintptr_t)sp->cts_data;
406 406 uintptr_t pageoff = addr & ~_PAGEMASK;
407 407
408 408 (void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
409 409 }
410 410
411 411 /*
412 412 * Open the specified file descriptor and return a pointer to a CTF container.
413 413 * The file can be either an ELF file or raw CTF file. The caller is
414 414 * responsible for closing the file descriptor when it is no longer needed.
415 415 */
416 416 ctf_file_t *
417 417 ctf_fdcreate_int(int fd, int *errp, ctf_sect_t *ctfp)
418 418 {
419 419 ctf_sect_t ctfsect, symsect, strsect;
420 420 ctf_file_t *fp = NULL;
421 421 size_t shstrndx, shnum;
422 422
423 423 struct stat64 st;
424 424 ssize_t nbytes;
425 425
426 426 union {
427 427 ctf_preamble_t ctf;
428 428 Elf32_Ehdr e32;
429 429 GElf_Ehdr e64;
430 430 } hdr;
431 431
432 432 bzero(&ctfsect, sizeof (ctf_sect_t));
433 433 bzero(&symsect, sizeof (ctf_sect_t));
434 434 bzero(&strsect, sizeof (ctf_sect_t));
435 435 bzero(&hdr.ctf, sizeof (hdr));
436 436
437 437 if (fstat64(fd, &st) == -1)
438 438 return (ctf_set_open_errno(errp, errno));
439 439
440 440 if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
441 441 return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
442 442
443 443 /*
444 444 * If we have read enough bytes to form a CTF header and the magic
445 445 * string matches, attempt to interpret the file as raw CTF.
446 446 */
447 447 if (nbytes >= sizeof (ctf_preamble_t) &&
448 448 hdr.ctf.ctp_magic == CTF_MAGIC) {
449 449 if (ctfp != NULL)
450 450 return (ctf_set_open_errno(errp, EINVAL));
451 451
452 452 if (hdr.ctf.ctp_version > CTF_VERSION)
453 453 return (ctf_set_open_errno(errp, ECTF_CTFVERS));
454 454
455 455 ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
456 456 MAP_PRIVATE, fd, 0);
457 457
458 458 if (ctfsect.cts_data == MAP_FAILED)
459 459 return (ctf_set_open_errno(errp, errno));
460 460
461 461 ctfsect.cts_name = _CTF_SECTION;
462 462 ctfsect.cts_type = SHT_PROGBITS;
463 463 ctfsect.cts_flags = SHF_ALLOC;
464 464 ctfsect.cts_size = (size_t)st.st_size;
465 465 ctfsect.cts_entsize = 1;
466 466 ctfsect.cts_offset = 0;
467 467
468 468 if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
469 469 ctf_sect_munmap(&ctfsect);
470 470
471 471 return (fp);
472 472 }
473 473
474 474 /*
475 475 * If we have read enough bytes to form an ELF header and the magic
476 476 * string matches, attempt to interpret the file as an ELF file. We
477 477 * do our own largefile ELF processing, and convert everything to
478 478 * GElf structures so that clients can operate on any data model.
479 479 */
480 480 if (nbytes >= sizeof (Elf32_Ehdr) &&
481 481 bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
482 482 #ifdef _BIG_ENDIAN
483 483 uchar_t order = ELFDATA2MSB;
484 484 #else
485 485 uchar_t order = ELFDATA2LSB;
486 486 #endif
487 487 GElf_Shdr *sp;
488 488
489 489 void *strs_map;
490 490 size_t strs_mapsz, i;
491 491 const char *strs;
492 492
493 493 if (hdr.e32.e_ident[EI_DATA] != order)
494 494 return (ctf_set_open_errno(errp, ECTF_ENDIAN));
495 495 if (hdr.e32.e_version != EV_CURRENT)
496 496 return (ctf_set_open_errno(errp, ECTF_ELFVERS));
497 497
498 498 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
499 499 if (nbytes < sizeof (GElf_Ehdr))
500 500 return (ctf_set_open_errno(errp, ECTF_FMT));
501 501 } else {
502 502 Elf32_Ehdr e32 = hdr.e32;
503 503 ehdr_to_gelf(&e32, &hdr.e64);
504 504 }
505 505
506 506 shnum = hdr.e64.e_shnum;
507 507 shstrndx = hdr.e64.e_shstrndx;
508 508
509 509 /* Extended ELF sections */
510 510 if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {
511 511 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
512 512 Elf32_Shdr x32;
513 513
514 514 if (pread64(fd, &x32, sizeof (x32),
515 515 hdr.e64.e_shoff) != sizeof (x32))
516 516 return (ctf_set_open_errno(errp,
517 517 errno));
518 518
519 519 shnum = x32.sh_size;
520 520 shstrndx = x32.sh_link;
521 521 } else {
522 522 Elf64_Shdr x64;
523 523
524 524 if (pread64(fd, &x64, sizeof (x64),
525 525 hdr.e64.e_shoff) != sizeof (x64))
526 526 return (ctf_set_open_errno(errp,
527 527 errno));
528 528
529 529 shnum = x64.sh_size;
530 530 shstrndx = x64.sh_link;
531 531 }
532 532 }
533 533
534 534 if (shstrndx >= shnum)
535 535 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
536 536
537 537 nbytes = sizeof (GElf_Shdr) * shnum;
538 538
539 539 if ((sp = malloc(nbytes)) == NULL)
540 540 return (ctf_set_open_errno(errp, errno));
541 541
542 542 /*
543 543 * Read in and convert to GElf the array of Shdr structures
544 544 * from e_shoff so we can locate sections of interest.
545 545 */
546 546 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
547 547 Elf32_Shdr *sp32;
548 548
549 549 nbytes = sizeof (Elf32_Shdr) * shnum;
550 550
551 551 if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
552 552 sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
553 553 free(sp);
554 554 return (ctf_set_open_errno(errp, errno));
555 555 }
556 556
557 557 for (i = 0; i < shnum; i++)
558 558 shdr_to_gelf(&sp32[i], &sp[i]);
559 559
560 560 free(sp32);
561 561
562 562 } else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
563 563 free(sp);
564 564 return (ctf_set_open_errno(errp, errno));
565 565 }
566 566
567 567 /*
568 568 * Now mmap the section header strings section so that we can
569 569 * perform string comparison on the section names.
570 570 */
571 571 strs_mapsz = sp[shstrndx].sh_size +
572 572 (sp[shstrndx].sh_offset & ~_PAGEMASK);
573 573
574 574 strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
575 575 fd, sp[shstrndx].sh_offset & _PAGEMASK);
576 576
577 577 strs = (const char *)strs_map +
578 578 (sp[shstrndx].sh_offset & ~_PAGEMASK);
579 579
580 580 if (strs_map == MAP_FAILED) {
581 581 free(sp);
582 582 return (ctf_set_open_errno(errp, ECTF_MMAP));
583 583 }
584 584
585 585 /*
586 586 * Iterate over the section header array looking for the CTF
587 587 * section and symbol table. The strtab is linked to symtab.
588 588 */
589 589 for (i = 0; i < shnum; i++) {
590 590 const GElf_Shdr *shp = &sp[i];
591 591 const GElf_Shdr *lhp = &sp[shp->sh_link];
592 592
593 593 if (shp->sh_link >= shnum)
594 594 continue; /* corrupt sh_link field */
595 595
596 596 if (shp->sh_name >= sp[shstrndx].sh_size ||
597 597 lhp->sh_name >= sp[shstrndx].sh_size)
598 598 continue; /* corrupt sh_name field */
599 599
600 600 if (shp->sh_type == SHT_PROGBITS &&
601 601 strcmp(strs + shp->sh_name, _CTF_SECTION) == 0 &&
602 602 ctfp == NULL) {
603 603 ctfsect.cts_name = strs + shp->sh_name;
604 604 ctfsect.cts_type = shp->sh_type;
605 605 ctfsect.cts_flags = shp->sh_flags;
606 606 ctfsect.cts_size = shp->sh_size;
607 607 ctfsect.cts_entsize = shp->sh_entsize;
608 608 ctfsect.cts_offset = (off64_t)shp->sh_offset;
609 609
610 610 } else if (shp->sh_type == SHT_SYMTAB) {
611 611 symsect.cts_name = strs + shp->sh_name;
612 612 symsect.cts_type = shp->sh_type;
613 613 symsect.cts_flags = shp->sh_flags;
614 614 symsect.cts_size = shp->sh_size;
615 615 symsect.cts_entsize = shp->sh_entsize;
616 616 symsect.cts_offset = (off64_t)shp->sh_offset;
617 617
618 618 strsect.cts_name = strs + lhp->sh_name;
619 619 strsect.cts_type = lhp->sh_type;
620 620 strsect.cts_flags = lhp->sh_flags;
621 621 strsect.cts_size = lhp->sh_size;
622 622 strsect.cts_entsize = lhp->sh_entsize;
623 623 strsect.cts_offset = (off64_t)lhp->sh_offset;
624 624 }
625 625 }
626 626
627 627 free(sp); /* free section header array */
628 628
629 629 if (ctfp == NULL) {
630 630 if (ctfsect.cts_type == SHT_NULL && ctfp == NULL) {
631 631 (void) munmap(strs_map, strs_mapsz);
632 632 return (ctf_set_open_errno(errp,
633 633 ECTF_NOCTFDATA));
634 634 }
635 635
636 636 /*
637 637 * Now mmap the CTF data, symtab, and strtab sections
638 638 * and call ctf_bufopen() to do the rest of the work.
639 639 */
640 640 if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
641 641 (void) munmap(strs_map, strs_mapsz);
642 642 return (ctf_set_open_errno(errp, ECTF_MMAP));
643 643 }
644 644 ctfp = &ctfsect;
645 645 }
646 646
647 647 if (symsect.cts_type != SHT_NULL &&
648 648 strsect.cts_type != SHT_NULL) {
649 649 if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
650 650 ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
651 651 (void) ctf_set_open_errno(errp, ECTF_MMAP);
652 652 goto bad; /* unmap all and abort */
653 653 }
654 654 fp = ctf_bufopen(ctfp, &symsect, &strsect, errp);
655 655 } else
656 656 fp = ctf_bufopen(ctfp, NULL, NULL, errp);
657 657 bad:
658 658 if (fp == NULL) {
659 659 if (ctfp == NULL)
660 660 ctf_sect_munmap(&ctfsect);
661 661 ctf_sect_munmap(&symsect);
662 662 ctf_sect_munmap(&strsect);
663 663 } else
664 664 fp->ctf_flags |= LCTF_MMAP;
665 665
666 666 (void) munmap(strs_map, strs_mapsz);
667 667 return (fp);
668 668 }
669 669
670 670 return (ctf_set_open_errno(errp, ECTF_FMT));
671 671 }
672 672
673 673 ctf_file_t *
674 674 ctf_fdopen(int fd, int *errp)
675 675 {
676 676 return (ctf_fdcreate_int(fd, errp, NULL));
677 677 }
678 678
679 679 /*
680 680 * Open the specified file and return a pointer to a CTF container. The file
681 681 * can be either an ELF file or raw CTF file. This is just a convenient
682 682 * wrapper around ctf_fdopen() for callers.
683 683 */
684 684 ctf_file_t *
685 685 ctf_open(const char *filename, int *errp)
686 686 {
687 687 ctf_file_t *fp;
688 688 int fd;
689 689
690 690 if ((fd = open64(filename, O_RDONLY)) == -1) {
691 691 if (errp != NULL)
692 692 *errp = errno;
693 693 return (NULL);
694 694 }
695 695
696 696 fp = ctf_fdopen(fd, errp);
697 697 (void) close(fd);
698 698 return (fp);
699 699 }
700 700
701 701 /*
702 702 * Write the uncompressed CTF data stream to the specified file descriptor.
703 703 * This is useful for saving the results of dynamic CTF containers.
704 704 */
705 705 int
706 706 ctf_write(ctf_file_t *fp, int fd)
707 707 {
708 708 const uchar_t *buf = fp->ctf_base;
709 709 ssize_t resid = fp->ctf_size;
710 710 ssize_t len;
711 711
712 712 while (resid != 0) {
713 713 if ((len = write(fd, buf, resid)) <= 0)
714 714 return (ctf_set_errno(fp, errno));
715 715 resid -= len;
716 716 buf += len;
717 717 }
718 718
719 719 return (0);
720 720 }
721 721
722 722 /*
723 723 * Set the CTF library client version to the specified version. If version is
724 724 * zero, we just return the default library version number.
725 725 */
726 726 int
727 727 ctf_version(int version)
728 728 {
729 729 if (version < 0) {
730 730 errno = EINVAL;
731 731 return (-1);
732 732 }
733 733
734 734 if (version > 0) {
735 735 if (version > CTF_VERSION) {
736 736 errno = ENOTSUP;
737 737 return (-1);
738 738 }
739 739 ctf_dprintf("ctf_version: client using version %d\n", version);
↓ open down ↓ |
696 lines elided |
↑ open up ↑ |
740 740 _libctf_version = version;
741 741 }
742 742
743 743 return (_libctf_version);
744 744 }
745 745
746 746 /*
747 747 * A utility function for folks debugging CTF conversion and merging.
748 748 */
749 749 void
750 -ctf_phase_dump(ctf_file_t *fp, const char *phase)
750 +ctf_phase_dump(ctf_file_t *fp, const char *phase, const char *name)
751 751 {
752 752 int fd;
753 753 static char *base;
754 754 char path[MAXPATHLEN];
755 755
756 756 if (base == NULL && (base = getenv("LIBCTF_WRITE_PHASES")) == NULL)
757 757 return;
758 758
759 - (void) snprintf(path, sizeof (path), "%s/libctf.%s.%d.ctf", base,
759 + if (name == NULL)
760 + name = "libctf";
761 +
762 + (void) snprintf(path, sizeof (path), "%s/%s.%s.%d.ctf", base, name,
760 763 phase != NULL ? phase : "",
761 764 ctf_phase);
762 765 if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0777)) < 0)
763 766 return;
764 767 (void) ctf_write(fp, fd);
765 768 (void) close(fd);
769 +}
770 +
771 +void
772 +ctf_phase_bump(void)
773 +{
774 + ctf_phase++;
775 +}
776 +
777 +int
778 +ctf_symtab_iter(ctf_file_t *fp, ctf_symtab_f func, void *arg)
779 +{
780 + ulong_t i;
781 + uintptr_t symbase;
782 + uintptr_t strbase;
783 + const char *file = NULL;
784 + boolean_t primary = B_TRUE;
785 +
786 + if (fp->ctf_symtab.cts_data == NULL ||
787 + fp->ctf_strtab.cts_data == NULL) {
788 + return (ECTF_NOSYMTAB);
789 + }
790 +
791 + symbase = (uintptr_t)fp->ctf_symtab.cts_data;
792 + strbase = (uintptr_t)fp->ctf_strtab.cts_data;
793 +
794 + for (i = 0; i < fp->ctf_nsyms; i++) {
795 + const char *name;
796 + int ret;
797 + uint_t type;
798 + Elf64_Sym sym;
799 +
800 + /*
801 + * The CTF library has historically tried to handle large file
802 + * offsets itself so that way clients can be unaware of such
803 + * isseus. Therefore, we translate everything to a 64-bit ELF
804 + * symbol, this is done to make it so that the rest of the
805 + * library doesn't have to know about these differences. For
806 + * more information see, lib/libctf/common/ctf_lib.c.
807 + */
808 + if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
809 + const Elf32_Sym *symp = (Elf32_Sym *)symbase + i;
810 + uint_t bind, itype;
811 +
812 + sym.st_name = symp->st_name;
813 + sym.st_value = symp->st_value;
814 + sym.st_size = symp->st_size;
815 + bind = ELF32_ST_BIND(symp->st_info);
816 + itype = ELF32_ST_TYPE(symp->st_info);
817 + sym.st_info = ELF64_ST_INFO(bind, itype);
818 + sym.st_other = symp->st_other;
819 + sym.st_shndx = symp->st_shndx;
820 + } else {
821 + const Elf64_Sym *symp = (Elf64_Sym *)symbase + i;
822 +
823 + sym = *symp;
824 + }
825 +
826 + type = ELF64_ST_TYPE(sym.st_info);
827 + name = (const char *)(strbase + sym.st_name);
828 +
829 + /*
830 + * Check first if we have an STT_FILE entry. This is used to
831 + * distinguish between various local symbols when merging.
832 + */
833 + if (type == STT_FILE) {
834 + if (file != NULL) {
835 + primary = B_FALSE;
836 + }
837 + file = name;
838 + continue;
839 + }
840 +
841 + /*
842 + * Check if this is a symbol that we care about.
843 + */
844 + if (!ctf_sym_valid(strbase, type, sym.st_shndx, sym.st_value,
845 + sym.st_name)) {
846 + continue;
847 + }
848 +
849 + if ((ret = func(&sym, i, file, name, primary, arg)) != 0) {
850 + return (ret);
851 + }
852 + }
853 +
854 + return (0);
766 855 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX