1*5ac3bc71Schristos /* $NetBSD: dwarf_errmsg.c,v 1.5 2024/03/03 17:37:30 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
49dd9d0cfSchristos * Copyright (c) 2007 John Birrell (jb@freebsd.org)
59dd9d0cfSchristos * All rights reserved.
69dd9d0cfSchristos *
79dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos * modification, are permitted provided that the following conditions
99dd9d0cfSchristos * are met:
109dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos *
169dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos * SUCH DAMAGE.
279dd9d0cfSchristos */
289dd9d0cfSchristos
299dd9d0cfSchristos #include "_libdwarf.h"
309dd9d0cfSchristos
31*5ac3bc71Schristos __RCSID("$NetBSD: dwarf_errmsg.c,v 1.5 2024/03/03 17:37:30 christos Exp $");
3242bd3019Schristos ELFTC_VCSID("Id: dwarf_errmsg.c 2975 2014-01-21 20:08:04Z kaiwang27");
339dd9d0cfSchristos
349dd9d0cfSchristos static const char *_libdwarf_errors[] = {
359dd9d0cfSchristos #define DEFINE_ERROR(N,S) [DW_DLE_##N] = S
369dd9d0cfSchristos DEFINE_ERROR(NONE, "No Error"),
379dd9d0cfSchristos DEFINE_ERROR(ERROR, "An error"),
389dd9d0cfSchristos DEFINE_ERROR(NO_ENTRY, "No entry found"),
399dd9d0cfSchristos DEFINE_ERROR(ARGUMENT, "Invalid argument"),
409dd9d0cfSchristos DEFINE_ERROR(DEBUG_INFO_NULL, "Debug info NULL"),
419dd9d0cfSchristos DEFINE_ERROR(MEMORY, "Insufficient memory"),
429dd9d0cfSchristos DEFINE_ERROR(ELF, "ELF error"),
439dd9d0cfSchristos DEFINE_ERROR(CU_LENGTH_ERROR, "Invalid compilation unit data"),
449dd9d0cfSchristos DEFINE_ERROR(VERSION_STAMP_ERROR, "Unsupported version"),
459dd9d0cfSchristos DEFINE_ERROR(DEBUG_ABBREV_NULL, "Abbrev not found"),
469dd9d0cfSchristos DEFINE_ERROR(DIE_NO_CU_CONTEXT, "No current compilation unit"),
479dd9d0cfSchristos DEFINE_ERROR(LOC_EXPR_BAD, "Invalid location expression"),
489dd9d0cfSchristos DEFINE_ERROR(EXPR_LENGTH_BAD, "Invalid DWARF expression length"),
499dd9d0cfSchristos DEFINE_ERROR(DEBUG_LOC_SECTION_SHORT, "Loclist section too short"),
509dd9d0cfSchristos DEFINE_ERROR(ATTR_FORM_BAD, "Invalid attribute form"),
519dd9d0cfSchristos DEFINE_ERROR(DEBUG_LINE_LENGTH_BAD, "Line info section too short"),
529dd9d0cfSchristos DEFINE_ERROR(LINE_FILE_NUM_BAD, "Invalid file number."),
539dd9d0cfSchristos DEFINE_ERROR(DIR_INDEX_BAD, "Invalid dir index."),
549dd9d0cfSchristos DEFINE_ERROR(DEBUG_FRAME_LENGTH_BAD, "Frame section too short"),
559dd9d0cfSchristos DEFINE_ERROR(NO_CIE_FOR_FDE, "FDE without corresponding CIE"),
569dd9d0cfSchristos DEFINE_ERROR(FRAME_AUGMENTATION_UNKNOWN, "Unknown CIE augmentation"),
579dd9d0cfSchristos DEFINE_ERROR(FRAME_INSTR_EXEC_ERROR, "Frame instruction exec error"),
589dd9d0cfSchristos DEFINE_ERROR(FRAME_VERSION_BAD, "Unsupported frame section version"),
599dd9d0cfSchristos DEFINE_ERROR(FRAME_TABLE_COL_BAD, "Invalid table column value"),
609dd9d0cfSchristos DEFINE_ERROR(DF_REG_NUM_TOO_HIGH, "Register number too large"),
619dd9d0cfSchristos DEFINE_ERROR(PC_NOT_IN_FDE_RANGE, "PC requested not in the FDE range"),
629dd9d0cfSchristos DEFINE_ERROR(ARANGE_OFFSET_BAD, "Invalid address range offset"),
639dd9d0cfSchristos DEFINE_ERROR(DEBUG_MACRO_INCONSISTENT, "Invalid macinfo data"),
649dd9d0cfSchristos DEFINE_ERROR(ELF_SECT_ERR, "Application callback failed"),
659dd9d0cfSchristos DEFINE_ERROR(NUM, "Unknown DWARF error")
669dd9d0cfSchristos #undef DEFINE_ERROR
679dd9d0cfSchristos };
689dd9d0cfSchristos
699dd9d0cfSchristos const char *
dwarf_errmsg_(Dwarf_Error * error)709dd9d0cfSchristos dwarf_errmsg_(Dwarf_Error *error)
719dd9d0cfSchristos {
729dd9d0cfSchristos const char *p;
739dd9d0cfSchristos
749dd9d0cfSchristos if (error == NULL)
759dd9d0cfSchristos return NULL;
769dd9d0cfSchristos
779dd9d0cfSchristos if (error->err_error < 0 || error->err_error >= DW_DLE_NUM)
789dd9d0cfSchristos return _libdwarf_errors[DW_DLE_NUM];
799dd9d0cfSchristos else if (error->err_error == DW_DLE_NONE)
809dd9d0cfSchristos return _libdwarf_errors[DW_DLE_NONE];
819dd9d0cfSchristos else
829dd9d0cfSchristos p = _libdwarf_errors[error->err_error];
839dd9d0cfSchristos
849dd9d0cfSchristos if (error->err_error == DW_DLE_ELF)
859dd9d0cfSchristos snprintf(error->err_msg, sizeof(error->err_msg),
869dd9d0cfSchristos "ELF error : %s [%s(%d)]", elf_errmsg(error->err_elferror),
879dd9d0cfSchristos error->err_func, error->err_line);
889dd9d0cfSchristos else
899dd9d0cfSchristos snprintf(error->err_msg, sizeof(error->err_msg),
909dd9d0cfSchristos "%s [%s(%d)]", p, error->err_func, error->err_line);
919dd9d0cfSchristos
929dd9d0cfSchristos return (const char *) error->err_msg;
939dd9d0cfSchristos }
94