/src/elfutils/libelf/elf_getdata_rawchunk.c
Line | Count | Source |
1 | | /* Return converted data from raw chunk of ELF file. |
2 | | Copyright (C) 2007, 2014, 2015 Red Hat, Inc. |
3 | | Copyright (C) 2022, 2023 Mark J. Wielaard <mark@klomp.org> |
4 | | This file is part of elfutils. |
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 <assert.h> |
35 | | #include <errno.h> |
36 | | #include <search.h> |
37 | | #include <stdlib.h> |
38 | | #include <string.h> |
39 | | |
40 | | #include "libelfP.h" |
41 | | #include "common.h" |
42 | | |
43 | | static int |
44 | | chunk_compare (const void *a, const void *b) |
45 | 80.6k | { |
46 | 80.6k | Elf_Data_Chunk *da = (Elf_Data_Chunk *)a; |
47 | 80.6k | Elf_Data_Chunk *db = (Elf_Data_Chunk *)b; |
48 | | |
49 | 80.6k | if (da->offset != db->offset) |
50 | 42.7k | return da->offset - db->offset; |
51 | | |
52 | 37.9k | if (da->data.d.d_size != db->data.d.d_size) |
53 | 21.2k | return da->data.d.d_size - db->data.d.d_size; |
54 | | |
55 | 16.7k | return da->data.d.d_type - db->data.d.d_type; |
56 | 37.9k | } |
57 | | |
58 | | Elf_Data * |
59 | | elf_getdata_rawchunk (Elf *elf, int64_t offset, size_t size, Elf_Type type) |
60 | 34.3k | { |
61 | 34.3k | if (unlikely (elf == NULL)) |
62 | 0 | return NULL; |
63 | | |
64 | 34.3k | if (unlikely (elf->kind != ELF_K_ELF)) |
65 | 0 | { |
66 | | /* No valid descriptor. */ |
67 | 0 | __libelf_seterrno (ELF_E_INVALID_HANDLE); |
68 | 0 | return NULL; |
69 | 0 | } |
70 | | |
71 | 34.3k | if (unlikely (offset < 0 || (uint64_t) offset > elf->maximum_size |
72 | 34.3k | || elf->maximum_size - (uint64_t) offset < size)) |
73 | | |
74 | 14.7k | { |
75 | | /* Invalid request. */ |
76 | 14.7k | __libelf_seterrno (ELF_E_INVALID_OP); |
77 | 14.7k | return NULL; |
78 | 14.7k | } |
79 | | |
80 | 19.5k | if (type >= ELF_T_NUM) |
81 | 0 | { |
82 | 0 | __libelf_seterrno (ELF_E_UNKNOWN_TYPE); |
83 | 0 | return NULL; |
84 | 0 | } |
85 | | |
86 | | /* Get the raw bytes from the file. */ |
87 | 19.5k | void *rawchunk; |
88 | 19.5k | int flags = 0; |
89 | 19.5k | Elf_Data *result = NULL; |
90 | | |
91 | 19.5k | rwlock_rdlock (elf->lock); |
92 | | |
93 | | /* Maybe we already got this chunk? */ |
94 | 19.5k | Elf_Data_Chunk key; |
95 | 19.5k | key.offset = offset; |
96 | 19.5k | key.data.d.d_size = size; |
97 | 19.5k | key.data.d.d_type = type; |
98 | 19.5k | Elf_Data_Chunk **found = tsearch (&key, &elf->state.elf.rawchunks, |
99 | 19.5k | &chunk_compare); |
100 | 19.5k | if (found == NULL) |
101 | 0 | goto nomem; |
102 | | |
103 | | /* Existing entry. */ |
104 | 19.5k | if (*found != &key && *found != NULL) |
105 | 16.5k | { |
106 | 16.5k | result = &(*found)->data.d; |
107 | 16.5k | goto out; |
108 | 16.5k | } |
109 | | |
110 | | /* New entry. Note that *found will point to the newly inserted |
111 | | (dummy) key. We'll replace it with a real rawchunk when that is |
112 | | setup. Make sure to tdelete the dummy key if anything goes |
113 | | wrong. */ |
114 | | |
115 | 3.03k | size_t align = __libelf_type_align (elf->class, type); |
116 | 3.03k | if (elf->map_address != NULL) |
117 | 3.03k | { |
118 | | /* If the file is mmap'ed we can use it directly, if aligned for type. */ |
119 | 3.03k | char *rawdata = elf->map_address + elf->start_offset + offset; |
120 | 3.03k | if (((uintptr_t) rawdata & (align - 1)) == 0) |
121 | 2.01k | rawchunk = rawdata; |
122 | 1.01k | else |
123 | 1.01k | { |
124 | | /* We allocate the memory and memcpy it to get aligned data. */ |
125 | 1.01k | rawchunk = malloc (size); |
126 | 1.01k | if (rawchunk == NULL) |
127 | 0 | goto nomem; |
128 | 1.01k | memcpy (rawchunk, rawdata, size); |
129 | 1.01k | flags = ELF_F_MALLOCED; |
130 | 1.01k | } |
131 | 3.03k | } |
132 | 0 | else |
133 | 0 | { |
134 | | /* We allocate the memory and read the data from the file. */ |
135 | 0 | rawchunk = malloc (size); |
136 | 0 | if (rawchunk == NULL) |
137 | 0 | { |
138 | 0 | nomem: |
139 | 0 | tdelete (&key, &elf->state.elf.rawchunks, &chunk_compare); |
140 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
141 | 0 | goto out; |
142 | 0 | } |
143 | | |
144 | | /* Read the file content. */ |
145 | 0 | if (unlikely ((size_t) pread_retry (elf->fildes, rawchunk, size, |
146 | 0 | elf->start_offset + offset) |
147 | 0 | != size)) |
148 | 0 | { |
149 | | /* Something went wrong. */ |
150 | 0 | tdelete (&key, &elf->state.elf.rawchunks, &chunk_compare); |
151 | 0 | free (rawchunk); |
152 | 0 | __libelf_seterrno (ELF_E_READ_ERROR); |
153 | 0 | goto out; |
154 | 0 | } |
155 | | |
156 | 0 | flags = ELF_F_MALLOCED; |
157 | 0 | } |
158 | | |
159 | | /* Copy and/or convert the data as needed for aligned native-order access. */ |
160 | 3.03k | void *buffer; |
161 | 3.03k | if (elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA) |
162 | 582 | { |
163 | 582 | if (((uintptr_t) rawchunk & (align - 1)) == 0) |
164 | | /* No need to copy, we can use the raw data. */ |
165 | 582 | buffer = rawchunk; |
166 | 0 | else |
167 | 0 | { |
168 | | /* A malloc'd block is always sufficiently aligned. */ |
169 | 0 | assert (flags == 0); |
170 | |
|
171 | 0 | buffer = malloc (size); |
172 | 0 | if (unlikely (buffer == NULL)) |
173 | 0 | goto nomem; |
174 | 0 | flags = ELF_F_MALLOCED; |
175 | | |
176 | | /* The copy will be appropriately aligned for direct access. */ |
177 | 0 | memcpy (buffer, rawchunk, size); |
178 | 0 | } |
179 | 582 | } |
180 | 2.44k | else |
181 | 2.44k | { |
182 | 2.44k | if (flags) |
183 | 773 | buffer = rawchunk; |
184 | 1.67k | else |
185 | 1.67k | { |
186 | 1.67k | buffer = malloc (size); |
187 | 1.67k | if (unlikely (buffer == NULL)) |
188 | 0 | goto nomem; |
189 | 1.67k | flags = ELF_F_MALLOCED; |
190 | 1.67k | } |
191 | | |
192 | | /* Call the conversion function. */ |
193 | 2.44k | (*__elf_xfctstom[elf->class - 1][type])(buffer, rawchunk, size, 0); |
194 | 2.44k | } |
195 | | |
196 | | /* Allocate the dummy container to point at this buffer. */ |
197 | 3.03k | Elf_Data_Chunk *chunk = calloc (1, sizeof *chunk); |
198 | 3.03k | if (chunk == NULL) |
199 | 0 | { |
200 | 0 | if (flags) |
201 | 0 | free (buffer); |
202 | 0 | goto nomem; |
203 | 0 | } |
204 | | |
205 | 3.03k | chunk->dummy_scn.elf = elf; |
206 | 3.03k | chunk->dummy_scn.flags = flags; |
207 | 3.03k | chunk->data.s = &chunk->dummy_scn; |
208 | 3.03k | chunk->data.d.d_buf = buffer; |
209 | 3.03k | chunk->data.d.d_size = size; |
210 | 3.03k | chunk->data.d.d_type = type; |
211 | 3.03k | chunk->data.d.d_align = align; |
212 | 3.03k | chunk->data.d.d_version = EV_CURRENT; |
213 | 3.03k | chunk->offset = offset; |
214 | | |
215 | 3.03k | rwlock_unlock (elf->lock); |
216 | 3.03k | rwlock_wrlock (elf->lock); |
217 | | |
218 | 3.03k | *found = chunk; |
219 | 3.03k | result = &chunk->data.d; |
220 | | |
221 | 19.5k | out: |
222 | 19.5k | rwlock_unlock (elf->lock); |
223 | 19.5k | return result; |
224 | 3.03k | } |