Home | History | Annotate | Line # | Download | only in libdwarf
libdwarf_reloc.c revision 1.1.1.3
      1  1.1.1.2  christos /*	$NetBSD: libdwarf_reloc.c,v 1.1.1.3 2024/03/03 14:41:48 christos Exp $	*/
      2  1.1.1.3  christos 
      3      1.1  christos /*-
      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.3  christos ELFTC_VCSID("Id: libdwarf_reloc.c 3741 2019-06-07 06:32:01Z jkoshy");
     32      1.1  christos 
     33      1.1  christos Dwarf_Unsigned
     34      1.1  christos _dwarf_get_reloc_type(Dwarf_P_Debug dbg, int is64)
     35      1.1  christos {
     36      1.1  christos 
     37      1.1  christos 	assert(dbg != NULL);
     38      1.1  christos 
     39      1.1  christos 	switch (dbg->dbgp_isa) {
     40  1.1.1.2  christos 	case DW_ISA_AARCH64:
     41  1.1.1.2  christos 		return (is64 ? R_AARCH64_ABS64 : R_AARCH64_ABS32);
     42      1.1  christos 	case DW_ISA_X86:
     43      1.1  christos 		return (R_386_32);
     44      1.1  christos 	case DW_ISA_X86_64:
     45      1.1  christos 		return (is64 ? R_X86_64_64 : R_X86_64_32);
     46      1.1  christos 	case DW_ISA_SPARC:
     47      1.1  christos 		return (is64 ? R_SPARC_UA64 : R_SPARC_UA32);
     48      1.1  christos 	case DW_ISA_PPC:
     49  1.1.1.3  christos 		return (is64 ? R_PPC64_ADDR64 : R_PPC_ADDR32);
     50      1.1  christos 	case DW_ISA_ARM:
     51      1.1  christos 		return (R_ARM_ABS32);
     52      1.1  christos 	case DW_ISA_MIPS:
     53      1.1  christos 		return (is64 ? R_MIPS_64 : R_MIPS_32);
     54  1.1.1.3  christos 	case DW_ISA_RISCV:
     55  1.1.1.3  christos 		return (is64 ? R_RISCV_64 : R_RISCV_32);
     56      1.1  christos 	case DW_ISA_IA64:
     57      1.1  christos 		return (is64 ? R_IA_64_DIR64LSB : R_IA_64_DIR32LSB);
     58      1.1  christos 	default:
     59      1.1  christos 		break;
     60      1.1  christos 	}
     61      1.1  christos 	return (0);		/* NOT REACHED */
     62      1.1  christos }
     63      1.1  christos 
     64      1.1  christos int
     65      1.1  christos _dwarf_get_reloc_size(Dwarf_Debug dbg, Dwarf_Unsigned rel_type)
     66      1.1  christos {
     67      1.1  christos 
     68      1.1  christos 	switch (dbg->dbg_machine) {
     69      1.1  christos 	case EM_NONE:
     70      1.1  christos 		break;
     71  1.1.1.2  christos 	case EM_AARCH64:
     72  1.1.1.2  christos 		if (rel_type == R_AARCH64_ABS32)
     73  1.1.1.2  christos 			return (4);
     74  1.1.1.2  christos 		else if (rel_type == R_AARCH64_ABS64)
     75  1.1.1.2  christos 			return (8);
     76  1.1.1.2  christos 		break;
     77      1.1  christos 	case EM_ARM:
     78      1.1  christos 		if (rel_type == R_ARM_ABS32)
     79      1.1  christos 			return (4);
     80      1.1  christos 		break;
     81      1.1  christos 	case EM_386:
     82  1.1.1.2  christos 	case EM_IAMCU:
     83      1.1  christos 		if (rel_type == R_386_32)
     84      1.1  christos 			return (4);
     85      1.1  christos 		break;
     86      1.1  christos 	case EM_X86_64:
     87      1.1  christos 		if (rel_type == R_X86_64_32)
     88      1.1  christos 			return (4);
     89      1.1  christos 		else if (rel_type == R_X86_64_64)
     90      1.1  christos 			return (8);
     91      1.1  christos 		break;
     92      1.1  christos 	case EM_SPARC:
     93      1.1  christos 		if (rel_type == R_SPARC_UA32)
     94      1.1  christos 			return (4);
     95      1.1  christos 		else if (rel_type == R_SPARC_UA64)
     96      1.1  christos 			return (8);
     97      1.1  christos 		break;
     98      1.1  christos 	case EM_PPC:
     99      1.1  christos 		if (rel_type == R_PPC_ADDR32)
    100      1.1  christos 			return (4);
    101      1.1  christos 		break;
    102  1.1.1.3  christos 	case EM_PPC64:
    103  1.1.1.3  christos 		if (rel_type == R_PPC_ADDR32)
    104  1.1.1.3  christos 			return (4);
    105  1.1.1.3  christos 		else if (rel_type == R_PPC64_ADDR64)
    106  1.1.1.3  christos 			return (8);
    107  1.1.1.3  christos 		break;
    108      1.1  christos 	case EM_MIPS:
    109      1.1  christos 		if (rel_type == R_MIPS_32)
    110      1.1  christos 			return (4);
    111      1.1  christos 		else if (rel_type == R_MIPS_64)
    112      1.1  christos 			return (8);
    113      1.1  christos 		break;
    114  1.1.1.3  christos 	case EM_RISCV:
    115  1.1.1.3  christos 		if (rel_type == R_RISCV_32)
    116  1.1.1.3  christos 			return (4);
    117  1.1.1.3  christos 		else if (rel_type == R_RISCV_64)
    118  1.1.1.3  christos 			return (8);
    119  1.1.1.3  christos 		break;
    120      1.1  christos 	case EM_IA_64:
    121      1.1  christos 		if (rel_type == R_IA_64_SECREL32LSB)
    122      1.1  christos 			return (4);
    123      1.1  christos 		else if (rel_type == R_IA_64_DIR64LSB)
    124      1.1  christos 			return (8);
    125      1.1  christos 		break;
    126      1.1  christos 	default:
    127      1.1  christos 		break;
    128      1.1  christos 	}
    129      1.1  christos 
    130      1.1  christos 	/* unknown relocation. */
    131      1.1  christos 	return (0);
    132      1.1  christos }
    133      1.1  christos 
    134      1.1  christos int
    135      1.1  christos _dwarf_reloc_section_init(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp,
    136      1.1  christos     Dwarf_P_Section ref, Dwarf_Error *error)
    137      1.1  christos {
    138      1.1  christos 	Dwarf_Rel_Section drs;
    139      1.1  christos 	char name[128];
    140      1.1  christos 	int pseudo;
    141      1.1  christos 
    142      1.1  christos 	assert(dbg != NULL && drsp != NULL && ref != NULL);
    143      1.1  christos 
    144      1.1  christos 	if ((drs = calloc(1, sizeof(struct _Dwarf_Rel_Section))) == NULL) {
    145      1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
    146      1.1  christos 		return (DW_DLE_MEMORY);
    147      1.1  christos 	}
    148      1.1  christos 
    149      1.1  christos 	drs->drs_ref = ref;
    150      1.1  christos 
    151      1.1  christos 	/*
    152      1.1  christos 	 * FIXME The logic here is most likely wrong. It should
    153      1.1  christos 	 * be the ISA that determines relocation type.
    154      1.1  christos 	 */
    155      1.1  christos 	if (dbg->dbgp_flags & DW_DLC_SIZE_64)
    156      1.1  christos 		drs->drs_addend = 1;
    157      1.1  christos 	else
    158      1.1  christos 		drs->drs_addend = 0;
    159      1.1  christos 
    160      1.1  christos 	if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS)
    161      1.1  christos 		pseudo = 1;
    162      1.1  christos 	else
    163      1.1  christos 		pseudo = 0;
    164      1.1  christos 
    165      1.1  christos 	snprintf(name, sizeof(name), "%s%s",
    166      1.1  christos 	    drs->drs_addend ? ".rela" : ".rel", ref->ds_name);
    167      1.1  christos 	if (_dwarf_section_init(dbg, &drs->drs_ds, name, pseudo, error) !=
    168      1.1  christos 	    DW_DLE_NONE) {
    169      1.1  christos 		free(drs);
    170      1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
    171      1.1  christos 		return (DW_DLE_MEMORY);
    172      1.1  christos 	}
    173      1.1  christos 
    174      1.1  christos 	STAILQ_INIT(&drs->drs_dre);
    175      1.1  christos 	STAILQ_INSERT_TAIL(&dbg->dbgp_drslist, drs, drs_next);
    176      1.1  christos 	dbg->dbgp_drscnt++;
    177      1.1  christos 	*drsp = drs;
    178      1.1  christos 
    179      1.1  christos 	return (DW_DLE_NONE);
    180      1.1  christos }
    181      1.1  christos 
    182      1.1  christos void
    183      1.1  christos _dwarf_reloc_section_free(Dwarf_P_Debug dbg, Dwarf_Rel_Section *drsp)
    184      1.1  christos {
    185      1.1  christos 	Dwarf_Rel_Section drs, tdrs;
    186      1.1  christos 	Dwarf_Rel_Entry dre, tdre;
    187      1.1  christos 
    188      1.1  christos 	assert(dbg != NULL && drsp != NULL);
    189      1.1  christos 
    190      1.1  christos 	if (*drsp == NULL)
    191      1.1  christos 		return;
    192      1.1  christos 
    193      1.1  christos 	STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
    194      1.1  christos 		if (drs != *drsp)
    195      1.1  christos 			continue;
    196      1.1  christos 		STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
    197      1.1  christos 		    drs_next);
    198      1.1  christos 		STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
    199      1.1  christos 			STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
    200      1.1  christos 			    dre_next);
    201      1.1  christos 			free(dre);
    202      1.1  christos 		}
    203      1.1  christos 		if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0)
    204      1.1  christos 			_dwarf_section_free(dbg, &drs->drs_ds);
    205      1.1  christos 		else {
    206      1.1  christos 			if (drs->drs_ds->ds_name)
    207      1.1  christos 				free(drs->drs_ds->ds_name);
    208      1.1  christos 			free(drs->drs_ds);
    209      1.1  christos 		}
    210      1.1  christos 		free(drs);
    211      1.1  christos 		*drsp = NULL;
    212      1.1  christos 		dbg->dbgp_drscnt--;
    213      1.1  christos 		break;
    214      1.1  christos 	}
    215      1.1  christos }
    216      1.1  christos 
    217      1.1  christos int
    218      1.1  christos _dwarf_reloc_entry_add(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
    219      1.1  christos     Dwarf_P_Section ds, unsigned char type, unsigned char length,
    220      1.1  christos     Dwarf_Unsigned offset, Dwarf_Unsigned symndx, Dwarf_Unsigned addend,
    221      1.1  christos     const char *secname, Dwarf_Error *error)
    222      1.1  christos {
    223      1.1  christos 	Dwarf_Rel_Entry dre;
    224      1.1  christos 	Dwarf_Unsigned reloff;
    225      1.1  christos 	int ret;
    226      1.1  christos 
    227      1.1  christos 	assert(drs != NULL);
    228      1.1  christos 	assert(offset <= ds->ds_size);
    229      1.1  christos 	reloff = offset;
    230      1.1  christos 
    231      1.1  christos 	/*
    232      1.1  christos 	 * If the DW_DLC_SYMBOLIC_RELOCATIONS flag is set or ElfXX_Rel
    233      1.1  christos 	 * is used instead of ELfXX_Rela, we need to write the addend
    234      1.1  christos 	 * in the storage unit to be relocated. Otherwise write 0 in the
    235      1.1  christos 	 * storage unit and the addend will be written into relocation
    236      1.1  christos 	 * section later.
    237      1.1  christos 	 */
    238      1.1  christos 	if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) ||
    239      1.1  christos 	    drs->drs_addend == 0)
    240      1.1  christos 		ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
    241      1.1  christos 		    addend, length, error);
    242      1.1  christos 	else
    243      1.1  christos 		ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
    244      1.1  christos 		    0, length, error);
    245      1.1  christos 	if (ret != DW_DLE_NONE)
    246      1.1  christos 		return (ret);
    247      1.1  christos 	if (offset > ds->ds_size)
    248      1.1  christos 		ds->ds_size = offset;
    249      1.1  christos 
    250      1.1  christos 	if ((dre = calloc(1, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
    251      1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
    252      1.1  christos 		return (DW_DLE_MEMORY);
    253      1.1  christos 	}
    254      1.1  christos 	STAILQ_INSERT_TAIL(&drs->drs_dre, dre, dre_next);
    255      1.1  christos 	dre->dre_type = type;
    256      1.1  christos 	dre->dre_length = length;
    257      1.1  christos 	dre->dre_offset = reloff;
    258      1.1  christos 	dre->dre_symndx = symndx;
    259      1.1  christos 	dre->dre_addend = addend;
    260      1.1  christos 	dre->dre_secname = secname;
    261      1.1  christos 	drs->drs_drecnt++;
    262      1.1  christos 
    263      1.1  christos 	return (DW_DLE_NONE);
    264      1.1  christos }
    265      1.1  christos 
    266      1.1  christos int
    267      1.1  christos _dwarf_reloc_entry_add_pair(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
    268      1.1  christos     Dwarf_P_Section ds, unsigned char length, Dwarf_Unsigned offset,
    269      1.1  christos     Dwarf_Unsigned symndx, Dwarf_Unsigned esymndx, Dwarf_Unsigned symoff,
    270      1.1  christos     Dwarf_Unsigned esymoff, Dwarf_Error *error)
    271      1.1  christos {
    272      1.1  christos 	Dwarf_Rel_Entry dre;
    273      1.1  christos 	Dwarf_Unsigned reloff;
    274      1.1  christos 	int ret;
    275      1.1  christos 
    276      1.1  christos 	assert(drs != NULL);
    277      1.1  christos 	assert(offset <= ds->ds_size);
    278      1.1  christos 	assert(dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS);
    279      1.1  christos 	reloff = offset;
    280      1.1  christos 
    281      1.1  christos 	/* Write net offset into section stream. */
    282      1.1  christos 	ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
    283      1.1  christos 	    esymoff - symoff, length, error);
    284      1.1  christos 	if (ret != DW_DLE_NONE)
    285      1.1  christos 		return (ret);
    286      1.1  christos 	if (offset > ds->ds_size)
    287      1.1  christos 		ds->ds_size = offset;
    288      1.1  christos 
    289      1.1  christos 	if ((dre = calloc(2, sizeof(struct _Dwarf_Rel_Entry))) == NULL) {
    290      1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
    291      1.1  christos 		return (DW_DLE_MEMORY);
    292      1.1  christos 	}
    293      1.1  christos 	STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[0], dre_next);
    294      1.1  christos 	STAILQ_INSERT_TAIL(&drs->drs_dre, &dre[1], dre_next);
    295      1.1  christos 	dre[0].dre_type = dwarf_drt_first_of_length_pair;
    296      1.1  christos 	dre[0].dre_length = length;
    297      1.1  christos 	dre[0].dre_offset = reloff;
    298      1.1  christos 	dre[0].dre_symndx = symndx;
    299      1.1  christos 	dre[0].dre_addend = 0;
    300      1.1  christos 	dre[0].dre_secname = NULL;
    301      1.1  christos 	dre[1].dre_type = dwarf_drt_second_of_length_pair;
    302      1.1  christos 	dre[1].dre_length = length;
    303      1.1  christos 	dre[1].dre_offset = reloff;
    304      1.1  christos 	dre[1].dre_symndx = esymndx;
    305      1.1  christos 	dre[1].dre_addend = 0;
    306      1.1  christos 	dre[1].dre_secname = NULL;
    307      1.1  christos 	drs->drs_drecnt += 2;
    308      1.1  christos 
    309      1.1  christos 	return (DW_DLE_NONE);
    310      1.1  christos }
    311      1.1  christos 
    312      1.1  christos int
    313      1.1  christos _dwarf_reloc_section_finalize(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
    314      1.1  christos     Dwarf_Error *error)
    315      1.1  christos {
    316      1.1  christos 	Dwarf_P_Section ds;
    317      1.1  christos 	Dwarf_Unsigned unit;
    318      1.1  christos 	int ret, size;
    319      1.1  christos 
    320      1.1  christos 	assert(dbg != NULL && drs != NULL && drs->drs_ds != NULL &&
    321      1.1  christos 	    drs->drs_ref != NULL);
    322      1.1  christos 
    323      1.1  christos 	ds = drs->drs_ds;
    324      1.1  christos 
    325      1.1  christos 	/*
    326      1.1  christos 	 * Calculate the size (in bytes) of the relocation section.
    327      1.1  christos 	 */
    328      1.1  christos 	if (dbg->dbgp_flags & DW_DLC_SIZE_64)
    329      1.1  christos 		unit = drs->drs_addend ? sizeof(Elf64_Rela) : sizeof(Elf64_Rel);
    330      1.1  christos 	else
    331      1.1  christos 		unit = drs->drs_addend ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel);
    332      1.1  christos 	assert(ds->ds_size == 0);
    333      1.1  christos 	size = drs->drs_drecnt * unit;
    334      1.1  christos 
    335      1.1  christos 	/*
    336      1.1  christos 	 * Discard this relocation section if there is no entry in it.
    337      1.1  christos 	 */
    338      1.1  christos 	if (size == 0) {
    339      1.1  christos 		_dwarf_reloc_section_free(dbg, &drs);
    340      1.1  christos 		return (DW_DLE_NONE);
    341      1.1  christos 	}
    342      1.1  christos 
    343      1.1  christos 	/*
    344      1.1  christos 	 * If we are under stream mode, realloc the section data block to
    345      1.1  christos 	 * this size.
    346      1.1  christos 	 */
    347      1.1  christos 	if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
    348      1.1  christos 		ds->ds_cap = size;
    349      1.1  christos 		if ((ds->ds_data = realloc(ds->ds_data, (size_t) ds->ds_cap)) ==
    350      1.1  christos 		    NULL) {
    351      1.1  christos 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
    352      1.1  christos 			return (DW_DLE_MEMORY);
    353      1.1  christos 		}
    354      1.1  christos 	}
    355      1.1  christos 
    356      1.1  christos 	/*
    357      1.1  christos 	 * Notify the application the creation of this relocation section.
    358      1.1  christos 	 * Note that the section link here should point to the .symtab
    359      1.1  christos 	 * section, we set it to 0 since we have no way to know .symtab
    360      1.1  christos 	 * section index.
    361      1.1  christos 	 */
    362      1.1  christos 	ret = _dwarf_pro_callback(dbg, ds->ds_name, size,
    363      1.1  christos 	    drs->drs_addend ? SHT_RELA : SHT_REL, 0, 0, drs->drs_ref->ds_ndx,
    364      1.1  christos 	    &ds->ds_symndx, NULL);
    365      1.1  christos 	if (ret < 0) {
    366      1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ELF_SECT_ERR);
    367      1.1  christos 		return (DW_DLE_ELF_SECT_ERR);
    368      1.1  christos 	}
    369      1.1  christos 	ds->ds_ndx = ret;
    370      1.1  christos 
    371      1.1  christos 	return (DW_DLE_NONE);
    372      1.1  christos }
    373      1.1  christos 
    374      1.1  christos int
    375      1.1  christos _dwarf_reloc_section_gen(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
    376      1.1  christos     Dwarf_Error *error)
    377      1.1  christos {
    378      1.1  christos 	Dwarf_Rel_Entry dre;
    379      1.1  christos 	Dwarf_P_Section ds;
    380      1.1  christos 	Dwarf_Unsigned type;
    381      1.1  christos 	int ret;
    382      1.1  christos 
    383      1.1  christos 	assert((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0);
    384      1.1  christos 	assert(drs->drs_ds != NULL && drs->drs_ds->ds_size == 0);
    385      1.1  christos 	assert(!STAILQ_EMPTY(&drs->drs_dre));
    386      1.1  christos 	ds = drs->drs_ds;
    387      1.1  christos 
    388      1.1  christos 	STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
    389      1.1  christos 		assert(dre->dre_length == 4 || dre->dre_length == 8);
    390      1.1  christos 		type = _dwarf_get_reloc_type(dbg, dre->dre_length == 8);
    391      1.1  christos 		if (dbg->dbgp_flags & DW_DLC_SIZE_64) {
    392      1.1  christos 			/* Write r_offset (8 bytes) */
    393      1.1  christos 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
    394      1.1  christos 			    &ds->ds_size, dre->dre_offset, 8, error);
    395      1.1  christos 			if (ret != DW_DLE_NONE)
    396      1.1  christos 				return (ret);
    397      1.1  christos 			/* Write r_info (8 bytes) */
    398      1.1  christos 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
    399      1.1  christos 			    &ds->ds_size, ELF64_R_INFO(dre->dre_symndx, type),
    400      1.1  christos 			    8, error);
    401      1.1  christos 			if (ret != DW_DLE_NONE)
    402      1.1  christos 				return (ret);
    403      1.1  christos 			/* Write r_addend (8 bytes) */
    404      1.1  christos 			if (drs->drs_addend) {
    405      1.1  christos 				ret = dbg->write_alloc(&ds->ds_data,
    406      1.1  christos 				    &ds->ds_cap, &ds->ds_size, dre->dre_addend,
    407      1.1  christos 				    8, error);
    408      1.1  christos 				if (ret != DW_DLE_NONE)
    409      1.1  christos 					return (ret);
    410      1.1  christos 			}
    411      1.1  christos 		} else {
    412      1.1  christos 			/* Write r_offset (4 bytes) */
    413      1.1  christos 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
    414      1.1  christos 			    &ds->ds_size, dre->dre_offset, 4, error);
    415      1.1  christos 			if (ret != DW_DLE_NONE)
    416      1.1  christos 				return (ret);
    417      1.1  christos 			/* Write r_info (4 bytes) */
    418      1.1  christos 			ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
    419      1.1  christos 			    &ds->ds_size, ELF32_R_INFO(dre->dre_symndx, type),
    420      1.1  christos 			    4, error);
    421      1.1  christos 			if (ret != DW_DLE_NONE)
    422      1.1  christos 				return (ret);
    423      1.1  christos 			/* Write r_addend (4 bytes) */
    424      1.1  christos 			if (drs->drs_addend) {
    425      1.1  christos 				ret = dbg->write_alloc(&ds->ds_data,
    426      1.1  christos 				    &ds->ds_cap, &ds->ds_size, dre->dre_addend,
    427      1.1  christos 				    4, error);
    428      1.1  christos 				if (ret != DW_DLE_NONE)
    429      1.1  christos 					return (ret);
    430      1.1  christos 			}
    431      1.1  christos 		}
    432      1.1  christos 	}
    433      1.1  christos 	assert(ds->ds_size == ds->ds_cap);
    434      1.1  christos 
    435      1.1  christos 	return (DW_DLE_NONE);
    436      1.1  christos }
    437      1.1  christos 
    438      1.1  christos int
    439      1.1  christos _dwarf_reloc_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
    440      1.1  christos {
    441      1.1  christos 	Dwarf_Rel_Section drs;
    442      1.1  christos 	Dwarf_Rel_Entry dre;
    443      1.1  christos 	Dwarf_P_Section ds;
    444      1.1  christos 	int ret;
    445      1.1  christos 
    446      1.1  christos 	STAILQ_FOREACH(drs, &dbg->dbgp_drslist, drs_next) {
    447      1.1  christos 		/*
    448      1.1  christos 		 * Update relocation entries: translate any section name
    449      1.1  christos 		 * reference to section symbol index.
    450      1.1  christos 		 */
    451      1.1  christos 		STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
    452      1.1  christos 			if (dre->dre_secname == NULL)
    453      1.1  christos 				continue;
    454      1.1  christos 			ds = _dwarf_pro_find_section(dbg, dre->dre_secname);
    455      1.1  christos 			assert(ds != NULL && ds->ds_symndx != 0);
    456      1.1  christos 			dre->dre_symndx = ds->ds_symndx;
    457      1.1  christos 		}
    458      1.1  christos 
    459      1.1  christos 		/*
    460      1.1  christos 		 * Generate ELF relocation section if we are under stream
    461      1.1  christos 		 * mode.
    462      1.1  christos 		 */
    463      1.1  christos 		if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
    464      1.1  christos 			ret = _dwarf_reloc_section_gen(dbg, drs, error);
    465      1.1  christos 			if (ret != DW_DLE_NONE)
    466      1.1  christos 				return (ret);
    467      1.1  christos 		}
    468      1.1  christos 	}
    469      1.1  christos 
    470      1.1  christos 	return (DW_DLE_NONE);
    471      1.1  christos }
    472      1.1  christos 
    473      1.1  christos void
    474      1.1  christos _dwarf_reloc_cleanup(Dwarf_P_Debug dbg)
    475      1.1  christos {
    476      1.1  christos 	Dwarf_Rel_Section drs, tdrs;
    477      1.1  christos 	Dwarf_Rel_Entry dre, tdre;
    478      1.1  christos 
    479      1.1  christos 	assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
    480      1.1  christos 
    481      1.1  christos 	STAILQ_FOREACH_SAFE(drs, &dbg->dbgp_drslist, drs_next, tdrs) {
    482      1.1  christos 		STAILQ_REMOVE(&dbg->dbgp_drslist, drs, _Dwarf_Rel_Section,
    483      1.1  christos 		    drs_next);
    484      1.1  christos 		free(drs->drs_drd);
    485      1.1  christos 		STAILQ_FOREACH_SAFE(dre, &drs->drs_dre, dre_next, tdre) {
    486      1.1  christos 			STAILQ_REMOVE(&drs->drs_dre, dre, _Dwarf_Rel_Entry,
    487      1.1  christos 			    dre_next);
    488      1.1  christos 			free(dre);
    489      1.1  christos 		}
    490      1.1  christos 		if (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
    491      1.1  christos 			if (drs->drs_ds) {
    492      1.1  christos 				if (drs->drs_ds->ds_name)
    493      1.1  christos 					free(drs->drs_ds->ds_name);
    494      1.1  christos 				free(drs->drs_ds);
    495      1.1  christos 			}
    496      1.1  christos 		}
    497      1.1  christos 		free(drs);
    498      1.1  christos 	}
    499      1.1  christos 	dbg->dbgp_drscnt = 0;
    500      1.1  christos 	dbg->dbgp_drspos = NULL;
    501      1.1  christos }
    502