libdwarf_rw.c revision 1.3 1 1.2 christos /* $NetBSD: libdwarf_rw.c,v 1.3 2016/02/20 02:43:41 christos Exp $ */
2 1.2 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2007 John Birrell (jb (at) freebsd.org)
5 1.1 christos * Copyright (c) 2010 Kai Wang
6 1.1 christos * All rights reserved.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos * 1. Redistributions of source code must retain the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer.
13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer in the
15 1.1 christos * documentation and/or other materials provided with the distribution.
16 1.1 christos *
17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 christos * SUCH DAMAGE.
28 1.1 christos */
29 1.1 christos
30 1.1 christos #include "_libdwarf.h"
31 1.1 christos
32 1.2 christos __RCSID("$NetBSD: libdwarf_rw.c,v 1.3 2016/02/20 02:43:41 christos Exp $");
33 1.3 christos ELFTC_VCSID("Id: libdwarf_rw.c 3286 2015-12-31 16:45:46Z emaste ");
34 1.1 christos
35 1.1 christos uint64_t
36 1.1 christos _dwarf_read_lsb(uint8_t *data, uint64_t *offsetp, int bytes_to_read)
37 1.1 christos {
38 1.1 christos uint64_t ret;
39 1.1 christos uint8_t *src;
40 1.1 christos
41 1.1 christos src = data + *offsetp;
42 1.1 christos
43 1.1 christos ret = 0;
44 1.1 christos switch (bytes_to_read) {
45 1.1 christos case 8:
46 1.1 christos ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
47 1.1 christos ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
48 1.3 christos /* FALLTHROUGH */
49 1.1 christos case 4:
50 1.1 christos ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
51 1.3 christos /* FALLTHROUGH */
52 1.1 christos case 2:
53 1.1 christos ret |= ((uint64_t) src[1]) << 8;
54 1.3 christos /* FALLTHROUGH */
55 1.1 christos case 1:
56 1.1 christos ret |= src[0];
57 1.1 christos break;
58 1.1 christos default:
59 1.1 christos return (0);
60 1.1 christos }
61 1.1 christos
62 1.1 christos *offsetp += bytes_to_read;
63 1.1 christos
64 1.1 christos return (ret);
65 1.1 christos }
66 1.1 christos
67 1.1 christos uint64_t
68 1.1 christos _dwarf_decode_lsb(uint8_t **data, int bytes_to_read)
69 1.1 christos {
70 1.1 christos uint64_t ret;
71 1.1 christos uint8_t *src;
72 1.1 christos
73 1.1 christos src = *data;
74 1.1 christos
75 1.1 christos ret = 0;
76 1.1 christos switch (bytes_to_read) {
77 1.1 christos case 8:
78 1.1 christos ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
79 1.1 christos ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
80 1.3 christos /* FALLTHROUGH */
81 1.1 christos case 4:
82 1.1 christos ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
83 1.3 christos /* FALLTHROUGH */
84 1.1 christos case 2:
85 1.1 christos ret |= ((uint64_t) src[1]) << 8;
86 1.3 christos /* FALLTHROUGH */
87 1.1 christos case 1:
88 1.1 christos ret |= src[0];
89 1.1 christos break;
90 1.1 christos default:
91 1.1 christos return (0);
92 1.1 christos }
93 1.1 christos
94 1.1 christos *data += bytes_to_read;
95 1.1 christos
96 1.1 christos return (ret);
97 1.1 christos }
98 1.1 christos
99 1.1 christos uint64_t
100 1.1 christos _dwarf_read_msb(uint8_t *data, uint64_t *offsetp, int bytes_to_read)
101 1.1 christos {
102 1.1 christos uint64_t ret;
103 1.1 christos uint8_t *src;
104 1.1 christos
105 1.1 christos src = data + *offsetp;
106 1.1 christos
107 1.1 christos switch (bytes_to_read) {
108 1.1 christos case 1:
109 1.1 christos ret = src[0];
110 1.1 christos break;
111 1.1 christos case 2:
112 1.1 christos ret = src[1] | ((uint64_t) src[0]) << 8;
113 1.1 christos break;
114 1.1 christos case 4:
115 1.1 christos ret = src[3] | ((uint64_t) src[2]) << 8;
116 1.1 christos ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
117 1.1 christos break;
118 1.1 christos case 8:
119 1.1 christos ret = src[7] | ((uint64_t) src[6]) << 8;
120 1.1 christos ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
121 1.1 christos ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
122 1.1 christos ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
123 1.1 christos break;
124 1.1 christos default:
125 1.1 christos return (0);
126 1.1 christos }
127 1.1 christos
128 1.1 christos *offsetp += bytes_to_read;
129 1.1 christos
130 1.1 christos return (ret);
131 1.1 christos }
132 1.1 christos
133 1.1 christos uint64_t
134 1.1 christos _dwarf_decode_msb(uint8_t **data, int bytes_to_read)
135 1.1 christos {
136 1.1 christos uint64_t ret;
137 1.1 christos uint8_t *src;
138 1.1 christos
139 1.1 christos src = *data;
140 1.1 christos
141 1.1 christos ret = 0;
142 1.1 christos switch (bytes_to_read) {
143 1.1 christos case 1:
144 1.1 christos ret = src[0];
145 1.1 christos break;
146 1.1 christos case 2:
147 1.1 christos ret = src[1] | ((uint64_t) src[0]) << 8;
148 1.1 christos break;
149 1.1 christos case 4:
150 1.1 christos ret = src[3] | ((uint64_t) src[2]) << 8;
151 1.1 christos ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
152 1.1 christos break;
153 1.1 christos case 8:
154 1.1 christos ret = src[7] | ((uint64_t) src[6]) << 8;
155 1.1 christos ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
156 1.1 christos ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
157 1.1 christos ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
158 1.1 christos break;
159 1.1 christos default:
160 1.1 christos return (0);
161 1.1 christos break;
162 1.1 christos }
163 1.1 christos
164 1.1 christos *data += bytes_to_read;
165 1.1 christos
166 1.1 christos return (ret);
167 1.1 christos }
168 1.1 christos
169 1.1 christos void
170 1.1 christos _dwarf_write_lsb(uint8_t *data, uint64_t *offsetp, uint64_t value,
171 1.1 christos int bytes_to_write)
172 1.1 christos {
173 1.1 christos uint8_t *dst;
174 1.1 christos
175 1.1 christos dst = data + *offsetp;
176 1.1 christos
177 1.1 christos switch (bytes_to_write) {
178 1.1 christos case 8:
179 1.1 christos dst[7] = (value >> 56) & 0xff;
180 1.1 christos dst[6] = (value >> 48) & 0xff;
181 1.1 christos dst[5] = (value >> 40) & 0xff;
182 1.1 christos dst[4] = (value >> 32) & 0xff;
183 1.3 christos /* FALLTHROUGH */
184 1.1 christos case 4:
185 1.1 christos dst[3] = (value >> 24) & 0xff;
186 1.1 christos dst[2] = (value >> 16) & 0xff;
187 1.3 christos /* FALLTHROUGH */
188 1.1 christos case 2:
189 1.1 christos dst[1] = (value >> 8) & 0xff;
190 1.3 christos /* FALLTHROUGH */
191 1.1 christos case 1:
192 1.1 christos dst[0] = value & 0xff;
193 1.1 christos break;
194 1.1 christos default:
195 1.1 christos return;
196 1.1 christos }
197 1.1 christos
198 1.1 christos *offsetp += bytes_to_write;
199 1.1 christos }
200 1.1 christos
201 1.1 christos int
202 1.1 christos _dwarf_write_lsb_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
203 1.1 christos uint64_t value, int bytes_to_write, Dwarf_Error *error)
204 1.1 christos {
205 1.1 christos
206 1.1 christos assert(*size > 0);
207 1.1 christos
208 1.1 christos while (*offsetp + bytes_to_write > *size) {
209 1.1 christos *size *= 2;
210 1.1 christos *block = realloc(*block, (size_t) *size);
211 1.1 christos if (*block == NULL) {
212 1.1 christos DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
213 1.1 christos return (DW_DLE_MEMORY);
214 1.1 christos }
215 1.1 christos }
216 1.1 christos
217 1.1 christos _dwarf_write_lsb(*block, offsetp, value, bytes_to_write);
218 1.1 christos
219 1.1 christos return (DW_DLE_NONE);
220 1.1 christos }
221 1.1 christos
222 1.1 christos void
223 1.1 christos _dwarf_write_msb(uint8_t *data, uint64_t *offsetp, uint64_t value,
224 1.1 christos int bytes_to_write)
225 1.1 christos {
226 1.1 christos uint8_t *dst;
227 1.1 christos
228 1.1 christos dst = data + *offsetp;
229 1.1 christos
230 1.1 christos switch (bytes_to_write) {
231 1.1 christos case 8:
232 1.1 christos dst[7] = value & 0xff;
233 1.1 christos dst[6] = (value >> 8) & 0xff;
234 1.1 christos dst[5] = (value >> 16) & 0xff;
235 1.1 christos dst[4] = (value >> 24) & 0xff;
236 1.1 christos value >>= 32;
237 1.3 christos /* FALLTHROUGH */
238 1.1 christos case 4:
239 1.1 christos dst[3] = value & 0xff;
240 1.1 christos dst[2] = (value >> 8) & 0xff;
241 1.1 christos value >>= 16;
242 1.3 christos /* FALLTHROUGH */
243 1.1 christos case 2:
244 1.1 christos dst[1] = value & 0xff;
245 1.1 christos value >>= 8;
246 1.3 christos /* FALLTHROUGH */
247 1.1 christos case 1:
248 1.1 christos dst[0] = value & 0xff;
249 1.1 christos break;
250 1.1 christos default:
251 1.1 christos return;
252 1.1 christos }
253 1.1 christos
254 1.1 christos *offsetp += bytes_to_write;
255 1.1 christos }
256 1.1 christos
257 1.1 christos int
258 1.1 christos _dwarf_write_msb_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
259 1.1 christos uint64_t value, int bytes_to_write, Dwarf_Error *error)
260 1.1 christos {
261 1.1 christos
262 1.1 christos assert(*size > 0);
263 1.1 christos
264 1.1 christos while (*offsetp + bytes_to_write > *size) {
265 1.1 christos *size *= 2;
266 1.1 christos *block = realloc(*block, (size_t) *size);
267 1.1 christos if (*block == NULL) {
268 1.1 christos DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
269 1.1 christos return (DW_DLE_MEMORY);
270 1.1 christos }
271 1.1 christos }
272 1.1 christos
273 1.1 christos _dwarf_write_msb(*block, offsetp, value, bytes_to_write);
274 1.1 christos
275 1.1 christos return (DW_DLE_NONE);
276 1.1 christos }
277 1.1 christos
278 1.1 christos int64_t
279 1.1 christos _dwarf_read_sleb128(uint8_t *data, uint64_t *offsetp)
280 1.1 christos {
281 1.1 christos int64_t ret = 0;
282 1.1 christos uint8_t b;
283 1.1 christos int shift = 0;
284 1.1 christos uint8_t *src;
285 1.1 christos
286 1.1 christos src = data + *offsetp;
287 1.1 christos
288 1.1 christos do {
289 1.1 christos b = *src++;
290 1.1 christos ret |= ((b & 0x7f) << shift);
291 1.1 christos (*offsetp)++;
292 1.1 christos shift += 7;
293 1.1 christos } while ((b & 0x80) != 0);
294 1.1 christos
295 1.1 christos if (shift < 64 && (b & 0x40) != 0)
296 1.1 christos ret |= (-1 << shift);
297 1.1 christos
298 1.1 christos return (ret);
299 1.1 christos }
300 1.1 christos
301 1.1 christos int
302 1.1 christos _dwarf_write_sleb128(uint8_t *data, uint8_t *end, int64_t val)
303 1.1 christos {
304 1.1 christos uint8_t *p;
305 1.1 christos
306 1.1 christos p = data;
307 1.1 christos
308 1.1 christos for (;;) {
309 1.1 christos if (p >= end)
310 1.1 christos return (-1);
311 1.1 christos *p = val & 0x7f;
312 1.1 christos val >>= 7;
313 1.1 christos if ((val == 0 && (*p & 0x40) == 0) ||
314 1.1 christos (val == -1 && (*p & 0x40) != 0)) {
315 1.1 christos p++;
316 1.1 christos break;
317 1.1 christos }
318 1.1 christos *p++ |= 0x80;
319 1.1 christos }
320 1.1 christos
321 1.1 christos return (p - data);
322 1.1 christos }
323 1.1 christos
324 1.1 christos int
325 1.1 christos _dwarf_write_sleb128_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
326 1.1 christos int64_t val, Dwarf_Error *error)
327 1.1 christos {
328 1.1 christos int len;
329 1.1 christos
330 1.1 christos assert(*size > 0);
331 1.1 christos
332 1.1 christos while ((len = _dwarf_write_sleb128(*block + *offsetp, *block + *size,
333 1.1 christos val)) < 0) {
334 1.1 christos *size *= 2;
335 1.1 christos *block = realloc(*block, (size_t) *size);
336 1.1 christos if (*block == NULL) {
337 1.1 christos DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
338 1.1 christos return (DW_DLE_MEMORY);
339 1.1 christos }
340 1.1 christos }
341 1.1 christos
342 1.1 christos *offsetp += len;
343 1.1 christos
344 1.1 christos return (DW_DLE_NONE);
345 1.1 christos }
346 1.1 christos
347 1.1 christos uint64_t
348 1.1 christos _dwarf_read_uleb128(uint8_t *data, uint64_t *offsetp)
349 1.1 christos {
350 1.1 christos uint64_t ret = 0;
351 1.1 christos uint8_t b;
352 1.1 christos int shift = 0;
353 1.1 christos uint8_t *src;
354 1.1 christos
355 1.1 christos src = data + *offsetp;
356 1.1 christos
357 1.1 christos do {
358 1.1 christos b = *src++;
359 1.1 christos ret |= ((b & 0x7f) << shift);
360 1.1 christos (*offsetp)++;
361 1.1 christos shift += 7;
362 1.1 christos } while ((b & 0x80) != 0);
363 1.1 christos
364 1.1 christos return (ret);
365 1.1 christos }
366 1.1 christos
367 1.1 christos int
368 1.1 christos _dwarf_write_uleb128(uint8_t *data, uint8_t *end, uint64_t val)
369 1.1 christos {
370 1.1 christos uint8_t *p;
371 1.1 christos
372 1.1 christos p = data;
373 1.1 christos
374 1.1 christos do {
375 1.1 christos if (p >= end)
376 1.1 christos return (-1);
377 1.1 christos *p = val & 0x7f;
378 1.1 christos val >>= 7;
379 1.1 christos if (val > 0)
380 1.1 christos *p |= 0x80;
381 1.1 christos p++;
382 1.1 christos } while (val > 0);
383 1.1 christos
384 1.1 christos return (p - data);
385 1.1 christos }
386 1.1 christos
387 1.1 christos int
388 1.1 christos _dwarf_write_uleb128_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
389 1.1 christos uint64_t val, Dwarf_Error *error)
390 1.1 christos {
391 1.1 christos int len;
392 1.1 christos
393 1.1 christos assert(*size > 0);
394 1.1 christos
395 1.1 christos while ((len = _dwarf_write_uleb128(*block + *offsetp, *block + *size,
396 1.1 christos val)) < 0) {
397 1.1 christos *size *= 2;
398 1.1 christos *block = realloc(*block, (size_t) *size);
399 1.1 christos if (*block == NULL) {
400 1.1 christos DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
401 1.1 christos return (DW_DLE_MEMORY);
402 1.1 christos }
403 1.1 christos }
404 1.1 christos
405 1.1 christos *offsetp += len;
406 1.1 christos
407 1.1 christos return (DW_DLE_NONE);
408 1.1 christos }
409 1.1 christos
410 1.1 christos int64_t
411 1.1 christos _dwarf_decode_sleb128(uint8_t **dp)
412 1.1 christos {
413 1.1 christos int64_t ret = 0;
414 1.1 christos uint8_t b;
415 1.1 christos int shift = 0;
416 1.1 christos
417 1.1 christos uint8_t *src = *dp;
418 1.1 christos
419 1.1 christos do {
420 1.1 christos b = *src++;
421 1.1 christos ret |= ((b & 0x7f) << shift);
422 1.1 christos shift += 7;
423 1.1 christos } while ((b & 0x80) != 0);
424 1.1 christos
425 1.1 christos if (shift < 64 && (b & 0x40) != 0)
426 1.1 christos ret |= (-1 << shift);
427 1.1 christos
428 1.1 christos *dp = src;
429 1.1 christos
430 1.1 christos return (ret);
431 1.1 christos }
432 1.1 christos
433 1.1 christos uint64_t
434 1.1 christos _dwarf_decode_uleb128(uint8_t **dp)
435 1.1 christos {
436 1.1 christos uint64_t ret = 0;
437 1.1 christos uint8_t b;
438 1.1 christos int shift = 0;
439 1.1 christos
440 1.1 christos uint8_t *src = *dp;
441 1.1 christos
442 1.1 christos do {
443 1.1 christos b = *src++;
444 1.1 christos ret |= ((b & 0x7f) << shift);
445 1.1 christos shift += 7;
446 1.1 christos } while ((b & 0x80) != 0);
447 1.1 christos
448 1.1 christos *dp = src;
449 1.1 christos
450 1.1 christos return (ret);
451 1.1 christos }
452 1.1 christos
453 1.1 christos char *
454 1.1 christos _dwarf_read_string(void *data, Dwarf_Unsigned size, uint64_t *offsetp)
455 1.1 christos {
456 1.1 christos char *ret, *src;
457 1.1 christos
458 1.1 christos ret = src = (char *) data + *offsetp;
459 1.1 christos
460 1.1 christos while (*src != '\0' && *offsetp < size) {
461 1.1 christos src++;
462 1.1 christos (*offsetp)++;
463 1.1 christos }
464 1.1 christos
465 1.1 christos if (*src == '\0' && *offsetp < size)
466 1.1 christos (*offsetp)++;
467 1.1 christos
468 1.1 christos return (ret);
469 1.1 christos }
470 1.1 christos
471 1.1 christos void
472 1.1 christos _dwarf_write_string(void *data, uint64_t *offsetp, char *string)
473 1.1 christos {
474 1.1 christos char *dst;
475 1.1 christos
476 1.1 christos dst = (char *) data + *offsetp;
477 1.1 christos strcpy(dst, string);
478 1.1 christos (*offsetp) += strlen(string) + 1;
479 1.1 christos }
480 1.1 christos
481 1.1 christos int
482 1.1 christos _dwarf_write_string_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
483 1.1 christos char *string, Dwarf_Error *error)
484 1.1 christos {
485 1.1 christos size_t len;
486 1.1 christos
487 1.1 christos assert(*size > 0);
488 1.1 christos
489 1.1 christos len = strlen(string) + 1;
490 1.1 christos while (*offsetp + len > *size) {
491 1.1 christos *size *= 2;
492 1.1 christos *block = realloc(*block, (size_t) *size);
493 1.1 christos if (*block == NULL) {
494 1.1 christos DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
495 1.1 christos return (DW_DLE_MEMORY);
496 1.1 christos }
497 1.1 christos }
498 1.1 christos
499 1.1 christos _dwarf_write_string(*block, offsetp, string);
500 1.1 christos
501 1.1 christos return (DW_DLE_NONE);
502 1.1 christos }
503 1.1 christos
504 1.1 christos uint8_t *
505 1.1 christos _dwarf_read_block(void *data, uint64_t *offsetp, uint64_t length)
506 1.1 christos {
507 1.1 christos uint8_t *ret, *src;
508 1.1 christos
509 1.1 christos ret = src = (uint8_t *) data + *offsetp;
510 1.1 christos
511 1.1 christos (*offsetp) += length;
512 1.1 christos
513 1.1 christos return (ret);
514 1.1 christos }
515 1.1 christos
516 1.1 christos void
517 1.1 christos _dwarf_write_block(void *data, uint64_t *offsetp, uint8_t *blk,
518 1.1 christos uint64_t length)
519 1.1 christos {
520 1.1 christos uint8_t *dst;
521 1.1 christos
522 1.1 christos dst = (uint8_t *) data + *offsetp;
523 1.1 christos memcpy(dst, blk, length);
524 1.1 christos (*offsetp) += length;
525 1.1 christos }
526 1.1 christos
527 1.1 christos int
528 1.1 christos _dwarf_write_block_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
529 1.1 christos uint8_t *blk, uint64_t length, Dwarf_Error *error)
530 1.1 christos {
531 1.1 christos
532 1.1 christos assert(*size > 0);
533 1.1 christos
534 1.1 christos while (*offsetp + length > *size) {
535 1.1 christos *size *= 2;
536 1.1 christos *block = realloc(*block, (size_t) *size);
537 1.1 christos if (*block == NULL) {
538 1.1 christos DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
539 1.1 christos return (DW_DLE_MEMORY);
540 1.1 christos }
541 1.1 christos }
542 1.1 christos
543 1.1 christos _dwarf_write_block(*block, offsetp, blk, length);
544 1.1 christos
545 1.1 christos return (DW_DLE_NONE);
546 1.1 christos }
547 1.1 christos
548 1.1 christos void
549 1.1 christos _dwarf_write_padding(void *data, uint64_t *offsetp, uint8_t byte,
550 1.1 christos uint64_t length)
551 1.1 christos {
552 1.1 christos uint8_t *dst;
553 1.1 christos
554 1.1 christos dst = (uint8_t *) data + *offsetp;
555 1.1 christos memset(dst, byte, length);
556 1.1 christos (*offsetp) += length;
557 1.1 christos }
558 1.1 christos
559 1.1 christos int
560 1.1 christos _dwarf_write_padding_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
561 1.1 christos uint8_t byte, uint64_t cnt, Dwarf_Error *error)
562 1.1 christos {
563 1.1 christos assert(*size > 0);
564 1.1 christos
565 1.1 christos while (*offsetp + cnt > *size) {
566 1.1 christos *size *= 2;
567 1.1 christos *block = realloc(*block, (size_t) *size);
568 1.1 christos if (*block == NULL) {
569 1.1 christos DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
570 1.1 christos return (DW_DLE_MEMORY);
571 1.1 christos }
572 1.1 christos }
573 1.1 christos
574 1.1 christos _dwarf_write_padding(*block, offsetp, byte, cnt);
575 1.1 christos
576 1.1 christos return (DW_DLE_NONE);
577 1.1 christos }
578