1*0a6a1f1dSLionel Sambuc /* $NetBSD: libdwarf_attr.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5*0a6a1f1dSLionel Sambuc * Copyright (c) 2009-2011 Kai Wang
6*0a6a1f1dSLionel Sambuc * All rights reserved.
7*0a6a1f1dSLionel Sambuc *
8*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
9*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
10*0a6a1f1dSLionel Sambuc * are met:
11*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
12*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
13*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
14*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
15*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
16*0a6a1f1dSLionel Sambuc *
17*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc */
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libdwarf_attr.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
33*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libdwarf_attr.c 2966 2013-09-21 14:40:14Z kaiwang27 ");
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc int
_dwarf_attr_alloc(Dwarf_Die die,Dwarf_Attribute * atp,Dwarf_Error * error)36*0a6a1f1dSLionel Sambuc _dwarf_attr_alloc(Dwarf_Die die, Dwarf_Attribute *atp, Dwarf_Error *error)
37*0a6a1f1dSLionel Sambuc {
38*0a6a1f1dSLionel Sambuc Dwarf_Attribute at;
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc assert(die != NULL);
41*0a6a1f1dSLionel Sambuc assert(atp != NULL);
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc if ((at = calloc(1, sizeof(struct _Dwarf_Attribute))) == NULL) {
44*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(die->die_dbg, error, DW_DLE_MEMORY);
45*0a6a1f1dSLionel Sambuc return (DW_DLE_MEMORY);
46*0a6a1f1dSLionel Sambuc }
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambuc *atp = at;
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
51*0a6a1f1dSLionel Sambuc }
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc static int
_dwarf_attr_add(Dwarf_Die die,Dwarf_Attribute atref,Dwarf_Attribute * atp,Dwarf_Error * error)54*0a6a1f1dSLionel Sambuc _dwarf_attr_add(Dwarf_Die die, Dwarf_Attribute atref, Dwarf_Attribute *atp,
55*0a6a1f1dSLionel Sambuc Dwarf_Error *error)
56*0a6a1f1dSLionel Sambuc {
57*0a6a1f1dSLionel Sambuc Dwarf_Attribute at;
58*0a6a1f1dSLionel Sambuc int ret;
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_attr_alloc(die, &at, error)) != DW_DLE_NONE)
61*0a6a1f1dSLionel Sambuc return (ret);
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc memcpy(at, atref, sizeof(struct _Dwarf_Attribute));
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc /* Save a pointer to the attribute name if this is one. */
68*0a6a1f1dSLionel Sambuc if (at->at_attrib == DW_AT_name) {
69*0a6a1f1dSLionel Sambuc switch (at->at_form) {
70*0a6a1f1dSLionel Sambuc case DW_FORM_strp:
71*0a6a1f1dSLionel Sambuc die->die_name = at->u[1].s;
72*0a6a1f1dSLionel Sambuc break;
73*0a6a1f1dSLionel Sambuc case DW_FORM_string:
74*0a6a1f1dSLionel Sambuc die->die_name = at->u[0].s;
75*0a6a1f1dSLionel Sambuc break;
76*0a6a1f1dSLionel Sambuc default:
77*0a6a1f1dSLionel Sambuc break;
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc }
80*0a6a1f1dSLionel Sambuc
81*0a6a1f1dSLionel Sambuc if (atp != NULL)
82*0a6a1f1dSLionel Sambuc *atp = at;
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc
87*0a6a1f1dSLionel Sambuc Dwarf_Attribute
_dwarf_attr_find(Dwarf_Die die,Dwarf_Half attr)88*0a6a1f1dSLionel Sambuc _dwarf_attr_find(Dwarf_Die die, Dwarf_Half attr)
89*0a6a1f1dSLionel Sambuc {
90*0a6a1f1dSLionel Sambuc Dwarf_Attribute at;
91*0a6a1f1dSLionel Sambuc
92*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(at, &die->die_attr, at_next) {
93*0a6a1f1dSLionel Sambuc if (at->at_attrib == attr)
94*0a6a1f1dSLionel Sambuc break;
95*0a6a1f1dSLionel Sambuc }
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc return (at);
98*0a6a1f1dSLionel Sambuc }
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc int
_dwarf_attr_init(Dwarf_Debug dbg,Dwarf_Section * ds,uint64_t * offsetp,int dwarf_size,Dwarf_CU cu,Dwarf_Die die,Dwarf_AttrDef ad,uint64_t form,int indirect,Dwarf_Error * error)101*0a6a1f1dSLionel Sambuc _dwarf_attr_init(Dwarf_Debug dbg, Dwarf_Section *ds, uint64_t *offsetp,
102*0a6a1f1dSLionel Sambuc int dwarf_size, Dwarf_CU cu, Dwarf_Die die, Dwarf_AttrDef ad,
103*0a6a1f1dSLionel Sambuc uint64_t form, int indirect, Dwarf_Error *error)
104*0a6a1f1dSLionel Sambuc {
105*0a6a1f1dSLionel Sambuc struct _Dwarf_Attribute atref;
106*0a6a1f1dSLionel Sambuc Dwarf_Section *str;
107*0a6a1f1dSLionel Sambuc int ret;
108*0a6a1f1dSLionel Sambuc
109*0a6a1f1dSLionel Sambuc ret = DW_DLE_NONE;
110*0a6a1f1dSLionel Sambuc memset(&atref, 0, sizeof(atref));
111*0a6a1f1dSLionel Sambuc atref.at_die = die;
112*0a6a1f1dSLionel Sambuc atref.at_attrib = ad->ad_attrib;
113*0a6a1f1dSLionel Sambuc atref.at_form = indirect ? form : ad->ad_form;
114*0a6a1f1dSLionel Sambuc atref.at_indirect = indirect;
115*0a6a1f1dSLionel Sambuc atref.at_ld = NULL;
116*0a6a1f1dSLionel Sambuc
117*0a6a1f1dSLionel Sambuc switch (form) {
118*0a6a1f1dSLionel Sambuc case DW_FORM_addr:
119*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp,
120*0a6a1f1dSLionel Sambuc cu->cu_pointer_size);
121*0a6a1f1dSLionel Sambuc break;
122*0a6a1f1dSLionel Sambuc case DW_FORM_block:
123*0a6a1f1dSLionel Sambuc case DW_FORM_exprloc:
124*0a6a1f1dSLionel Sambuc atref.u[0].u64 = _dwarf_read_uleb128(ds->ds_data, offsetp);
125*0a6a1f1dSLionel Sambuc atref.u[1].u8p = _dwarf_read_block(ds->ds_data, offsetp,
126*0a6a1f1dSLionel Sambuc atref.u[0].u64);
127*0a6a1f1dSLionel Sambuc break;
128*0a6a1f1dSLionel Sambuc case DW_FORM_block1:
129*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, 1);
130*0a6a1f1dSLionel Sambuc atref.u[1].u8p = _dwarf_read_block(ds->ds_data, offsetp,
131*0a6a1f1dSLionel Sambuc atref.u[0].u64);
132*0a6a1f1dSLionel Sambuc break;
133*0a6a1f1dSLionel Sambuc case DW_FORM_block2:
134*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, 2);
135*0a6a1f1dSLionel Sambuc atref.u[1].u8p = _dwarf_read_block(ds->ds_data, offsetp,
136*0a6a1f1dSLionel Sambuc atref.u[0].u64);
137*0a6a1f1dSLionel Sambuc break;
138*0a6a1f1dSLionel Sambuc case DW_FORM_block4:
139*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, 4);
140*0a6a1f1dSLionel Sambuc atref.u[1].u8p = _dwarf_read_block(ds->ds_data, offsetp,
141*0a6a1f1dSLionel Sambuc atref.u[0].u64);
142*0a6a1f1dSLionel Sambuc break;
143*0a6a1f1dSLionel Sambuc case DW_FORM_data1:
144*0a6a1f1dSLionel Sambuc case DW_FORM_flag:
145*0a6a1f1dSLionel Sambuc case DW_FORM_ref1:
146*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, 1);
147*0a6a1f1dSLionel Sambuc break;
148*0a6a1f1dSLionel Sambuc case DW_FORM_data2:
149*0a6a1f1dSLionel Sambuc case DW_FORM_ref2:
150*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, 2);
151*0a6a1f1dSLionel Sambuc break;
152*0a6a1f1dSLionel Sambuc case DW_FORM_data4:
153*0a6a1f1dSLionel Sambuc case DW_FORM_ref4:
154*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, 4);
155*0a6a1f1dSLionel Sambuc break;
156*0a6a1f1dSLionel Sambuc case DW_FORM_data8:
157*0a6a1f1dSLionel Sambuc case DW_FORM_ref8:
158*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, 8);
159*0a6a1f1dSLionel Sambuc break;
160*0a6a1f1dSLionel Sambuc case DW_FORM_indirect:
161*0a6a1f1dSLionel Sambuc form = _dwarf_read_uleb128(ds->ds_data, offsetp);
162*0a6a1f1dSLionel Sambuc return (_dwarf_attr_init(dbg, ds, offsetp, dwarf_size, cu, die,
163*0a6a1f1dSLionel Sambuc ad, form, 1, error));
164*0a6a1f1dSLionel Sambuc case DW_FORM_ref_addr:
165*0a6a1f1dSLionel Sambuc if (cu->cu_version == 2)
166*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp,
167*0a6a1f1dSLionel Sambuc cu->cu_pointer_size);
168*0a6a1f1dSLionel Sambuc else if (cu->cu_version == 3)
169*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp,
170*0a6a1f1dSLionel Sambuc dwarf_size);
171*0a6a1f1dSLionel Sambuc break;
172*0a6a1f1dSLionel Sambuc case DW_FORM_ref_udata:
173*0a6a1f1dSLionel Sambuc case DW_FORM_udata:
174*0a6a1f1dSLionel Sambuc atref.u[0].u64 = _dwarf_read_uleb128(ds->ds_data, offsetp);
175*0a6a1f1dSLionel Sambuc break;
176*0a6a1f1dSLionel Sambuc case DW_FORM_sdata:
177*0a6a1f1dSLionel Sambuc atref.u[0].s64 = _dwarf_read_sleb128(ds->ds_data, offsetp);
178*0a6a1f1dSLionel Sambuc break;
179*0a6a1f1dSLionel Sambuc case DW_FORM_sec_offset:
180*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, dwarf_size);
181*0a6a1f1dSLionel Sambuc break;
182*0a6a1f1dSLionel Sambuc case DW_FORM_string:
183*0a6a1f1dSLionel Sambuc atref.u[0].s = _dwarf_read_string(ds->ds_data, ds->ds_size,
184*0a6a1f1dSLionel Sambuc offsetp);
185*0a6a1f1dSLionel Sambuc break;
186*0a6a1f1dSLionel Sambuc case DW_FORM_strp:
187*0a6a1f1dSLionel Sambuc atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, dwarf_size);
188*0a6a1f1dSLionel Sambuc str = _dwarf_find_section(dbg, ".debug_str");
189*0a6a1f1dSLionel Sambuc assert(str != NULL);
190*0a6a1f1dSLionel Sambuc atref.u[1].s = (char *) str->ds_data + atref.u[0].u64;
191*0a6a1f1dSLionel Sambuc break;
192*0a6a1f1dSLionel Sambuc case DW_FORM_ref_sig8:
193*0a6a1f1dSLionel Sambuc atref.u[0].u64 = 8;
194*0a6a1f1dSLionel Sambuc atref.u[1].u8p = _dwarf_read_block(ds->ds_data, offsetp,
195*0a6a1f1dSLionel Sambuc atref.u[0].u64);
196*0a6a1f1dSLionel Sambuc break;
197*0a6a1f1dSLionel Sambuc case DW_FORM_flag_present:
198*0a6a1f1dSLionel Sambuc /* This form has no value encoded in the DIE. */
199*0a6a1f1dSLionel Sambuc atref.u[0].u64 = 1;
200*0a6a1f1dSLionel Sambuc break;
201*0a6a1f1dSLionel Sambuc default:
202*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
203*0a6a1f1dSLionel Sambuc ret = DW_DLE_ATTR_FORM_BAD;
204*0a6a1f1dSLionel Sambuc break;
205*0a6a1f1dSLionel Sambuc }
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc if (ret == DW_DLE_NONE) {
208*0a6a1f1dSLionel Sambuc if (form == DW_FORM_block || form == DW_FORM_block1 ||
209*0a6a1f1dSLionel Sambuc form == DW_FORM_block2 || form == DW_FORM_block4) {
210*0a6a1f1dSLionel Sambuc atref.at_block.bl_len = atref.u[0].u64;
211*0a6a1f1dSLionel Sambuc atref.at_block.bl_data = atref.u[1].u8p;
212*0a6a1f1dSLionel Sambuc }
213*0a6a1f1dSLionel Sambuc ret = _dwarf_attr_add(die, &atref, NULL, error);
214*0a6a1f1dSLionel Sambuc }
215*0a6a1f1dSLionel Sambuc
216*0a6a1f1dSLionel Sambuc return (ret);
217*0a6a1f1dSLionel Sambuc }
218*0a6a1f1dSLionel Sambuc
219*0a6a1f1dSLionel Sambuc static int
_dwarf_attr_write(Dwarf_P_Debug dbg,Dwarf_P_Section ds,Dwarf_Rel_Section drs,Dwarf_CU cu,Dwarf_Attribute at,int pass2,Dwarf_Error * error)220*0a6a1f1dSLionel Sambuc _dwarf_attr_write(Dwarf_P_Debug dbg, Dwarf_P_Section ds, Dwarf_Rel_Section drs,
221*0a6a1f1dSLionel Sambuc Dwarf_CU cu, Dwarf_Attribute at, int pass2, Dwarf_Error *error)
222*0a6a1f1dSLionel Sambuc {
223*0a6a1f1dSLionel Sambuc struct _Dwarf_P_Expr_Entry *ee;
224*0a6a1f1dSLionel Sambuc uint64_t value, offset, bs;
225*0a6a1f1dSLionel Sambuc int ret;
226*0a6a1f1dSLionel Sambuc
227*0a6a1f1dSLionel Sambuc assert(dbg != NULL && ds != NULL && cu != NULL && at != NULL);
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc /* Fill in reference to other DIE in the second pass. */
230*0a6a1f1dSLionel Sambuc if (pass2) {
231*0a6a1f1dSLionel Sambuc if (at->at_form != DW_FORM_ref4 && at->at_form != DW_FORM_ref8)
232*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
233*0a6a1f1dSLionel Sambuc if (at->at_refdie == NULL || at->at_offset == 0)
234*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
235*0a6a1f1dSLionel Sambuc offset = at->at_offset;
236*0a6a1f1dSLionel Sambuc dbg->write(ds->ds_data, &offset, at->at_refdie->die_offset,
237*0a6a1f1dSLionel Sambuc at->at_form == DW_FORM_ref4 ? 4 : 8);
238*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
239*0a6a1f1dSLionel Sambuc }
240*0a6a1f1dSLionel Sambuc
241*0a6a1f1dSLionel Sambuc switch (at->at_form) {
242*0a6a1f1dSLionel Sambuc case DW_FORM_addr:
243*0a6a1f1dSLionel Sambuc if (at->at_relsym)
244*0a6a1f1dSLionel Sambuc ret = _dwarf_reloc_entry_add(dbg, drs, ds,
245*0a6a1f1dSLionel Sambuc dwarf_drt_data_reloc, cu->cu_pointer_size,
246*0a6a1f1dSLionel Sambuc ds->ds_size, at->at_relsym, at->u[0].u64, NULL,
247*0a6a1f1dSLionel Sambuc error);
248*0a6a1f1dSLionel Sambuc else
249*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(at->u[0].u64, cu->cu_pointer_size);
250*0a6a1f1dSLionel Sambuc break;
251*0a6a1f1dSLionel Sambuc case DW_FORM_block:
252*0a6a1f1dSLionel Sambuc case DW_FORM_block1:
253*0a6a1f1dSLionel Sambuc case DW_FORM_block2:
254*0a6a1f1dSLionel Sambuc case DW_FORM_block4:
255*0a6a1f1dSLionel Sambuc /* Write block size. */
256*0a6a1f1dSLionel Sambuc if (at->at_form == DW_FORM_block) {
257*0a6a1f1dSLionel Sambuc ret = _dwarf_write_uleb128_alloc(&ds->ds_data,
258*0a6a1f1dSLionel Sambuc &ds->ds_cap, &ds->ds_size, at->u[0].u64, error);
259*0a6a1f1dSLionel Sambuc if (ret != DW_DLE_NONE)
260*0a6a1f1dSLionel Sambuc break;
261*0a6a1f1dSLionel Sambuc } else {
262*0a6a1f1dSLionel Sambuc if (at->at_form == DW_FORM_block1)
263*0a6a1f1dSLionel Sambuc bs = 1;
264*0a6a1f1dSLionel Sambuc else if (at->at_form == DW_FORM_block2)
265*0a6a1f1dSLionel Sambuc bs = 2;
266*0a6a1f1dSLionel Sambuc else
267*0a6a1f1dSLionel Sambuc bs = 4;
268*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(at->u[0].u64, bs);
269*0a6a1f1dSLionel Sambuc if (ret != DW_DLE_NONE)
270*0a6a1f1dSLionel Sambuc break;
271*0a6a1f1dSLionel Sambuc }
272*0a6a1f1dSLionel Sambuc
273*0a6a1f1dSLionel Sambuc /* Keep block data offset for later use. */
274*0a6a1f1dSLionel Sambuc offset = ds->ds_size;
275*0a6a1f1dSLionel Sambuc
276*0a6a1f1dSLionel Sambuc /* Write block data. */
277*0a6a1f1dSLionel Sambuc ret = WRITE_BLOCK(at->u[1].u8p, at->u[0].u64);
278*0a6a1f1dSLionel Sambuc if (ret != DW_DLE_NONE)
279*0a6a1f1dSLionel Sambuc break;
280*0a6a1f1dSLionel Sambuc if (at->at_expr == NULL)
281*0a6a1f1dSLionel Sambuc break;
282*0a6a1f1dSLionel Sambuc
283*0a6a1f1dSLionel Sambuc /* Generate relocation entry for DW_OP_addr expressions. */
284*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(ee, &at->at_expr->pe_eelist, ee_next) {
285*0a6a1f1dSLionel Sambuc if (ee->ee_loc.lr_atom != DW_OP_addr || ee->ee_sym == 0)
286*0a6a1f1dSLionel Sambuc continue;
287*0a6a1f1dSLionel Sambuc ret = _dwarf_reloc_entry_add(dbg, drs, ds,
288*0a6a1f1dSLionel Sambuc dwarf_drt_data_reloc, dbg->dbg_pointer_size,
289*0a6a1f1dSLionel Sambuc offset + ee->ee_loc.lr_offset + 1, ee->ee_sym,
290*0a6a1f1dSLionel Sambuc ee->ee_loc.lr_number, NULL, error);
291*0a6a1f1dSLionel Sambuc if (ret != DW_DLE_NONE)
292*0a6a1f1dSLionel Sambuc break;
293*0a6a1f1dSLionel Sambuc }
294*0a6a1f1dSLionel Sambuc break;
295*0a6a1f1dSLionel Sambuc case DW_FORM_data1:
296*0a6a1f1dSLionel Sambuc case DW_FORM_flag:
297*0a6a1f1dSLionel Sambuc case DW_FORM_ref1:
298*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(at->u[0].u64, 1);
299*0a6a1f1dSLionel Sambuc break;
300*0a6a1f1dSLionel Sambuc case DW_FORM_data2:
301*0a6a1f1dSLionel Sambuc case DW_FORM_ref2:
302*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(at->u[0].u64, 2);
303*0a6a1f1dSLionel Sambuc break;
304*0a6a1f1dSLionel Sambuc case DW_FORM_data4:
305*0a6a1f1dSLionel Sambuc if (at->at_relsym || at->at_relsec != NULL)
306*0a6a1f1dSLionel Sambuc ret = _dwarf_reloc_entry_add(dbg, drs, ds,
307*0a6a1f1dSLionel Sambuc dwarf_drt_data_reloc, 4, ds->ds_size, at->at_relsym,
308*0a6a1f1dSLionel Sambuc at->u[0].u64, at->at_relsec, error);
309*0a6a1f1dSLionel Sambuc else
310*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(at->u[0].u64, 4);
311*0a6a1f1dSLionel Sambuc break;
312*0a6a1f1dSLionel Sambuc case DW_FORM_data8:
313*0a6a1f1dSLionel Sambuc if (at->at_relsym || at->at_relsec != NULL)
314*0a6a1f1dSLionel Sambuc ret = _dwarf_reloc_entry_add(dbg, drs, ds,
315*0a6a1f1dSLionel Sambuc dwarf_drt_data_reloc, 8, ds->ds_size, at->at_relsym,
316*0a6a1f1dSLionel Sambuc at->u[0].u64, at->at_relsec, error);
317*0a6a1f1dSLionel Sambuc else
318*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(at->u[0].u64, 8);
319*0a6a1f1dSLionel Sambuc break;
320*0a6a1f1dSLionel Sambuc case DW_FORM_ref4:
321*0a6a1f1dSLionel Sambuc case DW_FORM_ref8:
322*0a6a1f1dSLionel Sambuc /*
323*0a6a1f1dSLionel Sambuc * The value of ref4 and ref8 could be a reference to another
324*0a6a1f1dSLionel Sambuc * DIE within the CU. And if we don't know the ref DIE's
325*0a6a1f1dSLionel Sambuc * offset at the moement, then we remember at_offset and fill
326*0a6a1f1dSLionel Sambuc * it in the second pass.
327*0a6a1f1dSLionel Sambuc */
328*0a6a1f1dSLionel Sambuc if (at->at_refdie) {
329*0a6a1f1dSLionel Sambuc value = at->at_refdie->die_offset;
330*0a6a1f1dSLionel Sambuc if (value == 0) {
331*0a6a1f1dSLionel Sambuc cu->cu_pass2 = 1;
332*0a6a1f1dSLionel Sambuc at->at_offset = ds->ds_size;
333*0a6a1f1dSLionel Sambuc }
334*0a6a1f1dSLionel Sambuc } else
335*0a6a1f1dSLionel Sambuc value = at->u[0].u64;
336*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(value, at->at_form == DW_FORM_ref4 ? 4 : 8);
337*0a6a1f1dSLionel Sambuc break;
338*0a6a1f1dSLionel Sambuc case DW_FORM_indirect:
339*0a6a1f1dSLionel Sambuc /* TODO. */
340*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
341*0a6a1f1dSLionel Sambuc ret = DW_DLE_ATTR_FORM_BAD;
342*0a6a1f1dSLionel Sambuc break;
343*0a6a1f1dSLionel Sambuc case DW_FORM_ref_addr:
344*0a6a1f1dSLionel Sambuc /* DWARF2 format. */
345*0a6a1f1dSLionel Sambuc if (at->at_relsym)
346*0a6a1f1dSLionel Sambuc ret = _dwarf_reloc_entry_add(dbg, drs, ds,
347*0a6a1f1dSLionel Sambuc dwarf_drt_data_reloc, cu->cu_pointer_size,
348*0a6a1f1dSLionel Sambuc ds->ds_size, at->at_relsym, at->u[0].u64, NULL,
349*0a6a1f1dSLionel Sambuc error);
350*0a6a1f1dSLionel Sambuc else
351*0a6a1f1dSLionel Sambuc ret = WRITE_VALUE(at->u[0].u64, cu->cu_pointer_size);
352*0a6a1f1dSLionel Sambuc break;
353*0a6a1f1dSLionel Sambuc case DW_FORM_ref_udata:
354*0a6a1f1dSLionel Sambuc case DW_FORM_udata:
355*0a6a1f1dSLionel Sambuc ret = WRITE_ULEB128(at->u[0].u64);
356*0a6a1f1dSLionel Sambuc break;
357*0a6a1f1dSLionel Sambuc case DW_FORM_sdata:
358*0a6a1f1dSLionel Sambuc ret = WRITE_SLEB128(at->u[0].s64);
359*0a6a1f1dSLionel Sambuc break;
360*0a6a1f1dSLionel Sambuc case DW_FORM_string:
361*0a6a1f1dSLionel Sambuc assert(at->u[0].s != NULL);
362*0a6a1f1dSLionel Sambuc ret = WRITE_STRING(at->u[0].s);
363*0a6a1f1dSLionel Sambuc break;
364*0a6a1f1dSLionel Sambuc case DW_FORM_strp:
365*0a6a1f1dSLionel Sambuc ret = _dwarf_reloc_entry_add(dbg, drs, ds, dwarf_drt_data_reloc,
366*0a6a1f1dSLionel Sambuc 4, ds->ds_size, 0, at->u[0].u64, ".debug_str", error);
367*0a6a1f1dSLionel Sambuc break;
368*0a6a1f1dSLionel Sambuc default:
369*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
370*0a6a1f1dSLionel Sambuc ret = DW_DLE_ATTR_FORM_BAD;
371*0a6a1f1dSLionel Sambuc break;
372*0a6a1f1dSLionel Sambuc }
373*0a6a1f1dSLionel Sambuc
374*0a6a1f1dSLionel Sambuc return (ret);
375*0a6a1f1dSLionel Sambuc }
376*0a6a1f1dSLionel Sambuc
377*0a6a1f1dSLionel Sambuc int
_dwarf_add_AT_dataref(Dwarf_P_Debug dbg,Dwarf_P_Die die,Dwarf_Half attr,Dwarf_Unsigned pc_value,Dwarf_Unsigned sym_index,const char * secname,Dwarf_P_Attribute * atp,Dwarf_Error * error)378*0a6a1f1dSLionel Sambuc _dwarf_add_AT_dataref(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
379*0a6a1f1dSLionel Sambuc Dwarf_Unsigned pc_value, Dwarf_Unsigned sym_index, const char *secname,
380*0a6a1f1dSLionel Sambuc Dwarf_P_Attribute *atp, Dwarf_Error *error)
381*0a6a1f1dSLionel Sambuc {
382*0a6a1f1dSLionel Sambuc Dwarf_Attribute at;
383*0a6a1f1dSLionel Sambuc int ret;
384*0a6a1f1dSLionel Sambuc
385*0a6a1f1dSLionel Sambuc assert(dbg != NULL && die != NULL);
386*0a6a1f1dSLionel Sambuc
387*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_attr_alloc(die, &at, error)) != DW_DLE_NONE)
388*0a6a1f1dSLionel Sambuc return (ret);
389*0a6a1f1dSLionel Sambuc
390*0a6a1f1dSLionel Sambuc at->at_die = die;
391*0a6a1f1dSLionel Sambuc at->at_attrib = attr;
392*0a6a1f1dSLionel Sambuc if (dbg->dbg_pointer_size == 4)
393*0a6a1f1dSLionel Sambuc at->at_form = DW_FORM_data4;
394*0a6a1f1dSLionel Sambuc else
395*0a6a1f1dSLionel Sambuc at->at_form = DW_FORM_data8;
396*0a6a1f1dSLionel Sambuc at->at_relsym = sym_index;
397*0a6a1f1dSLionel Sambuc at->at_relsec = secname;
398*0a6a1f1dSLionel Sambuc at->u[0].u64 = pc_value;
399*0a6a1f1dSLionel Sambuc
400*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
401*0a6a1f1dSLionel Sambuc
402*0a6a1f1dSLionel Sambuc if (atp)
403*0a6a1f1dSLionel Sambuc *atp = at;
404*0a6a1f1dSLionel Sambuc
405*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
406*0a6a1f1dSLionel Sambuc }
407*0a6a1f1dSLionel Sambuc
408*0a6a1f1dSLionel Sambuc int
_dwarf_add_string_attr(Dwarf_P_Die die,Dwarf_P_Attribute * atp,Dwarf_Half attr,char * string,Dwarf_Error * error)409*0a6a1f1dSLionel Sambuc _dwarf_add_string_attr(Dwarf_P_Die die, Dwarf_P_Attribute *atp, Dwarf_Half attr,
410*0a6a1f1dSLionel Sambuc char *string, Dwarf_Error *error)
411*0a6a1f1dSLionel Sambuc {
412*0a6a1f1dSLionel Sambuc Dwarf_Attribute at;
413*0a6a1f1dSLionel Sambuc Dwarf_Debug dbg;
414*0a6a1f1dSLionel Sambuc int ret;
415*0a6a1f1dSLionel Sambuc
416*0a6a1f1dSLionel Sambuc dbg = die != NULL ? die->die_dbg : NULL;
417*0a6a1f1dSLionel Sambuc
418*0a6a1f1dSLionel Sambuc assert(atp != NULL);
419*0a6a1f1dSLionel Sambuc
420*0a6a1f1dSLionel Sambuc if (die == NULL || string == NULL) {
421*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
422*0a6a1f1dSLionel Sambuc return (DW_DLE_ARGUMENT);
423*0a6a1f1dSLionel Sambuc }
424*0a6a1f1dSLionel Sambuc
425*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_attr_alloc(die, &at, error)) != DW_DLE_NONE)
426*0a6a1f1dSLionel Sambuc return (ret);
427*0a6a1f1dSLionel Sambuc
428*0a6a1f1dSLionel Sambuc at->at_die = die;
429*0a6a1f1dSLionel Sambuc at->at_attrib = attr;
430*0a6a1f1dSLionel Sambuc at->at_form = DW_FORM_strp;
431*0a6a1f1dSLionel Sambuc if ((ret = _dwarf_strtab_add(dbg, string, &at->u[0].u64,
432*0a6a1f1dSLionel Sambuc error)) != DW_DLE_NONE) {
433*0a6a1f1dSLionel Sambuc free(at);
434*0a6a1f1dSLionel Sambuc return (ret);
435*0a6a1f1dSLionel Sambuc }
436*0a6a1f1dSLionel Sambuc at->u[1].s = _dwarf_strtab_get_table(dbg) + at->u[0].u64;
437*0a6a1f1dSLionel Sambuc
438*0a6a1f1dSLionel Sambuc *atp = at;
439*0a6a1f1dSLionel Sambuc
440*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
441*0a6a1f1dSLionel Sambuc
442*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
443*0a6a1f1dSLionel Sambuc }
444*0a6a1f1dSLionel Sambuc
445*0a6a1f1dSLionel Sambuc int
_dwarf_attr_gen(Dwarf_P_Debug dbg,Dwarf_P_Section ds,Dwarf_Rel_Section drs,Dwarf_CU cu,Dwarf_Die die,int pass2,Dwarf_Error * error)446*0a6a1f1dSLionel Sambuc _dwarf_attr_gen(Dwarf_P_Debug dbg, Dwarf_P_Section ds, Dwarf_Rel_Section drs,
447*0a6a1f1dSLionel Sambuc Dwarf_CU cu, Dwarf_Die die, int pass2, Dwarf_Error *error)
448*0a6a1f1dSLionel Sambuc {
449*0a6a1f1dSLionel Sambuc Dwarf_Attribute at;
450*0a6a1f1dSLionel Sambuc int ret;
451*0a6a1f1dSLionel Sambuc
452*0a6a1f1dSLionel Sambuc assert(dbg != NULL && ds != NULL && cu != NULL && die != NULL);
453*0a6a1f1dSLionel Sambuc
454*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(at, &die->die_attr, at_next) {
455*0a6a1f1dSLionel Sambuc ret = _dwarf_attr_write(dbg, ds, drs, cu, at, pass2, error);
456*0a6a1f1dSLionel Sambuc if (ret != DW_DLE_NONE)
457*0a6a1f1dSLionel Sambuc return (ret);
458*0a6a1f1dSLionel Sambuc }
459*0a6a1f1dSLionel Sambuc
460*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
461*0a6a1f1dSLionel Sambuc }
462