blob: 5c323dc5b5bc9c55eb0f0426ee15bf596bd4e97c [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Get ELF header.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 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 <stddef.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "libelfP.h"
28
29
30GElf_Ehdr *
31gelf_getehdr (elf, dest)
32 Elf *elf;
33 GElf_Ehdr *dest;
34{
35 GElf_Ehdr *result = NULL;
36
37 if (elf == NULL)
38 return NULL;
39
40 if (unlikely (elf->kind != ELF_K_ELF))
41 {
42 __libelf_seterrno (ELF_E_INVALID_HANDLE);
43 return NULL;
44 }
45
46 rwlock_rdlock (elf->lock);
47
48 /* The following is an optimization: the ehdr element is at the same
49 position in both the elf32 and elf64 structure. */
50 if (offsetof (struct Elf, state.elf32.ehdr)
51 != offsetof (struct Elf, state.elf64.ehdr))
52 abort ();
53 /* Just pick one of the values. */
54 if (unlikely (elf->state.elf64.ehdr == NULL))
55 /* Maybe no ELF header was created yet. */
56 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
57 else if (elf->class == ELFCLASS32)
58 {
59 Elf32_Ehdr *ehdr = elf->state.elf32.ehdr;
60
61 /* Convert the 32-bit struct to an 64-bit one. */
62 memcpy (dest->e_ident, ehdr->e_ident, EI_NIDENT);
63#define COPY(name) \
64 dest->name = ehdr->name
65 COPY (e_type);
66 COPY (e_machine);
67 COPY (e_version);
68 COPY (e_entry);
69 COPY (e_phoff);
70 COPY (e_shoff);
71 COPY (e_flags);
72 COPY (e_ehsize);
73 COPY (e_phentsize);
74 COPY (e_phnum);
75 COPY (e_shentsize);
76 COPY (e_shnum);
77 COPY (e_shstrndx);
78
79 result = dest;
80 }
81 else
82 result = memcpy (dest, elf->state.elf64.ehdr, sizeof (*dest));
83
84 rwlock_unlock (elf->lock);
85
86 return result;
87}
88INTDEF(gelf_getehdr)