]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/coregrind/m_debuginfo/d3basics.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / coregrind / m_debuginfo / d3basics.c
1
2 /*--------------------------------------------------------------------*/
3 /*--- Basic definitions and helper functions for DWARF3.           ---*/
4 /*---                                                   d3basics.c ---*/
5 /*--------------------------------------------------------------------*/
6
7 /*
8    This file is part of Valgrind, a dynamic binary instrumentation
9    framework.
10
11    Copyright (C) 2008-2010 OpenWorks LLP
12       info@open-works.co.uk
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of the
17    License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful, but
20    WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27    02111-1307, USA.
28
29    The GNU General Public License is contained in the file COPYING.
30
31    Neither the names of the U.S. Department of Energy nor the
32    University of California nor the names of its contributors may be
33    used to endorse or promote products derived from this software
34    without prior written permission.
35 */
36
37 #include "pub_core_basics.h"
38 #include "pub_core_debuginfo.h"
39 #include "pub_core_libcassert.h"
40 #include "pub_core_libcprint.h"
41 #include "pub_core_options.h"
42 #include "pub_core_xarray.h"
43
44 #include "pub_core_vki.h"       /* VKI_PROT_READ */
45 #include "pub_core_aspacemgr.h" /* VG_(is_valid_for_client) */
46
47 #include "priv_misc.h"
48 #include "priv_d3basics.h"      /* self */
49 #include "priv_storage.h"
50
51 HChar* ML_(pp_DW_children) ( DW_children hashch )
52 {
53    switch (hashch) {
54       case DW_children_no:  return "no children";
55       case DW_children_yes: return "has children";
56       default:              return "DW_children_???";
57    }
58 }
59
60 HChar* ML_(pp_DW_TAG) ( DW_TAG tag )
61 {
62    switch (tag) {
63       case DW_TAG_padding:            return "DW_TAG_padding";
64       case DW_TAG_array_type:         return "DW_TAG_array_type";
65       case DW_TAG_class_type:         return "DW_TAG_class_type";
66       case DW_TAG_entry_point:        return "DW_TAG_entry_point";
67       case DW_TAG_enumeration_type:   return "DW_TAG_enumeration_type";
68       case DW_TAG_formal_parameter:   return "DW_TAG_formal_parameter";
69       case DW_TAG_imported_declaration: 
70          return "DW_TAG_imported_declaration";
71       case DW_TAG_label:              return "DW_TAG_label";
72       case DW_TAG_lexical_block:      return "DW_TAG_lexical_block";
73       case DW_TAG_member:             return "DW_TAG_member";
74       case DW_TAG_pointer_type:       return "DW_TAG_pointer_type";
75       case DW_TAG_reference_type:     return "DW_TAG_reference_type";
76       case DW_TAG_compile_unit:       return "DW_TAG_compile_unit";
77       case DW_TAG_string_type:        return "DW_TAG_string_type";
78       case DW_TAG_structure_type:     return "DW_TAG_structure_type";
79       case DW_TAG_subroutine_type:    return "DW_TAG_subroutine_type";
80       case DW_TAG_typedef:            return "DW_TAG_typedef";
81       case DW_TAG_union_type:         return "DW_TAG_union_type";
82       case DW_TAG_unspecified_parameters: 
83          return "DW_TAG_unspecified_parameters";
84       case DW_TAG_variant:            return "DW_TAG_variant";
85       case DW_TAG_common_block:       return "DW_TAG_common_block";
86       case DW_TAG_common_inclusion:   return "DW_TAG_common_inclusion";
87       case DW_TAG_inheritance:        return "DW_TAG_inheritance";
88       case DW_TAG_inlined_subroutine:
89          return "DW_TAG_inlined_subroutine";
90       case DW_TAG_module:             return "DW_TAG_module";
91       case DW_TAG_ptr_to_member_type: return "DW_TAG_ptr_to_member_type";
92       case DW_TAG_set_type:           return "DW_TAG_set_type";
93       case DW_TAG_subrange_type:      return "DW_TAG_subrange_type";
94       case DW_TAG_with_stmt:          return "DW_TAG_with_stmt";
95       case DW_TAG_access_declaration: return "DW_TAG_access_declaration";
96       case DW_TAG_base_type:          return "DW_TAG_base_type";
97       case DW_TAG_catch_block:        return "DW_TAG_catch_block";
98       case DW_TAG_const_type:         return "DW_TAG_const_type";
99       case DW_TAG_constant:           return "DW_TAG_constant";
100       case DW_TAG_enumerator:         return "DW_TAG_enumerator";
101       case DW_TAG_file_type:          return "DW_TAG_file_type";
102       case DW_TAG_friend:             return "DW_TAG_friend";
103       case DW_TAG_namelist:           return "DW_TAG_namelist";
104       case DW_TAG_namelist_item:      return "DW_TAG_namelist_item";
105       case DW_TAG_packed_type:        return "DW_TAG_packed_type";
106       case DW_TAG_subprogram:         return "DW_TAG_subprogram";
107       case DW_TAG_template_type_param:
108          return "DW_TAG_template_type_param";
109       case DW_TAG_template_value_param:
110          return "DW_TAG_template_value_param";
111       case DW_TAG_thrown_type:        return "DW_TAG_thrown_type";
112       case DW_TAG_try_block:          return "DW_TAG_try_block";
113       case DW_TAG_variant_part:       return "DW_TAG_variant_part";
114       case DW_TAG_variable:           return "DW_TAG_variable";
115       case DW_TAG_volatile_type:      return "DW_TAG_volatile_type";
116       /* DWARF 3.  */
117       case DW_TAG_dwarf_procedure:    return "DW_TAG_dwarf_procedure";
118       case DW_TAG_restrict_type:      return "DW_TAG_restrict_type";
119       case DW_TAG_interface_type:     return "DW_TAG_interface_type";
120       case DW_TAG_namespace:          return "DW_TAG_namespace";
121       case DW_TAG_imported_module:    return "DW_TAG_imported_module";
122       case DW_TAG_unspecified_type:   return "DW_TAG_unspecified_type";
123       case DW_TAG_partial_unit:       return "DW_TAG_partial_unit";
124       case DW_TAG_imported_unit:      return "DW_TAG_imported_unit";
125       case DW_TAG_condition:          return "DW_TAG_condition";
126       case DW_TAG_shared_type:        return "DW_TAG_shared_type";
127       /* DWARF 4.  */
128       case DW_TAG_type_unit:          return "DW_TAG_type_unit";
129       case DW_TAG_rvalue_reference_type: return "DW_TAG_rvalue_reference_type";
130       case DW_TAG_template_alias:     return "DW_TAG_template_alias";
131       /* SGI/MIPS Extensions.  */
132       case DW_TAG_MIPS_loop:          return "DW_TAG_MIPS_loop";
133       /* HP extensions.  See:
134          ftp://ftp.hp.com/pub/lang/tools/WDB/wdb-4.0.tar.gz .  */
135       case DW_TAG_HP_array_descriptor:
136          return "DW_TAG_HP_array_descriptor";
137       /* GNU extensions.  */
138       case DW_TAG_format_label:       return "DW_TAG_format_label";
139       case DW_TAG_function_template:  return "DW_TAG_function_template";
140       case DW_TAG_class_template:     return "DW_TAG_class_template";
141       case DW_TAG_GNU_BINCL:          return "DW_TAG_GNU_BINCL";
142       case DW_TAG_GNU_EINCL:          return "DW_TAG_GNU_EINCL";
143       /* Extensions for UPC.  See: http://upc.gwu.edu/~upc.  */
144       case DW_TAG_upc_shared_type:    return "DW_TAG_upc_shared_type";
145       case DW_TAG_upc_strict_type:    return "DW_TAG_upc_strict_type";
146       case DW_TAG_upc_relaxed_type:   return "DW_TAG_upc_relaxed_type";
147       /* PGI (STMicroelectronics) extensions.  No documentation available.  */
148       case DW_TAG_PGI_kanji_type:     return "DW_TAG_PGI_kanji_type";
149       case DW_TAG_PGI_interface_block:
150          return "DW_TAG_PGI_interface_block";
151       default:                        return "DW_TAG_???";
152    }
153 }
154
155 HChar* ML_(pp_DW_FORM) ( DW_FORM form )
156 {
157    switch (form) {
158       case DW_FORM_addr:      return "DW_FORM_addr";
159       case DW_FORM_block2:    return "DW_FORM_block2";
160       case DW_FORM_block4:    return "DW_FORM_block4";
161       case DW_FORM_data2:     return "DW_FORM_data2";
162       case DW_FORM_data4:     return "DW_FORM_data4";
163       case DW_FORM_data8:     return "DW_FORM_data8";
164       case DW_FORM_string:    return "DW_FORM_string";
165       case DW_FORM_block:     return "DW_FORM_block";
166       case DW_FORM_block1:    return "DW_FORM_block1";
167       case DW_FORM_data1:     return "DW_FORM_data1";
168       case DW_FORM_flag:      return "DW_FORM_flag";
169       case DW_FORM_sdata:     return "DW_FORM_sdata";
170       case DW_FORM_strp:      return "DW_FORM_strp";
171       case DW_FORM_udata:     return "DW_FORM_udata";
172       case DW_FORM_ref_addr:  return "DW_FORM_ref_addr";
173       case DW_FORM_ref1:      return "DW_FORM_ref1";
174       case DW_FORM_ref2:      return "DW_FORM_ref2";
175       case DW_FORM_ref4:      return "DW_FORM_ref4";
176       case DW_FORM_ref8:      return "DW_FORM_ref8";
177       case DW_FORM_ref_udata: return "DW_FORM_ref_udata";
178       case DW_FORM_indirect:  return "DW_FORM_indirect";
179       case DW_FORM_sec_offset:return "DW_FORM_sec_offset";
180       case DW_FORM_exprloc:   return "DW_FORM_exprloc";
181       case DW_FORM_flag_present:return "DW_FORM_flag_present";
182       case DW_FORM_ref_sig8:  return "DW_FORM_ref_sig8";
183       default:                return "DW_FORM_???";
184    }
185 }
186
187 HChar* ML_(pp_DW_AT) ( DW_AT attr )
188 {
189    switch (attr) {
190       case DW_AT_sibling:             return "DW_AT_sibling";
191       case DW_AT_location:            return "DW_AT_location";
192       case DW_AT_name: return "DW_AT_name";
193       case DW_AT_ordering: return "DW_AT_ordering";
194       case DW_AT_subscr_data: return "DW_AT_subscr_data";
195       case DW_AT_byte_size: return "DW_AT_byte_size";
196       case DW_AT_bit_offset: return "DW_AT_bit_offset";
197       case DW_AT_bit_size: return "DW_AT_bit_size";
198       case DW_AT_element_list: return "DW_AT_element_list";
199       case DW_AT_stmt_list: return "DW_AT_stmt_list";
200       case DW_AT_low_pc: return "DW_AT_low_pc";
201       case DW_AT_high_pc: return "DW_AT_high_pc";
202       case DW_AT_language: return "DW_AT_language";
203       case DW_AT_member: return "DW_AT_member";
204       case DW_AT_discr: return "DW_AT_discr";
205       case DW_AT_discr_value: return "DW_AT_discr_value";
206       case DW_AT_visibility: return "DW_AT_visibility";
207       case DW_AT_import: return "DW_AT_import";
208       case DW_AT_string_length: return "DW_AT_string_length";
209       case DW_AT_common_reference: return "DW_AT_common_reference";
210       case DW_AT_comp_dir: return "DW_AT_comp_dir";
211       case DW_AT_const_value: return "DW_AT_const_value";
212       case DW_AT_containing_type: return "DW_AT_containing_type";
213       case DW_AT_default_value: return "DW_AT_default_value";
214       case DW_AT_inline: return "DW_AT_inline";
215       case DW_AT_is_optional: return "DW_AT_is_optional";
216       case DW_AT_lower_bound: return "DW_AT_lower_bound";
217       case DW_AT_producer: return "DW_AT_producer";
218       case DW_AT_prototyped: return "DW_AT_prototyped";
219       case DW_AT_return_addr: return "DW_AT_return_addr";
220       case DW_AT_start_scope: return "DW_AT_start_scope";
221       case DW_AT_stride_size: return "DW_AT_stride_size";
222       case DW_AT_upper_bound: return "DW_AT_upper_bound";
223       case DW_AT_abstract_origin: return "DW_AT_abstract_origin";
224       case DW_AT_accessibility: return "DW_AT_accessibility";
225       case DW_AT_address_class: return "DW_AT_address_class";
226       case DW_AT_artificial: return "DW_AT_artificial";
227       case DW_AT_base_types: return "DW_AT_base_types";
228       case DW_AT_calling_convention: return "DW_AT_calling_convention";
229       case DW_AT_count: return "DW_AT_count";
230       case DW_AT_data_member_location: return "DW_AT_data_member_location";
231       case DW_AT_decl_column: return "DW_AT_decl_column";
232       case DW_AT_decl_file: return "DW_AT_decl_file";
233       case DW_AT_decl_line: return "DW_AT_decl_line";
234       case DW_AT_declaration: return "DW_AT_declaration";
235       case DW_AT_discr_list: return "DW_AT_discr_list";
236       case DW_AT_encoding: return "DW_AT_encoding";
237       case DW_AT_external: return "DW_AT_external";
238       case DW_AT_frame_base: return "DW_AT_frame_base";
239       case DW_AT_friend: return "DW_AT_friend";
240       case DW_AT_identifier_case: return "DW_AT_identifier_case";
241       case DW_AT_macro_info: return "DW_AT_macro_info";
242       case DW_AT_namelist_items: return "DW_AT_namelist_items";
243       case DW_AT_priority: return "DW_AT_priority";
244       case DW_AT_segment: return "DW_AT_segment";
245       case DW_AT_specification: return "DW_AT_specification";
246       case DW_AT_static_link: return "DW_AT_static_link";
247       case DW_AT_type: return "DW_AT_type";
248       case DW_AT_use_location: return "DW_AT_use_location";
249       case DW_AT_variable_parameter: return "DW_AT_variable_parameter";
250       case DW_AT_virtuality: return "DW_AT_virtuality";
251       case DW_AT_vtable_elem_location: return "DW_AT_vtable_elem_location";
252       /* DWARF 3 values.  */
253       case DW_AT_allocated: return "DW_AT_allocated";
254       case DW_AT_associated: return "DW_AT_associated";
255       case DW_AT_data_location: return "DW_AT_data_location";
256       case DW_AT_stride: return "DW_AT_stride";
257       case DW_AT_entry_pc: return "DW_AT_entry_pc";
258       case DW_AT_use_UTF8: return "DW_AT_use_UTF8";
259       case DW_AT_extension: return "DW_AT_extension";
260       case DW_AT_ranges: return "DW_AT_ranges";
261       case DW_AT_trampoline: return "DW_AT_trampoline";
262       case DW_AT_call_column: return "DW_AT_call_column";
263       case DW_AT_call_file: return "DW_AT_call_file";
264       case DW_AT_call_line: return "DW_AT_call_line";
265       case DW_AT_description: return "DW_AT_description";
266       case DW_AT_binary_scale: return "DW_AT_binary_scale";
267       case DW_AT_decimal_scale: return "DW_AT_decimal_scale";
268       case DW_AT_small: return "DW_AT_small";
269       case DW_AT_decimal_sign: return "DW_AT_decimal_sign";
270       case DW_AT_digit_count: return "DW_AT_digit_count";
271       case DW_AT_picture_string: return "DW_AT_picture_string";
272       case DW_AT_mutable: return "DW_AT_mutable";
273       case DW_AT_threads_scaled: return "DW_AT_threads_scaled";
274       case DW_AT_explicit: return "DW_AT_explicit";
275       case DW_AT_object_pointer: return "DW_AT_object_pointer";
276       case DW_AT_endianity: return "DW_AT_endianity";
277       case DW_AT_elemental: return "DW_AT_elemental";
278       case DW_AT_pure: return "DW_AT_pure";
279       case DW_AT_recursive: return "DW_AT_recursive";
280       /* DWARF 4 values.  */
281       case DW_AT_signature: return "DW_AT_signature";
282       case DW_AT_main_subprogram: return "DW_AT_main_subprogram";
283       case DW_AT_data_bit_offset: return "DW_AT_data_bit_offset";
284       case DW_AT_const_expr: return "DW_AT_const_expr";
285       case DW_AT_enum_class: return "DW_AT_enum_class";
286       case DW_AT_linkage_name: return "DW_AT_linkage_name";
287       /* SGI/MIPS extensions.  */
288       /* case DW_AT_MIPS_fde: return "DW_AT_MIPS_fde"; */
289       /* DW_AT_MIPS_fde == DW_AT_HP_unmodifiable */
290       case DW_AT_MIPS_loop_begin: return "DW_AT_MIPS_loop_begin";
291       case DW_AT_MIPS_tail_loop_begin: return "DW_AT_MIPS_tail_loop_begin";
292       case DW_AT_MIPS_epilog_begin: return "DW_AT_MIPS_epilog_begin";
293       case DW_AT_MIPS_loop_unroll_factor: return "DW_AT_MIPS_loop_unroll_factor";
294       case DW_AT_MIPS_software_pipeline_depth: return "DW_AT_MIPS_software_pipeline_depth";
295       case DW_AT_MIPS_linkage_name: return "DW_AT_MIPS_linkage_name";
296       case DW_AT_MIPS_stride: return "DW_AT_MIPS_stride";
297       case DW_AT_MIPS_abstract_name: return "DW_AT_MIPS_abstract_name";
298       case DW_AT_MIPS_clone_origin: return "DW_AT_MIPS_clone_origin";
299       case DW_AT_MIPS_has_inlines: return "DW_AT_MIPS_has_inlines";
300       /* HP extensions.  */
301       case DW_AT_HP_block_index: return "DW_AT_HP_block_index";
302       case DW_AT_HP_unmodifiable: return "DW_AT_HP_unmodifiable";
303       case DW_AT_HP_actuals_stmt_list: return "DW_AT_HP_actuals_stmt_list";
304       case DW_AT_HP_proc_per_section: return "DW_AT_HP_proc_per_section";
305       case DW_AT_HP_raw_data_ptr: return "DW_AT_HP_raw_data_ptr";
306       case DW_AT_HP_pass_by_reference: return "DW_AT_HP_pass_by_reference";
307       case DW_AT_HP_opt_level: return "DW_AT_HP_opt_level";
308       case DW_AT_HP_prof_version_id: return "DW_AT_HP_prof_version_id";
309       case DW_AT_HP_opt_flags: return "DW_AT_HP_opt_flags";
310       case DW_AT_HP_cold_region_low_pc: return "DW_AT_HP_cold_region_low_pc";
311       case DW_AT_HP_cold_region_high_pc: return "DW_AT_HP_cold_region_high_pc";
312       case DW_AT_HP_all_variables_modifiable: return "DW_AT_HP_all_variables_modifiable";
313       case DW_AT_HP_linkage_name: return "DW_AT_HP_linkage_name";
314       case DW_AT_HP_prof_flags: return "DW_AT_HP_prof_flags";
315       /* GNU extensions.  */
316       case DW_AT_sf_names: return "DW_AT_sf_names";
317       case DW_AT_src_info: return "DW_AT_src_info";
318       case DW_AT_mac_info: return "DW_AT_mac_info";
319       case DW_AT_src_coords: return "DW_AT_src_coords";
320       case DW_AT_body_begin: return "DW_AT_body_begin";
321       case DW_AT_body_end: return "DW_AT_body_end";
322       case DW_AT_GNU_vector: return "DW_AT_GNU_vector";
323       /* VMS extensions.  */
324       case DW_AT_VMS_rtnbeg_pd_address: return "DW_AT_VMS_rtnbeg_pd_address";
325       /* UPC extension.  */
326       case DW_AT_upc_threads_scaled: return "DW_AT_upc_threads_scaled";
327       /* PGI (STMicroelectronics) extensions.  */
328       case DW_AT_PGI_lbase: return "DW_AT_PGI_lbase";
329       case DW_AT_PGI_soffset: return "DW_AT_PGI_soffset";
330       case DW_AT_PGI_lstride: return "DW_AT_PGI_lstride";
331       default: return "DW_AT_???";
332    }
333 }
334
335
336 /* ------ To do with evaluation of Dwarf expressions ------ */
337
338 /* FIXME: duplicated in readdwarf.c */
339 static 
340 ULong read_leb128 ( UChar* data, Int* length_return, Int sign )
341 {
342   ULong  result = 0;
343   UInt   num_read = 0;
344   Int    shift = 0;
345   UChar  byte;
346
347   vg_assert(sign == 0 || sign == 1);
348
349   do
350     {
351       byte = * data ++;
352       num_read ++;
353
354       result |= ((ULong)(byte & 0x7f)) << shift;
355
356       shift += 7;
357
358     }
359   while (byte & 0x80);
360
361   if (length_return != NULL)
362     * length_return = num_read;
363
364   if (sign && (shift < 64) && (byte & 0x40))
365     result |= -(1ULL << shift);
366
367   return result;
368 }
369
370 /* Small helper functions easier to use
371  * value is returned and the given pointer is
372  * moved past end of leb128 data */
373 /* FIXME: duplicated in readdwarf.c */
374 static ULong read_leb128U( UChar **data )
375 {
376   Int len;
377   ULong val = read_leb128( *data, &len, 0 );
378   *data += len;
379   return val;
380 }
381
382 /* Same for signed data */
383 /* FIXME: duplicated in readdwarf.c */
384 static Long read_leb128S( UChar **data )
385 {
386    Int len;
387    ULong val = read_leb128( *data, &len, 1 );
388    *data += len;
389    return (Long)val;
390 }
391
392 /* FIXME: duplicates logic in readdwarf.c: copy_convert_CfiExpr_tree
393    and {FP,SP}_REG decls */
394 static Bool get_Dwarf_Reg( /*OUT*/Addr* a, Word regno, RegSummary* regs )
395 {
396    vg_assert(regs);
397 #  if defined(VGP_x86_linux) || defined(VGP_x86_darwin) || defined(VGP_x86_l4re)
398    if (regno == 5/*EBP*/) { *a = regs->fp; return True; }
399    if (regno == 4/*ESP*/) { *a = regs->sp; return True; }
400 #  elif defined(VGP_amd64_linux) || defined(VGP_amd64_darwin)
401    if (regno == 6/*RBP*/) { *a = regs->fp; return True; }
402    if (regno == 7/*RSP*/) { *a = regs->sp; return True; }
403 #  elif defined(VGP_ppc32_linux)
404    if (regno == 1/*SP*/) { *a = regs->sp; return True; }
405 #  elif defined(VGP_ppc64_linux)
406    if (regno == 1/*SP*/) { *a = regs->sp; return True; }
407 #  elif defined(VGP_arm_linux)
408    if (regno == 13) { *a = regs->sp; return True; }
409    if (regno == 11) { *a = regs->fp; return True; } 
410 #  elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5)
411    vg_assert(0); /* this function should never be called */
412 #  elif defined(VGP_s390x_linux)
413    if (regno == 15) { *a = regs->sp; return True; }
414    if (regno == 11) { *a = regs->fp; return True; }
415 #  else
416 #    error "Unknown platform"
417 #  endif
418    return False;
419 }
420
421 /* Convert a stated address to an actual address */
422 static Bool bias_address( Addr* a, const DebugInfo* di )
423 {
424    if (di->text_present
425        && di->text_size > 0
426        && *a >= di->text_debug_svma && *a < di->text_debug_svma + di->text_size) {
427       *a += di->text_debug_bias;
428    }
429    else if (di->data_present
430             && di->data_size > 0
431             && *a >= di->data_debug_svma && *a < di->data_debug_svma + di->data_size) {
432       *a += di->data_debug_bias;
433    }
434    else if (di->sdata_present
435             && di->sdata_size > 0
436             && *a >= di->sdata_debug_svma && *a < di->sdata_debug_svma + di->sdata_size) {
437       *a += di->sdata_debug_bias;
438    }
439    else if (di->rodata_present
440             && di->rodata_size > 0
441             && *a >= di->rodata_debug_svma && *a < di->rodata_debug_svma + di->rodata_size) {
442       *a += di->rodata_debug_bias;
443    }
444    else if (di->bss_present
445             && di->bss_size > 0
446             && *a >= di->bss_debug_svma && *a < di->bss_debug_svma + di->bss_size) {
447       *a += di->bss_debug_bias;
448    }
449    else if (di->sbss_present
450             && di->sbss_size > 0
451             && *a >= di->sbss_debug_svma && *a < di->sbss_debug_svma + di->sbss_size) {
452       *a += di->sbss_debug_bias;
453    }
454    else {
455       return False;
456    }
457
458    return True;
459 }
460
461
462 /* Evaluate a standard DWARF3 expression.  See detailed description in
463    priv_d3basics.h.  Doesn't handle DW_OP_piece/DW_OP_bit_piece yet.  */
464 GXResult ML_(evaluate_Dwarf3_Expr) ( UChar* expr, UWord exprszB, 
465                                      GExpr* fbGX, RegSummary* regs,
466                                      const DebugInfo* di,
467                                      Bool push_initial_zero )
468 {
469 #  define N_EXPR_STACK 20
470
471 #  define FAIL(_str)                                          \
472       do {                                                    \
473          res.kind = GXR_Failure;                              \
474          res.word = (UWord)(_str);                            \
475          return res;                                          \
476       } while (0)
477
478 #  define PUSH(_arg)                                          \
479       do {                                                    \
480          vg_assert(sp >= -1 && sp < N_EXPR_STACK);            \
481          if (sp == N_EXPR_STACK-1)                            \
482             FAIL("evaluate_Dwarf3_Expr: stack overflow(1)");  \
483          sp++;                                                \
484          stack[sp] = (_arg);                                  \
485       } while (0)
486
487 #  define POP(_lval)                                          \
488       do {                                                    \
489          vg_assert(sp >= -1 && sp < N_EXPR_STACK);            \
490          if (sp == -1)                                        \
491             FAIL("evaluate_Dwarf3_Expr: stack underflow(1)"); \
492          _lval = stack[sp];                                   \
493          sp--;                                                \
494       } while (0)
495
496    UChar    opcode;
497    UChar*   limit;
498    Int      sp; /* # of top element: valid is -1 .. N_EXPR_STACK-1 */
499    Addr     stack[N_EXPR_STACK]; /* stack of addresses, as per D3 spec */
500    GXResult fbval, res;
501    Addr     a1;
502    Word     sw1, sw2;
503    UWord    uw1, uw2;
504    Bool     ok;
505
506    sp = -1;
507    vg_assert(expr);
508    vg_assert(exprszB >= 0);
509    limit = expr + exprszB;
510
511    /* Deal with the case where the entire expression is a single
512       Register Name Operation (D3 spec sec 2.6.1).  Then the
513       denotation of the expression as a whole is a register name. */
514    if (exprszB == 1
515        && expr[0] >= DW_OP_reg0 && expr[0] <= DW_OP_reg31) {
516       res.kind = GXR_RegNo;
517       res.word = (UWord)(expr[0] - DW_OP_reg0);
518       return res;
519    }
520    if (exprszB > 1
521        && expr[0] == DW_OP_regx) {
522       /* JRS: 2008Feb20: I believe the following is correct, but would
523          like to see a test case show up before enabling it. */
524       expr++;
525       res.kind = GXR_RegNo;
526       res.word = (UWord)read_leb128U( &expr );
527       if (expr != limit)
528          FAIL("evaluate_Dwarf3_Expr: DW_OP_regx*: invalid expr size");
529       else
530          return res;
531       /*NOTREACHED*/
532    }
533
534    /* Evidently this expression denotes a value, not a register name.
535       So evaluate it accordingly. */
536
537    if (push_initial_zero)
538       PUSH(0);
539
540    while (True) {
541
542       vg_assert(sp >= -1 && sp < N_EXPR_STACK);
543
544       if (expr > limit) 
545          /* overrun - something's wrong */
546          FAIL("evaluate_Dwarf3_Expr: ran off end of expr");
547
548       if (expr == limit) {
549          /* end of expr - return expr on the top of stack. */
550          if (sp == -1)
551             /* stack empty.  Bad. */
552             FAIL("evaluate_Dwarf3_Expr: stack empty at end of expr");
553          else
554             break;
555       }
556
557       opcode = *expr++;
558       switch (opcode) {
559          case DW_OP_addr:
560             /* Presumably what is given in the Dwarf3 is a SVMA (how
561                could it be otherwise?)  So we add the appropriate bias
562                on before pushing the result. */
563             a1 = *(Addr*)expr;
564             if (bias_address(&a1, di)) {
565                PUSH( a1 ); 
566                expr += sizeof(Addr);
567             }
568             else {
569                FAIL("evaluate_Dwarf3_Expr: DW_OP_addr with address "
570                     "in unknown section");
571             }
572             break;
573          case DW_OP_fbreg:
574             if (!fbGX)
575                FAIL("evaluate_Dwarf3_Expr: DW_OP_fbreg with "
576                     "no expr for fbreg present");
577             fbval = ML_(evaluate_GX)(fbGX, NULL, regs, di);
578             /* Convert fbval into something we can use.  If we got a
579                Value, no problem.  However, as per D3 spec sec 3.3.5
580                (Low Level Information) sec 2, we could also get a
581                RegNo, and that is taken to mean the value in the
582                indicated register.  So we have to manually
583                "dereference" it. */
584             a1 = 0;
585             switch (fbval.kind) {
586                case GXR_Failure:
587                   return fbval; /* propagate failure */
588                case GXR_Addr:
589                   a1 = fbval.word; break; /* use as-is */
590                case GXR_RegNo:
591                   ok = get_Dwarf_Reg( &a1, fbval.word, regs );
592                   if (!ok) return fbval; /* propagate failure */
593                   break;
594                case GXR_Value:
595                   FAIL("evaluate_Dwarf3_Expr: DW_OP_{implicit,stack}_value "
596                        "in DW_AT_frame_base");
597                default:
598                   vg_assert(0);
599             }
600             sw1 = (Word)read_leb128S( &expr );
601             PUSH( a1 + sw1 );
602             break;
603          /* DW_OP_breg* denotes 'contents of specified register, plus
604             constant offset'.  So provided we know what the register's
605             value is, we can evaluate this.  Contrast DW_OP_reg*,
606             which indicates that denoted location is in a register
607             itself.  If DW_OP_reg* shows up here the expression is
608             malformed, since we are evaluating for value now, and
609             DW_OP_reg* denotes a register location, not a value.  See
610             D3 Spec sec 2.6.1 ("Register Name Operations") for
611             details. */
612          case DW_OP_breg0 ... DW_OP_breg31:
613             if (!regs)
614                FAIL("evaluate_Dwarf3_Expr: DW_OP_breg* but no reg info");
615             a1 = 0;
616             if (!get_Dwarf_Reg( &a1, opcode - DW_OP_breg0, regs ))
617                FAIL("evaluate_Dwarf3_Expr: unhandled DW_OP_breg*");
618             sw1 = (Word)read_leb128S( &expr );
619             a1 += sw1;
620             PUSH( a1 );
621             break;
622          case DW_OP_bregx:
623             if (!regs)
624                FAIL("evaluate_Dwarf3_Expr: DW_OP_bregx but no reg info");
625             a1 = 0;
626             uw1 = (UWord)read_leb128U( &expr );
627             if (!get_Dwarf_Reg( &a1, uw1, regs ))
628                FAIL("evaluate_Dwarf3_Expr: unhandled DW_OP_bregx reg value");
629             sw1 = (Word)read_leb128S( &expr );
630             a1 += sw1;
631             PUSH( a1 );
632             break;
633          /* As per comment on DW_OP_breg*, the following denote that
634             the value in question is in a register, not in memory.  So
635             we simply return failure. (iow, the expression is
636             malformed). */
637          case DW_OP_reg0 ... DW_OP_reg31:
638          case DW_OP_regx:
639             FAIL("evaluate_Dwarf3_Expr: DW_OP_reg* "
640                  "whilst evaluating for a value");
641             break;
642          case DW_OP_plus_uconst:
643             POP(uw1);
644             uw1 += (UWord)read_leb128U( &expr );
645             PUSH(uw1);
646             break;
647          case DW_OP_GNU_push_tls_address:
648             /* GDB contains the following cryptic comment: */
649             /* Variable is at a constant offset in the thread-local
650             storage block into the objfile for the current thread and
651             the dynamic linker module containing this expression. Here
652             we return returns the offset from that base.  The top of the
653             stack has the offset from the beginning of the thread
654             control block at which the variable is located.  Nothing
655             should follow this operator, so the top of stack would be
656             returned.  */
657             /* But no spec resulting from Googling.  Punt for now. */
658             FAIL("warning: evaluate_Dwarf3_Expr: unhandled "         
659                  "DW_OP_GNU_push_tls_address");
660             /*NOTREACHED*/
661          case DW_OP_deref:
662             POP(uw1);
663             if (VG_(am_is_valid_for_client)( (Addr)uw1, sizeof(Addr),
664                                              VKI_PROT_READ )) {
665                uw1 = *(UWord*)uw1;
666                PUSH(uw1);
667             } else {
668                FAIL("warning: evaluate_Dwarf3_Expr: DW_OP_deref: "
669                     "address not valid for client");
670             }
671             break;
672          case DW_OP_deref_size:
673             POP(uw1);
674             uw2 = *expr++;
675             if (VG_(am_is_valid_for_client)( (Addr)uw1, uw2,
676                                              VKI_PROT_READ )) {
677                switch (uw2) {
678                  case 1: uw1 = *(UChar*)uw1; break;
679                  case 2: uw1 = *(UShort*)uw1; break;
680                  case 4: uw1 = *(UInt*)uw1; break;
681                  case 8: uw1 = *(ULong*)uw1; break;
682                  default:
683                     FAIL("warning: evaluate_Dwarf3_Expr: unhandled "
684                          "DW_OP_deref_size size");
685                }
686                PUSH(uw1);
687             } else {
688                FAIL("warning: evaluate_Dwarf3_Expr: DW_OP_deref_size: "
689                     "address not valid for client");
690             }
691             break;
692          case DW_OP_lit0 ... DW_OP_lit31:
693             PUSH(opcode - DW_OP_lit0);
694             break;
695          case DW_OP_const1u:
696             uw1 = *expr++;
697             PUSH(uw1);
698             break;
699          case DW_OP_const2u:
700             uw1 = *(UShort *)expr;
701             expr += 2;
702             PUSH(uw1);
703             break;
704          case DW_OP_const4u:
705             uw1 = *(UInt *)expr;
706             expr += 4;
707             PUSH(uw1);
708             break;
709          case DW_OP_const8u:
710             uw1 = *(ULong *)expr;
711             expr += 8;
712             PUSH(uw1);
713             break;
714          case DW_OP_constu:
715             uw1 = read_leb128U( &expr );
716             PUSH(uw1);
717             break;
718          case DW_OP_const1s:
719             uw1 = *(Char *)expr;
720             expr++;
721             PUSH(uw1);
722             break;
723          case DW_OP_const2s:
724             uw1 = *(Short *)expr;
725             expr += 2;
726             PUSH(uw1);
727             break;
728          case DW_OP_const4s:
729             uw1 = *(Int *)expr;
730             expr += 4;
731             PUSH(uw1);
732             break;
733          case DW_OP_const8s:
734             uw1 = *(Long *)expr;
735             expr += 8;
736             PUSH(uw1);
737             break;
738          case DW_OP_consts:
739             uw1 = read_leb128S( &expr );
740             PUSH(uw1);
741             break;
742          case DW_OP_dup:
743             POP(uw1);
744             PUSH(uw1);
745             PUSH(uw1);
746             break;
747          case DW_OP_drop:
748             POP(uw1);
749             break;
750          case DW_OP_over:
751             uw1 = 1;
752             goto do_pick;
753          case DW_OP_pick:
754             uw1 = *expr++;
755          do_pick:
756             if (sp < (Int)uw1)
757                FAIL("evaluate_Dwarf3_Expr: stack underflow");
758             uw1 = stack[sp - uw1];
759             PUSH(uw1);
760             break;
761          case DW_OP_swap:
762             if (sp < 1)
763                FAIL("evaluate_Dwarf3_Expr: stack underflow");
764             uw1 = stack[sp];
765             stack[sp] = stack[sp - 1];
766             stack[sp - 1] = uw1;
767             break;
768          case DW_OP_rot:
769             if (sp < 2)
770                FAIL("evaluate_Dwarf3_Expr: stack underflow");
771             uw1 = stack[sp];
772             stack[sp] = stack[sp - 1];
773             stack[sp - 1] = stack[sp - 2];
774             stack[sp - 2] = uw1;
775             break;
776          case DW_OP_abs:
777             POP(sw1);
778             if (sw1 < 0)
779                sw1 = -sw1;
780             PUSH(sw1);
781             break;
782          case DW_OP_div:
783             POP(sw2);
784             if (sw2 == 0)
785                FAIL("evaluate_Dwarf3_Expr: division by zero");
786             POP(sw1);
787             sw1 /= sw2;
788             PUSH(sw1);
789             break;
790          case DW_OP_mod:
791             POP(uw2);
792             if (uw2 == 0)
793                FAIL("evaluate_Dwarf3_Expr: division by zero");
794             POP(uw1);
795             uw1 %= uw2;
796             PUSH(uw1);
797             break;
798 #define BINARY(name, op, s) \
799          case DW_OP_##name:             \
800             POP(s##w2);                 \
801             POP(s##w1);                 \
802             s##w1 = s##w1 op s##w2;     \
803             PUSH(s##w1);                \
804             break
805 #define UNARY(name, op, s) \
806          case DW_OP_##name:             \
807             POP(s##w1);                 \
808             s##w1 = op s##w1;           \
809             PUSH(s##w1);                \
810             break
811          BINARY (and, &, u);
812          BINARY (minus, -, u);
813          BINARY (mul, *, u);
814          UNARY (neg, -, u);
815          UNARY (not, ~, u);
816          BINARY (or, |, u);
817          BINARY (plus, +, u);
818          BINARY (shl, <<, u);
819          BINARY (shr, >>, u);
820          BINARY (shra, >>, s);
821          BINARY (xor, ^, u);
822          BINARY (le, <=, s);
823          BINARY (lt, <, s);
824          BINARY (ge, >=, s);
825          BINARY (gt, >, s);
826          BINARY (ne, !=, u);
827          BINARY (eq, ==, u);
828 #undef UNARY
829 #undef BINARY
830          case DW_OP_skip:
831             sw1 = *(Short *)expr;
832             expr += 2;
833             if (expr + sw1 < limit - exprszB)
834                FAIL("evaluate_Dwarf3_Expr: DW_OP_skip before start of expr");
835             if (expr + sw1 >= limit)
836                FAIL("evaluate_Dwarf3_Expr: DW_OP_skip after end of expr");
837             expr += sw1;
838             break;
839          case DW_OP_bra:
840             sw1 = *(Short *)expr;
841             expr += 2;
842             if (expr + sw1 < limit - exprszB)
843                FAIL("evaluate_Dwarf3_Expr: DW_OP_bra before start of expr");
844             if (expr + sw1 >= limit)
845                FAIL("evaluate_Dwarf3_Expr: DW_OP_bra after end of expr");
846             POP(uw1);
847             if (uw1)
848                expr += sw1;
849             break;
850          case DW_OP_nop:
851             break;
852          case DW_OP_call_frame_cfa:
853             if (!regs)
854                FAIL("evaluate_Dwarf3_Expr: "
855                     "DW_OP_call_frame_cfa but no reg info");
856 #if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
857             /* Valgrind on ppc32/ppc64 currently doesn't use unwind info. */
858             uw1 = *(Addr *)(regs->sp);
859 #else
860             uw1 = ML_(get_CFA)(regs->ip, regs->sp, regs->fp, 0, ~(UWord) 0);
861 #endif
862             /* we expect this to fail on arm-linux, since ML_(get_CFA)
863                always returns zero at present. */
864             if (!uw1)
865                FAIL("evaluate_Dwarf3_Expr: Could not resolve "
866                     "DW_OP_call_frame_cfa");
867             PUSH(uw1);
868             break;
869          case DW_OP_implicit_value:
870             sw1 = (Word)read_leb128S( &expr );
871             uw1 = 0;
872             switch (sw1) {
873                case 1:
874                   uw1 = *(UChar *)expr;
875                   expr += 1;
876                   break;
877                case 2:
878                   uw1 = *(UShort *)expr;
879                   expr += 2;
880                   break;
881                case 4:
882                   uw1 = *(UInt *)expr;
883                   expr += 4;
884                   break;
885                case 8:
886                   uw1 = *(ULong *)expr;
887                   expr += 8;
888                   break;
889                default:
890                   FAIL("evaluate_Dwarf3_Expr: Unhandled "
891                        "DW_OP_implicit_value size");
892             }
893             if (expr != limit)
894                FAIL("evaluate_Dwarf3_Expr: DW_OP_implicit_value "
895                     "does not terminate expression");
896             res.word = uw1;
897             res.kind = GXR_Value;
898             return res;
899          case DW_OP_stack_value:
900             POP (uw1);
901             res.word = uw1;
902             res.kind = GXR_Value;
903             if (expr != limit)
904                FAIL("evaluate_Dwarf3_Expr: DW_OP_stack_value "
905                     "does not terminate expression");
906             break;
907          default:
908             if (!VG_(clo_xml))
909                VG_(message)(Vg_DebugMsg, 
910                             "warning: evaluate_Dwarf3_Expr: unhandled "
911                             "DW_OP_ 0x%x\n", (Int)opcode); 
912             FAIL("evaluate_Dwarf3_Expr: unhandled DW_OP_");
913             /*NOTREACHED*/
914       }
915
916    }
917
918    vg_assert(sp >= 0 && sp < N_EXPR_STACK);
919    res.word = stack[sp];
920    res.kind = GXR_Addr;
921    return res;
922  
923 #  undef POP
924 #  undef PUSH
925 #  undef FAIL
926 #  undef N_EXPR_STACK
927 }
928
929
930 /* Evaluate a so-called Guarded (DWARF3) expression.  See detailed
931    description in priv_d3basics.h. */
932 GXResult ML_(evaluate_GX)( GExpr* gx, GExpr* fbGX,
933                            RegSummary* regs, const DebugInfo* di )
934 {
935    GXResult res;
936    Addr     aMin, aMax;
937    UChar    uc;
938    UShort   nbytes;
939    UWord    nGuards = 0;
940    UChar* p = &gx->payload[0];
941    uc = *p++; /*biasMe*/
942    vg_assert(uc == 0 || uc == 1);
943    /* in fact it's senseless to evaluate if the guards need biasing.
944       So don't. */
945    vg_assert(uc == 0);
946    while (True) {
947       uc = *p++;
948       if (uc == 1) { /*isEnd*/
949          /* didn't find any matching range. */
950          res.kind = GXR_Failure;
951          res.word = (UWord)"no matching range";
952          return res;
953       }
954       vg_assert(uc == 0);
955       aMin   = * (Addr*)p;   p += sizeof(Addr);
956       aMax   = * (Addr*)p;   p += sizeof(Addr);
957       nbytes = * (UShort*)p; p += sizeof(UShort);
958       nGuards++;
959       if (0) VG_(printf)("           guard %d: %#lx %#lx\n",
960                          (Int)nGuards, aMin,aMax);
961       if (regs == NULL) {
962          vg_assert(aMin == (Addr)0);
963          vg_assert(aMax == ~(Addr)0);
964          /* Assert this is the first guard. */
965          vg_assert(nGuards == 1);
966          res = ML_(evaluate_Dwarf3_Expr)(
967                   p, (UWord)nbytes, fbGX, regs, di,
968                   False/*push_initial_zero*/ );
969          /* Now check there are no more guards. */
970          p += (UWord)nbytes;
971          vg_assert(*p == 1); /*isEnd*/
972          return res;
973       } else {
974          if (aMin <= regs->ip && regs->ip <= aMax) {
975             /* found a matching range.  Evaluate the expression. */
976             return ML_(evaluate_Dwarf3_Expr)(
977                       p, (UWord)nbytes, fbGX, regs, di,
978                       False/*push_initial_zero*/ );
979          }
980       }
981       /* else keep searching */
982       p += (UWord)nbytes;
983    }
984 }
985
986
987 /* Evaluate a very simple Guarded (DWARF3) expression.  The expression
988    is expected to denote a constant, with no reference to any
989    registers nor to any frame base expression.  The expression is
990    expected to have at least one guard.  If there is more than one
991    guard, all the sub-expressions are evaluated and compared.  The
992    address ranges on the guards are ignored.  GXR_Failure is returned
993    in the following circumstances:
994    * no guards
995    * any of the subexpressions require a frame base expression
996    * any of the subexpressions denote a register location
997    * any of the subexpressions do not produce a manifest constant
998    * there's more than one subexpression, all of which successfully
999      evaluate to a constant, but they don't all produce the same constant.
1000    JRS 23Jan09: the special-casing in this function is a nasty kludge.
1001    Really it ought to be pulled out and turned into a general
1002    constant- expression evaluator.
1003 */
1004 GXResult ML_(evaluate_trivial_GX)( GExpr* gx, const DebugInfo* di )
1005 {
1006    GXResult   res;
1007    Addr       aMin, aMax;
1008    UChar      uc;
1009    UShort     nbytes;
1010    Word       i, nGuards;
1011    MaybeULong *mul, *mul2;
1012
1013    HChar*  badness = NULL;
1014    UChar*  p       = &gx->payload[0]; /* must remain unsigned */
1015    XArray* results = VG_(newXA)( ML_(dinfo_zalloc), "di.d3basics.etG.1",
1016                                  ML_(dinfo_free),
1017                                  sizeof(MaybeULong) );
1018
1019    uc = *p++; /*biasMe*/
1020    vg_assert(uc == 0 || uc == 1);
1021    /* in fact it's senseless to evaluate if the guards need biasing.
1022       So don't. */
1023    vg_assert(uc == 0);
1024
1025    nGuards = 0;
1026    while (True) {
1027       MaybeULong thisResult;
1028       uc = *p++;
1029       if (uc == 1) /*isEnd*/
1030          break;
1031       vg_assert(uc == 0);
1032       aMin   = * (Addr*)p;   p += sizeof(Addr);
1033       aMax   = * (Addr*)p;   p += sizeof(Addr);
1034       nbytes = * (UShort*)p; p += sizeof(UShort);
1035       nGuards++;
1036       if (0) VG_(printf)("           guard %ld: %#lx %#lx\n", 
1037                          nGuards, aMin,aMax);
1038
1039       thisResult.b  = False;
1040       thisResult.ul = 0;
1041
1042       /* Peer at this particular subexpression, to see if it's
1043          obviously a constant. */
1044       if (nbytes == 1 + sizeof(Addr) && *p == DW_OP_addr) {
1045          /* DW_OP_addr a */
1046          Addr a = *(Addr*)(p+1);
1047          if (bias_address(&a, di)) {
1048             thisResult.b = True;
1049             thisResult.ul = (ULong)a;
1050          } else {
1051             if (!badness)
1052                badness = "trivial GExpr denotes constant address "
1053                          "in unknown section (1)";
1054          }
1055       }
1056       else 
1057       if (nbytes == 1 + sizeof(Addr) + 1 + 1
1058           /* 11 byte block: 3 c0 b6 2b 0 0 0 0 0 23 4
1059              (DW_OP_addr: 2bb6c0; DW_OP_plus_uconst: 4)
1060              This is really a nasty kludge - only matches if the
1061              trailing ULEB denotes a number in the range 0 .. 127
1062              inclusive. */
1063           && p[0] == DW_OP_addr
1064           && p[1 + sizeof(Addr)] == DW_OP_plus_uconst 
1065           && p[1 + sizeof(Addr) + 1] < 0x80 /*1-byte ULEB*/) {
1066          Addr a = *(Addr*)&p[1];
1067          if (bias_address(&a, di)) {
1068             thisResult.b = True;
1069             thisResult.ul = (ULong)a + (ULong)p[1 + sizeof(Addr) + 1];
1070          } else {
1071             if (!badness)
1072                badness = "trivial GExpr denotes constant address "
1073                          "in unknown section (2)";
1074          }
1075       }
1076       else
1077       if (nbytes == 2 + sizeof(Addr) 
1078           && *p == DW_OP_addr
1079           && *(p + 1 + sizeof(Addr)) == DW_OP_GNU_push_tls_address) {
1080          if (!badness)
1081             badness = "trivial GExpr is DW_OP_addr plus trailing junk";
1082       }
1083       else if (nbytes >= 1 && *p >= DW_OP_reg0 && *p <= DW_OP_reg31) {
1084          if (!badness)
1085             badness = "trivial GExpr denotes register (1)";
1086       }
1087       else if (nbytes >= 1 && *p == DW_OP_fbreg) {
1088          if (!badness)
1089             badness = "trivial GExpr requires fbGX";
1090       }
1091       else if (nbytes >= 1 && *p >= DW_OP_breg0 && *p <= DW_OP_breg31) {
1092          if (!badness)
1093             badness = "trivial GExpr requires register value";
1094       }
1095       else if (nbytes >= 1 && *p == DW_OP_regx) {
1096          if (!badness)
1097             badness = "trivial GExpr denotes register (2)";
1098       }
1099       else if (0) {
1100          VG_(printf)(" ML_(evaluate_trivial_GX): unhandled:\n   ");
1101          ML_(pp_GX)( gx );
1102          VG_(printf)("\n");
1103          tl_assert(0);
1104       }
1105       else
1106          if (!badness)
1107             badness = "non-trivial GExpr";
1108
1109       VG_(addToXA)( results, &thisResult );
1110
1111       p += (UWord)nbytes;
1112    }
1113
1114    res.kind = GXR_Failure;
1115
1116    tl_assert(nGuards == VG_(sizeXA)( results ));
1117    tl_assert(nGuards >= 0);
1118    if (nGuards == 0) {
1119       tl_assert(!badness);
1120       res.word = (UWord)"trivial GExpr has no guards (!)";
1121       VG_(deleteXA)( results );
1122       return res;
1123    }
1124
1125    for (i = 0; i < nGuards; i++) {
1126       mul = VG_(indexXA)( results, i );
1127       if (mul->b == False)
1128          break;
1129    }
1130
1131    vg_assert(i >= 0 && i <= nGuards);
1132    if (i < nGuards) {
1133       /* at least one subexpression failed to produce a manifest constant. */
1134       vg_assert(badness);
1135       res.word = (UWord)badness;
1136       VG_(deleteXA)( results );
1137       return res;
1138    }
1139
1140    /* All the subexpressions produced a constant, but did they all produce
1141       the same one? */
1142    mul = VG_(indexXA)( results, 0 );
1143    tl_assert(mul->b == True); /* we just established that all exprs are ok */
1144
1145    for (i = 1; i < nGuards; i++) {
1146       mul2 = VG_(indexXA)( results, i );
1147       tl_assert(mul2->b == True);
1148       if (mul2->ul != mul->ul) {
1149          res.word = (UWord)"trivial GExpr: subexpressions disagree";
1150          VG_(deleteXA)( results );
1151          return res;
1152       }
1153    }
1154
1155    /* Well, we have success.  All subexpressions evaluated, and 
1156       they all agree.  Hurrah. */
1157    res.kind = GXR_Addr;
1158    res.word = (UWord)mul->ul; /* NB: narrowing from ULong */
1159    VG_(deleteXA)( results );
1160    return res;
1161 }
1162
1163
1164 void ML_(pp_GXResult) ( GXResult res )
1165 {
1166    switch (res.kind) {
1167       case GXR_Failure:
1168          VG_(printf)("GXR_Failure(%s)", (HChar*)res.word); break;
1169       case GXR_Addr:
1170          VG_(printf)("GXR_Addr(0x%lx)", res.word); break;
1171       case GXR_Value:
1172          VG_(printf)("GXR_Value(0x%lx)", res.word); break;
1173       case GXR_RegNo:
1174          VG_(printf)("GXR_RegNo(%lu)", res.word); break;
1175       default:
1176          VG_(printf)("GXR_???"); break;
1177    }
1178 }
1179
1180
1181 void ML_(pp_GX) ( GExpr* gx ) {
1182    Addr   aMin, aMax;
1183    UChar  uc;
1184    UShort nbytes;
1185    UChar* p = &gx->payload[0];
1186    uc = *p++;
1187    VG_(printf)("GX(%s){", uc == 0 ? "final" : "Breqd" );
1188    vg_assert(uc == 0 || uc == 1);
1189    while (True) {
1190       uc = *p++;
1191       if (uc == 1)
1192          break; /*isEnd*/
1193       vg_assert(uc == 0);
1194       aMin   = * (Addr*)p;  p += sizeof(Addr);
1195       aMax   = * (Addr*)p;  p += sizeof(Addr);
1196       nbytes = * (UShort*)p; p += sizeof(UShort);
1197       VG_(printf)("[%#lx,%#lx]=", aMin, aMax);
1198       while (nbytes > 0) {
1199          VG_(printf)("%02x", (UInt)*p++);
1200          nbytes--;
1201       }
1202       if (*p == 0)
1203          VG_(printf)(",");
1204    }
1205    VG_(printf)("}");
1206 }
1207
1208
1209 /*--------------------------------------------------------------------*/
1210 /*--- end                                               d3basics.c ---*/
1211 /*--------------------------------------------------------------------*/