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