Print this page
5688 ELF tools need to be more careful with dwarf data
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/elfdump/common/elfdump.c
+++ new/usr/src/cmd/sgs/elfdump/common/elfdump.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 (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 28 * Dump an elf file.
29 29 */
30 30 #include <stddef.h>
31 31 #include <sys/elf_386.h>
32 32 #include <sys/elf_amd64.h>
33 33 #include <sys/elf_SPARC.h>
34 34 #include <_libelf.h>
35 35 #include <dwarf.h>
36 36 #include <stdio.h>
37 37 #include <unistd.h>
38 38 #include <errno.h>
39 39 #include <strings.h>
40 40 #include <debug.h>
41 41 #include <conv.h>
42 42 #include <msg.h>
43 43 #include <_elfdump.h>
44 44
45 45
46 46 /*
47 47 * VERSYM_STATE is used to maintain information about the VERSYM section
48 48 * in the object being analyzed. It is filled in by versions(), and used
49 49 * by init_symtbl_state() when displaying symbol information.
50 50 *
51 51 * There are three forms of symbol versioning known to us:
52 52 *
53 53 * 1) The original form, introduced with Solaris 2.5, in which
54 54 * the Versym contains indexes to Verdef records, and the
55 55 * Versym values for UNDEF symbols resolved by other objects
56 56 * are all set to 0.
57 57 * 2) The GNU form, which is backward compatible with the original
58 58 * Solaris form, but which adds several extensions:
59 59 * - The Versym also contains indexes to Verneed records, recording
60 60 * which object/version contributed the external symbol at
61 61 * link time. These indexes start with the next value following
62 62 * the final Verdef index. The index is written to the previously
63 63 * reserved vna_other field of the ELF Vernaux structure.
64 64 * - The top bit of the Versym value is no longer part of the index,
65 65 * but is used as a "hidden bit" to prevent binding to the symbol.
66 66 * - Multiple implementations of a given symbol, contained in varying
67 67 * versions are allowed, using special assembler pseudo ops,
68 68 * and encoded in the symbol name using '@' characters.
69 69 * 3) Modified Solaris form, in which we adopt the first GNU extension
70 70 * (Versym indexes to Verneed records), but not the others.
71 71 *
72 72 * elfdump can handle any of these cases. The presence of a DT_VERSYM
73 73 * dynamic element indicates a full GNU object. An object that lacks
74 74 * a DT_VERSYM entry, but which has non-zero vna_other fields in the Vernaux
75 75 * structures is a modified Solaris object. An object that has neither of
76 76 * these uses the original form.
77 77 *
78 78 * max_verndx contains the largest version index that can appear
79 79 * in a Versym entry. This can never be less than 1: In the case where
80 80 * there is no verdef/verneed sections, the [0] index is reserved
81 81 * for local symbols, and the [1] index for globals. If the original
82 82 * Solaris versioning rules are in effect and there is a verdef section,
83 83 * then max_verndex is the number of defined versions. If one of the
84 84 * other versioning forms is in effect, then:
85 85 * 1) If there is no verneed section, it is the same as for
86 86 * original Solaris versioning.
87 87 * 2) If there is a verneed section, the vna_other field of the
88 88 * Vernaux structs contain versions, and max_verndx is the
89 89 * largest such index.
90 90 *
91 91 * If gnu_full is True, the object uses the full GNU form of versioning.
92 92 * The value of the gnu_full field is based on the presence of
93 93 * a DT_VERSYM entry in the dynamic section: GNU ld produces these, and
94 94 * Solaris ld does not.
95 95 *
96 96 * The gnu_needed field is True if the Versym contains indexes to
97 97 * Verneed records, as indicated by non-zero vna_other fields in the Verneed
98 98 * section. If gnu_full is True, then gnu_needed will always be true.
99 99 * However, gnu_needed can be true without gnu_full. This is the modified
100 100 * Solaris form.
101 101 */
102 102 typedef struct {
103 103 Cache *cache; /* Pointer to cache entry for VERSYM */
104 104 Versym *data; /* Pointer to versym array */
105 105 int gnu_full; /* True if object uses GNU versioning rules */
106 106 int gnu_needed; /* True if object uses VERSYM indexes for */
107 107 /* VERNEED (subset of gnu_full) */
108 108 int max_verndx; /* largest versym index value */
109 109 } VERSYM_STATE;
110 110
111 111 /*
112 112 * SYMTBL_STATE is used to maintain information about a single symbol
113 113 * table section, for use by the routines that display symbol information.
114 114 */
115 115 typedef struct {
116 116 const char *file; /* Name of file */
117 117 Ehdr *ehdr; /* ELF header for file */
118 118 Cache *cache; /* Cache of all section headers */
119 119 uchar_t osabi; /* OSABI to use */
120 120 Word shnum; /* # of sections in cache */
121 121 Cache *seccache; /* Cache of symbol table section hdr */
122 122 Word secndx; /* Index of symbol table section hdr */
123 123 const char *secname; /* Name of section */
124 124 uint_t flags; /* Command line option flags */
125 125 struct { /* Extended section index data */
126 126 int checked; /* TRUE if already checked for shxndx */
127 127 Word *data; /* NULL, or extended section index */
128 128 /* used for symbol table entries */
129 129 uint_t n; /* # items in shxndx.data */
130 130 } shxndx;
131 131 VERSYM_STATE *versym; /* NULL, or associated VERSYM section */
132 132 Sym *sym; /* Array of symbols */
133 133 Word symn; /* # of symbols */
134 134 } SYMTBL_STATE;
135 135
136 136 /*
137 137 * A variable of this type is used to track information related to
138 138 * .eh_frame and .eh_frame_hdr sections across calls to unwind_eh_frame().
139 139 */
140 140 typedef struct {
141 141 Word frame_cnt; /* # .eh_frame sections seen */
142 142 Word frame_ndx; /* Section index of 1st .eh_frame */
143 143 Word hdr_cnt; /* # .eh_frame_hdr sections seen */
144 144 Word hdr_ndx; /* Section index of 1st .eh_frame_hdr */
145 145 uint64_t frame_ptr; /* Value of FramePtr field from first */
146 146 /* .eh_frame_hdr section */
147 147 uint64_t frame_base; /* Data addr of 1st .eh_frame */
148 148 } gnu_eh_state_t;
149 149
150 150 /*
151 151 * C++ .exception_ranges entries make use of the signed ptrdiff_t
152 152 * type to record self-relative pointer values. We need a type
153 153 * for this that is matched to the ELFCLASS being processed.
154 154 */
155 155 #if defined(_ELF64)
156 156 typedef int64_t PTRDIFF_T;
157 157 #else
158 158 typedef int32_t PTRDIFF_T;
159 159 #endif
160 160
161 161 /*
162 162 * The Sun C++ ABI uses this struct to define each .exception_ranges
163 163 * entry. From the ABI:
164 164 *
165 165 * The field ret_addr is a self relative pointer to the start of the address
166 166 * range. The name was chosen because in the current implementation the range
167 167 * typically starts at the return address for a call site.
168 168 *
169 169 * The field length is the difference, in bytes, between the pc of the last
170 170 * instruction covered by the exception range and the first. When only a
171 171 * single call site is represented without optimization, this will equal zero.
172 172 *
173 173 * The field handler_addr is a relative pointer which stores the difference
174 174 * between the start of the exception range and the address of all code to
175 175 * catch exceptions and perform the cleanup for stack unwinding.
176 176 *
177 177 * The field type_block is a relative pointer which stores the difference
178 178 * between the start of the exception range and the address of an array used
179 179 * for storing a list of the types of exceptions which can be caught within
180 180 * the exception range.
181 181 */
182 182 typedef struct {
183 183 PTRDIFF_T ret_addr;
184 184 Xword length;
185 185 PTRDIFF_T handler_addr;
186 186 PTRDIFF_T type_block;
187 187 Xword reserved;
188 188 } exception_range_entry;
189 189
190 190 /*
191 191 * Focal point for verifying symbol names.
192 192 */
193 193 static const char *
194 194 string(Cache *refsec, Word ndx, Cache *strsec, const char *file, Word name)
195 195 {
196 196 /*
197 197 * If an error in this routine is due to a property of the string
198 198 * section, as opposed to a bad offset into the section (a property of
199 199 * the referencing section), then we will detect the same error on
200 200 * every call involving those sections. We use these static variables
201 201 * to retain the information needed to only issue each such error once.
202 202 */
203 203 static Cache *last_refsec; /* Last referencing section seen */
204 204 static int strsec_err; /* True if error issued */
205 205
206 206 const char *strs;
207 207 Word strn;
208 208
209 209 if (strsec->c_data == NULL)
210 210 return (NULL);
211 211
212 212 strs = (char *)strsec->c_data->d_buf;
213 213 strn = strsec->c_data->d_size;
214 214
215 215 /*
216 216 * We only print a diagnostic regarding a bad string table once per
217 217 * input section being processed. If the refsec has changed, reset
218 218 * our retained error state.
219 219 */
220 220 if (last_refsec != refsec) {
221 221 last_refsec = refsec;
222 222 strsec_err = 0;
223 223 }
224 224
225 225 /* Verify that strsec really is a string table */
226 226 if (strsec->c_shdr->sh_type != SHT_STRTAB) {
227 227 if (!strsec_err) {
228 228 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOTSTRTAB),
229 229 file, strsec->c_ndx, refsec->c_ndx);
230 230 strsec_err = 1;
231 231 }
232 232 return (MSG_INTL(MSG_STR_UNKNOWN));
233 233 }
234 234
235 235 /*
236 236 * Is the string table offset within range of the available strings?
237 237 */
238 238 if (name >= strn) {
239 239 /*
240 240 * Do we have a empty string table?
241 241 */
242 242 if (strs == NULL) {
243 243 if (!strsec_err) {
244 244 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
245 245 file, strsec->c_name);
246 246 strsec_err = 1;
247 247 }
248 248 } else {
249 249 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSTOFF),
250 250 file, refsec->c_name, EC_WORD(ndx), strsec->c_name,
251 251 EC_WORD(name), EC_WORD(strn - 1));
252 252 }
253 253
254 254 /*
255 255 * Return the empty string so that the calling function can
256 256 * continue it's output diagnostics.
257 257 */
258 258 return (MSG_INTL(MSG_STR_UNKNOWN));
259 259 }
260 260 return (strs + name);
261 261 }
262 262
263 263 /*
264 264 * Relocations can reference section symbols and standard symbols. If the
265 265 * former, establish the section name.
266 266 */
267 267 static const char *
268 268 relsymname(Cache *cache, Cache *csec, Cache *strsec, Word symndx, Word symnum,
269 269 Word relndx, Sym *syms, char *secstr, size_t secsz, const char *file)
270 270 {
271 271 Sym *sym;
272 272 const char *name;
273 273
274 274 if (symndx >= symnum) {
275 275 (void) fprintf(stderr, MSG_INTL(MSG_ERR_RELBADSYMNDX),
276 276 file, EC_WORD(symndx), EC_WORD(relndx));
277 277 return (MSG_INTL(MSG_STR_UNKNOWN));
278 278 }
279 279
280 280 sym = (Sym *)(syms + symndx);
281 281 name = string(csec, symndx, strsec, file, sym->st_name);
282 282
283 283 /*
284 284 * If the symbol represents a section offset construct an appropriate
285 285 * string. Note, although section symbol table entries typically have
286 286 * a NULL name pointer, entries do exist that point into the string
287 287 * table to their own NULL strings.
288 288 */
289 289 if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) &&
290 290 ((sym->st_name == 0) || (*name == '\0'))) {
291 291 (void) snprintf(secstr, secsz, MSG_INTL(MSG_STR_SECTION),
292 292 cache[sym->st_shndx].c_name);
293 293 return ((const char *)secstr);
294 294 }
295 295
296 296 return (name);
297 297 }
298 298
299 299 /*
300 300 * Focal point for establishing a string table section. Data such as the
301 301 * dynamic information simply points to a string table. Data such as
302 302 * relocations, reference a symbol table, which in turn is associated with a
303 303 * string table.
304 304 */
305 305 static int
306 306 stringtbl(Cache *cache, int symtab, Word ndx, Word shnum, const char *file,
307 307 Word *symnum, Cache **symsec, Cache **strsec)
308 308 {
309 309 Shdr *shdr = cache[ndx].c_shdr;
310 310
311 311 if (symtab) {
312 312 /*
313 313 * Validate the symbol table section.
314 314 */
315 315 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
316 316 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
317 317 file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
318 318 return (0);
319 319 }
320 320 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
321 321 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
322 322 file, cache[ndx].c_name);
323 323 return (0);
324 324 }
325 325
326 326 /*
327 327 * Obtain, and verify the symbol table data.
328 328 */
329 329 if ((cache[ndx].c_data == NULL) ||
330 330 (cache[ndx].c_data->d_buf == NULL)) {
331 331 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
332 332 file, cache[ndx].c_name);
333 333 return (0);
334 334 }
335 335
336 336 /*
337 337 * Establish the string table index.
338 338 */
339 339 ndx = shdr->sh_link;
340 340 shdr = cache[ndx].c_shdr;
341 341
342 342 /*
343 343 * Return symbol table information.
344 344 */
345 345 if (symnum)
346 346 *symnum = (shdr->sh_size / shdr->sh_entsize);
347 347 if (symsec)
348 348 *symsec = &cache[ndx];
349 349 }
350 350
351 351 /*
352 352 * Validate the associated string table section.
353 353 */
354 354 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
355 355 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
356 356 file, cache[ndx].c_name, EC_WORD(shdr->sh_link));
357 357 return (0);
358 358 }
359 359
360 360 if (strsec)
361 361 *strsec = &cache[shdr->sh_link];
362 362
363 363 return (1);
364 364 }
365 365
366 366 /*
367 367 * Lookup a symbol and set Sym accordingly.
368 368 *
369 369 * entry:
370 370 * name - Name of symbol to lookup
371 371 * cache - Cache of all section headers
372 372 * shnum - # of sections in cache
373 373 * sym - Address of pointer to receive symbol
374 374 * target - NULL, or section to which the symbol must be associated.
375 375 * symtab - Symbol table to search for symbol
376 376 * file - Name of file
377 377 *
378 378 * exit:
379 379 * If the symbol is found, *sym is set to reference it, and True is
380 380 * returned. If target is non-NULL, the symbol must reference the given
381 381 * section --- otherwise the section is not checked.
382 382 *
383 383 * If no symbol is found, False is returned.
384 384 */
385 385 static int
386 386 symlookup(const char *name, Cache *cache, Word shnum, Sym **sym,
387 387 Cache *target, Cache *symtab, const char *file)
388 388 {
389 389 Shdr *shdr;
390 390 Word symn, cnt;
391 391 Sym *syms;
392 392
393 393 if (symtab == 0)
394 394 return (0);
395 395
396 396 shdr = symtab->c_shdr;
397 397
398 398 /*
399 399 * Determine the symbol data and number.
400 400 */
401 401 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
402 402 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
403 403 file, symtab->c_name);
404 404 return (0);
405 405 }
406 406 if (symtab->c_data == NULL)
407 407 return (0);
408 408
409 409 /* LINTED */
410 410 symn = (Word)(shdr->sh_size / shdr->sh_entsize);
411 411 syms = (Sym *)symtab->c_data->d_buf;
412 412
413 413 /*
414 414 * Get the associated string table section.
415 415 */
416 416 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
417 417 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
418 418 file, symtab->c_name, EC_WORD(shdr->sh_link));
419 419 return (0);
420 420 }
421 421
422 422 /*
423 423 * Loop through the symbol table to find a match.
424 424 */
425 425 *sym = NULL;
426 426 for (cnt = 0; cnt < symn; syms++, cnt++) {
427 427 const char *symname;
428 428
429 429 symname = string(symtab, cnt, &cache[shdr->sh_link], file,
430 430 syms->st_name);
431 431
432 432 if (symname && (strcmp(name, symname) == 0) &&
433 433 ((target == NULL) || (target->c_ndx == syms->st_shndx))) {
434 434 /*
435 435 * It is possible, though rare, for a local and
436 436 * global symbol of the same name to exist, each
437 437 * contributed by a different input object. If the
438 438 * symbol just found is local, remember it, but
439 439 * continue looking.
440 440 */
441 441 *sym = syms;
442 442 if (ELF_ST_BIND(syms->st_info) != STB_LOCAL)
443 443 break;
444 444 }
445 445 }
446 446
447 447 return (*sym != NULL);
448 448 }
449 449
450 450 /*
451 451 * Print section headers.
452 452 */
453 453 static void
454 454 sections(const char *file, Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi)
455 455 {
456 456 size_t seccnt;
457 457
458 458 for (seccnt = 1; seccnt < shnum; seccnt++) {
459 459 Cache *_cache = &cache[seccnt];
460 460 Shdr *shdr = _cache->c_shdr;
461 461 const char *secname = _cache->c_name;
462 462
463 463 /*
464 464 * Although numerous section header entries can be zero, it's
465 465 * usually a sign of trouble if the type is zero.
466 466 */
467 467 if (shdr->sh_type == 0) {
468 468 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHTYPE),
469 469 file, secname, EC_WORD(shdr->sh_type));
470 470 }
471 471
472 472 if (!match(MATCH_F_ALL, secname, seccnt, shdr->sh_type))
473 473 continue;
474 474
475 475 /*
476 476 * Identify any sections that are suspicious. A .got section
477 477 * shouldn't exist in a relocatable object.
478 478 */
479 479 if (ehdr->e_type == ET_REL) {
480 480 if (strncmp(secname, MSG_ORIG(MSG_ELF_GOT),
481 481 MSG_ELF_GOT_SIZE) == 0) {
482 482 (void) fprintf(stderr,
483 483 MSG_INTL(MSG_GOT_UNEXPECTED), file,
484 484 secname);
485 485 }
486 486 }
487 487
488 488 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
489 489 dbg_print(0, MSG_INTL(MSG_ELF_SHDR), EC_WORD(seccnt), secname);
490 490 Elf_shdr(0, osabi, ehdr->e_machine, shdr);
491 491 }
492 492 }
493 493
494 494 /*
495 495 * Obtain a specified Phdr entry.
496 496 */
497 497 static Phdr *
498 498 getphdr(Word phnum, Word *type_arr, Word type_cnt, const char *file, Elf *elf)
499 499 {
500 500 Word cnt, tcnt;
501 501 Phdr *phdr;
502 502
503 503 if ((phdr = elf_getphdr(elf)) == NULL) {
504 504 failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
505 505 return (NULL);
506 506 }
507 507
508 508 for (cnt = 0; cnt < phnum; phdr++, cnt++) {
509 509 for (tcnt = 0; tcnt < type_cnt; tcnt++) {
510 510 if (phdr->p_type == type_arr[tcnt])
511 511 return (phdr);
512 512 }
513 513 }
514 514 return (NULL);
515 515 }
516 516
517 517 /*
518 518 * Display the contents of GNU/amd64 .eh_frame and .eh_frame_hdr
519 519 * sections.
520 520 *
521 521 * entry:
522 522 * cache - Cache of all section headers
523 523 * shndx - Index of .eh_frame or .eh_frame_hdr section to be displayed
524 524 * shnum - Total number of sections which exist
525 525 * uphdr - NULL, or unwind program header associated with
526 526 * the .eh_frame_hdr section.
527 527 * ehdr - ELF header for file
528 528 * eh_state - Data used across calls to this routine. The
529 529 * caller should zero it before the first call, and
530 530 * pass it on every call.
531 531 * osabi - OSABI to use in displaying information
532 532 * file - Name of file
533 533 * flags - Command line option flags
534 534 */
535 535 static void
536 536 unwind_eh_frame(Cache *cache, Word shndx, Word shnum, Phdr *uphdr, Ehdr *ehdr,
537 537 gnu_eh_state_t *eh_state, uchar_t osabi, const char *file, uint_t flags)
538 538 {
539 539 #if defined(_ELF64)
540 540 #define MSG_UNW_BINSRTAB2 MSG_UNW_BINSRTAB2_64
541 541 #define MSG_UNW_BINSRTABENT MSG_UNW_BINSRTABENT_64
542 542 #else
543 543 #define MSG_UNW_BINSRTAB2 MSG_UNW_BINSRTAB2_32
↓ open down ↓ |
543 lines elided |
↑ open up ↑ |
544 544 #define MSG_UNW_BINSRTABENT MSG_UNW_BINSRTABENT_32
545 545 #endif
546 546
547 547 Cache *_cache = &cache[shndx];
548 548 Shdr *shdr = _cache->c_shdr;
549 549 uchar_t *data = (uchar_t *)(_cache->c_data->d_buf);
550 550 size_t datasize = _cache->c_data->d_size;
551 551 Conv_dwarf_ehe_buf_t dwarf_ehe_buf;
552 552 uint64_t ndx, frame_ptr, fde_cnt, tabndx;
553 553 uint_t vers, frame_ptr_enc, fde_cnt_enc, table_enc;
554 - uint64_t initloc, initloc0;
554 + uint64_t initloc, initloc0 = 0;
555 555 uint64_t gotaddr = 0;
556 556 int cnt;
557 557
558 558 for (cnt = 1; cnt < shnum; cnt++) {
559 559 if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
560 560 MSG_ELF_GOT_SIZE) == 0) {
561 561 gotaddr = cache[cnt].c_shdr->sh_addr;
562 562 break;
563 563 }
564 564 }
565 565
566 + if ((data == NULL) || (datasize == 0)) {
567 + (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
568 + file, _cache ->c_name);
569 + return;
570 + }
571 +
566 572 /*
567 573 * Is this a .eh_frame_hdr?
568 574 */
569 575 if ((uphdr && (shdr->sh_addr == uphdr->p_vaddr)) ||
570 576 (strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRMHDR),
571 577 MSG_SCN_FRMHDR_SIZE) == 0)) {
572 578 /*
573 579 * There can only be a single .eh_frame_hdr.
574 580 * Flag duplicates.
575 581 */
576 582 if (++eh_state->hdr_cnt > 1)
577 583 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTEHFRMHDR),
578 584 file, EC_WORD(shndx), _cache->c_name);
579 585
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
580 586 dbg_print(0, MSG_ORIG(MSG_UNW_FRMHDR));
581 587 ndx = 0;
582 588
583 589 vers = data[ndx++];
584 590 frame_ptr_enc = data[ndx++];
585 591 fde_cnt_enc = data[ndx++];
586 592 table_enc = data[ndx++];
587 593
588 594 dbg_print(0, MSG_ORIG(MSG_UNW_FRMVERS), vers);
589 595
590 - frame_ptr = dwarf_ehe_extract(data, &ndx, frame_ptr_enc,
591 - ehdr->e_ident, B_TRUE, shdr->sh_addr, ndx, gotaddr);
596 + switch (dwarf_ehe_extract(data, datasize, &ndx,
597 + &frame_ptr, frame_ptr_enc, ehdr->e_ident, B_TRUE,
598 + shdr->sh_addr, ndx, gotaddr)) {
599 + case DW_OVERFLOW:
600 + (void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
601 + file, _cache->c_name);
602 + return;
603 + case DW_BAD_ENCODING:
604 + (void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
605 + file, _cache->c_name, frame_ptr_enc);
606 + return;
607 + case DW_SUCCESS:
608 + break;
609 + }
592 610 if (eh_state->hdr_cnt == 1) {
593 611 eh_state->hdr_ndx = shndx;
594 612 eh_state->frame_ptr = frame_ptr;
595 613 }
596 614
597 615 dbg_print(0, MSG_ORIG(MSG_UNW_FRPTRENC),
598 616 conv_dwarf_ehe(frame_ptr_enc, &dwarf_ehe_buf),
599 617 EC_XWORD(frame_ptr));
600 618
601 - fde_cnt = dwarf_ehe_extract(data, &ndx, fde_cnt_enc,
602 - ehdr->e_ident, B_TRUE, shdr->sh_addr, ndx, gotaddr);
619 + switch (dwarf_ehe_extract(data, datasize, &ndx, &fde_cnt,
620 + fde_cnt_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr, ndx,
621 + gotaddr)) {
622 + case DW_OVERFLOW:
623 + (void) fprintf(stderr, MSG_INTL(MSG_ERR_DWOVRFLW),
624 + file, _cache->c_name);
625 + return;
626 + case DW_BAD_ENCODING:
627 + (void) fprintf(stderr, MSG_INTL(MSG_ERR_DWBADENC),
628 + file, _cache->c_name, fde_cnt_enc);
629 + return;
630 + case DW_SUCCESS:
631 + break;
632 + }
603 633
604 634 dbg_print(0, MSG_ORIG(MSG_UNW_FDCNENC),
605 635 conv_dwarf_ehe(fde_cnt_enc, &dwarf_ehe_buf),
606 636 EC_XWORD(fde_cnt));
607 637 dbg_print(0, MSG_ORIG(MSG_UNW_TABENC),
608 638 conv_dwarf_ehe(table_enc, &dwarf_ehe_buf));
609 639 dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB1));
610 640 dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTAB2));
611 641
612 642 for (tabndx = 0; tabndx < fde_cnt; tabndx++) {
613 - initloc = dwarf_ehe_extract(data, &ndx, table_enc,
614 - ehdr->e_ident, B_TRUE, shdr->sh_addr, ndx, gotaddr);
615 - /*LINTED:E_VAR_USED_BEFORE_SET*/
643 + uint64_t table;
644 +
645 + switch (dwarf_ehe_extract(data, datasize, &ndx,
646 + &initloc, table_enc, ehdr->e_ident, B_TRUE,
647 + shdr->sh_addr, ndx, gotaddr)) {
648 + case DW_OVERFLOW:
649 + (void) fprintf(stderr,
650 + MSG_INTL(MSG_ERR_DWOVRFLW), file,
651 + _cache->c_name);
652 + return;
653 + case DW_BAD_ENCODING:
654 + (void) fprintf(stderr,
655 + MSG_INTL(MSG_ERR_DWBADENC), file,
656 + _cache->c_name, table_enc);
657 + return;
658 + case DW_SUCCESS:
659 + break;
660 + }
616 661 if ((tabndx != 0) && (initloc0 > initloc))
617 662 (void) fprintf(stderr,
618 663 MSG_INTL(MSG_ERR_BADSORT), file,
619 664 _cache->c_name, EC_WORD(tabndx));
665 + switch (dwarf_ehe_extract(data, datasize, &ndx, &table,
666 + table_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr,
667 + ndx, gotaddr)) {
668 + case DW_OVERFLOW:
669 + (void) fprintf(stderr,
670 + MSG_INTL(MSG_ERR_DWOVRFLW), file,
671 + _cache->c_name);
672 + return;
673 + case DW_BAD_ENCODING:
674 + (void) fprintf(stderr,
675 + MSG_INTL(MSG_ERR_DWBADENC), file,
676 + _cache->c_name, table_enc);
677 + return;
678 + case DW_SUCCESS:
679 + break;
680 + }
681 +
620 682 dbg_print(0, MSG_ORIG(MSG_UNW_BINSRTABENT),
621 683 EC_XWORD(initloc),
622 - EC_XWORD(dwarf_ehe_extract(data, &ndx,
623 - table_enc, ehdr->e_ident, B_TRUE, shdr->sh_addr,
624 - ndx, gotaddr)));
684 + EC_XWORD(table));
625 685 initloc0 = initloc;
626 686 }
627 687 } else { /* Display the .eh_frame section */
628 688 eh_state->frame_cnt++;
629 689 if (eh_state->frame_cnt == 1) {
630 690 eh_state->frame_ndx = shndx;
631 691 eh_state->frame_base = shdr->sh_addr;
632 692 } else if ((eh_state->frame_cnt > 1) &&
633 693 (ehdr->e_type != ET_REL)) {
634 694 Conv_inv_buf_t inv_buf;
635 695
636 696 (void) fprintf(stderr, MSG_INTL(MSG_WARN_MULTEHFRM),
637 697 file, EC_WORD(shndx), _cache->c_name,
638 698 conv_ehdr_type(osabi, ehdr->e_type, 0, &inv_buf));
639 699 }
640 - dump_eh_frame(data, datasize, shdr->sh_addr,
641 - ehdr->e_machine, ehdr->e_ident, gotaddr);
700 + dump_eh_frame(file, _cache->c_name, data, datasize,
701 + shdr->sh_addr, ehdr->e_machine, ehdr->e_ident, gotaddr);
642 702 }
643 703
644 704 /*
645 705 * If we've seen the .eh_frame_hdr and the first .eh_frame section,
646 706 * compare the header frame_ptr to the address of the actual frame
647 707 * section to ensure the link-editor got this right. Note, this
648 708 * diagnostic is only produced when unwind information is explicitly
649 709 * asked for, as shared objects built with an older ld(1) may reveal
650 710 * this inconsistency. Although an inconsistency, it doesn't seem to
651 711 * have any adverse effect on existing tools.
652 712 */
653 713 if (((flags & FLG_MASK_SHOW) != FLG_MASK_SHOW) &&
654 714 (eh_state->hdr_cnt > 0) && (eh_state->frame_cnt > 0) &&
655 715 (eh_state->frame_ptr != eh_state->frame_base))
656 716 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADEHFRMPTR),
657 717 file, EC_WORD(eh_state->hdr_ndx),
658 718 cache[eh_state->hdr_ndx].c_name,
659 719 EC_XWORD(eh_state->frame_ptr),
660 720 EC_WORD(eh_state->frame_ndx),
661 721 cache[eh_state->frame_ndx].c_name,
662 722 EC_XWORD(eh_state->frame_base));
663 723 #undef MSG_UNW_BINSRTAB2
664 724 #undef MSG_UNW_BINSRTABENT
665 725 }
666 726
667 727 /*
668 728 * Convert a self relative pointer into an address. A self relative
669 729 * pointer adds the address where the pointer resides to the offset
670 730 * contained in the pointer. The benefit is that the value of the
671 731 * pointer does not require relocation.
672 732 *
673 733 * entry:
674 734 * base_addr - Address of the pointer.
675 735 * delta - Offset relative to base_addr giving desired address
676 736 *
677 737 * exit:
678 738 * The computed address is returned.
679 739 *
680 740 * note:
681 741 * base_addr is an unsigned value, while ret_addr is signed. This routine
682 742 * used explicit testing and casting to explicitly control type
683 743 * conversion, and ensure that we handle the maximum possible range.
684 744 */
685 745 static Addr
686 746 srelptr(Addr base_addr, PTRDIFF_T delta)
687 747 {
688 748 if (delta < 0)
689 749 return (base_addr - (Addr) (-delta));
690 750
691 751 return (base_addr + (Addr) delta);
692 752 }
693 753
694 754 /*
695 755 * Byte swap a PTRDIFF_T value.
696 756 */
697 757 static PTRDIFF_T
698 758 swap_ptrdiff(PTRDIFF_T value)
699 759 {
700 760 PTRDIFF_T r;
701 761 uchar_t *dst = (uchar_t *)&r;
702 762 uchar_t *src = (uchar_t *)&value;
703 763
704 764 UL_ASSIGN_BSWAP_XWORD(dst, src);
705 765 return (r);
706 766 }
707 767
708 768 /*
709 769 * Display exception_range_entry items from the .exception_ranges section
710 770 * of a Sun C++ object.
711 771 */
712 772 static void
713 773 unwind_exception_ranges(Cache *_cache, const char *file, int do_swap)
714 774 {
715 775 /*
716 776 * Translate a PTRDIFF_T self-relative address field of
717 777 * an exception_range_entry struct into an address.
718 778 *
719 779 * entry:
720 780 * exc_addr - Address of base of exception_range_entry struct
721 781 * cur_ent - Pointer to data in the struct to be translated
722 782 *
723 783 * _f - Field of struct to be translated
724 784 */
725 785 #define SRELPTR(_f) \
726 786 srelptr(exc_addr + offsetof(exception_range_entry, _f), cur_ent->_f)
727 787
728 788 #if defined(_ELF64)
↓ open down ↓ |
77 lines elided |
↑ open up ↑ |
729 789 #define MSG_EXR_TITLE MSG_EXR_TITLE_64
730 790 #define MSG_EXR_ENTRY MSG_EXR_ENTRY_64
731 791 #else
732 792 #define MSG_EXR_TITLE MSG_EXR_TITLE_32
733 793 #define MSG_EXR_ENTRY MSG_EXR_ENTRY_32
734 794 #endif
735 795
736 796 exception_range_entry scratch, *ent, *cur_ent = &scratch;
737 797 char index[MAXNDXSIZE];
738 798 Word i, nelts;
739 - Addr addr, addr0, offset = 0;
799 + Addr addr, addr0 = 0, offset = 0;
740 800 Addr exc_addr = _cache->c_shdr->sh_addr;
741 801
742 802 dbg_print(0, MSG_INTL(MSG_EXR_TITLE));
743 803 ent = (exception_range_entry *)(_cache->c_data->d_buf);
744 804 nelts = _cache->c_data->d_size / sizeof (exception_range_entry);
745 805
746 806 for (i = 0; i < nelts; i++, ent++) {
747 807 if (do_swap) {
748 808 /*
749 809 * Copy byte swapped values into the scratch buffer.
750 810 * The reserved field is not used, so we skip it.
751 811 */
752 812 scratch.ret_addr = swap_ptrdiff(ent->ret_addr);
753 813 scratch.length = BSWAP_XWORD(ent->length);
754 814 scratch.handler_addr = swap_ptrdiff(ent->handler_addr);
755 815 scratch.type_block = swap_ptrdiff(ent->type_block);
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
756 816 } else {
757 817 cur_ent = ent;
758 818 }
759 819
760 820 /*
761 821 * The table is required to be sorted by the address
762 822 * derived from ret_addr, to allow binary searching. Ensure
763 823 * that addresses grow monotonically.
764 824 */
765 825 addr = SRELPTR(ret_addr);
766 - /*LINTED:E_VAR_USED_BEFORE_SET*/
767 826 if ((i != 0) && (addr0 > addr))
768 827 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSORT),
769 828 file, _cache->c_name, EC_WORD(i));
770 829
771 830 (void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
772 831 EC_XWORD(i));
773 832 dbg_print(0, MSG_INTL(MSG_EXR_ENTRY), index, EC_ADDR(offset),
774 833 EC_ADDR(addr), EC_ADDR(cur_ent->length),
775 834 EC_ADDR(SRELPTR(handler_addr)),
776 835 EC_ADDR(SRELPTR(type_block)));
777 836
778 837 addr0 = addr;
779 838 exc_addr += sizeof (exception_range_entry);
780 839 offset += sizeof (exception_range_entry);
781 840 }
782 841
783 842 #undef SRELPTR
784 843 #undef MSG_EXR_TITLE
785 844 #undef MSG_EXR_ENTRY
786 845 }
787 846
788 847 /*
789 848 * Display information from unwind/exception sections:
790 849 *
791 850 * - GNU/amd64 .eh_frame and .eh_frame_hdr
792 851 * - Sun C++ .exception_ranges
793 852 *
794 853 */
795 854 static void
796 855 unwind(Cache *cache, Word shnum, Word phnum, Ehdr *ehdr, uchar_t osabi,
797 856 const char *file, Elf *elf, uint_t flags)
798 857 {
799 858 static Word phdr_types[] = { PT_SUNW_UNWIND, PT_SUNW_EH_FRAME };
800 859
801 860 Word cnt;
802 861 Phdr *uphdr = NULL;
803 862 gnu_eh_state_t eh_state;
804 863
805 864 /*
806 865 * Historical background: .eh_frame and .eh_frame_hdr sections
807 866 * come from the GNU compilers (particularly C++), and are used
808 867 * under all architectures. Their format is based on DWARF. When
809 868 * the amd64 ABI was defined, these sections were adopted wholesale
810 869 * from the existing practice.
811 870 *
812 871 * When amd64 support was added to Solaris, support for these
813 872 * sections was added, using the SHT_AMD64_UNWIND section type
814 873 * to identify them. At first, we ignored them in objects for
815 874 * non-amd64 targets, but later broadened our support to include
816 875 * other architectures in order to better support gcc-generated
817 876 * objects.
818 877 *
819 878 * .exception_ranges implement the same basic concepts, but
820 879 * were invented at Sun for the Sun C++ compiler.
821 880 *
822 881 * We match these sections by name, rather than section type,
823 882 * because they can come in as either SHT_AMD64_UNWIND, or as
824 883 * SHT_PROGBITS, and because the type isn't enough to determine
825 884 * how they should be interpreted.
826 885 */
827 886 /* Find the program header for .eh_frame_hdr if present */
828 887 if (phnum)
829 888 uphdr = getphdr(phnum, phdr_types,
830 889 sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
831 890
832 891 /*
833 892 * eh_state is used to retain data used by unwind_eh_frame()
834 893 * across calls.
835 894 */
836 895 bzero(&eh_state, sizeof (eh_state));
837 896
838 897 for (cnt = 1; cnt < shnum; cnt++) {
839 898 Cache *_cache = &cache[cnt];
840 899 Shdr *shdr = _cache->c_shdr;
841 900 int is_exrange;
842 901
843 902 /*
844 903 * Skip sections of the wrong type. On amd64, they
845 904 * can be SHT_AMD64_UNWIND. On all platforms, they
846 905 * can be SHT_PROGBITS (including amd64, if using
847 906 * the GNU compilers).
848 907 *
849 908 * Skip anything other than these two types. The name
850 909 * test below will thin out the SHT_PROGBITS that don't apply.
851 910 */
852 911 if ((shdr->sh_type != SHT_PROGBITS) &&
853 912 (shdr->sh_type != SHT_AMD64_UNWIND))
854 913 continue;
855 914
856 915 /*
857 916 * Only sections with certain well known names are of interest.
858 917 * These are:
859 918 *
860 919 * .eh_frame - amd64/GNU-compiler unwind sections
861 920 * .eh_frame_hdr - Sorted table referencing .eh_frame
862 921 * .exception_ranges - Sun C++ unwind sections
863 922 *
864 923 * We do a prefix comparison, allowing for naming conventions
865 924 * like .eh_frame.foo, hence the use of strncmp() rather than
866 925 * strcmp(). This means that we only really need to test for
867 926 * .eh_frame, as it's a prefix of .eh_frame_hdr.
868 927 */
869 928 is_exrange = strncmp(_cache->c_name,
870 929 MSG_ORIG(MSG_SCN_EXRANGE), MSG_SCN_EXRANGE_SIZE) == 0;
871 930 if ((strncmp(_cache->c_name, MSG_ORIG(MSG_SCN_FRM),
872 931 MSG_SCN_FRM_SIZE) != 0) && !is_exrange)
873 932 continue;
874 933
875 934 if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
876 935 continue;
877 936
878 937 if (_cache->c_data == NULL)
879 938 continue;
880 939
881 940 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
882 941 dbg_print(0, MSG_INTL(MSG_ELF_SCN_UNWIND), _cache->c_name);
883 942
884 943 if (is_exrange)
885 944 unwind_exception_ranges(_cache, file,
886 945 _elf_sys_encoding() != ehdr->e_ident[EI_DATA]);
887 946 else
888 947 unwind_eh_frame(cache, cnt, shnum, uphdr, ehdr,
889 948 &eh_state, osabi, file, flags);
890 949 }
891 950 }
892 951
893 952 /*
894 953 * Initialize a symbol table state structure
895 954 *
896 955 * entry:
897 956 * state - State structure to be initialized
898 957 * cache - Cache of all section headers
899 958 * shnum - # of sections in cache
900 959 * secndx - Index of symbol table section
901 960 * ehdr - ELF header for file
902 961 * versym - Information about versym section
903 962 * file - Name of file
904 963 * flags - Command line option flags
905 964 */
906 965 static int
907 966 init_symtbl_state(SYMTBL_STATE *state, Cache *cache, Word shnum, Word secndx,
908 967 Ehdr *ehdr, uchar_t osabi, VERSYM_STATE *versym, const char *file,
909 968 uint_t flags)
910 969 {
911 970 Shdr *shdr;
912 971
913 972 state->file = file;
914 973 state->ehdr = ehdr;
915 974 state->cache = cache;
916 975 state->osabi = osabi;
917 976 state->shnum = shnum;
918 977 state->seccache = &cache[secndx];
919 978 state->secndx = secndx;
920 979 state->secname = state->seccache->c_name;
921 980 state->flags = flags;
922 981 state->shxndx.checked = 0;
923 982 state->shxndx.data = NULL;
924 983 state->shxndx.n = 0;
925 984
926 985 shdr = state->seccache->c_shdr;
927 986
928 987 /*
929 988 * Check the symbol data and per-item size.
930 989 */
931 990 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
932 991 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
933 992 file, state->secname);
934 993 return (0);
935 994 }
936 995 if (state->seccache->c_data == NULL)
937 996 return (0);
938 997
939 998 /* LINTED */
940 999 state->symn = (Word)(shdr->sh_size / shdr->sh_entsize);
941 1000 state->sym = (Sym *)state->seccache->c_data->d_buf;
942 1001
943 1002 /*
944 1003 * Check associated string table section.
945 1004 */
946 1005 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
947 1006 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
948 1007 file, state->secname, EC_WORD(shdr->sh_link));
949 1008 return (0);
950 1009 }
951 1010
952 1011 /*
953 1012 * Determine if there is a associated Versym section
954 1013 * with this Symbol Table.
955 1014 */
956 1015 if (versym && versym->cache &&
957 1016 (versym->cache->c_shdr->sh_link == state->secndx))
958 1017 state->versym = versym;
959 1018 else
960 1019 state->versym = NULL;
961 1020
962 1021
963 1022 return (1);
964 1023 }
965 1024
966 1025 /*
967 1026 * Determine the extended section index used for symbol tables entries.
968 1027 */
969 1028 static void
970 1029 symbols_getxindex(SYMTBL_STATE *state)
971 1030 {
972 1031 uint_t symn;
973 1032 Word symcnt;
974 1033
975 1034 state->shxndx.checked = 1; /* Note that we've been called */
976 1035 for (symcnt = 1; symcnt < state->shnum; symcnt++) {
977 1036 Cache *_cache = &state->cache[symcnt];
978 1037 Shdr *shdr = _cache->c_shdr;
979 1038
980 1039 if ((shdr->sh_type != SHT_SYMTAB_SHNDX) ||
981 1040 (shdr->sh_link != state->secndx))
982 1041 continue;
983 1042
984 1043 if ((shdr->sh_entsize) &&
985 1044 /* LINTED */
986 1045 ((symn = (uint_t)(shdr->sh_size / shdr->sh_entsize)) == 0))
987 1046 continue;
988 1047
989 1048 if (_cache->c_data == NULL)
990 1049 continue;
991 1050
992 1051 state->shxndx.data = _cache->c_data->d_buf;
993 1052 state->shxndx.n = symn;
994 1053 return;
995 1054 }
996 1055 }
997 1056
998 1057 /*
999 1058 * Produce a line of output for the given symbol
1000 1059 *
1001 1060 * entry:
1002 1061 * state - Symbol table state
1003 1062 * symndx - Index of symbol within the table
1004 1063 * info - Value of st_info (indicates local/global range)
1005 1064 * symndx_disp - Index to display. This may not be the same
1006 1065 * as symndx if the display is relative to the logical
1007 1066 * combination of the SUNW_ldynsym/dynsym tables.
1008 1067 * sym - Symbol to display
1009 1068 */
1010 1069 static void
1011 1070 output_symbol(SYMTBL_STATE *state, Word symndx, Word info, Word disp_symndx,
1012 1071 Sym *sym)
1013 1072 {
1014 1073 /*
1015 1074 * Symbol types for which we check that the specified
1016 1075 * address/size land inside the target section.
1017 1076 */
1018 1077 static const int addr_symtype[] = {
1019 1078 0, /* STT_NOTYPE */
1020 1079 1, /* STT_OBJECT */
1021 1080 1, /* STT_FUNC */
1022 1081 0, /* STT_SECTION */
1023 1082 0, /* STT_FILE */
1024 1083 1, /* STT_COMMON */
1025 1084 0, /* STT_TLS */
1026 1085 0, /* 7 */
1027 1086 0, /* 8 */
1028 1087 0, /* 9 */
1029 1088 0, /* 10 */
1030 1089 0, /* 11 */
1031 1090 0, /* 12 */
1032 1091 0, /* STT_SPARC_REGISTER */
1033 1092 0, /* 14 */
1034 1093 0, /* 15 */
1035 1094 };
1036 1095 #if STT_NUM != (STT_TLS + 1)
1037 1096 #error "STT_NUM has grown. Update addr_symtype[]"
1038 1097 #endif
1039 1098
1040 1099 char index[MAXNDXSIZE];
1041 1100 const char *symname, *sec;
1042 1101 Versym verndx;
1043 1102 int gnuver;
1044 1103 uchar_t type;
1045 1104 Shdr *tshdr;
1046 1105 Word shndx;
1047 1106 Conv_inv_buf_t inv_buf;
1048 1107
1049 1108 /* Ensure symbol index is in range */
1050 1109 if (symndx >= state->symn) {
1051 1110 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSYMNDX),
1052 1111 state->file, state->secname, EC_WORD(symndx));
1053 1112 return;
1054 1113 }
1055 1114
1056 1115 /*
1057 1116 * If we are using extended symbol indexes, find the
1058 1117 * corresponding SHN_SYMTAB_SHNDX table.
1059 1118 */
1060 1119 if ((sym->st_shndx == SHN_XINDEX) && (state->shxndx.checked == 0))
1061 1120 symbols_getxindex(state);
1062 1121
1063 1122 /* LINTED */
1064 1123 symname = string(state->seccache, symndx,
1065 1124 &state->cache[state->seccache->c_shdr->sh_link], state->file,
1066 1125 sym->st_name);
1067 1126
1068 1127 tshdr = NULL;
1069 1128 sec = NULL;
1070 1129
1071 1130 if (state->ehdr->e_type == ET_CORE) {
1072 1131 sec = (char *)MSG_INTL(MSG_STR_UNKNOWN);
1073 1132 } else if (state->flags & FLG_CTL_FAKESHDR) {
1074 1133 /*
1075 1134 * If we are using fake section headers derived from
1076 1135 * the program headers, then the section indexes
1077 1136 * in the symbols do not correspond to these headers.
1078 1137 * The section names are not available, so all we can
1079 1138 * do is to display them in numeric form.
1080 1139 */
1081 1140 sec = conv_sym_shndx(state->osabi, state->ehdr->e_machine,
1082 1141 sym->st_shndx, CONV_FMT_DECIMAL, &inv_buf);
1083 1142 } else if ((sym->st_shndx < SHN_LORESERVE) &&
1084 1143 (sym->st_shndx < state->shnum)) {
1085 1144 shndx = sym->st_shndx;
1086 1145 tshdr = state->cache[shndx].c_shdr;
1087 1146 sec = state->cache[shndx].c_name;
1088 1147 } else if (sym->st_shndx == SHN_XINDEX) {
1089 1148 if (state->shxndx.data) {
1090 1149 Word _shxndx;
1091 1150
1092 1151 if (symndx > state->shxndx.n) {
1093 1152 (void) fprintf(stderr,
1094 1153 MSG_INTL(MSG_ERR_BADSYMXINDEX1),
1095 1154 state->file, state->secname,
1096 1155 EC_WORD(symndx));
1097 1156 } else if ((_shxndx =
1098 1157 state->shxndx.data[symndx]) > state->shnum) {
1099 1158 (void) fprintf(stderr,
1100 1159 MSG_INTL(MSG_ERR_BADSYMXINDEX2),
1101 1160 state->file, state->secname,
1102 1161 EC_WORD(symndx), EC_WORD(_shxndx));
1103 1162 } else {
1104 1163 shndx = _shxndx;
1105 1164 tshdr = state->cache[shndx].c_shdr;
1106 1165 sec = state->cache[shndx].c_name;
1107 1166 }
1108 1167 } else {
1109 1168 (void) fprintf(stderr,
1110 1169 MSG_INTL(MSG_ERR_BADSYMXINDEX3),
1111 1170 state->file, state->secname, EC_WORD(symndx));
1112 1171 }
1113 1172 } else if ((sym->st_shndx < SHN_LORESERVE) &&
1114 1173 (sym->st_shndx >= state->shnum)) {
1115 1174 (void) fprintf(stderr,
1116 1175 MSG_INTL(MSG_ERR_BADSYM5), state->file,
1117 1176 state->secname, EC_WORD(symndx),
1118 1177 demangle(symname, state->flags), sym->st_shndx);
1119 1178 }
1120 1179
1121 1180 /*
1122 1181 * If versioning is available display the
1123 1182 * version index. If not, then use 0.
1124 1183 */
1125 1184 if (state->versym) {
1126 1185 Versym test_verndx;
1127 1186
1128 1187 verndx = test_verndx = state->versym->data[symndx];
1129 1188 gnuver = state->versym->gnu_full;
1130 1189
1131 1190 /*
1132 1191 * Check to see if this is a defined symbol with a
1133 1192 * version index that is outside the valid range for
1134 1193 * the file. The interpretation of this depends on
1135 1194 * the style of versioning used by the object.
1136 1195 *
1137 1196 * Versions >= VER_NDX_LORESERVE have special meanings,
1138 1197 * and are exempt from this checking.
1139 1198 *
1140 1199 * GNU style version indexes use the top bit of the
1141 1200 * 16-bit index value (0x8000) as the "hidden bit".
1142 1201 * We must mask off this bit in order to compare
1143 1202 * the version against the maximum value.
1144 1203 */
1145 1204 if (gnuver)
1146 1205 test_verndx &= ~0x8000;
1147 1206
1148 1207 if ((test_verndx > state->versym->max_verndx) &&
1149 1208 (verndx < VER_NDX_LORESERVE))
1150 1209 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADVER),
1151 1210 state->file, state->secname, EC_WORD(symndx),
1152 1211 EC_HALF(test_verndx), state->versym->max_verndx);
1153 1212 } else {
1154 1213 verndx = 0;
1155 1214 gnuver = 0;
1156 1215 }
1157 1216
1158 1217 /*
1159 1218 * Error checking for TLS.
1160 1219 */
1161 1220 type = ELF_ST_TYPE(sym->st_info);
1162 1221 if (type == STT_TLS) {
1163 1222 if (tshdr &&
1164 1223 (sym->st_shndx != SHN_UNDEF) &&
1165 1224 ((tshdr->sh_flags & SHF_TLS) == 0)) {
1166 1225 (void) fprintf(stderr,
1167 1226 MSG_INTL(MSG_ERR_BADSYM3), state->file,
1168 1227 state->secname, EC_WORD(symndx),
1169 1228 demangle(symname, state->flags));
1170 1229 }
1171 1230 } else if ((type != STT_SECTION) && sym->st_size &&
1172 1231 tshdr && (tshdr->sh_flags & SHF_TLS)) {
1173 1232 (void) fprintf(stderr,
1174 1233 MSG_INTL(MSG_ERR_BADSYM4), state->file,
1175 1234 state->secname, EC_WORD(symndx),
1176 1235 demangle(symname, state->flags));
1177 1236 }
1178 1237
1179 1238 /*
1180 1239 * If a symbol with non-zero size has a type that
1181 1240 * specifies an address, then make sure the location
1182 1241 * it references is actually contained within the
1183 1242 * section. UNDEF symbols don't count in this case,
1184 1243 * so we ignore them.
1185 1244 *
1186 1245 * The meaning of the st_value field in a symbol
1187 1246 * depends on the type of object. For a relocatable
1188 1247 * object, it is the offset within the section.
1189 1248 * For sharable objects, it is the offset relative to
1190 1249 * the base of the object, and for other types, it is
1191 1250 * the virtual address. To get an offset within the
1192 1251 * section for non-ET_REL files, we subtract the
1193 1252 * base address of the section.
1194 1253 */
1195 1254 if (addr_symtype[type] && (sym->st_size > 0) &&
1196 1255 (sym->st_shndx != SHN_UNDEF) && ((sym->st_shndx < SHN_LORESERVE) ||
1197 1256 (sym->st_shndx == SHN_XINDEX)) && (tshdr != NULL)) {
1198 1257 Word v = sym->st_value;
1199 1258 if (state->ehdr->e_type != ET_REL)
1200 1259 v -= tshdr->sh_addr;
1201 1260 if (((v + sym->st_size) > tshdr->sh_size)) {
1202 1261 (void) fprintf(stderr,
1203 1262 MSG_INTL(MSG_ERR_BADSYM6), state->file,
1204 1263 state->secname, EC_WORD(symndx),
1205 1264 demangle(symname, state->flags),
1206 1265 EC_WORD(shndx), EC_XWORD(tshdr->sh_size),
1207 1266 EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1208 1267 }
1209 1268 }
1210 1269
1211 1270 /*
1212 1271 * A typical symbol table uses the sh_info field to indicate one greater
1213 1272 * than the symbol table index of the last local symbol, STB_LOCAL.
1214 1273 * Therefore, symbol indexes less than sh_info should have local
1215 1274 * binding. Symbol indexes greater than, or equal to sh_info, should
1216 1275 * have global binding. Note, we exclude UNDEF/NOTY symbols with zero
1217 1276 * value and size, as these symbols may be the result of an mcs(1)
1218 1277 * section deletion.
1219 1278 */
1220 1279 if (info) {
1221 1280 uchar_t bind = ELF_ST_BIND(sym->st_info);
1222 1281
1223 1282 if ((symndx < info) && (bind != STB_LOCAL)) {
1224 1283 (void) fprintf(stderr,
1225 1284 MSG_INTL(MSG_ERR_BADSYM7), state->file,
1226 1285 state->secname, EC_WORD(symndx),
1227 1286 demangle(symname, state->flags), EC_XWORD(info));
1228 1287
1229 1288 } else if ((symndx >= info) && (bind == STB_LOCAL) &&
1230 1289 ((sym->st_shndx != SHN_UNDEF) ||
1231 1290 (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) ||
1232 1291 (sym->st_size != 0) || (sym->st_value != 0))) {
1233 1292 (void) fprintf(stderr,
1234 1293 MSG_INTL(MSG_ERR_BADSYM8), state->file,
1235 1294 state->secname, EC_WORD(symndx),
1236 1295 demangle(symname, state->flags), EC_XWORD(info));
1237 1296 }
1238 1297 }
1239 1298
1240 1299 (void) snprintf(index, MAXNDXSIZE,
1241 1300 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(disp_symndx));
1242 1301 Elf_syms_table_entry(0, ELF_DBG_ELFDUMP, index, state->osabi,
1243 1302 state->ehdr->e_machine, sym, verndx, gnuver, sec, symname);
1244 1303 }
1245 1304
1246 1305 /*
1247 1306 * Process a SHT_SUNW_cap capabilities section.
1248 1307 */
1249 1308 static int
1250 1309 cap_section(const char *file, Cache *cache, Word shnum, Cache *ccache,
1251 1310 uchar_t osabi, Ehdr *ehdr, uint_t flags)
1252 1311 {
1253 1312 SYMTBL_STATE state;
1254 1313 Word cnum, capnum, nulls, symcaps;
1255 1314 int descapndx, objcap, title;
1256 1315 Cap *cap = (Cap *)ccache->c_data->d_buf;
1257 1316 Shdr *cishdr, *cshdr = ccache->c_shdr;
1258 1317 Cache *cicache, *strcache;
1259 1318 Capinfo *capinfo = NULL;
1260 1319 Word capinfonum;
1261 1320 const char *strs = NULL;
1262 1321 size_t strs_size;
1263 1322
1264 1323 if ((cshdr->sh_entsize == 0) || (cshdr->sh_size == 0)) {
1265 1324 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1266 1325 file, ccache->c_name);
1267 1326 return (0);
1268 1327 }
1269 1328
1270 1329 /*
1271 1330 * If this capabilities section is associated with symbols, then the
1272 1331 * sh_link field points to the associated capabilities information
1273 1332 * section. The sh_link field of the capabilities information section
1274 1333 * points to the associated symbol table.
1275 1334 */
1276 1335 if (cshdr->sh_link) {
1277 1336 Cache *scache;
1278 1337 Shdr *sshdr;
1279 1338
1280 1339 /*
1281 1340 * Validate that the sh_link field points to a capabilities
1282 1341 * information section.
1283 1342 */
1284 1343 if (cshdr->sh_link >= shnum) {
1285 1344 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1286 1345 file, ccache->c_name, EC_WORD(cshdr->sh_link));
1287 1346 return (0);
1288 1347 }
1289 1348
1290 1349 cicache = &cache[cshdr->sh_link];
1291 1350 cishdr = cicache->c_shdr;
1292 1351
1293 1352 if (cishdr->sh_type != SHT_SUNW_capinfo) {
1294 1353 (void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
1295 1354 file, ccache->c_name, EC_WORD(cshdr->sh_link));
1296 1355 return (0);
1297 1356 }
1298 1357
1299 1358 capinfo = cicache->c_data->d_buf;
1300 1359 capinfonum = (Word)(cishdr->sh_size / cishdr->sh_entsize);
1301 1360
1302 1361 /*
1303 1362 * Validate that the sh_link field of the capabilities
1304 1363 * information section points to a valid symbol table.
1305 1364 */
1306 1365 if ((cishdr->sh_link == 0) || (cishdr->sh_link >= shnum)) {
1307 1366 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1308 1367 file, cicache->c_name, EC_WORD(cishdr->sh_link));
1309 1368 return (0);
1310 1369 }
1311 1370 scache = &cache[cishdr->sh_link];
1312 1371 sshdr = scache->c_shdr;
1313 1372
1314 1373 if ((sshdr->sh_type != SHT_SYMTAB) &&
1315 1374 (sshdr->sh_type != SHT_DYNSYM)) {
1316 1375 (void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO1),
1317 1376 file, cicache->c_name, EC_WORD(cishdr->sh_link));
1318 1377 return (0);
1319 1378 }
1320 1379
1321 1380 if (!init_symtbl_state(&state, cache, shnum,
1322 1381 cishdr->sh_link, ehdr, osabi, NULL, file, flags))
1323 1382 return (0);
1324 1383 }
1325 1384
1326 1385 /*
1327 1386 * If this capabilities section contains capability string entries,
1328 1387 * then determine the associated string table. Capabilities entries
1329 1388 * that define names require that the capability section indicate
1330 1389 * which string table to use via sh_info.
1331 1390 */
1332 1391 if (cshdr->sh_info) {
1333 1392 Shdr *strshdr;
1334 1393
1335 1394 /*
1336 1395 * Validate that the sh_info field points to a string table.
1337 1396 */
1338 1397 if (cshdr->sh_info >= shnum) {
1339 1398 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1340 1399 file, ccache->c_name, EC_WORD(cshdr->sh_info));
1341 1400 return (0);
1342 1401 }
1343 1402
1344 1403 strcache = &cache[cshdr->sh_info];
1345 1404 strshdr = strcache->c_shdr;
1346 1405
1347 1406 if (strshdr->sh_type != SHT_STRTAB) {
1348 1407 (void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAP),
1349 1408 file, ccache->c_name, EC_WORD(cshdr->sh_info));
1350 1409 return (0);
1351 1410 }
1352 1411 strs = (const char *)strcache->c_data->d_buf;
1353 1412 strs_size = strcache->c_data->d_size;
1354 1413 }
1355 1414
1356 1415 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1357 1416 dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAP), ccache->c_name);
1358 1417
1359 1418 capnum = (Word)(cshdr->sh_size / cshdr->sh_entsize);
1360 1419
1361 1420 nulls = symcaps = 0;
1362 1421 objcap = title = 1;
1363 1422 descapndx = -1;
1364 1423
1365 1424 /*
1366 1425 * Traverse the capabilities section printing each capability group.
1367 1426 * The first capabilities group defines any object capabilities. Any
1368 1427 * following groups define symbol capabilities. In the case where no
1369 1428 * object capabilities exist, but symbol capabilities do, a single
1370 1429 * CA_SUNW_NULL terminator for the object capabilities exists.
1371 1430 */
1372 1431 for (cnum = 0; cnum < capnum; cap++, cnum++) {
1373 1432 if (cap->c_tag == CA_SUNW_NULL) {
1374 1433 /*
1375 1434 * A CA_SUNW_NULL tag terminates a capabilities group.
1376 1435 * If the first capabilities tag is CA_SUNW_NULL, then
1377 1436 * no object capabilities exist.
1378 1437 */
1379 1438 if ((nulls++ == 0) && (cnum == 0))
1380 1439 objcap = 0;
1381 1440 title = 1;
1382 1441 } else {
1383 1442 if (title) {
1384 1443 if (nulls == 0) {
1385 1444 /*
1386 1445 * If this capabilities group represents
1387 1446 * the object capabilities (i.e., no
1388 1447 * CA_SUNW_NULL tag has been processed
1389 1448 * yet), then display an object
1390 1449 * capabilities title.
1391 1450 */
1392 1451 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1393 1452 dbg_print(0,
1394 1453 MSG_INTL(MSG_OBJ_CAP_TITLE));
1395 1454 } else {
1396 1455 /*
1397 1456 * If this is a symbols capabilities
1398 1457 * group (i.e., a CA_SUNW_NULL tag has
1399 1458 * already be found that terminates
1400 1459 * the object capabilities group), then
1401 1460 * display a symbol capabilities title,
1402 1461 * and retain this capabilities index
1403 1462 * for later processing.
1404 1463 */
1405 1464 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1406 1465 dbg_print(0,
1407 1466 MSG_INTL(MSG_SYM_CAP_TITLE));
1408 1467 descapndx = cnum;
1409 1468 }
1410 1469 Elf_cap_title(0);
1411 1470 title = 0;
1412 1471 }
1413 1472
1414 1473 /*
1415 1474 * Print the capabilities data.
1416 1475 *
1417 1476 * Note that CA_SUNW_PLAT, CA_SUNW_MACH and CA_SUNW_ID
1418 1477 * entries require a string table, which should have
1419 1478 * already been established.
1420 1479 */
1421 1480 if ((strs == NULL) && ((cap->c_tag == CA_SUNW_PLAT) ||
1422 1481 (cap->c_tag == CA_SUNW_MACH) ||
1423 1482 (cap->c_tag == CA_SUNW_ID))) {
1424 1483 (void) fprintf(stderr,
1425 1484 MSG_INTL(MSG_WARN_INVCAP4), file,
1426 1485 EC_WORD(elf_ndxscn(ccache->c_scn)),
1427 1486 ccache->c_name, EC_WORD(cshdr->sh_info));
1428 1487 }
1429 1488 Elf_cap_entry(0, cap, cnum, strs, strs_size,
1430 1489 ehdr->e_machine);
1431 1490 }
1432 1491
1433 1492 /*
1434 1493 * If this CA_SUNW_NULL tag terminates a symbol capabilities
1435 1494 * group, determine the associated symbols.
1436 1495 */
1437 1496 if ((cap->c_tag == CA_SUNW_NULL) && (nulls > 1) &&
1438 1497 (descapndx != -1)) {
1439 1498 Capinfo *cip;
1440 1499 Word inum;
1441 1500
1442 1501 symcaps++;
1443 1502
1444 1503 /*
1445 1504 * Make sure we've discovered a SHT_SUNW_capinfo table.
1446 1505 */
1447 1506 if ((cip = capinfo) == NULL) {
1448 1507 (void) fprintf(stderr,
1449 1508 MSG_INTL(MSG_ERR_INVCAP), file,
1450 1509 ccache->c_name, EC_WORD(cshdr->sh_link));
1451 1510 return (0);
1452 1511 }
1453 1512
1454 1513 /*
1455 1514 * Determine what symbols reference this capabilities
1456 1515 * group.
1457 1516 */
1458 1517 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1459 1518 dbg_print(0, MSG_INTL(MSG_CAPINFO_ENTRIES));
1460 1519 Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
1461 1520
1462 1521 for (inum = 1, cip++; inum < capinfonum;
1463 1522 inum++, cip++) {
1464 1523 Word gndx = (Word)ELF_C_GROUP(*cip);
1465 1524
1466 1525 if (gndx && (gndx == descapndx)) {
1467 1526 output_symbol(&state, inum, 0,
1468 1527 inum, state.sym + inum);
1469 1528 }
1470 1529 }
1471 1530 descapndx = -1;
1472 1531 continue;
1473 1532 }
1474 1533
1475 1534 /*
1476 1535 * An SF1_SUNW_ADDR32 software capability tag in a 32-bit
1477 1536 * object is suspicious as it has no effect.
1478 1537 */
1479 1538 if ((cap->c_tag == CA_SUNW_SF_1) &&
1480 1539 (ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
1481 1540 (cap->c_un.c_val & SF1_SUNW_ADDR32)) {
1482 1541 (void) fprintf(stderr, MSG_INTL(MSG_WARN_INADDR32SF1),
1483 1542 file, ccache->c_name);
1484 1543 }
1485 1544 }
1486 1545
1487 1546 /*
1488 1547 * If this is a dynamic object, with symbol capabilities, then a
1489 1548 * .SUNW_capchain section should exist. This section contains a chain
1490 1549 * of symbol indexes for each capabilities family. This is the list
1491 1550 * that is searched by ld.so.1 to determine the best capabilities
1492 1551 * candidate.
1493 1552 *
1494 1553 * Note, more than one capabilities lead symbol can point to the same
1495 1554 * family chain. For example, a weak/global pair of symbols can both
1496 1555 * represent the same family of capabilities symbols. Therefore, to
1497 1556 * display all possible families we traverse the capabilities
1498 1557 * information section looking for CAPINFO_SUNW_GLOB lead symbols.
1499 1558 * From these we determine the associated capabilities chain to inspect.
1500 1559 */
1501 1560 if (symcaps &&
1502 1561 ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
1503 1562 Capinfo *cip;
1504 1563 Capchain *chain;
1505 1564 Cache *chcache;
1506 1565 Shdr *chshdr;
1507 1566 Word chainnum, inum;
1508 1567
1509 1568 /*
1510 1569 * Validate that the sh_info field of the capabilities
1511 1570 * information section points to a capabilities chain section.
1512 1571 */
1513 1572 if (cishdr->sh_info >= shnum) {
1514 1573 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
1515 1574 file, cicache->c_name, EC_WORD(cishdr->sh_info));
1516 1575 return (0);
1517 1576 }
1518 1577
1519 1578 chcache = &cache[cishdr->sh_info];
1520 1579 chshdr = chcache->c_shdr;
1521 1580
1522 1581 if (chshdr->sh_type != SHT_SUNW_capchain) {
1523 1582 (void) fprintf(stderr, MSG_INTL(MSG_ERR_INVCAPINFO2),
1524 1583 file, cicache->c_name, EC_WORD(cishdr->sh_info));
1525 1584 return (0);
1526 1585 }
1527 1586
1528 1587 chainnum = (Word)(chshdr->sh_size / chshdr->sh_entsize);
1529 1588 chain = (Capchain *)chcache->c_data->d_buf;
1530 1589
1531 1590 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1532 1591 dbg_print(0, MSG_INTL(MSG_ELF_SCN_CAPCHAIN), chcache->c_name);
1533 1592
1534 1593 /*
1535 1594 * Traverse the capabilities information section looking for
1536 1595 * CAPINFO_SUNW_GLOB lead capabilities symbols.
1537 1596 */
1538 1597 cip = capinfo;
1539 1598 for (inum = 1, cip++; inum < capinfonum; inum++, cip++) {
1540 1599 const char *name;
1541 1600 Sym *sym;
1542 1601 Word sndx, cndx;
1543 1602 Word gndx = (Word)ELF_C_GROUP(*cip);
1544 1603
1545 1604 if ((gndx == 0) || (gndx != CAPINFO_SUNW_GLOB))
1546 1605 continue;
1547 1606
1548 1607 /*
1549 1608 * Determine the symbol that is associated with this
1550 1609 * capability information entry, and use this to
1551 1610 * identify this capability family.
1552 1611 */
1553 1612 sym = (Sym *)(state.sym + inum);
1554 1613 name = string(cicache, inum, strcache, file,
1555 1614 sym->st_name);
1556 1615
1557 1616 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1558 1617 dbg_print(0, MSG_INTL(MSG_CAPCHAIN_TITLE), name);
1559 1618 dbg_print(0, MSG_INTL(MSG_CAPCHAIN_ENTRY));
1560 1619
1561 1620 cndx = (Word)ELF_C_SYM(*cip);
1562 1621
1563 1622 /*
1564 1623 * Traverse this families chain and identify each
1565 1624 * family member.
1566 1625 */
1567 1626 for (;;) {
1568 1627 char _chain[MAXNDXSIZE], _symndx[MAXNDXSIZE];
1569 1628
1570 1629 if (cndx >= chainnum) {
1571 1630 (void) fprintf(stderr,
1572 1631 MSG_INTL(MSG_ERR_INVCAPINFO3), file,
1573 1632 cicache->c_name, EC_WORD(inum),
1574 1633 EC_WORD(cndx));
1575 1634 break;
1576 1635 }
1577 1636 if ((sndx = chain[cndx]) == 0)
1578 1637 break;
1579 1638
1580 1639 /*
1581 1640 * Determine this entries symbol reference.
1582 1641 */
1583 1642 if (sndx > state.symn) {
1584 1643 (void) fprintf(stderr,
1585 1644 MSG_INTL(MSG_ERR_CHBADSYMNDX), file,
1586 1645 EC_WORD(sndx), chcache->c_name,
1587 1646 EC_WORD(cndx));
1588 1647 name = MSG_INTL(MSG_STR_UNKNOWN);
1589 1648 } else {
1590 1649 sym = (Sym *)(state.sym + sndx);
1591 1650 name = string(chcache, sndx,
1592 1651 strcache, file, sym->st_name);
1593 1652 }
1594 1653
1595 1654 /*
1596 1655 * Display the family member.
1597 1656 */
1598 1657 (void) snprintf(_chain, MAXNDXSIZE,
1599 1658 MSG_ORIG(MSG_FMT_INTEGER), cndx);
1600 1659 (void) snprintf(_symndx, MAXNDXSIZE,
1601 1660 MSG_ORIG(MSG_FMT_INDEX2), EC_WORD(sndx));
1602 1661 dbg_print(0, MSG_ORIG(MSG_FMT_CHAIN_INFO),
1603 1662 _chain, _symndx, demangle(name, flags));
1604 1663
1605 1664 cndx++;
1606 1665 }
1607 1666 }
1608 1667 }
1609 1668 return (objcap);
1610 1669 }
1611 1670
1612 1671 /*
1613 1672 * Print the capabilities.
1614 1673 *
1615 1674 * A .SUNW_cap section can contain one or more, CA_SUNW_NULL terminated,
1616 1675 * capabilities groups. The first group defines the object capabilities.
1617 1676 * This group defines the minimum capability requirements of the entire
1618 1677 * object file. If this is a dynamic object, this group should be associated
1619 1678 * with a PT_SUNWCAP program header.
1620 1679 *
1621 1680 * Additional capabilities groups define the association of individual symbols
1622 1681 * to specific capabilities.
1623 1682 */
1624 1683 static void
1625 1684 cap(const char *file, Cache *cache, Word shnum, Word phnum, Ehdr *ehdr,
1626 1685 uchar_t osabi, Elf *elf, uint_t flags)
1627 1686 {
1628 1687 Word cnt;
1629 1688 Shdr *cshdr = NULL;
1630 1689 Cache *ccache;
1631 1690 Off cphdr_off = 0;
1632 1691 Xword cphdr_sz;
1633 1692
1634 1693 /*
1635 1694 * Determine if a global capabilities header exists.
1636 1695 */
1637 1696 if (phnum) {
1638 1697 Phdr *phdr;
1639 1698
1640 1699 if ((phdr = elf_getphdr(elf)) == NULL) {
1641 1700 failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
1642 1701 return;
1643 1702 }
1644 1703
1645 1704 for (cnt = 0; cnt < phnum; phdr++, cnt++) {
1646 1705 if (phdr->p_type == PT_SUNWCAP) {
1647 1706 cphdr_off = phdr->p_offset;
1648 1707 cphdr_sz = phdr->p_filesz;
1649 1708 break;
1650 1709 }
1651 1710 }
1652 1711 }
1653 1712
1654 1713 /*
1655 1714 * Determine if a capabilities section exists.
1656 1715 */
1657 1716 for (cnt = 1; cnt < shnum; cnt++) {
1658 1717 Cache *_cache = &cache[cnt];
1659 1718 Shdr *shdr = _cache->c_shdr;
1660 1719
1661 1720 /*
1662 1721 * Process any capabilities information.
1663 1722 */
1664 1723 if (shdr->sh_type == SHT_SUNW_cap) {
1665 1724 if (cap_section(file, cache, shnum, _cache, osabi,
1666 1725 ehdr, flags)) {
1667 1726 /*
1668 1727 * If this section defined an object capability
1669 1728 * group, retain the section information for
1670 1729 * program header validation.
1671 1730 */
1672 1731 ccache = _cache;
1673 1732 cshdr = shdr;
1674 1733 }
1675 1734 continue;
1676 1735 }
1677 1736 }
1678 1737
1679 1738 if ((cshdr == NULL) && (cphdr_off == 0))
1680 1739 return;
1681 1740
1682 1741 if (cphdr_off && (cshdr == NULL))
1683 1742 (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP1), file);
1684 1743
1685 1744 /*
1686 1745 * If this object is an executable or shared object, and it provided
1687 1746 * an object capabilities group, then the group should have an
1688 1747 * accompanying PT_SUNWCAP program header.
1689 1748 */
1690 1749 if (cshdr && ((ehdr->e_type == ET_EXEC) || (ehdr->e_type == ET_DYN))) {
1691 1750 if (cphdr_off == 0) {
1692 1751 (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP2),
1693 1752 file, EC_WORD(elf_ndxscn(ccache->c_scn)),
1694 1753 ccache->c_name);
1695 1754 } else if ((cphdr_off != cshdr->sh_offset) ||
1696 1755 (cphdr_sz != cshdr->sh_size)) {
1697 1756 (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVCAP3),
1698 1757 file, EC_WORD(elf_ndxscn(ccache->c_scn)),
1699 1758 ccache->c_name);
1700 1759 }
1701 1760 }
1702 1761 }
1703 1762
1704 1763 /*
1705 1764 * Print the interpretor.
1706 1765 */
1707 1766 static void
1708 1767 interp(const char *file, Cache *cache, Word shnum, Word phnum, Elf *elf)
1709 1768 {
1710 1769 static Word phdr_types[] = { PT_INTERP };
1711 1770
1712 1771
1713 1772 Word cnt;
1714 1773 Shdr *ishdr = NULL;
1715 1774 Cache *icache;
1716 1775 Off iphdr_off = 0;
1717 1776 Xword iphdr_fsz;
1718 1777
1719 1778 /*
1720 1779 * Determine if an interp header exists.
1721 1780 */
1722 1781 if (phnum) {
1723 1782 Phdr *phdr;
1724 1783
1725 1784 phdr = getphdr(phnum, phdr_types,
1726 1785 sizeof (phdr_types) / sizeof (*phdr_types), file, elf);
1727 1786 if (phdr != NULL) {
1728 1787 iphdr_off = phdr->p_offset;
1729 1788 iphdr_fsz = phdr->p_filesz;
1730 1789 }
1731 1790 }
1732 1791
1733 1792 if (iphdr_off == 0)
1734 1793 return;
1735 1794
1736 1795 /*
1737 1796 * Determine if an interp section exists.
1738 1797 */
1739 1798 for (cnt = 1; cnt < shnum; cnt++) {
1740 1799 Cache *_cache = &cache[cnt];
1741 1800 Shdr *shdr = _cache->c_shdr;
1742 1801
1743 1802 /*
1744 1803 * Scan sections to find a section which contains the PT_INTERP
1745 1804 * string. The target section can't be in a NOBITS section.
1746 1805 */
1747 1806 if ((shdr->sh_type == SHT_NOBITS) ||
1748 1807 (iphdr_off < shdr->sh_offset) ||
1749 1808 (iphdr_off + iphdr_fsz) > (shdr->sh_offset + shdr->sh_size))
1750 1809 continue;
1751 1810
1752 1811 icache = _cache;
1753 1812 ishdr = shdr;
1754 1813 break;
1755 1814 }
1756 1815
1757 1816 /*
1758 1817 * Print the interpreter string based on the offset defined in the
1759 1818 * program header, as this is the offset used by the kernel.
1760 1819 */
1761 1820 if (ishdr && icache->c_data) {
1762 1821 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1763 1822 dbg_print(0, MSG_INTL(MSG_ELF_SCN_INTERP), icache->c_name);
1764 1823 dbg_print(0, MSG_ORIG(MSG_FMT_INDENT),
1765 1824 (char *)icache->c_data->d_buf +
1766 1825 (iphdr_off - ishdr->sh_offset));
1767 1826 } else
1768 1827 (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP1), file);
1769 1828
1770 1829 /*
1771 1830 * If there are any inconsistences between the program header and
1772 1831 * section information, flag them.
1773 1832 */
1774 1833 if (ishdr && ((iphdr_off != ishdr->sh_offset) ||
1775 1834 (iphdr_fsz != ishdr->sh_size))) {
1776 1835 (void) fprintf(stderr, MSG_INTL(MSG_WARN_INVINTERP2), file,
1777 1836 icache->c_name);
1778 1837 }
1779 1838 }
1780 1839
1781 1840 /*
1782 1841 * Print the syminfo section.
1783 1842 */
1784 1843 static void
1785 1844 syminfo(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
1786 1845 {
1787 1846 Shdr *infoshdr;
1788 1847 Syminfo *info;
1789 1848 Sym *syms;
1790 1849 Dyn *dyns;
1791 1850 Word infonum, cnt, ndx, symnum, dynnum;
1792 1851 Cache *infocache = NULL, *dyncache = NULL, *symsec, *strsec;
1793 1852 Boolean *dynerr;
1794 1853
1795 1854 for (cnt = 1; cnt < shnum; cnt++) {
1796 1855 if (cache[cnt].c_shdr->sh_type == SHT_SUNW_syminfo) {
1797 1856 infocache = &cache[cnt];
1798 1857 break;
1799 1858 }
1800 1859 }
1801 1860 if (infocache == NULL)
1802 1861 return;
1803 1862
1804 1863 infoshdr = infocache->c_shdr;
1805 1864 if ((infoshdr->sh_entsize == 0) || (infoshdr->sh_size == 0)) {
1806 1865 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1807 1866 file, infocache->c_name);
1808 1867 return;
1809 1868 }
1810 1869 if (infocache->c_data == NULL)
1811 1870 return;
1812 1871
1813 1872 infonum = (Word)(infoshdr->sh_size / infoshdr->sh_entsize);
1814 1873 info = (Syminfo *)infocache->c_data->d_buf;
1815 1874
1816 1875 /*
1817 1876 * If there is no associated dynamic section, determine if one
1818 1877 * is needed, and if so issue a warning. If there is an
1819 1878 * associated dynamic section, validate it and get the data buffer
1820 1879 * for it.
1821 1880 */
1822 1881 dyns = NULL;
1823 1882 dynnum = 0;
1824 1883 if (infoshdr->sh_info == 0) {
1825 1884 Syminfo *_info = info + 1;
1826 1885
1827 1886 for (ndx = 1; ndx < infonum; ndx++, _info++) {
1828 1887 if ((_info->si_flags == 0) && (_info->si_boundto == 0))
1829 1888 continue;
1830 1889
1831 1890 if (_info->si_boundto < SYMINFO_BT_LOWRESERVE)
1832 1891 (void) fprintf(stderr,
1833 1892 MSG_INTL(MSG_ERR_BADSHINFO), file,
1834 1893 infocache->c_name,
1835 1894 EC_WORD(infoshdr->sh_info));
1836 1895 }
1837 1896 } else if ((infoshdr->sh_info >= shnum) ||
1838 1897 (cache[infoshdr->sh_info].c_shdr->sh_type != SHT_DYNAMIC)) {
1839 1898 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
1840 1899 file, infocache->c_name, EC_WORD(infoshdr->sh_info));
1841 1900 } else {
1842 1901 dyncache = &cache[infoshdr->sh_info];
1843 1902 if ((dyncache->c_data == NULL) ||
1844 1903 ((dyns = dyncache->c_data->d_buf) == NULL)) {
1845 1904 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
1846 1905 file, dyncache->c_name);
1847 1906 }
1848 1907 if (dyns != NULL) {
1849 1908 dynnum = dyncache->c_shdr->sh_size /
1850 1909 dyncache->c_shdr->sh_entsize;
1851 1910
1852 1911 /*
1853 1912 * We validate the type of dynamic elements referenced
1854 1913 * from the syminfo. This array is used report any
1855 1914 * bad dynamic entries.
1856 1915 */
1857 1916 if ((dynerr = calloc(dynnum, sizeof (*dynerr))) ==
1858 1917 NULL) {
1859 1918 int err = errno;
1860 1919 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
1861 1920 file, strerror(err));
1862 1921 return;
1863 1922 }
1864 1923 }
1865 1924 }
1866 1925
1867 1926 /*
1868 1927 * Get the data buffer for the associated symbol table and string table.
1869 1928 */
1870 1929 if (stringtbl(cache, 1, cnt, shnum, file,
1871 1930 &symnum, &symsec, &strsec) == 0)
1872 1931 return;
1873 1932
1874 1933 syms = symsec->c_data->d_buf;
1875 1934
1876 1935 /*
1877 1936 * Loop through the syminfo entries.
1878 1937 */
1879 1938 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
1880 1939 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMINFO), infocache->c_name);
1881 1940 Elf_syminfo_title(0);
1882 1941
1883 1942 for (ndx = 1, info++; ndx < infonum; ndx++, info++) {
1884 1943 Sym *sym;
1885 1944 const char *needed, *name;
1886 1945 Word expect_dt;
1887 1946 Word boundto = info->si_boundto;
1888 1947
1889 1948 if ((info->si_flags == 0) && (boundto == 0))
1890 1949 continue;
1891 1950
1892 1951 sym = &syms[ndx];
1893 1952 name = string(infocache, ndx, strsec, file, sym->st_name);
1894 1953
1895 1954 /* Is si_boundto set to one of the reserved values? */
1896 1955 if (boundto >= SYMINFO_BT_LOWRESERVE) {
1897 1956 Elf_syminfo_entry(0, ndx, info, name, NULL);
1898 1957 continue;
1899 1958 }
1900 1959
1901 1960 /*
1902 1961 * si_boundto is referencing a dynamic section. If we don't
1903 1962 * have one, an error was already issued above, so it suffices
1904 1963 * to display an empty string. If we are out of bounds, then
1905 1964 * report that and then display an empty string.
1906 1965 */
1907 1966 if ((dyns == NULL) || (boundto >= dynnum)) {
1908 1967 if (dyns != NULL)
1909 1968 (void) fprintf(stderr,
1910 1969 MSG_INTL(MSG_ERR_BADSIDYNNDX), file,
1911 1970 infocache->c_ndx, infocache->c_name,
1912 1971 EC_WORD(ndx), EC_WORD(dynnum - 1),
1913 1972 EC_WORD(boundto));
1914 1973 Elf_syminfo_entry(0, ndx, info, name,
1915 1974 MSG_ORIG(MSG_STR_EMPTY));
1916 1975 continue;
1917 1976 }
1918 1977
1919 1978 /*
1920 1979 * The si_boundto reference expects a specific dynamic element
1921 1980 * type at the given index. The dynamic element is always a
1922 1981 * string that gives an object name. The specific type depends
1923 1982 * on the si_flags present. Ensure that we've got the right
1924 1983 * type.
1925 1984 */
1926 1985 if (info->si_flags & SYMINFO_FLG_FILTER)
1927 1986 expect_dt = DT_SUNW_FILTER;
1928 1987 else if (info->si_flags & SYMINFO_FLG_AUXILIARY)
1929 1988 expect_dt = DT_SUNW_AUXILIARY;
1930 1989 else if (info->si_flags & (SYMINFO_FLG_DIRECT |
1931 1990 SYMINFO_FLG_LAZYLOAD | SYMINFO_FLG_DIRECTBIND))
1932 1991 expect_dt = DT_NEEDED;
1933 1992 else
1934 1993 expect_dt = DT_NULL; /* means we ignore the type */
1935 1994
1936 1995 if ((dyns[boundto].d_tag != expect_dt) &&
1937 1996 (expect_dt != DT_NULL)) {
1938 1997 Conv_inv_buf_t buf1, buf2;
1939 1998
1940 1999 /* Only complain about each dynamic element once */
1941 2000 if (!dynerr[boundto]) {
1942 2001 (void) fprintf(stderr,
1943 2002 MSG_INTL(MSG_ERR_BADSIDYNTAG),
1944 2003 file, infocache->c_ndx, infocache->c_name,
1945 2004 EC_WORD(ndx), dyncache->c_ndx,
1946 2005 dyncache->c_name, EC_WORD(boundto),
1947 2006 conv_dyn_tag(expect_dt, osabi,
1948 2007 ehdr->e_machine, CONV_FMT_ALT_CF, &buf1),
1949 2008 conv_dyn_tag(dyns[boundto].d_tag, osabi,
1950 2009 ehdr->e_machine, CONV_FMT_ALT_CF, &buf2));
1951 2010 dynerr[boundto] = TRUE;
1952 2011 }
1953 2012 }
1954 2013
1955 2014 /*
1956 2015 * Whether or not the DT item we're pointing at is
1957 2016 * of the right type, if it's a type we recognize as
1958 2017 * providing a string, go ahead and show it. Otherwise
1959 2018 * an empty string.
1960 2019 */
1961 2020 switch (dyns[boundto].d_tag) {
1962 2021 case DT_NEEDED:
1963 2022 case DT_SONAME:
1964 2023 case DT_RPATH:
1965 2024 case DT_RUNPATH:
1966 2025 case DT_CONFIG:
1967 2026 case DT_DEPAUDIT:
1968 2027 case DT_USED:
1969 2028 case DT_AUDIT:
1970 2029 case DT_SUNW_AUXILIARY:
1971 2030 case DT_SUNW_FILTER:
1972 2031 case DT_FILTER:
1973 2032 case DT_AUXILIARY:
1974 2033 needed = string(infocache, boundto,
1975 2034 strsec, file, dyns[boundto].d_un.d_val);
1976 2035 break;
1977 2036 default:
1978 2037 needed = MSG_ORIG(MSG_STR_EMPTY);
1979 2038 }
1980 2039 Elf_syminfo_entry(0, ndx, info, name, needed);
1981 2040 }
1982 2041 if (dyns != NULL)
1983 2042 free(dynerr);
1984 2043 }
1985 2044
1986 2045 /*
1987 2046 * Print version definition section entries.
1988 2047 */
1989 2048 static void
1990 2049 version_def(Verdef *vdf, Word vdf_num, Cache *vcache, Cache *scache,
1991 2050 const char *file)
1992 2051 {
1993 2052 Word cnt;
1994 2053 char index[MAXNDXSIZE];
1995 2054
1996 2055 Elf_ver_def_title(0);
1997 2056
1998 2057 for (cnt = 1; cnt <= vdf_num; cnt++,
1999 2058 vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
2000 2059 Conv_ver_flags_buf_t ver_flags_buf;
2001 2060 const char *name, *dep;
2002 2061 Half vcnt = vdf->vd_cnt - 1;
2003 2062 Half ndx = vdf->vd_ndx;
2004 2063 Verdaux *vdap = (Verdaux *)((uintptr_t)vdf + vdf->vd_aux);
2005 2064
2006 2065 /*
2007 2066 * Obtain the name and first dependency (if any).
2008 2067 */
2009 2068 name = string(vcache, cnt, scache, file, vdap->vda_name);
2010 2069 vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
2011 2070 if (vcnt)
2012 2071 dep = string(vcache, cnt, scache, file, vdap->vda_name);
2013 2072 else
2014 2073 dep = MSG_ORIG(MSG_STR_EMPTY);
2015 2074
2016 2075 (void) snprintf(index, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX),
2017 2076 EC_XWORD(ndx));
2018 2077 Elf_ver_line_1(0, index, name, dep,
2019 2078 conv_ver_flags(vdf->vd_flags, 0, &ver_flags_buf));
2020 2079
2021 2080 /*
2022 2081 * Print any additional dependencies.
2023 2082 */
2024 2083 if (vcnt) {
2025 2084 vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
2026 2085 for (vcnt--; vcnt; vcnt--,
2027 2086 vdap = (Verdaux *)((uintptr_t)vdap +
2028 2087 vdap->vda_next)) {
2029 2088 dep = string(vcache, cnt, scache, file,
2030 2089 vdap->vda_name);
2031 2090 Elf_ver_line_2(0, MSG_ORIG(MSG_STR_EMPTY), dep);
2032 2091 }
2033 2092 }
2034 2093 }
2035 2094 }
2036 2095
2037 2096 /*
2038 2097 * Print version needed section entries.
2039 2098 *
2040 2099 * entry:
2041 2100 * vnd - Address of verneed data
2042 2101 * vnd_num - # of Verneed entries
2043 2102 * vcache - Cache of verneed section being processed
2044 2103 * scache - Cache of associated string table section
2045 2104 * file - Name of object being processed.
2046 2105 * versym - Information about versym section
2047 2106 *
2048 2107 * exit:
2049 2108 * The versions have been printed. If GNU style versioning
2050 2109 * is in effect, versym->max_verndx has been updated to
2051 2110 * contain the largest version index seen.
2052 2111 *
2053 2112 * note:
2054 2113 * The versym section of an object that follows the original
2055 2114 * Solaris versioning rules only contains indexes into the verdef
2056 2115 * section. Symbols defined in other objects (UNDEF) are given
2057 2116 * a version of 0, indicating that they are not defined by
2058 2117 * this file, and the Verneed entries do not have associated version
2059 2118 * indexes. For these reasons, we do not display a version index
2060 2119 * for original-style Verneed sections.
2061 2120 *
2062 2121 * The GNU versioning extensions alter this: Symbols defined in other
2063 2122 * objects receive a version index in the range above those defined
2064 2123 * by the Verdef section, and the vna_other field of the Vernaux
2065 2124 * structs inside the Verneed section contain the version index for
2066 2125 * that item. We therefore display the index when showing the
2067 2126 * contents of a GNU style Verneed section. You should not
2068 2127 * necessarily expect these indexes to appear in sorted
2069 2128 * order --- it seems that the GNU ld assigns the versions as
2070 2129 * symbols are encountered during linking, and then the results
2071 2130 * are assembled into the Verneed section afterwards.
2072 2131 */
2073 2132 static void
2074 2133 version_need(Verneed *vnd, Word vnd_num, Cache *vcache, Cache *scache,
2075 2134 const char *file, VERSYM_STATE *versym)
2076 2135 {
2077 2136 Word cnt;
2078 2137 char index[MAXNDXSIZE];
2079 2138 const char *index_str;
2080 2139
2081 2140 Elf_ver_need_title(0, versym->gnu_needed);
2082 2141
2083 2142 for (cnt = 1; cnt <= vnd_num; cnt++,
2084 2143 vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2085 2144 Conv_ver_flags_buf_t ver_flags_buf;
2086 2145 const char *name, *dep;
2087 2146 Half vcnt = vnd->vn_cnt;
2088 2147 Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
2089 2148
2090 2149 /*
2091 2150 * Obtain the name of the needed file and the version name
2092 2151 * within it that we're dependent on. Note that the count
2093 2152 * should be at least one, otherwise this is a pretty bogus
2094 2153 * entry.
2095 2154 */
2096 2155 name = string(vcache, cnt, scache, file, vnd->vn_file);
2097 2156 if (vcnt)
2098 2157 dep = string(vcache, cnt, scache, file, vnap->vna_name);
2099 2158 else
2100 2159 dep = MSG_INTL(MSG_STR_NULL);
2101 2160
2102 2161 if (vnap->vna_other == 0) { /* Traditional form */
2103 2162 index_str = MSG_ORIG(MSG_STR_EMPTY);
2104 2163 } else { /* GNU form */
2105 2164 index_str = index;
2106 2165 /* Format the version index value */
2107 2166 (void) snprintf(index, MAXNDXSIZE,
2108 2167 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(vnap->vna_other));
2109 2168 if (vnap->vna_other > versym->max_verndx)
2110 2169 versym->max_verndx = vnap->vna_other;
2111 2170 }
2112 2171 Elf_ver_line_1(0, index_str, name, dep,
2113 2172 conv_ver_flags(vnap->vna_flags, 0, &ver_flags_buf));
2114 2173
2115 2174 /*
2116 2175 * Print any additional version dependencies.
2117 2176 */
2118 2177 if (vcnt) {
2119 2178 vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
2120 2179 for (vcnt--; vcnt; vcnt--,
2121 2180 vnap = (Vernaux *)((uintptr_t)vnap +
2122 2181 vnap->vna_next)) {
2123 2182 dep = string(vcache, cnt, scache, file,
2124 2183 vnap->vna_name);
2125 2184 if (vnap->vna_other > 0) {
2126 2185 /* Format the next index value */
2127 2186 (void) snprintf(index, MAXNDXSIZE,
2128 2187 MSG_ORIG(MSG_FMT_INDEX),
2129 2188 EC_XWORD(vnap->vna_other));
2130 2189 Elf_ver_line_1(0, index,
2131 2190 MSG_ORIG(MSG_STR_EMPTY), dep,
2132 2191 conv_ver_flags(vnap->vna_flags,
2133 2192 0, &ver_flags_buf));
2134 2193 if (vnap->vna_other >
2135 2194 versym->max_verndx)
2136 2195 versym->max_verndx =
2137 2196 vnap->vna_other;
2138 2197 } else {
2139 2198 Elf_ver_line_3(0,
2140 2199 MSG_ORIG(MSG_STR_EMPTY), dep,
2141 2200 conv_ver_flags(vnap->vna_flags,
2142 2201 0, &ver_flags_buf));
2143 2202 }
2144 2203 }
2145 2204 }
2146 2205 }
2147 2206 }
2148 2207
2149 2208 /*
2150 2209 * Examine the Verneed section for information related to GNU
2151 2210 * style Versym indexing:
2152 2211 * - A non-zero vna_other field indicates that Versym indexes can
2153 2212 * reference Verneed records.
2154 2213 * - If the object uses GNU style Versym indexing, the
2155 2214 * maximum index value is needed to detect bad Versym entries.
2156 2215 *
2157 2216 * entry:
2158 2217 * vnd - Address of verneed data
2159 2218 * vnd_num - # of Verneed entries
2160 2219 * versym - Information about versym section
2161 2220 *
2162 2221 * exit:
2163 2222 * If a non-zero vna_other field is seen, versym->gnu_needed is set.
2164 2223 *
2165 2224 * versym->max_verndx has been updated to contain the largest
2166 2225 * version index seen.
2167 2226 */
2168 2227 static void
2169 2228 update_gnu_verndx(Verneed *vnd, Word vnd_num, VERSYM_STATE *versym)
2170 2229 {
2171 2230 Word cnt;
2172 2231
2173 2232 for (cnt = 1; cnt <= vnd_num; cnt++,
2174 2233 vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
2175 2234 Half vcnt = vnd->vn_cnt;
2176 2235 Vernaux *vnap = (Vernaux *)((uintptr_t)vnd + vnd->vn_aux);
2177 2236
2178 2237 /*
2179 2238 * A non-zero value of vna_other indicates that this
2180 2239 * object references VERNEED items from the VERSYM
2181 2240 * array.
2182 2241 */
2183 2242 if (vnap->vna_other != 0) {
2184 2243 versym->gnu_needed = 1;
2185 2244 if (vnap->vna_other > versym->max_verndx)
2186 2245 versym->max_verndx = vnap->vna_other;
2187 2246 }
2188 2247
2189 2248 /*
2190 2249 * Check any additional version dependencies.
2191 2250 */
2192 2251 if (vcnt) {
2193 2252 vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next);
2194 2253 for (vcnt--; vcnt; vcnt--,
2195 2254 vnap = (Vernaux *)((uintptr_t)vnap +
2196 2255 vnap->vna_next)) {
2197 2256 if (vnap->vna_other == 0)
2198 2257 continue;
2199 2258
2200 2259 versym->gnu_needed = 1;
2201 2260 if (vnap->vna_other > versym->max_verndx)
2202 2261 versym->max_verndx = vnap->vna_other;
2203 2262 }
2204 2263 }
2205 2264 }
2206 2265 }
2207 2266
2208 2267 /*
2209 2268 * Display version section information if the flags require it.
2210 2269 * Return version information needed by other output.
2211 2270 *
2212 2271 * entry:
2213 2272 * cache - Cache of all section headers
2214 2273 * shnum - # of sections in cache
2215 2274 * file - Name of file
2216 2275 * flags - Command line option flags
2217 2276 * versym - VERSYM_STATE block to be filled in.
2218 2277 */
2219 2278 static void
2220 2279 versions(Cache *cache, Word shnum, const char *file, uint_t flags,
2221 2280 VERSYM_STATE *versym)
2222 2281 {
2223 2282 GElf_Word cnt;
2224 2283 Cache *verdef_cache = NULL, *verneed_cache = NULL;
2225 2284
2226 2285
2227 2286 /* Gather information about the version sections */
2228 2287 versym->max_verndx = 1;
2229 2288 for (cnt = 1; cnt < shnum; cnt++) {
2230 2289 Cache *_cache = &cache[cnt];
2231 2290 Shdr *shdr = _cache->c_shdr;
2232 2291 Dyn *dyn;
2233 2292 ulong_t numdyn;
2234 2293
2235 2294 switch (shdr->sh_type) {
2236 2295 case SHT_DYNAMIC:
2237 2296 /*
2238 2297 * The GNU ld puts a DT_VERSYM entry in the dynamic
2239 2298 * section so that the runtime linker can use it to
2240 2299 * implement their versioning rules. They allow multiple
2241 2300 * incompatible functions with the same name to exist
2242 2301 * in different versions. The Solaris ld does not
2243 2302 * support this mechanism, and as such, does not
2244 2303 * produce DT_VERSYM. We use this fact to determine
2245 2304 * which ld produced this object, and how to interpret
2246 2305 * the version values.
2247 2306 */
2248 2307 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0) ||
2249 2308 (_cache->c_data == NULL))
2250 2309 continue;
2251 2310 numdyn = shdr->sh_size / shdr->sh_entsize;
2252 2311 dyn = (Dyn *)_cache->c_data->d_buf;
2253 2312 for (; numdyn-- > 0; dyn++)
2254 2313 if (dyn->d_tag == DT_VERSYM) {
2255 2314 versym->gnu_full =
2256 2315 versym->gnu_needed = 1;
2257 2316 break;
2258 2317 }
2259 2318 break;
2260 2319
2261 2320 case SHT_SUNW_versym:
2262 2321 /* Record data address for later symbol processing */
2263 2322 if (_cache->c_data != NULL) {
2264 2323 versym->cache = _cache;
2265 2324 versym->data = _cache->c_data->d_buf;
2266 2325 continue;
2267 2326 }
2268 2327 break;
2269 2328
2270 2329 case SHT_SUNW_verdef:
2271 2330 case SHT_SUNW_verneed:
2272 2331 /*
2273 2332 * Ensure the data is non-NULL and the number
2274 2333 * of items is non-zero. Otherwise, we don't
2275 2334 * understand the section, and will not use it.
2276 2335 */
2277 2336 if ((_cache->c_data == NULL) ||
2278 2337 (_cache->c_data->d_buf == NULL)) {
2279 2338 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2280 2339 file, _cache->c_name);
2281 2340 continue;
2282 2341 }
2283 2342 if (shdr->sh_info == 0) {
2284 2343 (void) fprintf(stderr,
2285 2344 MSG_INTL(MSG_ERR_BADSHINFO),
2286 2345 file, _cache->c_name,
2287 2346 EC_WORD(shdr->sh_info));
2288 2347 continue;
2289 2348 }
2290 2349
2291 2350 /* Make sure the string table index is in range */
2292 2351 if ((shdr->sh_link == 0) || (shdr->sh_link >= shnum)) {
2293 2352 (void) fprintf(stderr,
2294 2353 MSG_INTL(MSG_ERR_BADSHLINK), file,
2295 2354 _cache->c_name, EC_WORD(shdr->sh_link));
2296 2355 continue;
2297 2356 }
2298 2357
2299 2358 /*
2300 2359 * The section is usable. Save the cache entry.
2301 2360 */
2302 2361 if (shdr->sh_type == SHT_SUNW_verdef) {
2303 2362 verdef_cache = _cache;
2304 2363 /*
2305 2364 * Under Solaris rules, if there is a verdef
2306 2365 * section, the max versym index is number
2307 2366 * of version definitions it supplies.
2308 2367 */
2309 2368 versym->max_verndx = shdr->sh_info;
2310 2369 } else {
2311 2370 verneed_cache = _cache;
2312 2371 }
2313 2372 break;
2314 2373 }
2315 2374 }
2316 2375
2317 2376 /*
2318 2377 * If there is a Verneed section, examine it for information
2319 2378 * related to GNU style versioning.
2320 2379 */
2321 2380 if (verneed_cache != NULL)
2322 2381 update_gnu_verndx((Verneed *)verneed_cache->c_data->d_buf,
2323 2382 verneed_cache->c_shdr->sh_info, versym);
2324 2383
2325 2384 /*
2326 2385 * Now that all the information is available, display the
2327 2386 * Verdef and Verneed section contents, if requested.
2328 2387 */
2329 2388 if ((flags & FLG_SHOW_VERSIONS) == 0)
2330 2389 return;
2331 2390 if (verdef_cache != NULL) {
2332 2391 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2333 2392 dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERDEF),
2334 2393 verdef_cache->c_name);
2335 2394 version_def((Verdef *)verdef_cache->c_data->d_buf,
2336 2395 verdef_cache->c_shdr->sh_info, verdef_cache,
2337 2396 &cache[verdef_cache->c_shdr->sh_link], file);
2338 2397 }
2339 2398 if (verneed_cache != NULL) {
2340 2399 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2341 2400 dbg_print(0, MSG_INTL(MSG_ELF_SCN_VERNEED),
2342 2401 verneed_cache->c_name);
2343 2402 /*
2344 2403 * If GNU versioning applies to this object, version_need()
2345 2404 * will update versym->max_verndx, and it is not
2346 2405 * necessary to call update_gnu_verndx().
2347 2406 */
2348 2407 version_need((Verneed *)verneed_cache->c_data->d_buf,
2349 2408 verneed_cache->c_shdr->sh_info, verneed_cache,
2350 2409 &cache[verneed_cache->c_shdr->sh_link], file, versym);
2351 2410 }
2352 2411 }
2353 2412
2354 2413 /*
2355 2414 * Search for and process any symbol tables.
2356 2415 */
2357 2416 void
2358 2417 symbols(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
2359 2418 VERSYM_STATE *versym, const char *file, uint_t flags)
2360 2419 {
2361 2420 SYMTBL_STATE state;
2362 2421 Cache *_cache;
2363 2422 Word secndx;
2364 2423
2365 2424 for (secndx = 1; secndx < shnum; secndx++) {
2366 2425 Word symcnt;
2367 2426 Shdr *shdr;
2368 2427
2369 2428 _cache = &cache[secndx];
2370 2429 shdr = _cache->c_shdr;
2371 2430
2372 2431 if ((shdr->sh_type != SHT_SYMTAB) &&
2373 2432 (shdr->sh_type != SHT_DYNSYM) &&
2374 2433 ((shdr->sh_type != SHT_SUNW_LDYNSYM) ||
2375 2434 (osabi != ELFOSABI_SOLARIS)))
2376 2435 continue;
2377 2436 if (!match(MATCH_F_ALL, _cache->c_name, secndx, shdr->sh_type))
2378 2437 continue;
2379 2438
2380 2439 if (!init_symtbl_state(&state, cache, shnum, secndx, ehdr,
2381 2440 osabi, versym, file, flags))
2382 2441 continue;
2383 2442 /*
2384 2443 * Loop through the symbol tables entries.
2385 2444 */
2386 2445 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2387 2446 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMTAB), state.secname);
2388 2447 Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2389 2448
2390 2449 for (symcnt = 0; symcnt < state.symn; symcnt++)
2391 2450 output_symbol(&state, symcnt, shdr->sh_info, symcnt,
2392 2451 state.sym + symcnt);
2393 2452 }
2394 2453 }
2395 2454
2396 2455 /*
2397 2456 * Search for and process any SHT_SUNW_symsort or SHT_SUNW_tlssort sections.
2398 2457 * These sections are always associated with the .SUNW_ldynsym./.dynsym pair.
2399 2458 */
2400 2459 static void
2401 2460 sunw_sort(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi,
2402 2461 VERSYM_STATE *versym, const char *file, uint_t flags)
2403 2462 {
2404 2463 SYMTBL_STATE ldynsym_state, dynsym_state;
2405 2464 Cache *sortcache, *symcache;
2406 2465 Shdr *sortshdr, *symshdr;
2407 2466 Word sortsecndx, symsecndx;
2408 2467 Word ldynsym_cnt;
2409 2468 Word *ndx;
2410 2469 Word ndxn;
2411 2470 int output_cnt = 0;
2412 2471 Conv_inv_buf_t inv_buf;
2413 2472
2414 2473 for (sortsecndx = 1; sortsecndx < shnum; sortsecndx++) {
2415 2474
2416 2475 sortcache = &cache[sortsecndx];
2417 2476 sortshdr = sortcache->c_shdr;
2418 2477
2419 2478 if ((sortshdr->sh_type != SHT_SUNW_symsort) &&
2420 2479 (sortshdr->sh_type != SHT_SUNW_tlssort))
2421 2480 continue;
2422 2481 if (!match(MATCH_F_ALL, sortcache->c_name, sortsecndx,
2423 2482 sortshdr->sh_type))
2424 2483 continue;
2425 2484
2426 2485 /*
2427 2486 * If the section references a SUNW_ldynsym, then we
2428 2487 * expect to see the associated .dynsym immediately
2429 2488 * following. If it references a .dynsym, there is no
2430 2489 * SUNW_ldynsym. If it is any other type, then we don't
2431 2490 * know what to do with it.
2432 2491 */
2433 2492 if ((sortshdr->sh_link == 0) || (sortshdr->sh_link >= shnum)) {
2434 2493 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
2435 2494 file, sortcache->c_name,
2436 2495 EC_WORD(sortshdr->sh_link));
2437 2496 continue;
2438 2497 }
2439 2498 symcache = &cache[sortshdr->sh_link];
2440 2499 symshdr = symcache->c_shdr;
2441 2500 symsecndx = sortshdr->sh_link;
2442 2501 ldynsym_cnt = 0;
2443 2502 switch (symshdr->sh_type) {
2444 2503 case SHT_SUNW_LDYNSYM:
2445 2504 if (!init_symtbl_state(&ldynsym_state, cache, shnum,
2446 2505 symsecndx, ehdr, osabi, versym, file, flags))
2447 2506 continue;
2448 2507 ldynsym_cnt = ldynsym_state.symn;
2449 2508 /*
2450 2509 * We know that the dynsym follows immediately
2451 2510 * after the SUNW_ldynsym, and so, should be at
2452 2511 * (sortshdr->sh_link + 1). However, elfdump is a
2453 2512 * diagnostic tool, so we do the full paranoid
2454 2513 * search instead.
2455 2514 */
2456 2515 for (symsecndx = 1; symsecndx < shnum; symsecndx++) {
2457 2516 symcache = &cache[symsecndx];
2458 2517 symshdr = symcache->c_shdr;
2459 2518 if (symshdr->sh_type == SHT_DYNSYM)
2460 2519 break;
2461 2520 }
2462 2521 if (symsecndx >= shnum) { /* Dynsym not found! */
2463 2522 (void) fprintf(stderr,
2464 2523 MSG_INTL(MSG_ERR_NODYNSYM),
2465 2524 file, sortcache->c_name);
2466 2525 continue;
2467 2526 }
2468 2527 /* Fallthrough to process associated dynsym */
2469 2528 /* FALLTHROUGH */
2470 2529 case SHT_DYNSYM:
2471 2530 if (!init_symtbl_state(&dynsym_state, cache, shnum,
2472 2531 symsecndx, ehdr, osabi, versym, file, flags))
2473 2532 continue;
2474 2533 break;
2475 2534 default:
2476 2535 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADNDXSEC),
2477 2536 file, sortcache->c_name,
2478 2537 conv_sec_type(osabi, ehdr->e_machine,
2479 2538 symshdr->sh_type, 0, &inv_buf));
2480 2539 continue;
2481 2540 }
2482 2541
2483 2542 /*
2484 2543 * Output header
2485 2544 */
2486 2545 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2487 2546 if (ldynsym_cnt > 0) {
2488 2547 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT2),
2489 2548 sortcache->c_name, ldynsym_state.secname,
2490 2549 dynsym_state.secname);
2491 2550 /*
2492 2551 * The data for .SUNW_ldynsym and dynsym sections
2493 2552 * is supposed to be adjacent with SUNW_ldynsym coming
2494 2553 * first. Check, and issue a warning if it isn't so.
2495 2554 */
2496 2555 if (((ldynsym_state.sym + ldynsym_state.symn)
2497 2556 != dynsym_state.sym) &&
2498 2557 ((flags & FLG_CTL_FAKESHDR) == 0))
2499 2558 (void) fprintf(stderr,
2500 2559 MSG_INTL(MSG_ERR_LDYNNOTADJ), file,
2501 2560 ldynsym_state.secname,
2502 2561 dynsym_state.secname);
2503 2562 } else {
2504 2563 dbg_print(0, MSG_INTL(MSG_ELF_SCN_SYMSORT1),
2505 2564 sortcache->c_name, dynsym_state.secname);
2506 2565 }
2507 2566 Elf_syms_table_title(0, ELF_DBG_ELFDUMP);
2508 2567
2509 2568 /* If not first one, insert a line of white space */
2510 2569 if (output_cnt++ > 0)
2511 2570 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2512 2571
2513 2572 /*
2514 2573 * SUNW_dynsymsort and SUNW_dyntlssort are arrays of
2515 2574 * symbol indices. Iterate over the array entries,
2516 2575 * dispaying the referenced symbols.
2517 2576 */
2518 2577 ndxn = sortshdr->sh_size / sortshdr->sh_entsize;
2519 2578 ndx = (Word *)sortcache->c_data->d_buf;
2520 2579 for (; ndxn-- > 0; ndx++) {
2521 2580 if (*ndx >= ldynsym_cnt) {
2522 2581 Word sec_ndx = *ndx - ldynsym_cnt;
2523 2582
2524 2583 output_symbol(&dynsym_state, sec_ndx, 0,
2525 2584 *ndx, dynsym_state.sym + sec_ndx);
2526 2585 } else {
2527 2586 output_symbol(&ldynsym_state, *ndx, 0,
2528 2587 *ndx, ldynsym_state.sym + *ndx);
2529 2588 }
2530 2589 }
2531 2590 }
2532 2591 }
2533 2592
2534 2593 /*
2535 2594 * Search for and process any relocation sections.
2536 2595 */
2537 2596 static void
2538 2597 reloc(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
2539 2598 {
2540 2599 Word cnt;
2541 2600
2542 2601 for (cnt = 1; cnt < shnum; cnt++) {
2543 2602 Word type, symnum;
2544 2603 Xword relndx, relnum, relsize;
2545 2604 void *rels;
2546 2605 Sym *syms;
2547 2606 Cache *symsec, *strsec;
2548 2607 Cache *_cache = &cache[cnt];
2549 2608 Shdr *shdr = _cache->c_shdr;
2550 2609 char *relname = _cache->c_name;
2551 2610 Conv_inv_buf_t inv_buf;
2552 2611
2553 2612 if (((type = shdr->sh_type) != SHT_RELA) &&
2554 2613 (type != SHT_REL))
2555 2614 continue;
2556 2615 if (!match(MATCH_F_ALL, relname, cnt, type))
2557 2616 continue;
2558 2617
2559 2618 /*
2560 2619 * Decide entry size.
2561 2620 */
2562 2621 if (((relsize = shdr->sh_entsize) == 0) ||
2563 2622 (relsize > shdr->sh_size)) {
2564 2623 if (type == SHT_RELA)
2565 2624 relsize = sizeof (Rela);
2566 2625 else
2567 2626 relsize = sizeof (Rel);
2568 2627 }
2569 2628
2570 2629 /*
2571 2630 * Determine the number of relocations available.
2572 2631 */
2573 2632 if (shdr->sh_size == 0) {
2574 2633 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
2575 2634 file, relname);
2576 2635 continue;
2577 2636 }
2578 2637 if (_cache->c_data == NULL)
2579 2638 continue;
2580 2639
2581 2640 rels = _cache->c_data->d_buf;
2582 2641 relnum = shdr->sh_size / relsize;
2583 2642
2584 2643 /*
2585 2644 * Get the data buffer for the associated symbol table and
2586 2645 * string table.
2587 2646 */
2588 2647 if (stringtbl(cache, 1, cnt, shnum, file,
2589 2648 &symnum, &symsec, &strsec) == 0)
2590 2649 continue;
2591 2650
2592 2651 syms = symsec->c_data->d_buf;
2593 2652
2594 2653 /*
2595 2654 * Loop through the relocation entries.
2596 2655 */
2597 2656 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
2598 2657 dbg_print(0, MSG_INTL(MSG_ELF_SCN_RELOC), _cache->c_name);
2599 2658 Elf_reloc_title(0, ELF_DBG_ELFDUMP, type);
2600 2659
2601 2660 for (relndx = 0; relndx < relnum; relndx++,
2602 2661 rels = (void *)((char *)rels + relsize)) {
2603 2662 Half mach = ehdr->e_machine;
2604 2663 char section[BUFSIZ];
2605 2664 const char *symname;
2606 2665 Word symndx, reltype;
2607 2666 Rela *rela;
2608 2667 Rel *rel;
2609 2668
2610 2669 /*
2611 2670 * Unravel the relocation and determine the symbol with
2612 2671 * which this relocation is associated.
2613 2672 */
2614 2673 if (type == SHT_RELA) {
2615 2674 rela = (Rela *)rels;
2616 2675 symndx = ELF_R_SYM(rela->r_info);
2617 2676 reltype = ELF_R_TYPE(rela->r_info, mach);
2618 2677 } else {
2619 2678 rel = (Rel *)rels;
2620 2679 symndx = ELF_R_SYM(rel->r_info);
2621 2680 reltype = ELF_R_TYPE(rel->r_info, mach);
2622 2681 }
2623 2682
2624 2683 symname = relsymname(cache, _cache, strsec, symndx,
2625 2684 symnum, relndx, syms, section, BUFSIZ, file);
2626 2685
2627 2686 /*
2628 2687 * A zero symbol index is only valid for a few
2629 2688 * relocations.
2630 2689 */
2631 2690 if (symndx == 0) {
2632 2691 int badrel = 0;
2633 2692
2634 2693 if ((mach == EM_SPARC) ||
2635 2694 (mach == EM_SPARC32PLUS) ||
2636 2695 (mach == EM_SPARCV9)) {
2637 2696 if ((reltype != R_SPARC_NONE) &&
2638 2697 (reltype != R_SPARC_REGISTER) &&
2639 2698 (reltype != R_SPARC_RELATIVE))
2640 2699 badrel++;
2641 2700 } else if (mach == EM_386) {
2642 2701 if ((reltype != R_386_NONE) &&
2643 2702 (reltype != R_386_RELATIVE))
2644 2703 badrel++;
2645 2704 } else if (mach == EM_AMD64) {
2646 2705 if ((reltype != R_AMD64_NONE) &&
2647 2706 (reltype != R_AMD64_RELATIVE))
2648 2707 badrel++;
2649 2708 }
2650 2709
2651 2710 if (badrel) {
2652 2711 (void) fprintf(stderr,
2653 2712 MSG_INTL(MSG_ERR_BADREL1), file,
2654 2713 conv_reloc_type(mach, reltype,
2655 2714 0, &inv_buf));
2656 2715 }
2657 2716 }
2658 2717
2659 2718 Elf_reloc_entry_1(0, ELF_DBG_ELFDUMP,
2660 2719 MSG_ORIG(MSG_STR_EMPTY), ehdr->e_machine, type,
2661 2720 rels, relname, symname, 0);
2662 2721 }
2663 2722 }
2664 2723 }
2665 2724
2666 2725
2667 2726 /*
2668 2727 * This value controls which test dyn_test() performs.
2669 2728 */
2670 2729 typedef enum { DYN_TEST_ADDR, DYN_TEST_SIZE, DYN_TEST_ENTSIZE } dyn_test_t;
2671 2730
2672 2731 /*
2673 2732 * Used by dynamic() to compare the value of a dynamic element against
2674 2733 * the starting address of the section it references.
2675 2734 *
2676 2735 * entry:
2677 2736 * test_type - Specify which dyn item is being tested.
2678 2737 * sh_type - SHT_* type value for required section.
2679 2738 * sec_cache - Cache entry for section, or NULL if the object lacks
2680 2739 * a section of this type.
2681 2740 * dyn - Dyn entry to be tested
2682 2741 * dynsec_cnt - # of dynamic section being examined. The first
2683 2742 * dynamic section is 1, the next is 2, and so on...
2684 2743 * ehdr - ELF header for file
2685 2744 * file - Name of file
2686 2745 */
2687 2746 static void
2688 2747 dyn_test(dyn_test_t test_type, Word sh_type, Cache *sec_cache, Dyn *dyn,
2689 2748 Word dynsec_cnt, Ehdr *ehdr, uchar_t osabi, const char *file)
2690 2749 {
2691 2750 Conv_inv_buf_t buf1, buf2;
2692 2751
2693 2752 /*
2694 2753 * These tests are based around the implicit assumption that
2695 2754 * there is only one dynamic section in an object, and also only
2696 2755 * one of the sections it references. We have therefore gathered
2697 2756 * all of the necessary information to test this in a single pass
2698 2757 * over the section headers, which is very efficient. We are not
2699 2758 * aware of any case where more than one dynamic section would
2700 2759 * be meaningful in an ELF object, so this is a reasonable solution.
2701 2760 *
2702 2761 * To test multiple dynamic sections correctly would be more
2703 2762 * expensive in code and time. We would have to build a data structure
2704 2763 * containing all the dynamic elements. Then, we would use the address
2705 2764 * to locate the section it references and ensure the section is of
2706 2765 * the right type and that the address in the dynamic element is
2707 2766 * to the start of the section. Then, we could check the size and
2708 2767 * entsize values against those same sections. This is O(n^2), and
2709 2768 * also complicated.
2710 2769 *
2711 2770 * In the highly unlikely case that there is more than one dynamic
2712 2771 * section, we only test the first one, and simply allow the values
2713 2772 * of the subsequent one to be displayed unchallenged.
2714 2773 */
2715 2774 if (dynsec_cnt != 1)
2716 2775 return;
2717 2776
2718 2777 /*
2719 2778 * A DT_ item that references a section address should always find
2720 2779 * the section in the file.
2721 2780 */
2722 2781 if (sec_cache == NULL) {
2723 2782 const char *name;
2724 2783
2725 2784 /*
2726 2785 * Supply section names instead of section types for
2727 2786 * things that reference progbits so that the error
2728 2787 * message will make more sense.
2729 2788 */
2730 2789 switch (dyn->d_tag) {
2731 2790 case DT_INIT:
2732 2791 name = MSG_ORIG(MSG_ELF_INIT);
2733 2792 break;
2734 2793 case DT_FINI:
2735 2794 name = MSG_ORIG(MSG_ELF_FINI);
2736 2795 break;
2737 2796 default:
2738 2797 name = conv_sec_type(osabi, ehdr->e_machine,
2739 2798 sh_type, 0, &buf1);
2740 2799 break;
2741 2800 }
2742 2801 (void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNNOBCKSEC), file,
2743 2802 name, conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2744 2803 CONV_FMT_ALT_CF, &buf2));
2745 2804 return;
2746 2805 }
2747 2806
2748 2807
2749 2808 switch (test_type) {
2750 2809 case DYN_TEST_ADDR:
2751 2810 /* The section address should match the DT_ item value */
2752 2811 if (dyn->d_un.d_val != sec_cache->c_shdr->sh_addr)
2753 2812 (void) fprintf(stderr,
2754 2813 MSG_INTL(MSG_ERR_DYNBADADDR), file,
2755 2814 conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2756 2815 CONV_FMT_ALT_CF, &buf1), EC_ADDR(dyn->d_un.d_val),
2757 2816 sec_cache->c_ndx, sec_cache->c_name,
2758 2817 EC_ADDR(sec_cache->c_shdr->sh_addr));
2759 2818 break;
2760 2819
2761 2820 case DYN_TEST_SIZE:
2762 2821 /* The section size should match the DT_ item value */
2763 2822 if (dyn->d_un.d_val != sec_cache->c_shdr->sh_size)
2764 2823 (void) fprintf(stderr,
2765 2824 MSG_INTL(MSG_ERR_DYNBADSIZE), file,
2766 2825 conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2767 2826 CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2768 2827 sec_cache->c_ndx, sec_cache->c_name,
2769 2828 EC_XWORD(sec_cache->c_shdr->sh_size));
2770 2829 break;
2771 2830
2772 2831 case DYN_TEST_ENTSIZE:
2773 2832 /* The sh_entsize value should match the DT_ item value */
2774 2833 if (dyn->d_un.d_val != sec_cache->c_shdr->sh_entsize)
2775 2834 (void) fprintf(stderr,
2776 2835 MSG_INTL(MSG_ERR_DYNBADENTSIZE), file,
2777 2836 conv_dyn_tag(dyn->d_tag, osabi, ehdr->e_machine,
2778 2837 CONV_FMT_ALT_CF, &buf1), EC_XWORD(dyn->d_un.d_val),
2779 2838 sec_cache->c_ndx, sec_cache->c_name,
2780 2839 EC_XWORD(sec_cache->c_shdr->sh_entsize));
2781 2840 break;
2782 2841 }
2783 2842 }
2784 2843
2785 2844 /*
2786 2845 * There are some DT_ entries that have corresponding symbols
2787 2846 * (e.g. DT_INIT and _init). It is expected that these items will
2788 2847 * both have the same value if both are present. This routine
2789 2848 * examines the well known symbol tables for such symbols and
2790 2849 * issues warnings for any that don't match.
2791 2850 *
2792 2851 * entry:
2793 2852 * dyn - Dyn entry to be tested
2794 2853 * symname - Name of symbol that corresponds to dyn
2795 2854 * symtab_cache, dynsym_cache, ldynsym_cache - Symbol tables to check
2796 2855 * target_cache - Section the symname section is expected to be
2797 2856 * associated with.
2798 2857 * cache - Cache of all section headers
2799 2858 * shnum - # of sections in cache
2800 2859 * ehdr - ELF header for file
2801 2860 * osabi - OSABI to apply when interpreting object
2802 2861 * file - Name of file
2803 2862 */
2804 2863 static void
2805 2864 dyn_symtest(Dyn *dyn, const char *symname, Cache *symtab_cache,
2806 2865 Cache *dynsym_cache, Cache *ldynsym_cache, Cache *target_cache,
2807 2866 Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
2808 2867 {
2809 2868 Conv_inv_buf_t buf;
2810 2869 int i;
2811 2870 Sym *sym;
2812 2871 Cache *_cache;
2813 2872
2814 2873 for (i = 0; i < 3; i++) {
2815 2874 switch (i) {
2816 2875 case 0:
2817 2876 _cache = symtab_cache;
2818 2877 break;
2819 2878 case 1:
2820 2879 _cache = dynsym_cache;
2821 2880 break;
2822 2881 case 2:
2823 2882 _cache = ldynsym_cache;
2824 2883 break;
2825 2884 }
2826 2885
2827 2886 if ((_cache != NULL) &&
2828 2887 symlookup(symname, cache, shnum, &sym, target_cache,
2829 2888 _cache, file) && (sym->st_value != dyn->d_un.d_val))
2830 2889 (void) fprintf(stderr, MSG_INTL(MSG_ERR_DYNSYMVAL),
2831 2890 file, _cache->c_name, conv_dyn_tag(dyn->d_tag,
2832 2891 osabi, ehdr->e_machine, CONV_FMT_ALT_CF, &buf),
2833 2892 symname, EC_ADDR(sym->st_value));
2834 2893 }
2835 2894 }
2836 2895
2837 2896 /*
2838 2897 * Search for and process a .dynamic section.
2839 2898 */
2840 2899 static void
2841 2900 dynamic(Cache *cache, Word shnum, Ehdr *ehdr, uchar_t osabi, const char *file)
2842 2901 {
2843 2902 struct {
2844 2903 Cache *symtab;
2845 2904 Cache *dynstr;
2846 2905 Cache *dynsym;
2847 2906 Cache *hash;
2848 2907 Cache *fini;
2849 2908 Cache *fini_array;
2850 2909 Cache *init;
2851 2910 Cache *init_array;
2852 2911 Cache *preinit_array;
2853 2912 Cache *rel;
2854 2913 Cache *rela;
2855 2914 Cache *sunw_cap;
2856 2915 Cache *sunw_capinfo;
2857 2916 Cache *sunw_capchain;
2858 2917 Cache *sunw_ldynsym;
2859 2918 Cache *sunw_move;
2860 2919 Cache *sunw_syminfo;
2861 2920 Cache *sunw_symsort;
2862 2921 Cache *sunw_tlssort;
2863 2922 Cache *sunw_verdef;
2864 2923 Cache *sunw_verneed;
2865 2924 Cache *sunw_versym;
2866 2925 } sec;
2867 2926 Word dynsec_ndx;
2868 2927 Word dynsec_num;
2869 2928 int dynsec_cnt;
2870 2929 Word cnt;
2871 2930 int osabi_solaris = osabi == ELFOSABI_SOLARIS;
2872 2931
2873 2932 /*
2874 2933 * Make a pass over all the sections, gathering section information
2875 2934 * we'll need below.
2876 2935 */
2877 2936 dynsec_num = 0;
2878 2937 bzero(&sec, sizeof (sec));
2879 2938 for (cnt = 1; cnt < shnum; cnt++) {
2880 2939 Cache *_cache = &cache[cnt];
2881 2940
2882 2941 switch (_cache->c_shdr->sh_type) {
2883 2942 case SHT_DYNAMIC:
2884 2943 if (dynsec_num == 0) {
2885 2944 dynsec_ndx = cnt;
2886 2945
2887 2946 /* Does it have a valid string table? */
2888 2947 (void) stringtbl(cache, 0, cnt, shnum, file,
2889 2948 0, 0, &sec.dynstr);
2890 2949 }
2891 2950 dynsec_num++;
2892 2951 break;
2893 2952
2894 2953
2895 2954 case SHT_PROGBITS:
2896 2955 /*
2897 2956 * We want to detect the .init and .fini sections,
2898 2957 * if present. These are SHT_PROGBITS, so all we
2899 2958 * have to go on is the section name. Normally comparing
2900 2959 * names is a bad idea, but there are some special
2901 2960 * names (i.e. .init/.fini/.interp) that are very
2902 2961 * difficult to use in any other context, and for
2903 2962 * these symbols, we do the heuristic match.
2904 2963 */
2905 2964 if (strcmp(_cache->c_name,
2906 2965 MSG_ORIG(MSG_ELF_INIT)) == 0) {
2907 2966 if (sec.init == NULL)
2908 2967 sec.init = _cache;
2909 2968 } else if (strcmp(_cache->c_name,
2910 2969 MSG_ORIG(MSG_ELF_FINI)) == 0) {
2911 2970 if (sec.fini == NULL)
2912 2971 sec.fini = _cache;
2913 2972 }
2914 2973 break;
2915 2974
2916 2975 case SHT_REL:
2917 2976 /*
2918 2977 * We want the SHT_REL section with the lowest
2919 2978 * offset. The linker gathers them together,
2920 2979 * and puts the address of the first one
2921 2980 * into the DT_REL dynamic element.
2922 2981 */
2923 2982 if ((sec.rel == NULL) ||
2924 2983 (_cache->c_shdr->sh_offset <
2925 2984 sec.rel->c_shdr->sh_offset))
2926 2985 sec.rel = _cache;
2927 2986 break;
2928 2987
2929 2988 case SHT_RELA:
2930 2989 /* RELA is handled just like RELA above */
2931 2990 if ((sec.rela == NULL) ||
2932 2991 (_cache->c_shdr->sh_offset <
2933 2992 sec.rela->c_shdr->sh_offset))
2934 2993 sec.rela = _cache;
2935 2994 break;
2936 2995
2937 2996 /*
2938 2997 * The GRAB macro is used for the simple case in which
2939 2998 * we simply grab the first section of the desired type.
2940 2999 */
2941 3000 #define GRAB(_sec_type, _sec_field) \
2942 3001 case _sec_type: \
2943 3002 if (sec._sec_field == NULL) \
2944 3003 sec._sec_field = _cache; \
2945 3004 break
2946 3005 GRAB(SHT_SYMTAB, symtab);
2947 3006 GRAB(SHT_DYNSYM, dynsym);
2948 3007 GRAB(SHT_FINI_ARRAY, fini_array);
2949 3008 GRAB(SHT_HASH, hash);
2950 3009 GRAB(SHT_INIT_ARRAY, init_array);
2951 3010 GRAB(SHT_SUNW_move, sunw_move);
2952 3011 GRAB(SHT_PREINIT_ARRAY, preinit_array);
2953 3012 GRAB(SHT_SUNW_cap, sunw_cap);
2954 3013 GRAB(SHT_SUNW_capinfo, sunw_capinfo);
2955 3014 GRAB(SHT_SUNW_capchain, sunw_capchain);
2956 3015 GRAB(SHT_SUNW_LDYNSYM, sunw_ldynsym);
2957 3016 GRAB(SHT_SUNW_syminfo, sunw_syminfo);
2958 3017 GRAB(SHT_SUNW_symsort, sunw_symsort);
2959 3018 GRAB(SHT_SUNW_tlssort, sunw_tlssort);
2960 3019 GRAB(SHT_SUNW_verdef, sunw_verdef);
2961 3020 GRAB(SHT_SUNW_verneed, sunw_verneed);
2962 3021 GRAB(SHT_SUNW_versym, sunw_versym);
2963 3022 #undef GRAB
2964 3023 }
2965 3024 }
2966 3025
2967 3026 /*
2968 3027 * If no dynamic section, return immediately. If more than one
2969 3028 * dynamic section, then something odd is going on and an error
2970 3029 * is in order, but then continue on and display them all.
2971 3030 */
2972 3031 if (dynsec_num == 0)
2973 3032 return;
2974 3033 if (dynsec_num > 1)
2975 3034 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MULTDYN),
2976 3035 file, EC_WORD(dynsec_num));
2977 3036
2978 3037
2979 3038 dynsec_cnt = 0;
2980 3039 for (cnt = dynsec_ndx; (cnt < shnum) && (dynsec_cnt < dynsec_num);
2981 3040 cnt++) {
2982 3041 Dyn *dyn;
2983 3042 ulong_t numdyn;
2984 3043 int ndx, end_ndx;
2985 3044 Cache *_cache = &cache[cnt], *strsec;
2986 3045 Shdr *shdr = _cache->c_shdr;
2987 3046 int dumped = 0;
2988 3047
2989 3048 if (shdr->sh_type != SHT_DYNAMIC)
2990 3049 continue;
2991 3050 dynsec_cnt++;
2992 3051
2993 3052 /*
2994 3053 * Verify the associated string table section.
2995 3054 */
2996 3055 if (stringtbl(cache, 0, cnt, shnum, file, 0, 0, &strsec) == 0)
2997 3056 continue;
2998 3057
2999 3058 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
3000 3059 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3001 3060 file, _cache->c_name);
3002 3061 continue;
3003 3062 }
3004 3063 if (_cache->c_data == NULL)
3005 3064 continue;
3006 3065
3007 3066 numdyn = shdr->sh_size / shdr->sh_entsize;
3008 3067 dyn = (Dyn *)_cache->c_data->d_buf;
3009 3068
3010 3069 /*
3011 3070 * We expect the REL/RELA entries to reference the reloc
3012 3071 * section with the lowest address. However, this is
3013 3072 * not true for dumped objects. Detect if this object has
3014 3073 * been dumped so that we can skip the reloc address test
3015 3074 * in that case.
3016 3075 */
3017 3076 for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3018 3077 if (dyn->d_tag == DT_FLAGS_1) {
3019 3078 dumped = (dyn->d_un.d_val & DF_1_CONFALT) != 0;
3020 3079 break;
3021 3080 }
3022 3081 }
3023 3082 dyn = (Dyn *)_cache->c_data->d_buf;
3024 3083
3025 3084 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3026 3085 dbg_print(0, MSG_INTL(MSG_ELF_SCN_DYNAMIC), _cache->c_name);
3027 3086
3028 3087 Elf_dyn_title(0);
3029 3088
3030 3089 for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
3031 3090 union {
3032 3091 Conv_inv_buf_t inv;
3033 3092 Conv_dyn_flag_buf_t flag;
3034 3093 Conv_dyn_flag1_buf_t flag1;
3035 3094 Conv_dyn_posflag1_buf_t posflag1;
3036 3095 Conv_dyn_feature1_buf_t feature1;
3037 3096 } c_buf;
3038 3097 const char *name = NULL;
3039 3098
3040 3099 /*
3041 3100 * Print the information numerically, and if possible
3042 3101 * as a string. If a string is available, name is
3043 3102 * set to reference it.
3044 3103 *
3045 3104 * Also, take this opportunity to sanity check
3046 3105 * the values of DT elements. In the code above,
3047 3106 * we gathered information on sections that are
3048 3107 * referenced by the dynamic section. Here, we
3049 3108 * compare the attributes of those sections to
3050 3109 * the DT_ items that reference them and report
3051 3110 * on inconsistencies.
3052 3111 *
3053 3112 * Things not currently tested that could be improved
3054 3113 * in later revisions include:
3055 3114 * - We don't check PLT or GOT related items
3056 3115 * - We don't handle computing the lengths of
3057 3116 * relocation arrays. To handle this
3058 3117 * requires examining data that spans
3059 3118 * across sections, in a contiguous span
3060 3119 * within a single segment.
3061 3120 * - DT_VERDEFNUM and DT_VERNEEDNUM can't be
3062 3121 * verified without parsing the sections.
3063 3122 * - We don't handle DT_SUNW_SYMSZ, which would
3064 3123 * be the sum of the lengths of .dynsym and
3065 3124 * .SUNW_ldynsym
3066 3125 * - DT_SUNW_STRPAD can't be verified other than
3067 3126 * to check that it's not larger than
3068 3127 * the string table.
3069 3128 * - Some items come in "all or none" clusters
3070 3129 * that give an address, element size,
3071 3130 * and data length in bytes. We don't
3072 3131 * verify that there are no missing items
3073 3132 * in such groups.
3074 3133 */
3075 3134 switch (dyn->d_tag) {
3076 3135 case DT_NULL:
3077 3136 /*
3078 3137 * Special case: DT_NULLs can come in groups
3079 3138 * that we prefer to reduce to a single line.
3080 3139 */
3081 3140 end_ndx = ndx;
3082 3141 while ((end_ndx < (numdyn - 1)) &&
3083 3142 ((dyn + 1)->d_tag == DT_NULL)) {
3084 3143 dyn++;
3085 3144 end_ndx++;
3086 3145 }
3087 3146 Elf_dyn_null_entry(0, dyn, ndx, end_ndx);
3088 3147 ndx = end_ndx;
3089 3148 continue;
3090 3149
3091 3150 /*
3092 3151 * String items all reference the dynstr. The string()
3093 3152 * function does the necessary sanity checking.
3094 3153 */
3095 3154 case DT_NEEDED:
3096 3155 case DT_SONAME:
3097 3156 case DT_FILTER:
3098 3157 case DT_AUXILIARY:
3099 3158 case DT_CONFIG:
3100 3159 case DT_RPATH:
3101 3160 case DT_RUNPATH:
3102 3161 case DT_USED:
3103 3162 case DT_DEPAUDIT:
3104 3163 case DT_AUDIT:
3105 3164 name = string(_cache, ndx, strsec,
3106 3165 file, dyn->d_un.d_ptr);
3107 3166 break;
3108 3167
3109 3168 case DT_SUNW_AUXILIARY:
3110 3169 case DT_SUNW_FILTER:
3111 3170 if (osabi_solaris)
3112 3171 name = string(_cache, ndx, strsec,
3113 3172 file, dyn->d_un.d_ptr);
3114 3173 break;
3115 3174
3116 3175 case DT_FLAGS:
3117 3176 name = conv_dyn_flag(dyn->d_un.d_val,
3118 3177 0, &c_buf.flag);
3119 3178 break;
3120 3179 case DT_FLAGS_1:
3121 3180 name = conv_dyn_flag1(dyn->d_un.d_val, 0,
3122 3181 &c_buf.flag1);
3123 3182 break;
3124 3183 case DT_POSFLAG_1:
3125 3184 name = conv_dyn_posflag1(dyn->d_un.d_val, 0,
3126 3185 &c_buf.posflag1);
3127 3186 break;
3128 3187 case DT_FEATURE_1:
3129 3188 name = conv_dyn_feature1(dyn->d_un.d_val, 0,
3130 3189 &c_buf.feature1);
3131 3190 break;
3132 3191 case DT_DEPRECATED_SPARC_REGISTER:
3133 3192 name = MSG_INTL(MSG_STR_DEPRECATED);
3134 3193 break;
3135 3194
3136 3195 case DT_SUNW_LDMACH:
3137 3196 if (!osabi_solaris)
3138 3197 break;
3139 3198 name = conv_ehdr_mach((Half)dyn->d_un.d_val,
3140 3199 0, &c_buf.inv);
3141 3200 break;
3142 3201
3143 3202 /*
3144 3203 * Cases below this point are strictly sanity checking,
3145 3204 * and do not generate a name string. The TEST_ macros
3146 3205 * are used to hide the boiler plate arguments neeeded
3147 3206 * by dyn_test().
3148 3207 */
3149 3208 #define TEST_ADDR(_sh_type, _sec_field) \
3150 3209 dyn_test(DYN_TEST_ADDR, _sh_type, \
3151 3210 sec._sec_field, dyn, dynsec_cnt, ehdr, \
3152 3211 osabi, file)
3153 3212 #define TEST_SIZE(_sh_type, _sec_field) \
3154 3213 dyn_test(DYN_TEST_SIZE, _sh_type, \
3155 3214 sec._sec_field, dyn, dynsec_cnt, ehdr, \
3156 3215 osabi, file)
3157 3216 #define TEST_ENTSIZE(_sh_type, _sec_field) \
3158 3217 dyn_test(DYN_TEST_ENTSIZE, _sh_type, \
3159 3218 sec._sec_field, dyn, dynsec_cnt, ehdr, \
3160 3219 osabi, file)
3161 3220
3162 3221 case DT_FINI:
3163 3222 dyn_symtest(dyn, MSG_ORIG(MSG_SYM_FINI),
3164 3223 sec.symtab, sec.dynsym, sec.sunw_ldynsym,
3165 3224 sec.fini, cache, shnum, ehdr, osabi, file);
3166 3225 TEST_ADDR(SHT_PROGBITS, fini);
3167 3226 break;
3168 3227
3169 3228 case DT_FINI_ARRAY:
3170 3229 TEST_ADDR(SHT_FINI_ARRAY, fini_array);
3171 3230 break;
3172 3231
3173 3232 case DT_FINI_ARRAYSZ:
3174 3233 TEST_SIZE(SHT_FINI_ARRAY, fini_array);
3175 3234 break;
3176 3235
3177 3236 case DT_HASH:
3178 3237 TEST_ADDR(SHT_HASH, hash);
3179 3238 break;
3180 3239
3181 3240 case DT_INIT:
3182 3241 dyn_symtest(dyn, MSG_ORIG(MSG_SYM_INIT),
3183 3242 sec.symtab, sec.dynsym, sec.sunw_ldynsym,
3184 3243 sec.init, cache, shnum, ehdr, osabi, file);
3185 3244 TEST_ADDR(SHT_PROGBITS, init);
3186 3245 break;
3187 3246
3188 3247 case DT_INIT_ARRAY:
3189 3248 TEST_ADDR(SHT_INIT_ARRAY, init_array);
3190 3249 break;
3191 3250
3192 3251 case DT_INIT_ARRAYSZ:
3193 3252 TEST_SIZE(SHT_INIT_ARRAY, init_array);
3194 3253 break;
3195 3254
3196 3255 case DT_MOVEENT:
3197 3256 TEST_ENTSIZE(SHT_SUNW_move, sunw_move);
3198 3257 break;
3199 3258
3200 3259 case DT_MOVESZ:
3201 3260 TEST_SIZE(SHT_SUNW_move, sunw_move);
3202 3261 break;
3203 3262
3204 3263 case DT_MOVETAB:
3205 3264 TEST_ADDR(SHT_SUNW_move, sunw_move);
3206 3265 break;
3207 3266
3208 3267 case DT_PREINIT_ARRAY:
3209 3268 TEST_ADDR(SHT_PREINIT_ARRAY, preinit_array);
3210 3269 break;
3211 3270
3212 3271 case DT_PREINIT_ARRAYSZ:
3213 3272 TEST_SIZE(SHT_PREINIT_ARRAY, preinit_array);
3214 3273 break;
3215 3274
3216 3275 case DT_REL:
3217 3276 if (!dumped)
3218 3277 TEST_ADDR(SHT_REL, rel);
3219 3278 break;
3220 3279
3221 3280 case DT_RELENT:
3222 3281 TEST_ENTSIZE(SHT_REL, rel);
3223 3282 break;
3224 3283
3225 3284 case DT_RELA:
3226 3285 if (!dumped)
3227 3286 TEST_ADDR(SHT_RELA, rela);
3228 3287 break;
3229 3288
3230 3289 case DT_RELAENT:
3231 3290 TEST_ENTSIZE(SHT_RELA, rela);
3232 3291 break;
3233 3292
3234 3293 case DT_STRTAB:
3235 3294 TEST_ADDR(SHT_STRTAB, dynstr);
3236 3295 break;
3237 3296
3238 3297 case DT_STRSZ:
3239 3298 TEST_SIZE(SHT_STRTAB, dynstr);
3240 3299 break;
3241 3300
3242 3301 case DT_SUNW_CAP:
3243 3302 if (osabi_solaris)
3244 3303 TEST_ADDR(SHT_SUNW_cap, sunw_cap);
3245 3304 break;
3246 3305
3247 3306 case DT_SUNW_CAPINFO:
3248 3307 if (osabi_solaris)
3249 3308 TEST_ADDR(SHT_SUNW_capinfo,
3250 3309 sunw_capinfo);
3251 3310 break;
3252 3311
3253 3312 case DT_SUNW_CAPCHAIN:
3254 3313 if (osabi_solaris)
3255 3314 TEST_ADDR(SHT_SUNW_capchain,
3256 3315 sunw_capchain);
3257 3316 break;
3258 3317
3259 3318 case DT_SUNW_SYMTAB:
3260 3319 TEST_ADDR(SHT_SUNW_LDYNSYM, sunw_ldynsym);
3261 3320 break;
3262 3321
3263 3322 case DT_SYMENT:
3264 3323 TEST_ENTSIZE(SHT_DYNSYM, dynsym);
3265 3324 break;
3266 3325
3267 3326 case DT_SYMINENT:
3268 3327 TEST_ENTSIZE(SHT_SUNW_syminfo, sunw_syminfo);
3269 3328 break;
3270 3329
3271 3330 case DT_SYMINFO:
3272 3331 TEST_ADDR(SHT_SUNW_syminfo, sunw_syminfo);
3273 3332 break;
3274 3333
3275 3334 case DT_SYMINSZ:
3276 3335 TEST_SIZE(SHT_SUNW_syminfo, sunw_syminfo);
3277 3336 break;
3278 3337
3279 3338 case DT_SYMTAB:
3280 3339 TEST_ADDR(SHT_DYNSYM, dynsym);
3281 3340 break;
3282 3341
3283 3342 case DT_SUNW_SORTENT:
3284 3343 /*
3285 3344 * This entry is related to both the symsort and
3286 3345 * tlssort sections.
3287 3346 */
3288 3347 if (osabi_solaris) {
3289 3348 int test_tls =
3290 3349 (sec.sunw_tlssort != NULL);
3291 3350 int test_sym =
3292 3351 (sec.sunw_symsort != NULL) ||
3293 3352 !test_tls;
3294 3353 if (test_sym)
3295 3354 TEST_ENTSIZE(SHT_SUNW_symsort,
3296 3355 sunw_symsort);
3297 3356 if (test_tls)
3298 3357 TEST_ENTSIZE(SHT_SUNW_tlssort,
3299 3358 sunw_tlssort);
3300 3359 }
3301 3360 break;
3302 3361
3303 3362
3304 3363 case DT_SUNW_SYMSORT:
3305 3364 if (osabi_solaris)
3306 3365 TEST_ADDR(SHT_SUNW_symsort,
3307 3366 sunw_symsort);
3308 3367 break;
3309 3368
3310 3369 case DT_SUNW_SYMSORTSZ:
3311 3370 if (osabi_solaris)
3312 3371 TEST_SIZE(SHT_SUNW_symsort,
3313 3372 sunw_symsort);
3314 3373 break;
3315 3374
3316 3375 case DT_SUNW_TLSSORT:
3317 3376 if (osabi_solaris)
3318 3377 TEST_ADDR(SHT_SUNW_tlssort,
3319 3378 sunw_tlssort);
3320 3379 break;
3321 3380
3322 3381 case DT_SUNW_TLSSORTSZ:
3323 3382 if (osabi_solaris)
3324 3383 TEST_SIZE(SHT_SUNW_tlssort,
3325 3384 sunw_tlssort);
3326 3385 break;
3327 3386
3328 3387 case DT_VERDEF:
3329 3388 TEST_ADDR(SHT_SUNW_verdef, sunw_verdef);
3330 3389 break;
3331 3390
3332 3391 case DT_VERNEED:
3333 3392 TEST_ADDR(SHT_SUNW_verneed, sunw_verneed);
3334 3393 break;
3335 3394
3336 3395 case DT_VERSYM:
3337 3396 TEST_ADDR(SHT_SUNW_versym, sunw_versym);
3338 3397 break;
3339 3398 #undef TEST_ADDR
3340 3399 #undef TEST_SIZE
3341 3400 #undef TEST_ENTSIZE
3342 3401 }
3343 3402
3344 3403 if (name == NULL)
3345 3404 name = MSG_ORIG(MSG_STR_EMPTY);
3346 3405 Elf_dyn_entry(0, dyn, ndx, name,
3347 3406 osabi, ehdr->e_machine);
3348 3407 }
3349 3408 }
3350 3409 }
3351 3410
3352 3411 /*
3353 3412 * Search for and process a MOVE section.
3354 3413 */
3355 3414 static void
3356 3415 move(Cache *cache, Word shnum, const char *file, uint_t flags)
3357 3416 {
3358 3417 Word cnt;
3359 3418 const char *fmt = NULL;
3360 3419
3361 3420 for (cnt = 1; cnt < shnum; cnt++) {
3362 3421 Word movenum, symnum, ndx;
3363 3422 Sym *syms;
3364 3423 Cache *_cache = &cache[cnt];
3365 3424 Shdr *shdr = _cache->c_shdr;
3366 3425 Cache *symsec, *strsec;
3367 3426 Move *move;
3368 3427
3369 3428 if (shdr->sh_type != SHT_SUNW_move)
3370 3429 continue;
3371 3430 if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
3372 3431 continue;
3373 3432
3374 3433 /*
3375 3434 * Determine the move data and number.
3376 3435 */
3377 3436 if ((shdr->sh_entsize == 0) || (shdr->sh_size == 0)) {
3378 3437 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3379 3438 file, _cache->c_name);
3380 3439 continue;
3381 3440 }
3382 3441 if (_cache->c_data == NULL)
3383 3442 continue;
3384 3443
3385 3444 move = (Move *)_cache->c_data->d_buf;
3386 3445 movenum = shdr->sh_size / shdr->sh_entsize;
3387 3446
3388 3447 /*
3389 3448 * Get the data buffer for the associated symbol table and
3390 3449 * string table.
3391 3450 */
3392 3451 if (stringtbl(cache, 1, cnt, shnum, file,
3393 3452 &symnum, &symsec, &strsec) == 0)
3394 3453 return;
3395 3454
3396 3455 syms = (Sym *)symsec->c_data->d_buf;
3397 3456
3398 3457 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3399 3458 dbg_print(0, MSG_INTL(MSG_ELF_SCN_MOVE), _cache->c_name);
3400 3459 dbg_print(0, MSG_INTL(MSG_MOVE_TITLE));
3401 3460
3402 3461 if (fmt == NULL)
3403 3462 fmt = MSG_INTL(MSG_MOVE_ENTRY);
3404 3463
3405 3464 for (ndx = 0; ndx < movenum; move++, ndx++) {
3406 3465 const char *symname;
3407 3466 char index[MAXNDXSIZE], section[BUFSIZ];
3408 3467 Word symndx, shndx;
3409 3468 Sym *sym;
3410 3469
3411 3470 /*
3412 3471 * Check for null entries
3413 3472 */
3414 3473 if ((move->m_info == 0) && (move->m_value == 0) &&
3415 3474 (move->m_poffset == 0) && (move->m_repeat == 0) &&
3416 3475 (move->m_stride == 0)) {
3417 3476 dbg_print(0, fmt, MSG_ORIG(MSG_STR_EMPTY),
3418 3477 EC_XWORD(move->m_poffset), 0, 0, 0,
3419 3478 EC_LWORD(0), MSG_ORIG(MSG_STR_EMPTY));
3420 3479 continue;
3421 3480 }
3422 3481 if (((symndx = ELF_M_SYM(move->m_info)) == 0) ||
3423 3482 (symndx >= symnum)) {
3424 3483 (void) fprintf(stderr,
3425 3484 MSG_INTL(MSG_ERR_BADMINFO), file,
3426 3485 _cache->c_name, EC_XWORD(move->m_info));
3427 3486
3428 3487 (void) snprintf(index, MAXNDXSIZE,
3429 3488 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
3430 3489 dbg_print(0, fmt, index,
3431 3490 EC_XWORD(move->m_poffset),
3432 3491 ELF_M_SIZE(move->m_info), move->m_repeat,
3433 3492 move->m_stride, move->m_value,
3434 3493 MSG_INTL(MSG_STR_UNKNOWN));
3435 3494 continue;
3436 3495 }
3437 3496
3438 3497 symname = relsymname(cache, _cache, strsec,
3439 3498 symndx, symnum, ndx, syms, section, BUFSIZ, file);
3440 3499 sym = (Sym *)(syms + symndx);
3441 3500
3442 3501 /*
3443 3502 * Additional sanity check.
3444 3503 */
3445 3504 shndx = sym->st_shndx;
3446 3505 if (!((shndx == SHN_COMMON) ||
3447 3506 (((shndx >= 1) && (shndx <= shnum)) &&
3448 3507 (cache[shndx].c_shdr)->sh_type == SHT_NOBITS))) {
3449 3508 (void) fprintf(stderr,
3450 3509 MSG_INTL(MSG_ERR_BADSYM2), file,
3451 3510 _cache->c_name, EC_WORD(symndx),
3452 3511 demangle(symname, flags));
3453 3512 }
3454 3513
3455 3514 (void) snprintf(index, MAXNDXSIZE,
3456 3515 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(symndx));
3457 3516 dbg_print(0, fmt, index, EC_XWORD(move->m_poffset),
3458 3517 ELF_M_SIZE(move->m_info), move->m_repeat,
3459 3518 move->m_stride, move->m_value,
3460 3519 demangle(symname, flags));
3461 3520 }
3462 3521 }
3463 3522 }
3464 3523
3465 3524 /*
3466 3525 * parse_note_t is used to track the state used by parse_note_entry()
3467 3526 * between calls, and also to return the results of each call.
3468 3527 */
3469 3528 typedef struct {
3470 3529 /* pns_ fields track progress through the data */
3471 3530 const char *pns_file; /* File name */
3472 3531 Cache *pns_cache; /* Note section cache entry */
3473 3532 size_t pns_size; /* # unprocessed data bytes */
3474 3533 Word *pns_data; /* # to next unused data byte */
3475 3534
3476 3535 /* pn_ fields return the results for a single call */
3477 3536 Word pn_namesz; /* Value of note namesz field */
3478 3537 Word pn_descsz; /* Value of note descsz field */
3479 3538 Word pn_type; /* Value of note type field */
3480 3539 const char *pn_name; /* if (namesz > 0) ptr to name bytes */
3481 3540 const char *pn_desc; /* if (descsx > 0) ptr to data bytes */
3482 3541 } parse_note_t;
3483 3542
3484 3543 /*
3485 3544 * Extract the various sub-parts of a note entry, and advance the
3486 3545 * data pointer past it.
3487 3546 *
3488 3547 * entry:
3489 3548 * The state pns_ fields contain current values for the Note section
3490 3549 *
3491 3550 * exit:
3492 3551 * On success, True (1) is returned, the state pns_ fields have been
3493 3552 * advanced to point at the start of the next entry, and the information
3494 3553 * for the recovered note entry is found in the state pn_ fields.
3495 3554 *
3496 3555 * On failure, False (0) is returned. The values contained in state
3497 3556 * are undefined.
3498 3557 */
3499 3558 static int
3500 3559 parse_note_entry(parse_note_t *state)
3501 3560 {
3502 3561 size_t pad, noteoff;
3503 3562
3504 3563 noteoff = (Word)state->pns_cache->c_data->d_size - state->pns_size;
3505 3564 /*
3506 3565 * Make sure we can at least reference the 3 initial entries
3507 3566 * (4-byte words) of the note information block.
3508 3567 */
3509 3568 if (state->pns_size >= (sizeof (Word) * 3)) {
3510 3569 state->pns_size -= (sizeof (Word) * 3);
3511 3570 } else {
3512 3571 (void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDATASZ),
3513 3572 state->pns_file, state->pns_cache->c_name,
3514 3573 EC_WORD(noteoff));
3515 3574 return (0);
3516 3575 }
3517 3576
3518 3577 /*
3519 3578 * Make sure any specified name string can be referenced.
3520 3579 */
3521 3580 if ((state->pn_namesz = *state->pns_data++) != 0) {
3522 3581 if (state->pns_size >= state->pn_namesz) {
3523 3582 state->pns_size -= state->pn_namesz;
3524 3583 } else {
3525 3584 (void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADNMSZ),
3526 3585 state->pns_file, state->pns_cache->c_name,
3527 3586 EC_WORD(noteoff), EC_WORD(state->pn_namesz));
3528 3587 return (0);
3529 3588 }
3530 3589 }
3531 3590
3532 3591 /*
3533 3592 * Make sure any specified descriptor can be referenced.
3534 3593 */
3535 3594 if ((state->pn_descsz = *state->pns_data++) != 0) {
3536 3595 /*
3537 3596 * If namesz isn't a 4-byte multiple, account for any
3538 3597 * padding that must exist before the descriptor.
3539 3598 */
3540 3599 if ((pad = (state->pn_namesz & (sizeof (Word) - 1))) != 0) {
3541 3600 pad = sizeof (Word) - pad;
3542 3601 state->pns_size -= pad;
3543 3602 }
3544 3603 if (state->pns_size >= state->pn_descsz) {
3545 3604 state->pns_size -= state->pn_descsz;
3546 3605 } else {
3547 3606 (void) fprintf(stderr, MSG_INTL(MSG_NOTE_BADDESZ),
3548 3607 state->pns_file, state->pns_cache->c_name,
3549 3608 EC_WORD(noteoff), EC_WORD(state->pn_namesz));
3550 3609 return (0);
3551 3610 }
3552 3611 }
3553 3612
3554 3613 state->pn_type = *state->pns_data++;
3555 3614
3556 3615 /* Name */
3557 3616 if (state->pn_namesz) {
3558 3617 state->pn_name = (char *)state->pns_data;
3559 3618 pad = (state->pn_namesz +
3560 3619 (sizeof (Word) - 1)) & ~(sizeof (Word) - 1);
3561 3620 /* LINTED */
3562 3621 state->pns_data = (Word *)(state->pn_name + pad);
3563 3622 }
3564 3623
3565 3624 /*
3566 3625 * If multiple information blocks exist within a .note section
3567 3626 * account for any padding that must exist before the next
3568 3627 * information block.
3569 3628 */
3570 3629 if ((pad = (state->pn_descsz & (sizeof (Word) - 1))) != 0) {
3571 3630 pad = sizeof (Word) - pad;
3572 3631 if (state->pns_size > pad)
3573 3632 state->pns_size -= pad;
3574 3633 }
3575 3634
3576 3635 /* Data */
3577 3636 if (state->pn_descsz) {
3578 3637 state->pn_desc = (const char *)state->pns_data;
3579 3638 /* LINTED */
3580 3639 state->pns_data = (Word *)(state->pn_desc +
3581 3640 state->pn_descsz + pad);
3582 3641 }
3583 3642
3584 3643 return (1);
3585 3644 }
3586 3645
3587 3646 /*
3588 3647 * Callback function for use with conv_str_to_c_literal() below.
3589 3648 */
3590 3649 /*ARGSUSED2*/
3591 3650 static void
3592 3651 c_literal_cb(const void *ptr, size_t size, void *uvalue)
3593 3652 {
3594 3653 (void) fwrite(ptr, size, 1, stdout);
3595 3654 }
3596 3655
3597 3656 /*
3598 3657 * Traverse a note section analyzing each note information block.
3599 3658 * The data buffers size is used to validate references before they are made,
3600 3659 * and is decremented as each element is processed.
3601 3660 */
3602 3661 void
3603 3662 note_entry(Cache *cache, Word *data, size_t size, Ehdr *ehdr, const char *file)
3604 3663 {
3605 3664 int cnt = 0;
3606 3665 int is_corenote;
3607 3666 int do_swap;
3608 3667 Conv_inv_buf_t inv_buf;
3609 3668 parse_note_t pnstate;
3610 3669
3611 3670 pnstate.pns_file = file;
3612 3671 pnstate.pns_cache = cache;
3613 3672 pnstate.pns_size = size;
3614 3673 pnstate.pns_data = data;
3615 3674 do_swap = _elf_sys_encoding() != ehdr->e_ident[EI_DATA];
3616 3675
3617 3676 /*
3618 3677 * Print out a single `note' information block.
3619 3678 */
3620 3679 while (pnstate.pns_size > 0) {
3621 3680
3622 3681 if (parse_note_entry(&pnstate) == 0)
3623 3682 return;
3624 3683
3625 3684 /*
3626 3685 * Is this a Solaris core note? Such notes all have
3627 3686 * the name "CORE".
3628 3687 */
3629 3688 is_corenote = (ehdr->e_type == ET_CORE) &&
3630 3689 (pnstate.pn_namesz == (MSG_STR_CORE_SIZE + 1)) &&
3631 3690 (strncmp(MSG_ORIG(MSG_STR_CORE), pnstate.pn_name,
3632 3691 MSG_STR_CORE_SIZE + 1) == 0);
3633 3692
3634 3693 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3635 3694 dbg_print(0, MSG_INTL(MSG_FMT_NOTEENTNDX), EC_WORD(cnt));
3636 3695 cnt++;
3637 3696 dbg_print(0, MSG_ORIG(MSG_NOTE_NAMESZ),
3638 3697 EC_WORD(pnstate.pn_namesz));
3639 3698 dbg_print(0, MSG_ORIG(MSG_NOTE_DESCSZ),
3640 3699 EC_WORD(pnstate.pn_descsz));
3641 3700
3642 3701 if (is_corenote)
3643 3702 dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE_STR),
3644 3703 conv_cnote_type(pnstate.pn_type, 0, &inv_buf));
3645 3704 else
3646 3705 dbg_print(0, MSG_ORIG(MSG_NOTE_TYPE),
3647 3706 EC_WORD(pnstate.pn_type));
3648 3707 if (pnstate.pn_namesz) {
3649 3708 dbg_print(0, MSG_ORIG(MSG_NOTE_NAME));
3650 3709 /*
3651 3710 * The name string can contain embedded 'null'
3652 3711 * bytes and/or unprintable characters. Also,
3653 3712 * the final NULL is documented in the ELF ABI
3654 3713 * as being included in the namesz. So, display
3655 3714 * the name using C literal string notation, and
3656 3715 * include the terminating NULL in the output.
3657 3716 * We don't show surrounding double quotes, as
3658 3717 * that implies the termination that we are showing
3659 3718 * explicitly.
3660 3719 */
3661 3720 (void) fwrite(MSG_ORIG(MSG_STR_8SP),
3662 3721 MSG_STR_8SP_SIZE, 1, stdout);
3663 3722 conv_str_to_c_literal(pnstate.pn_name,
3664 3723 pnstate.pn_namesz, c_literal_cb, NULL);
3665 3724 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3666 3725 }
3667 3726
3668 3727 if (pnstate.pn_descsz) {
3669 3728 int hexdump = 1;
3670 3729
3671 3730 /*
3672 3731 * If this is a core note, let the corenote()
3673 3732 * function handle it.
↓ open down ↓ |
2897 lines elided |
↑ open up ↑ |
3674 3733 */
3675 3734 if (is_corenote) {
3676 3735 /* We only issue the bad arch error once */
3677 3736 static int badnote_done = 0;
3678 3737 corenote_ret_t corenote_ret;
3679 3738
3680 3739 corenote_ret = corenote(ehdr->e_machine,
3681 3740 do_swap, pnstate.pn_type, pnstate.pn_desc,
3682 3741 pnstate.pn_descsz);
3683 3742 switch (corenote_ret) {
3743 + case CORENOTE_R_OK_DUMP:
3744 + hexdump = 1;
3745 + break;
3684 3746 case CORENOTE_R_OK:
3685 3747 hexdump = 0;
3686 3748 break;
3687 3749 case CORENOTE_R_BADDATA:
3688 3750 (void) fprintf(stderr,
3689 3751 MSG_INTL(MSG_NOTE_BADCOREDATA),
3690 3752 file);
3691 3753 break;
3692 3754 case CORENOTE_R_BADARCH:
3693 3755 if (badnote_done)
3694 3756 break;
3695 3757 (void) fprintf(stderr,
3696 3758 MSG_INTL(MSG_NOTE_BADCOREARCH),
3697 3759 file,
3698 3760 conv_ehdr_mach(ehdr->e_machine,
3699 3761 0, &inv_buf));
3700 3762 break;
3763 + case CORENOTE_R_BADTYPE:
3764 + (void) fprintf(stderr,
3765 + MSG_INTL(MSG_NOTE_BADCORETYPE),
3766 + file,
3767 + EC_WORD(pnstate.pn_type));
3768 + break;
3769 +
3701 3770 }
3702 3771 }
3703 3772
3704 3773 /*
3705 3774 * The default thing when we don't understand
3706 3775 * the note data is to display it as hex bytes.
3707 3776 */
3708 3777 if (hexdump) {
3709 3778 dbg_print(0, MSG_ORIG(MSG_NOTE_DESC));
3710 3779 dump_hex_bytes(pnstate.pn_desc,
3711 3780 pnstate.pn_descsz, 8, 4, 4);
3712 3781 }
3713 3782 }
3714 3783 }
3715 3784 }
3716 3785
3717 3786 /*
3718 3787 * Search for and process .note sections.
3719 3788 *
3720 3789 * Returns the number of note sections seen.
3721 3790 */
3722 3791 static Word
3723 3792 note(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
3724 3793 {
3725 3794 Word cnt, note_cnt = 0;
3726 3795
3727 3796 /*
3728 3797 * Otherwise look for any .note sections.
3729 3798 */
3730 3799 for (cnt = 1; cnt < shnum; cnt++) {
3731 3800 Cache *_cache = &cache[cnt];
3732 3801 Shdr *shdr = _cache->c_shdr;
3733 3802
3734 3803 if (shdr->sh_type != SHT_NOTE)
3735 3804 continue;
3736 3805 note_cnt++;
3737 3806 if (!match(MATCH_F_ALL, _cache->c_name, cnt, shdr->sh_type))
3738 3807 continue;
3739 3808
3740 3809 /*
3741 3810 * As these sections are often hand rolled, make sure they're
3742 3811 * properly aligned before proceeding, and issue an error
3743 3812 * as necessary.
3744 3813 *
3745 3814 * Note that we will continue on to display the note even
3746 3815 * if it has bad alignment. We can do this safely, because
3747 3816 * libelf knows the alignment required for SHT_NOTE, and
3748 3817 * takes steps to deliver a properly aligned buffer to us
3749 3818 * even if the actual file is misaligned.
3750 3819 */
3751 3820 if (shdr->sh_offset & (sizeof (Word) - 1))
3752 3821 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADALIGN),
3753 3822 file, _cache->c_name);
3754 3823
3755 3824 if (_cache->c_data == NULL)
3756 3825 continue;
3757 3826
3758 3827 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3759 3828 dbg_print(0, MSG_INTL(MSG_ELF_SCN_NOTE), _cache->c_name);
3760 3829 note_entry(_cache, (Word *)_cache->c_data->d_buf,
3761 3830 /* LINTED */
3762 3831 (Word)_cache->c_data->d_size, ehdr, file);
3763 3832 }
3764 3833
3765 3834 return (note_cnt);
3766 3835 }
3767 3836
3768 3837 /*
3769 3838 * The Linux Standard Base defines a special note named .note.ABI-tag
3770 3839 * that is used to maintain Linux ABI information. Presence of this section
3771 3840 * is a strong indication that the object should be considered to be
3772 3841 * ELFOSABI_LINUX.
3773 3842 *
3774 3843 * This function returns True (1) if such a note is seen, and False (0)
3775 3844 * otherwise.
3776 3845 */
3777 3846 static int
3778 3847 has_linux_abi_note(Cache *cache, Word shnum, const char *file)
3779 3848 {
3780 3849 Word cnt;
3781 3850
3782 3851 for (cnt = 1; cnt < shnum; cnt++) {
3783 3852 parse_note_t pnstate;
3784 3853 Cache *_cache = &cache[cnt];
3785 3854 Shdr *shdr = _cache->c_shdr;
3786 3855
3787 3856 /*
3788 3857 * Section must be SHT_NOTE, must have the name
3789 3858 * .note.ABI-tag, and must have data.
3790 3859 */
3791 3860 if ((shdr->sh_type != SHT_NOTE) ||
3792 3861 (strcmp(MSG_ORIG(MSG_STR_NOTEABITAG),
3793 3862 _cache->c_name) != 0) || (_cache->c_data == NULL))
3794 3863 continue;
3795 3864
3796 3865 pnstate.pns_file = file;
3797 3866 pnstate.pns_cache = _cache;
3798 3867 pnstate.pns_size = _cache->c_data->d_size;
3799 3868 pnstate.pns_data = (Word *)_cache->c_data->d_buf;
3800 3869
3801 3870 while (pnstate.pns_size > 0) {
3802 3871 Word *w;
3803 3872
3804 3873 if (parse_note_entry(&pnstate) == 0)
3805 3874 break;
3806 3875
3807 3876 /*
3808 3877 * The type must be 1, and the name must be "GNU".
3809 3878 * The descsz must be at least 16 bytes.
3810 3879 */
3811 3880 if ((pnstate.pn_type != 1) ||
3812 3881 (pnstate.pn_namesz != (MSG_STR_GNU_SIZE + 1)) ||
3813 3882 (strncmp(MSG_ORIG(MSG_STR_GNU), pnstate.pn_name,
3814 3883 MSG_STR_CORE_SIZE + 1) != 0) ||
3815 3884 (pnstate.pn_descsz < 16))
3816 3885 continue;
3817 3886
3818 3887 /*
3819 3888 * desc contains 4 32-bit fields. Field 0 must be 0,
3820 3889 * indicating Linux. The second, third, and fourth
3821 3890 * fields represent the earliest Linux kernel
3822 3891 * version compatible with this object.
3823 3892 */
3824 3893 /*LINTED*/
3825 3894 w = (Word *) pnstate.pn_desc;
3826 3895 if (*w == 0)
3827 3896 return (1);
3828 3897 }
3829 3898 }
3830 3899
3831 3900 return (0);
3832 3901 }
3833 3902
3834 3903 /*
3835 3904 * Determine an individual hash entry. This may be the initial hash entry,
3836 3905 * or an associated chain entry.
3837 3906 */
3838 3907 static void
3839 3908 hash_entry(Cache *refsec, Cache *strsec, const char *hsecname, Word hashndx,
3840 3909 Word symndx, Word symn, Sym *syms, const char *file, ulong_t bkts,
3841 3910 uint_t flags, int chain)
3842 3911 {
3843 3912 Sym *sym;
3844 3913 const char *symname, *str;
3845 3914 char _bucket[MAXNDXSIZE], _symndx[MAXNDXSIZE];
3846 3915 ulong_t nbkt, nhash;
3847 3916
3848 3917 if (symndx > symn) {
3849 3918 (void) fprintf(stderr, MSG_INTL(MSG_ERR_HSBADSYMNDX), file,
3850 3919 EC_WORD(symndx), EC_WORD(hashndx));
3851 3920 symname = MSG_INTL(MSG_STR_UNKNOWN);
3852 3921 } else {
3853 3922 sym = (Sym *)(syms + symndx);
3854 3923 symname = string(refsec, symndx, strsec, file, sym->st_name);
3855 3924 }
3856 3925
3857 3926 if (chain == 0) {
3858 3927 (void) snprintf(_bucket, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
3859 3928 hashndx);
3860 3929 str = (const char *)_bucket;
3861 3930 } else
3862 3931 str = MSG_ORIG(MSG_STR_EMPTY);
3863 3932
3864 3933 (void) snprintf(_symndx, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX2),
3865 3934 EC_WORD(symndx));
3866 3935 dbg_print(0, MSG_ORIG(MSG_FMT_HASH_INFO), str, _symndx,
3867 3936 demangle(symname, flags));
3868 3937
3869 3938 /*
3870 3939 * Determine if this string is in the correct bucket.
3871 3940 */
3872 3941 nhash = elf_hash(symname);
3873 3942 nbkt = nhash % bkts;
3874 3943
3875 3944 if (nbkt != hashndx) {
3876 3945 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADHASH), file,
3877 3946 hsecname, symname, EC_WORD(hashndx), nbkt);
3878 3947 }
3879 3948 }
3880 3949
3881 3950 #define MAXCOUNT 500
3882 3951
3883 3952 static void
3884 3953 hash(Cache *cache, Word shnum, const char *file, uint_t flags)
3885 3954 {
3886 3955 static int count[MAXCOUNT];
3887 3956 Word cnt;
3888 3957 ulong_t ndx, bkts;
3889 3958 char number[MAXNDXSIZE];
3890 3959
3891 3960 for (cnt = 1; cnt < shnum; cnt++) {
3892 3961 uint_t *hash, *chain;
3893 3962 Cache *_cache = &cache[cnt];
3894 3963 Shdr *sshdr, *hshdr = _cache->c_shdr;
3895 3964 char *ssecname, *hsecname = _cache->c_name;
3896 3965 Sym *syms;
3897 3966 Word symn;
3898 3967
3899 3968 if (hshdr->sh_type != SHT_HASH)
3900 3969 continue;
3901 3970
3902 3971 /*
3903 3972 * Determine the hash table data and size.
3904 3973 */
3905 3974 if ((hshdr->sh_entsize == 0) || (hshdr->sh_size == 0)) {
3906 3975 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3907 3976 file, hsecname);
3908 3977 continue;
3909 3978 }
3910 3979 if (_cache->c_data == NULL)
3911 3980 continue;
3912 3981
3913 3982 hash = (uint_t *)_cache->c_data->d_buf;
3914 3983 bkts = *hash;
3915 3984 chain = hash + 2 + bkts;
3916 3985 hash += 2;
3917 3986
3918 3987 /*
3919 3988 * Get the data buffer for the associated symbol table.
3920 3989 */
3921 3990 if ((hshdr->sh_link == 0) || (hshdr->sh_link >= shnum)) {
3922 3991 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3923 3992 file, hsecname, EC_WORD(hshdr->sh_link));
3924 3993 continue;
3925 3994 }
3926 3995
3927 3996 _cache = &cache[hshdr->sh_link];
3928 3997 ssecname = _cache->c_name;
3929 3998
3930 3999 if (_cache->c_data == NULL)
3931 4000 continue;
3932 4001
3933 4002 if ((syms = (Sym *)_cache->c_data->d_buf) == NULL) {
3934 4003 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
3935 4004 file, ssecname);
3936 4005 continue;
3937 4006 }
3938 4007
3939 4008 sshdr = _cache->c_shdr;
3940 4009 /* LINTED */
3941 4010 symn = (Word)(sshdr->sh_size / sshdr->sh_entsize);
3942 4011
3943 4012 /*
3944 4013 * Get the associated string table section.
3945 4014 */
3946 4015 if ((sshdr->sh_link == 0) || (sshdr->sh_link >= shnum)) {
3947 4016 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHLINK),
3948 4017 file, ssecname, EC_WORD(sshdr->sh_link));
3949 4018 continue;
3950 4019 }
3951 4020
3952 4021 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
3953 4022 dbg_print(0, MSG_INTL(MSG_ELF_SCN_HASH), hsecname);
3954 4023 dbg_print(0, MSG_INTL(MSG_ELF_HASH_INFO));
3955 4024
3956 4025 /*
3957 4026 * Loop through the hash buckets, printing the appropriate
3958 4027 * symbols.
3959 4028 */
3960 4029 for (ndx = 0; ndx < bkts; ndx++, hash++) {
3961 4030 Word _ndx, _cnt;
3962 4031
3963 4032 if (*hash == 0) {
3964 4033 count[0]++;
3965 4034 continue;
3966 4035 }
3967 4036
3968 4037 hash_entry(_cache, &cache[sshdr->sh_link], hsecname,
3969 4038 ndx, *hash, symn, syms, file, bkts, flags, 0);
3970 4039
3971 4040 /*
3972 4041 * Determine if any other symbols are chained to this
3973 4042 * bucket.
3974 4043 */
3975 4044 _ndx = chain[*hash];
3976 4045 _cnt = 1;
3977 4046 while (_ndx) {
3978 4047 hash_entry(_cache, &cache[sshdr->sh_link],
3979 4048 hsecname, ndx, _ndx, symn, syms, file,
3980 4049 bkts, flags, 1);
3981 4050 _ndx = chain[_ndx];
3982 4051 _cnt++;
3983 4052 }
3984 4053
3985 4054 if (_cnt >= MAXCOUNT) {
3986 4055 (void) fprintf(stderr,
3987 4056 MSG_INTL(MSG_HASH_OVERFLW), file,
3988 4057 _cache->c_name, EC_WORD(ndx),
3989 4058 EC_WORD(_cnt));
3990 4059 } else
3991 4060 count[_cnt]++;
3992 4061 }
3993 4062 break;
3994 4063 }
3995 4064
3996 4065 /*
3997 4066 * Print out the count information.
3998 4067 */
3999 4068 bkts = cnt = 0;
4000 4069 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4001 4070
4002 4071 for (ndx = 0; ndx < MAXCOUNT; ndx++) {
4003 4072 Word _cnt;
4004 4073
4005 4074 if ((_cnt = count[ndx]) == 0)
4006 4075 continue;
4007 4076
4008 4077 (void) snprintf(number, MAXNDXSIZE,
4009 4078 MSG_ORIG(MSG_FMT_INTEGER), _cnt);
4010 4079 dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS1), number,
4011 4080 EC_WORD(ndx));
4012 4081 bkts += _cnt;
4013 4082 cnt += (Word)(ndx * _cnt);
4014 4083 }
4015 4084 if (cnt) {
4016 4085 (void) snprintf(number, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INTEGER),
4017 4086 bkts);
4018 4087 dbg_print(0, MSG_INTL(MSG_ELF_HASH_BKTS2), number,
4019 4088 EC_WORD(cnt));
4020 4089 }
4021 4090 }
4022 4091
4023 4092 static void
4024 4093 group(Cache *cache, Word shnum, const char *file, uint_t flags)
4025 4094 {
4026 4095 Word scnt;
4027 4096
4028 4097 for (scnt = 1; scnt < shnum; scnt++) {
4029 4098 Cache *_cache = &cache[scnt];
4030 4099 Shdr *shdr = _cache->c_shdr;
4031 4100 Word *grpdata, gcnt, grpcnt, symnum, unknown;
4032 4101 Cache *symsec, *strsec;
4033 4102 Sym *syms, *sym;
4034 4103 char flgstrbuf[MSG_GRP_COMDAT_SIZE + 10];
4035 4104 const char *grpnam;
4036 4105
4037 4106 if (shdr->sh_type != SHT_GROUP)
4038 4107 continue;
4039 4108 if (!match(MATCH_F_ALL, _cache->c_name, scnt, shdr->sh_type))
4040 4109 continue;
4041 4110 if ((_cache->c_data == NULL) ||
4042 4111 ((grpdata = (Word *)_cache->c_data->d_buf) == NULL))
4043 4112 continue;
4044 4113 grpcnt = shdr->sh_size / sizeof (Word);
4045 4114
4046 4115 /*
4047 4116 * Get the data buffer for the associated symbol table and
4048 4117 * string table.
4049 4118 */
4050 4119 if (stringtbl(cache, 1, scnt, shnum, file,
4051 4120 &symnum, &symsec, &strsec) == 0)
4052 4121 return;
4053 4122
4054 4123 syms = symsec->c_data->d_buf;
4055 4124
4056 4125 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4057 4126 dbg_print(0, MSG_INTL(MSG_ELF_SCN_GRP), _cache->c_name);
4058 4127 dbg_print(0, MSG_INTL(MSG_GRP_TITLE));
4059 4128
4060 4129 /*
4061 4130 * The first element of the group defines the group. The
4062 4131 * associated symbol is defined by the sh_link field.
4063 4132 */
4064 4133 if ((shdr->sh_info == SHN_UNDEF) || (shdr->sh_info > symnum)) {
4065 4134 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHINFO),
4066 4135 file, _cache->c_name, EC_WORD(shdr->sh_info));
4067 4136 return;
4068 4137 }
4069 4138
4070 4139 (void) strcpy(flgstrbuf, MSG_ORIG(MSG_STR_OSQBRKT));
4071 4140 if (grpdata[0] & GRP_COMDAT) {
4072 4141 (void) strcat(flgstrbuf, MSG_ORIG(MSG_GRP_COMDAT));
4073 4142 }
4074 4143 if ((unknown = (grpdata[0] & ~GRP_COMDAT)) != 0) {
4075 4144 size_t len = strlen(flgstrbuf);
4076 4145
4077 4146 (void) snprintf(&flgstrbuf[len],
4078 4147 (MSG_GRP_COMDAT_SIZE + 10 - len),
4079 4148 MSG_ORIG(MSG_GRP_UNKNOWN), unknown);
4080 4149 }
4081 4150 (void) strcat(flgstrbuf, MSG_ORIG(MSG_STR_CSQBRKT));
4082 4151 sym = (Sym *)(syms + shdr->sh_info);
4083 4152
4084 4153 /*
4085 4154 * The GNU assembler can use section symbols as the signature
4086 4155 * symbol as described by this comment in the gold linker
4087 4156 * (found via google):
4088 4157 *
4089 4158 * It seems that some versions of gas will create a
4090 4159 * section group associated with a section symbol, and
4091 4160 * then fail to give a name to the section symbol. In
4092 4161 * such a case, use the name of the section.
4093 4162 *
4094 4163 * In order to support such objects, we do the same.
4095 4164 */
4096 4165 grpnam = string(_cache, 0, strsec, file, sym->st_name);
4097 4166 if (((sym->st_name == 0) || (*grpnam == '\0')) &&
4098 4167 (ELF_ST_TYPE(sym->st_info) == STT_SECTION))
4099 4168 grpnam = cache[sym->st_shndx].c_name;
4100 4169
4101 4170 dbg_print(0, MSG_INTL(MSG_GRP_SIGNATURE), flgstrbuf,
4102 4171 demangle(grpnam, flags));
4103 4172
4104 4173 for (gcnt = 1; gcnt < grpcnt; gcnt++) {
4105 4174 char index[MAXNDXSIZE];
4106 4175 const char *name;
4107 4176
4108 4177 (void) snprintf(index, MAXNDXSIZE,
4109 4178 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(gcnt));
4110 4179
4111 4180 if (grpdata[gcnt] >= shnum)
4112 4181 name = MSG_INTL(MSG_GRP_INVALSCN);
4113 4182 else
4114 4183 name = cache[grpdata[gcnt]].c_name;
4115 4184
4116 4185 (void) printf(MSG_ORIG(MSG_GRP_ENTRY), index, name,
4117 4186 EC_XWORD(grpdata[gcnt]));
4118 4187 }
4119 4188 }
4120 4189 }
4121 4190
4122 4191 static void
4123 4192 got(Cache *cache, Word shnum, Ehdr *ehdr, const char *file)
4124 4193 {
4125 4194 Cache *gotcache = NULL, *symtab = NULL;
4126 4195 Addr gotbgn, gotend;
4127 4196 Shdr *gotshdr;
4128 4197 Word cnt, gotents, gotndx;
4129 4198 size_t gentsize;
4130 4199 Got_info *gottable;
4131 4200 char *gotdata;
4132 4201 Sym *gotsym;
4133 4202 Xword gotsymaddr;
4134 4203 uint_t sys_encoding;
4135 4204
4136 4205 /*
4137 4206 * First, find the got.
4138 4207 */
4139 4208 for (cnt = 1; cnt < shnum; cnt++) {
4140 4209 if (strncmp(cache[cnt].c_name, MSG_ORIG(MSG_ELF_GOT),
4141 4210 MSG_ELF_GOT_SIZE) == 0) {
4142 4211 gotcache = &cache[cnt];
4143 4212 break;
4144 4213 }
4145 4214 }
4146 4215 if (gotcache == NULL)
4147 4216 return;
4148 4217
4149 4218 /*
4150 4219 * A got section within a relocatable object is suspicious.
4151 4220 */
4152 4221 if (ehdr->e_type == ET_REL) {
4153 4222 (void) fprintf(stderr, MSG_INTL(MSG_GOT_UNEXPECTED), file,
4154 4223 gotcache->c_name);
4155 4224 }
4156 4225
4157 4226 gotshdr = gotcache->c_shdr;
4158 4227 if (gotshdr->sh_size == 0) {
4159 4228 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
4160 4229 file, gotcache->c_name);
4161 4230 return;
4162 4231 }
4163 4232
4164 4233 gotbgn = gotshdr->sh_addr;
4165 4234 gotend = gotbgn + gotshdr->sh_size;
4166 4235
4167 4236 /*
4168 4237 * Some architectures don't properly set the sh_entsize for the GOT
4169 4238 * table. If it's not set, default to a size of a pointer.
4170 4239 */
4171 4240 if ((gentsize = gotshdr->sh_entsize) == 0)
4172 4241 gentsize = sizeof (Xword);
4173 4242
4174 4243 if (gotcache->c_data == NULL)
4175 4244 return;
4176 4245
4177 4246 /* LINTED */
4178 4247 gotents = (Word)(gotshdr->sh_size / gentsize);
4179 4248 gotdata = gotcache->c_data->d_buf;
4180 4249
4181 4250 if ((gottable = calloc(gotents, sizeof (Got_info))) == 0) {
4182 4251 int err = errno;
4183 4252 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC), file,
4184 4253 strerror(err));
4185 4254 return;
4186 4255 }
4187 4256
4188 4257 /*
4189 4258 * Now we scan through all the sections looking for any relocations
4190 4259 * that may be against the GOT. Since these may not be isolated to a
4191 4260 * .rel[a].got section we check them all.
4192 4261 * While scanning sections save the symbol table entry (a symtab
4193 4262 * overriding a dynsym) so that we can lookup _GLOBAL_OFFSET_TABLE_.
4194 4263 */
4195 4264 for (cnt = 1; cnt < shnum; cnt++) {
4196 4265 Word type, symnum;
4197 4266 Xword relndx, relnum, relsize;
4198 4267 void *rels;
4199 4268 Sym *syms;
4200 4269 Cache *symsec, *strsec;
4201 4270 Cache *_cache = &cache[cnt];
4202 4271 Shdr *shdr;
4203 4272
4204 4273 shdr = _cache->c_shdr;
4205 4274 type = shdr->sh_type;
4206 4275
4207 4276 if ((symtab == 0) && (type == SHT_DYNSYM)) {
4208 4277 symtab = _cache;
4209 4278 continue;
4210 4279 }
4211 4280 if (type == SHT_SYMTAB) {
4212 4281 symtab = _cache;
4213 4282 continue;
4214 4283 }
4215 4284 if ((type != SHT_RELA) && (type != SHT_REL))
4216 4285 continue;
4217 4286
4218 4287 /*
4219 4288 * Decide entry size.
4220 4289 */
4221 4290 if (((relsize = shdr->sh_entsize) == 0) ||
4222 4291 (relsize > shdr->sh_size)) {
4223 4292 if (type == SHT_RELA)
4224 4293 relsize = sizeof (Rela);
4225 4294 else
4226 4295 relsize = sizeof (Rel);
4227 4296 }
4228 4297
4229 4298 /*
4230 4299 * Determine the number of relocations available.
4231 4300 */
4232 4301 if (shdr->sh_size == 0) {
4233 4302 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSZ),
4234 4303 file, _cache->c_name);
4235 4304 continue;
4236 4305 }
4237 4306 if (_cache->c_data == NULL)
4238 4307 continue;
4239 4308
4240 4309 rels = _cache->c_data->d_buf;
4241 4310 relnum = shdr->sh_size / relsize;
4242 4311
4243 4312 /*
4244 4313 * Get the data buffer for the associated symbol table and
4245 4314 * string table.
4246 4315 */
4247 4316 if (stringtbl(cache, 1, cnt, shnum, file,
4248 4317 &symnum, &symsec, &strsec) == 0)
4249 4318 continue;
4250 4319
4251 4320 syms = symsec->c_data->d_buf;
4252 4321
4253 4322 /*
4254 4323 * Loop through the relocation entries.
4255 4324 */
4256 4325 for (relndx = 0; relndx < relnum; relndx++,
4257 4326 rels = (void *)((char *)rels + relsize)) {
4258 4327 char section[BUFSIZ];
4259 4328 Addr offset;
4260 4329 Got_info *gip;
4261 4330 Word symndx, reltype;
4262 4331 Rela *rela;
4263 4332 Rel *rel;
4264 4333
4265 4334 /*
4266 4335 * Unravel the relocation.
4267 4336 */
4268 4337 if (type == SHT_RELA) {
4269 4338 rela = (Rela *)rels;
4270 4339 symndx = ELF_R_SYM(rela->r_info);
4271 4340 reltype = ELF_R_TYPE(rela->r_info,
4272 4341 ehdr->e_machine);
4273 4342 offset = rela->r_offset;
4274 4343 } else {
4275 4344 rel = (Rel *)rels;
4276 4345 symndx = ELF_R_SYM(rel->r_info);
4277 4346 reltype = ELF_R_TYPE(rel->r_info,
4278 4347 ehdr->e_machine);
4279 4348 offset = rel->r_offset;
4280 4349 }
4281 4350
4282 4351 /*
4283 4352 * Only pay attention to relocations against the GOT.
4284 4353 */
4285 4354 if ((offset < gotbgn) || (offset >= gotend))
4286 4355 continue;
4287 4356
4288 4357 /* LINTED */
4289 4358 gotndx = (Word)((offset - gotbgn) /
4290 4359 gotshdr->sh_entsize);
4291 4360 gip = &gottable[gotndx];
4292 4361
4293 4362 if (gip->g_reltype != 0) {
4294 4363 (void) fprintf(stderr,
4295 4364 MSG_INTL(MSG_GOT_MULTIPLE), file,
4296 4365 EC_WORD(gotndx), EC_ADDR(offset));
4297 4366 continue;
4298 4367 }
4299 4368
4300 4369 if (symndx)
4301 4370 gip->g_symname = relsymname(cache, _cache,
4302 4371 strsec, symndx, symnum, relndx, syms,
4303 4372 section, BUFSIZ, file);
4304 4373 gip->g_reltype = reltype;
4305 4374 gip->g_rel = rels;
4306 4375 }
4307 4376 }
4308 4377
4309 4378 if (symlookup(MSG_ORIG(MSG_SYM_GOT), cache, shnum, &gotsym, NULL,
4310 4379 symtab, file))
4311 4380 gotsymaddr = gotsym->st_value;
4312 4381 else
4313 4382 gotsymaddr = gotbgn;
4314 4383
4315 4384 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4316 4385 dbg_print(0, MSG_INTL(MSG_ELF_SCN_GOT), gotcache->c_name);
4317 4386 Elf_got_title(0);
4318 4387
4319 4388 sys_encoding = _elf_sys_encoding();
4320 4389 for (gotndx = 0; gotndx < gotents; gotndx++) {
4321 4390 Got_info *gip;
4322 4391 Sword gindex;
4323 4392 Addr gaddr;
4324 4393 Xword gotentry;
4325 4394
4326 4395 gip = &gottable[gotndx];
4327 4396
4328 4397 gaddr = gotbgn + (gotndx * gentsize);
4329 4398 gindex = (Sword)(gaddr - gotsymaddr) / (Sword)gentsize;
4330 4399
4331 4400 if (gentsize == sizeof (Word))
4332 4401 /* LINTED */
4333 4402 gotentry = (Xword)(*((Word *)(gotdata) + gotndx));
4334 4403 else
4335 4404 /* LINTED */
4336 4405 gotentry = *((Xword *)(gotdata) + gotndx);
4337 4406
4338 4407 Elf_got_entry(0, gindex, gaddr, gotentry, ehdr->e_machine,
4339 4408 ehdr->e_ident[EI_DATA], sys_encoding,
4340 4409 gip->g_reltype, gip->g_rel, gip->g_symname);
4341 4410 }
4342 4411 free(gottable);
4343 4412 }
4344 4413
4345 4414 void
4346 4415 checksum(Elf *elf)
4347 4416 {
4348 4417 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4349 4418 dbg_print(0, MSG_INTL(MSG_STR_CHECKSUM), elf_checksum(elf));
4350 4419 }
4351 4420
4352 4421 /*
4353 4422 * This variable is used by regular() to communicate the address of
4354 4423 * the section header cache to sort_shdr_ndx_arr(). Unfortunately,
4355 4424 * the qsort() interface does not include a userdata argument by which
4356 4425 * such arbitrary data can be passed, so we are stuck using global data.
4357 4426 */
4358 4427 static Cache *sort_shdr_ndx_arr_cache;
4359 4428
4360 4429
4361 4430 /*
4362 4431 * Used with qsort() to sort the section indices so that they can be
4363 4432 * used to access the section headers in order of increasing data offset.
4364 4433 *
4365 4434 * entry:
4366 4435 * sort_shdr_ndx_arr_cache - Contains address of
4367 4436 * section header cache.
4368 4437 * v1, v2 - Point at elements of sort_shdr_bits array to be compared.
4369 4438 *
4370 4439 * exit:
4371 4440 * Returns -1 (less than), 0 (equal) or 1 (greater than).
4372 4441 */
4373 4442 static int
4374 4443 sort_shdr_ndx_arr(const void *v1, const void *v2)
4375 4444 {
4376 4445 Cache *cache1 = sort_shdr_ndx_arr_cache + *((size_t *)v1);
4377 4446 Cache *cache2 = sort_shdr_ndx_arr_cache + *((size_t *)v2);
4378 4447
4379 4448 if (cache1->c_shdr->sh_offset < cache2->c_shdr->sh_offset)
4380 4449 return (-1);
4381 4450
4382 4451 if (cache1->c_shdr->sh_offset > cache2->c_shdr->sh_offset)
4383 4452 return (1);
4384 4453
4385 4454 return (0);
4386 4455 }
4387 4456
4388 4457
4389 4458 static int
4390 4459 shdr_cache(const char *file, Elf *elf, Ehdr *ehdr, size_t shstrndx,
4391 4460 size_t shnum, Cache **cache_ret, Word flags)
4392 4461 {
4393 4462 Elf_Scn *scn;
4394 4463 Elf_Data *data;
4395 4464 size_t ndx;
4396 4465 Shdr *nameshdr;
4397 4466 char *names = NULL;
4398 4467 Cache *cache, *_cache;
4399 4468 size_t *shdr_ndx_arr, shdr_ndx_arr_cnt;
4400 4469
4401 4470
4402 4471 /*
4403 4472 * Obtain the .shstrtab data buffer to provide the required section
4404 4473 * name strings.
4405 4474 */
4406 4475 if (shstrndx == SHN_UNDEF) {
4407 4476 /*
4408 4477 * It is rare, but legal, for an object to lack a
4409 4478 * header string table section.
4410 4479 */
4411 4480 names = NULL;
4412 4481 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHSTRSEC), file);
4413 4482 } else if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
4414 4483 failure(file, MSG_ORIG(MSG_ELF_GETSCN));
4415 4484 (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SHDR),
4416 4485 EC_XWORD(shstrndx));
4417 4486
4418 4487 } else if ((data = elf_getdata(scn, NULL)) == NULL) {
4419 4488 failure(file, MSG_ORIG(MSG_ELF_GETDATA));
4420 4489 (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_DATA),
4421 4490 EC_XWORD(shstrndx));
4422 4491
4423 4492 } else if ((nameshdr = elf_getshdr(scn)) == NULL) {
4424 4493 failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
4425 4494 (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4426 4495 EC_WORD(elf_ndxscn(scn)));
4427 4496
4428 4497 } else if ((names = data->d_buf) == NULL)
4429 4498 (void) fprintf(stderr, MSG_INTL(MSG_ERR_SHSTRNULL), file);
4430 4499
4431 4500 /*
4432 4501 * Allocate a cache to maintain a descriptor for each section.
4433 4502 */
4434 4503 if ((*cache_ret = cache = malloc(shnum * sizeof (Cache))) == NULL) {
4435 4504 int err = errno;
4436 4505 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
4437 4506 file, strerror(err));
4438 4507 return (0);
4439 4508 }
4440 4509
4441 4510 *cache = cache_init;
4442 4511 _cache = cache;
4443 4512 _cache++;
4444 4513
4445 4514 /*
4446 4515 * Allocate an array that will hold the section index for
4447 4516 * each section that has data in the ELF file:
4448 4517 *
4449 4518 * - Is not a NOBITS section
4450 4519 * - Data has non-zero length
4451 4520 *
4452 4521 * Note that shnum is an upper bound on the size required. It
4453 4522 * is likely that we won't use a few of these array elements.
4454 4523 * Allocating a modest amount of extra memory in this case means
4455 4524 * that we can avoid an extra loop to count the number of needed
4456 4525 * items, and can fill this array immediately in the first loop
4457 4526 * below.
4458 4527 */
4459 4528 if ((shdr_ndx_arr = malloc(shnum * sizeof (*shdr_ndx_arr))) == NULL) {
4460 4529 int err = errno;
4461 4530 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
4462 4531 file, strerror(err));
4463 4532 return (0);
4464 4533 }
4465 4534 shdr_ndx_arr_cnt = 0;
4466 4535
4467 4536 /*
4468 4537 * Traverse the sections of the file. This gathering of data is
4469 4538 * carried out in two passes. First, the section headers are captured
4470 4539 * and the section header names are evaluated. A verification pass is
4471 4540 * then carried out over the section information. Files have been
4472 4541 * known to exhibit overlapping (and hence erroneous) section header
4473 4542 * information.
4474 4543 *
4475 4544 * Finally, the data for each section is obtained. This processing is
4476 4545 * carried out after section verification because should any section
4477 4546 * header overlap occur, and a file needs translating (ie. xlate'ing
4478 4547 * information from a non-native architecture file), then the process
4479 4548 * of translation can corrupt the section header information. Of
4480 4549 * course, if there is any section overlap, the data related to the
4481 4550 * sections is going to be compromised. However, it is the translation
4482 4551 * of this data that has caused problems with elfdump()'s ability to
4483 4552 * extract the data.
4484 4553 */
4485 4554 for (ndx = 1, scn = NULL; scn = elf_nextscn(elf, scn);
4486 4555 ndx++, _cache++) {
4487 4556 char scnndxnm[100];
4488 4557
4489 4558 _cache->c_ndx = ndx;
4490 4559 _cache->c_scn = scn;
4491 4560
4492 4561 if ((_cache->c_shdr = elf_getshdr(scn)) == NULL) {
4493 4562 failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
4494 4563 (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN),
4495 4564 EC_WORD(elf_ndxscn(scn)));
4496 4565 }
4497 4566
4498 4567 /*
4499 4568 * If this section has data in the file, include it in
4500 4569 * the array of sections to check for address overlap.
4501 4570 */
4502 4571 if ((_cache->c_shdr->sh_size != 0) &&
4503 4572 (_cache->c_shdr->sh_type != SHT_NOBITS))
4504 4573 shdr_ndx_arr[shdr_ndx_arr_cnt++] = ndx;
4505 4574
4506 4575 /*
4507 4576 * If a shstrtab exists, assign the section name.
4508 4577 */
4509 4578 if (names && _cache->c_shdr) {
4510 4579 if (_cache->c_shdr->sh_name &&
4511 4580 /* LINTED */
4512 4581 (nameshdr->sh_size > _cache->c_shdr->sh_name)) {
4513 4582 const char *symname;
4514 4583 char *secname;
4515 4584
4516 4585 secname = names + _cache->c_shdr->sh_name;
4517 4586
4518 4587 /*
4519 4588 * A SUN naming convention employs a "%" within
4520 4589 * a section name to indicate a section/symbol
4521 4590 * name. This originated from the compilers
4522 4591 * -xF option, that places functions into their
4523 4592 * own sections. This convention (which has no
4524 4593 * formal standard) has also been followed for
4525 4594 * COMDAT sections. To demangle the symbol
4526 4595 * name, the name must be separated from the
4527 4596 * section name.
4528 4597 */
4529 4598 if (((flags & FLG_CTL_DEMANGLE) == 0) ||
4530 4599 ((symname = strchr(secname, '%')) == NULL))
4531 4600 _cache->c_name = secname;
4532 4601 else {
4533 4602 size_t secsz = ++symname - secname;
4534 4603 size_t strsz;
4535 4604
4536 4605 symname = demangle(symname, flags);
4537 4606 strsz = secsz + strlen(symname) + 1;
4538 4607
4539 4608 if ((_cache->c_name =
4540 4609 malloc(strsz)) == NULL) {
4541 4610 int err = errno;
4542 4611 (void) fprintf(stderr,
4543 4612 MSG_INTL(MSG_ERR_MALLOC),
4544 4613 file, strerror(err));
4545 4614 return (0);
4546 4615 }
4547 4616 (void) snprintf(_cache->c_name, strsz,
4548 4617 MSG_ORIG(MSG_FMT_SECSYM),
4549 4618 EC_WORD(secsz), secname, symname);
4550 4619 }
4551 4620
4552 4621 continue;
4553 4622 }
4554 4623
4555 4624 /*
4556 4625 * Generate an error if the section name index is zero
4557 4626 * or exceeds the shstrtab data. Fall through to
4558 4627 * fabricate a section name.
4559 4628 */
4560 4629 if ((_cache->c_shdr->sh_name == 0) ||
4561 4630 /* LINTED */
4562 4631 (nameshdr->sh_size <= _cache->c_shdr->sh_name)) {
4563 4632 (void) fprintf(stderr,
4564 4633 MSG_INTL(MSG_ERR_BADSHNAME), file,
4565 4634 EC_WORD(ndx),
4566 4635 EC_XWORD(_cache->c_shdr->sh_name));
4567 4636 }
4568 4637 }
4569 4638
4570 4639 /*
4571 4640 * If there exists no shstrtab data, or a section header has no
4572 4641 * name (an invalid index of 0), then compose a name for the
4573 4642 * section.
4574 4643 */
4575 4644 (void) snprintf(scnndxnm, sizeof (scnndxnm),
4576 4645 MSG_INTL(MSG_FMT_SCNNDX), ndx);
4577 4646
4578 4647 if ((_cache->c_name = malloc(strlen(scnndxnm) + 1)) == NULL) {
4579 4648 int err = errno;
4580 4649 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALLOC),
4581 4650 file, strerror(err));
4582 4651 return (0);
4583 4652 }
4584 4653 (void) strcpy(_cache->c_name, scnndxnm);
4585 4654 }
4586 4655
4587 4656 /*
4588 4657 * Having collected all the sections, validate their address range.
4589 4658 * Cases have existed where the section information has been invalid.
4590 4659 * This can lead to all sorts of other, hard to diagnose errors, as
4591 4660 * each section is processed individually (ie. with elf_getdata()).
4592 4661 * Here, we carry out some address comparisons to catch a family of
4593 4662 * overlapping memory issues we have observed (likely, there are others
4594 4663 * that we have yet to discover).
4595 4664 *
4596 4665 * Note, should any memory overlap occur, obtaining any additional
4597 4666 * data from the file is questionable. However, it might still be
4598 4667 * possible to inspect the ELF header, Programs headers, or individual
4599 4668 * sections, so rather than bailing on an error condition, continue
4600 4669 * processing to see if any data can be salvaged.
4601 4670 */
4602 4671 if (shdr_ndx_arr_cnt > 1) {
4603 4672 sort_shdr_ndx_arr_cache = cache;
4604 4673 qsort(shdr_ndx_arr, shdr_ndx_arr_cnt,
4605 4674 sizeof (*shdr_ndx_arr), sort_shdr_ndx_arr);
4606 4675 }
4607 4676 for (ndx = 0; ndx < shdr_ndx_arr_cnt; ndx++) {
4608 4677 Cache *_cache = cache + shdr_ndx_arr[ndx];
4609 4678 Shdr *shdr = _cache->c_shdr;
4610 4679 Off bgn1, bgn = shdr->sh_offset;
4611 4680 Off end1, end = shdr->sh_offset + shdr->sh_size;
4612 4681 size_t ndx1;
4613 4682
4614 4683 /*
4615 4684 * Check the section against all following ones, reporting
4616 4685 * any overlaps. Since we've sorted the sections by offset,
4617 4686 * we can stop after the first comparison that fails. There
4618 4687 * are no overlaps in a properly formed ELF file, in which
4619 4688 * case this algorithm runs in O(n) time. This will degenerate
4620 4689 * to O(n^2) for a completely broken file. Such a file is
4621 4690 * (1) highly unlikely, and (2) unusable, so it is reasonable
4622 4691 * for the analysis to take longer.
4623 4692 */
4624 4693 for (ndx1 = ndx + 1; ndx1 < shdr_ndx_arr_cnt; ndx1++) {
4625 4694 Cache *_cache1 = cache + shdr_ndx_arr[ndx1];
4626 4695 Shdr *shdr1 = _cache1->c_shdr;
4627 4696
4628 4697 bgn1 = shdr1->sh_offset;
4629 4698 end1 = shdr1->sh_offset + shdr1->sh_size;
4630 4699
4631 4700 if (((bgn1 <= bgn) && (end1 > bgn)) ||
4632 4701 ((bgn1 < end) && (end1 >= end))) {
4633 4702 (void) fprintf(stderr,
4634 4703 MSG_INTL(MSG_ERR_SECMEMOVER), file,
4635 4704 EC_WORD(elf_ndxscn(_cache->c_scn)),
4636 4705 _cache->c_name, EC_OFF(bgn), EC_OFF(end),
4637 4706 EC_WORD(elf_ndxscn(_cache1->c_scn)),
4638 4707 _cache1->c_name, EC_OFF(bgn1),
4639 4708 EC_OFF(end1));
4640 4709 } else { /* No overlap, so can stop */
4641 4710 break;
4642 4711 }
4643 4712 }
4644 4713
4645 4714 /*
4646 4715 * In addition to checking for sections overlapping
4647 4716 * each other (done above), we should also make sure
4648 4717 * the section doesn't overlap the section header array.
4649 4718 */
4650 4719 bgn1 = ehdr->e_shoff;
4651 4720 end1 = ehdr->e_shoff + (ehdr->e_shentsize * ehdr->e_shnum);
4652 4721
4653 4722 if (((bgn1 <= bgn) && (end1 > bgn)) ||
4654 4723 ((bgn1 < end) && (end1 >= end))) {
4655 4724 (void) fprintf(stderr,
4656 4725 MSG_INTL(MSG_ERR_SHDRMEMOVER), file, EC_OFF(bgn1),
4657 4726 EC_OFF(end1),
4658 4727 EC_WORD(elf_ndxscn(_cache->c_scn)),
4659 4728 _cache->c_name, EC_OFF(bgn), EC_OFF(end));
4660 4729 }
4661 4730 }
4662 4731
4663 4732 /*
4664 4733 * Obtain the data for each section.
4665 4734 */
4666 4735 for (ndx = 1; ndx < shnum; ndx++) {
4667 4736 Cache *_cache = &cache[ndx];
4668 4737 Elf_Scn *scn = _cache->c_scn;
4669 4738
4670 4739 if ((_cache->c_data = elf_getdata(scn, NULL)) == NULL) {
4671 4740 failure(file, MSG_ORIG(MSG_ELF_GETDATA));
4672 4741 (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCNDATA),
4673 4742 EC_WORD(elf_ndxscn(scn)));
4674 4743 }
4675 4744
4676 4745 /*
4677 4746 * If a string table, verify that it has NULL first and
4678 4747 * final bytes.
4679 4748 */
4680 4749 if ((_cache->c_shdr->sh_type == SHT_STRTAB) &&
4681 4750 (_cache->c_data->d_buf != NULL) &&
4682 4751 (_cache->c_data->d_size > 0)) {
4683 4752 const char *s = _cache->c_data->d_buf;
4684 4753
4685 4754 if ((*s != '\0') ||
4686 4755 (*(s + _cache->c_data->d_size - 1) != '\0'))
4687 4756 (void) fprintf(stderr, MSG_INTL(MSG_ERR_MALSTR),
4688 4757 file, _cache->c_name);
4689 4758 }
4690 4759 }
4691 4760
4692 4761 return (1);
4693 4762 }
4694 4763
4695 4764
4696 4765
4697 4766 /*
4698 4767 * Generate a cache of section headers and related information
4699 4768 * for use by the rest of elfdump. If requested (or the file
4700 4769 * contains no section headers), we generate a fake set of
4701 4770 * headers from the information accessible from the program headers.
4702 4771 * Otherwise, we use the real section headers contained in the file.
4703 4772 */
4704 4773 static int
4705 4774 create_cache(const char *file, int fd, Elf *elf, Ehdr *ehdr, Cache **cache,
4706 4775 size_t shstrndx, size_t *shnum, uint_t *flags)
4707 4776 {
4708 4777 /*
4709 4778 * If there are no section headers, then resort to synthesizing
4710 4779 * section headers from the program headers. This is normally
4711 4780 * only done by explicit request, but in this case there's no
4712 4781 * reason not to go ahead, since the alternative is simply to quit.
4713 4782 */
4714 4783 if ((*shnum <= 1) && ((*flags & FLG_CTL_FAKESHDR) == 0)) {
4715 4784 (void) fprintf(stderr, MSG_INTL(MSG_ERR_NOSHDR), file);
4716 4785 *flags |= FLG_CTL_FAKESHDR;
4717 4786 }
4718 4787
4719 4788 if (*flags & FLG_CTL_FAKESHDR) {
4720 4789 if (fake_shdr_cache(file, fd, elf, ehdr, cache, shnum) == 0)
4721 4790 return (0);
4722 4791 } else {
4723 4792 if (shdr_cache(file, elf, ehdr, shstrndx, *shnum,
4724 4793 cache, *flags) == 0)
4725 4794 return (0);
4726 4795 }
4727 4796
4728 4797 return (1);
4729 4798 }
4730 4799
4731 4800 int
4732 4801 regular(const char *file, int fd, Elf *elf, uint_t flags,
4733 4802 const char *wname, int wfd, uchar_t osabi)
4734 4803 {
4735 4804 enum { CACHE_NEEDED, CACHE_OK, CACHE_FAIL} cache_state = CACHE_NEEDED;
4736 4805 Elf_Scn *scn;
4737 4806 Ehdr *ehdr;
4738 4807 size_t ndx, shstrndx, shnum, phnum;
4739 4808 Shdr *shdr;
4740 4809 Cache *cache;
4741 4810 VERSYM_STATE versym = { 0 };
4742 4811 int ret = 0;
4743 4812 int addr_align;
4744 4813
4745 4814 if ((ehdr = elf_getehdr(elf)) == NULL) {
4746 4815 failure(file, MSG_ORIG(MSG_ELF_GETEHDR));
4747 4816 return (ret);
4748 4817 }
4749 4818
4750 4819 if (elf_getshdrnum(elf, &shnum) == -1) {
4751 4820 failure(file, MSG_ORIG(MSG_ELF_GETSHDRNUM));
4752 4821 return (ret);
4753 4822 }
4754 4823
4755 4824 if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
4756 4825 failure(file, MSG_ORIG(MSG_ELF_GETSHDRSTRNDX));
4757 4826 return (ret);
4758 4827 }
4759 4828
4760 4829 if (elf_getphdrnum(elf, &phnum) == -1) {
4761 4830 failure(file, MSG_ORIG(MSG_ELF_GETPHDRNUM));
4762 4831 return (ret);
4763 4832 }
4764 4833 /*
4765 4834 * If the user requested section headers derived from the
4766 4835 * program headers (-P option) and this file doesn't have
4767 4836 * any program headers (i.e. ET_REL), then we can't do it.
4768 4837 */
4769 4838 if ((phnum == 0) && (flags & FLG_CTL_FAKESHDR)) {
4770 4839 (void) fprintf(stderr, MSG_INTL(MSG_ERR_PNEEDSPH), file);
4771 4840 return (ret);
4772 4841 }
4773 4842
4774 4843
4775 4844 if ((scn = elf_getscn(elf, 0)) != NULL) {
4776 4845 if ((shdr = elf_getshdr(scn)) == NULL) {
4777 4846 failure(file, MSG_ORIG(MSG_ELF_GETSHDR));
4778 4847 (void) fprintf(stderr, MSG_INTL(MSG_ELF_ERR_SCN), 0);
4779 4848 return (ret);
4780 4849 }
4781 4850 } else
4782 4851 shdr = NULL;
4783 4852
4784 4853 /*
4785 4854 * Print the elf header.
4786 4855 */
4787 4856 if (flags & FLG_SHOW_EHDR)
4788 4857 Elf_ehdr(0, ehdr, shdr);
4789 4858
4790 4859 /*
4791 4860 * If the section headers or program headers have inadequate
4792 4861 * alignment for the class of object, print a warning. libelf
4793 4862 * can handle such files, but programs that use them can crash
4794 4863 * when they dereference unaligned items.
4795 4864 *
4796 4865 * Note that the AMD64 ABI, although it is a 64-bit architecture,
4797 4866 * allows access to data types smaller than 128-bits to be on
4798 4867 * word alignment.
4799 4868 */
4800 4869 if (ehdr->e_machine == EM_AMD64)
4801 4870 addr_align = sizeof (Word);
4802 4871 else
4803 4872 addr_align = sizeof (Addr);
4804 4873
4805 4874 if (ehdr->e_phoff & (addr_align - 1))
4806 4875 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADPHDRALIGN), file);
4807 4876 if (ehdr->e_shoff & (addr_align - 1))
4808 4877 (void) fprintf(stderr, MSG_INTL(MSG_ERR_BADSHDRALIGN), file);
4809 4878
4810 4879
4811 4880 /*
4812 4881 * Determine the Operating System ABI (osabi) we will use to
4813 4882 * interpret the object.
4814 4883 */
4815 4884 if (flags & FLG_CTL_OSABI) {
4816 4885 /*
4817 4886 * If the user explicitly specifies '-O none', we need
4818 4887 * to display a completely generic view of the file.
4819 4888 * However, libconv is written to assume that ELFOSABI_NONE
4820 4889 * is equivalent to ELFOSABI_SOLARIS. To get the desired
4821 4890 * effect, we use an osabi that libconv has no knowledge of.
4822 4891 */
4823 4892 if (osabi == ELFOSABI_NONE)
4824 4893 osabi = ELFOSABI_UNKNOWN4;
4825 4894 } else {
4826 4895 /* Determine osabi from file */
4827 4896 osabi = ehdr->e_ident[EI_OSABI];
4828 4897 if (osabi == ELFOSABI_NONE) {
4829 4898 /*
4830 4899 * Chicken/Egg scenario:
4831 4900 *
4832 4901 * Ideally, we wait to create the section header cache
4833 4902 * until after the program headers are printed. If we
4834 4903 * only output program headers, we can skip building
4835 4904 * the cache entirely.
4836 4905 *
4837 4906 * Proper interpretation of program headers requires
4838 4907 * the osabi, which is supposed to be in the ELF header.
4839 4908 * However, many systems (Solaris and Linux included)
4840 4909 * have a history of setting the osabi to the generic
4841 4910 * SysV ABI (ELFOSABI_NONE). We assume ELFOSABI_SOLARIS
4842 4911 * in such cases, but would like to check the object
4843 4912 * to see if it has a Linux .note.ABI-tag section,
4844 4913 * which implies ELFOSABI_LINUX. This requires a
4845 4914 * section header cache.
4846 4915 *
4847 4916 * To break the cycle, we create section headers now
4848 4917 * if osabi is ELFOSABI_NONE, and later otherwise.
4849 4918 * If it succeeds, we use them, if not, we defer
4850 4919 * exiting until after the program headers are out.
4851 4920 */
4852 4921 if (create_cache(file, fd, elf, ehdr, &cache,
4853 4922 shstrndx, &shnum, &flags) == 0) {
4854 4923 cache_state = CACHE_FAIL;
4855 4924 } else {
4856 4925 cache_state = CACHE_OK;
4857 4926 if (has_linux_abi_note(cache, shnum, file)) {
4858 4927 Conv_inv_buf_t ibuf1, ibuf2;
4859 4928
4860 4929 (void) fprintf(stderr,
4861 4930 MSG_INTL(MSG_INFO_LINUXOSABI), file,
4862 4931 conv_ehdr_osabi(osabi, 0, &ibuf1),
4863 4932 conv_ehdr_osabi(ELFOSABI_LINUX,
4864 4933 0, &ibuf2));
4865 4934 osabi = ELFOSABI_LINUX;
4866 4935 }
4867 4936 }
4868 4937 }
4869 4938 /*
4870 4939 * We treat ELFOSABI_NONE identically to ELFOSABI_SOLARIS.
4871 4940 * Mapping NONE to SOLARIS simplifies the required test.
4872 4941 */
4873 4942 if (osabi == ELFOSABI_NONE)
4874 4943 osabi = ELFOSABI_SOLARIS;
4875 4944 }
4876 4945
4877 4946 /*
4878 4947 * Print the program headers.
4879 4948 */
4880 4949 if ((flags & FLG_SHOW_PHDR) && (phnum != 0)) {
4881 4950 Phdr *phdr;
4882 4951
4883 4952 if ((phdr = elf_getphdr(elf)) == NULL) {
4884 4953 failure(file, MSG_ORIG(MSG_ELF_GETPHDR));
4885 4954 return (ret);
4886 4955 }
4887 4956
4888 4957 for (ndx = 0; ndx < phnum; phdr++, ndx++) {
4889 4958 if (!match(MATCH_F_PHDR| MATCH_F_NDX | MATCH_F_TYPE,
4890 4959 NULL, ndx, phdr->p_type))
4891 4960 continue;
4892 4961
4893 4962 dbg_print(0, MSG_ORIG(MSG_STR_EMPTY));
4894 4963 dbg_print(0, MSG_INTL(MSG_ELF_PHDR), EC_WORD(ndx));
4895 4964 Elf_phdr(0, osabi, ehdr->e_machine, phdr);
4896 4965 }
4897 4966 }
4898 4967
4899 4968 /*
4900 4969 * If we have flag bits set that explicitly require a show or calc
4901 4970 * operation, but none of them require the section headers, then
4902 4971 * we are done and can return now.
4903 4972 */
4904 4973 if (((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) != 0) &&
4905 4974 ((flags & (FLG_MASK_SHOW_SHDR | FLG_MASK_CALC_SHDR)) == 0))
4906 4975 return (ret);
4907 4976
4908 4977 /*
4909 4978 * Everything from this point on requires section headers.
4910 4979 * If we have no section headers, there is no reason to continue.
↓ open down ↓ |
1200 lines elided |
↑ open up ↑ |
4911 4980 *
4912 4981 * If we tried above to create the section header cache and failed,
4913 4982 * it is time to exit. Otherwise, create it if needed.
4914 4983 */
4915 4984 switch (cache_state) {
4916 4985 case CACHE_NEEDED:
4917 4986 if (create_cache(file, fd, elf, ehdr, &cache, shstrndx,
4918 4987 &shnum, &flags) == 0)
4919 4988 return (ret);
4920 4989 break;
4990 + case CACHE_OK:
4991 + break;
4921 4992 case CACHE_FAIL:
4922 4993 return (ret);
4923 4994 }
4924 4995 if (shnum <= 1)
4925 4996 goto done;
4926 4997
4927 4998 /*
4928 4999 * If -w was specified, find and write out the section(s) data.
4929 5000 */
4930 5001 if (wfd) {
4931 5002 for (ndx = 1; ndx < shnum; ndx++) {
4932 5003 Cache *_cache = &cache[ndx];
4933 5004
4934 5005 if (match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
4935 5006 ndx, _cache->c_shdr->sh_type) &&
4936 5007 _cache->c_data && _cache->c_data->d_buf) {
4937 5008 if (write(wfd, _cache->c_data->d_buf,
4938 5009 _cache->c_data->d_size) !=
4939 5010 _cache->c_data->d_size) {
4940 5011 int err = errno;
4941 5012 (void) fprintf(stderr,
4942 5013 MSG_INTL(MSG_ERR_WRITE), wname,
4943 5014 strerror(err));
4944 5015 /*
4945 5016 * Return an exit status of 1, because
4946 5017 * the failure is not related to the
4947 5018 * ELF file, but by system resources.
4948 5019 */
4949 5020 ret = 1;
4950 5021 goto done;
4951 5022 }
4952 5023 }
4953 5024 }
4954 5025 }
4955 5026
4956 5027 /*
4957 5028 * If we have no flag bits set that explicitly require a show or calc
4958 5029 * operation, but match options (-I, -N, -T) were used, then run
4959 5030 * through the section headers and see if we can't deduce show flags
4960 5031 * from the match options given.
4961 5032 *
4962 5033 * We don't do this if -w was specified, because (-I, -N, -T) used
4963 5034 * with -w in lieu of some other option is supposed to be quiet.
4964 5035 */
4965 5036 if ((wfd == 0) && (flags & FLG_CTL_MATCH) &&
4966 5037 ((flags & (FLG_MASK_SHOW | FLG_MASK_CALC)) == 0)) {
4967 5038 for (ndx = 1; ndx < shnum; ndx++) {
4968 5039 Cache *_cache = &cache[ndx];
4969 5040
4970 5041 if (!match(MATCH_F_STRICT | MATCH_F_ALL, _cache->c_name,
4971 5042 ndx, _cache->c_shdr->sh_type))
4972 5043 continue;
4973 5044
4974 5045 switch (_cache->c_shdr->sh_type) {
4975 5046 case SHT_PROGBITS:
4976 5047 /*
4977 5048 * Heuristic time: It is usually bad form
4978 5049 * to assume the meaning/format of a PROGBITS
4979 5050 * section based on its name. However, there
4980 5051 * are ABI mandated exceptions. Check for
4981 5052 * these special names.
4982 5053 */
4983 5054
4984 5055 /* The ELF ABI specifies .interp and .got */
4985 5056 if (strcmp(_cache->c_name,
4986 5057 MSG_ORIG(MSG_ELF_INTERP)) == 0) {
4987 5058 flags |= FLG_SHOW_INTERP;
4988 5059 break;
4989 5060 }
4990 5061 if (strcmp(_cache->c_name,
4991 5062 MSG_ORIG(MSG_ELF_GOT)) == 0) {
4992 5063 flags |= FLG_SHOW_GOT;
4993 5064 break;
4994 5065 }
4995 5066 /*
4996 5067 * The GNU compilers, and amd64 ABI, define
4997 5068 * .eh_frame and .eh_frame_hdr. The Sun
4998 5069 * C++ ABI defines .exception_ranges.
4999 5070 */
5000 5071 if ((strncmp(_cache->c_name,
5001 5072 MSG_ORIG(MSG_SCN_FRM),
5002 5073 MSG_SCN_FRM_SIZE) == 0) ||
5003 5074 (strncmp(_cache->c_name,
5004 5075 MSG_ORIG(MSG_SCN_EXRANGE),
5005 5076 MSG_SCN_EXRANGE_SIZE) == 0)) {
5006 5077 flags |= FLG_SHOW_UNWIND;
5007 5078 break;
5008 5079 }
5009 5080 break;
5010 5081
5011 5082 case SHT_SYMTAB:
5012 5083 case SHT_DYNSYM:
5013 5084 case SHT_SUNW_LDYNSYM:
5014 5085 case SHT_SUNW_versym:
5015 5086 case SHT_SYMTAB_SHNDX:
5016 5087 flags |= FLG_SHOW_SYMBOLS;
5017 5088 break;
5018 5089
5019 5090 case SHT_RELA:
5020 5091 case SHT_REL:
5021 5092 flags |= FLG_SHOW_RELOC;
5022 5093 break;
5023 5094
5024 5095 case SHT_HASH:
5025 5096 flags |= FLG_SHOW_HASH;
5026 5097 break;
5027 5098
5028 5099 case SHT_DYNAMIC:
5029 5100 flags |= FLG_SHOW_DYNAMIC;
5030 5101 break;
5031 5102
5032 5103 case SHT_NOTE:
5033 5104 flags |= FLG_SHOW_NOTE;
5034 5105 break;
5035 5106
5036 5107 case SHT_GROUP:
5037 5108 flags |= FLG_SHOW_GROUP;
5038 5109 break;
5039 5110
5040 5111 case SHT_SUNW_symsort:
5041 5112 case SHT_SUNW_tlssort:
5042 5113 flags |= FLG_SHOW_SORT;
5043 5114 break;
5044 5115
5045 5116 case SHT_SUNW_cap:
5046 5117 flags |= FLG_SHOW_CAP;
5047 5118 break;
5048 5119
5049 5120 case SHT_SUNW_move:
5050 5121 flags |= FLG_SHOW_MOVE;
5051 5122 break;
5052 5123
5053 5124 case SHT_SUNW_syminfo:
5054 5125 flags |= FLG_SHOW_SYMINFO;
5055 5126 break;
5056 5127
5057 5128 case SHT_SUNW_verdef:
5058 5129 case SHT_SUNW_verneed:
5059 5130 flags |= FLG_SHOW_VERSIONS;
5060 5131 break;
5061 5132
5062 5133 case SHT_AMD64_UNWIND:
5063 5134 flags |= FLG_SHOW_UNWIND;
5064 5135 break;
5065 5136 }
5066 5137 }
5067 5138 }
5068 5139
5069 5140
5070 5141 if (flags & FLG_SHOW_SHDR)
5071 5142 sections(file, cache, shnum, ehdr, osabi);
5072 5143
5073 5144 if (flags & FLG_SHOW_INTERP)
5074 5145 interp(file, cache, shnum, phnum, elf);
5075 5146
5076 5147 if ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX))
5077 5148 versions(cache, shnum, file, flags, &versym);
5078 5149
5079 5150 if (flags & FLG_SHOW_SYMBOLS)
5080 5151 symbols(cache, shnum, ehdr, osabi, &versym, file, flags);
5081 5152
5082 5153 if ((flags & FLG_SHOW_SORT) && (osabi == ELFOSABI_SOLARIS))
5083 5154 sunw_sort(cache, shnum, ehdr, osabi, &versym, file, flags);
5084 5155
5085 5156 if (flags & FLG_SHOW_HASH)
5086 5157 hash(cache, shnum, file, flags);
5087 5158
5088 5159 if (flags & FLG_SHOW_GOT)
5089 5160 got(cache, shnum, ehdr, file);
5090 5161
5091 5162 if (flags & FLG_SHOW_GROUP)
5092 5163 group(cache, shnum, file, flags);
5093 5164
5094 5165 if (flags & FLG_SHOW_SYMINFO)
5095 5166 syminfo(cache, shnum, ehdr, osabi, file);
5096 5167
5097 5168 if (flags & FLG_SHOW_RELOC)
5098 5169 reloc(cache, shnum, ehdr, file);
5099 5170
5100 5171 if (flags & FLG_SHOW_DYNAMIC)
5101 5172 dynamic(cache, shnum, ehdr, osabi, file);
5102 5173
5103 5174 if (flags & FLG_SHOW_NOTE) {
5104 5175 Word note_cnt;
5105 5176 size_t note_shnum;
5106 5177 Cache *note_cache;
5107 5178
5108 5179 note_cnt = note(cache, shnum, ehdr, file);
5109 5180
5110 5181 /*
5111 5182 * Solaris core files have section headers, but these
5112 5183 * headers do not include SHT_NOTE sections that reference
5113 5184 * the core note sections. This means that note() won't
5114 5185 * find the core notes. Fake section headers (-P option)
5115 5186 * recover these sections, but it is inconvenient to require
5116 5187 * users to specify -P in this situation. If the following
5117 5188 * are all true:
5118 5189 *
5119 5190 * - No note sections were found
5120 5191 * - This is a core file
5121 5192 * - We are not already using fake section headers
5122 5193 *
5123 5194 * then we will automatically generate fake section headers
5124 5195 * and then process them in a second call to note().
5125 5196 */
5126 5197 if ((note_cnt == 0) && (ehdr->e_type == ET_CORE) &&
5127 5198 !(flags & FLG_CTL_FAKESHDR) &&
5128 5199 (fake_shdr_cache(file, fd, elf, ehdr,
5129 5200 ¬e_cache, ¬e_shnum) != 0)) {
5130 5201 (void) note(note_cache, note_shnum, ehdr, file);
5131 5202 fake_shdr_cache_free(note_cache, note_shnum);
5132 5203 }
5133 5204 }
5134 5205
5135 5206 if ((flags & FLG_SHOW_MOVE) && (osabi == ELFOSABI_SOLARIS))
5136 5207 move(cache, shnum, file, flags);
5137 5208
5138 5209 if (flags & FLG_CALC_CHECKSUM)
5139 5210 checksum(elf);
5140 5211
5141 5212 if ((flags & FLG_SHOW_CAP) && (osabi == ELFOSABI_SOLARIS))
5142 5213 cap(file, cache, shnum, phnum, ehdr, osabi, elf, flags);
5143 5214
5144 5215 if ((flags & FLG_SHOW_UNWIND) &&
5145 5216 ((osabi == ELFOSABI_SOLARIS) || (osabi == ELFOSABI_LINUX)))
5146 5217 unwind(cache, shnum, phnum, ehdr, osabi, file, elf, flags);
5147 5218
5148 5219
5149 5220 /* Release the memory used to cache section headers */
5150 5221 done:
5151 5222 if (flags & FLG_CTL_FAKESHDR)
5152 5223 fake_shdr_cache_free(cache, shnum);
5153 5224 else
5154 5225 free(cache);
5155 5226
5156 5227 return (ret);
5157 5228 }
↓ open down ↓ |
227 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX