Home | History | Annotate | Line # | Download | only in libdwarf
libdwarf_rw.c revision 1.2
      1  1.2  christos /*	$NetBSD: libdwarf_rw.c,v 1.2 2014/03/09 16:58:04 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.2 2014/03/09 16:58:04 christos Exp $");
     33  1.1  christos ELFTC_VCSID("Id: libdwarf_rw.c 2952 2013-06-26 19:09:40Z kaiwang27 ");
     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.1  christos 	case 4:
     49  1.1  christos 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
     50  1.1  christos 	case 2:
     51  1.1  christos 		ret |= ((uint64_t) src[1]) << 8;
     52  1.1  christos 	case 1:
     53  1.1  christos 		ret |= src[0];
     54  1.1  christos 		break;
     55  1.1  christos 	default:
     56  1.1  christos 		return (0);
     57  1.1  christos 	}
     58  1.1  christos 
     59  1.1  christos 	*offsetp += bytes_to_read;
     60  1.1  christos 
     61  1.1  christos 	return (ret);
     62  1.1  christos }
     63  1.1  christos 
     64  1.1  christos uint64_t
     65  1.1  christos _dwarf_decode_lsb(uint8_t **data, int bytes_to_read)
     66  1.1  christos {
     67  1.1  christos 	uint64_t ret;
     68  1.1  christos 	uint8_t *src;
     69  1.1  christos 
     70  1.1  christos 	src = *data;
     71  1.1  christos 
     72  1.1  christos 	ret = 0;
     73  1.1  christos 	switch (bytes_to_read) {
     74  1.1  christos 	case 8:
     75  1.1  christos 		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
     76  1.1  christos 		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
     77  1.1  christos 	case 4:
     78  1.1  christos 		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
     79  1.1  christos 	case 2:
     80  1.1  christos 		ret |= ((uint64_t) src[1]) << 8;
     81  1.1  christos 	case 1:
     82  1.1  christos 		ret |= src[0];
     83  1.1  christos 		break;
     84  1.1  christos 	default:
     85  1.1  christos 		return (0);
     86  1.1  christos 	}
     87  1.1  christos 
     88  1.1  christos 	*data += bytes_to_read;
     89  1.1  christos 
     90  1.1  christos 	return (ret);
     91  1.1  christos }
     92  1.1  christos 
     93  1.1  christos uint64_t
     94  1.1  christos _dwarf_read_msb(uint8_t *data, uint64_t *offsetp, int bytes_to_read)
     95  1.1  christos {
     96  1.1  christos 	uint64_t ret;
     97  1.1  christos 	uint8_t *src;
     98  1.1  christos 
     99  1.1  christos 	src = data + *offsetp;
    100  1.1  christos 
    101  1.1  christos 	switch (bytes_to_read) {
    102  1.1  christos 	case 1:
    103  1.1  christos 		ret = src[0];
    104  1.1  christos 		break;
    105  1.1  christos 	case 2:
    106  1.1  christos 		ret = src[1] | ((uint64_t) src[0]) << 8;
    107  1.1  christos 		break;
    108  1.1  christos 	case 4:
    109  1.1  christos 		ret = src[3] | ((uint64_t) src[2]) << 8;
    110  1.1  christos 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
    111  1.1  christos 		break;
    112  1.1  christos 	case 8:
    113  1.1  christos 		ret = src[7] | ((uint64_t) src[6]) << 8;
    114  1.1  christos 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
    115  1.1  christos 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
    116  1.1  christos 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
    117  1.1  christos 		break;
    118  1.1  christos 	default:
    119  1.1  christos 		return (0);
    120  1.1  christos 	}
    121  1.1  christos 
    122  1.1  christos 	*offsetp += bytes_to_read;
    123  1.1  christos 
    124  1.1  christos 	return (ret);
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos uint64_t
    128  1.1  christos _dwarf_decode_msb(uint8_t **data, int bytes_to_read)
    129  1.1  christos {
    130  1.1  christos 	uint64_t ret;
    131  1.1  christos 	uint8_t *src;
    132  1.1  christos 
    133  1.1  christos 	src = *data;
    134  1.1  christos 
    135  1.1  christos 	ret = 0;
    136  1.1  christos 	switch (bytes_to_read) {
    137  1.1  christos 	case 1:
    138  1.1  christos 		ret = src[0];
    139  1.1  christos 		break;
    140  1.1  christos 	case 2:
    141  1.1  christos 		ret = src[1] | ((uint64_t) src[0]) << 8;
    142  1.1  christos 		break;
    143  1.1  christos 	case 4:
    144  1.1  christos 		ret = src[3] | ((uint64_t) src[2]) << 8;
    145  1.1  christos 		ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24;
    146  1.1  christos 		break;
    147  1.1  christos 	case 8:
    148  1.1  christos 		ret = src[7] | ((uint64_t) src[6]) << 8;
    149  1.1  christos 		ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24;
    150  1.1  christos 		ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40;
    151  1.1  christos 		ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56;
    152  1.1  christos 		break;
    153  1.1  christos 	default:
    154  1.1  christos 		return (0);
    155  1.1  christos 		break;
    156  1.1  christos 	}
    157  1.1  christos 
    158  1.1  christos 	*data += bytes_to_read;
    159  1.1  christos 
    160  1.1  christos 	return (ret);
    161  1.1  christos }
    162  1.1  christos 
    163  1.1  christos void
    164  1.1  christos _dwarf_write_lsb(uint8_t *data, uint64_t *offsetp, uint64_t value,
    165  1.1  christos     int bytes_to_write)
    166  1.1  christos {
    167  1.1  christos 	uint8_t *dst;
    168  1.1  christos 
    169  1.1  christos 	dst = data + *offsetp;
    170  1.1  christos 
    171  1.1  christos 	switch (bytes_to_write) {
    172  1.1  christos 	case 8:
    173  1.1  christos 		dst[7] = (value >> 56) & 0xff;
    174  1.1  christos 		dst[6] = (value >> 48) & 0xff;
    175  1.1  christos 		dst[5] = (value >> 40) & 0xff;
    176  1.1  christos 		dst[4] = (value >> 32) & 0xff;
    177  1.1  christos 	case 4:
    178  1.1  christos 		dst[3] = (value >> 24) & 0xff;
    179  1.1  christos 		dst[2] = (value >> 16) & 0xff;
    180  1.1  christos 	case 2:
    181  1.1  christos 		dst[1] = (value >> 8) & 0xff;
    182  1.1  christos 	case 1:
    183  1.1  christos 		dst[0] = value & 0xff;
    184  1.1  christos 		break;
    185  1.1  christos 	default:
    186  1.1  christos 		return;
    187  1.1  christos 	}
    188  1.1  christos 
    189  1.1  christos 	*offsetp += bytes_to_write;
    190  1.1  christos }
    191  1.1  christos 
    192  1.1  christos int
    193  1.1  christos _dwarf_write_lsb_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
    194  1.1  christos     uint64_t value, int bytes_to_write, Dwarf_Error *error)
    195  1.1  christos {
    196  1.1  christos 
    197  1.1  christos 	assert(*size > 0);
    198  1.1  christos 
    199  1.1  christos 	while (*offsetp + bytes_to_write > *size) {
    200  1.1  christos 		*size *= 2;
    201  1.1  christos 		*block = realloc(*block, (size_t) *size);
    202  1.1  christos 		if (*block == NULL) {
    203  1.1  christos 			DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
    204  1.1  christos 			return (DW_DLE_MEMORY);
    205  1.1  christos 		}
    206  1.1  christos 	}
    207  1.1  christos 
    208  1.1  christos 	_dwarf_write_lsb(*block, offsetp, value, bytes_to_write);
    209  1.1  christos 
    210  1.1  christos 	return (DW_DLE_NONE);
    211  1.1  christos }
    212  1.1  christos 
    213  1.1  christos void
    214  1.1  christos _dwarf_write_msb(uint8_t *data, uint64_t *offsetp, uint64_t value,
    215  1.1  christos     int bytes_to_write)
    216  1.1  christos {
    217  1.1  christos 	uint8_t *dst;
    218  1.1  christos 
    219  1.1  christos 	dst = data + *offsetp;
    220  1.1  christos 
    221  1.1  christos 	switch (bytes_to_write) {
    222  1.1  christos 	case 8:
    223  1.1  christos 		dst[7] = value & 0xff;
    224  1.1  christos 		dst[6] = (value >> 8) & 0xff;
    225  1.1  christos 		dst[5] = (value >> 16) & 0xff;
    226  1.1  christos 		dst[4] = (value >> 24) & 0xff;
    227  1.1  christos 		value >>= 32;
    228  1.1  christos 	case 4:
    229  1.1  christos 		dst[3] = value & 0xff;
    230  1.1  christos 		dst[2] = (value >> 8) & 0xff;
    231  1.1  christos 		value >>= 16;
    232  1.1  christos 	case 2:
    233  1.1  christos 		dst[1] = value & 0xff;
    234  1.1  christos 		value >>= 8;
    235  1.1  christos 	case 1:
    236  1.1  christos 		dst[0] = value & 0xff;
    237  1.1  christos 		break;
    238  1.1  christos 	default:
    239  1.1  christos 		return;
    240  1.1  christos 	}
    241  1.1  christos 
    242  1.1  christos 	*offsetp += bytes_to_write;
    243  1.1  christos }
    244  1.1  christos 
    245  1.1  christos int
    246  1.1  christos _dwarf_write_msb_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
    247  1.1  christos     uint64_t value, int bytes_to_write, Dwarf_Error *error)
    248  1.1  christos {
    249  1.1  christos 
    250  1.1  christos 	assert(*size > 0);
    251  1.1  christos 
    252  1.1  christos 	while (*offsetp + bytes_to_write > *size) {
    253  1.1  christos 		*size *= 2;
    254  1.1  christos 		*block = realloc(*block, (size_t) *size);
    255  1.1  christos 		if (*block == NULL) {
    256  1.1  christos 			DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
    257  1.1  christos 			return (DW_DLE_MEMORY);
    258  1.1  christos 		}
    259  1.1  christos 	}
    260  1.1  christos 
    261  1.1  christos 	_dwarf_write_msb(*block, offsetp, value, bytes_to_write);
    262  1.1  christos 
    263  1.1  christos 	return (DW_DLE_NONE);
    264  1.1  christos }
    265  1.1  christos 
    266  1.1  christos int64_t
    267  1.1  christos _dwarf_read_sleb128(uint8_t *data, uint64_t *offsetp)
    268  1.1  christos {
    269  1.1  christos 	int64_t ret = 0;
    270  1.1  christos 	uint8_t b;
    271  1.1  christos 	int shift = 0;
    272  1.1  christos 	uint8_t *src;
    273  1.1  christos 
    274  1.1  christos 	src = data + *offsetp;
    275  1.1  christos 
    276  1.1  christos 	do {
    277  1.1  christos 		b = *src++;
    278  1.1  christos 		ret |= ((b & 0x7f) << shift);
    279  1.1  christos 		(*offsetp)++;
    280  1.1  christos 		shift += 7;
    281  1.1  christos 	} while ((b & 0x80) != 0);
    282  1.1  christos 
    283  1.1  christos 	if (shift < 64 && (b & 0x40) != 0)
    284  1.1  christos 		ret |= (-1 << shift);
    285  1.1  christos 
    286  1.1  christos 	return (ret);
    287  1.1  christos }
    288  1.1  christos 
    289  1.1  christos int
    290  1.1  christos _dwarf_write_sleb128(uint8_t *data, uint8_t *end, int64_t val)
    291  1.1  christos {
    292  1.1  christos 	uint8_t *p;
    293  1.1  christos 
    294  1.1  christos 	p = data;
    295  1.1  christos 
    296  1.1  christos 	for (;;) {
    297  1.1  christos 		if (p >= end)
    298  1.1  christos 			return (-1);
    299  1.1  christos 		*p = val & 0x7f;
    300  1.1  christos 		val >>= 7;
    301  1.1  christos 		if ((val == 0 && (*p & 0x40) == 0) ||
    302  1.1  christos 		    (val == -1 && (*p & 0x40) != 0)) {
    303  1.1  christos 			p++;
    304  1.1  christos 			break;
    305  1.1  christos 		}
    306  1.1  christos 		*p++ |= 0x80;
    307  1.1  christos 	}
    308  1.1  christos 
    309  1.1  christos 	return (p - data);
    310  1.1  christos }
    311  1.1  christos 
    312  1.1  christos int
    313  1.1  christos _dwarf_write_sleb128_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
    314  1.1  christos     int64_t val, Dwarf_Error *error)
    315  1.1  christos {
    316  1.1  christos 	int len;
    317  1.1  christos 
    318  1.1  christos 	assert(*size > 0);
    319  1.1  christos 
    320  1.1  christos 	while ((len = _dwarf_write_sleb128(*block + *offsetp, *block + *size,
    321  1.1  christos 	    val)) < 0) {
    322  1.1  christos 		*size *= 2;
    323  1.1  christos 		*block = realloc(*block, (size_t) *size);
    324  1.1  christos 		if (*block == NULL) {
    325  1.1  christos 			DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
    326  1.1  christos 			return (DW_DLE_MEMORY);
    327  1.1  christos 		}
    328  1.1  christos 	}
    329  1.1  christos 
    330  1.1  christos 	*offsetp += len;
    331  1.1  christos 
    332  1.1  christos 	return (DW_DLE_NONE);
    333  1.1  christos }
    334  1.1  christos 
    335  1.1  christos uint64_t
    336  1.1  christos _dwarf_read_uleb128(uint8_t *data, uint64_t *offsetp)
    337  1.1  christos {
    338  1.1  christos 	uint64_t ret = 0;
    339  1.1  christos 	uint8_t b;
    340  1.1  christos 	int shift = 0;
    341  1.1  christos 	uint8_t *src;
    342  1.1  christos 
    343  1.1  christos 	src = data + *offsetp;
    344  1.1  christos 
    345  1.1  christos 	do {
    346  1.1  christos 		b = *src++;
    347  1.1  christos 		ret |= ((b & 0x7f) << shift);
    348  1.1  christos 		(*offsetp)++;
    349  1.1  christos 		shift += 7;
    350  1.1  christos 	} while ((b & 0x80) != 0);
    351  1.1  christos 
    352  1.1  christos 	return (ret);
    353  1.1  christos }
    354  1.1  christos 
    355  1.1  christos int
    356  1.1  christos _dwarf_write_uleb128(uint8_t *data, uint8_t *end, uint64_t val)
    357  1.1  christos {
    358  1.1  christos 	uint8_t *p;
    359  1.1  christos 
    360  1.1  christos 	p = data;
    361  1.1  christos 
    362  1.1  christos 	do {
    363  1.1  christos 		if (p >= end)
    364  1.1  christos 			return (-1);
    365  1.1  christos 		*p = val & 0x7f;
    366  1.1  christos 		val >>= 7;
    367  1.1  christos 		if (val > 0)
    368  1.1  christos 			*p |= 0x80;
    369  1.1  christos 		p++;
    370  1.1  christos 	} while (val > 0);
    371  1.1  christos 
    372  1.1  christos 	return (p - data);
    373  1.1  christos }
    374  1.1  christos 
    375  1.1  christos int
    376  1.1  christos _dwarf_write_uleb128_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
    377  1.1  christos     uint64_t val, Dwarf_Error *error)
    378  1.1  christos {
    379  1.1  christos 	int len;
    380  1.1  christos 
    381  1.1  christos 	assert(*size > 0);
    382  1.1  christos 
    383  1.1  christos 	while ((len = _dwarf_write_uleb128(*block + *offsetp, *block + *size,
    384  1.1  christos 	    val)) < 0) {
    385  1.1  christos 		*size *= 2;
    386  1.1  christos 		*block = realloc(*block, (size_t) *size);
    387  1.1  christos 		if (*block == NULL) {
    388  1.1  christos 			DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
    389  1.1  christos 			return (DW_DLE_MEMORY);
    390  1.1  christos 		}
    391  1.1  christos 	}
    392  1.1  christos 
    393  1.1  christos 	*offsetp += len;
    394  1.1  christos 
    395  1.1  christos 	return (DW_DLE_NONE);
    396  1.1  christos }
    397  1.1  christos 
    398  1.1  christos int64_t
    399  1.1  christos _dwarf_decode_sleb128(uint8_t **dp)
    400  1.1  christos {
    401  1.1  christos 	int64_t ret = 0;
    402  1.1  christos 	uint8_t b;
    403  1.1  christos 	int shift = 0;
    404  1.1  christos 
    405  1.1  christos 	uint8_t *src = *dp;
    406  1.1  christos 
    407  1.1  christos 	do {
    408  1.1  christos 		b = *src++;
    409  1.1  christos 		ret |= ((b & 0x7f) << shift);
    410  1.1  christos 		shift += 7;
    411  1.1  christos 	} while ((b & 0x80) != 0);
    412  1.1  christos 
    413  1.1  christos 	if (shift < 64 && (b & 0x40) != 0)
    414  1.1  christos 		ret |= (-1 << shift);
    415  1.1  christos 
    416  1.1  christos 	*dp = src;
    417  1.1  christos 
    418  1.1  christos 	return (ret);
    419  1.1  christos }
    420  1.1  christos 
    421  1.1  christos uint64_t
    422  1.1  christos _dwarf_decode_uleb128(uint8_t **dp)
    423  1.1  christos {
    424  1.1  christos 	uint64_t ret = 0;
    425  1.1  christos 	uint8_t b;
    426  1.1  christos 	int shift = 0;
    427  1.1  christos 
    428  1.1  christos 	uint8_t *src = *dp;
    429  1.1  christos 
    430  1.1  christos 	do {
    431  1.1  christos 		b = *src++;
    432  1.1  christos 		ret |= ((b & 0x7f) << shift);
    433  1.1  christos 		shift += 7;
    434  1.1  christos 	} while ((b & 0x80) != 0);
    435  1.1  christos 
    436  1.1  christos 	*dp = src;
    437  1.1  christos 
    438  1.1  christos 	return (ret);
    439  1.1  christos }
    440  1.1  christos 
    441  1.1  christos char *
    442  1.1  christos _dwarf_read_string(void *data, Dwarf_Unsigned size, uint64_t *offsetp)
    443  1.1  christos {
    444  1.1  christos 	char *ret, *src;
    445  1.1  christos 
    446  1.1  christos 	ret = src = (char *) data + *offsetp;
    447  1.1  christos 
    448  1.1  christos 	while (*src != '\0' && *offsetp < size) {
    449  1.1  christos 		src++;
    450  1.1  christos 		(*offsetp)++;
    451  1.1  christos 	}
    452  1.1  christos 
    453  1.1  christos 	if (*src == '\0' && *offsetp < size)
    454  1.1  christos 		(*offsetp)++;
    455  1.1  christos 
    456  1.1  christos 	return (ret);
    457  1.1  christos }
    458  1.1  christos 
    459  1.1  christos void
    460  1.1  christos _dwarf_write_string(void *data, uint64_t *offsetp, char *string)
    461  1.1  christos {
    462  1.1  christos 	char *dst;
    463  1.1  christos 
    464  1.1  christos 	dst = (char *) data + *offsetp;
    465  1.1  christos 	strcpy(dst, string);
    466  1.1  christos 	(*offsetp) += strlen(string) + 1;
    467  1.1  christos }
    468  1.1  christos 
    469  1.1  christos int
    470  1.1  christos _dwarf_write_string_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
    471  1.1  christos     char *string, Dwarf_Error *error)
    472  1.1  christos {
    473  1.1  christos 	size_t len;
    474  1.1  christos 
    475  1.1  christos 	assert(*size > 0);
    476  1.1  christos 
    477  1.1  christos 	len = strlen(string) + 1;
    478  1.1  christos 	while (*offsetp + len > *size) {
    479  1.1  christos 		*size *= 2;
    480  1.1  christos 		*block = realloc(*block, (size_t) *size);
    481  1.1  christos 		if (*block == NULL) {
    482  1.1  christos 			DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
    483  1.1  christos 			return (DW_DLE_MEMORY);
    484  1.1  christos 		}
    485  1.1  christos 	}
    486  1.1  christos 
    487  1.1  christos 	_dwarf_write_string(*block, offsetp, string);
    488  1.1  christos 
    489  1.1  christos 	return (DW_DLE_NONE);
    490  1.1  christos }
    491  1.1  christos 
    492  1.1  christos uint8_t *
    493  1.1  christos _dwarf_read_block(void *data, uint64_t *offsetp, uint64_t length)
    494  1.1  christos {
    495  1.1  christos 	uint8_t *ret, *src;
    496  1.1  christos 
    497  1.1  christos 	ret = src = (uint8_t *) data + *offsetp;
    498  1.1  christos 
    499  1.1  christos 	(*offsetp) += length;
    500  1.1  christos 
    501  1.1  christos 	return (ret);
    502  1.1  christos }
    503  1.1  christos 
    504  1.1  christos void
    505  1.1  christos _dwarf_write_block(void *data, uint64_t *offsetp, uint8_t *blk,
    506  1.1  christos     uint64_t length)
    507  1.1  christos {
    508  1.1  christos 	uint8_t *dst;
    509  1.1  christos 
    510  1.1  christos 	dst = (uint8_t *) data + *offsetp;
    511  1.1  christos 	memcpy(dst, blk, length);
    512  1.1  christos 	(*offsetp) += length;
    513  1.1  christos }
    514  1.1  christos 
    515  1.1  christos int
    516  1.1  christos _dwarf_write_block_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
    517  1.1  christos     uint8_t *blk, uint64_t length, Dwarf_Error *error)
    518  1.1  christos {
    519  1.1  christos 
    520  1.1  christos 	assert(*size > 0);
    521  1.1  christos 
    522  1.1  christos 	while (*offsetp + length > *size) {
    523  1.1  christos 		*size *= 2;
    524  1.1  christos 		*block = realloc(*block, (size_t) *size);
    525  1.1  christos 		if (*block == NULL) {
    526  1.1  christos 			DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
    527  1.1  christos 			return (DW_DLE_MEMORY);
    528  1.1  christos 		}
    529  1.1  christos 	}
    530  1.1  christos 
    531  1.1  christos 	_dwarf_write_block(*block, offsetp, blk, length);
    532  1.1  christos 
    533  1.1  christos 	return (DW_DLE_NONE);
    534  1.1  christos }
    535  1.1  christos 
    536  1.1  christos void
    537  1.1  christos _dwarf_write_padding(void *data, uint64_t *offsetp, uint8_t byte,
    538  1.1  christos     uint64_t length)
    539  1.1  christos {
    540  1.1  christos 	uint8_t *dst;
    541  1.1  christos 
    542  1.1  christos 	dst = (uint8_t *) data + *offsetp;
    543  1.1  christos 	memset(dst, byte, length);
    544  1.1  christos 	(*offsetp) += length;
    545  1.1  christos }
    546  1.1  christos 
    547  1.1  christos int
    548  1.1  christos _dwarf_write_padding_alloc(uint8_t **block, uint64_t *size, uint64_t *offsetp,
    549  1.1  christos     uint8_t byte, uint64_t cnt, Dwarf_Error *error)
    550  1.1  christos {
    551  1.1  christos 	assert(*size > 0);
    552  1.1  christos 
    553  1.1  christos 	while (*offsetp + cnt > *size) {
    554  1.1  christos 		*size *= 2;
    555  1.1  christos 		*block = realloc(*block, (size_t) *size);
    556  1.1  christos 		if (*block == NULL) {
    557  1.1  christos 			DWARF_SET_ERROR(NULL, error, DW_DLE_MEMORY);
    558  1.1  christos 			return (DW_DLE_MEMORY);
    559  1.1  christos 		}
    560  1.1  christos 	}
    561  1.1  christos 
    562  1.1  christos 	_dwarf_write_padding(*block, offsetp, byte, cnt);
    563  1.1  christos 
    564  1.1  christos 	return (DW_DLE_NONE);
    565  1.1  christos }
    566