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