1*5ac3bc71Schristos /* $NetBSD: dwarf_init.c,v 1.5 2024/03/03 17:37:31 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
49dd9d0cfSchristos * Copyright (c) 2007 John Birrell (jb@freebsd.org)
59dd9d0cfSchristos * Copyright (c) 2009 Kai Wang
69dd9d0cfSchristos * All rights reserved.
79dd9d0cfSchristos *
89dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
99dd9d0cfSchristos * modification, are permitted provided that the following conditions
109dd9d0cfSchristos * are met:
119dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
129dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
139dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
149dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
159dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
169dd9d0cfSchristos *
179dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
189dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
219dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279dd9d0cfSchristos * SUCH DAMAGE.
289dd9d0cfSchristos */
299dd9d0cfSchristos
309dd9d0cfSchristos #include "_libdwarf.h"
319dd9d0cfSchristos
32*5ac3bc71Schristos __RCSID("$NetBSD: dwarf_init.c,v 1.5 2024/03/03 17:37:31 christos Exp $");
339dd9d0cfSchristos ELFTC_VCSID("Id: dwarf_init.c 2073 2011-10-27 03:30:47Z jkoshy");
349dd9d0cfSchristos
359dd9d0cfSchristos int
dwarf_elf_init(Elf * elf,int mode,Dwarf_Handler errhand,Dwarf_Ptr errarg,Dwarf_Debug * ret_dbg,Dwarf_Error * error)369dd9d0cfSchristos dwarf_elf_init(Elf *elf, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
379dd9d0cfSchristos Dwarf_Debug *ret_dbg, Dwarf_Error *error)
389dd9d0cfSchristos {
399dd9d0cfSchristos Dwarf_Debug dbg;
409dd9d0cfSchristos int ret;
419dd9d0cfSchristos
429dd9d0cfSchristos if (elf == NULL || ret_dbg == NULL) {
439dd9d0cfSchristos DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
449dd9d0cfSchristos return (DW_DLV_ERROR);
459dd9d0cfSchristos }
469dd9d0cfSchristos
479dd9d0cfSchristos if (mode != DW_DLC_READ) {
489dd9d0cfSchristos DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
499dd9d0cfSchristos return (DW_DLV_ERROR);
509dd9d0cfSchristos }
519dd9d0cfSchristos
529dd9d0cfSchristos if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
539dd9d0cfSchristos return (DW_DLV_ERROR);
549dd9d0cfSchristos
559dd9d0cfSchristos if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
569dd9d0cfSchristos free(dbg);
579dd9d0cfSchristos return (DW_DLV_ERROR);
589dd9d0cfSchristos }
599dd9d0cfSchristos
609dd9d0cfSchristos if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
619dd9d0cfSchristos DW_DLE_NONE) {
629dd9d0cfSchristos _dwarf_elf_deinit(dbg);
639dd9d0cfSchristos free(dbg);
649dd9d0cfSchristos if (ret == DW_DLE_DEBUG_INFO_NULL)
659dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
669dd9d0cfSchristos else
679dd9d0cfSchristos return (DW_DLV_ERROR);
689dd9d0cfSchristos }
699dd9d0cfSchristos
709dd9d0cfSchristos *ret_dbg = dbg;
719dd9d0cfSchristos
729dd9d0cfSchristos return (DW_DLV_OK);
739dd9d0cfSchristos }
749dd9d0cfSchristos
759dd9d0cfSchristos int
dwarf_get_elf(Dwarf_Debug dbg,Elf ** elf,Dwarf_Error * error)769dd9d0cfSchristos dwarf_get_elf(Dwarf_Debug dbg, Elf **elf, Dwarf_Error *error)
779dd9d0cfSchristos {
789dd9d0cfSchristos Dwarf_Elf_Object *e;
799dd9d0cfSchristos
809dd9d0cfSchristos if (dbg == NULL || elf == NULL) {
819dd9d0cfSchristos DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
829dd9d0cfSchristos return (DW_DLV_ERROR);
839dd9d0cfSchristos }
849dd9d0cfSchristos
859dd9d0cfSchristos e = dbg->dbg_iface->object;
869dd9d0cfSchristos *elf = e->eo_elf;
879dd9d0cfSchristos
889dd9d0cfSchristos return (DW_DLV_OK);
899dd9d0cfSchristos }
909dd9d0cfSchristos
919dd9d0cfSchristos int
dwarf_init(int fd,int mode,Dwarf_Handler errhand,Dwarf_Ptr errarg,Dwarf_Debug * ret_dbg,Dwarf_Error * error)929dd9d0cfSchristos dwarf_init(int fd, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
939dd9d0cfSchristos Dwarf_Debug *ret_dbg, Dwarf_Error *error)
949dd9d0cfSchristos {
959dd9d0cfSchristos Dwarf_Debug dbg;
969dd9d0cfSchristos Elf *elf;
979dd9d0cfSchristos int ret;
989dd9d0cfSchristos
999dd9d0cfSchristos if (fd < 0 || ret_dbg == NULL) {
1009dd9d0cfSchristos DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
1019dd9d0cfSchristos return (DW_DLV_ERROR);
1029dd9d0cfSchristos }
1039dd9d0cfSchristos
1049dd9d0cfSchristos if (mode != DW_DLC_READ) {
1059dd9d0cfSchristos DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
1069dd9d0cfSchristos return (DW_DLV_ERROR);
1079dd9d0cfSchristos }
1089dd9d0cfSchristos
1099dd9d0cfSchristos if (elf_version(EV_CURRENT) == EV_NONE) {
1109dd9d0cfSchristos DWARF_SET_ELF_ERROR(NULL, error);
1119dd9d0cfSchristos return (DW_DLV_ERROR);
1129dd9d0cfSchristos }
1139dd9d0cfSchristos
1149dd9d0cfSchristos if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1159dd9d0cfSchristos DWARF_SET_ELF_ERROR(NULL, error);
1169dd9d0cfSchristos return (DW_DLV_ERROR);
1179dd9d0cfSchristos }
1189dd9d0cfSchristos
1199dd9d0cfSchristos if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
1209dd9d0cfSchristos return (DW_DLV_ERROR);
1219dd9d0cfSchristos
1229dd9d0cfSchristos if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
1239dd9d0cfSchristos free(dbg);
1249dd9d0cfSchristos return (DW_DLV_ERROR);
1259dd9d0cfSchristos }
1269dd9d0cfSchristos
1279dd9d0cfSchristos if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
1289dd9d0cfSchristos DW_DLE_NONE) {
1299dd9d0cfSchristos _dwarf_elf_deinit(dbg);
1309dd9d0cfSchristos free(dbg);
1319dd9d0cfSchristos if (ret == DW_DLE_DEBUG_INFO_NULL)
1329dd9d0cfSchristos return (DW_DLV_NO_ENTRY);
1339dd9d0cfSchristos else
1349dd9d0cfSchristos return (DW_DLV_ERROR);
1359dd9d0cfSchristos }
1369dd9d0cfSchristos
1379dd9d0cfSchristos *ret_dbg = dbg;
1389dd9d0cfSchristos
1399dd9d0cfSchristos return (DW_DLV_OK);
1409dd9d0cfSchristos }
1419dd9d0cfSchristos
1429dd9d0cfSchristos int
dwarf_object_init(Dwarf_Obj_Access_Interface * iface,Dwarf_Handler errhand,Dwarf_Ptr errarg,Dwarf_Debug * ret_dbg,Dwarf_Error * error)1439dd9d0cfSchristos dwarf_object_init(Dwarf_Obj_Access_Interface *iface, Dwarf_Handler errhand,
1449dd9d0cfSchristos Dwarf_Ptr errarg, Dwarf_Debug *ret_dbg, Dwarf_Error *error)
1459dd9d0cfSchristos {
1469dd9d0cfSchristos Dwarf_Debug dbg;
1479dd9d0cfSchristos
1489dd9d0cfSchristos if (iface == NULL || ret_dbg == NULL) {
1499dd9d0cfSchristos DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
1509dd9d0cfSchristos return (DW_DLV_ERROR);
1519dd9d0cfSchristos }
1529dd9d0cfSchristos
1539dd9d0cfSchristos if (_dwarf_alloc(&dbg, DW_DLC_READ, error) != DW_DLE_NONE)
1549dd9d0cfSchristos return (DW_DLV_ERROR);
1559dd9d0cfSchristos
1569dd9d0cfSchristos dbg->dbg_iface = iface;
1579dd9d0cfSchristos
1589dd9d0cfSchristos if (_dwarf_init(dbg, 0, errhand, errarg, error) != DW_DLE_NONE) {
1599dd9d0cfSchristos free(dbg);
1609dd9d0cfSchristos return (DW_DLV_ERROR);
1619dd9d0cfSchristos }
1629dd9d0cfSchristos
1639dd9d0cfSchristos *ret_dbg = dbg;
1649dd9d0cfSchristos
1659dd9d0cfSchristos return (DW_DLV_OK);
1669dd9d0cfSchristos }
167