blob: ea83fc04513402c86e68948dae4c597390006baf [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.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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 Drepper361df7d2006-04-04 21:38:57 +000022 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 Drepperb08d5a82005-07-26 05:00:05 +000025
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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 Drepperb08d5a82005-07-26 05:00:05 +000029
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
42GElf_Ehdr *
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000043__gelf_getehdr_rdlock (elf, dest)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000044 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 Drepperb08d5a82005-07-26 05:00:05 +000058 /* 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 Drepperb08d5a82005-07-26 05:00:05 +000094 return result;
95}
Ulrich Drepperd56e2322008-08-16 03:09:13 +000096
97GElf_Ehdr *
98gelf_getehdr (elf, dest)
99 Elf *elf;
100 GElf_Ehdr *dest;
101{
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000102 GElf_Ehdr *result;
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000103 if (elf == NULL)
104 return NULL;
105
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000106 rwlock_rdlock (elf->lock);
107 result = __gelf_getehdr_rdlock (elf, dest);
108 rwlock_unlock (elf->lock);
109
110 return result;
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000111}