/src/elfutils/libelf/gelf_update_shdr.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Update section 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 | | gelf_update_shdr (Elf_Scn *scn, GElf_Shdr *src) |
42 | 77.2k | { |
43 | 77.2k | int result = 0; |
44 | 77.2k | Elf *elf; |
45 | | |
46 | 77.2k | if (scn == NULL || src == NULL) |
47 | 0 | return 0; |
48 | | |
49 | 77.2k | elf = scn->elf; |
50 | 77.2k | rwlock_wrlock (elf->lock); |
51 | | |
52 | 77.2k | if (elf->class == ELFCLASS32) |
53 | 34.1k | { |
54 | 34.1k | Elf32_Shdr *shdr |
55 | 34.1k | = scn->shdr.e32 ?: __elf32_getshdr_wrlock (scn); |
56 | | |
57 | 34.1k | if (shdr == NULL) |
58 | 0 | { |
59 | 0 | __libelf_seterrno (ELF_E_INVALID_OPERAND); |
60 | 0 | goto out; |
61 | 0 | } |
62 | | |
63 | 0 | if (unlikely (src->sh_flags > 0xffffffffull) |
64 | 34.1k | || unlikely (src->sh_addr > 0xffffffffull) |
65 | 34.1k | || unlikely (src->sh_offset > 0xffffffffull) |
66 | 34.1k | || unlikely (src->sh_size > 0xffffffffull) |
67 | 34.1k | || unlikely (src->sh_addralign > 0xffffffffull) |
68 | 34.1k | || unlikely (src->sh_entsize > 0xffffffffull)) |
69 | 72 | { |
70 | 72 | __libelf_seterrno (ELF_E_INVALID_DATA); |
71 | 72 | goto out; |
72 | 72 | } |
73 | | |
74 | 18.4E | #define COPY(name) \ |
75 | 18.4E | shdr->name = src->name |
76 | 18.4E | COPY (sh_name); |
77 | 18.4E | COPY (sh_type); |
78 | 18.4E | COPY (sh_flags); |
79 | 18.4E | COPY (sh_addr); |
80 | 18.4E | COPY (sh_offset); |
81 | 18.4E | COPY (sh_size); |
82 | 18.4E | COPY (sh_link); |
83 | 18.4E | COPY (sh_info); |
84 | 18.4E | COPY (sh_addralign); |
85 | 18.4E | COPY (sh_entsize); |
86 | 18.4E | } |
87 | 43.0k | else |
88 | 43.0k | { |
89 | 43.0k | Elf64_Shdr *shdr |
90 | 43.0k | = scn->shdr.e64 ?: __elf64_getshdr_wrlock (scn); |
91 | | |
92 | 43.0k | if (shdr == NULL) |
93 | 0 | { |
94 | 0 | __libelf_seterrno (ELF_E_INVALID_OPERAND); |
95 | 0 | goto out; |
96 | 0 | } |
97 | | |
98 | | /* We only have to copy the data. */ |
99 | 0 | (void) memcpy (shdr, src, sizeof (GElf_Shdr)); |
100 | 0 | } |
101 | | |
102 | | /* Mark the section header as modified. */ |
103 | 18.4E | scn->shdr_flags |= ELF_F_DIRTY; |
104 | | |
105 | 18.4E | result = 1; |
106 | | |
107 | 18.4E | out: |
108 | 77.2k | rwlock_unlock (elf->lock); |
109 | | |
110 | 77.2k | return result; |
111 | 18.4E | } |