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