Home | History | Annotate | Line # | Download | only in libdwarf
      1  1.5  christos /*	$NetBSD: libdwarf_sections.c,v 1.5 2024/03/03 17:37:32 christos Exp $	*/
      2  1.2  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.5  christos __RCSID("$NetBSD: libdwarf_sections.c,v 1.5 2024/03/03 17:37:32 christos Exp $");
     32  1.4    jkoshy ELFTC_VCSID("Id: libdwarf_sections.c 3041 2014-05-18 15:11:03Z kaiwang27");
     33  1.1  christos 
     34  1.1  christos #define	_SECTION_INIT_SIZE	128
     35  1.1  christos 
     36  1.1  christos int
     37  1.1  christos _dwarf_section_init(Dwarf_P_Debug dbg, Dwarf_P_Section *dsp, const char *name,
     38  1.1  christos     int pseudo, Dwarf_Error *error)
     39  1.1  christos {
     40  1.1  christos 	Dwarf_P_Section ds;
     41  1.1  christos 
     42  1.1  christos 	assert(dbg != NULL && dsp != NULL && name != NULL);
     43  1.1  christos 
     44  1.1  christos 	if ((ds = calloc(1, sizeof(struct _Dwarf_P_Section))) == NULL) {
     45  1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
     46  1.1  christos 		return (DW_DLE_MEMORY);
     47  1.1  christos 	}
     48  1.1  christos 
     49  1.1  christos 	if ((ds->ds_name = strdup(name)) == NULL) {
     50  1.1  christos 		free(ds);
     51  1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
     52  1.1  christos 		return (DW_DLE_MEMORY);
     53  1.1  christos 	}
     54  1.1  christos 
     55  1.1  christos 	if (!pseudo) {
     56  1.1  christos 		ds->ds_cap = _SECTION_INIT_SIZE;
     57  1.1  christos 		if ((ds->ds_data = malloc((size_t) ds->ds_cap)) == NULL) {
     58  1.1  christos 			free(ds->ds_name);
     59  1.1  christos 			free(ds);
     60  1.1  christos 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
     61  1.1  christos 			return (DW_DLE_MEMORY);
     62  1.1  christos 		}
     63  1.1  christos 		STAILQ_INSERT_TAIL(&dbg->dbgp_seclist, ds, ds_next);
     64  1.1  christos 		dbg->dbgp_seccnt++;
     65  1.1  christos 	}
     66  1.1  christos 
     67  1.1  christos 	*dsp = ds;
     68  1.1  christos 
     69  1.1  christos 	return (DW_DLE_NONE);
     70  1.1  christos }
     71  1.1  christos 
     72  1.1  christos void
     73  1.1  christos _dwarf_section_free(Dwarf_P_Debug dbg, Dwarf_P_Section *dsp)
     74  1.1  christos {
     75  1.1  christos 	Dwarf_P_Section ds, tds;
     76  1.1  christos 
     77  1.1  christos 	assert(dbg != NULL && dsp != NULL);
     78  1.1  christos 
     79  1.1  christos 	if (*dsp == NULL)
     80  1.1  christos 		return;
     81  1.1  christos 
     82  1.1  christos 	STAILQ_FOREACH_SAFE(ds, &dbg->dbgp_seclist, ds_next, tds) {
     83  1.1  christos 		if (ds == *dsp) {
     84  1.1  christos 			STAILQ_REMOVE(&dbg->dbgp_seclist, ds, _Dwarf_P_Section,
     85  1.1  christos 			    ds_next);
     86  1.1  christos 			dbg->dbgp_seccnt--;
     87  1.1  christos 			break;
     88  1.1  christos 		}
     89  1.1  christos 	}
     90  1.1  christos 	ds = *dsp;
     91  1.1  christos 	if (ds->ds_name)
     92  1.1  christos 		free(ds->ds_name);
     93  1.1  christos 	if (ds->ds_data)
     94  1.1  christos 		free(ds->ds_data);
     95  1.1  christos 	free(ds);
     96  1.1  christos 	*dsp = NULL;
     97  1.1  christos }
     98  1.1  christos 
     99  1.1  christos int
    100  1.1  christos _dwarf_pro_callback(Dwarf_P_Debug dbg, char *name, int size,
    101  1.1  christos     Dwarf_Unsigned type, Dwarf_Unsigned flags, Dwarf_Unsigned link,
    102  1.1  christos     Dwarf_Unsigned info, Dwarf_Unsigned *symndx, int *error)
    103  1.1  christos {
    104  1.1  christos 	int e, ret, isymndx;
    105  1.1  christos 
    106  1.1  christos 	assert(dbg != NULL && name != NULL && symndx != NULL);
    107  1.1  christos 
    108  1.1  christos 	if (dbg->dbgp_func_b)
    109  1.1  christos 		ret = dbg->dbgp_func_b(name, size, type, flags, link, info,
    110  1.1  christos 		    symndx, &e);
    111  1.1  christos 	else {
    112  1.1  christos 		ret = dbg->dbgp_func(name, size, type, flags, link, info,
    113  1.1  christos 		    &isymndx, &e);
    114  1.1  christos 		*symndx = isymndx;
    115  1.1  christos 	}
    116  1.1  christos 	if (ret < 0) {
    117  1.1  christos 		if (error)
    118  1.1  christos 			*error = e;
    119  1.1  christos 	}
    120  1.1  christos 
    121  1.1  christos 	return (ret);
    122  1.1  christos }
    123  1.1  christos 
    124  1.1  christos int
    125  1.1  christos _dwarf_section_callback(Dwarf_P_Debug dbg, Dwarf_P_Section ds,
    126  1.1  christos     Dwarf_Unsigned type, Dwarf_Unsigned flags, Dwarf_Unsigned link,
    127  1.1  christos     Dwarf_Unsigned info, Dwarf_Error *error)
    128  1.1  christos {
    129  1.1  christos 	int ret, ndx;
    130  1.1  christos 
    131  1.1  christos 	ndx = _dwarf_pro_callback(dbg, ds->ds_name, (int) ds->ds_size,
    132  1.1  christos 	    type, flags, link, info, &ds->ds_symndx, NULL);
    133  1.1  christos 	if (ndx < 0) {
    134  1.1  christos 		ret = DW_DLE_ELF_SECT_ERR;
    135  1.1  christos 		DWARF_SET_ERROR(dbg, error, ret);
    136  1.1  christos 		return (ret);
    137  1.1  christos 	}
    138  1.1  christos 	ds->ds_ndx = ndx;
    139  1.1  christos 
    140  1.1  christos 	return (DW_DLE_NONE);
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos int
    144  1.1  christos _dwarf_generate_sections(Dwarf_P_Debug dbg, Dwarf_Error *error)
    145  1.1  christos {
    146  1.1  christos 	int ret;
    147  1.1  christos 
    148  1.1  christos 	/* Produce .debug_info section. */
    149  1.1  christos 	if ((ret = _dwarf_info_gen(dbg, error)) != DW_DLE_NONE)
    150  1.1  christos 		return (ret);
    151  1.1  christos 
    152  1.1  christos 	/* Produce .debug_abbrev section. */
    153  1.1  christos 	if ((ret = _dwarf_abbrev_gen(dbg, error)) != DW_DLE_NONE)
    154  1.1  christos 		return (ret);
    155  1.1  christos 
    156  1.1  christos 	/* Produce .debug_line section. */
    157  1.1  christos 	if ((ret = _dwarf_lineno_gen(dbg, error)) != DW_DLE_NONE)
    158  1.1  christos 		return (ret);
    159  1.1  christos 
    160  1.1  christos 	/* Produce .debug_frame section. */
    161  1.1  christos 	if ((ret = _dwarf_frame_gen(dbg, error)) != DW_DLE_NONE)
    162  1.1  christos 		return (ret);
    163  1.1  christos 
    164  1.1  christos 	/* Produce .debug_aranges section. */
    165  1.1  christos 	if ((ret = _dwarf_arange_gen(dbg, error)) != DW_DLE_NONE)
    166  1.1  christos 		return (ret);
    167  1.1  christos 
    168  1.1  christos 	/* Produce .debug_macinfo section. */
    169  1.1  christos 	if ((ret = _dwarf_macinfo_gen(dbg, error)) != DW_DLE_NONE)
    170  1.1  christos 		return (ret);
    171  1.1  christos 
    172  1.1  christos 	/* Produce .debug_pubnames section. */
    173  1.1  christos 	if ((ret = _dwarf_nametbl_gen(dbg, ".debug_pubnames", dbg->dbgp_pubs,
    174  1.1  christos 	    error)) != DW_DLE_NONE)
    175  1.1  christos 		return (ret);
    176  1.1  christos 
    177  1.1  christos 	/* Produce .debug_weaknames section. */
    178  1.1  christos 	if ((ret = _dwarf_nametbl_gen(dbg, ".debug_weaknames", dbg->dbgp_weaks,
    179  1.1  christos 	    error)) != DW_DLE_NONE)
    180  1.1  christos 		return (ret);
    181  1.1  christos 
    182  1.1  christos 	/* Produce .debug_funcnames section. */
    183  1.1  christos 	if ((ret = _dwarf_nametbl_gen(dbg, ".debug_funcnames", dbg->dbgp_funcs,
    184  1.1  christos 	    error)) != DW_DLE_NONE)
    185  1.1  christos 		return (ret);
    186  1.1  christos 
    187  1.1  christos 	/* Produce .debug_typenames section. */
    188  1.1  christos 	if ((ret = _dwarf_nametbl_gen(dbg, ".debug_typenames", dbg->dbgp_types,
    189  1.1  christos 	    error)) != DW_DLE_NONE)
    190  1.1  christos 		return (ret);
    191  1.1  christos 
    192  1.1  christos 	/* Produce .debug_varnames section. */
    193  1.1  christos 	if ((ret = _dwarf_nametbl_gen(dbg, ".debug_varnames", dbg->dbgp_vars,
    194  1.1  christos 	    error)) != DW_DLE_NONE)
    195  1.1  christos 		return (ret);
    196  1.1  christos 
    197  1.1  christos 	/* Produce .debug_str section. */
    198  1.1  christos 	if ((ret = _dwarf_strtab_gen(dbg, error)) != DW_DLE_NONE)
    199  1.1  christos 		return (ret);
    200  1.1  christos 
    201  1.1  christos 	/* Finally, update and generate all relocation sections. */
    202  1.1  christos 	if ((ret = _dwarf_reloc_gen(dbg, error)) != DW_DLE_NONE)
    203  1.1  christos 		return (ret);
    204  1.1  christos 
    205  1.1  christos 	/* Set section/relocation iterator to the first element. */
    206  1.1  christos 	dbg->dbgp_secpos = STAILQ_FIRST(&dbg->dbgp_seclist);
    207  1.1  christos 	dbg->dbgp_drspos = STAILQ_FIRST(&dbg->dbgp_drslist);
    208  1.1  christos 
    209  1.1  christos 	return (DW_DLE_NONE);
    210  1.1  christos }
    211  1.1  christos 
    212  1.1  christos Dwarf_Section *
    213  1.1  christos _dwarf_find_section(Dwarf_Debug dbg, const char *name)
    214  1.1  christos {
    215  1.1  christos 	Dwarf_Section *ds;
    216  1.1  christos 	Dwarf_Half i;
    217  1.1  christos 
    218  1.3  christos 	assert(dbg != NULL && name != NULL);
    219  1.1  christos 
    220  1.1  christos 	for (i = 0; i < dbg->dbg_seccnt; i++) {
    221  1.1  christos 		ds = &dbg->dbg_section[i];
    222  1.1  christos 		if (ds->ds_name != NULL && !strcmp(ds->ds_name, name))
    223  1.1  christos 			return (ds);
    224  1.1  christos 	}
    225  1.1  christos 
    226  1.1  christos 	return (NULL);
    227  1.1  christos }
    228  1.1  christos 
    229  1.3  christos Dwarf_Section *
    230  1.3  christos _dwarf_find_next_types_section(Dwarf_Debug dbg, Dwarf_Section *ds)
    231  1.3  christos {
    232  1.3  christos 
    233  1.3  christos 	assert(dbg != NULL);
    234  1.3  christos 
    235  1.3  christos 	if (ds == NULL)
    236  1.3  christos 		return (_dwarf_find_section(dbg, ".debug_types"));
    237  1.3  christos 
    238  1.3  christos 	assert(ds->ds_name != NULL);
    239  1.3  christos 
    240  1.3  christos 	do {
    241  1.3  christos 		ds++;
    242  1.3  christos 		if (ds->ds_name != NULL &&
    243  1.3  christos 		    !strcmp(ds->ds_name, ".debug_types"))
    244  1.3  christos 			return (ds);
    245  1.3  christos 	} while (ds->ds_name != NULL);
    246  1.3  christos 
    247  1.3  christos 	return (NULL);
    248  1.3  christos }
    249  1.3  christos 
    250  1.1  christos Dwarf_P_Section
    251  1.1  christos _dwarf_pro_find_section(Dwarf_P_Debug dbg, const char *name)
    252  1.1  christos {
    253  1.1  christos 	Dwarf_P_Section ds;
    254  1.1  christos 
    255  1.1  christos 	assert(dbg != NULL && name != NULL);
    256  1.1  christos 
    257  1.1  christos 	STAILQ_FOREACH(ds, &dbg->dbgp_seclist, ds_next) {
    258  1.1  christos 		if (ds->ds_name != NULL && !strcmp(ds->ds_name ,name))
    259  1.1  christos 			return (ds);
    260  1.1  christos 	}
    261  1.1  christos 
    262  1.1  christos 	return (NULL);
    263  1.1  christos }
    264  1.1  christos 
    265  1.1  christos void
    266  1.1  christos _dwarf_section_cleanup(Dwarf_P_Debug dbg)
    267  1.1  christos {
    268  1.1  christos 	Dwarf_P_Section ds, tds;
    269  1.1  christos 
    270  1.1  christos 	assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
    271  1.1  christos 
    272  1.1  christos 	STAILQ_FOREACH_SAFE(ds, &dbg->dbgp_seclist, ds_next, tds) {
    273  1.1  christos 		STAILQ_REMOVE(&dbg->dbgp_seclist, ds, _Dwarf_P_Section,
    274  1.1  christos 		    ds_next);
    275  1.1  christos 		if (ds->ds_name)
    276  1.1  christos 			free(ds->ds_name);
    277  1.1  christos 		if (ds->ds_data)
    278  1.1  christos 			free(ds->ds_data);
    279  1.1  christos 		free(ds);
    280  1.1  christos 	}
    281  1.1  christos 	dbg->dbgp_seccnt = 0;
    282  1.1  christos 	dbg->dbgp_secpos = 0;
    283  1.1  christos }
    284