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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
33 * Find elf or it's class from a pointer to an Elf_Data struct.
34 * Warning: this Assumes that the Elf_Data is part of a libelf
35 * Dnode structure, which is expected to be true for any Elf_Data
36 * passed into libelf *except* for the xlatetof() and xlatetom() functions.
38 #define EDATA_CLASS(edata) \
39 (((Dnode *)(edata))->db_scn->s_elf->ed_class)
41 #define EDATA_ELF(edata) \
42 (((Dnode *)(edata))->db_scn->s_elf)
44 #define EDATA_SCN(edata) \
45 (((Dnode *)(edata))->db_scn)
47 #define EDATA_READLOCKS(edata) \
48 READLOCKS(EDATA_ELF((edata)), EDATA_SCN((edata)))
50 #define EDATA_READUNLOCKS(edata) \
51 READUNLOCKS(EDATA_ELF((edata)), EDATA_SCN((edata)))
55 gelf_fsize(Elf
* elf
, Elf_Type type
, size_t count
, unsigned ver
)
62 class = gelf_getclass(elf
);
63 if (class == ELFCLASS32
)
64 return (elf32_fsize(type
, count
, ver
));
65 else if (class == ELFCLASS64
)
66 return (elf64_fsize(type
, count
, ver
));
68 _elf_seterr(EREQ_CLASS
, 0);
74 gelf_getclass(Elf
*elf
)
80 * Don't rely on the idents, a new ehdr doesn't have it!
82 return (elf
->ed_class
);
87 gelf_getehdr(Elf
*elf
, GElf_Ehdr
*dst
)
94 class = gelf_getclass(elf
);
95 if (class == ELFCLASS32
) {
96 Elf32_Ehdr
* e
= elf32_getehdr(elf
);
102 (void) memcpy(dst
->e_ident
, e
->e_ident
, EI_NIDENT
);
103 dst
->e_type
= e
->e_type
;
104 dst
->e_machine
= e
->e_machine
;
105 dst
->e_version
= e
->e_version
;
106 dst
->e_entry
= (Elf64_Addr
)e
->e_entry
;
107 dst
->e_phoff
= (Elf64_Off
)e
->e_phoff
;
108 dst
->e_shoff
= (Elf64_Off
)e
->e_shoff
;
109 dst
->e_flags
= e
->e_flags
;
110 dst
->e_ehsize
= e
->e_ehsize
;
111 dst
->e_phentsize
= e
->e_phentsize
;
112 dst
->e_phnum
= e
->e_phnum
;
113 dst
->e_shentsize
= e
->e_shentsize
;
114 dst
->e_shnum
= e
->e_shnum
;
115 dst
->e_shstrndx
= e
->e_shstrndx
;
119 } else if (class == ELFCLASS64
) {
120 Elf64_Ehdr
* e
= elf64_getehdr(elf
);
132 _elf_seterr(EREQ_CLASS
, 0);
138 gelf_update_ehdr(Elf
*elf
, GElf_Ehdr
*src
)
146 * In case elf isn't cooked.
148 class = gelf_getclass(elf
);
149 if (class == ELFCLASSNONE
)
150 class = src
->e_ident
[EI_CLASS
];
153 if (class == ELFCLASS32
) {
154 Elf32_Ehdr
* d
= elf32_getehdr(elf
);
160 (void) memcpy(d
->e_ident
, src
->e_ident
, EI_NIDENT
);
161 d
->e_type
= src
->e_type
;
162 d
->e_machine
= src
->e_machine
;
163 d
->e_version
= src
->e_version
;
165 d
->e_entry
= (Elf32_Addr
)src
->e_entry
;
167 d
->e_phoff
= (Elf32_Off
)src
->e_phoff
;
169 d
->e_shoff
= (Elf32_Off
)src
->e_shoff
;
170 /* could memcpy the rest of these... */
171 d
->e_flags
= src
->e_flags
;
172 d
->e_ehsize
= src
->e_ehsize
;
173 d
->e_phentsize
= src
->e_phentsize
;
174 d
->e_phnum
= src
->e_phnum
;
175 d
->e_shentsize
= src
->e_shentsize
;
176 d
->e_shnum
= src
->e_shnum
;
177 d
->e_shstrndx
= src
->e_shstrndx
;
181 } else if (class == ELFCLASS64
) {
182 Elf64_Ehdr
* d
= elf64_getehdr(elf
);
188 *d
= *(Elf64_Ehdr
*)src
;
194 _elf_seterr(EREQ_CLASS
, 0);
200 gelf_newehdr(Elf
*elf
, int class)
205 if (class == ELFCLASS32
)
206 return ((unsigned long)elf32_newehdr(elf
));
207 else if (class == ELFCLASS64
)
208 return ((unsigned long)elf64_newehdr(elf
));
210 _elf_seterr(EREQ_CLASS
, 0);
216 gelf_getphdr(Elf
*elf
, int ndx
, GElf_Phdr
*dst
)
224 if (elf_getphdrnum(elf
, &phnum
) == -1)
228 _elf_seterr(EREQ_RAND
, 0);
232 class = gelf_getclass(elf
);
233 if ((class != ELFCLASS32
) && (class != ELFCLASS64
)) {
234 _elf_seterr(EREQ_CLASS
, 0);
238 if (class == ELFCLASS32
) {
239 Elf32_Phdr
*p
= &((Elf32_Phdr
*)elf32_getphdr(elf
))[ndx
];
242 dst
->p_type
= p
->p_type
;
243 dst
->p_flags
= p
->p_flags
;
244 dst
->p_offset
= (Elf64_Off
)p
->p_offset
;
245 dst
->p_vaddr
= (Elf64_Addr
)p
->p_vaddr
;
246 dst
->p_paddr
= (Elf64_Addr
)p
->p_paddr
;
247 dst
->p_filesz
= (Elf64_Xword
)p
->p_filesz
;
248 dst
->p_memsz
= (Elf64_Xword
)p
->p_memsz
;
249 dst
->p_align
= (Elf64_Xword
)p
->p_align
;
251 } else if (class == ELFCLASS64
) {
252 Elf64_Phdr
*phdrs
= elf64_getphdr(elf
);
254 *dst
= ((GElf_Phdr
*)phdrs
)[ndx
];
263 gelf_update_phdr(Elf
*elf
, int ndx
, GElf_Phdr
*src
)
271 if (elf_getphdrnum(elf
, &phnum
) == -1)
275 _elf_seterr(EREQ_RAND
, 0);
279 class = gelf_getclass(elf
);
280 if (class == ELFCLASS32
) {
281 Elf32_Phdr
*dst
= &((Elf32_Phdr
*)elf32_getphdr(elf
))[ndx
];
283 dst
->p_type
= src
->p_type
;
284 dst
->p_flags
= src
->p_flags
;
286 dst
->p_offset
= (Elf32_Off
)src
->p_offset
;
288 dst
->p_vaddr
= (Elf32_Addr
)src
->p_vaddr
;
290 dst
->p_paddr
= (Elf32_Addr
)src
->p_paddr
;
292 dst
->p_filesz
= (Elf32_Word
)src
->p_filesz
;
294 dst
->p_memsz
= (Elf32_Word
)src
->p_memsz
;
296 dst
->p_align
= (Elf32_Word
)src
->p_align
;
298 } else if (class == ELFCLASS64
) {
299 Elf64_Phdr
*dst
= elf64_getphdr(elf
);
301 dst
[ndx
] = *(GElf_Phdr
*)src
;
304 _elf_seterr(EREQ_CLASS
, 0);
312 gelf_newphdr(Elf
*elf
, size_t phnum
)
319 class = gelf_getclass(elf
);
320 if (class == ELFCLASS32
)
321 return ((unsigned long)elf32_newphdr(elf
, phnum
));
322 else if (class == ELFCLASS64
)
323 return ((unsigned long)elf64_newphdr(elf
, phnum
));
325 _elf_seterr(EREQ_CLASS
, 0);
331 gelf_getshdr(Elf_Scn
*scn
, GElf_Shdr
*dst
)
336 if (scn
->s_elf
->ed_class
== ELFCLASS32
) {
337 Elf32_Shdr
*s
= elf32_getshdr(scn
);
342 READLOCKS(scn
->s_elf
, scn
);
343 dst
->sh_name
= s
->sh_name
;
344 dst
->sh_type
= s
->sh_type
;
345 dst
->sh_flags
= (Elf64_Xword
)s
->sh_flags
;
346 dst
->sh_addr
= (Elf64_Addr
)s
->sh_addr
;
347 dst
->sh_offset
= (Elf64_Off
)s
->sh_offset
;
348 dst
->sh_size
= (Elf64_Xword
)s
->sh_size
;
349 dst
->sh_link
= s
->sh_link
;
350 dst
->sh_info
= s
->sh_info
;
351 dst
->sh_addralign
= (Elf64_Xword
)s
->sh_addralign
;
352 dst
->sh_entsize
= (Elf64_Xword
)s
->sh_entsize
;
353 READUNLOCKS(scn
->s_elf
, scn
);
356 } else if (scn
->s_elf
->ed_class
== ELFCLASS64
) {
357 Elf64_Shdr
*s
= elf64_getshdr(scn
);
362 READLOCKS(scn
->s_elf
, scn
);
363 *dst
= *(Elf64_Shdr
*)s
;
364 READUNLOCKS(scn
->s_elf
, scn
);
369 _elf_seterr(EREQ_CLASS
, 0);
375 gelf_update_shdr(Elf_Scn
*scn
, GElf_Shdr
*src
)
380 if (scn
->s_elf
->ed_class
== ELFCLASS32
) {
381 Elf32_Shdr
*dst
= elf32_getshdr(scn
);
386 ELFWLOCK(scn
->s_elf
);
387 dst
->sh_name
= src
->sh_name
;
388 dst
->sh_type
= src
->sh_type
;
390 dst
->sh_flags
= (Elf32_Word
)src
->sh_flags
;
392 dst
->sh_addr
= (Elf32_Addr
)src
->sh_addr
;
394 dst
->sh_offset
= (Elf32_Off
) src
->sh_offset
;
396 dst
->sh_size
= (Elf32_Word
)src
->sh_size
;
397 dst
->sh_link
= src
->sh_link
;
398 dst
->sh_info
= src
->sh_info
;
400 dst
->sh_addralign
= (Elf32_Word
)src
->sh_addralign
;
402 dst
->sh_entsize
= (Elf32_Word
)src
->sh_entsize
;
404 ELFUNLOCK(scn
->s_elf
);
406 } else if (scn
->s_elf
->ed_class
== ELFCLASS64
) {
407 Elf64_Shdr
* dst
= elf64_getshdr(scn
);
412 ELFWLOCK(scn
->s_elf
);
413 *dst
= *(Elf64_Shdr
*)src
;
414 ELFUNLOCK(scn
->s_elf
);
418 _elf_seterr(EREQ_CLASS
, 0);
424 * gelf_xlatetof/gelf_xlatetom use 'elf' to find the class
425 * because these are the odd case where the Elf_Data structs
426 * might not have been allocated by libelf (and therefore
427 * don't have Dnode's associated with them).
430 gelf_xlatetof(Elf
*elf
, Elf_Data
*dst
, const Elf_Data
*src
, unsigned encode
)
434 if ((elf
== NULL
) || (dst
== NULL
) || (src
== NULL
))
437 class = gelf_getclass(elf
);
438 if (class == ELFCLASS32
)
439 return (elf32_xlatetof(dst
, src
, encode
));
440 else if (class == ELFCLASS64
)
441 return (elf64_xlatetof(dst
, src
, encode
));
443 _elf_seterr(EREQ_CLASS
, 0);
449 gelf_xlatetom(Elf
*elf
, Elf_Data
*dst
, const Elf_Data
*src
, unsigned encode
)
453 if ((elf
== NULL
) || (dst
== NULL
) || (src
== NULL
))
456 class = gelf_getclass(elf
);
457 if (class == ELFCLASS32
)
458 return (elf32_xlatetom(dst
, src
, encode
));
459 else if (class == ELFCLASS64
)
460 return (elf64_xlatetom(dst
, src
, encode
));
462 _elf_seterr(EREQ_CLASS
, 0);
468 gelf_getsym(Elf_Data
* data
, int ndx
, GElf_Sym
* dst
)
476 class = EDATA_CLASS(data
);
477 if (class == ELFCLASS32
)
478 entsize
= sizeof (Elf32_Sym
);
479 else if (class == ELFCLASS64
)
480 entsize
= sizeof (GElf_Sym
);
482 _elf_seterr(EREQ_CLASS
, 0);
486 EDATA_READLOCKS(data
);
488 if ((entsize
* ndx
) >= data
->d_size
) {
489 _elf_seterr(EREQ_RAND
, 0);
491 } else if (class == ELFCLASS32
) {
493 s
= &(((Elf32_Sym
*)data
->d_buf
)[ndx
]);
494 dst
->st_name
= s
->st_name
;
495 dst
->st_value
= (Elf64_Addr
)s
->st_value
;
496 dst
->st_size
= (Elf64_Xword
)s
->st_size
;
497 dst
->st_info
= ELF64_ST_INFO(ELF32_ST_BIND(s
->st_info
),
498 ELF32_ST_TYPE(s
->st_info
));
499 dst
->st_other
= s
->st_other
;
500 dst
->st_shndx
= s
->st_shndx
;
502 *dst
= ((GElf_Sym
*)data
->d_buf
)[ndx
];
504 EDATA_READUNLOCKS(data
);
510 gelf_update_sym(Elf_Data
*dst
, int ndx
, GElf_Sym
*src
)
518 class = EDATA_CLASS(dst
);
519 if (class == ELFCLASS32
)
520 entsize
= sizeof (Elf32_Sym
);
521 else if (class == ELFCLASS64
)
522 entsize
= sizeof (GElf_Sym
);
524 _elf_seterr(EREQ_CLASS
, 0);
528 ELFWLOCK(EDATA_ELF(dst
));
530 if ((entsize
* ndx
) >= dst
->d_size
) {
531 _elf_seterr(EREQ_RAND
, 0);
533 } else if (class == ELFCLASS32
) {
536 d
= &(((Elf32_Sym
*)dst
->d_buf
)[ndx
]);
537 d
->st_name
= src
->st_name
;
539 d
->st_value
= (Elf32_Addr
)src
->st_value
;
541 d
->st_size
= (Elf32_Word
)src
->st_size
;
542 d
->st_info
= ELF32_ST_INFO(ELF64_ST_BIND(src
->st_info
),
543 ELF64_ST_TYPE(src
->st_info
));
544 d
->st_other
= src
->st_other
;
545 d
->st_shndx
= src
->st_shndx
;
547 ((Elf64_Sym
*)dst
->d_buf
)[ndx
] = *((Elf64_Sym
*)src
);
549 ELFUNLOCK(EDATA_ELF(dst
));
555 gelf_getsyminfo(Elf_Data
*data
, int ndx
, GElf_Syminfo
*dst
)
563 class = EDATA_CLASS(data
);
564 if (class == ELFCLASS32
)
565 entsize
= sizeof (Elf32_Syminfo
);
566 else if (class == ELFCLASS64
)
567 entsize
= sizeof (GElf_Syminfo
);
569 _elf_seterr(EREQ_CLASS
, 0);
572 EDATA_READLOCKS(data
);
574 if ((entsize
* ndx
) >= data
->d_size
) {
575 _elf_seterr(EREQ_RAND
, 0);
577 } else if (class == ELFCLASS32
) {
580 si
= &(((Elf32_Syminfo
*)data
->d_buf
)[ndx
]);
581 dst
->si_boundto
= si
->si_boundto
;
582 dst
->si_flags
= si
->si_flags
;
584 *dst
= ((GElf_Syminfo
*)data
->d_buf
)[ndx
];
586 EDATA_READUNLOCKS(data
);
591 gelf_update_syminfo(Elf_Data
*dst
, int ndx
, GElf_Syminfo
*src
)
599 class = EDATA_CLASS(dst
);
600 if (class == ELFCLASS32
)
601 entsize
= sizeof (Elf32_Syminfo
);
602 else if (class == ELFCLASS64
)
603 entsize
= sizeof (GElf_Syminfo
);
605 _elf_seterr(EREQ_CLASS
, 0);
608 ELFWLOCK(EDATA_ELF(dst
));
610 if ((entsize
* ndx
) >= dst
->d_size
) {
611 _elf_seterr(EREQ_RAND
, 0);
613 } else if (class == ELFCLASS32
) {
614 Elf32_Syminfo
* d
= &(((Elf32_Syminfo
*)dst
->d_buf
)[ndx
]);
615 d
->si_boundto
= src
->si_boundto
;
616 d
->si_flags
= src
->si_flags
;
618 ((Elf64_Syminfo
*)dst
->d_buf
)[ndx
] = *((Elf64_Syminfo
*)src
);
620 ELFUNLOCK(EDATA_ELF(dst
));
625 gelf_getdyn(Elf_Data
*data
, int ndx
, GElf_Dyn
*dst
)
633 class = EDATA_CLASS(data
);
634 if (class == ELFCLASS32
)
635 entsize
= sizeof (Elf32_Dyn
);
636 else if (class == ELFCLASS64
)
637 entsize
= sizeof (GElf_Dyn
);
639 _elf_seterr(EREQ_CLASS
, 0);
642 EDATA_READLOCKS(data
);
644 if ((entsize
* ndx
) >= data
->d_size
) {
645 _elf_seterr(EREQ_RAND
, 0);
647 } else if (class == ELFCLASS32
) {
648 Elf32_Dyn
* d
= &((Elf32_Dyn
*)data
->d_buf
)[ndx
];
650 dst
->d_tag
= (Elf32_Sword
)d
->d_tag
;
651 dst
->d_un
.d_val
= (Elf32_Word
) d
->d_un
.d_val
;
653 *dst
= ((Elf64_Dyn
*)data
->d_buf
)[ndx
];
655 EDATA_READUNLOCKS(data
);
661 gelf_update_dyn(Elf_Data
*dst
, int ndx
, GElf_Dyn
*src
)
669 class = EDATA_CLASS(dst
);
670 if (class == ELFCLASS32
)
671 entsize
= sizeof (Elf32_Dyn
);
672 else if (class == ELFCLASS64
)
673 entsize
= sizeof (GElf_Dyn
);
675 _elf_seterr(EREQ_CLASS
, 0);
678 ELFWLOCK(EDATA_ELF(dst
));
680 if ((entsize
* ndx
) >= dst
->d_size
) {
681 _elf_seterr(EREQ_RAND
, 0);
683 } else if (class == ELFCLASS32
) {
684 Elf32_Dyn
* d
= &((Elf32_Dyn
*)dst
->d_buf
)[ndx
];
687 d
->d_tag
= (Elf32_Word
)src
->d_tag
;
689 d
->d_un
.d_val
= (Elf32_Word
)src
->d_un
.d_val
;
691 ((Elf64_Dyn
*)dst
->d_buf
)[ndx
] = *(Elf64_Dyn
*)src
;
693 ELFUNLOCK(EDATA_ELF(dst
));
700 gelf_getsymshndx(Elf_Data
*symdata
, Elf_Data
*shndxdata
,
701 int ndx
, GElf_Sym
*symptr
, Elf32_Word
*xshndx
)
703 if (gelf_getsym(symdata
, ndx
, symptr
) == 0)
705 if (shndxdata
&& xshndx
) {
706 EDATA_READLOCKS(shndxdata
);
707 if ((ndx
* sizeof (Elf32_Word
)) >= shndxdata
->d_size
) {
708 _elf_seterr(EREQ_RAND
, 0);
709 EDATA_READUNLOCKS(shndxdata
);
712 *xshndx
= (((Elf32_Word
*)shndxdata
->d_buf
)[ndx
]);
713 EDATA_READUNLOCKS(shndxdata
);
721 gelf_update_symshndx(Elf_Data
*symdata
, Elf_Data
*shndxdata
,
722 int ndx
, GElf_Sym
*symptr
, Elf32_Word xshndx
)
724 if (gelf_update_sym(symdata
, ndx
, symptr
) == 0)
727 ELFWLOCK(EDATA_ELF(shndxdata
));
728 if ((ndx
* sizeof (Elf32_Word
)) >= shndxdata
->d_size
) {
729 _elf_seterr(EREQ_RAND
, 0);
730 ELFUNLOCK(EDATA_ELF(shndxdata
));
733 ((Elf32_Word
*)shndxdata
->d_buf
)[ndx
] = xshndx
;
734 ELFUNLOCK(EDATA_ELF(shndxdata
));
741 gelf_getmove(Elf_Data
*src
, int ndx
, GElf_Move
*dst
)
749 class = EDATA_CLASS(src
);
750 if (class == ELFCLASS32
)
751 entsize
= sizeof (Elf32_Move
);
752 else if (class == ELFCLASS64
)
753 entsize
= sizeof (GElf_Move
);
755 _elf_seterr(EREQ_CLASS
, 0);
758 EDATA_READLOCKS(src
);
760 if ((entsize
* ndx
) >= src
->d_size
) {
761 _elf_seterr(EREQ_RAND
, 0);
763 } else if (class == ELFCLASS32
) {
764 Elf32_Move
* m
= &((Elf32_Move
*)src
->d_buf
)[ndx
];
766 dst
->m_poffset
= (Elf64_Word
)m
->m_poffset
;
767 dst
->m_repeat
= (Elf64_Xword
)m
->m_repeat
;
768 dst
->m_stride
= (Elf64_Half
)m
->m_stride
;
769 dst
->m_value
= (Elf64_Xword
)m
->m_value
;
770 dst
->m_info
= ELF64_M_INFO(ELF32_M_SYM(m
->m_info
),
771 ELF32_M_SIZE(m
->m_info
));
773 *dst
= ((Elf64_Move
*)src
->d_buf
)[ndx
];
776 EDATA_READUNLOCKS(src
);
781 gelf_update_move(Elf_Data
*dest
, int ndx
, GElf_Move
*src
)
789 class = EDATA_CLASS(dest
);
790 if (class == ELFCLASS32
)
791 entsize
= sizeof (Elf32_Move
);
792 else if (class == ELFCLASS64
)
793 entsize
= sizeof (GElf_Move
);
795 _elf_seterr(EREQ_CLASS
, 0);
798 ELFWLOCK(EDATA_ELF(dest
));
800 if ((entsize
* ndx
) >= dest
->d_size
) {
801 _elf_seterr(EREQ_RAND
, 0);
803 } else if (class == ELFCLASS32
) {
804 Elf32_Move
* m
= &((Elf32_Move
*)dest
->d_buf
)[ndx
];
806 m
->m_poffset
= (Elf32_Word
)src
->m_poffset
;
807 m
->m_repeat
= (Elf32_Half
)src
->m_repeat
;
808 m
->m_stride
= (Elf32_Half
)src
->m_stride
;
809 m
->m_value
= (Elf32_Lword
)src
->m_value
;
810 m
->m_info
= (Elf32_Word
)ELF32_M_INFO(ELF64_M_SYM(src
->m_info
),
811 ELF64_M_SIZE(src
->m_info
));
813 ((Elf64_Move
*)dest
->d_buf
)[ndx
] = *(Elf64_Move
*)src
;
816 ELFUNLOCK(EDATA_ELF(dest
));
822 gelf_getrela(Elf_Data
*src
, int ndx
, GElf_Rela
*dst
)
830 class = EDATA_CLASS(src
);
831 if (class == ELFCLASS32
)
832 entsize
= sizeof (Elf32_Rela
);
833 else if (class == ELFCLASS64
)
834 entsize
= sizeof (GElf_Rela
);
836 _elf_seterr(EREQ_CLASS
, 0);
839 EDATA_READLOCKS(src
);
841 if ((entsize
* ndx
) >= src
->d_size
) {
842 _elf_seterr(EREQ_RAND
, 0);
844 } else if (class == ELFCLASS32
) {
845 Elf32_Rela
* r
= &((Elf32_Rela
*)src
->d_buf
)[ndx
];
847 dst
->r_offset
= (GElf_Addr
)r
->r_offset
;
848 dst
->r_addend
= (GElf_Addr
)r
->r_addend
;
851 * Elf32 will never have the extra data field that
852 * Elf64's r_info field can have, so ignore it.
855 dst
->r_info
= ELF64_R_INFO(
856 ELF32_R_SYM(r
->r_info
),
857 ELF32_R_TYPE(r
->r_info
));
859 *dst
= ((Elf64_Rela
*)src
->d_buf
)[ndx
];
861 EDATA_READUNLOCKS(src
);
867 gelf_update_rela(Elf_Data
*dst
, int ndx
, GElf_Rela
*src
)
875 class = EDATA_CLASS(dst
);
876 if (class == ELFCLASS32
)
877 entsize
= sizeof (Elf32_Rela
);
878 else if (class == ELFCLASS64
)
879 entsize
= sizeof (GElf_Rela
);
881 _elf_seterr(EREQ_CLASS
, 0);
884 ELFWLOCK(EDATA_ELF(dst
));
886 if ((entsize
* ndx
) >= dst
->d_size
) {
887 _elf_seterr(EREQ_RAND
, 0);
889 } else if (class == ELFCLASS32
) {
890 Elf32_Rela
* r
= &((Elf32_Rela
*)dst
->d_buf
)[ndx
];
893 r
->r_offset
= (Elf32_Addr
) src
->r_offset
;
895 r
->r_addend
= (Elf32_Sword
)src
->r_addend
;
898 * Elf32 will never have the extra data field that
899 * Elf64's r_info field can have, so ignore it.
902 r
->r_info
= ELF32_R_INFO(ELF64_R_SYM(src
->r_info
),
903 ELF64_R_TYPE(src
->r_info
));
905 ((Elf64_Rela
*)dst
->d_buf
)[ndx
] = *(Elf64_Rela
*)src
;
908 ELFUNLOCK(EDATA_ELF(dst
));
915 gelf_getrel(Elf_Data
*src
, int ndx
, GElf_Rel
*dst
)
923 class = EDATA_CLASS(src
);
924 if (class == ELFCLASS32
)
925 entsize
= sizeof (Elf32_Rel
);
926 else if (class == ELFCLASS64
)
927 entsize
= sizeof (GElf_Rel
);
929 _elf_seterr(EREQ_CLASS
, 0);
932 EDATA_READLOCKS(src
);
934 if ((entsize
* ndx
) >= src
->d_size
) {
935 _elf_seterr(EREQ_RAND
, 0);
937 } else if (class == ELFCLASS32
) {
938 Elf32_Rel
* r
= &((Elf32_Rel
*)src
->d_buf
)[ndx
];
940 dst
->r_offset
= (GElf_Addr
)r
->r_offset
;
943 * Elf32 will never have the extra data field that
944 * Elf64's r_info field can have, so ignore it.
947 dst
->r_info
= ELF64_R_INFO(ELF32_R_SYM(r
->r_info
),
948 ELF32_R_TYPE(r
->r_info
));
950 *dst
= ((Elf64_Rel
*)src
->d_buf
)[ndx
];
952 EDATA_READUNLOCKS(src
);
958 gelf_update_rel(Elf_Data
*dst
, int ndx
, GElf_Rel
*src
)
966 class = EDATA_CLASS(dst
);
967 if (class == ELFCLASS32
)
968 entsize
= sizeof (Elf32_Rel
);
969 else if (class == ELFCLASS64
)
970 entsize
= sizeof (GElf_Rel
);
972 _elf_seterr(EREQ_CLASS
, 0);
975 ELFWLOCK(EDATA_ELF(dst
));
977 if ((entsize
* ndx
) >= dst
->d_size
) {
978 _elf_seterr(EREQ_RAND
, 0);
980 } else if (class == ELFCLASS32
) {
981 Elf32_Rel
* r
= &((Elf32_Rel
*)dst
->d_buf
)[ndx
];
984 r
->r_offset
= (Elf32_Addr
) src
->r_offset
;
987 * Elf32 will never have the extra data field that
988 * Elf64's r_info field can have, so ignore it.
991 r
->r_info
= ELF32_R_INFO(ELF64_R_SYM(src
->r_info
),
992 ELF64_R_TYPE(src
->r_info
));
995 ((Elf64_Rel
*)dst
->d_buf
)[ndx
] = *(Elf64_Rel
*)src
;
998 ELFUNLOCK(EDATA_ELF(dst
));
1003 gelf_checksum(Elf
*elf
)
1005 int class = gelf_getclass(elf
);
1007 if (class == ELFCLASS32
)
1008 return (elf32_checksum(elf
));
1009 else if (class == ELFCLASS64
)
1010 return (elf64_checksum(elf
));
1012 _elf_seterr(EREQ_CLASS
, 0);
1017 gelf_getcap(Elf_Data
*data
, int ndx
, GElf_Cap
*dst
)
1025 class = EDATA_CLASS(data
);
1026 if (class == ELFCLASS32
)
1027 entsize
= sizeof (Elf32_Cap
);
1028 else if (class == ELFCLASS64
)
1029 entsize
= sizeof (GElf_Cap
);
1031 _elf_seterr(EREQ_CLASS
, 0);
1035 EDATA_READLOCKS(data
);
1037 if ((entsize
* ndx
) >= data
->d_size
) {
1038 _elf_seterr(EREQ_RAND
, 0);
1040 } else if (class == ELFCLASS32
) {
1041 Elf32_Cap
*c
= &(((Elf32_Cap
*)data
->d_buf
)[ndx
]);
1043 dst
->c_tag
= (Elf64_Xword
)c
->c_tag
;
1044 dst
->c_un
.c_val
= (Elf64_Xword
)c
->c_un
.c_val
;
1046 *dst
= ((GElf_Cap
*)data
->d_buf
)[ndx
];
1048 EDATA_READUNLOCKS(data
);
1053 gelf_update_cap(Elf_Data
*dst
, int ndx
, GElf_Cap
*src
)
1061 class = EDATA_CLASS(dst
);
1062 if (class == ELFCLASS32
)
1063 entsize
= sizeof (Elf32_Cap
);
1064 else if (class == ELFCLASS64
)
1065 entsize
= sizeof (GElf_Cap
);
1067 _elf_seterr(EREQ_CLASS
, 0);
1071 ELFWLOCK(EDATA_ELF(dst
));
1073 if ((entsize
* ndx
) >= dst
->d_size
) {
1074 _elf_seterr(EREQ_RAND
, 0);
1076 } else if (class == ELFCLASS32
) {
1077 Elf32_Cap
*c
= &(((Elf32_Cap
*)dst
->d_buf
)[ndx
]);
1079 c
->c_tag
= (Elf32_Word
)src
->c_tag
;
1080 c
->c_un
.c_val
= (Elf32_Word
)src
->c_un
.c_val
;
1082 ((Elf64_Cap
*)dst
->d_buf
)[ndx
] = *((Elf64_Cap
*)src
);
1084 ELFUNLOCK(EDATA_ELF(dst
));
1089 * If the specified object has a dynamic section, and that section
1090 * contains a DT_FLAGS_1 entry, then return the value of that entry.
1091 * Otherwise, return 0.
1094 _gelf_getdyndtflags_1(Elf
*elf
)
1096 Elf_Scn
*scn
= NULL
;
1102 while (scn
= elf_nextscn(elf
, scn
)) {
1103 if (gelf_getshdr(scn
, &shdr
) == NULL
)
1105 if (shdr
.sh_type
!= SHT_DYNAMIC
)
1107 if (data
= elf_getdata(scn
, NULL
)) {
1108 n
= shdr
.sh_size
/ shdr
.sh_entsize
;
1109 for (i
= 0; i
< n
; i
++) {
1110 (void) gelf_getdyn(data
, i
, &dyn
);
1111 if (dyn
.d_tag
== DT_FLAGS_1
) {
1112 return (dyn
.d_un
.d_val
);