Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Get ELF header. |
| 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 1998. |
| 5 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 25 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | # include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <gelf.h> |
| 35 | #include <stddef.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "libelfP.h" |
| 40 | |
| 41 | |
| 42 | GElf_Ehdr * |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 43 | __gelf_getehdr_rdlock (elf, dest) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 44 | Elf *elf; |
| 45 | GElf_Ehdr *dest; |
| 46 | { |
| 47 | GElf_Ehdr *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 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 58 | /* The following is an optimization: the ehdr element is at the same |
| 59 | position in both the elf32 and elf64 structure. */ |
| 60 | if (offsetof (struct Elf, state.elf32.ehdr) |
| 61 | != offsetof (struct Elf, state.elf64.ehdr)) |
| 62 | abort (); |
| 63 | /* Just pick one of the values. */ |
| 64 | if (unlikely (elf->state.elf64.ehdr == NULL)) |
| 65 | /* Maybe no ELF header was created yet. */ |
| 66 | __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR); |
| 67 | else if (elf->class == ELFCLASS32) |
| 68 | { |
| 69 | Elf32_Ehdr *ehdr = elf->state.elf32.ehdr; |
| 70 | |
| 71 | /* Convert the 32-bit struct to an 64-bit one. */ |
| 72 | memcpy (dest->e_ident, ehdr->e_ident, EI_NIDENT); |
| 73 | #define COPY(name) \ |
| 74 | dest->name = ehdr->name |
| 75 | COPY (e_type); |
| 76 | COPY (e_machine); |
| 77 | COPY (e_version); |
| 78 | COPY (e_entry); |
| 79 | COPY (e_phoff); |
| 80 | COPY (e_shoff); |
| 81 | COPY (e_flags); |
| 82 | COPY (e_ehsize); |
| 83 | COPY (e_phentsize); |
| 84 | COPY (e_phnum); |
| 85 | COPY (e_shentsize); |
| 86 | COPY (e_shnum); |
| 87 | COPY (e_shstrndx); |
| 88 | |
| 89 | result = dest; |
| 90 | } |
| 91 | else |
| 92 | result = memcpy (dest, elf->state.elf64.ehdr, sizeof (*dest)); |
| 93 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 94 | return result; |
| 95 | } |
Ulrich Drepper | d56e232 | 2008-08-16 03:09:13 +0000 | [diff] [blame] | 96 | |
| 97 | GElf_Ehdr * |
| 98 | gelf_getehdr (elf, dest) |
| 99 | Elf *elf; |
| 100 | GElf_Ehdr *dest; |
| 101 | { |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 102 | GElf_Ehdr *result; |
Ulrich Drepper | d56e232 | 2008-08-16 03:09:13 +0000 | [diff] [blame] | 103 | if (elf == NULL) |
| 104 | return NULL; |
| 105 | |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 106 | rwlock_rdlock (elf->lock); |
| 107 | result = __gelf_getehdr_rdlock (elf, dest); |
| 108 | rwlock_unlock (elf->lock); |
| 109 | |
| 110 | return result; |
Ulrich Drepper | d56e232 | 2008-08-16 03:09:13 +0000 | [diff] [blame] | 111 | } |