add some utils
[libelf-compat.git] / src / gelf_getphdr.c
blobfa2826138f15b1138aec75c550f44d47e24b64a0
1 /* Return program header table entry.
2 Copyright (C) 1998-2010 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/>. */
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
34 #include <string.h>
35 #include <stdbool.h>
37 #include "libelfP.h"
38 #include "gelf.h"
39 #include "elf.h"
42 GElf_Phdr *
43 gelf_getphdr (elf, ndx, dst)
44 Elf *elf;
45 int ndx;
46 GElf_Phdr *dst;
48 GElf_Phdr *result = NULL;
50 if (elf == NULL)
51 return NULL;
53 if (unlikely (elf->kind != ELF_K_ELF))
55 __libelf_seterrno (ELF_E_INVALID_HANDLE);
56 return NULL;
59 if (dst == NULL)
61 __libelf_seterrno (ELF_E_INVALID_OPERAND);
62 return NULL;
65 rwlock_rdlock (elf->lock);
67 if (elf->class == ELFCLASS32)
69 /* Copy the elements one-by-one. */
70 Elf32_Phdr *phdr = elf->state.elf32.phdr;
72 if (phdr == NULL)
74 rwlock_unlock (elf->lock);
75 phdr = INTUSE(elf32_getphdr) (elf);
76 if (phdr == NULL)
77 /* The error number is already set. */
78 return NULL;
79 rwlock_rdlock (elf->lock);
82 /* Test whether the index is ok. */
83 size_t phnum;
84 if (ndx >= elf->state.elf32.ehdr->e_phnum
85 && (elf->state.elf32.ehdr->e_phnum != PN_XNUM
86 || __elf_getphdrnum_rdlock (elf, &phnum) != 0
87 || (size_t) ndx >= phnum))
89 __libelf_seterrno (ELF_E_INVALID_INDEX);
90 goto out;
93 /* We know the result now. */
94 result = dst;
96 /* Now correct the pointer to point to the correct element. */
97 phdr += ndx;
99 #define COPY(Name) result->Name = phdr->Name
100 COPY (p_type);
101 COPY (p_offset);
102 COPY (p_vaddr);
103 COPY (p_paddr);
104 COPY (p_filesz);
105 COPY (p_memsz);
106 COPY (p_flags);
107 COPY (p_align);
109 else
111 /* Copy the elements one-by-one. */
112 Elf64_Phdr *phdr = elf->state.elf64.phdr;
114 if (phdr == NULL)
116 rwlock_unlock (elf->lock);
117 phdr = INTUSE(elf64_getphdr) (elf);
118 if (phdr == NULL)
119 /* The error number is already set. */
120 return NULL;
121 rwlock_rdlock (elf->lock);
124 /* Test whether the index is ok. */
125 size_t phnum;
126 if (ndx >= elf->state.elf64.ehdr->e_phnum
127 && (elf->state.elf64.ehdr->e_phnum != PN_XNUM
128 || __elf_getphdrnum_rdlock (elf, &phnum) != 0
129 || (size_t) ndx >= phnum))
131 __libelf_seterrno (ELF_E_INVALID_INDEX);
132 goto out;
135 /* We only have to copy the data. */
136 result = memcpy (dst, phdr + ndx, sizeof (GElf_Phdr));
139 out:
140 rwlock_unlock (elf->lock);
142 return result;