]>
Commit | Line | Data |
---|---|---|
b08d5a8f | 1 | /* Update ELF header. |
d7635784 | 2 | Copyright (C) 2000, 2001, 2002, 2010 Red Hat, Inc. |
de2ed97f | 3 | This file is part of elfutils. |
b08d5a8f UD |
4 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
5 | ||
de2ed97f MW |
6 | This file is free software; you can redistribute it and/or modify |
7 | it under the terms of either | |
b08d5a8f | 8 | |
de2ed97f MW |
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 | |
361df7da UD |
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. | |
b08d5a8f | 25 | |
de2ed97f MW |
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/>. */ | |
b08d5a8f UD |
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 | gelf_update_ehdr (Elf *elf, GElf_Ehdr *src) | |
42 | { | |
43 | int result = 0; | |
44 | ||
45 | if (elf == NULL) | |
46 | return 0; | |
47 | ||
48 | if (unlikely (elf->kind != ELF_K_ELF)) | |
49 | { | |
50 | __libelf_seterrno (ELF_E_INVALID_HANDLE); | |
51 | return 0; | |
52 | } | |
53 | ||
b4d6f0f8 | 54 | rwlock_wrlock (elf->lock); |
b08d5a8f UD |
55 | |
56 | if (elf->class == ELFCLASS32) | |
57 | { | |
58 | Elf32_Ehdr *ehdr = elf->state.elf32.ehdr; | |
59 | ||
60 | if (ehdr == NULL) | |
61 | { | |
62 | __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR); | |
63 | 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 | if (unlikely (src->e_entry > 0xffffffffull) | |
70 | || unlikely (src->e_phoff > 0xffffffffull) | |
71 | || unlikely (src->e_shoff > 0xffffffffull)) | |
72 | { | |
73 | __libelf_seterrno (ELF_E_INVALID_DATA); | |
74 | goto out; | |
75 | } | |
76 | ||
77 | /* Copy the data. */ | |
78 | memcpy (ehdr->e_ident, src->e_ident, EI_NIDENT); | |
79 | #define COPY(name) \ | |
80 | ehdr->name = src->name | |
81 | COPY (e_type); | |
82 | COPY (e_machine); | |
83 | COPY (e_version); | |
84 | COPY (e_entry); | |
85 | COPY (e_phoff); | |
86 | COPY (e_shoff); | |
87 | COPY (e_flags); | |
88 | COPY (e_ehsize); | |
89 | COPY (e_phentsize); | |
90 | COPY (e_phnum); | |
91 | COPY (e_shentsize); | |
92 | COPY (e_shnum); | |
93 | COPY (e_shstrndx); | |
94 | } | |
95 | else | |
96 | { | |
97 | Elf64_Ehdr *ehdr = elf->state.elf64.ehdr; | |
98 | ||
99 | if (ehdr == NULL) | |
100 | { | |
101 | __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR); | |
102 | goto out; | |
103 | } | |
104 | ||
105 | /* Just copy the data. */ | |
106 | memcpy (ehdr, src, sizeof (Elf64_Ehdr)); | |
107 | } | |
108 | ||
d7635784 UD |
109 | /* Mark the ELF header as modified. */ |
110 | elf->state.elf.ehdr_flags |= ELF_F_DIRTY; | |
111 | ||
b08d5a8f UD |
112 | result = 1; |
113 | ||
114 | out: | |
b4d6f0f8 | 115 | rwlock_unlock (elf->lock); |
b08d5a8f UD |
116 | |
117 | return result; | |
118 | } |