xref: /titanic_50/usr/src/lib/libdwarf/common/dwarf_ranges.c (revision f3e7f55e73a39377d55a030f124cc86b3b66a9cc)
1*f3e7f55eSRobert Mustacchi /*
2*f3e7f55eSRobert Mustacchi 
3*f3e7f55eSRobert Mustacchi   Copyright (C) 2008-2010 David Anderson. All Rights Reserved.
4*f3e7f55eSRobert Mustacchi 
5*f3e7f55eSRobert Mustacchi   This program is free software; you can redistribute it and/or modify it
6*f3e7f55eSRobert Mustacchi   under the terms of version 2.1 of the GNU Lesser General Public License
7*f3e7f55eSRobert Mustacchi   as published by the Free Software Foundation.
8*f3e7f55eSRobert Mustacchi 
9*f3e7f55eSRobert Mustacchi   This program is distributed in the hope that it would be useful, but
10*f3e7f55eSRobert Mustacchi   WITHOUT ANY WARRANTY; without even the implied warranty of
11*f3e7f55eSRobert Mustacchi   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*f3e7f55eSRobert Mustacchi 
13*f3e7f55eSRobert Mustacchi   Further, this software is distributed without any warranty that it is
14*f3e7f55eSRobert Mustacchi   free of the rightful claim of any third person regarding infringement
15*f3e7f55eSRobert Mustacchi   or the like.  Any license provided herein, whether implied or
16*f3e7f55eSRobert Mustacchi   otherwise, applies only to this software file.  Patent licenses, if
17*f3e7f55eSRobert Mustacchi   any, provided herein do not apply to combinations of this program with
18*f3e7f55eSRobert Mustacchi   other software, or any other product whatsoever.
19*f3e7f55eSRobert Mustacchi 
20*f3e7f55eSRobert Mustacchi   You should have received a copy of the GNU Lesser General Public
21*f3e7f55eSRobert Mustacchi   License along with this program; if not, write the Free Software
22*f3e7f55eSRobert Mustacchi   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
23*f3e7f55eSRobert Mustacchi   USA.
24*f3e7f55eSRobert Mustacchi 
25*f3e7f55eSRobert Mustacchi   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
26*f3e7f55eSRobert Mustacchi   Mountain View, CA 94043, or:
27*f3e7f55eSRobert Mustacchi 
28*f3e7f55eSRobert Mustacchi   http://www.sgi.com
29*f3e7f55eSRobert Mustacchi 
30*f3e7f55eSRobert Mustacchi   For further information regarding this notice, see:
31*f3e7f55eSRobert Mustacchi 
32*f3e7f55eSRobert Mustacchi   http://oss.sgi.com/projects/GenInfo/NoticeExplan
33*f3e7f55eSRobert Mustacchi 
34*f3e7f55eSRobert Mustacchi */
35*f3e7f55eSRobert Mustacchi /* The address of the Free Software Foundation is
36*f3e7f55eSRobert Mustacchi    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
37*f3e7f55eSRobert Mustacchi    Boston, MA 02110-1301, USA.
38*f3e7f55eSRobert Mustacchi    SGI has moved from the Crittenden Lane address.
39*f3e7f55eSRobert Mustacchi */
40*f3e7f55eSRobert Mustacchi 
41*f3e7f55eSRobert Mustacchi 
42*f3e7f55eSRobert Mustacchi 
43*f3e7f55eSRobert Mustacchi #include "config.h"
44*f3e7f55eSRobert Mustacchi #include <stdlib.h>
45*f3e7f55eSRobert Mustacchi #include "dwarf_incl.h"
46*f3e7f55eSRobert Mustacchi 
47*f3e7f55eSRobert Mustacchi struct ranges_entry {
48*f3e7f55eSRobert Mustacchi    struct ranges_entry *next;
49*f3e7f55eSRobert Mustacchi    Dwarf_Ranges cur;
50*f3e7f55eSRobert Mustacchi };
51*f3e7f55eSRobert Mustacchi 
52*f3e7f55eSRobert Mustacchi 
53*f3e7f55eSRobert Mustacchi #define MAX_ADDR ((address_size == 8)?0xffffffffffffffffULL:0xffffffff)
dwarf_get_ranges_a(Dwarf_Debug dbg,Dwarf_Off rangesoffset,Dwarf_Die die,Dwarf_Ranges ** rangesbuf,Dwarf_Signed * listlen,Dwarf_Unsigned * bytecount,Dwarf_Error * error)54*f3e7f55eSRobert Mustacchi int dwarf_get_ranges_a(Dwarf_Debug dbg,
55*f3e7f55eSRobert Mustacchi     Dwarf_Off rangesoffset,
56*f3e7f55eSRobert Mustacchi     Dwarf_Die die,
57*f3e7f55eSRobert Mustacchi     Dwarf_Ranges ** rangesbuf,
58*f3e7f55eSRobert Mustacchi     Dwarf_Signed * listlen,
59*f3e7f55eSRobert Mustacchi     Dwarf_Unsigned * bytecount,
60*f3e7f55eSRobert Mustacchi     Dwarf_Error * error)
61*f3e7f55eSRobert Mustacchi {
62*f3e7f55eSRobert Mustacchi     Dwarf_Small *rangeptr = 0;
63*f3e7f55eSRobert Mustacchi     Dwarf_Small *beginrangeptr = 0;
64*f3e7f55eSRobert Mustacchi     Dwarf_Small *section_end = 0;
65*f3e7f55eSRobert Mustacchi     unsigned entry_count = 0;
66*f3e7f55eSRobert Mustacchi     struct ranges_entry *base = 0;
67*f3e7f55eSRobert Mustacchi     struct ranges_entry *last = 0;
68*f3e7f55eSRobert Mustacchi     struct ranges_entry *curre = 0;
69*f3e7f55eSRobert Mustacchi     Dwarf_Ranges * ranges_data_out = 0;
70*f3e7f55eSRobert Mustacchi     unsigned copyindex = 0;
71*f3e7f55eSRobert Mustacchi     Dwarf_Half address_size = 0;
72*f3e7f55eSRobert Mustacchi     int res = DW_DLV_ERROR;
73*f3e7f55eSRobert Mustacchi 
74*f3e7f55eSRobert Mustacchi     res = _dwarf_load_section(dbg, &dbg->de_debug_ranges,error);
75*f3e7f55eSRobert Mustacchi     if (res != DW_DLV_OK) {
76*f3e7f55eSRobert Mustacchi         return res;
77*f3e7f55eSRobert Mustacchi     }
78*f3e7f55eSRobert Mustacchi     if(rangesoffset >= dbg->de_debug_ranges.dss_size) {
79*f3e7f55eSRobert Mustacchi         _dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OFFSET_BAD);
80*f3e7f55eSRobert Mustacchi         return (DW_DLV_ERROR);
81*f3e7f55eSRobert Mustacchi 
82*f3e7f55eSRobert Mustacchi     }
83*f3e7f55eSRobert Mustacchi     address_size = _dwarf_get_address_size(dbg, die);
84*f3e7f55eSRobert Mustacchi     section_end = dbg->de_debug_ranges.dss_data +
85*f3e7f55eSRobert Mustacchi         dbg->de_debug_ranges.dss_size;
86*f3e7f55eSRobert Mustacchi     rangeptr = dbg->de_debug_ranges.dss_data + rangesoffset;
87*f3e7f55eSRobert Mustacchi     beginrangeptr = rangeptr;
88*f3e7f55eSRobert Mustacchi 
89*f3e7f55eSRobert Mustacchi     for(;;) {
90*f3e7f55eSRobert Mustacchi         struct ranges_entry * re = calloc(sizeof(struct ranges_entry),1);
91*f3e7f55eSRobert Mustacchi         if(!re) {
92*f3e7f55eSRobert Mustacchi             _dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OUT_OF_MEM);
93*f3e7f55eSRobert Mustacchi             return (DW_DLV_ERROR);
94*f3e7f55eSRobert Mustacchi         }
95*f3e7f55eSRobert Mustacchi         if(rangeptr  >= section_end) {
96*f3e7f55eSRobert Mustacchi             return (DW_DLV_NO_ENTRY);
97*f3e7f55eSRobert Mustacchi         }
98*f3e7f55eSRobert Mustacchi         if((rangeptr + (2*address_size)) > section_end) {
99*f3e7f55eSRobert Mustacchi             _dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OFFSET_BAD);
100*f3e7f55eSRobert Mustacchi             return (DW_DLV_ERROR);
101*f3e7f55eSRobert Mustacchi         }
102*f3e7f55eSRobert Mustacchi         entry_count++;
103*f3e7f55eSRobert Mustacchi         READ_UNALIGNED(dbg,re->cur.dwr_addr1,
104*f3e7f55eSRobert Mustacchi                        Dwarf_Addr, rangeptr,
105*f3e7f55eSRobert Mustacchi                        address_size);
106*f3e7f55eSRobert Mustacchi         rangeptr +=  address_size;
107*f3e7f55eSRobert Mustacchi         READ_UNALIGNED(dbg,re->cur.dwr_addr2 ,
108*f3e7f55eSRobert Mustacchi                        Dwarf_Addr, rangeptr,
109*f3e7f55eSRobert Mustacchi                        address_size);
110*f3e7f55eSRobert Mustacchi         rangeptr +=  address_size;
111*f3e7f55eSRobert Mustacchi         if(!base) {
112*f3e7f55eSRobert Mustacchi            base = re;
113*f3e7f55eSRobert Mustacchi            last = re;
114*f3e7f55eSRobert Mustacchi         } else {
115*f3e7f55eSRobert Mustacchi            last->next = re;
116*f3e7f55eSRobert Mustacchi            last = re;
117*f3e7f55eSRobert Mustacchi         }
118*f3e7f55eSRobert Mustacchi         if(re->cur.dwr_addr1 == 0 && re->cur.dwr_addr2 == 0) {
119*f3e7f55eSRobert Mustacchi             re->cur.dwr_type =  DW_RANGES_END;
120*f3e7f55eSRobert Mustacchi             break;
121*f3e7f55eSRobert Mustacchi         } else if ( re->cur.dwr_addr1 == MAX_ADDR) {
122*f3e7f55eSRobert Mustacchi             re->cur.dwr_type =  DW_RANGES_ADDRESS_SELECTION;
123*f3e7f55eSRobert Mustacchi         } else {
124*f3e7f55eSRobert Mustacchi             re->cur.dwr_type =  DW_RANGES_ENTRY;
125*f3e7f55eSRobert Mustacchi         }
126*f3e7f55eSRobert Mustacchi     }
127*f3e7f55eSRobert Mustacchi 
128*f3e7f55eSRobert Mustacchi     ranges_data_out =   (Dwarf_Ranges *)
129*f3e7f55eSRobert Mustacchi     _dwarf_get_alloc(dbg,DW_DLA_RANGES,entry_count);
130*f3e7f55eSRobert Mustacchi     if(!ranges_data_out) {
131*f3e7f55eSRobert Mustacchi             _dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OUT_OF_MEM);
132*f3e7f55eSRobert Mustacchi             return (DW_DLV_ERROR);
133*f3e7f55eSRobert Mustacchi     }
134*f3e7f55eSRobert Mustacchi     curre = base;
135*f3e7f55eSRobert Mustacchi     *rangesbuf = ranges_data_out;
136*f3e7f55eSRobert Mustacchi     *listlen = entry_count;
137*f3e7f55eSRobert Mustacchi     for( copyindex = 0; curre && (copyindex < entry_count);
138*f3e7f55eSRobert Mustacchi         ++copyindex,++ranges_data_out) {
139*f3e7f55eSRobert Mustacchi 
140*f3e7f55eSRobert Mustacchi         struct ranges_entry *r = curre;
141*f3e7f55eSRobert Mustacchi         *ranges_data_out = curre->cur;
142*f3e7f55eSRobert Mustacchi         curre = curre->next;
143*f3e7f55eSRobert Mustacchi         free(r);
144*f3e7f55eSRobert Mustacchi     }
145*f3e7f55eSRobert Mustacchi     /* Callers will often not care about the bytes used. */
146*f3e7f55eSRobert Mustacchi     if(bytecount) {
147*f3e7f55eSRobert Mustacchi         *bytecount = rangeptr - beginrangeptr;
148*f3e7f55eSRobert Mustacchi     }
149*f3e7f55eSRobert Mustacchi     return DW_DLV_OK;
150*f3e7f55eSRobert Mustacchi }
dwarf_get_ranges(Dwarf_Debug dbg,Dwarf_Off rangesoffset,Dwarf_Ranges ** rangesbuf,Dwarf_Signed * listlen,Dwarf_Unsigned * bytecount,Dwarf_Error * error)151*f3e7f55eSRobert Mustacchi int dwarf_get_ranges(Dwarf_Debug dbg,
152*f3e7f55eSRobert Mustacchi     Dwarf_Off rangesoffset,
153*f3e7f55eSRobert Mustacchi     Dwarf_Ranges ** rangesbuf,
154*f3e7f55eSRobert Mustacchi     Dwarf_Signed * listlen,
155*f3e7f55eSRobert Mustacchi     Dwarf_Unsigned * bytecount,
156*f3e7f55eSRobert Mustacchi     Dwarf_Error * error)
157*f3e7f55eSRobert Mustacchi {
158*f3e7f55eSRobert Mustacchi     Dwarf_Die die = 0;
159*f3e7f55eSRobert Mustacchi     int res = dwarf_get_ranges_a(dbg,rangesoffset,die,
160*f3e7f55eSRobert Mustacchi         rangesbuf,listlen,bytecount,error);
161*f3e7f55eSRobert Mustacchi     return res;
162*f3e7f55eSRobert Mustacchi }
163*f3e7f55eSRobert Mustacchi 
164*f3e7f55eSRobert Mustacchi void
dwarf_ranges_dealloc(Dwarf_Debug dbg,Dwarf_Ranges * rangesbuf,Dwarf_Signed rangecount)165*f3e7f55eSRobert Mustacchi dwarf_ranges_dealloc(Dwarf_Debug dbg, Dwarf_Ranges * rangesbuf,
166*f3e7f55eSRobert Mustacchi     Dwarf_Signed rangecount)
167*f3e7f55eSRobert Mustacchi {
168*f3e7f55eSRobert Mustacchi     dwarf_dealloc(dbg,rangesbuf, DW_DLA_RANGES);
169*f3e7f55eSRobert Mustacchi 
170*f3e7f55eSRobert Mustacchi }
171*f3e7f55eSRobert Mustacchi 
172