4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Generate a cache of section header information for an ELF
29 * object from the information found in its program headers.
31 * Malicious code can remove or corrupt section headers. The
32 * resulting program will be difficult to analyze, but is still
33 * runnable. Hence, scribbling on the section headers or removing
34 * them is an effective form of obfuscation. On the other hand,
35 * program headers must be accurate or the program will not run.
36 * Section headers derived from them will necessarily lack information
37 * found in the originals (particularly for non-allocable sections),
38 * but will provide essential symbol information. The focus is on
39 * recovering information that elfdump knows how to display, and that
40 * might be interesting in a forensic situation.
42 * There are some things we don't attempt to create sections for:
45 * We have no way to determine the length of either of
46 * these sections from the information available via
47 * the program headers or dynamic section. The data in
48 * the PLT is of little use to elfdump. The data in the
49 * GOT might be somewhat more interesting, especially as
50 * it pertains to relocations. However, the sizing issue
54 * Although we could create these, there is little value
55 * to doing so. elfdump cannot display the arbitrary
56 * data in these sections, so this would amount to a
57 * simple repetition of the information already displayed
58 * in the program headers, with no additional benefit.
63 #include <sys/elf_amd64.h>
76 * Common information about the object that is needed by
77 * all the routines in this module.
90 * These values uniquely identify the sections that we know
93 * Note: We write the sections to the cache array in this same order.
94 * It simplifies this code if the dynamic, dynstr, dynsym, and ldynsym
95 * sections occupy known slots in the cache array. Other sections reference
96 * them by index, and if they are at a known spot, there is no need
97 * for a fixup pass. Putting them in positions [1-4] solves this.
99 * The order they are in was chosen such that if any one of them exists,
100 * all of the ones before it must also exist. This means that if the
101 * desired section exists, it will end up in the desired index in the
104 * The order of the other sections is arbitrary. I've arranged them
105 * in roughly related groups.
123 SINFO_T_CAPINFO
= 14,
124 SINFO_T_CAPCHAIN
= 15,
129 SINFO_T_PREINITARR
= 20,
130 SINFO_T_INITARR
= 21,
131 SINFO_T_FINIARR
= 22,
134 SINFO_T_NUM
= 24 /* Count of items. Must come last */
140 * Table of per-section constant data used to set up the section
141 * header cache and the various sub-parts it references. Indexed by
144 * note: The sh_flags value should be either SHF_ALLOC, or 0.
145 * get_data() sets SHF_WRITE if the program header containing the
146 * section is writable. The other flags require information that
147 * the program headers don't contain (i.e. SHF_STRINGS, etc) so
156 Elf_Type libelf_type
;
160 * Many of these sections use an alignment given by M_WORD_ALIGN, a
161 * value that varies depending on the object target machine. Since we
162 * don't know that value at compile time, we settle for a value of
163 * 4 for ELFCLASS32 objects, and 8 for ELFCLASS64. This matches the
164 * platforms we current support (sparc and x86), and is good enough for
165 * a fake section header in any event, as the resulting object is only
166 * analyzed, and is not executed.
169 #define FAKE_M_WORD_ALIGN 8
171 #define FAKE_M_WORD_ALIGN 4
174 static SINFO_DATA sinfo_data
[SINFO_T_NUM
] = {
179 { MSG_ORIG(MSG_PHDRNAM_DYN
), SHT_DYNAMIC
, SHF_ALLOC
,
180 FAKE_M_WORD_ALIGN
, sizeof (Dyn
), ELF_T_DYN
},
183 { MSG_ORIG(MSG_PHDRNAM_DYNSTR
), SHT_STRTAB
, SHF_ALLOC
,
187 { MSG_ORIG(MSG_PHDRNAM_DYNSYM
), SHT_DYNSYM
, SHF_ALLOC
,
188 FAKE_M_WORD_ALIGN
, sizeof (Sym
), ELF_T_SYM
},
190 /* SINFO_T_LDYNSYM */
191 { MSG_ORIG(MSG_PHDRNAM_LDYNSYM
), SHT_SUNW_LDYNSYM
, SHF_ALLOC
,
192 FAKE_M_WORD_ALIGN
, sizeof (Sym
), ELF_T_SYM
},
195 { MSG_ORIG(MSG_PHDRNAM_HASH
), SHT_HASH
, SHF_ALLOC
,
196 FAKE_M_WORD_ALIGN
, sizeof (Word
), ELF_T_WORD
},
198 /* SINFO_T_SYMINFO */
199 { MSG_ORIG(MSG_PHDRNAM_SYMINFO
), SHT_SUNW_syminfo
, SHF_ALLOC
,
200 FAKE_M_WORD_ALIGN
, sizeof (Syminfo
), ELF_T_SYMINFO
},
202 /* SINFO_T_SYMSORT */
203 { MSG_ORIG(MSG_PHDRNAM_SYMSORT
), SHT_SUNW_symsort
, SHF_ALLOC
,
204 FAKE_M_WORD_ALIGN
, sizeof (Word
), ELF_T_WORD
},
206 /* SINFO_T_TLSSORT */
207 { MSG_ORIG(MSG_PHDRNAM_TLSSORT
), SHT_SUNW_tlssort
, SHF_ALLOC
,
208 FAKE_M_WORD_ALIGN
, sizeof (Word
), ELF_T_WORD
},
210 /* SINFO_T_VERNEED */
211 { MSG_ORIG(MSG_PHDRNAM_VER
), SHT_SUNW_verneed
, SHF_ALLOC
,
212 FAKE_M_WORD_ALIGN
, 1, ELF_T_VNEED
},
215 { MSG_ORIG(MSG_PHDRNAM_VER
), SHT_SUNW_verdef
, SHF_ALLOC
,
216 FAKE_M_WORD_ALIGN
, 1, ELF_T_VDEF
},
219 { MSG_ORIG(MSG_PHDRNAM_VER
), SHT_SUNW_versym
, SHF_ALLOC
,
220 FAKE_M_WORD_ALIGN
, sizeof (Versym
), ELF_T_HALF
},
223 { MSG_ORIG(MSG_PHDRNAM_INTERP
), SHT_PROGBITS
, SHF_ALLOC
,
227 { MSG_ORIG(MSG_PHDRNAM_CAP
), SHT_SUNW_cap
, SHF_ALLOC
,
228 sizeof (Addr
), sizeof (Cap
), ELF_T_CAP
},
230 /* SINFO_T_CAPINFO */
231 { MSG_ORIG(MSG_PHDRNAM_CAPINFO
), SHT_SUNW_capinfo
, SHF_ALLOC
,
232 FAKE_M_WORD_ALIGN
, sizeof (Capinfo
), ELF_T_WORD
},
234 /* SINFO_T_CAPCHAIN */
235 { MSG_ORIG(MSG_PHDRNAM_CAPCHAIN
), SHT_SUNW_capchain
, SHF_ALLOC
,
236 FAKE_M_WORD_ALIGN
, sizeof (Capchain
), ELF_T_WORD
},
239 { MSG_ORIG(MSG_PHDRNAM_UNWIND
), SHT_AMD64_UNWIND
, SHF_ALLOC
,
240 sizeof (Addr
), 0, ELF_T_BYTE
},
243 { MSG_ORIG(MSG_PHDRNAM_MOVE
), SHT_SUNW_move
, SHF_ALLOC
,
244 sizeof (Lword
), sizeof (Move
), ELF_T_MOVE
},
247 { MSG_ORIG(MSG_PHDRNAM_REL
), SHT_REL
, SHF_ALLOC
,
248 FAKE_M_WORD_ALIGN
, sizeof (Rel
), ELF_T_REL
},
251 { MSG_ORIG(MSG_PHDRNAM_RELA
), SHT_RELA
, SHF_ALLOC
,
252 FAKE_M_WORD_ALIGN
, sizeof (Rela
), ELF_T_RELA
},
254 /* SINFO_T_PREINITARR */
255 { MSG_ORIG(MSG_PHDRNAM_PREINITARR
), SHT_PREINIT_ARRAY
, SHF_ALLOC
,
256 sizeof (Addr
), sizeof (Addr
), ELF_T_ADDR
},
258 /* SINFO_T_INITARR */
259 { MSG_ORIG(MSG_PHDRNAM_INITARR
), SHT_INIT_ARRAY
, SHF_ALLOC
,
260 sizeof (Addr
), sizeof (Addr
), ELF_T_ADDR
},
262 /* SINFO_T_FINIARR */
263 { MSG_ORIG(MSG_PHDRNAM_FINIARR
), SHT_FINI_ARRAY
, SHF_ALLOC
,
264 sizeof (Addr
), sizeof (Addr
), ELF_T_ADDR
},
267 { MSG_ORIG(MSG_PHDRNAM_NOTE
), SHT_NOTE
, 0,
268 FAKE_M_WORD_ALIGN
, 1, ELF_T_NOTE
}
276 * As we read program headers and dynamic elements, we build up
277 * the data for our fake section headers in variables of the
278 * SINFO type. SINFO is used to track the sections that can only
279 * appear a fixed number of times (usually once).
281 * SINFO_LISTELT is used for sections that can occur an arbitrary
282 * number of times. They are kept in a doubly linked circular
286 SINFO_TYPE type
; /* Our type code for the section */
287 Addr vaddr
; /* Virtual memory address */
288 Off offset
; /* File offset of data. Ignored unless */
289 /* vaddr is 0. Used by program headers */
290 size_t size
; /* # bytes in section */
291 size_t vercnt
; /* Used by verdef and verneed to hold count */
292 Shdr
*shdr
; /* Constructed shdr */
293 Elf_Data
*data
; /* Constructed data descriptor */
296 typedef struct _sinfo_listelt
{
297 struct _sinfo_listelt
*next
;
298 struct _sinfo_listelt
*prev
;
305 * Free dynamic memory used by SINFO structures.
308 * sinfo - Address of first SINFO structure to free
309 * n - # of structures to clear
312 * For each SINFO struct, the section header, data descriptor,
313 * and data buffer are freed if non-NULL. The relevant
314 * fields are set to NULL, and the type is set to SINFO_T_NULL.
317 sinfo_free(SINFO
*sinfo
, size_t n
)
319 for (; n
-- > 0; sinfo
++) {
320 if (sinfo
->data
!= NULL
) {
321 if (sinfo
->data
->d_buf
!= NULL
)
322 free(sinfo
->data
->d_buf
);
331 sinfo
->type
= SINFO_T_NULL
;
338 * Allocate a new SINFO_LISTELT and put it at the end of the
339 * doubly linked list anchored by the given list root node.
341 * On success, a new node has been put at the end of the circular
342 * doubly linked list, and a pointer to the SINFO sub-structure is
343 * returned. On failure, an error is printed, and NULL is returned.
347 sinfo_list_alloc(FSTATE
*fstate
, SINFO_LISTELT
*root
)
351 if ((elt
= malloc(sizeof (*elt
))) == NULL
) {
353 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_MALLOC
),
354 fstate
->file
, strerror(err
));
359 elt
->prev
= root
->prev
;
362 elt
->prev
->next
= elt
;
364 bzero(&elt
->sinfo
, sizeof (elt
->sinfo
));
365 return (&elt
->sinfo
);
371 * Release the memory used by the given list, restoring it to
375 sinfo_list_free_all(SINFO_LISTELT
*root
)
379 for (elt
= root
->next
; elt
!= root
; elt
= elt
->next
)
380 sinfo_free(&elt
->sinfo
, 1);
382 root
->next
= root
->prev
= root
;
388 * Given a virtual address and desired size of the data to be found
389 * at that address, look through the program headers for the PT_LOAD
390 * segment that contains it and return the offset within the ELF file
391 * at which it resides.
394 * fstate - Object state
395 * addr - virtual address to be translated
396 * size - Size of the data to be found at that address, in bytes
397 * zero_bytes - NULL, or address to receive the number of data
398 * bytes at the end of the data that are not contained
399 * in the file, and which must be zero filled by the caller.
400 * If zero_bytes is NULL, the file must contain all of the
401 * desired data. If zero_bytes is not NULL, then the program
402 * header must reserve the space for all of the data (p_memsz)
403 * but it is acceptable for only part of the data to be in
404 * the file (p_filesz). *zero_bytes is set to the difference
405 * in size, and is the number of bytes the caller must
406 * set to 0 rather than reading from the file.
407 * phdr_ret - NULL, or address of variable to receive pointer
408 * to program header that contains offset.
410 * On success: If zero_bytes is non-NULL, it is updated. If phdr_ret
411 * is non-NULL, it is updated. The file offset is returned.
413 * On failure, 0 is returned. Since any ELF file we can understand
414 * must start with an ELF magic number, 0 cannot be a valid file
415 * offset for a virtual address, and is therefore unambiguous as
416 * a failure indication.
419 map_addr_to_offset(FSTATE
*fstate
, Addr addr
, size_t size
, size_t *zero_bytes
,
423 Addr end_addr
= addr
+ size
;
425 Phdr
*phdr
= fstate
->phdr
;
426 size_t phnum
= fstate
->phnum
;
428 for (; phnum
--; phdr
++) {
429 if (phdr
->p_type
!= PT_LOAD
)
432 if ((addr
>= phdr
->p_vaddr
) &&
433 (end_addr
<= (phdr
->p_vaddr
+ phdr
->p_memsz
))) {
435 * Subtract segment virtual address, leaving the
436 * offset relative to the segment (not the file).
438 offset
= addr
- phdr
->p_vaddr
;
439 avail_file
= phdr
->p_filesz
- offset
;
442 * The addr/size are in bounds for this segment.
443 * Is there enough data in the file to satisfy
444 * the request? If zero_bytes is NULL, it must
445 * all be in the file. Otherwise it can be
448 if (zero_bytes
== NULL
) {
449 if (size
> avail_file
)
452 *zero_bytes
= (size
> avail_file
) ?
453 (size
- avail_file
) : 0;
456 if (phdr_ret
!= NULL
)
459 /* Add segment file offset, giving overall offset */
460 return (phdr
->p_offset
+ offset
);
464 /* If we get here, the mapping failed */
471 * This routine is the same thing as map_addr_to_offset(), except that
472 * it goes the other way, mapping from offset to virtual address.
474 * The comments for map_addr_to_offset() are applicable if you
475 * reverse offset and address.
479 map_offset_to_addr(FSTATE
*fstate
, Off offset
, size_t size
, size_t *zero_bytes
,
482 Off end_offset
= offset
+ size
;
484 Phdr
*phdr
= fstate
->phdr
;
485 size_t phnum
= fstate
->phnum
;
487 for (; phnum
--; phdr
++) {
488 if (phdr
->p_type
!= PT_LOAD
)
491 if ((offset
>= phdr
->p_offset
) &&
492 (end_offset
<= (phdr
->p_offset
+ phdr
->p_memsz
))) {
494 * Subtract segment offset, leaving the
495 * offset relative to the segment (not the file).
497 offset
-= phdr
->p_offset
;
498 avail_file
= phdr
->p_filesz
- offset
;
501 * The offset/size are in bounds for this segment.
502 * Is there enough data in the file to satisfy
503 * the request? If zero_bytes is NULL, it must
504 * all be in the file. Otherwise it can be
507 if (zero_bytes
== NULL
) {
508 if (size
> avail_file
)
511 *zero_bytes
= (size
> avail_file
) ?
512 (size
- avail_file
) : 0;
515 if (phdr_ret
!= NULL
)
518 /* Add segment virtual address, giving overall addr */
519 return (phdr
->p_vaddr
+ offset
);
523 /* If we get here, the mapping failed */
530 * Use elf_xlatetom() to convert the bytes in buf from their
531 * in-file representation to their in-memory representation.
533 * Returns True(1) for success. On failure, an error message is printed
534 * and False(0) is returned.
537 xlate_data(FSTATE
*fstate
, void *buf
, size_t nbyte
, Elf_Type xlate_type
)
541 data
.d_type
= xlate_type
;
545 data
.d_version
= fstate
->ehdr
->e_version
;
548 if (elf_xlatetom(&data
, &data
,
549 fstate
->ehdr
->e_ident
[EI_DATA
]) == NULL
) {
550 failure(fstate
->file
, MSG_ORIG(MSG_ELF_XLATETOM
));
559 * Read nbytes of data into buf, starting at the specified offset
560 * within the ELF file.
563 * fstate - Object state
564 * offset - Offset within the file at which desired data resides.
565 * buf - Buffer to receive the data
566 * nbyte - # of bytes to read into buf
567 * xlate_type - An ELF xlate type, specifying the type of data
568 * being input. If xlate_type is ELF_T_BYTE, xlate is not
569 * done. Otherwise, xlate_data() is called to convert the
570 * data into its in-memory representation.
572 * On success, the data has been written into buf, xlate_data()
573 * called on it if required, and True(1) is returned. Otherwise
574 * False(0) is returned.
577 * This routine does not move the file pointer.
580 read_data(FSTATE
*fstate
, Off offset
, void *buf
, size_t nbyte
,
583 if (pread(fstate
->fd
, buf
, nbyte
, offset
) != nbyte
) {
586 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_READ
),
587 fstate
->file
, strerror(err
));
591 if (xlate_type
!= ELF_T_BYTE
)
592 return (xlate_data(fstate
, buf
, nbyte
, xlate_type
));
600 * Read the hash nbucket/nchain values from the start of the hash
601 * table found at the given virtual address in the mapped ELF object.
603 * On success, *nbucket, and *nchain have been filled in with their
604 * values, *total contains the number of elements in the hash table,
605 * and this routine returns True (1).
607 * On failure, False (0) is returned.
610 hash_size(FSTATE
*fstate
, SINFO
*hash_sinfo
,
611 Word
*nbucket
, Word
*nchain
, size_t *total
)
616 offset
= map_addr_to_offset(fstate
, hash_sinfo
->vaddr
,
617 sizeof (buf
), NULL
, NULL
);
621 if (read_data(fstate
, offset
, buf
, sizeof (buf
), ELF_T_WORD
) == 0)
626 *total
= 2 + *nbucket
+ *nchain
;
633 * Read a Verdef structure at the specified file offset and return
634 * its vd_cnt, vd_aux, and vd_next fields.
637 read_verdef(FSTATE
*fstate
, Off offset
, Half
*cnt
, Word
*aux
, Word
*next
)
641 if (read_data(fstate
, offset
, &verdef
, sizeof (verdef
),
646 if (xlate_data(fstate
, &verdef
.vd_cnt
, sizeof (verdef
.vd_cnt
),
651 * xlate vd_aux and vd_next. These items are adjacent and are
652 * both Words, so they can be handled in a single operation.
654 if (xlate_data(fstate
, &verdef
.vd_aux
,
655 2 * sizeof (Word
), ELF_T_WORD
) == 0)
658 *cnt
= verdef
.vd_cnt
;
659 *aux
= verdef
.vd_aux
;
660 *next
= verdef
.vd_next
;
668 * Read a Verdaux structure at the specified file offset and return
669 * its vda_next field.
672 read_verdaux(FSTATE
*fstate
, Off offset
, Word
*next
)
676 if (read_data(fstate
, offset
, &verdaux
, sizeof (verdaux
),
681 if (xlate_data(fstate
, &verdaux
.vda_next
, sizeof (verdaux
.vda_next
),
685 *next
= verdaux
.vda_next
;
693 * Read a Verneed structure at the specified file offset and return
694 * its vn_cnt, vn_aux, and vn_next fields.
697 read_verneed(FSTATE
*fstate
, Off offset
, Half
*cnt
, Word
*aux
, Word
*next
)
701 if (read_data(fstate
, offset
, &verneed
, sizeof (verneed
),
706 if (xlate_data(fstate
, &verneed
.vn_cnt
, sizeof (verneed
.vn_cnt
),
711 * xlate vn_aux and vn_next. These items are adjacent and are
712 * both Words, so they can be handled in a single operation.
714 if (xlate_data(fstate
, &verneed
.vn_aux
,
715 2 * sizeof (Word
), ELF_T_WORD
) == 0)
718 *cnt
= verneed
.vn_cnt
;
719 *aux
= verneed
.vn_aux
;
720 *next
= verneed
.vn_next
;
728 * Read a Vernaux structure at the specified file offset and return
729 * its vna_next field.
732 read_vernaux(FSTATE
*fstate
, Off offset
, Word
*next
)
736 if (read_data(fstate
, offset
, &vernaux
, sizeof (vernaux
),
741 if (xlate_data(fstate
, &vernaux
.vna_next
, sizeof (vernaux
.vna_next
),
745 *next
= vernaux
.vna_next
;
753 * Compute the size of Verdef and Verneed sections. Both of these
754 * sections are made up of interleaved main nodes (Verdef and Verneed)
755 * and auxiliary blocks (Verdaux and Vernaux). These nodes refer to
756 * each other by relative offsets. The linker has a lot of flexibility
757 * in how it lays out these items, and we cannot assume a standard
758 * layout. To determine the size of the section, we must read each
759 * main node and compute the high water mark of the memory it and its
760 * auxiliary structs access.
762 * Although Verdef/Verdaux and Verneed/Vernaux are different types,
763 * their logical organization is the same. Each main block has
764 * a cnt field that tells how many auxiliary blocks it has, an
765 * aux field that gives the offset of the first auxiliary block, and
766 * an offset to the next main block. Each auxiliary block contains
767 * an offset to the next auxiliary block. By breaking the type specific
768 * code into separate sub-functions, we can process both Verdef and
769 * sections Verdaux from a single routine.
772 * fstate - Object state
773 * sec - Section to be processed (SINFO_T_VERDEF or SINFO_T_VERNEED).
776 * On success, sec->size is set to the section size in bytes, and
777 * True (1) is returned. On failure, False (0) is returned.
780 verdefneed_size(FSTATE
*fstate
, SINFO
*sec
)
782 int (* read_main
)(FSTATE
*, Off
, Half
*, Word
*, Word
*);
783 int (* read_aux
)(FSTATE
*, Off
, Word
*);
784 size_t size_main
, size_aux
;
786 Off offset
, aux_offset
;
787 Off highwater
, extent
;
788 size_t num_main
= sec
->vercnt
;
790 Word v_aux
, v_next
, va_next
;
794 * Set up the function pointers to the type-specific code
795 * for fetching data from the main and auxiliary blocks.
797 if (sec
->type
== SINFO_T_VERDEF
) {
798 read_main
= read_verdef
;
799 read_aux
= read_verdaux
;
800 size_main
= sizeof (Verdef
);
801 size_aux
= sizeof (Verdaux
);
802 } else { /* SINFO_T_VERNEED */
803 read_main
= read_verneed
;
804 read_aux
= read_vernaux
;
805 size_main
= sizeof (Verneed
);
806 size_aux
= sizeof (Vernaux
);
810 * Map starting address to file offset. Save the starting offset
811 * in the SINFO size field. Once we have the high water offset, we
812 * can subtract this from it to get the size.
814 * Note: The size argument set here is a lower bound --- the
815 * size of the main blocks without any auxiliary ones. It's
816 * the best we can do until the size has been determined for real.
818 offset
= highwater
= map_addr_to_offset(fstate
, sec
->vaddr
,
819 size_main
* num_main
, NULL
, NULL
);
824 for (; num_main
-- > 0; offset
+= v_next
) {
825 /* Does this move the high water mark up? */
826 extent
= offset
+ size_main
;
827 if (extent
> highwater
)
830 if ((*read_main
)(fstate
, offset
, &v_cnt
, &v_aux
, &v_next
) == 0)
834 * If there are auxiliary structures referenced,
835 * check their position to see if it pushes
836 * the high water mark.
838 aux_offset
= offset
+ v_aux
;
839 for (; v_cnt
-- > 0; aux_offset
+= va_next
) {
840 extent
= aux_offset
+ size_aux
;
841 if (extent
> highwater
)
844 if ((*read_aux
)(fstate
, aux_offset
, &va_next
) == 0)
849 sec
->size
= highwater
- sec
->size
;
855 * Allocate and fill in a fake section header, data descriptor,
856 * and data buffer for the given section. Fill them in and read
857 * the associated data into the buffer.
860 * fstate - Object state
861 * sec - Section information
864 * On success, the actions described above are complete, and
865 * True (1) is returned.
867 * On failure, an error is reported, all resources used by sec
868 * are released, and sec->type is set to SINFO_T_NULL, effectively
869 * eliminating its contents from any further use. False (0) is
873 get_data(FSTATE
*fstate
, SINFO
*sec
)
877 size_t read_bytes
, zero_bytes
;
881 * If this is a NULL section, or if we've already processed
882 * this item, then we are already done.
884 if ((sec
->type
== SINFO_T_NULL
) || (sec
->shdr
!= NULL
))
887 if (((sec
->shdr
= malloc(sizeof (*sec
->shdr
))) == NULL
) ||
888 ((sec
->data
= malloc(sizeof (*sec
->data
))) == NULL
)) {
891 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_MALLOC
),
892 fstate
->file
, strerror(err
));
895 tinfo
= &sinfo_data
[sec
->type
];
900 * Fill in fake section header
902 * sh_name should be the offset of the name in the shstrtab
903 * section referenced by the ELF header. There is no
904 * value to elfdump in creating shstrtab, so we set
905 * sh_name to 0, knowing that elfdump doesn't look at it.
907 sec
->shdr
->sh_name
= 0;
908 sec
->shdr
->sh_type
= tinfo
->sh_type
;
909 sec
->shdr
->sh_flags
= tinfo
->sh_flags
;
910 if ((tinfo
->sh_flags
& SHF_ALLOC
) == 0) {
912 * Non-allocable section: Pass the addr (which is probably
913 * 0) and offset through without inspection.
915 sec
->shdr
->sh_addr
= sec
->vaddr
;
916 sec
->shdr
->sh_offset
= sec
->offset
;
918 } else if (sec
->vaddr
== 0) {
920 * Allocable section with a 0 vaddr. Figure out the
921 * real address by mapping the offset to it using the
924 sec
->shdr
->sh_addr
= map_offset_to_addr(fstate
, sec
->offset
,
925 sec
->size
, &zero_bytes
, &phdr
);
926 sec
->shdr
->sh_offset
= sec
->offset
;
929 * Allocable section with non-0 vaddr. Use the vaddr
930 * to derive the offset.
932 sec
->shdr
->sh_addr
= sec
->vaddr
;
933 sec
->shdr
->sh_offset
= map_addr_to_offset(fstate
,
934 sec
->vaddr
, sec
->size
, &zero_bytes
, &phdr
);
936 if (sec
->shdr
->sh_offset
== 0) {
941 * If the program header has its write flags set, then set
942 * the section write flag.
944 if (phdr
&& ((phdr
->p_flags
& PF_W
) != 0))
945 sec
->shdr
->sh_flags
|= SHF_WRITE
;
946 sec
->shdr
->sh_size
= sec
->size
;
947 sec
->shdr
->sh_link
= 0;
948 sec
->shdr
->sh_info
= 0;
949 sec
->shdr
->sh_addralign
= tinfo
->sh_addralign
;
950 sec
->shdr
->sh_entsize
= tinfo
->sh_entsize
;
953 * Some sections define special meanings for sh_link and sh_info.
955 switch (tinfo
->sh_type
) {
957 sec
->shdr
->sh_link
= SINFO_T_DYNSTR
;
961 sec
->shdr
->sh_link
= SINFO_T_DYNSTR
;
962 sec
->shdr
->sh_info
= 1; /* First global symbol */
965 case SHT_SUNW_LDYNSYM
:
966 sec
->shdr
->sh_link
= SINFO_T_DYNSTR
;
968 * ldynsym is all local symbols, so the index of the
969 * first global is equivalent to the number of symbols.
971 sec
->shdr
->sh_info
= sec
->shdr
->sh_size
/ sizeof (Sym
);
978 case SHT_SUNW_versym
:
979 sec
->shdr
->sh_link
= SINFO_T_DYNSYM
;
982 case SHT_SUNW_verdef
:
983 case SHT_SUNW_verneed
:
984 sec
->shdr
->sh_link
= SINFO_T_DYNSTR
;
985 sec
->shdr
->sh_info
= sec
->vercnt
;
988 case SHT_SUNW_syminfo
:
989 sec
->shdr
->sh_link
= SINFO_T_DYNSYM
;
990 sec
->shdr
->sh_info
= SINFO_T_DYN
;
993 case SHT_SUNW_symsort
:
994 case SHT_SUNW_tlssort
:
995 sec
->shdr
->sh_link
= SINFO_T_LDYNSYM
;
1001 /* Fill in fake Elf_Data descriptor */
1002 sec
->data
->d_type
= tinfo
->libelf_type
;
1003 sec
->data
->d_size
= sec
->size
;
1004 sec
->data
->d_off
= 0;
1005 sec
->data
->d_align
= tinfo
->sh_addralign
;
1006 sec
->data
->d_version
= fstate
->ehdr
->e_version
;
1008 if (sec
->size
== 0) {
1009 sec
->data
->d_buf
= NULL
;
1013 if ((sec
->data
->d_buf
= malloc(sec
->size
)) == NULL
) {
1017 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_MALLOC
),
1018 fstate
->file
, strerror(err
));
1022 read_bytes
= sec
->size
- zero_bytes
;
1023 if ((read_bytes
> 0) &&
1024 (read_data(fstate
, sec
->shdr
->sh_offset
, sec
->data
->d_buf
,
1025 read_bytes
, ELF_T_BYTE
) == 0)) {
1030 bzero(read_bytes
+ (char *)sec
->data
->d_buf
, zero_bytes
);
1032 if ((tinfo
->libelf_type
!= ELF_T_BYTE
) &&
1033 (elf_xlatetom(sec
->data
, sec
->data
,
1034 fstate
->ehdr
->e_ident
[EI_DATA
]) == NULL
)) {
1036 failure(fstate
->file
, MSG_ORIG(MSG_ELF_XLATETOM
));
1046 * Generate a section header cache made up of information derived
1047 * from the program headers.
1050 * file - Name of object
1051 * fd - Open file handle for object
1052 * elf - ELF descriptor
1054 * cache, shnum - Addresses of variables to receive resulting
1055 * cache and number of sections.
1058 * On success, *cache and *shnum are set, and True (1) is returned.
1059 * On failure, False (0) is returned.
1062 * The cache returned by this routine must be freed using
1063 * fake_shdr_cache_free(), and not by a direct call to free().
1064 * Otherwise, memory will leak.
1067 fake_shdr_cache(const char *file
, int fd
, Elf
*elf
, Ehdr
*ehdr
,
1068 Cache
**cache
, size_t *shnum
)
1071 * The C language guarantees that a structure of homogeneous
1072 * items will receive exactly the same layout in a structure
1073 * as a plain array of the same type. Hence, this structure, which
1074 * gives us by-name or by-index access to the various section
1075 * info descriptors we maintain.
1077 * We use this for sections where
1078 * - Only one instance is allowed
1079 * - We need to be able to access them easily by
1080 * name (for instance, when mining the .dynamic
1081 * section for information to build them up.
1083 * NOTE: These fields must be in the same order as the
1084 * SINFO_T_ type codes that correspond to them. Otherwise,
1085 * they will end up in the wrong order in the cache array,
1086 * and the sh_link/sh_info fields may be wrong.
1089 /* Note: No entry is needed for SINFO_T_NULL */
1114 static const size_t sinfo_n
= sizeof (sec
) / sizeof (sec
.dyn
);
1115 SINFO
*secarr
= (SINFO
*) &sec
;
1118 * Doubly linked circular list, used to track sections
1119 * where multiple sections of a given type can exist.
1120 * seclist is the root of the list. Its sinfo field is not
1121 * used --- it serves to anchor the root of the list, allowing
1122 * rapid access to the first and last element in the list.
1124 SINFO_LISTELT seclist
;
1128 size_t num_sinfo
, num_list_sinfo
;
1130 SINFO_LISTELT
*sinfo_list
;
1137 if (elf_getphdrnum(elf
, &fstate
.phnum
) == -1) {
1138 failure(file
, MSG_ORIG(MSG_ELF_GETPHDRNUM
));
1141 if ((fstate
.phdr
= elf_getphdr(elf
)) == NULL
) {
1142 failure(file
, MSG_ORIG(MSG_ELF_GETPHDR
));
1146 bzero(&sec
, sizeof (sec
)); /* Initialize "by-name" sec info */
1147 seclist
.next
= seclist
.prev
= &seclist
; /* Empty circular list */
1150 * Go through the program headers and look for information
1151 * we can use to synthesize section headers. By far the most
1152 * valuable thing is a dynamic section, the contents of
1153 * which point at all sections used by ld.so.1.
1155 for (ndx
= 0; ndx
< fstate
.phnum
; ndx
++) {
1157 * A program header with no file size does
1158 * not have a backing section.
1160 if (fstate
.phdr
[ndx
].p_filesz
== 0)
1164 switch (fstate
.phdr
[ndx
].p_type
) {
1166 /* Header we can't use. Move on to next one */
1170 sec
.dyn
.type
= SINFO_T_DYN
;
1175 sec
.interp
.type
= SINFO_T_INTERP
;
1176 sinfo
= &sec
.interp
;
1180 if ((sinfo
= sinfo_list_alloc(&fstate
, &seclist
)) ==
1183 sinfo
->type
= SINFO_T_NOTE
;
1186 case PT_SUNW_UNWIND
:
1187 case PT_SUNW_EH_FRAME
:
1188 sec
.unwind
.type
= SINFO_T_UNWIND
;
1189 sinfo
= &sec
.unwind
;
1193 sec
.cap
.type
= SINFO_T_CAP
;
1199 * Capture the position/extent information for
1200 * the header in the SINFO struct set up by the
1201 * switch statement above.
1203 sinfo
->vaddr
= fstate
.phdr
[ndx
].p_vaddr
;
1204 sinfo
->offset
= fstate
.phdr
[ndx
].p_offset
;
1205 sinfo
->size
= fstate
.phdr
[ndx
].p_filesz
;
1209 * If we found a dynamic section, look through it and
1210 * gather information about the sections it references.
1212 if (sec
.dyn
.type
== SINFO_T_DYN
)
1213 (void) get_data(&fstate
, &sec
.dyn
);
1214 if ((sec
.dyn
.type
== SINFO_T_DYN
) && (sec
.dyn
.data
->d_buf
!= NULL
)) {
1216 for (dyn
= sec
.dyn
.data
->d_buf
; dyn
->d_tag
!= DT_NULL
; dyn
++) {
1217 switch (dyn
->d_tag
) {
1219 sec
.hash
.type
= SINFO_T_HASH
;
1220 sec
.hash
.vaddr
= dyn
->d_un
.d_ptr
;
1224 sec
.dynstr
.type
= SINFO_T_DYNSTR
;
1225 sec
.dynstr
.vaddr
= dyn
->d_un
.d_ptr
;
1229 sec
.dynsym
.type
= SINFO_T_DYNSYM
;
1230 sec
.dynsym
.vaddr
= dyn
->d_un
.d_ptr
;
1234 sec
.rela
.type
= SINFO_T_RELA
;
1235 sec
.rela
.vaddr
= dyn
->d_un
.d_ptr
;
1239 sec
.rela
.size
= dyn
->d_un
.d_val
;
1243 sec
.dynstr
.size
= dyn
->d_un
.d_val
;
1247 sec
.rel
.type
= SINFO_T_REL
;
1248 sec
.rel
.vaddr
= dyn
->d_un
.d_ptr
;
1252 sec
.rel
.size
= dyn
->d_un
.d_val
;
1256 sec
.initarr
.type
= SINFO_T_INITARR
;
1257 sec
.initarr
.vaddr
= dyn
->d_un
.d_ptr
;
1260 case DT_INIT_ARRAYSZ
:
1261 sec
.initarr
.size
= dyn
->d_un
.d_val
;
1265 sec
.finiarr
.type
= SINFO_T_FINIARR
;
1266 sec
.finiarr
.vaddr
= dyn
->d_un
.d_ptr
;
1269 case DT_FINI_ARRAYSZ
:
1270 sec
.finiarr
.size
= dyn
->d_un
.d_val
;
1273 case DT_PREINIT_ARRAY
:
1274 sec
.preinitarr
.type
= SINFO_T_PREINITARR
;
1275 sec
.preinitarr
.vaddr
= dyn
->d_un
.d_ptr
;
1278 case DT_PREINIT_ARRAYSZ
:
1279 sec
.preinitarr
.size
= dyn
->d_un
.d_val
;
1282 case DT_SUNW_CAPINFO
:
1283 sec
.capinfo
.type
= SINFO_T_CAPINFO
;
1284 sec
.capinfo
.vaddr
= dyn
->d_un
.d_ptr
;
1287 case DT_SUNW_CAPCHAIN
:
1288 sec
.capchain
.type
= SINFO_T_CAPCHAIN
;
1289 sec
.capchain
.vaddr
= dyn
->d_un
.d_ptr
;
1292 case DT_SUNW_SYMTAB
:
1293 sec
.ldynsym
.type
= SINFO_T_LDYNSYM
;
1294 sec
.ldynsym
.vaddr
= dyn
->d_un
.d_ptr
;
1298 sec
.ldynsym
.size
= dyn
->d_un
.d_val
;
1301 case DT_SUNW_SYMSORT
:
1302 sec
.symsort
.type
= SINFO_T_SYMSORT
;
1303 sec
.symsort
.vaddr
= dyn
->d_un
.d_ptr
;
1306 case DT_SUNW_SYMSORTSZ
:
1307 sec
.symsort
.size
= dyn
->d_un
.d_val
;
1310 case DT_SUNW_TLSSORT
:
1311 sec
.tlssort
.type
= SINFO_T_TLSSORT
;
1312 sec
.tlssort
.vaddr
= dyn
->d_un
.d_ptr
;
1315 case DT_SUNW_TLSSORTSZ
:
1316 sec
.tlssort
.size
= dyn
->d_un
.d_val
;
1320 sec
.move
.type
= SINFO_T_MOVE
;
1321 sec
.move
.vaddr
= dyn
->d_un
.d_ptr
;
1325 sec
.move
.size
= dyn
->d_un
.d_val
;
1329 sec
.syminfo
.type
= SINFO_T_SYMINFO
;
1330 sec
.syminfo
.vaddr
= dyn
->d_un
.d_ptr
;
1334 sec
.syminfo
.size
= dyn
->d_un
.d_val
;
1338 sec
.versym
.type
= SINFO_T_VERSYM
;
1339 sec
.versym
.vaddr
= dyn
->d_un
.d_ptr
;
1343 sec
.verdef
.type
= SINFO_T_VERDEF
;
1344 sec
.verdef
.vaddr
= dyn
->d_un
.d_ptr
;
1348 sec
.verdef
.vercnt
= dyn
->d_un
.d_val
;
1349 sec
.verdef
.size
= sizeof (Verdef
) *
1354 sec
.verneed
.type
= SINFO_T_VERNEED
;
1355 sec
.verneed
.vaddr
= dyn
->d_un
.d_ptr
;
1359 sec
.verneed
.vercnt
= dyn
->d_un
.d_val
;
1360 sec
.verneed
.size
= sizeof (Verneed
) *
1368 * Different sections depend on each other, and are meaningless
1369 * without them. For instance, even if a .dynsym exists,
1370 * no use can be made of it without a dynstr. These relationships
1371 * fan out: Disqualifying the .dynsym will disqualify the hash
1372 * section, and so forth.
1374 * Disqualify sections that don't have the necessary prerequisites.
1377 /* Things that need the dynamic string table */
1378 if (sec
.dynstr
.size
== 0)
1379 sec
.dynstr
.type
= SINFO_T_NULL
;
1380 if (sec
.dynstr
.type
!= SINFO_T_DYNSTR
) {
1381 sinfo_free(&sec
.dyn
, 1); /* Data already fetched */
1382 sec
.dynsym
.type
= SINFO_T_NULL
;
1383 sec
.dynsym
.type
= SINFO_T_NULL
;
1384 sec
.verdef
.type
= SINFO_T_NULL
;
1385 sec
.verneed
.type
= SINFO_T_NULL
;
1389 * The length of the hash section is encoded in its first two
1390 * elements (nbucket, and nchain). The length of the dynsym,
1391 * ldynsym, and versym are not given in the dynamic section,
1392 * but are known to be the same as nchain.
1394 * If we don't have a hash table, or cannot read nbuckets and
1395 * nchain, we have to invalidate all of these.
1397 if (sec
.hash
.type
== SINFO_T_HASH
) {
1402 if (hash_size(&fstate
, &sec
.hash
,
1403 &nbucket
, &nchain
, &total
) == 0) {
1404 sec
.hash
.type
= SINFO_T_NULL
;
1406 /* Use these counts to set sizes for related sections */
1407 sec
.hash
.size
= total
* sizeof (Word
);
1408 sec
.dynsym
.size
= nchain
* sizeof (Sym
);
1409 sec
.versym
.size
= nchain
* sizeof (Versym
);
1412 * The ldynsym size received the DT_SUNW_SYMSZ
1413 * value, which is the combined size of .dynsym
1414 * and .ldynsym. Now that we have the dynsym size,
1415 * use it to lower the ldynsym size to its real size.
1417 if (sec
.ldynsym
.size
> sec
.dynsym
.size
)
1418 sec
.ldynsym
.size
-= sec
.dynsym
.size
;
1422 * If the hash table is not present, or if the call to
1423 * hash_size() failed, then discard the sections that
1424 * need it to determine their length.
1426 if (sec
.hash
.type
!= SINFO_T_HASH
) {
1427 sec
.dynsym
.type
= SINFO_T_NULL
;
1428 sec
.ldynsym
.type
= SINFO_T_NULL
;
1429 sec
.versym
.type
= SINFO_T_NULL
;
1433 * The runtime linker does not receive size information for
1434 * Verdef and Verneed sections. We have to read their data
1435 * in pieces and calculate it.
1437 if ((sec
.verdef
.type
== SINFO_T_VERDEF
) &&
1438 (verdefneed_size(&fstate
, &sec
.verdef
) == 0))
1439 sec
.verdef
.type
= SINFO_T_NULL
;
1440 if ((sec
.verneed
.type
== SINFO_T_VERNEED
) &&
1441 (verdefneed_size(&fstate
, &sec
.verneed
) == 0))
1442 sec
.verneed
.type
= SINFO_T_NULL
;
1444 /* Discard any section with a zero length */
1446 for (sinfo
= secarr
; ndx
-- > 0; sinfo
++)
1447 if ((sinfo
->type
!= SINFO_T_NULL
) && (sinfo
->size
== 0))
1448 sinfo
->type
= SINFO_T_NULL
;
1450 /* Things that need the dynamic symbol table */
1451 if (sec
.dynsym
.type
!= SINFO_T_DYNSYM
) {
1452 sec
.ldynsym
.type
= SINFO_T_NULL
;
1453 sec
.hash
.type
= SINFO_T_NULL
;
1454 sec
.syminfo
.type
= SINFO_T_NULL
;
1455 sec
.versym
.type
= SINFO_T_NULL
;
1456 sec
.move
.type
= SINFO_T_NULL
;
1457 sec
.rel
.type
= SINFO_T_NULL
;
1458 sec
.rela
.type
= SINFO_T_NULL
;
1461 /* Things that need the dynamic local symbol table */
1462 if (sec
.ldynsym
.type
!= SINFO_T_DYNSYM
) {
1463 sec
.symsort
.type
= SINFO_T_NULL
;
1464 sec
.tlssort
.type
= SINFO_T_NULL
;
1468 * Look through the results and fetch the data for any sections
1469 * we have found. At the same time, count the number.
1471 num_sinfo
= num_list_sinfo
= 0;
1473 for (sinfo
= secarr
; ndx
-- > 0; sinfo
++) {
1474 if ((sinfo
->type
!= SINFO_T_NULL
) && (sinfo
->data
== NULL
))
1475 (void) get_data(&fstate
, sinfo
);
1476 if (sinfo
->data
!= NULL
)
1479 for (sinfo_list
= seclist
.next
; sinfo_list
!= &seclist
;
1480 sinfo_list
= sinfo_list
->next
) {
1481 sinfo
= &sinfo_list
->sinfo
;
1482 if ((sinfo
->type
!= SINFO_T_NULL
) && (sinfo
->data
== NULL
))
1483 (void) get_data(&fstate
, sinfo
);
1484 if (sinfo
->data
!= NULL
)
1489 * Allocate the cache array and fill it in. The cache array
1490 * ends up taking all the dynamic memory we've allocated
1491 * to build up sec and seclist, so on success, we have nothing
1492 * left to clean up. If we can't allocate the cache array
1493 * though, we have to free up everything else.
1495 *shnum
= num_sinfo
+ num_list_sinfo
+ 1; /* Extra for 1st NULL sec. */
1496 if ((*cache
= _cache
= malloc((*shnum
) * sizeof (Cache
))) == NULL
) {
1498 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_MALLOC
),
1499 file
, strerror(err
));
1500 sinfo_free(secarr
, num_sinfo
);
1501 sinfo_list_free_all(&seclist
);
1504 *_cache
= cache_init
;
1507 for (sinfo
= secarr
; num_sinfo
> 0; sinfo
++) {
1508 if (sinfo
->data
!= NULL
) {
1509 _cache
->c_scn
= NULL
;
1510 _cache
->c_shdr
= sinfo
->shdr
;
1511 _cache
->c_data
= sinfo
->data
;
1512 _cache
->c_name
= (char *)sinfo_data
[sinfo
->type
].name
;
1513 _cache
->c_ndx
= ndx
++;
1518 for (sinfo_list
= seclist
.next
; num_list_sinfo
> 0;
1519 sinfo_list
= sinfo_list
->next
) {
1520 sinfo
= &sinfo_list
->sinfo
;
1521 if (sinfo
->data
!= NULL
) {
1522 _cache
->c_scn
= NULL
;
1523 _cache
->c_shdr
= sinfo
->shdr
;
1524 _cache
->c_data
= sinfo
->data
;
1525 _cache
->c_name
= (char *)sinfo_data
[sinfo
->type
].name
;
1526 _cache
->c_ndx
= ndx
++;
1540 * Release all the memory referenced by a cache array allocated
1541 * by fake_shdr_cache().
1544 fake_shdr_cache_free(Cache
*cache
, size_t shnum
)
1548 for (_cache
= cache
; shnum
--; _cache
++) {
1549 if (_cache
->c_data
!= NULL
) {
1550 if (_cache
->c_data
->d_buf
!= NULL
)
1551 free(_cache
->c_data
->d_buf
);
1552 free(_cache
->c_data
);
1555 free(_cache
->c_shdr
);