Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 1 | /* Return number of program headers in the ELF file. |
| 2 | Copyright (C) 2010 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 8 | * 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 McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 21 | 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 Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 25 | 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 McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 28 | |
| 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 | |
| 40 | int |
| 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 | |
| 74 | int |
| 75 | elf_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 | } |