blob: 3bf7123f7acf485bf67e730a89018d5638ea2df0 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Return program header table entry.
Roland McGrath6fd3cd12010-01-07 19:41:04 -08002 Copyright (C) 1998-2010 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <gelf.h>
35#include <string.h>
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000036#include <stdbool.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000037
38#include "libelfP.h"
39
40
41GElf_Phdr *
42gelf_getphdr (elf, ndx, dst)
43 Elf *elf;
44 int ndx;
45 GElf_Phdr *dst;
46{
47 GElf_Phdr *result = NULL;
48
49 if (elf == NULL)
50 return NULL;
51
52 if (unlikely (elf->kind != ELF_K_ELF))
53 {
54 __libelf_seterrno (ELF_E_INVALID_HANDLE);
55 return NULL;
56 }
57
58 if (dst == NULL)
59 {
60 __libelf_seterrno (ELF_E_INVALID_OPERAND);
61 return NULL;
62 }
63
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000064 rwlock_rdlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000065
66 if (elf->class == ELFCLASS32)
67 {
68 /* Copy the elements one-by-one. */
69 Elf32_Phdr *phdr = elf->state.elf32.phdr;
70
71 if (phdr == NULL)
72 {
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000073 rwlock_unlock (elf->lock);
74 phdr = INTUSE(elf32_getphdr) (elf);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000075 if (phdr == NULL)
76 /* The error number is already set. */
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000077 return NULL;
78 rwlock_rdlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000079 }
80
81 /* Test whether the index is ok. */
Roland McGrath6fd3cd12010-01-07 19:41:04 -080082 size_t phnum;
83 if (ndx >= elf->state.elf32.ehdr->e_phnum
84 && (elf->state.elf32.ehdr->e_phnum != PN_XNUM
85 || __elf_getphdrnum_rdlock (elf, &phnum) != 0
86 || (size_t) ndx >= phnum))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000087 {
88 __libelf_seterrno (ELF_E_INVALID_INDEX);
89 goto out;
90 }
91
92 /* We know the result now. */
93 result = dst;
94
95 /* Now correct the pointer to point to the correct element. */
96 phdr += ndx;
97
98#define COPY(Name) result->Name = phdr->Name
99 COPY (p_type);
100 COPY (p_offset);
101 COPY (p_vaddr);
102 COPY (p_paddr);
103 COPY (p_filesz);
104 COPY (p_memsz);
105 COPY (p_flags);
106 COPY (p_align);
107 }
108 else
109 {
110 /* Copy the elements one-by-one. */
111 Elf64_Phdr *phdr = elf->state.elf64.phdr;
112
113 if (phdr == NULL)
114 {
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000115 rwlock_unlock (elf->lock);
116 phdr = INTUSE(elf64_getphdr) (elf);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000117 if (phdr == NULL)
118 /* The error number is already set. */
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000119 return NULL;
120 rwlock_rdlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000121 }
122
123 /* Test whether the index is ok. */
Roland McGrath6fd3cd12010-01-07 19:41:04 -0800124 size_t phnum;
125 if (ndx >= elf->state.elf64.ehdr->e_phnum
126 && (elf->state.elf64.ehdr->e_phnum != PN_XNUM
127 || __elf_getphdrnum_rdlock (elf, &phnum) != 0
128 || (size_t) ndx >= phnum))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000129 {
130 __libelf_seterrno (ELF_E_INVALID_INDEX);
131 goto out;
132 }
133
134 /* We only have to copy the data. */
135 result = memcpy (dst, phdr + ndx, sizeof (GElf_Phdr));
136 }
137
138 out:
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000139 rwlock_unlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000140
141 return result;
142}