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