3 Copyright (C) 2000,2001,2002,2005,2006 Silicon Graphics, Inc. All Rights Reserved.
4 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
5 Portions Copyright 2008-2010 Arxan Technologies, Inc. All rights reserved.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of version 2.1 of the GNU Lesser General Public License
9 as published by the Free Software Foundation.
11 This program is distributed in the hope that it would be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 Further, this software is distributed without any warranty that it is
16 free of the rightful claim of any third person regarding infringement
17 or the like. Any license provided herein, whether implied or
18 otherwise, applies only to this software file. Patent licenses, if
19 any, provided herein do not apply to combinations of this program with
20 other software, or any other product whatsoever.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this program; if not, write the Free Software
24 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
27 Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
28 Mountain View, CA 94043, or:
32 For further information regarding this notice, see:
34 http://oss.sgi.com/projects/GenInfo/NoticeExplan
40 #include "dwarf_incl.h"
41 #include "dwarf_elf_access.h"
49 #ifdef HAVE_LIBELF_LIBELF_H
50 #include <libelf/libelf.h>
56 #include <sys/types.h>
60 #define DWARF_DBG_ERROR(dbg,errval,retval) \
61 _dwarf_error(dbg, error, errval); return(retval);
67 dwarf_elf_init_file_ownership(dwarf_elf_handle elf_file_pointer
,
68 int libdwarf_owns_elf
,
69 Dwarf_Unsigned access
,
70 Dwarf_Handler errhand
,
72 Dwarf_Debug
* ret_dbg
,
77 The basic dwarf initializer function for consumers using
79 Return a libdwarf error code on error, return DW_DLV_OK
84 Dwarf_Unsigned access
,
85 Dwarf_Handler errhand
,
86 Dwarf_Ptr errarg
, Dwarf_Debug
* ret_dbg
, Dwarf_Error
* error
)
88 struct stat fstat_buf
;
89 dwarf_elf_handle elf_file_pointer
= 0;
90 /* ELF_C_READ is a portable value */
91 Elf_Cmd what_kind_of_elf_read
= ELF_C_READ
;
94 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
96 if (fstat(fd
, &fstat_buf
) != 0) {
97 DWARF_DBG_ERROR(NULL
, DW_DLE_FSTAT_ERROR
, DW_DLV_ERROR
);
99 if (!S_ISREG(fstat_buf
.st_mode
)) {
100 DWARF_DBG_ERROR(NULL
, DW_DLE_FSTAT_MODE_ERROR
, DW_DLV_ERROR
);
103 if (access
!= DW_DLC_READ
) {
104 DWARF_DBG_ERROR(NULL
, DW_DLE_INIT_ACCESS_WRONG
, DW_DLV_ERROR
);
107 elf_version(EV_CURRENT
);
108 /* changed to mmap request per bug 281217. 6/95 */
109 #ifdef HAVE_ELF_C_READ_MMAP
110 /* ELF_C_READ_MMAP is an SGI IRIX specific enum value from IRIX
111 libelf.h meaning read but use mmap */
112 what_kind_of_elf_read
= ELF_C_READ_MMAP
;
113 #endif /* !HAVE_ELF_C_READ_MMAP */
115 elf_file_pointer
= elf_begin(fd
, what_kind_of_elf_read
, 0);
116 if (elf_file_pointer
== NULL
) {
117 DWARF_DBG_ERROR(NULL
, DW_DLE_ELF_BEGIN_ERROR
, DW_DLV_ERROR
);
120 return dwarf_elf_init_file_ownership(elf_file_pointer
,
130 An alternate dwarf setup call for consumers using
132 When the caller has opened libelf already, so the
133 caller must free libelf.
136 dwarf_elf_init(dwarf_elf_handle elf_file_pointer
,
137 Dwarf_Unsigned access
,
138 Dwarf_Handler errhand
,
140 Dwarf_Debug
* ret_dbg
, Dwarf_Error
* error
)
142 return dwarf_elf_init_file_ownership(elf_file_pointer
,
153 Initialize the ELF object access for libdwarf.
156 dwarf_elf_init_file_ownership(dwarf_elf_handle elf_file_pointer
,
157 int libdwarf_owns_elf
,
158 Dwarf_Unsigned access
,
159 Dwarf_Handler errhand
,
161 Dwarf_Debug
* ret_dbg
,
164 /* ELF is no longer tied to libdwarf. */
165 Dwarf_Obj_Access_Interface
*binary_interface
= 0;
169 if (access
!= DW_DLC_READ
) {
170 DWARF_DBG_ERROR(NULL
, DW_DLE_INIT_ACCESS_WRONG
, DW_DLV_ERROR
);
173 /* This allocates and fills in *binary_interface. */
174 res
= dwarf_elf_object_access_init(
179 if(res
!= DW_DLV_OK
){
180 DWARF_DBG_ERROR(NULL
, err
, DW_DLV_ERROR
);
183 /* This mallocs space and returns pointer thru ret_dbg,
184 saving the binary interface in 'ret-dbg' */
185 res
= dwarf_object_init(binary_interface
, errhand
, errarg
,
187 if(res
!= DW_DLV_OK
){
188 dwarf_elf_object_access_finish(binary_interface
);
195 Frees all memory that was not previously freed
197 Aside from certain categories.
199 This is only applicable when dwarf_init() or dwarf_elf_init()
200 was used to init 'dbg'.
203 dwarf_finish(Dwarf_Debug dbg
, Dwarf_Error
* error
)
205 dwarf_elf_object_access_finish(dbg
->de_obj_file
);
207 return dwarf_object_finish(dbg
, error
);