2 * Copyright (c) 2009 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: dwarf_pro_attr.c 2074 2011-10-27 03:34:33Z jkoshy $");
32 dwarf_add_AT_location_expr(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
33 Dwarf_P_Expr loc_expr, Dwarf_Error *error)
37 if (dbg == NULL || die == NULL || loc_expr == NULL) {
38 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
39 return (DW_DLV_BADADDR);
42 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
43 return (DW_DLV_BADADDR);
47 at->at_expr = loc_expr;
49 if (_dwarf_expr_into_block(loc_expr, error) != DW_DLE_NONE)
50 return (DW_DLV_BADADDR);
51 at->u[0].u64 = loc_expr->pe_length;
52 at->u[1].u8p = loc_expr->pe_block;
53 if (loc_expr->pe_length <= UCHAR_MAX)
54 at->at_form = DW_FORM_block1;
55 else if (loc_expr->pe_length <= USHRT_MAX)
56 at->at_form = DW_FORM_block2;
57 else if (loc_expr->pe_length <= UINT_MAX)
58 at->at_form = DW_FORM_block4;
60 at->at_form = DW_FORM_block;
62 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
68 dwarf_add_AT_name(Dwarf_P_Die die, char *name, Dwarf_Error *error)
72 if (_dwarf_add_string_attr(die, &at, DW_AT_name, name, error) !=
74 return (DW_DLV_BADADDR);
80 dwarf_add_AT_comp_dir(Dwarf_P_Die die, char *dir, Dwarf_Error *error)
84 if (_dwarf_add_string_attr(die, &at, DW_AT_comp_dir, dir, error) !=
86 return (DW_DLV_BADADDR);
92 dwarf_add_AT_producer(Dwarf_P_Die die, char *producer, Dwarf_Error *error)
96 if (_dwarf_add_string_attr(die, &at, DW_AT_producer, producer, error) !=
98 return (DW_DLV_BADADDR);
104 dwarf_add_AT_const_value_signedint(Dwarf_P_Die die, Dwarf_Signed value,
110 dbg = die != NULL ? die->die_dbg : NULL;
113 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
114 return (DW_DLV_BADADDR);
117 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
118 return (DW_DLV_BADADDR);
121 at->at_attrib = DW_AT_const_value;
122 at->at_form = DW_FORM_sdata;
123 at->u[0].s64 = value;
125 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
131 dwarf_add_AT_const_value_unsignedint(Dwarf_P_Die die, Dwarf_Unsigned value,
137 dbg = die != NULL ? die->die_dbg : NULL;
140 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
141 return (DW_DLV_BADADDR);
144 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
145 return (DW_DLV_BADADDR);
148 at->at_attrib = DW_AT_const_value;
149 at->at_form = DW_FORM_udata;
150 at->u[0].u64 = value;
152 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
158 dwarf_add_AT_const_value_string(Dwarf_P_Die die, char *string,
163 if (_dwarf_add_string_attr(die, &at, DW_AT_const_value, string,
164 error) != DW_DLE_NONE)
165 return (DW_DLV_BADADDR);
171 dwarf_add_AT_targ_address(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
172 Dwarf_Unsigned pc_value, Dwarf_Signed sym_index, Dwarf_Error *error)
175 return (dwarf_add_AT_targ_address_b(dbg, die, attr, pc_value, sym_index,
180 dwarf_add_AT_targ_address_b(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
181 Dwarf_Unsigned pc_value, Dwarf_Unsigned sym_index, Dwarf_Error *error)
185 if (dbg == NULL || die == NULL) {
186 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
187 return (DW_DLV_BADADDR);
190 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
191 return (DW_DLV_BADADDR);
194 at->at_attrib = attr;
195 at->at_form = DW_FORM_addr;
196 at->at_relsym = sym_index;
197 at->u[0].u64 = pc_value;
199 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
205 dwarf_add_AT_dataref(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
206 Dwarf_Unsigned pc_value, Dwarf_Unsigned sym_index, Dwarf_Error *error)
211 if (dbg == NULL || die == NULL) {
212 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
213 return (DW_DLV_BADADDR);
216 ret = _dwarf_add_AT_dataref(dbg, die, attr, pc_value, sym_index,
218 if (ret != DW_DLE_NONE)
219 return (DW_DLV_BADADDR);
226 dwarf_add_AT_ref_address(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
227 Dwarf_Unsigned pc_value, Dwarf_Unsigned sym_index, Dwarf_Error *error)
231 if (dbg == NULL || die == NULL) {
232 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
233 return (DW_DLV_BADADDR);
236 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
237 return (DW_DLV_BADADDR);
240 at->at_attrib = attr;
241 at->at_form = DW_FORM_ref_addr;
242 at->at_relsym = sym_index;
243 at->u[0].u64 = pc_value;
245 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
251 dwarf_add_AT_unsigned_const(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
252 Dwarf_Unsigned value, Dwarf_Error *error)
256 if (dbg == NULL || die == NULL) {
257 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
258 return (DW_DLV_BADADDR);
261 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
262 return (DW_DLV_BADADDR);
265 at->at_attrib = attr;
266 at->u[0].u64 = value;
268 if (value <= UCHAR_MAX)
269 at->at_form = DW_FORM_data1;
270 else if (value <= USHRT_MAX)
271 at->at_form = DW_FORM_data2;
272 else if (value <= UINT_MAX)
273 at->at_form = DW_FORM_data4;
275 at->at_form = DW_FORM_data8;
277 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
283 dwarf_add_AT_signed_const(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
284 Dwarf_Signed value, Dwarf_Error *error)
288 if (dbg == NULL || die == NULL) {
289 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
290 return (DW_DLV_BADADDR);
293 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
294 return (DW_DLV_BADADDR);
297 at->at_attrib = attr;
298 at->u[0].u64 = value;
300 if (value >= SCHAR_MIN && value <= SCHAR_MAX)
301 at->at_form = DW_FORM_data1;
302 else if (value >= SHRT_MIN && value <= SHRT_MAX)
303 at->at_form = DW_FORM_data2;
304 else if (value >= INT_MIN && value <= INT_MAX)
305 at->at_form = DW_FORM_data4;
307 at->at_form = DW_FORM_data8;
309 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
315 dwarf_add_AT_reference(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
316 Dwarf_P_Die ref_die, Dwarf_Error *error)
320 if (dbg == NULL || die == NULL) {
321 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
322 return (DW_DLV_BADADDR);
325 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
326 return (DW_DLV_BADADDR);
329 at->at_attrib = attr;
330 if (dbg->dbg_offset_size == 4)
331 at->at_form = DW_FORM_ref4;
333 at->at_form = DW_FORM_ref8;
335 at->at_refdie = ref_die;
337 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
343 dwarf_add_AT_flag(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
344 Dwarf_Small flag, Dwarf_Error *error)
348 if (dbg == NULL || die == NULL) {
349 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
350 return (DW_DLV_BADADDR);
353 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
354 return (DW_DLV_BADADDR);
357 at->at_attrib = attr;
358 at->at_form = DW_FORM_flag;
359 at->u[0].u64 = flag ? 1 : 0;
361 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
367 dwarf_add_AT_string(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
368 char *string, Dwarf_Error *error)
372 if (dbg == NULL || die == NULL) {
373 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
374 return (DW_DLV_BADADDR);
377 /* XXX Add DW_FORM_string style string instead? */
379 if (_dwarf_add_string_attr(die, &at, attr, string, error) !=
381 return (DW_DLV_BADADDR);