]> git.ipfire.org Git - thirdparty/elfutils.git/blame - libelf/gelf_getphdr.c
funcretval-struct
[thirdparty/elfutils.git] / libelf / gelf_getphdr.c
CommitLineData
b08d5a8f 1/* Return program header table entry.
1ccdfb68 2 Copyright (C) 1998-2010, 2015 Red Hat, Inc.
de2ed97f 3 This file is part of elfutils.
b08d5a8f
UD
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
de2ed97f
MW
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
b08d5a8f 8
de2ed97f
MW
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
12
13 or
14
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
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
361df7da
UD
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.
b08d5a8f 25
de2ed97f
MW
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/>. */
b08d5a8f
UD
29
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <gelf.h>
35#include <string.h>
b4d6f0f8 36#include <stdbool.h>
b08d5a8f
UD
37
38#include "libelfP.h"
39
40
41GElf_Phdr *
1ccdfb68 42gelf_getphdr (Elf *elf, int ndx, GElf_Phdr *dst)
b08d5a8f
UD
43{
44 GElf_Phdr *result = NULL;
45
46 if (elf == NULL)
47 return NULL;
48
49 if (unlikely (elf->kind != ELF_K_ELF))
50 {
51 __libelf_seterrno (ELF_E_INVALID_HANDLE);
52 return NULL;
53 }
54
55 if (dst == NULL)
56 {
57 __libelf_seterrno (ELF_E_INVALID_OPERAND);
58 return NULL;
59 }
60
b4d6f0f8 61 rwlock_rdlock (elf->lock);
b08d5a8f
UD
62
63 if (elf->class == ELFCLASS32)
64 {
65 /* Copy the elements one-by-one. */
66 Elf32_Phdr *phdr = elf->state.elf32.phdr;
67
68 if (phdr == NULL)
69 {
b4d6f0f8
RM
70 rwlock_unlock (elf->lock);
71 phdr = INTUSE(elf32_getphdr) (elf);
b08d5a8f
UD
72 if (phdr == NULL)
73 /* The error number is already set. */
b4d6f0f8
RM
74 return NULL;
75 rwlock_rdlock (elf->lock);
b08d5a8f
UD
76 }
77
78 /* Test whether the index is ok. */
6fd3cd10 79 size_t phnum;
cc62e378
MW
80 if (__elf_getphdrnum_chk_rdlock (elf, &phnum) != 0
81 || (size_t) ndx >= phnum)
b08d5a8f
UD
82 {
83 __libelf_seterrno (ELF_E_INVALID_INDEX);
84 goto out;
85 }
86
87 /* We know the result now. */
88 result = dst;
89
90 /* Now correct the pointer to point to the correct element. */
91 phdr += ndx;
92
93#define COPY(Name) result->Name = phdr->Name
94 COPY (p_type);
95 COPY (p_offset);
96 COPY (p_vaddr);
97 COPY (p_paddr);
98 COPY (p_filesz);
99 COPY (p_memsz);
100 COPY (p_flags);
101 COPY (p_align);
102 }
103 else
104 {
105 /* Copy the elements one-by-one. */
106 Elf64_Phdr *phdr = elf->state.elf64.phdr;
107
108 if (phdr == NULL)
109 {
b4d6f0f8
RM
110 rwlock_unlock (elf->lock);
111 phdr = INTUSE(elf64_getphdr) (elf);
b08d5a8f
UD
112 if (phdr == NULL)
113 /* The error number is already set. */
b4d6f0f8
RM
114 return NULL;
115 rwlock_rdlock (elf->lock);
b08d5a8f
UD
116 }
117
118 /* Test whether the index is ok. */
6fd3cd10 119 size_t phnum;
cc62e378
MW
120 if (__elf_getphdrnum_chk_rdlock (elf, &phnum) != 0
121 || (size_t) ndx >= phnum)
b08d5a8f
UD
122 {
123 __libelf_seterrno (ELF_E_INVALID_INDEX);
124 goto out;
125 }
126
127 /* We only have to copy the data. */
128 result = memcpy (dst, phdr + ndx, sizeof (GElf_Phdr));
129 }
130
131 out:
b4d6f0f8 132 rwlock_unlock (elf->lock);
b08d5a8f
UD
133
134 return result;
135}