3 Copyright (C) 2000,2002,2003,2004,2005 Silicon Graphics, Inc. All Rights Reserved.
4 Portions Copyright (C) 2008-2010 Arxan Technologies, Inc. All Rights Reserved.
5 Portions Copyright (C) 2009-2010 David Anderson. 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
39 #include "dwarf_incl.h"
43 #include <sys/types.h>
47 #include "dwarf_incl.h"
48 #include "malloc_check.h"
50 #define DWARF_DBG_ERROR(dbg,errval,retval) \
51 _dwarf_error(dbg, error, errval); return(retval);
58 /* This static is copied to the dbg on dbg init
59 so that the static need not be referenced at
60 run time, preserving better locality of
62 Value is 0 means do the string check.
63 Value non-zero means do not do the check.
65 static Dwarf_Small _dwarf_assume_string_bad
;
66 static Dwarf_Small _dwarf_apply_relocs
= 1;
68 /* Call this after calling dwarf_init but before doing anything else.
69 * It applies to all objects, not just the current object.
72 dwarf_set_reloc_application(int apply
)
74 int oldval
= _dwarf_apply_relocs
;
75 _dwarf_apply_relocs
= apply
;
80 dwarf_set_stringcheck(int newval
)
82 int oldval
= _dwarf_assume_string_bad
;
84 _dwarf_assume_string_bad
= newval
;
88 /* Unifies the basic duplicate/empty testing and section
89 * data setting to one place. */
91 get_basic_section_data(Dwarf_Debug dbg
,
92 struct Dwarf_Section_s
*secdata
,
93 struct Dwarf_Obj_Access_Section_s
*doas
,
94 Dwarf_Half section_index
,
96 int duperr
, int emptyerr
)
98 if (secdata
->dss_index
!= 0) {
99 DWARF_DBG_ERROR(dbg
, duperr
, DW_DLV_ERROR
);
101 if (doas
->size
== 0) {
102 if (emptyerr
== 0 ) {
103 /* Allow empty section. */
106 /* Know no reason to allow section */
107 DWARF_DBG_ERROR(dbg
, emptyerr
, DW_DLV_ERROR
);
109 secdata
->dss_index
= section_index
;
110 secdata
->dss_size
= doas
->size
;
111 secdata
->dss_addr
= doas
->addr
;
112 secdata
->dss_link
= doas
->link
;
118 add_rela_data( struct Dwarf_Section_s
*secdata
,
119 struct Dwarf_Obj_Access_Section_s
*doas
,
120 Dwarf_Half section_index
)
122 secdata
->dss_reloc_index
= section_index
;
123 secdata
->dss_reloc_size
= doas
->size
;
124 secdata
->dss_reloc_addr
= doas
->addr
;
125 secdata
->dss_reloc_symtab
= doas
->link
;
126 secdata
->dss_reloc_link
= doas
->link
;
130 Given an Elf ptr, set up dbg with pointers
131 to all the Dwarf data sections.
132 Return NULL on error.
134 This function is also responsible for determining
135 whether the given object contains Dwarf information
136 or not. The test currently used is that it contains
137 either a .debug_info or a .debug_frame section. If
138 not, it returns DW_DLV_NO_ENTRY causing dwarf_init() also to
139 return DW_DLV_NO_ENTRY. Earlier, we had thought of using only
140 the presence/absence of .debug_info to test, but we
141 added .debug_frame since there could be stripped objects
142 that have only a .debug_frame section for exception
144 DW_DLV_NO_ENTRY or DW_DLV_OK or DW_DLV_ERROR
147 _dwarf_setup(Dwarf_Debug dbg
, Dwarf_Error
* error
)
149 const char *scn_name
= 0;
151 struct Dwarf_Obj_Access_Interface_s
* obj
= 0;
153 Dwarf_Endianness endianness
;
155 Dwarf_Unsigned section_size
= 0;
156 Dwarf_Unsigned section_count
= 0;
157 Dwarf_Half section_index
= 0;
158 Dwarf_Addr section_addr
= 0;
162 dbg
->de_assume_string_in_bounds
= _dwarf_assume_string_bad
;
164 dbg
->de_same_endian
= 1;
165 dbg
->de_copy_word
= memcpy
;
166 obj
= dbg
->de_obj_file
;
167 endianness
= obj
->methods
->get_byte_order(obj
->object
);
168 #ifdef WORDS_BIGENDIAN
169 dbg
->de_big_endian_object
= 1;
170 if (endianness
== DW_OBJECT_LSB
) {
171 dbg
->de_same_endian
= 0;
172 dbg
->de_big_endian_object
= 0;
173 dbg
->de_copy_word
= _dwarf_memcpy_swap_bytes
;
175 #else /* little endian */
176 dbg
->de_big_endian_object
= 0;
177 if (endianness
== DW_OBJECT_MSB
) {
178 dbg
->de_same_endian
= 0;
179 dbg
->de_big_endian_object
= 1;
180 dbg
->de_copy_word
= _dwarf_memcpy_swap_bytes
;
182 #endif /* !WORDS_BIGENDIAN */
185 /* The following de_length_size is Not Too Significant. Only used
186 one calculation, and an approximate one at that. */
187 dbg
->de_length_size
= obj
->methods
->get_length_size(obj
->object
);
188 dbg
->de_pointer_size
= obj
->methods
->get_pointer_size(obj
->object
);
190 section_count
= obj
->methods
->get_section_count(obj
->object
);
192 /* We can skip index 0 when considering ELF files, but not other
194 for (section_index
= 0; section_index
< section_count
;
197 struct Dwarf_Obj_Access_Section_s doas
;
198 int res
= DW_DLV_ERROR
;
201 res
= obj
->methods
->get_section_info(obj
->object
,
204 if(res
== DW_DLV_ERROR
){
205 DWARF_DBG_ERROR(dbg
, err
, DW_DLV_ERROR
);
208 section_addr
= doas
.addr
;
209 section_size
= doas
.size
;
210 scn_name
= doas
.name
;
212 if (strncmp(scn_name
, ".debug_", 7)
213 && strcmp(scn_name
, ".eh_frame")
214 && strcmp(scn_name
, ".symtab")
215 && strcmp(scn_name
, ".strtab")
216 && strncmp(scn_name
, ".rela.",6)) {
219 else if (strcmp(scn_name
, ".debug_info") == 0) {
220 res
= get_basic_section_data(dbg
,&dbg
->de_debug_info
, &doas
,
222 DW_DLE_DEBUG_INFO_DUPLICATE
,DW_DLE_DEBUG_INFO_NULL
);
223 if(res
!= DW_DLV_OK
) {
228 else if (strcmp(scn_name
, ".debug_abbrev") == 0) {
229 res
= get_basic_section_data(dbg
,&dbg
->de_debug_abbrev
, &doas
,
231 DW_DLE_DEBUG_ABBREV_DUPLICATE
,DW_DLE_DEBUG_ABBREV_NULL
);
232 if(res
!= DW_DLV_OK
) {
236 else if (strcmp(scn_name
, ".debug_aranges") == 0) {
237 res
= get_basic_section_data(dbg
,&dbg
->de_debug_aranges
, &doas
,
239 DW_DLE_DEBUG_ARANGES_DUPLICATE
,0);
240 if(res
!= DW_DLV_OK
) {
245 else if (strcmp(scn_name
, ".debug_line") == 0) {
246 res
= get_basic_section_data(dbg
,&dbg
->de_debug_line
, &doas
,
248 DW_DLE_DEBUG_LINE_DUPLICATE
,0);
249 if(res
!= DW_DLV_OK
) {
253 else if (strcmp(scn_name
, ".debug_frame") == 0) {
254 res
= get_basic_section_data(dbg
,&dbg
->de_debug_frame
, &doas
,
256 DW_DLE_DEBUG_FRAME_DUPLICATE
,0);
257 if(res
!= DW_DLV_OK
) {
261 } else if (strcmp(scn_name
, ".eh_frame") == 0) {
262 /* gnu egcs-1.1.2 data */
263 res
= get_basic_section_data(dbg
,&dbg
->de_debug_frame_eh_gnu
, &doas
,
265 DW_DLE_DEBUG_FRAME_DUPLICATE
,0);
266 if(res
!= DW_DLV_OK
) {
271 else if (strcmp(scn_name
, ".debug_loc") == 0) {
272 res
= get_basic_section_data(dbg
,&dbg
->de_debug_loc
, &doas
,
274 DW_DLE_DEBUG_LOC_DUPLICATE
,0);
275 if(res
!= DW_DLV_OK
) {
279 else if (strcmp(scn_name
, ".debug_pubnames") == 0) {
280 res
= get_basic_section_data(dbg
,&dbg
->de_debug_pubnames
, &doas
,
282 DW_DLE_DEBUG_PUBNAMES_DUPLICATE
,0);
283 if(res
!= DW_DLV_OK
) {
288 else if (strcmp(scn_name
, ".debug_str") == 0) {
289 res
= get_basic_section_data(dbg
,&dbg
->de_debug_str
, &doas
,
291 DW_DLE_DEBUG_STR_DUPLICATE
,0);
292 if(res
!= DW_DLV_OK
) {
296 else if (strcmp(scn_name
, ".debug_funcnames") == 0) {
298 res
= get_basic_section_data(dbg
,&dbg
->de_debug_funcnames
, &doas
,
300 DW_DLE_DEBUG_FUNCNAMES_DUPLICATE
,0);
301 if(res
!= DW_DLV_OK
) {
305 else if (strcmp(scn_name
, ".debug_typenames") == 0) {
306 /* SGI IRIX-only, created years before DWARF3. Content
307 essentially identical to .debug_pubtypes. */
308 res
= get_basic_section_data(dbg
,&dbg
->de_debug_typenames
, &doas
,
310 DW_DLE_DEBUG_TYPENAMES_DUPLICATE
,0);
311 if(res
!= DW_DLV_OK
) {
314 } else if (strcmp(scn_name
, ".debug_pubtypes") == 0) {
315 /* Section new in DWARF3. */
316 res
= get_basic_section_data(dbg
,&dbg
->de_debug_pubtypes
, &doas
,
318 DW_DLE_DEBUG_PUBTYPES_DUPLICATE
,0);
319 if(res
!= DW_DLV_OK
) {
323 else if (strcmp(scn_name
, ".debug_varnames") == 0) {
325 res
= get_basic_section_data(dbg
,&dbg
->de_debug_varnames
, &doas
,
327 DW_DLE_DEBUG_VARNAMES_DUPLICATE
,0);
328 if(res
!= DW_DLV_OK
) {
332 else if (strcmp(scn_name
, ".debug_weaknames") == 0) {
334 res
= get_basic_section_data(dbg
,&dbg
->de_debug_weaknames
, &doas
,
336 DW_DLE_DEBUG_WEAKNAMES_DUPLICATE
,0);
337 if(res
!= DW_DLV_OK
) {
340 } else if (strcmp(scn_name
, ".debug_macinfo") == 0) {
341 res
= get_basic_section_data(dbg
,&dbg
->de_debug_macinfo
, &doas
,
343 DW_DLE_DEBUG_MACINFO_DUPLICATE
,0);
344 if(res
!= DW_DLV_OK
) {
348 else if (strcmp(scn_name
, ".debug_ranges") == 0) {
349 res
= get_basic_section_data(dbg
,&dbg
->de_debug_ranges
, &doas
,
351 DW_DLE_DEBUG_RANGES_DUPLICATE
,0);
352 if(res
!= DW_DLV_OK
) {
357 else if (strcmp(scn_name
, ".symtab") == 0) {
358 res
= get_basic_section_data(dbg
,&dbg
->de_elf_symtab
, &doas
,
360 DW_DLE_DEBUG_SYMTAB_ERR
,0);
361 if(res
!= DW_DLV_OK
) {
365 else if (strcmp(scn_name
, ".strtab") == 0) {
366 res
= get_basic_section_data(dbg
,&dbg
->de_elf_strtab
, &doas
,
368 DW_DLE_DEBUG_STRTAB_ERR
,0);
369 if(res
!= DW_DLV_OK
) {
373 else if (strncmp(scn_name
, ".rela.debug_",12) == 0) {
374 const char *rcn_name
= scn_name
+ 5;
375 if (strcmp(rcn_name
, ".debug_info") == 0) {
376 add_rela_data(&dbg
->de_debug_info
,&doas
,section_index
);
377 } else if (strcmp(rcn_name
, ".debug_abbrev") == 0) {
378 add_rela_data(&dbg
->de_debug_abbrev
,&doas
,section_index
);
379 } else if (strcmp(rcn_name
, ".debug_aranges") == 0) {
380 add_rela_data(&dbg
->de_debug_aranges
,&doas
,section_index
);
381 } else if (strcmp(rcn_name
, ".debug_line") == 0) {
382 add_rela_data(&dbg
->de_debug_line
,&doas
,section_index
);
383 } else if (strcmp(rcn_name
, ".debug_frame") == 0) {
384 add_rela_data(&dbg
->de_debug_frame
,&doas
,section_index
);
385 } else if (strcmp(rcn_name
, ".eh_frame") == 0) {
386 add_rela_data(&dbg
->de_debug_frame_eh_gnu
,&doas
,section_index
);
387 } else if (strcmp(rcn_name
, ".debug_loc") == 0) {
388 add_rela_data(&dbg
->de_debug_loc
,&doas
,section_index
);
389 } else if (strcmp(rcn_name
, ".debug_pubnames") == 0) {
390 add_rela_data(&dbg
->de_debug_pubnames
,&doas
,section_index
);
391 } else if (strcmp(rcn_name
, ".debug_str") == 0) {
392 add_rela_data(&dbg
->de_debug_str
,&doas
,section_index
);
393 } else if (strcmp(rcn_name
, ".debug_funcnames") == 0) {
394 add_rela_data(&dbg
->de_debug_funcnames
,&doas
,section_index
);
395 } else if (strcmp(rcn_name
, ".debug_typenames") == 0) {
396 add_rela_data(&dbg
->de_debug_typenames
,&doas
,section_index
);
397 } else if (strcmp(rcn_name
, ".debug_pubtypes") == 0) {
398 add_rela_data(&dbg
->de_debug_pubtypes
,&doas
,section_index
);
399 } else if (strcmp(rcn_name
, ".debug_varnames") == 0) {
400 add_rela_data(&dbg
->de_debug_varnames
,&doas
,section_index
);
401 } else if (strcmp(rcn_name
, ".debug_weaknames") == 0) {
402 add_rela_data(&dbg
->de_debug_weaknames
,&doas
,section_index
);
403 } else if (strcmp(rcn_name
, ".debug_macinfo") == 0) {
404 add_rela_data(&dbg
->de_debug_macinfo
,&doas
,section_index
);
411 return DW_DLV_NO_ENTRY
;
416 Use a Dwarf_Obj_Access_Interface to kick things off. All other
417 init routines eventually use this one.
418 The returned Dwarf_Debug contains a copy of *obj
419 the callers copy of *obj may be freed whenever the caller
423 dwarf_object_init(Dwarf_Obj_Access_Interface
* obj
, Dwarf_Handler errhand
,
424 Dwarf_Ptr errarg
, Dwarf_Debug
* ret_dbg
,
428 int setup_result
= DW_DLV_OK
;
430 dbg
= _dwarf_get_debug();
432 DWARF_DBG_ERROR(dbg
, DW_DLE_DBG_ALLOC
, DW_DLV_ERROR
);
434 dbg
->de_errhand
= errhand
;
435 dbg
->de_errarg
= errarg
;
436 dbg
->de_frame_rule_initial_value
= DW_FRAME_REG_INITIAL_VALUE
;
437 dbg
->de_frame_reg_rules_entry_count
= DW_FRAME_LAST_REG_NUM
;
438 #ifdef HAVE_OLD_FRAME_CFA_COL
439 /* DW_FRAME_CFA_COL is really only suitable for old libdwarf frame
440 interfaces and its value of 0 there is only usable where
441 (as in MIPS) register 0 has no value other than 0 so
442 we can use the frame table column 0 for the CFA value
443 (and rely on client software to know when 'register 0'
444 is the cfa and when to just use a value 0 for register 0).
446 dbg
->de_frame_cfa_col_number
= DW_FRAME_CFA_COL
;
448 dbg
->de_frame_cfa_col_number
= DW_FRAME_CFA_COL3
;
450 dbg
->de_frame_same_value_number
= DW_FRAME_SAME_VAL
;
451 dbg
->de_frame_undefined_value_number
= DW_FRAME_UNDEFINED_VAL
;
453 dbg
->de_obj_file
= obj
;
455 setup_result
= _dwarf_setup(dbg
, error
);
456 if (setup_result
!= DW_DLV_OK
) {
457 /* The status we want to return here is of _dwarf_setup,
458 not of the _dwarf_free_all_of_one_debug(dbg) call.
459 So use a local status variable for the free. */
460 int freeresult
= _dwarf_free_all_of_one_debug(dbg
);
461 if (freeresult
== DW_DLV_ERROR
) {
462 DWARF_DBG_ERROR(dbg
, DW_DLE_DBG_ALLOC
, DW_DLV_ERROR
);
464 dwarf_malloc_check_complete("After Final free");
468 dwarf_harmless_init(&dbg
->de_harmless_errors
,
469 DW_HARMLESS_ERROR_CIRCULAR_LIST_DEFAULT_SIZE
);
471 /* This call cannot fail: allocates nothing, releases nothing */
472 _dwarf_setup_debug(dbg
);
481 A finish routine that is completely unaware of ELF.
483 Frees all memory that was not previously freed by
485 Aside frmo certain categories.
488 dwarf_object_finish(Dwarf_Debug dbg
, Dwarf_Error
* error
)
492 res
= _dwarf_free_all_of_one_debug(dbg
);
493 if (res
== DW_DLV_ERROR
) {
494 DWARF_DBG_ERROR(dbg
, DW_DLE_DBG_ALLOC
, DW_DLV_ERROR
);
496 dwarf_malloc_check_complete("After Final free");
503 Load the ELF section with the specified index and set the
504 pointer pointed to by section_data to the memory where it
508 _dwarf_load_section(Dwarf_Debug dbg
,
509 struct Dwarf_Section_s
*section
,
512 int res
= DW_DLV_ERROR
;
514 struct Dwarf_Obj_Access_Interface_s
*o
= 0;
516 /* check to see if the section is already loaded */
517 if (section
->dss_data
!= NULL
) {
520 o
= dbg
->de_obj_file
;
521 res
= o
->methods
->load_section(
522 o
->object
, section
->dss_index
,
523 §ion
->dss_data
, &err
);
524 if(res
== DW_DLV_ERROR
){
525 DWARF_DBG_ERROR(dbg
, err
, DW_DLV_ERROR
);
527 if(_dwarf_apply_relocs
== 0) {
530 if(section
->dss_reloc_size
== 0) {
533 if(!o
->methods
->relocate_a_section
) {
536 /*apply relocations */
537 res
= o
->methods
->relocate_a_section( o
->object
, section
->dss_index
,
539 if(res
== DW_DLV_ERROR
) {
540 DWARF_DBG_ERROR(dbg
, err
, DW_DLV_ERROR
);
545 /* This is a hack so clients can verify offsets.
546 Added April 2005 so that debugger can detect broken offsets
547 (which happened in an IRIX -64 executable larger than 2GB
548 using MIPSpro 7.3.1.3 compilers. A couple .debug_pubnames
549 offsets were wrong.).
552 dwarf_get_section_max_offsets(Dwarf_Debug dbg
,
553 Dwarf_Unsigned
* debug_info_size
,
554 Dwarf_Unsigned
* debug_abbrev_size
,
555 Dwarf_Unsigned
* debug_line_size
,
556 Dwarf_Unsigned
* debug_loc_size
,
557 Dwarf_Unsigned
* debug_aranges_size
,
558 Dwarf_Unsigned
* debug_macinfo_size
,
559 Dwarf_Unsigned
* debug_pubnames_size
,
560 Dwarf_Unsigned
* debug_str_size
,
561 Dwarf_Unsigned
* debug_frame_size
,
562 Dwarf_Unsigned
* debug_ranges_size
,
563 Dwarf_Unsigned
* debug_typenames_size
)
565 *debug_info_size
= dbg
->de_debug_info
.dss_size
;
566 *debug_abbrev_size
= dbg
->de_debug_abbrev
.dss_size
;
567 *debug_line_size
= dbg
->de_debug_line
.dss_size
;
568 *debug_loc_size
= dbg
->de_debug_loc
.dss_size
;
569 *debug_aranges_size
= dbg
->de_debug_aranges
.dss_size
;
570 *debug_macinfo_size
= dbg
->de_debug_macinfo
.dss_size
;
571 *debug_pubnames_size
= dbg
->de_debug_pubnames
.dss_size
;
572 *debug_str_size
= dbg
->de_debug_str
.dss_size
;
573 *debug_frame_size
= dbg
->de_debug_frame
.dss_size
;
574 *debug_ranges_size
= dbg
->de_debug_ranges
.dss_size
;
575 *debug_typenames_size
= dbg
->de_debug_typenames
.dss_size
;