blob: 99649beef777babb746194899e0bb1e6d4b72397 [file] [log] [blame]
Roland McGrath6fd3cd12010-01-07 19:41:04 -08001/* Return number of program headers in the ELF file.
2 Copyright (C) 2010 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Roland McGrath6fd3cd12010-01-07 19:41:04 -08004
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
Roland McGrath6fd3cd12010-01-07 19:41:04 -08007
Mark Wielaardde2ed972012-06-05 17:15:16 +02008 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
11
12 or
13
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
17
18 or both in parallel, as here.
19
20 elfutils is distributed in the hope that it will be useful, but
Roland McGrath6fd3cd12010-01-07 19:41:04 -080021 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
Mark Wielaardde2ed972012-06-05 17:15:16 +020025 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
Roland McGrath6fd3cd12010-01-07 19:41:04 -080028
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include <assert.h>
34#include <gelf.h>
35#include <stddef.h>
36
37#include "libelfP.h"
38
39
40int
41__elf_getphdrnum_rdlock (elf, dst)
42 Elf *elf;
43 size_t *dst;
44{
45 if (unlikely (elf->state.elf64.ehdr == NULL))
46 {
47 /* Maybe no ELF header was created yet. */
48 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
49 return -1;
50 }
51
52 *dst = (elf->class == ELFCLASS32
53 ? elf->state.elf32.ehdr->e_phnum
54 : elf->state.elf64.ehdr->e_phnum);
55
56 if (*dst == PN_XNUM)
57 {
58 const Elf_ScnList *const scns = (elf->class == ELFCLASS32
59 ? &elf->state.elf32.scns
60 : &elf->state.elf64.scns);
61
62 /* If there are no section headers, perhaps this is really just 65536
63 written without PN_XNUM support. Either that or it's bad data. */
64
65 if (likely (scns->cnt > 0))
66 *dst = (elf->class == ELFCLASS32
67 ? scns->data[0].shdr.e32->sh_info
68 : scns->data[0].shdr.e64->sh_info);
69 }
70
71 return 0;
72}
73
74int
75elf_getphdrnum (elf, dst)
76 Elf *elf;
77 size_t *dst;
78{
79 int result;
80
81 if (elf == NULL)
82 return -1;
83
84 if (unlikely (elf->kind != ELF_K_ELF))
85 {
86 __libelf_seterrno (ELF_E_INVALID_HANDLE);
87 return -1;
88 }
89
90 rwlock_rdlock (elf->lock);
91 result = __elf_getphdrnum_rdlock (elf, dst);
92 rwlock_unlock (elf->lock);
93
94 return result;
95}