1 /* Get ELF header.
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5 
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8 
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
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.
25 
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/>.  */
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 *
__gelf_getehdr_rdlock(elf,dest)43 __gelf_getehdr_rdlock (elf, dest)
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 
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 
94   return result;
95 }
96 
97 GElf_Ehdr *
gelf_getehdr(elf,dest)98 gelf_getehdr (elf, dest)
99      Elf *elf;
100      GElf_Ehdr *dest;
101 {
102   GElf_Ehdr *result;
103   if (elf == NULL)
104     return NULL;
105 
106   rwlock_rdlock (elf->lock);
107   result = __gelf_getehdr_rdlock (elf, dest);
108   rwlock_unlock (elf->lock);
109 
110   return result;
111 }
112