Line data Source code
1 : /* Update ELF header.
2 : Copyright (C) 2000, 2001, 2002, 2010 Red Hat, Inc.
3 : This file is part of elfutils.
4 : Written by Ulrich Drepper <drepper@redhat.com>, 2000.
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 <string.h>
36 :
37 : #include "libelfP.h"
38 :
39 :
40 : int
41 1120 : gelf_update_ehdr (Elf *elf, GElf_Ehdr *src)
42 : {
43 1120 : int result = 0;
44 :
45 1120 : if (elf == NULL)
46 : return 0;
47 :
48 1120 : if (unlikely (elf->kind != ELF_K_ELF))
49 : {
50 0 : __libelf_seterrno (ELF_E_INVALID_HANDLE);
51 0 : return 0;
52 : }
53 :
54 1120 : rwlock_wrlock (elf->lock);
55 :
56 1120 : if (elf->class == ELFCLASS32)
57 : {
58 461 : Elf32_Ehdr *ehdr = elf->state.elf32.ehdr;
59 :
60 461 : if (ehdr == NULL)
61 : {
62 0 : __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
63 0 : goto out;
64 : }
65 :
66 : /* We have to convert the data to the 32 bit format. This might
67 : overflow some fields so we have to test for this case before
68 : copying. */
69 461 : if (unlikely (src->e_entry > 0xffffffffull)
70 461 : || unlikely (src->e_phoff > 0xffffffffull)
71 461 : || unlikely (src->e_shoff > 0xffffffffull))
72 : {
73 0 : __libelf_seterrno (ELF_E_INVALID_DATA);
74 0 : goto out;
75 : }
76 :
77 : /* Copy the data. */
78 461 : memcpy (ehdr->e_ident, src->e_ident, EI_NIDENT);
79 : #define COPY(name) \
80 : ehdr->name = src->name
81 461 : COPY (e_type);
82 461 : COPY (e_machine);
83 461 : COPY (e_version);
84 461 : COPY (e_entry);
85 461 : COPY (e_phoff);
86 461 : COPY (e_shoff);
87 461 : COPY (e_flags);
88 461 : COPY (e_ehsize);
89 461 : COPY (e_phentsize);
90 461 : COPY (e_phnum);
91 461 : COPY (e_shentsize);
92 461 : COPY (e_shnum);
93 461 : COPY (e_shstrndx);
94 : }
95 : else
96 : {
97 659 : Elf64_Ehdr *ehdr = elf->state.elf64.ehdr;
98 :
99 659 : if (ehdr == NULL)
100 : {
101 0 : __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
102 0 : goto out;
103 : }
104 :
105 : /* Just copy the data. */
106 659 : memcpy (ehdr, src, sizeof (Elf64_Ehdr));
107 : }
108 :
109 : /* Mark the ELF header as modified. */
110 1120 : elf->state.elf.ehdr_flags |= ELF_F_DIRTY;
111 :
112 1120 : result = 1;
113 :
114 : out:
115 : rwlock_unlock (elf->lock);
116 :
117 : return result;
118 : }
|