Home | History | Annotate | Download | only in libdw
      1 /* Get register location expression for frame.
      2    Copyright (C) 2009-2010, 2014 Red Hat, Inc.
      3    This file is part of elfutils.
      4 
      5    This file is free software; you can redistribute it and/or modify
      6    it under the terms of either
      7 
      8      * the GNU Lesser General Public License as published by the Free
      9        Software Foundation; either version 3 of the License, or (at
     10        your option) any later version
     11 
     12    or
     13 
     14      * the GNU General Public License as published by the Free
     15        Software Foundation; either version 2 of the License, or (at
     16        your option) any later version
     17 
     18    or both in parallel, as here.
     19 
     20    elfutils is distributed in the hope that it will be useful, but
     21    WITHOUT ANY WARRANTY; without even the implied warranty of
     22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     23    General Public License for more details.
     24 
     25    You should have received copies of the GNU General Public License and
     26    the GNU Lesser General Public License along with this program.  If
     27    not, see <http://www.gnu.org/licenses/>.  */
     28 
     29 #ifdef HAVE_CONFIG_H
     30 # include <config.h>
     31 #endif
     32 
     33 #include "cfi.h"
     34 #include <dwarf.h>
     35 
     36 int
     37 dwarf_frame_register (fs, regno, ops_mem, ops, nops)
     38      Dwarf_Frame *fs;
     39      int regno;
     40      Dwarf_Op ops_mem[3];
     41      Dwarf_Op **ops;
     42      size_t *nops;
     43 {
     44   /* Maybe there was a previous error.  */
     45   if (fs == NULL)
     46     return -1;
     47 
     48   if (unlikely (regno < 0))
     49     {
     50       __libdw_seterrno (DWARF_E_INVALID_ACCESS);
     51       return -1;
     52     }
     53 
     54   *ops = ops_mem;
     55   *nops = 0;
     56 
     57   if (unlikely ((size_t) regno >= fs->nregs))
     58     goto default_rule;
     59 
     60   const struct dwarf_frame_register *reg = &fs->regs[regno];
     61 
     62   switch (reg->rule)
     63     {
     64     case reg_unspecified:
     65     default_rule:
     66       /* Use the default rule for registers not yet mentioned in CFI.  */
     67       if (fs->cache->default_same_value)
     68 	goto same_value;
     69       /*FALLTHROUGH*/
     70     case reg_undefined:
     71       /* The value is known to be unavailable.  */
     72       break;
     73 
     74     case reg_same_value:
     75     same_value:
     76       /* The location is not known here, but the caller might know it.  */
     77       *ops = NULL;
     78       break;
     79 
     80     case reg_offset:
     81     case reg_val_offset:
     82       ops_mem[(*nops)++] = (Dwarf_Op) { .atom = DW_OP_call_frame_cfa };
     83       if (reg->value != 0)
     84 	ops_mem[(*nops)++] = (Dwarf_Op) { .atom = DW_OP_plus_uconst,
     85 					  .number = reg->value };
     86       if (reg->rule == reg_val_offset)
     87 	/* A value, not a location.  */
     88 	ops_mem[(*nops)++] = (Dwarf_Op) { .atom = DW_OP_stack_value };
     89       *ops = ops_mem;
     90       break;
     91 
     92     case reg_register:
     93       ops_mem[(*nops)++] = (Dwarf_Op) { .atom = DW_OP_regx,
     94 					.number = reg->value };
     95       break;
     96 
     97     case reg_val_expression:
     98     case reg_expression:
     99       {
    100 	unsigned int address_size = (fs->cache->e_ident[EI_CLASS] == ELFCLASS32
    101 				     ? 4 : 8);
    102 
    103 	Dwarf_Block block;
    104 	const uint8_t *p = fs->cache->data->d.d_buf + reg->value;
    105 	const uint8_t *end = (fs->cache->data->d.d_buf
    106 			      + fs->cache->data->d.d_size);
    107 	get_uleb128 (block.length, p, end);
    108 	block.data = (void *) p;
    109 
    110 	/* Parse the expression into internal form.  */
    111 	if (__libdw_intern_expression (NULL,
    112 				       fs->cache->other_byte_order,
    113 				       address_size, 4,
    114 				       &fs->cache->expr_tree, &block,
    115 				       true, reg->rule == reg_val_expression,
    116 				       ops, nops, IDX_debug_frame) < 0)
    117 	  return -1;
    118 	break;
    119       }
    120     }
    121 
    122   return 0;
    123 }
    124