/*
Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
Portions Copyright (C) 2007-2010 David Anderson. All Rights Reserved.
under the terms of version 2.1 of the GNU Lesser General Public License
as published by the Free Software Foundation.
This program is distributed in the hope that it would be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Further, this software is distributed without any warranty that it is
free of the rightful claim of any third person regarding infringement
or the like. Any license provided herein, whether implied or
otherwise, applies only to this software file. Patent licenses, if
any, provided herein do not apply to combinations of this program with
other software, or any other product whatsoever.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
USA.
Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
Mountain View, CA 94043, or:
For further information regarding this notice, see:
*/
/* The address of the Free Software Foundation is
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, USA.
SGI has moved from the Crittenden Lane address.
*/
/*
This implements _dwarf_get_fde_list_internal()
*/
#include "config.h"
#include "dwarf_incl.h"
#include <stdio.h>
#include <stdlib.h>
#include "dwarf_frame.h"
#include "dwarf_arange.h" /* using Arange as a way to build a
list */
int use_gnu_cie_calc,
Dwarf_Error * error);
int use_gnu_cie_calc,
unsigned long
char *augmentation);
static int
unsigned char *pers_hand_enc_out,
unsigned char *lsda_enc_out,
unsigned char *fde_begin_enc_out,
int gnu_encoding,
/* Adds 'newone' to the end of the list starting at 'head'
and makes the new one 'cur'rent. */
static void
{
else {
}
}
/* Adds 'newone' to the end of the list starting at 'head'
and makes the new one 'cur'rent. */
static void
{
} else {
}
}
/* The size of the length field plus the
value of length must be an integral
multiple of the address size. Dwarf4 standard.
A constant that gives the number of bytes of the CIE
structure, not including the length field itself
(where length mod <size of an address> == 0)
(see Section 7.2.2). Dwarf3 standard.
A uword constant that gives the number of bytes of
the CIE structure, not including the
length field, itself (length mod <addressing unit size> == 0).
Dwarf2 standard.*/
static void
const char * cieorfde)
{
if (mod != 0) {
"DW_DLE_DEBUG_FRAME_LENGTH_NOT_MULTIPLE"
" len=0x%" DW_PR_DUx
", len size=0x%" DW_PR_DUx
", extn size=0x%" DW_PR_DUx
", totl length=0x%" DW_PR_DUx
", addr size=0x%" DW_PR_DUx
" in %s"
}
return;
}
#if 0
/* For debugging only. */
static void
{
printf("prefix-print, prefix at 0x%lx, line %d\n",
printf(" start addr 0x%lx after prefix 0x%lx\n",
(long) prefix->cf_start_addr,
(long) prefix->cf_addr_after_prefix);
(long) prefix->cf_cie_id_addr);
(long) prefix->cf_section_ptr,
}
#endif
/* Internal function called from various places to create
lists of CIEs and FDEs. Not directly called
by consumer code */
int
{
/* Scans the debug_frame section. */
/*
New_cie points to the Cie being read, and head_cie_ptr and
cur_cie_ptr are used for chaining them up in sequence.
In case cie's are reused aggressively we need tail_cie_ptr
to add to the chain. If we re-use an early cie
later on, that does not mean we chain a new cie to the early one,
we always chain it to the tail. */
/*
Points to a list of contiguous pointers to Dwarf_Cie structures.
*/
/*
New_fde points to the Fde being created, and head_fde_ptr and
cur_fde_ptr are used to chain them up. */
/*
Points to a list of contiguous pointers to Dwarf_Fde structures.
*/
Dwarf_Word i = 0;
if (frame_ptr == 0) {
return DW_DLV_NO_ENTRY;
}
/* We create the fde and cie arrays. Processing each CIE as we come
to it or as an FDE refers to it. We cannot process 'late' CIEs
late as GNU .eh_frame complexities mean we need the whole CIE
before we can process the FDE correctly. */
while (frame_ptr < frame_ptr_end) {
/* First read in the 'common prefix' to figure out what we are
to do with this entry. */
if (res == DW_DLV_ERROR) {
return res;
}
if (res == DW_DLV_NO_ENTRY)
break;
if (frame_ptr >= frame_ptr_end) {
return DW_DLV_ERROR;
}
/* This is a CIE. */
/* Ok. Seen already. */
} else if (res == DW_DLV_NO_ENTRY) {
/* CIE before its FDE in this case. */
&prefix,
error);
/* ASSERT: res==DW_DLV_NO_ENTRY impossible. */
if (res == DW_DLV_ERROR) {
return res;
}
/* ASSERT res != DW_DLV_NO_ENTRY */
cie_count++;
&tail_cie_ptr);
} else { /* res == DW_DLV_ERROR */
return res;
}
continue;
} else {
/* this is an FDE, Frame Description Entry, see the Dwarf
Spec, section 6.4.1 */
/* Do not call this twice on one prefix, as
prefix.cf_cie_id_addr is altered as a side effect. */
/* Ok. Seen CIE already. */
} else if (res == DW_DLV_NO_ENTRY) {
error);
if (res == DW_DLV_ERROR) {
return res;
} else if (res == DW_DLV_NO_ENTRY) {
return res;
}
++cie_count;
&tail_cie_ptr);
} else {
/* DW_DLV_ERROR */
return res;
}
&prefix,
error);
if (res == DW_DLV_ERROR) {
return res;
}
fde_count++;
/* ASSERT: DW_DLV_OK. */
continue;
}
}
/* Now build list of CIEs from the list. If there are no CIEs
there should be no FDEs. */
if (cie_count > 0) {
cie_list_ptr = (Dwarf_Cie *)
} else {
if(fde_count > 0) {
return DW_DLV_ERROR;
}
return DW_DLV_NO_ENTRY;
}
if (cie_list_ptr == NULL) {
return DW_DLV_ERROR;
}
for (i = 0; i < cie_count; i++) {
*(cie_list_ptr + i) = cur_cie_ptr;
}
/* Now build array of FDEs from the list.
With orphan CIEs (meaning no FDEs) lets not return DW_DLV_NO_ENTRY */
if (fde_count > 0) {
fde_list_ptr = (Dwarf_Fde *)
}
/* It is ok if fde_list_ptr is NULL, we just have no fdes. */
for (i = 0; i < fde_count; i++) {
*(fde_list_ptr + i) = cur_fde_ptr;
}
/* Return arguments. */
*cie_data = cie_list_ptr;
*fde_data = fde_list_ptr;
if(use_gnu_cie_calc) {
} else {
}
/* Sort the list by the address so that dwarf_get_fde_at_pc() can
binary search this list. */
if(fde_count > 0) {
}
return (DW_DLV_OK);
}
/* Internal function, not called by consumer code.
'prefix' has accumulated the info up thru the cie-id
and now we consume the rest and build a Dwarf_Cie_s structure.
*/
int
struct cie_fde_prefix_s *prefix,
int use_gnu_cie_calc,
Dwarf_Error * error)
{
/* egcs-1.1.2 .eh_frame uses 0 as the distinguishing id. sgi uses
-1 (in .debug_frame). .eh_frame not quite identical to
.debug_frame */
/* We here default the address size as it is not present
in DWARF2 or DWARF3 cie data, below we set it right if
it is present. */
int local_length_size = 0;
unsigned char gnu_personality_handler_encoding = 0;
unsigned char gnu_lsda_encoding = 0;
unsigned char gnu_fde_begin_encoding = 0;
/* this is a CIE, Common Information Entry: See the dwarf spec,
section 6.4.1 */
frame_ptr++;
version != DW_CIE_VERSION4) {
return (DW_DLV_ERROR);
}
/* REFERENCED *//* Not used in this instance */
/* this is per egcs-1.1.2 as on RH 6.0 */
}
{
unsigned long size = 0;
if( version == DW_CIE_VERSION4) {
address_size = *((unsigned char *)frame_ptr);
++frame_ptr;
segment_size = *((unsigned char *)frame_ptr);
++frame_ptr;
}
return (DW_DLV_ERROR);
}
}
switch (augt) {
case aug_empty_string:
break;
case aug_irix_mti_v1:
break;
case aug_irix_exception_table:{
/* Decode the length of augmented fields. */
/* set the frame_ptr to point at the instruction start. */
}
break;
case aug_eh:{
int err = 0;
unsigned long increment = 0;
if (!use_gnu_cie_calc) {
/* This should be impossible. */
return DW_DLV_ERROR;
}
augt,
(char *) augmentation);
if (err == DW_DLV_ERROR) {
return DW_DLV_ERROR;
}
break;
}
case aug_gcc_eh_z:{
/* Here we have Augmentation Data Length (uleb128) followed
by Augmentation Data bytes. */
(char *) augmentation,
return res;
}
break;
}
case aug_armcc:
break;
default:{
/* We do not understand the augmentation string. No
assumption can be made about any fields other than what
we have already read. */
/* FIX -- What are the values of data_alignment_factor,
code_alignement_factor, return_address_register and
instruction start? They were clearly uninitalized in the
previous version and I am leaving them the same way. */
break;
}
} /* End switch on augmentation type. */
return (DW_DLV_ERROR);
}
/* The Following new in DWARF4 */
*cie_ptr_out = new_cie;
return DW_DLV_OK;
}
/* Internal function, not called by consumer code.
'prefix' has accumulated the info up thru the cie-id
and now we consume the rest and build a Dwarf_Fde_s structure.
*/
int
struct cie_fde_prefix_s *prefix,
int use_gnu_cie_calc,
Dwarf_Error * error)
{
/* must be min dwarf_sfixed in size */
bytes in size */
bytes in size */
if (augt == aug_gcc_eh_z) {
/* If z augmentation this is eh_frame, and initial_location and
address_range in the FDE are read according to the CIE
augmentation string instructions. */
{
&fp_updated);
return DW_DLV_ERROR;
}
/* For the address-range it makes no sense to be
pc-relative, so we turn it off with a section_pointer of
NULL. Masking off DW_EH_PE_pcrel from the
ci_gnu_fde_begin_encoding in this call would also work
to turn off DW_EH_PE_pcrel. */
&address_range, &fp_updated);
return DW_DLV_ERROR;
}
}
{
}
} else {
}
switch (augt) {
case aug_irix_mti_v1:
case aug_empty_string:
break;
case aug_irix_exception_table:{
/* The first word is an offset into exception tables.
Defined as a 32bit offset even for CC -64. */
sizeof(Dwarf_sfixed));
}
break;
case aug_eh:{
if (!use_gnu_cie_calc) {
/* This should be impossible. */
return DW_DLV_ERROR;
}
/* gnu eh fde case. we do not need to do anything */
/*REFERENCED*/ /* Not used in this instance of the
macro */
}
break;
case aug_gcc_eh_z:{
/* The Augmentation Data Length is here, followed by the
Augmentation Data bytes themselves. */
}
break;
case aug_armcc:
break;
case aug_past_last:
break;
case aug_unknown:
return DW_DLV_ERROR;
} /* End switch on augmentation type */
return (DW_DLV_ERROR);
}
*fde_ptr_out = new_fde;
return DW_DLV_OK;
}
/* called by qsort to compare FDE entries.
Consumer code expects the array of FDE pointers to be in address order.
*/
static int
{
return -1;
return 1;
}
return 0;
}
* the cie-value which shows which this is: cie or fde.
* */
int
struct cie_fde_prefix_s *data_out,
Dwarf_Error * error)
{
int local_length_size = 0;
int local_extension_size = 0;
/* READ_AREA_LENGTH updates frame_ptr for consumed bytes */
if (length == 0) {
/* nul bytes at end of section, seen at end of egcs eh_frame
data. We should be very close to end of section. */
return DW_DLV_NO_ENTRY;
}
return DW_DLV_OK;
}
/* On various errors previously-allocated CIEs and FDEs
must be cleaned up.
This helps avoid leaks in case of errors.
*/
static void
{
}
if (frame)
}
}
/* Find the cie whose id value is given: the id
* value is, per DWARF2/3, an offset in the section.
* For .debug_frame, zero is a legal offset. For
* GNU .eh_frame it is not a legal offset.
* 'cie_ptr' is a pointer into our section, not an offset. */
static int
{
/* Usually, we use the same cie again and again. */
return DW_DLV_OK;
}
return DW_DLV_OK;
}
}
return DW_DLV_NO_ENTRY;
}
/* We have a valid cie_ptr_val that has not been
* turned into an internal Cie yet. Do so now.
* Returns DW_DLV_OK or DW_DLV_ERROR, never
* DW_DLV_NO_ENTRY.
'section_ptr' - Points to first byte of section data.
'section_length' - Length of the section, in bytes.
'frame_ptr_end' - Points 1-past last byte of section data.
* */
static int
int use_gnu_cie_calc,
Dwarf_Error * error)
{
return DW_DLV_ERROR;
}
/* First read in the 'common prefix' to figure out what * we are to
do with this entry. If it is not a cie * we are in big trouble. */
if (res == DW_DLV_ERROR) {
return res;
}
if (res == DW_DLV_NO_ENTRY) {
/* error. */
return DW_DLV_ERROR;
}
return DW_DLV_ERROR;
}
&prefix,
return res;
}
/* This is for gnu eh frames, the 'z' case.
We find the letter involved
Return the augmentation character and, if applicable,
the personality routine address.
personality_routine_out -
if 'P' is augchar, is personality handler addr.
Otherwise is not set.
aug_data - if 'P' points to data space of the
aug_data_len - length of areas aug_data points to.
*/
#if 0
/* For debugging only. */
void
{
}
}
#endif
static int
unsigned char *pers_hand_enc_out,
unsigned char *lsda_enc_out,
unsigned char *fde_begin_enc_out,
{
char *nc = 0;
char c = *nc;
switch (c) {
case 'z':
/* Means that the augmentation data is present. */
continue;
case 'S':
/* Indicates this is a signal stack frame. Debuggers have to do
special handling. We don't need to do more than print this flag at
the right time, though (see dwarfdump where it prints the augmentation
string).
A signal stack frame (in some OS's) can only be
unwound (backtraced) by knowing it is a signal stack frame
(perhaps by noticing the name of the function for the stack frame
if the name can be found somehow) and figuring
out (or knowing) how the kernel and libc pushed a structure
onto the stack and loading registers from that structure.
Totally different from normal stack unwinding.
This flag gives an unwinder a big leg up by decoupling the
'hint: this is a stack frame' from knowledge like
the function name (the name might be unavailable at unwind time).
*/
break;
case 'L':
return DW_DLV_ERROR;
}
*lsda_enc_out = *(unsigned char *) cur_aug_p;
++cur_aug_p;
break;
case 'R':
/* Followed by a one byte argument giving the
pointer encoding for the address pointers in the fde. */
return DW_DLV_ERROR;
}
*fde_begin_enc_out = *(unsigned char *) cur_aug_p;
++cur_aug_p;
break;
case 'P':{
unsigned char encoding = 0;
return DW_DLV_ERROR;
}
++cur_aug_p;
return DW_DLV_ERROR;
}
/* DW_EH_PE_pcrel makes no sense here, so we turn it
off via a section pointer of NULL. */
(Dwarf_Small *) NULL,
return res;
}
return DW_DLV_ERROR;
}
}
break;
default:
return DW_DLV_ERROR;
}
}
return DW_DLV_OK;
}
/* Given augmentation character (the encoding) giving the
address format, read the address from input_field
and return an incremented value 1 past the input bytes of the
address.
Push the address read back thru the *addr pointer.
See LSB (Linux Standar Base) exception handling documents.
*/
static int
int gnu_encoding,
{
if (gnu_encoding == 0xff) {
/* There is no data here. */
*addr = 0;
/* Should we return DW_DLV_NO_ENTRY? */
return DW_DLV_OK;
}
switch (value_type) {
case DW_EH_PE_absptr:{
/* value_type is zero. Treat as pointer size of the object.
*/
}
break;
case DW_EH_PE_uleb128:{
&length);
}
break;
case DW_EH_PE_udata2:{
input_field, 2);
}
break;
case DW_EH_PE_udata4:{
/* ASSERT: sizeof(Dwarf_ufixed) == 4 */
input_field, sizeof(Dwarf_ufixed));
}
break;
case DW_EH_PE_udata8:{
/* ASSERT: sizeof(Dwarf_Unsigned) == 8 */
input_field, sizeof(Dwarf_Unsigned));
}
break;
case DW_EH_PE_sleb128:{
&length);
}
break;
case DW_EH_PE_sdata2:{
}
break;
case DW_EH_PE_sdata4:{
/* ASSERT: sizeof(Dwarf_ufixed) == 4 */
sizeof(Dwarf_ufixed));
}
break;
case DW_EH_PE_sdata8:{
/* ASSERT: sizeof(Dwarf_Unsigned) == 8 */
sizeof(Dwarf_Unsigned));
}
break;
default:
return DW_DLV_ERROR;
};
/* The ELF ABI for gnu does not document the meaning of
DW_EH_PE_pcrel, which is awkward. It apparently means the value
we got above is pc-relative (meaning section-relative), so we
adjust the value. Section_pointer may be null if it is known
DW_EH_PE_pcrel cannot apply, such as for .debug_frame or for an
address-range value. */
/* Address (*addr) above is pc relative with respect to a
section. Add to the offset the base address (from elf) of
section and the distance of the field we are reading from
the section-beginning to get the actual address. */
/* ASSERT: input_field_original >= section_pointer */
}
return DW_DLV_OK;
}
/*
All augmentation string checking done here now.
For .eh_frame, gcc from 3.3 uses the z style, earlier used
only "eh" as augmentation. We don't yet handle
decoding .eh_frame with the z style extensions like L P.
These are nasty heuristics, but then that's life
as augmentations are implementation specific.
*/
/* ARGSUSED */
int is_gcc_eh_frame)
{
enum Dwarf_augmentation_type t = aug_unknown;
if (ag_string[0] == 0) {
/* Empty string. We'll just guess that we know what this means:
standard dwarf2/3 with no implementation-defined fields. */
t = aug_empty_string;
/* The string is "mti v1". Used internally at SGI, probably
never shipped. Replaced by "z". Treat like 'nothing
special'. */
t = aug_irix_mti_v1;
} else if (ag_string[0] == 'z') {
/* If it's IRIX cc, z means aug_irix_exception_table. z1 z2
were designed as for IRIX CC, but never implemented */
/* If it's gcc, z may be any of several things. "z" or z
followed optionally followed by one or more of L R P, each
of which means a value may be present. Should be in eh_frame
only, I think. */
if (is_gcc_eh_frame) {
t = aug_gcc_eh_z;
} else if (ag_string[1] == 0) {
/* This is the normal IRIX C++ case, where there is an
offset into a table in each fde. The table being for
IRIX CC exception handling. */
/* DW_CIE_AUGMENTER_STRING_V0 "z" */
} /* Else unknown. */
/* gcc .eh_frame augmentation for egcs and gcc 2.x, at least
for x86. */
t = aug_eh;
/* Arm uses this string to mean a bug in
in Arm compilers was fixed, changing to the standard
calculation of the CFA. See
for details. */
t = aug_armcc;
} else {
}
return t;
}
/* Using augmentation, and version
read in the augmentation data for GNU eh.
Return DW_DLV_OK if we succeeded,
DW_DLV_ERR if we fail.
On success, update 'size_of_augmentation_data' with
the length of the fields that are part of augmentation (so the
caller can increment frame_ptr appropriately).
'frame_ptr' points within section.
'section_pointer' points to section base address in memory.
*/
/* ARGSUSED */
static int
unsigned long *size_of_augmentation_data,
char *augmentation)
{
char *suffix = 0;
unsigned long augdata_size = 0;
if (augtype == aug_gcc_eh_z) {
/* Has leading 'z'. */
/* Dwarf_Unsigned eh_value = */
} else {
/* Prefix is 'eh'. As in gcc 3.2. No suffix present
apparently. */
}
/* We have no idea what this is as yet. Some extensions beyond
dwarf exist which we do not yet handle. */
return DW_DLV_ERROR;
}
return DW_DLV_OK;
}
/* Make the 'cie_id_addr' consistent across .debug_frame and .eh_frame.
Calculate a pointer into section bytes given a cie_id, which is
trivial for .debug_frame, but a bit more work for .eh_frame.
*/
static Dwarf_Small *
int use_gnu_cie_calc,
{
if (use_gnu_cie_calc) {
/* cie_id value is offset, in section, of the cie_id itself, to
use vm ptr of the value, less the value, to get to the cie
itself. In addition, munge *cie_id_addr to look *as if* it
was from real dwarf. */
} else {
/* Traditional dwarf section offset is in cie_id */
}
return cieptr;
}
/* To properly release all spaced used.
Earlier approaches (before July 15, 2005)
letting client do the dealloc directly left
some data allocated.
This is directly called by consumer code.
*/
void
{
Dwarf_Signed i = 0;
for (i = 0; i < cie_element_count; ++i) {
if (frame)
}
for (i = 0; i < fde_element_count; ++i) {
}
if (cie_data)
if (fde_data)
}