1*0a6a1f1dSLionel Sambuc /* $NetBSD: libdwarf_arange.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2009-2011 Kai Wang
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libdwarf_arange.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libdwarf_arange.c 2070 2011-10-27 03:05:32Z jkoshy ");
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc void
_dwarf_arange_cleanup(Dwarf_Debug dbg)35*0a6a1f1dSLionel Sambuc _dwarf_arange_cleanup(Dwarf_Debug dbg)
36*0a6a1f1dSLionel Sambuc {
37*0a6a1f1dSLionel Sambuc Dwarf_ArangeSet as, tas;
38*0a6a1f1dSLionel Sambuc Dwarf_Arange ar, tar;
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(as, &dbg->dbg_aslist, as_next, tas) {
41*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(ar, &as->as_arlist, ar_next, tar) {
42*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&as->as_arlist, ar, _Dwarf_Arange,
43*0a6a1f1dSLionel Sambuc ar_next);
44*0a6a1f1dSLionel Sambuc free(ar);
45*0a6a1f1dSLionel Sambuc }
46*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&dbg->dbg_aslist, as, _Dwarf_ArangeSet, as_next);
47*0a6a1f1dSLionel Sambuc free(as);
48*0a6a1f1dSLionel Sambuc }
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc if (dbg->dbg_arange_array)
51*0a6a1f1dSLionel Sambuc free(dbg->dbg_arange_array);
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc dbg->dbg_arange_array = NULL;
54*0a6a1f1dSLionel Sambuc dbg->dbg_arange_cnt = 0;
55*0a6a1f1dSLionel Sambuc }
56*0a6a1f1dSLionel Sambuc
57*0a6a1f1dSLionel Sambuc int
_dwarf_arange_init(Dwarf_Debug dbg,Dwarf_Error * error)58*0a6a1f1dSLionel Sambuc _dwarf_arange_init(Dwarf_Debug dbg, Dwarf_Error *error)
59*0a6a1f1dSLionel Sambuc {
60*0a6a1f1dSLionel Sambuc Dwarf_CU cu;
61*0a6a1f1dSLionel Sambuc Dwarf_ArangeSet as;
62*0a6a1f1dSLionel Sambuc Dwarf_Arange ar;
63*0a6a1f1dSLionel Sambuc Dwarf_Section *ds;
64*0a6a1f1dSLionel Sambuc uint64_t offset, dwarf_size, length, addr, range;
65*0a6a1f1dSLionel Sambuc int i, ret;
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc ret = DW_DLE_NONE;
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc if ((ds = _dwarf_find_section(dbg, ".debug_aranges")) == NULL)
70*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc if (!dbg->dbg_info_loaded) {
73*0a6a1f1dSLionel Sambuc ret = _dwarf_info_load(dbg, 1, error);
74*0a6a1f1dSLionel Sambuc if (ret != DW_DLE_NONE)
75*0a6a1f1dSLionel Sambuc return (ret);
76*0a6a1f1dSLionel Sambuc }
77*0a6a1f1dSLionel Sambuc
78*0a6a1f1dSLionel Sambuc offset = 0;
79*0a6a1f1dSLionel Sambuc while (offset < ds->ds_size) {
80*0a6a1f1dSLionel Sambuc
81*0a6a1f1dSLionel Sambuc if ((as = malloc(sizeof(struct _Dwarf_ArangeSet))) == NULL) {
82*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
83*0a6a1f1dSLionel Sambuc return (DW_DLE_MEMORY);
84*0a6a1f1dSLionel Sambuc }
85*0a6a1f1dSLionel Sambuc STAILQ_INIT(&as->as_arlist);
86*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&dbg->dbg_aslist, as, as_next);
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc /* Read in the table header. */
89*0a6a1f1dSLionel Sambuc length = dbg->read(ds->ds_data, &offset, 4);
90*0a6a1f1dSLionel Sambuc if (length == 0xffffffff) {
91*0a6a1f1dSLionel Sambuc dwarf_size = 8;
92*0a6a1f1dSLionel Sambuc length = dbg->read(ds->ds_data, &offset, 8);
93*0a6a1f1dSLionel Sambuc } else
94*0a6a1f1dSLionel Sambuc dwarf_size = 4;
95*0a6a1f1dSLionel Sambuc
96*0a6a1f1dSLionel Sambuc as->as_length = length;
97*0a6a1f1dSLionel Sambuc as->as_version = dbg->read(ds->ds_data, &offset, 2);
98*0a6a1f1dSLionel Sambuc if (as->as_version != 2) {
99*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
100*0a6a1f1dSLionel Sambuc ret = DW_DLE_VERSION_STAMP_ERROR;
101*0a6a1f1dSLionel Sambuc goto fail_cleanup;
102*0a6a1f1dSLionel Sambuc }
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel Sambuc as->as_cu_offset = dbg->read(ds->ds_data, &offset, dwarf_size);
105*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) {
106*0a6a1f1dSLionel Sambuc if (cu->cu_offset == as->as_cu_offset)
107*0a6a1f1dSLionel Sambuc break;
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc if (cu == NULL) {
110*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_ARANGE_OFFSET_BAD);
111*0a6a1f1dSLionel Sambuc ret = DW_DLE_ARANGE_OFFSET_BAD;
112*0a6a1f1dSLionel Sambuc goto fail_cleanup;
113*0a6a1f1dSLionel Sambuc }
114*0a6a1f1dSLionel Sambuc as->as_cu = cu;
115*0a6a1f1dSLionel Sambuc
116*0a6a1f1dSLionel Sambuc as->as_addrsz = dbg->read(ds->ds_data, &offset, 1);
117*0a6a1f1dSLionel Sambuc as->as_segsz = dbg->read(ds->ds_data, &offset, 1);
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc /* Skip the padding bytes. */
120*0a6a1f1dSLionel Sambuc offset = roundup(offset, 2 * as->as_addrsz);
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc /* Read in address range descriptors. */
123*0a6a1f1dSLionel Sambuc while (offset < ds->ds_size) {
124*0a6a1f1dSLionel Sambuc addr = dbg->read(ds->ds_data, &offset, as->as_addrsz);
125*0a6a1f1dSLionel Sambuc range = dbg->read(ds->ds_data, &offset, as->as_addrsz);
126*0a6a1f1dSLionel Sambuc if (addr == 0 && range == 0)
127*0a6a1f1dSLionel Sambuc break;
128*0a6a1f1dSLionel Sambuc if ((ar = calloc(1, sizeof(struct _Dwarf_Arange))) ==
129*0a6a1f1dSLionel Sambuc NULL) {
130*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
131*0a6a1f1dSLionel Sambuc goto fail_cleanup;
132*0a6a1f1dSLionel Sambuc }
133*0a6a1f1dSLionel Sambuc ar->ar_as = as;
134*0a6a1f1dSLionel Sambuc ar->ar_address = addr;
135*0a6a1f1dSLionel Sambuc ar->ar_range = range;
136*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&as->as_arlist, ar, ar_next);
137*0a6a1f1dSLionel Sambuc dbg->dbg_arange_cnt++;
138*0a6a1f1dSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc }
140*0a6a1f1dSLionel Sambuc
141*0a6a1f1dSLionel Sambuc /* Build arange array. */
142*0a6a1f1dSLionel Sambuc if (dbg->dbg_arange_cnt > 0) {
143*0a6a1f1dSLionel Sambuc if ((dbg->dbg_arange_array = malloc(dbg->dbg_arange_cnt *
144*0a6a1f1dSLionel Sambuc sizeof(struct _Dwarf_Arange))) == NULL) {
145*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
146*0a6a1f1dSLionel Sambuc ret = DW_DLE_MEMORY;
147*0a6a1f1dSLionel Sambuc goto fail_cleanup;
148*0a6a1f1dSLionel Sambuc }
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambuc i = 0;
151*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(as, &dbg->dbg_aslist, as_next) {
152*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(ar, &as->as_arlist, ar_next)
153*0a6a1f1dSLionel Sambuc dbg->dbg_arange_array[i++] = ar;
154*0a6a1f1dSLionel Sambuc }
155*0a6a1f1dSLionel Sambuc assert((Dwarf_Unsigned)i == dbg->dbg_arange_cnt);
156*0a6a1f1dSLionel Sambuc }
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambuc fail_cleanup:
161*0a6a1f1dSLionel Sambuc
162*0a6a1f1dSLionel Sambuc _dwarf_arange_cleanup(dbg);
163*0a6a1f1dSLionel Sambuc
164*0a6a1f1dSLionel Sambuc return (ret);
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc int
_dwarf_arange_gen(Dwarf_P_Debug dbg,Dwarf_Error * error)168*0a6a1f1dSLionel Sambuc _dwarf_arange_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
169*0a6a1f1dSLionel Sambuc {
170*0a6a1f1dSLionel Sambuc Dwarf_P_Section ds;
171*0a6a1f1dSLionel Sambuc Dwarf_Rel_Section drs;
172*0a6a1f1dSLionel Sambuc Dwarf_ArangeSet as;
173*0a6a1f1dSLionel Sambuc Dwarf_Arange ar;
174*0a6a1f1dSLionel Sambuc uint64_t offset;
175*0a6a1f1dSLionel Sambuc int ret;
176*0a6a1f1dSLionel Sambuc
177*0a6a1f1dSLionel Sambuc as = dbg->dbgp_as;
178*0a6a1f1dSLionel Sambuc assert(as != NULL);
179*0a6a1f1dSLionel Sambuc if (STAILQ_EMPTY(&as->as_arlist))
180*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
181*0a6a1f1dSLionel Sambuc
182*0a6a1f1dSLionel Sambuc as->as_length = 0;
183*0a6a1f1dSLionel Sambuc as->as_version = 2;
184*0a6a1f1dSLionel Sambuc as->as_cu_offset = 0; /* We have only one CU. */
185*0a6a1f1dSLionel Sambuc as->as_addrsz = dbg->dbg_pointer_size;
186*0a6a1f1dSLionel Sambuc as->as_segsz = 0; /* XXX */
187*0a6a1f1dSLionel Sambuc
188*0a6a1f1dSLionel Sambuc /* Create .debug_arange section. */
189*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_section_init(dbg, &ds, ".debug_aranges", 0, error)) !=
190*0a6a1f1dSLionel Sambuc DW_DLE_NONE)
191*0a6a1f1dSLionel Sambuc goto gen_fail0;
192*0a6a1f1dSLionel Sambuc
193*0a6a1f1dSLionel Sambuc /* Create relocation section for .debug_aranges */
194*0a6a1f1dSLionel Sambuc RCHECK(_dwarf_reloc_section_init(dbg, &drs, ds, error));
195*0a6a1f1dSLionel Sambuc
196*0a6a1f1dSLionel Sambuc /* Write section header. */
197*0a6a1f1dSLionel Sambuc RCHECK(WRITE_VALUE(as->as_length, 4));
198*0a6a1f1dSLionel Sambuc RCHECK(WRITE_VALUE(as->as_version, 2));
199*0a6a1f1dSLionel Sambuc RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds, dwarf_drt_data_reloc, 4,
200*0a6a1f1dSLionel Sambuc ds->ds_size, 0, as->as_cu_offset, ".debug_info", error));
201*0a6a1f1dSLionel Sambuc RCHECK(WRITE_VALUE(as->as_addrsz, 1));
202*0a6a1f1dSLionel Sambuc RCHECK(WRITE_VALUE(as->as_segsz, 1));
203*0a6a1f1dSLionel Sambuc
204*0a6a1f1dSLionel Sambuc /* Pad to (2 * address_size) */
205*0a6a1f1dSLionel Sambuc offset = roundup(ds->ds_size, 2 * as->as_addrsz);
206*0a6a1f1dSLionel Sambuc if (offset > ds->ds_size)
207*0a6a1f1dSLionel Sambuc RCHECK(WRITE_PADDING(0, offset - ds->ds_size));
208*0a6a1f1dSLionel Sambuc
209*0a6a1f1dSLionel Sambuc /* Write tuples. */
210*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(ar, &as->as_arlist, ar_next) {
211*0a6a1f1dSLionel Sambuc RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds,
212*0a6a1f1dSLionel Sambuc dwarf_drt_data_reloc, dbg->dbg_pointer_size, ds->ds_size,
213*0a6a1f1dSLionel Sambuc ar->ar_symndx, ar->ar_address, NULL, error));
214*0a6a1f1dSLionel Sambuc if (ar->ar_esymndx > 0)
215*0a6a1f1dSLionel Sambuc RCHECK(_dwarf_reloc_entry_add_pair(dbg, drs, ds,
216*0a6a1f1dSLionel Sambuc dbg->dbg_pointer_size, ds->ds_size, ar->ar_symndx,
217*0a6a1f1dSLionel Sambuc ar->ar_esymndx, ar->ar_address, ar->ar_eoff, error));
218*0a6a1f1dSLionel Sambuc else
219*0a6a1f1dSLionel Sambuc RCHECK(WRITE_VALUE(ar->ar_range, dbg->dbg_pointer_size));
220*0a6a1f1dSLionel Sambuc }
221*0a6a1f1dSLionel Sambuc RCHECK(WRITE_VALUE(0, dbg->dbg_pointer_size));
222*0a6a1f1dSLionel Sambuc RCHECK(WRITE_VALUE(0, dbg->dbg_pointer_size));
223*0a6a1f1dSLionel Sambuc
224*0a6a1f1dSLionel Sambuc /* Fill in the length field. */
225*0a6a1f1dSLionel Sambuc as->as_length = ds->ds_size - 4;
226*0a6a1f1dSLionel Sambuc offset = 0;
227*0a6a1f1dSLionel Sambuc dbg->write(ds->ds_data, &offset, as->as_length, 4);
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc /* Inform application the creation of .debug_aranges ELF section. */
230*0a6a1f1dSLionel Sambuc RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
231*0a6a1f1dSLionel Sambuc
232*0a6a1f1dSLionel Sambuc /* Finalize relocation section for .debug_aranges */
233*0a6a1f1dSLionel Sambuc RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error));
234*0a6a1f1dSLionel Sambuc
235*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
236*0a6a1f1dSLionel Sambuc
237*0a6a1f1dSLionel Sambuc gen_fail:
238*0a6a1f1dSLionel Sambuc _dwarf_reloc_section_free(dbg, &drs);
239*0a6a1f1dSLionel Sambuc
240*0a6a1f1dSLionel Sambuc gen_fail0:
241*0a6a1f1dSLionel Sambuc _dwarf_section_free(dbg, &ds);
242*0a6a1f1dSLionel Sambuc
243*0a6a1f1dSLionel Sambuc return (ret);
244*0a6a1f1dSLionel Sambuc }
245*0a6a1f1dSLionel Sambuc
246*0a6a1f1dSLionel Sambuc void
_dwarf_arange_pro_cleanup(Dwarf_P_Debug dbg)247*0a6a1f1dSLionel Sambuc _dwarf_arange_pro_cleanup(Dwarf_P_Debug dbg)
248*0a6a1f1dSLionel Sambuc {
249*0a6a1f1dSLionel Sambuc Dwarf_ArangeSet as;
250*0a6a1f1dSLionel Sambuc Dwarf_Arange ar, tar;
251*0a6a1f1dSLionel Sambuc
252*0a6a1f1dSLionel Sambuc assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
253*0a6a1f1dSLionel Sambuc if (dbg->dbgp_as == NULL)
254*0a6a1f1dSLionel Sambuc return;
255*0a6a1f1dSLionel Sambuc
256*0a6a1f1dSLionel Sambuc as = dbg->dbgp_as;
257*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(ar, &as->as_arlist, ar_next, tar) {
258*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&as->as_arlist, ar, _Dwarf_Arange, ar_next);
259*0a6a1f1dSLionel Sambuc free(ar);
260*0a6a1f1dSLionel Sambuc }
261*0a6a1f1dSLionel Sambuc free(as);
262*0a6a1f1dSLionel Sambuc dbg->dbgp_as = NULL;
263*0a6a1f1dSLionel Sambuc }
264