1 /* Return program header table entry.
2 Copyright (C) 1998-2010, 2015 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
42 gelf_getphdr (Elf *elf, int ndx, GElf_Phdr *dst)
44 GElf_Phdr *result = NULL;
49 if (unlikely (elf->kind != ELF_K_ELF))
51 __libelf_seterrno (ELF_E_INVALID_HANDLE);
57 __libelf_seterrno (ELF_E_INVALID_OPERAND);
61 rwlock_rdlock (elf->lock);
63 if (elf->class == ELFCLASS32)
65 /* Copy the elements one-by-one. */
66 Elf32_Phdr *phdr = elf->state.elf32.phdr;
70 rwlock_unlock (elf->lock);
71 phdr = INTUSE(elf32_getphdr) (elf);
73 /* The error number is already set. */
75 rwlock_rdlock (elf->lock);
78 /* Test whether the index is ok. */
80 if (__elf_getphdrnum_chk_rdlock (elf, &phnum) != 0
81 || (size_t) ndx >= phnum)
83 __libelf_seterrno (ELF_E_INVALID_INDEX);
87 /* We know the result now. */
90 /* Now correct the pointer to point to the correct element. */
93 #define COPY(Name) result->Name = phdr->Name
105 /* Copy the elements one-by-one. */
106 Elf64_Phdr *phdr = elf->state.elf64.phdr;
110 rwlock_unlock (elf->lock);
111 phdr = INTUSE(elf64_getphdr) (elf);
113 /* The error number is already set. */
115 rwlock_rdlock (elf->lock);
118 /* Test whether the index is ok. */
120 if (__elf_getphdrnum_chk_rdlock (elf, &phnum) != 0
121 || (size_t) ndx >= phnum)
123 __libelf_seterrno (ELF_E_INVALID_INDEX);
127 /* We only have to copy the data. */
128 result = memcpy (dst, phdr + ndx, sizeof (GElf_Phdr));
132 rwlock_unlock (elf->lock);