Home | History | Annotate | Line # | Download | only in libdwarf
      1  1.5  christos /*	$NetBSD: dwarf_init.c,v 1.5 2024/03/03 17:37:31 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) 2009 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.5  christos __RCSID("$NetBSD: dwarf_init.c,v 1.5 2024/03/03 17:37:31 christos Exp $");
     33  1.4    jkoshy ELFTC_VCSID("Id: dwarf_init.c 2073 2011-10-27 03:30:47Z jkoshy");
     34  1.1  christos 
     35  1.1  christos int
     36  1.1  christos dwarf_elf_init(Elf *elf, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
     37  1.1  christos     Dwarf_Debug *ret_dbg, Dwarf_Error *error)
     38  1.1  christos {
     39  1.1  christos 	Dwarf_Debug dbg;
     40  1.1  christos 	int ret;
     41  1.1  christos 
     42  1.1  christos 	if (elf == NULL || ret_dbg == NULL) {
     43  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
     44  1.1  christos 		return (DW_DLV_ERROR);
     45  1.1  christos 	}
     46  1.1  christos 
     47  1.1  christos 	if (mode != DW_DLC_READ) {
     48  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
     49  1.1  christos 		return (DW_DLV_ERROR);
     50  1.1  christos 	}
     51  1.1  christos 
     52  1.1  christos 	if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
     53  1.1  christos 		return (DW_DLV_ERROR);
     54  1.1  christos 
     55  1.1  christos 	if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
     56  1.1  christos 		free(dbg);
     57  1.1  christos 		return (DW_DLV_ERROR);
     58  1.1  christos 	}
     59  1.1  christos 
     60  1.1  christos 	if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
     61  1.1  christos 	    DW_DLE_NONE) {
     62  1.1  christos 		_dwarf_elf_deinit(dbg);
     63  1.1  christos 		free(dbg);
     64  1.1  christos 		if (ret == DW_DLE_DEBUG_INFO_NULL)
     65  1.1  christos 			return (DW_DLV_NO_ENTRY);
     66  1.1  christos 		else
     67  1.1  christos 			return (DW_DLV_ERROR);
     68  1.1  christos 	}
     69  1.1  christos 
     70  1.1  christos 	*ret_dbg = dbg;
     71  1.1  christos 
     72  1.1  christos 	return (DW_DLV_OK);
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos int
     76  1.1  christos dwarf_get_elf(Dwarf_Debug dbg, Elf **elf, Dwarf_Error *error)
     77  1.1  christos {
     78  1.1  christos 	Dwarf_Elf_Object *e;
     79  1.1  christos 
     80  1.1  christos 	if (dbg == NULL || elf == NULL) {
     81  1.1  christos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
     82  1.1  christos 		return (DW_DLV_ERROR);
     83  1.1  christos 	}
     84  1.1  christos 
     85  1.1  christos 	e = dbg->dbg_iface->object;
     86  1.1  christos 	*elf = e->eo_elf;
     87  1.1  christos 
     88  1.1  christos 	return (DW_DLV_OK);
     89  1.1  christos }
     90  1.1  christos 
     91  1.1  christos int
     92  1.1  christos dwarf_init(int fd, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
     93  1.1  christos     Dwarf_Debug *ret_dbg, Dwarf_Error *error)
     94  1.1  christos {
     95  1.1  christos 	Dwarf_Debug dbg;
     96  1.1  christos 	Elf *elf;
     97  1.1  christos 	int ret;
     98  1.1  christos 
     99  1.1  christos 	if (fd < 0 || ret_dbg == NULL) {
    100  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
    101  1.1  christos 		return (DW_DLV_ERROR);
    102  1.1  christos 	}
    103  1.1  christos 
    104  1.1  christos 	if (mode != DW_DLC_READ) {
    105  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
    106  1.1  christos 		return (DW_DLV_ERROR);
    107  1.1  christos 	}
    108  1.1  christos 
    109  1.1  christos 	if (elf_version(EV_CURRENT) == EV_NONE) {
    110  1.1  christos 		DWARF_SET_ELF_ERROR(NULL, error);
    111  1.1  christos 		return (DW_DLV_ERROR);
    112  1.1  christos 	}
    113  1.1  christos 
    114  1.1  christos 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
    115  1.1  christos 		DWARF_SET_ELF_ERROR(NULL, error);
    116  1.1  christos 		return (DW_DLV_ERROR);
    117  1.1  christos 	}
    118  1.1  christos 
    119  1.1  christos 	if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
    120  1.1  christos 		return (DW_DLV_ERROR);
    121  1.1  christos 
    122  1.1  christos 	if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
    123  1.1  christos 		free(dbg);
    124  1.1  christos 		return (DW_DLV_ERROR);
    125  1.1  christos 	}
    126  1.1  christos 
    127  1.1  christos 	if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
    128  1.1  christos 	    DW_DLE_NONE) {
    129  1.1  christos 		_dwarf_elf_deinit(dbg);
    130  1.1  christos 		free(dbg);
    131  1.1  christos 		if (ret == DW_DLE_DEBUG_INFO_NULL)
    132  1.1  christos 			return (DW_DLV_NO_ENTRY);
    133  1.1  christos 		else
    134  1.1  christos 			return (DW_DLV_ERROR);
    135  1.1  christos 	}
    136  1.1  christos 
    137  1.1  christos 	*ret_dbg = dbg;
    138  1.1  christos 
    139  1.1  christos 	return (DW_DLV_OK);
    140  1.1  christos }
    141  1.1  christos 
    142  1.1  christos int
    143  1.1  christos dwarf_object_init(Dwarf_Obj_Access_Interface *iface, Dwarf_Handler errhand,
    144  1.1  christos     Dwarf_Ptr errarg, Dwarf_Debug *ret_dbg, Dwarf_Error *error)
    145  1.1  christos {
    146  1.1  christos 	Dwarf_Debug dbg;
    147  1.1  christos 
    148  1.1  christos 	if (iface == NULL || ret_dbg == NULL) {
    149  1.1  christos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
    150  1.1  christos 		return (DW_DLV_ERROR);
    151  1.1  christos 	}
    152  1.1  christos 
    153  1.1  christos 	if (_dwarf_alloc(&dbg, DW_DLC_READ, error) != DW_DLE_NONE)
    154  1.1  christos 		return (DW_DLV_ERROR);
    155  1.1  christos 
    156  1.1  christos 	dbg->dbg_iface = iface;
    157  1.1  christos 
    158  1.1  christos 	if (_dwarf_init(dbg, 0, errhand, errarg, error) != DW_DLE_NONE) {
    159  1.1  christos 		free(dbg);
    160  1.1  christos 		return (DW_DLV_ERROR);
    161  1.1  christos 	}
    162  1.1  christos 
    163  1.1  christos 	*ret_dbg = dbg;
    164  1.1  christos 
    165  1.1  christos 	return (DW_DLV_OK);
    166  1.1  christos }
    167