149d3bc9Richard Lowe/*
249d3bc9Richard Lowe
307dc194Richard Lowe  Copyright (C) 2000,2002,2004 Silicon Graphics, Inc.  All Rights Reserved.
44d9fdb4Robert Mustacchi  Portions Copyright 2011 David Anderson. All Rights Reserved.
549d3bc9Richard Lowe
649d3bc9Richard Lowe  This program is free software; you can redistribute it and/or modify it
74d9fdb4Robert Mustacchi  under the terms of version 2.1 of the GNU Lesser General Public License
849d3bc9Richard Lowe  as published by the Free Software Foundation.
949d3bc9Richard Lowe
1049d3bc9Richard Lowe  This program is distributed in the hope that it would be useful, but
1149d3bc9Richard Lowe  WITHOUT ANY WARRANTY; without even the implied warranty of
1349d3bc9Richard Lowe
1449d3bc9Richard Lowe  Further, this software is distributed without any warranty that it is
154d9fdb4Robert Mustacchi  free of the rightful claim of any third person regarding infringement
164d9fdb4Robert Mustacchi  or the like.  Any license provided herein, whether implied or
1749d3bc9Richard Lowe  otherwise, applies only to this software file.  Patent licenses, if
184d9fdb4Robert Mustacchi  any, provided herein do not apply to combinations of this program with
194d9fdb4Robert Mustacchi  other software, or any other product whatsoever.
2049d3bc9Richard Lowe
214d9fdb4Robert Mustacchi  You should have received a copy of the GNU Lesser General Public
224d9fdb4Robert Mustacchi  License along with this program; if not, write the Free Software
2307dc194Richard Lowe  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
2449d3bc9Richard Lowe  USA.
2549d3bc9Richard Lowe
2649d3bc9Richard Lowe*/
2749d3bc9Richard Lowe
2849d3bc9Richard Lowe#include "config.h"
2949d3bc9Richard Lowe#include "libdwarfdefs.h"
3049d3bc9Richard Lowe
3149d3bc9Richard Lowe#include <stdio.h>
3249d3bc9Richard Lowe#include <sys/stat.h>
3349d3bc9Richard Lowe#include <sys/types.h>
344d9fdb4Robert Mustacchi#ifdef HAVE_STDLIB_H
354d9fdb4Robert Mustacchi#include <stdlib.h> /* for exit(), C89 malloc */
364d9fdb4Robert Mustacchi#endif /* HAVE_STDLIB_H */
3749d3bc9Richard Lowe#include "pro_incl.h"
384d9fdb4Robert Mustacchi#include <stddef.h>
394d9fdb4Robert Mustacchi#include "dwarf.h"
404d9fdb4Robert Mustacchi#include "libdwarf.h"
414d9fdb4Robert Mustacchi#include "pro_opaque.h"
424d9fdb4Robert Mustacchi#include "pro_error.h"
434d9fdb4Robert Mustacchi#include "pro_alloc.h"
444d9fdb4Robert Mustacchi
4549d3bc9Richard Lowe
4649d3bc9Richard Loweextern char *_dwarf_errmsgs[];
4749d3bc9Richard Lowe
484d9fdb4Robert Mustacchi/*
494d9fdb4Robert Mustacchi    This function performs error handling as described in the
5049d3bc9Richard Lowe    libdwarf consumer document section 3.  Dbg is the Dwarf_P_debug
5149d3bc9Richard Lowe    structure being processed.  Error is a pointer to the pointer
5249d3bc9Richard Lowe    to the error descriptor that will be returned.  Errval is an
5349d3bc9Richard Lowe    error code listed in dwarf_error.h.
544d9fdb4Robert Mustacchi
554d9fdb4Robert Mustacchi    The error number may be retrieved from the Dwarf_Error
564d9fdb4Robert Mustacchi    by calling dwarf_errno().
574d9fdb4Robert Mustacchi    The error string implied by the error number may be retrieved
584d9fdb4Robert Mustacchi    from the Dwarf_Error by calling dwarf_errmsg().
5949d3bc9Richard Lowe*/
6049d3bc9Richard Lowevoid
6149d3bc9Richard Lowe_dwarf_p_error(Dwarf_P_Debug dbg,
624d9fdb4Robert Mustacchi    Dwarf_Error * error, Dwarf_Unsigned errval)
6349d3bc9Richard Lowe{
6449d3bc9Richard Lowe    Dwarf_Error errptr;
6549d3bc9Richard Lowe
664d9fdb4Robert Mustacchi    if (errval > DW_DLE_LAST) {
674d9fdb4Robert Mustacchi        /*  We do not expect to ever see such an error number,
684d9fdb4Robert Mustacchi            DW_DLE_LO_USER is not used. */
694d9fdb4Robert Mustacchi        fprintf(stderr,"ERROR VALUE: %lu - %s\n",
704d9fdb4Robert Mustacchi            (unsigned long) errval, "this error value is unknown to libdwarf.");
714d9fdb4Robert Mustacchi    }
724d9fdb4Robert Mustacchi    /*  Allow NULL dbg on entry, since sometimes that can happen and we
734d9fdb4Robert Mustacchi        want to report the upper-level error, not this one. */
7449d3bc9Richard Lowe    if (error != NULL) {
7507dc194Richard Lowe        errptr = (Dwarf_Error)
7607dc194Richard Lowe            _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_Error_s));
7707dc194Richard Lowe        if (errptr == NULL) {
7807dc194Richard Lowe            fprintf(stderr,
794d9fdb4Robert Mustacchi                "Could not allocate Dwarf_Error structure\n");
8007dc194Richard Lowe            abort();
8107dc194Richard Lowe        }
824d9fdb4Robert Mustacchi        errptr->er_errval = (Dwarf_Signed) errval;
8307dc194Richard Lowe        *error = errptr;
8407dc194Richard Lowe        return;
8549d3bc9Richard Lowe    }
8649d3bc9Richard Lowe
8749d3bc9Richard Lowe    if (dbg != NULL && dbg->de_errhand != NULL) {
8807dc194Richard Lowe        errptr = (Dwarf_Error)
8907dc194Richard Lowe            _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_Error_s));
9007dc194Richard Lowe        if (errptr == NULL) {
9107dc194Richard Lowe            fprintf(stderr,
924d9fdb4Robert Mustacchi                "Could not allocate Dwarf_Error structure\n");
9307dc194Richard Lowe            abort();
9407dc194Richard Lowe        }
954d9fdb4Robert Mustacchi        errptr->er_errval = (Dwarf_Signed) errval;
9607dc194Richard Lowe        dbg->de_errhand(errptr, dbg->de_errarg);
9707dc194Richard Lowe        return;
9849d3bc9Richard Lowe    }
9949d3bc9Richard Lowe
10049d3bc9Richard Lowe    abort();
10149d3bc9Richard Lowe}