1 /* Get ELF header.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <gelf.h>
23 #include <string.h>
24
25 #include "libelfP.h"
26
27
28 GElf_Ehdr *
gelf_getehdr(elf,dest)29 gelf_getehdr (elf, dest)
30 Elf *elf;
31 GElf_Ehdr *dest;
32 {
33 GElf_Ehdr *result = NULL;
34
35 if (elf == NULL)
36 return NULL;
37
38 if (unlikely (elf->kind != ELF_K_ELF))
39 {
40 __libelf_seterrno (ELF_E_INVALID_HANDLE);
41 return NULL;
42 }
43
44 rwlock_rdlock (elf->lock);
45
46 if (elf->class == ELFCLASS32)
47 {
48 Elf32_Ehdr *ehdr = elf->state.elf32.ehdr;
49
50 /* Maybe no ELF header was created yet. */
51 if (ehdr == NULL)
52 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
53 else
54 {
55 /* Convert the 32-bit struct to an 64-bit one. */
56 memcpy (dest->e_ident, ehdr->e_ident, EI_NIDENT);
57 #define COPY(name) \
58 dest->name = ehdr->name
59 COPY (e_type);
60 COPY (e_machine);
61 COPY (e_version);
62 COPY (e_entry);
63 COPY (e_phoff);
64 COPY (e_shoff);
65 COPY (e_flags);
66 COPY (e_ehsize);
67 COPY (e_phentsize);
68 COPY (e_phnum);
69 COPY (e_shentsize);
70 COPY (e_shnum);
71 COPY (e_shstrndx);
72
73 result = dest;
74 }
75 }
76 else
77 {
78 /* Maybe no ELF header was created yet. */
79 if (elf->state.elf64.ehdr == NULL)
80 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
81 else
82 result = memcpy (dest, elf->state.elf64.ehdr, sizeof (*dest));
83 }
84
85 rwlock_unlock (elf->lock);
86
87 return result;
88 }
89