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