1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

root/trunk/libdwarf/dwarf_init.c @ 484

Revision 484, 3.1 KB (checked in by kaiwang27, 5 years ago)

Merged libdwarf and addr2line back to trunk.

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1/*-
2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <string.h>
28#include "_libdwarf.h"
29
30int
31dwarf_elf_init(Elf *elf, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
32    Dwarf_Debug *ret_dbg, Dwarf_Error *error)
33{
34        Dwarf_Debug dbg;
35        int ret;
36
37        _libdwarf.errhand = errhand;
38        _libdwarf.errarg = errarg;
39
40        if (elf == NULL || ret_dbg == NULL) {
41                DWARF_SET_ERROR(error, DWARF_E_ARGUMENT);
42                return (DW_DLV_ERROR);
43        }
44
45        if ((dbg = calloc(sizeof(struct _Dwarf_Debug), 1)) == NULL) {
46                DWARF_SET_ERROR(error, DWARF_E_MEMORY);
47                return (DW_DLV_ERROR);
48        }
49
50        dbg->dbg_elf            = elf;
51        dbg->dbg_elf_close      = 0;
52        dbg->dbg_mode           = mode;
53        STAILQ_INIT(&dbg->dbg_cu);
54
55        *ret_dbg = dbg;
56
57        /* Read the ELF sections. */
58        ret = elf_read(dbg, error);
59
60        if (ret != DWARF_E_NONE)
61                return (DW_DLV_ERROR);
62
63        return (DW_DLV_OK);
64}
65
66int
67dwarf_init(int fd, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
68    Dwarf_Debug *ret_dbg, Dwarf_Error *error)
69{
70        Dwarf_Error lerror;
71        Elf *elf;
72        Elf_Cmd c;
73        int ret;
74
75        _libdwarf.errhand = errhand;
76        _libdwarf.errarg = errarg;
77
78        if (fd < 0 || ret_dbg == NULL) {
79                DWARF_SET_ERROR(error, DWARF_E_ARGUMENT);
80                return (DW_DLV_ERROR);
81        }
82
83        /* Translate the DWARF mode to ELF mode. */
84        switch (mode) {
85        default:
86        case DW_DLC_READ:
87                c = ELF_C_READ;
88                break;
89        }
90
91        if (elf_version(EV_CURRENT) == EV_NONE) {
92                DWARF_SET_ELF_ERROR(error, elf_errno());
93                return (DW_DLV_ERROR);
94        }
95
96        if ((elf = elf_begin(fd, c, NULL)) == NULL) {
97                DWARF_SET_ELF_ERROR(error, elf_errno());
98                return (DW_DLV_ERROR);
99        }
100
101        ret = dwarf_elf_init(elf, mode, errhand, errarg, ret_dbg, error);
102
103        if (*ret_dbg != NULL)
104                /* Remember to close the ELF file. */
105                (*ret_dbg)->dbg_elf_close = 1;
106
107        if (ret != DWARF_E_NONE) {
108                if (*ret_dbg != NULL) {
109                        dwarf_finish(*ret_dbg, &lerror);
110                        *ret_dbg = NULL;
111                } else
112                        elf_end(elf);
113
114                return (DW_DLV_ERROR);
115        }
116
117        return (DW_DLV_OK);
118}
Note: See TracBrowser for help on using the browser.