1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020, NTT DATA.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include <cstring>
  27 
  28 #include "dwarf.hpp"
  29 #include "libproc_impl.h"
  30 
  31 /* from read_leb128() in dwarf.c in binutils */
  32 uintptr_t DwarfParser::read_leb(bool sign) {
  33   uintptr_t result = 0L;
  34   unsigned char b;
  35   unsigned int shift = 0;
  36 
  37   while (true) {
  38     b = *_buf++;
  39     result |= static_cast<uintptr_t>(b & 0x7f) << shift;
  40     shift += 7;
  41     if ((b & 0x80) == 0) {
  42       break;
  43     }
  44   }
  45 
  46   if (sign && (shift < (8 * sizeof(result))) && (b & 0x40)) {
  47     result |= static_cast<uintptr_t>(-1L) << shift;
  48   }
  49 
  50   return result;
  51 }
  52 
  53 uint64_t DwarfParser::get_entry_length() {
  54   uint64_t length = *(reinterpret_cast<uint32_t *>(_buf));
  55   _buf += 4;
  56   if (length == 0xffffffff) {
  57     length = *(reinterpret_cast<uint64_t *>(_buf));
  58     _buf += 8;
  59   }
  60   return length;
  61 }
  62 
  63 void DwarfParser::process_cie(unsigned char *start_of_entry, uint32_t id) {
  64   unsigned char *orig_pos = _buf;
  65   _buf = start_of_entry - id;
  66 
  67   uint64_t length = get_entry_length();
  68   unsigned char *end = _buf + length;
  69 
  70   _buf += 4; // Skip ID (This value of CIE would be always 0)
  71   _buf++;    // Skip version (assume to be "1")
  72 
  73   char *augmentation_string = reinterpret_cast<char *>(_buf);
  74   bool has_ehdata = (strcmp("eh", augmentation_string) == 0);
  75   bool fde_encoded = (strchr(augmentation_string, 'R') != NULL);
  76   _buf += strlen(augmentation_string) + 1; // includes '\0'
  77   if (has_ehdata) {
  78     _buf += sizeof(void *); // Skip EH data
  79   }
  80 
  81   _code_factor = read_leb(false);
  82   _data_factor = static_cast<int>(read_leb(true));
  83   _return_address_reg = static_cast<enum DWARF_Register>(*_buf++);
  84 
  85   if (fde_encoded) {
  86     uintptr_t augmentation_length = read_leb(false);
  87     _encoding = *_buf++;
  88   }
  89 
  90   // Clear state
  91   _current_pc = 0L;
  92   _cfa_reg = RSP;
  93   _return_address_reg = RA;
  94   _cfa_offset = 0;
  95   _ra_cfa_offset = 0;
  96   _bp_cfa_offset = 0;
  97   _bp_offset_available = false;
  98 
  99   parse_dwarf_instructions(0L, static_cast<uintptr_t>(-1L), end);
 100 
 101   _buf = orig_pos;
 102 }
 103 
 104 void DwarfParser::parse_dwarf_instructions(uintptr_t begin, uintptr_t pc, const unsigned char *end) {
 105   uintptr_t operand1;
 106   _current_pc = begin;
 107 
 108   /* for remember state */
 109   enum DWARF_Register rem_cfa_reg = MAX_VALUE;
 110   int rem_cfa_offset = 0;
 111   int rem_ra_cfa_offset = 0;
 112   int rem_bp_cfa_offset = 0;
 113 
 114   while ((_buf < end) && (_current_pc < pc)) {
 115     unsigned char op = *_buf++;
 116     unsigned char opa = op & 0x3f;
 117     if (op & 0xc0) {
 118       op &= 0xc0;
 119     }
 120 
 121     switch (op) {
 122       case 0x0:  // DW_CFA_nop
 123         return;
 124       case 0x01: // DW_CFA_set_loc
 125         operand1 = get_decoded_value();
 126         if (_current_pc != 0L) {
 127           _current_pc = operand1;
 128         }
 129         break;
 130       case 0x0c: // DW_CFA_def_cfa
 131         _cfa_reg = static_cast<enum DWARF_Register>(read_leb(false));
 132         _cfa_offset = read_leb(false);
 133         break;
 134       case 0x80: {// DW_CFA_offset
 135         operand1 = read_leb(false);
 136         enum DWARF_Register reg = static_cast<enum DWARF_Register>(opa);
 137         if (reg == RBP) {
 138           _bp_cfa_offset = operand1 * _data_factor;
 139           _bp_offset_available = true;
 140         } else if (reg == RA) {
 141           _ra_cfa_offset = operand1 * _data_factor;
 142         }
 143         break;
 144       }
 145       case 0xe:  // DW_CFA_def_cfa_offset
 146         _cfa_offset = read_leb(false);
 147         break;
 148       case 0x40: // DW_CFA_advance_loc
 149         if (_current_pc != 0L) {
 150           _current_pc += opa * _code_factor;
 151         }
 152         break;
 153       case 0x02: { // DW_CFA_advance_loc1
 154         unsigned char ofs = *_buf++;
 155         if (_current_pc != 0L) {
 156           _current_pc += ofs * _code_factor;
 157         }
 158         break;
 159       }
 160       case 0x03: { // DW_CFA_advance_loc2
 161         unsigned short ofs = *(reinterpret_cast<unsigned short *>(_buf));
 162         _buf += 2;
 163         if (_current_pc != 0L) {
 164           _current_pc += ofs * _code_factor;
 165         }
 166         break;
 167       }
 168       case 0x0d: {// DW_CFA_def_cfa_register
 169         _cfa_reg = static_cast<enum DWARF_Register>(read_leb(false));
 170         break;
 171       }
 172       case 0x0a: // DW_CFA_remenber_state
 173         rem_cfa_reg = _cfa_reg;
 174         rem_cfa_offset = _cfa_offset;
 175         rem_ra_cfa_offset = _ra_cfa_offset;
 176         rem_bp_cfa_offset = _bp_cfa_offset;
 177         break;
 178       case 0x0b: // DW_CFA_restore_state
 179         _cfa_reg = rem_cfa_reg;
 180         _cfa_offset = rem_cfa_offset;
 181         _ra_cfa_offset = rem_ra_cfa_offset;
 182         _bp_cfa_offset = rem_bp_cfa_offset;
 183         break;
 184       default:
 185         print_debug("DWARF: Unknown opcode: 0x%x\n", op);
 186         return;
 187     }
 188   }
 189 }
 190 
 191 /* from dwarf.c in binutils */
 192 uint32_t DwarfParser::get_decoded_value() {
 193   int size;
 194   uintptr_t result;
 195 
 196   switch (_encoding & 0x7) {
 197     case 0:  // DW_EH_PE_absptr
 198       size = sizeof(void *);
 199       result = *(reinterpret_cast<uintptr_t *>(_buf));
 200       break;
 201     case 2:  // DW_EH_PE_udata2
 202       size = 2;
 203       result = *(reinterpret_cast<unsigned int *>(_buf));
 204       break;
 205     case 3:  // DW_EH_PE_udata4
 206       size = 4;
 207       result = *(reinterpret_cast<uint32_t *>(_buf));
 208       break;
 209     case 4:  // DW_EH_PE_udata8
 210       size = 8;
 211       result = *(reinterpret_cast<uint64_t *>(_buf));
 212       break;
 213     default:
 214       return 0;
 215   }
 216 
 217   // On x86-64, we have to handle it as 32 bit value, and it is PC relative.
 218   //   https://gcc.gnu.org/ml/gcc-help/2010-09/msg00166.html
 219 #if defined(_LP64)
 220   if (size == 8) {
 221     result += _lib->eh_frame.v_addr + static_cast<uintptr_t>(_buf - _lib->eh_frame.data);
 222     size = 4;
 223   } else
 224 #endif
 225   if ((_encoding & 0x70) == 0x10) { // 0x10 = DW_EH_PE_pcrel
 226     result += _lib->eh_frame.v_addr + static_cast<uintptr_t>(_buf - _lib->eh_frame.data);
 227   } else  if (size == 2) {
 228     result = static_cast<int>(result) + _lib->eh_frame.v_addr + static_cast<uintptr_t>(_buf - _lib->eh_frame.data);
 229     size = 4;
 230   }
 231 
 232   _buf += size;
 233   return static_cast<uint32_t>(result);
 234 }
 235 
 236 unsigned int DwarfParser::get_pc_range() {
 237   int size;
 238   uintptr_t result;
 239 
 240   switch (_encoding & 0x7) {
 241     case 0:  // DW_EH_PE_absptr
 242       size = sizeof(void *);
 243       result = *(reinterpret_cast<uintptr_t *>(_buf));
 244       break;
 245     case 2:  // DW_EH_PE_udata2
 246       size = 2;
 247       result = *(reinterpret_cast<unsigned int *>(_buf));
 248       break;
 249     case 3:  // DW_EH_PE_udata4
 250       size = 4;
 251       result = *(reinterpret_cast<uint32_t *>(_buf));
 252       break;
 253     case 4:  // DW_EH_PE_udata8
 254       size = 8;
 255       result = *(reinterpret_cast<uint64_t *>(_buf));
 256       break;
 257     default:
 258       return 0;
 259   }
 260 
 261   // On x86-64, we have to handle it as 32 bit value, and it is PC relative.
 262   //   https://gcc.gnu.org/ml/gcc-help/2010-09/msg00166.html
 263 #if defined(_LP64)
 264   if ((size == 8) || (size == 2)) {
 265     size = 4;
 266   }
 267 #endif
 268 
 269   _buf += size;
 270   return static_cast<unsigned int>(result);
 271 }
 272 
 273 uintptr_t DwarfParser::process_dwarf(const uintptr_t pc) {
 274   // https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html
 275   _buf = _lib->eh_frame.data;
 276   while (true) {
 277     uint64_t length = get_entry_length();
 278     unsigned char *next_entry = _buf + length;
 279     unsigned char *start_of_entry = _buf;
 280     uint32_t id = *(reinterpret_cast<uint32_t *>(_buf));
 281     _buf += 4;
 282     if (id != 0) { // FDE
 283       uintptr_t pc_begin = get_decoded_value() + _lib->eh_frame.library_base_addr;
 284       uintptr_t pc_end = pc_begin + get_pc_range();
 285 
 286       if ((pc >= pc_begin) && (pc < pc_end)) {
 287         // Process CIE
 288         process_cie(start_of_entry, id);
 289 
 290         // Skip Augumenation
 291         uintptr_t augmentation_length = read_leb(false);
 292         _buf += augmentation_length; // skip
 293 
 294         // Process FDE
 295         parse_dwarf_instructions(pc_begin, pc, next_entry);
 296         break;
 297       }
 298     }
 299 
 300     _buf = next_entry;
 301   }
 302 
 303   return 0L;
 304 }