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) {
51 return (DW_DLV_BADADDR);
53 at->u[0].u64 = loc_expr->pe_length;
54 at->u[1].u8p = loc_expr->pe_block;
55 if (loc_expr->pe_length <= UCHAR_MAX)
56 at->at_form = DW_FORM_block1;
57 else if (loc_expr->pe_length <= USHRT_MAX)
58 at->at_form = DW_FORM_block2;
59 else if (loc_expr->pe_length <= UINT_MAX)
60 at->at_form = DW_FORM_block4;
62 at->at_form = DW_FORM_block;
64 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
70 dwarf_add_AT_name(Dwarf_P_Die die, char *name, Dwarf_Error *error)
74 if (_dwarf_add_string_attr(die, &at, DW_AT_name, name, error) !=
76 return (DW_DLV_BADADDR);
82 dwarf_add_AT_comp_dir(Dwarf_P_Die die, char *dir, Dwarf_Error *error)
86 if (_dwarf_add_string_attr(die, &at, DW_AT_comp_dir, dir, error) !=
88 return (DW_DLV_BADADDR);
94 dwarf_add_AT_producer(Dwarf_P_Die die, char *producer, Dwarf_Error *error)
98 if (_dwarf_add_string_attr(die, &at, DW_AT_producer, producer, error) !=
100 return (DW_DLV_BADADDR);
106 dwarf_add_AT_const_value_signedint(Dwarf_P_Die die, Dwarf_Signed value,
112 dbg = die != NULL ? die->die_dbg : NULL;
115 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
116 return (DW_DLV_BADADDR);
119 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
120 return (DW_DLV_BADADDR);
123 at->at_attrib = DW_AT_const_value;
124 at->at_form = DW_FORM_sdata;
125 at->u[0].s64 = value;
127 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
133 dwarf_add_AT_const_value_unsignedint(Dwarf_P_Die die, Dwarf_Unsigned value,
139 dbg = die != NULL ? die->die_dbg : NULL;
142 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
143 return (DW_DLV_BADADDR);
146 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
147 return (DW_DLV_BADADDR);
150 at->at_attrib = DW_AT_const_value;
151 at->at_form = DW_FORM_udata;
152 at->u[0].u64 = value;
154 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
160 dwarf_add_AT_const_value_string(Dwarf_P_Die die, char *string,
165 if (_dwarf_add_string_attr(die, &at, DW_AT_const_value, string,
166 error) != DW_DLE_NONE)
167 return (DW_DLV_BADADDR);
173 dwarf_add_AT_targ_address(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
174 Dwarf_Unsigned pc_value, Dwarf_Signed sym_index, Dwarf_Error *error)
177 return (dwarf_add_AT_targ_address_b(dbg, die, attr, pc_value, sym_index,
182 dwarf_add_AT_targ_address_b(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
183 Dwarf_Unsigned pc_value, Dwarf_Unsigned sym_index, Dwarf_Error *error)
187 if (dbg == NULL || die == NULL) {
188 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
189 return (DW_DLV_BADADDR);
192 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
193 return (DW_DLV_BADADDR);
196 at->at_attrib = attr;
197 at->at_form = DW_FORM_addr;
198 at->at_relsym = sym_index;
199 at->u[0].u64 = pc_value;
201 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
207 dwarf_add_AT_dataref(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
208 Dwarf_Unsigned pc_value, Dwarf_Unsigned sym_index, Dwarf_Error *error)
213 if (dbg == NULL || die == NULL) {
214 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
215 return (DW_DLV_BADADDR);
218 ret = _dwarf_add_AT_dataref(dbg, die, attr, pc_value, sym_index,
220 if (ret != DW_DLE_NONE)
221 return (DW_DLV_BADADDR);
228 dwarf_add_AT_ref_address(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
229 Dwarf_Unsigned pc_value, Dwarf_Unsigned sym_index, Dwarf_Error *error)
233 if (dbg == NULL || die == NULL) {
234 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
235 return (DW_DLV_BADADDR);
238 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
239 return (DW_DLV_BADADDR);
242 at->at_attrib = attr;
243 at->at_form = DW_FORM_ref_addr;
244 at->at_relsym = sym_index;
245 at->u[0].u64 = pc_value;
247 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
253 dwarf_add_AT_unsigned_const(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
254 Dwarf_Unsigned value, Dwarf_Error *error)
258 if (dbg == NULL || die == NULL) {
259 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
260 return (DW_DLV_BADADDR);
263 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
264 return (DW_DLV_BADADDR);
267 at->at_attrib = attr;
268 at->u[0].u64 = value;
270 if (value <= UCHAR_MAX)
271 at->at_form = DW_FORM_data1;
272 else if (value <= USHRT_MAX)
273 at->at_form = DW_FORM_data2;
274 else if (value <= UINT_MAX)
275 at->at_form = DW_FORM_data4;
277 at->at_form = DW_FORM_data8;
279 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
285 dwarf_add_AT_signed_const(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
286 Dwarf_Signed value, Dwarf_Error *error)
290 if (dbg == NULL || die == NULL) {
291 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
292 return (DW_DLV_BADADDR);
295 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
296 return (DW_DLV_BADADDR);
299 at->at_attrib = attr;
300 at->u[0].u64 = value;
302 if (value >= SCHAR_MIN && value <= SCHAR_MAX)
303 at->at_form = DW_FORM_data1;
304 else if (value >= SHRT_MIN && value <= SHRT_MAX)
305 at->at_form = DW_FORM_data2;
306 else if (value >= INT_MIN && value <= INT_MAX)
307 at->at_form = DW_FORM_data4;
309 at->at_form = DW_FORM_data8;
311 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
317 dwarf_add_AT_reference(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
318 Dwarf_P_Die ref_die, Dwarf_Error *error)
322 if (dbg == NULL || die == NULL) {
323 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
324 return (DW_DLV_BADADDR);
327 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
328 return (DW_DLV_BADADDR);
331 at->at_attrib = attr;
332 if (dbg->dbg_offset_size == 4)
333 at->at_form = DW_FORM_ref4;
335 at->at_form = DW_FORM_ref8;
337 at->at_refdie = ref_die;
339 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
345 dwarf_add_AT_flag(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
346 Dwarf_Small flag, Dwarf_Error *error)
350 if (dbg == NULL || die == NULL) {
351 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
352 return (DW_DLV_BADADDR);
355 if (_dwarf_attr_alloc(die, &at, error) != DW_DLE_NONE)
356 return (DW_DLV_BADADDR);
359 at->at_attrib = attr;
360 at->at_form = DW_FORM_flag;
361 at->u[0].u64 = flag ? 1 : 0;
363 STAILQ_INSERT_TAIL(&die->die_attr, at, at_next);
369 dwarf_add_AT_string(Dwarf_P_Debug dbg, Dwarf_P_Die die, Dwarf_Half attr,
370 char *string, Dwarf_Error *error)
374 if (dbg == NULL || die == NULL) {
375 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
376 return (DW_DLV_BADADDR);
379 /* XXX Add DW_FORM_string style string instead? */
381 if (_dwarf_add_string_attr(die, &at, attr, string, error) !=
383 return (DW_DLV_BADADDR);