Fix PR symtab/15391
[external/binutils.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3    Copyright (C) 2003-2013 Free Software Foundation, Inc.
4
5    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "inferior.h"
29 #include "ax.h"
30 #include "ax-gdb.h"
31 #include "regcache.h"
32 #include "objfiles.h"
33 #include "exceptions.h"
34 #include "block.h"
35 #include "gdbcmd.h"
36
37 #include "dwarf2.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
41
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44
45 DEF_VEC_I(int);
46
47 extern int dwarf2_always_disassemble;
48
49 static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
50                                      const gdb_byte **start, size_t *length);
51
52 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
53
54 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
55                                                     struct frame_info *frame,
56                                                     const gdb_byte *data,
57                                                     size_t size,
58                                                     struct dwarf2_per_cu_data *per_cu,
59                                                     LONGEST byte_offset);
60
61 /* Until these have formal names, we define these here.
62    ref: http://gcc.gnu.org/wiki/DebugFission
63    Each entry in .debug_loc.dwo begins with a byte that describes the entry,
64    and is then followed by data specific to that entry.  */
65
66 enum debug_loc_kind
67 {
68   /* Indicates the end of the list of entries.  */
69   DEBUG_LOC_END_OF_LIST = 0,
70
71   /* This is followed by an unsigned LEB128 number that is an index into
72      .debug_addr and specifies the base address for all following entries.  */
73   DEBUG_LOC_BASE_ADDRESS = 1,
74
75   /* This is followed by two unsigned LEB128 numbers that are indices into
76      .debug_addr and specify the beginning and ending addresses, and then
77      a normal location expression as in .debug_loc.  */
78   DEBUG_LOC_START_END = 2,
79
80   /* This is followed by an unsigned LEB128 number that is an index into
81      .debug_addr and specifies the beginning address, and a 4 byte unsigned
82      number that specifies the length, and then a normal location expression
83      as in .debug_loc.  */
84   DEBUG_LOC_START_LENGTH = 3,
85
86   /* An internal value indicating there is insufficient data.  */
87   DEBUG_LOC_BUFFER_OVERFLOW = -1,
88
89   /* An internal value indicating an invalid kind of entry was found.  */
90   DEBUG_LOC_INVALID_ENTRY = -2
91 };
92
93 /* Helper function which throws an error if a synthetic pointer is
94    invalid.  */
95
96 static void
97 invalid_synthetic_pointer (void)
98 {
99   error (_("access outside bounds of object "
100            "referenced via synthetic pointer"));
101 }
102
103 /* Decode the addresses in a non-dwo .debug_loc entry.
104    A pointer to the next byte to examine is returned in *NEW_PTR.
105    The encoded low,high addresses are return in *LOW,*HIGH.
106    The result indicates the kind of entry found.  */
107
108 static enum debug_loc_kind
109 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
110                             const gdb_byte **new_ptr,
111                             CORE_ADDR *low, CORE_ADDR *high,
112                             enum bfd_endian byte_order,
113                             unsigned int addr_size,
114                             int signed_addr_p)
115 {
116   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
117
118   if (buf_end - loc_ptr < 2 * addr_size)
119     return DEBUG_LOC_BUFFER_OVERFLOW;
120
121   if (signed_addr_p)
122     *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
123   else
124     *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
125   loc_ptr += addr_size;
126
127   if (signed_addr_p)
128     *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
129   else
130     *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
131   loc_ptr += addr_size;
132
133   *new_ptr = loc_ptr;
134
135   /* A base-address-selection entry.  */
136   if ((*low & base_mask) == base_mask)
137     return DEBUG_LOC_BASE_ADDRESS;
138
139   /* An end-of-list entry.  */
140   if (*low == 0 && *high == 0)
141     return DEBUG_LOC_END_OF_LIST;
142
143   return DEBUG_LOC_START_END;
144 }
145
146 /* Decode the addresses in .debug_loc.dwo entry.
147    A pointer to the next byte to examine is returned in *NEW_PTR.
148    The encoded low,high addresses are return in *LOW,*HIGH.
149    The result indicates the kind of entry found.  */
150
151 static enum debug_loc_kind
152 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
153                                 const gdb_byte *loc_ptr,
154                                 const gdb_byte *buf_end,
155                                 const gdb_byte **new_ptr,
156                                 CORE_ADDR *low, CORE_ADDR *high,
157                                 enum bfd_endian byte_order)
158 {
159   uint64_t low_index, high_index;
160
161   if (loc_ptr == buf_end)
162     return DEBUG_LOC_BUFFER_OVERFLOW;
163
164   switch (*loc_ptr++)
165     {
166     case DEBUG_LOC_END_OF_LIST:
167       *new_ptr = loc_ptr;
168       return DEBUG_LOC_END_OF_LIST;
169     case DEBUG_LOC_BASE_ADDRESS:
170       *low = 0;
171       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
172       if (loc_ptr == NULL)
173         return DEBUG_LOC_BUFFER_OVERFLOW;
174       *high = dwarf2_read_addr_index (per_cu, high_index);
175       *new_ptr = loc_ptr;
176       return DEBUG_LOC_BASE_ADDRESS;
177     case DEBUG_LOC_START_END:
178       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
179       if (loc_ptr == NULL)
180         return DEBUG_LOC_BUFFER_OVERFLOW;
181       *low = dwarf2_read_addr_index (per_cu, low_index);
182       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
183       if (loc_ptr == NULL)
184         return DEBUG_LOC_BUFFER_OVERFLOW;
185       *high = dwarf2_read_addr_index (per_cu, high_index);
186       *new_ptr = loc_ptr;
187       return DEBUG_LOC_START_END;
188     case DEBUG_LOC_START_LENGTH:
189       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
190       if (loc_ptr == NULL)
191         return DEBUG_LOC_BUFFER_OVERFLOW;
192       *low = dwarf2_read_addr_index (per_cu, low_index);
193       if (loc_ptr + 4 > buf_end)
194         return DEBUG_LOC_BUFFER_OVERFLOW;
195       *high = *low;
196       *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
197       *new_ptr = loc_ptr + 4;
198       return DEBUG_LOC_START_LENGTH;
199     default:
200       return DEBUG_LOC_INVALID_ENTRY;
201     }
202 }
203
204 /* A function for dealing with location lists.  Given a
205    symbol baton (BATON) and a pc value (PC), find the appropriate
206    location expression, set *LOCEXPR_LENGTH, and return a pointer
207    to the beginning of the expression.  Returns NULL on failure.
208
209    For now, only return the first matching location expression; there
210    can be more than one in the list.  */
211
212 const gdb_byte *
213 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
214                                  size_t *locexpr_length, CORE_ADDR pc)
215 {
216   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
217   struct gdbarch *gdbarch = get_objfile_arch (objfile);
218   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
219   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
220   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
221   /* Adjust base_address for relocatable objects.  */
222   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
223   CORE_ADDR base_address = baton->base_address + base_offset;
224   const gdb_byte *loc_ptr, *buf_end;
225
226   loc_ptr = baton->data;
227   buf_end = baton->data + baton->size;
228
229   while (1)
230     {
231       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
232       int length;
233       enum debug_loc_kind kind;
234       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
235
236       if (baton->from_dwo)
237         kind = decode_debug_loc_dwo_addresses (baton->per_cu,
238                                                loc_ptr, buf_end, &new_ptr,
239                                                &low, &high, byte_order);
240       else
241         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
242                                            &low, &high,
243                                            byte_order, addr_size,
244                                            signed_addr_p);
245       loc_ptr = new_ptr;
246       switch (kind)
247         {
248         case DEBUG_LOC_END_OF_LIST:
249           *locexpr_length = 0;
250           return NULL;
251         case DEBUG_LOC_BASE_ADDRESS:
252           base_address = high + base_offset;
253           continue;
254         case DEBUG_LOC_START_END:
255         case DEBUG_LOC_START_LENGTH:
256           break;
257         case DEBUG_LOC_BUFFER_OVERFLOW:
258         case DEBUG_LOC_INVALID_ENTRY:
259           error (_("dwarf2_find_location_expression: "
260                    "Corrupted DWARF expression."));
261         default:
262           gdb_assert_not_reached ("bad debug_loc_kind");
263         }
264
265       /* Otherwise, a location expression entry.
266          If the entry is from a DWO, don't add base address: the entry is
267          from .debug_addr which has absolute addresses.  */
268       if (! baton->from_dwo)
269         {
270           low += base_address;
271           high += base_address;
272         }
273
274       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
275       loc_ptr += 2;
276
277       if (low == high && pc == low)
278         {
279           /* This is entry PC record present only at entry point
280              of a function.  Verify it is really the function entry point.  */
281
282           struct block *pc_block = block_for_pc (pc);
283           struct symbol *pc_func = NULL;
284
285           if (pc_block)
286             pc_func = block_linkage_function (pc_block);
287
288           if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
289             {
290               *locexpr_length = length;
291               return loc_ptr;
292             }
293         }
294
295       if (pc >= low && pc < high)
296         {
297           *locexpr_length = length;
298           return loc_ptr;
299         }
300
301       loc_ptr += length;
302     }
303 }
304
305 /* This is the baton used when performing dwarf2 expression
306    evaluation.  */
307 struct dwarf_expr_baton
308 {
309   struct frame_info *frame;
310   struct dwarf2_per_cu_data *per_cu;
311 };
312
313 /* Helper functions for dwarf2_evaluate_loc_desc.  */
314
315 /* Using the frame specified in BATON, return the value of register
316    REGNUM, treated as a pointer.  */
317 static CORE_ADDR
318 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
319 {
320   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
321   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
322   CORE_ADDR result;
323   int regnum;
324
325   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
326   result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
327                                   regnum, debaton->frame);
328   return result;
329 }
330
331 /* Read memory at ADDR (length LEN) into BUF.  */
332
333 static void
334 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
335 {
336   read_memory (addr, buf, len);
337 }
338
339 /* Using the frame specified in BATON, find the location expression
340    describing the frame base.  Return a pointer to it in START and
341    its length in LENGTH.  */
342 static void
343 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
344 {
345   /* FIXME: cagney/2003-03-26: This code should be using
346      get_frame_base_address(), and then implement a dwarf2 specific
347      this_base method.  */
348   struct symbol *framefunc;
349   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
350   struct block *bl = get_frame_block (debaton->frame, NULL);
351
352   if (bl == NULL)
353     error (_("frame address is not available."));
354
355   /* Use block_linkage_function, which returns a real (not inlined)
356      function, instead of get_frame_function, which may return an
357      inlined function.  */
358   framefunc = block_linkage_function (bl);
359
360   /* If we found a frame-relative symbol then it was certainly within
361      some function associated with a frame. If we can't find the frame,
362      something has gone wrong.  */
363   gdb_assert (framefunc != NULL);
364
365   dwarf_expr_frame_base_1 (framefunc,
366                            get_frame_address_in_block (debaton->frame),
367                            start, length);
368 }
369
370 /* Implement find_frame_base_location method for LOC_BLOCK functions using
371    DWARF expression for its DW_AT_frame_base.  */
372
373 static void
374 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
375                                   const gdb_byte **start, size_t *length)
376 {
377   struct dwarf2_locexpr_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
378
379   *length = symbaton->size;
380   *start = symbaton->data;
381 }
382
383 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
384    function uses DWARF expression for its DW_AT_frame_base.  */
385
386 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
387 {
388   locexpr_find_frame_base_location
389 };
390
391 /* Implement find_frame_base_location method for LOC_BLOCK functions using
392    DWARF location list for its DW_AT_frame_base.  */
393
394 static void
395 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
396                                   const gdb_byte **start, size_t *length)
397 {
398   struct dwarf2_loclist_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
399
400   *start = dwarf2_find_location_expression (symbaton, length, pc);
401 }
402
403 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
404    function uses DWARF location list for its DW_AT_frame_base.  */
405
406 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
407 {
408   loclist_find_frame_base_location
409 };
410
411 static void
412 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
413                          const gdb_byte **start, size_t *length)
414 {
415   if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
416     {
417       const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
418
419       ops_block->find_frame_base_location (framefunc, pc, start, length);
420     }
421   else
422     *length = 0;
423
424   if (*length == 0)
425     error (_("Could not find the frame base for \"%s\"."),
426            SYMBOL_NATURAL_NAME (framefunc));
427 }
428
429 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
430    the frame in BATON.  */
431
432 static CORE_ADDR
433 dwarf_expr_frame_cfa (void *baton)
434 {
435   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
436
437   return dwarf2_frame_cfa (debaton->frame);
438 }
439
440 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the PC for
441    the frame in BATON.  */
442
443 static CORE_ADDR
444 dwarf_expr_frame_pc (void *baton)
445 {
446   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
447
448   return get_frame_address_in_block (debaton->frame);
449 }
450
451 /* Using the objfile specified in BATON, find the address for the
452    current thread's thread-local storage with offset OFFSET.  */
453 static CORE_ADDR
454 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
455 {
456   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
457   struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
458
459   return target_translate_tls_address (objfile, offset);
460 }
461
462 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
463    current CU (as is PER_CU).  State of the CTX is not affected by the
464    call and return.  */
465
466 static void
467 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
468                    struct dwarf2_per_cu_data *per_cu,
469                    CORE_ADDR (*get_frame_pc) (void *baton),
470                    void *baton)
471 {
472   struct dwarf2_locexpr_baton block;
473
474   block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
475
476   /* DW_OP_call_ref is currently not supported.  */
477   gdb_assert (block.per_cu == per_cu);
478
479   dwarf_expr_eval (ctx, block.data, block.size);
480 }
481
482 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc.  */
483
484 static void
485 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
486 {
487   struct dwarf_expr_baton *debaton = ctx->baton;
488
489   per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
490                      ctx->funcs->get_frame_pc, ctx->baton);
491 }
492
493 /* Callback function for dwarf2_evaluate_loc_desc.  */
494
495 static struct type *
496 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
497                           cu_offset die_offset)
498 {
499   struct dwarf_expr_baton *debaton = ctx->baton;
500
501   return dwarf2_get_die_type (die_offset, debaton->per_cu);
502 }
503
504 /* See dwarf2loc.h.  */
505
506 unsigned int entry_values_debug = 0;
507
508 /* Helper to set entry_values_debug.  */
509
510 static void
511 show_entry_values_debug (struct ui_file *file, int from_tty,
512                          struct cmd_list_element *c, const char *value)
513 {
514   fprintf_filtered (file,
515                     _("Entry values and tail call frames debugging is %s.\n"),
516                     value);
517 }
518
519 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
520    CALLER_FRAME (for registers) can be NULL if it is not known.  This function
521    always returns valid address or it throws NO_ENTRY_VALUE_ERROR.  */
522
523 static CORE_ADDR
524 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
525                           struct call_site *call_site,
526                           struct frame_info *caller_frame)
527 {
528   switch (FIELD_LOC_KIND (call_site->target))
529     {
530     case FIELD_LOC_KIND_DWARF_BLOCK:
531       {
532         struct dwarf2_locexpr_baton *dwarf_block;
533         struct value *val;
534         struct type *caller_core_addr_type;
535         struct gdbarch *caller_arch;
536
537         dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
538         if (dwarf_block == NULL)
539           {
540             struct bound_minimal_symbol msym;
541             
542             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
543             throw_error (NO_ENTRY_VALUE_ERROR,
544                          _("DW_AT_GNU_call_site_target is not specified "
545                            "at %s in %s"),
546                          paddress (call_site_gdbarch, call_site->pc),
547                          (msym.minsym == NULL ? "???"
548                           : SYMBOL_PRINT_NAME (msym.minsym)));
549                         
550           }
551         if (caller_frame == NULL)
552           {
553             struct bound_minimal_symbol msym;
554             
555             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
556             throw_error (NO_ENTRY_VALUE_ERROR,
557                          _("DW_AT_GNU_call_site_target DWARF block resolving "
558                            "requires known frame which is currently not "
559                            "available at %s in %s"),
560                          paddress (call_site_gdbarch, call_site->pc),
561                          (msym.minsym == NULL ? "???"
562                           : SYMBOL_PRINT_NAME (msym.minsym)));
563                         
564           }
565         caller_arch = get_frame_arch (caller_frame);
566         caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
567         val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
568                                         dwarf_block->data, dwarf_block->size,
569                                         dwarf_block->per_cu);
570         /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
571            location.  */
572         if (VALUE_LVAL (val) == lval_memory)
573           return value_address (val);
574         else
575           return value_as_address (val);
576       }
577
578     case FIELD_LOC_KIND_PHYSNAME:
579       {
580         const char *physname;
581         struct minimal_symbol *msym;
582
583         physname = FIELD_STATIC_PHYSNAME (call_site->target);
584
585         /* Handle both the mangled and demangled PHYSNAME.  */
586         msym = lookup_minimal_symbol (physname, NULL, NULL);
587         if (msym == NULL)
588           {
589             msym = lookup_minimal_symbol_by_pc (call_site->pc - 1).minsym;
590             throw_error (NO_ENTRY_VALUE_ERROR,
591                          _("Cannot find function \"%s\" for a call site target "
592                            "at %s in %s"),
593                          physname, paddress (call_site_gdbarch, call_site->pc),
594                          msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
595                         
596           }
597         return SYMBOL_VALUE_ADDRESS (msym);
598       }
599
600     case FIELD_LOC_KIND_PHYSADDR:
601       return FIELD_STATIC_PHYSADDR (call_site->target);
602
603     default:
604       internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
605     }
606 }
607
608 /* Convert function entry point exact address ADDR to the function which is
609    compliant with TAIL_CALL_LIST_COMPLETE condition.  Throw
610    NO_ENTRY_VALUE_ERROR otherwise.  */
611
612 static struct symbol *
613 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
614 {
615   struct symbol *sym = find_pc_function (addr);
616   struct type *type;
617
618   if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
619     throw_error (NO_ENTRY_VALUE_ERROR,
620                  _("DW_TAG_GNU_call_site resolving failed to find function "
621                    "name for address %s"),
622                  paddress (gdbarch, addr));
623
624   type = SYMBOL_TYPE (sym);
625   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
626   gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
627
628   return sym;
629 }
630
631 /* Verify function with entry point exact address ADDR can never call itself
632    via its tail calls (incl. transitively).  Throw NO_ENTRY_VALUE_ERROR if it
633    can call itself via tail calls.
634
635    If a funtion can tail call itself its entry value based parameters are
636    unreliable.  There is no verification whether the value of some/all
637    parameters is unchanged through the self tail call, we expect if there is
638    a self tail call all the parameters can be modified.  */
639
640 static void
641 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
642 {
643   struct obstack addr_obstack;
644   struct cleanup *old_chain;
645   CORE_ADDR addr;
646
647   /* Track here CORE_ADDRs which were already visited.  */
648   htab_t addr_hash;
649
650   /* The verification is completely unordered.  Track here function addresses
651      which still need to be iterated.  */
652   VEC (CORE_ADDR) *todo = NULL;
653
654   obstack_init (&addr_obstack);
655   old_chain = make_cleanup_obstack_free (&addr_obstack);   
656   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
657                                     &addr_obstack, hashtab_obstack_allocate,
658                                     NULL);
659   make_cleanup_htab_delete (addr_hash);
660
661   make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
662
663   VEC_safe_push (CORE_ADDR, todo, verify_addr);
664   while (!VEC_empty (CORE_ADDR, todo))
665     {
666       struct symbol *func_sym;
667       struct call_site *call_site;
668
669       addr = VEC_pop (CORE_ADDR, todo);
670
671       func_sym = func_addr_to_tail_call_list (gdbarch, addr);
672
673       for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
674            call_site; call_site = call_site->tail_call_next)
675         {
676           CORE_ADDR target_addr;
677           void **slot;
678
679           /* CALLER_FRAME with registers is not available for tail-call jumped
680              frames.  */
681           target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
682
683           if (target_addr == verify_addr)
684             {
685               struct bound_minimal_symbol msym;
686               
687               msym = lookup_minimal_symbol_by_pc (verify_addr);
688               throw_error (NO_ENTRY_VALUE_ERROR,
689                            _("DW_OP_GNU_entry_value resolving has found "
690                              "function \"%s\" at %s can call itself via tail "
691                              "calls"),
692                            (msym.minsym == NULL ? "???"
693                             : SYMBOL_PRINT_NAME (msym.minsym)),
694                            paddress (gdbarch, verify_addr));
695             }
696
697           slot = htab_find_slot (addr_hash, &target_addr, INSERT);
698           if (*slot == NULL)
699             {
700               *slot = obstack_copy (&addr_obstack, &target_addr,
701                                     sizeof (target_addr));
702               VEC_safe_push (CORE_ADDR, todo, target_addr);
703             }
704         }
705     }
706
707   do_cleanups (old_chain);
708 }
709
710 /* Print user readable form of CALL_SITE->PC to gdb_stdlog.  Used only for
711    ENTRY_VALUES_DEBUG.  */
712
713 static void
714 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
715 {
716   CORE_ADDR addr = call_site->pc;
717   struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
718
719   fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
720                       (msym.minsym == NULL ? "???"
721                        : SYMBOL_PRINT_NAME (msym.minsym)));
722
723 }
724
725 /* vec.h needs single word type name, typedef it.  */
726 typedef struct call_site *call_sitep;
727
728 /* Define VEC (call_sitep) functions.  */
729 DEF_VEC_P (call_sitep);
730
731 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
732    only top callers and bottom callees which are present in both.  GDBARCH is
733    used only for ENTRY_VALUES_DEBUG.  RESULTP is NULL after return if there are
734    no remaining possibilities to provide unambiguous non-trivial result.
735    RESULTP should point to NULL on the first (initialization) call.  Caller is
736    responsible for xfree of any RESULTP data.  */
737
738 static void
739 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
740                  VEC (call_sitep) *chain)
741 {
742   struct call_site_chain *result = *resultp;
743   long length = VEC_length (call_sitep, chain);
744   int callers, callees, idx;
745
746   if (result == NULL)
747     {
748       /* Create the initial chain containing all the passed PCs.  */
749
750       result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
751                                            * (length - 1));
752       result->length = length;
753       result->callers = result->callees = length;
754       memcpy (result->call_site, VEC_address (call_sitep, chain),
755               sizeof (*result->call_site) * length);
756       *resultp = result;
757
758       if (entry_values_debug)
759         {
760           fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
761           for (idx = 0; idx < length; idx++)
762             tailcall_dump (gdbarch, result->call_site[idx]);
763           fputc_unfiltered ('\n', gdb_stdlog);
764         }
765
766       return;
767     }
768
769   if (entry_values_debug)
770     {
771       fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
772       for (idx = 0; idx < length; idx++)
773         tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
774       fputc_unfiltered ('\n', gdb_stdlog);
775     }
776
777   /* Intersect callers.  */
778
779   callers = min (result->callers, length);
780   for (idx = 0; idx < callers; idx++)
781     if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
782       {
783         result->callers = idx;
784         break;
785       }
786
787   /* Intersect callees.  */
788
789   callees = min (result->callees, length);
790   for (idx = 0; idx < callees; idx++)
791     if (result->call_site[result->length - 1 - idx]
792         != VEC_index (call_sitep, chain, length - 1 - idx))
793       {
794         result->callees = idx;
795         break;
796       }
797
798   if (entry_values_debug)
799     {
800       fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
801       for (idx = 0; idx < result->callers; idx++)
802         tailcall_dump (gdbarch, result->call_site[idx]);
803       fputs_unfiltered (" |", gdb_stdlog);
804       for (idx = 0; idx < result->callees; idx++)
805         tailcall_dump (gdbarch, result->call_site[result->length
806                                                   - result->callees + idx]);
807       fputc_unfiltered ('\n', gdb_stdlog);
808     }
809
810   if (result->callers == 0 && result->callees == 0)
811     {
812       /* There are no common callers or callees.  It could be also a direct
813          call (which has length 0) with ambiguous possibility of an indirect
814          call - CALLERS == CALLEES == 0 is valid during the first allocation
815          but any subsequence processing of such entry means ambiguity.  */
816       xfree (result);
817       *resultp = NULL;
818       return;
819     }
820
821   /* See call_site_find_chain_1 why there is no way to reach the bottom callee
822      PC again.  In such case there must be two different code paths to reach
823      it, therefore some of the former determined intermediate PCs must differ
824      and the unambiguous chain gets shortened.  */
825   gdb_assert (result->callers + result->callees < result->length);
826 }
827
828 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
829    assumed frames between them use GDBARCH.  Use depth first search so we can
830    keep single CHAIN of call_site's back to CALLER_PC.  Function recursion
831    would have needless GDB stack overhead.  Caller is responsible for xfree of
832    the returned result.  Any unreliability results in thrown
833    NO_ENTRY_VALUE_ERROR.  */
834
835 static struct call_site_chain *
836 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
837                         CORE_ADDR callee_pc)
838 {
839   CORE_ADDR save_callee_pc = callee_pc;
840   struct obstack addr_obstack;
841   struct cleanup *back_to_retval, *back_to_workdata;
842   struct call_site_chain *retval = NULL;
843   struct call_site *call_site;
844
845   /* Mark CALL_SITEs so we do not visit the same ones twice.  */
846   htab_t addr_hash;
847
848   /* CHAIN contains only the intermediate CALL_SITEs.  Neither CALLER_PC's
849      call_site nor any possible call_site at CALLEE_PC's function is there.
850      Any CALL_SITE in CHAIN will be iterated to its siblings - via
851      TAIL_CALL_NEXT.  This is inappropriate for CALLER_PC's call_site.  */
852   VEC (call_sitep) *chain = NULL;
853
854   /* We are not interested in the specific PC inside the callee function.  */
855   callee_pc = get_pc_function_start (callee_pc);
856   if (callee_pc == 0)
857     throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
858                  paddress (gdbarch, save_callee_pc));
859
860   back_to_retval = make_cleanup (free_current_contents, &retval);
861
862   obstack_init (&addr_obstack);
863   back_to_workdata = make_cleanup_obstack_free (&addr_obstack);   
864   addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
865                                     &addr_obstack, hashtab_obstack_allocate,
866                                     NULL);
867   make_cleanup_htab_delete (addr_hash);
868
869   make_cleanup (VEC_cleanup (call_sitep), &chain);
870
871   /* Do not push CALL_SITE to CHAIN.  Push there only the first tail call site
872      at the target's function.  All the possible tail call sites in the
873      target's function will get iterated as already pushed into CHAIN via their
874      TAIL_CALL_NEXT.  */
875   call_site = call_site_for_pc (gdbarch, caller_pc);
876
877   while (call_site)
878     {
879       CORE_ADDR target_func_addr;
880       struct call_site *target_call_site;
881
882       /* CALLER_FRAME with registers is not available for tail-call jumped
883          frames.  */
884       target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
885
886       if (target_func_addr == callee_pc)
887         {
888           chain_candidate (gdbarch, &retval, chain);
889           if (retval == NULL)
890             break;
891
892           /* There is no way to reach CALLEE_PC again as we would prevent
893              entering it twice as being already marked in ADDR_HASH.  */
894           target_call_site = NULL;
895         }
896       else
897         {
898           struct symbol *target_func;
899
900           target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
901           target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
902         }
903
904       do
905         {
906           /* Attempt to visit TARGET_CALL_SITE.  */
907
908           if (target_call_site)
909             {
910               void **slot;
911
912               slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
913               if (*slot == NULL)
914                 {
915                   /* Successfully entered TARGET_CALL_SITE.  */
916
917                   *slot = &target_call_site->pc;
918                   VEC_safe_push (call_sitep, chain, target_call_site);
919                   break;
920                 }
921             }
922
923           /* Backtrack (without revisiting the originating call_site).  Try the
924              callers's sibling; if there isn't any try the callers's callers's
925              sibling etc.  */
926
927           target_call_site = NULL;
928           while (!VEC_empty (call_sitep, chain))
929             {
930               call_site = VEC_pop (call_sitep, chain);
931
932               gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
933                                           NO_INSERT) != NULL);
934               htab_remove_elt (addr_hash, &call_site->pc);
935
936               target_call_site = call_site->tail_call_next;
937               if (target_call_site)
938                 break;
939             }
940         }
941       while (target_call_site);
942
943       if (VEC_empty (call_sitep, chain))
944         call_site = NULL;
945       else
946         call_site = VEC_last (call_sitep, chain);
947     }
948
949   if (retval == NULL)
950     {
951       struct bound_minimal_symbol msym_caller, msym_callee;
952       
953       msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
954       msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
955       throw_error (NO_ENTRY_VALUE_ERROR,
956                    _("There are no unambiguously determinable intermediate "
957                      "callers or callees between caller function \"%s\" at %s "
958                      "and callee function \"%s\" at %s"),
959                    (msym_caller.minsym == NULL
960                     ? "???" : SYMBOL_PRINT_NAME (msym_caller.minsym)),
961                    paddress (gdbarch, caller_pc),
962                    (msym_callee.minsym == NULL
963                     ? "???" : SYMBOL_PRINT_NAME (msym_callee.minsym)),
964                    paddress (gdbarch, callee_pc));
965     }
966
967   do_cleanups (back_to_workdata);
968   discard_cleanups (back_to_retval);
969   return retval;
970 }
971
972 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
973    assumed frames between them use GDBARCH.  If valid call_site_chain cannot be
974    constructed return NULL.  Caller is responsible for xfree of the returned
975    result.  */
976
977 struct call_site_chain *
978 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
979                       CORE_ADDR callee_pc)
980 {
981   volatile struct gdb_exception e;
982   struct call_site_chain *retval = NULL;
983
984   TRY_CATCH (e, RETURN_MASK_ERROR)
985     {
986       retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
987     }
988   if (e.reason < 0)
989     {
990       if (e.error == NO_ENTRY_VALUE_ERROR)
991         {
992           if (entry_values_debug)
993             exception_print (gdb_stdout, e);
994
995           return NULL;
996         }
997       else
998         throw_exception (e);
999     }
1000   return retval;
1001 }
1002
1003 /* Return 1 if KIND and KIND_U match PARAMETER.  Return 0 otherwise.  */
1004
1005 static int
1006 call_site_parameter_matches (struct call_site_parameter *parameter,
1007                              enum call_site_parameter_kind kind,
1008                              union call_site_parameter_u kind_u)
1009 {
1010   if (kind == parameter->kind)
1011     switch (kind)
1012       {
1013       case CALL_SITE_PARAMETER_DWARF_REG:
1014         return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1015       case CALL_SITE_PARAMETER_FB_OFFSET:
1016         return kind_u.fb_offset == parameter->u.fb_offset;
1017       case CALL_SITE_PARAMETER_PARAM_OFFSET:
1018         return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
1019       }
1020   return 0;
1021 }
1022
1023 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1024    FRAME is for callee.
1025
1026    Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1027    otherwise.  */
1028
1029 static struct call_site_parameter *
1030 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1031                                    enum call_site_parameter_kind kind,
1032                                    union call_site_parameter_u kind_u,
1033                                    struct dwarf2_per_cu_data **per_cu_return)
1034 {
1035   CORE_ADDR func_addr, caller_pc;
1036   struct gdbarch *gdbarch;
1037   struct frame_info *caller_frame;
1038   struct call_site *call_site;
1039   int iparams;
1040   /* Initialize it just to avoid a GCC false warning.  */
1041   struct call_site_parameter *parameter = NULL;
1042   CORE_ADDR target_addr;
1043
1044   while (get_frame_type (frame) == INLINE_FRAME)
1045     {
1046       frame = get_prev_frame (frame);
1047       gdb_assert (frame != NULL);
1048     }
1049
1050   func_addr = get_frame_func (frame);
1051   gdbarch = get_frame_arch (frame);
1052   caller_frame = get_prev_frame (frame);
1053   if (gdbarch != frame_unwind_arch (frame))
1054     {
1055       struct bound_minimal_symbol msym
1056         = lookup_minimal_symbol_by_pc (func_addr);
1057       struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1058
1059       throw_error (NO_ENTRY_VALUE_ERROR,
1060                    _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1061                      "(of %s (%s)) does not match caller gdbarch %s"),
1062                    gdbarch_bfd_arch_info (gdbarch)->printable_name,
1063                    paddress (gdbarch, func_addr),
1064                    (msym.minsym == NULL ? "???"
1065                     : SYMBOL_PRINT_NAME (msym.minsym)),
1066                    gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1067     }
1068
1069   if (caller_frame == NULL)
1070     {
1071       struct bound_minimal_symbol msym
1072         = lookup_minimal_symbol_by_pc (func_addr);
1073
1074       throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
1075                                            "requires caller of %s (%s)"),
1076                    paddress (gdbarch, func_addr),
1077                    (msym.minsym == NULL ? "???"
1078                     : SYMBOL_PRINT_NAME (msym.minsym)));
1079     }
1080   caller_pc = get_frame_pc (caller_frame);
1081   call_site = call_site_for_pc (gdbarch, caller_pc);
1082
1083   target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1084   if (target_addr != func_addr)
1085     {
1086       struct minimal_symbol *target_msym, *func_msym;
1087
1088       target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1089       func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1090       throw_error (NO_ENTRY_VALUE_ERROR,
1091                    _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1092                      "but the called frame is for %s at %s"),
1093                    (target_msym == NULL ? "???"
1094                                         : SYMBOL_PRINT_NAME (target_msym)),
1095                    paddress (gdbarch, target_addr),
1096                    func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
1097                    paddress (gdbarch, func_addr));
1098     }
1099
1100   /* No entry value based parameters would be reliable if this function can
1101      call itself via tail calls.  */
1102   func_verify_no_selftailcall (gdbarch, func_addr);
1103
1104   for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1105     {
1106       parameter = &call_site->parameter[iparams];
1107       if (call_site_parameter_matches (parameter, kind, kind_u))
1108         break;
1109     }
1110   if (iparams == call_site->parameter_count)
1111     {
1112       struct minimal_symbol *msym
1113         = lookup_minimal_symbol_by_pc (caller_pc).minsym;
1114
1115       /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1116          determine its value.  */
1117       throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1118                                            "at DW_TAG_GNU_call_site %s at %s"),
1119                    paddress (gdbarch, caller_pc),
1120                    msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); 
1121     }
1122
1123   *per_cu_return = call_site->per_cu;
1124   return parameter;
1125 }
1126
1127 /* Return value for PARAMETER matching DEREF_SIZE.  If DEREF_SIZE is -1, return
1128    the normal DW_AT_GNU_call_site_value block.  Otherwise return the
1129    DW_AT_GNU_call_site_data_value (dereferenced) block.
1130
1131    TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1132    struct value.
1133
1134    Function always returns non-NULL, non-optimized out value.  It throws
1135    NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason.  */
1136
1137 static struct value *
1138 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1139                                 CORE_ADDR deref_size, struct type *type,
1140                                 struct frame_info *caller_frame,
1141                                 struct dwarf2_per_cu_data *per_cu)
1142 {
1143   const gdb_byte *data_src;
1144   gdb_byte *data;
1145   size_t size;
1146
1147   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1148   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1149
1150   /* DEREF_SIZE size is not verified here.  */
1151   if (data_src == NULL)
1152     throw_error (NO_ENTRY_VALUE_ERROR,
1153                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1154
1155   /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1156      location.  Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1157      DWARF block.  */
1158   data = alloca (size + 1);
1159   memcpy (data, data_src, size);
1160   data[size] = DW_OP_stack_value;
1161
1162   return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1163 }
1164
1165 /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
1166    Choose DEREF_SIZE value of that parameter.  Search caller of the CTX's
1167    frame.  CTX must be of dwarf_expr_ctx_funcs kind.
1168
1169    The CTX caller can be from a different CU - per_cu_dwarf_call implementation
1170    can be more simple as it does not support cross-CU DWARF executions.  */
1171
1172 static void
1173 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1174                                        enum call_site_parameter_kind kind,
1175                                        union call_site_parameter_u kind_u,
1176                                        int deref_size)
1177 {
1178   struct dwarf_expr_baton *debaton;
1179   struct frame_info *frame, *caller_frame;
1180   struct dwarf2_per_cu_data *caller_per_cu;
1181   struct dwarf_expr_baton baton_local;
1182   struct dwarf_expr_context saved_ctx;
1183   struct call_site_parameter *parameter;
1184   const gdb_byte *data_src;
1185   size_t size;
1186
1187   gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
1188   debaton = ctx->baton;
1189   frame = debaton->frame;
1190   caller_frame = get_prev_frame (frame);
1191
1192   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1193                                                  &caller_per_cu);
1194   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1195   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1196
1197   /* DEREF_SIZE size is not verified here.  */
1198   if (data_src == NULL)
1199     throw_error (NO_ENTRY_VALUE_ERROR,
1200                  _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1201
1202   baton_local.frame = caller_frame;
1203   baton_local.per_cu = caller_per_cu;
1204
1205   saved_ctx.gdbarch = ctx->gdbarch;
1206   saved_ctx.addr_size = ctx->addr_size;
1207   saved_ctx.offset = ctx->offset;
1208   saved_ctx.baton = ctx->baton;
1209   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1210   ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1211   ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1212   ctx->baton = &baton_local;
1213
1214   dwarf_expr_eval (ctx, data_src, size);
1215
1216   ctx->gdbarch = saved_ctx.gdbarch;
1217   ctx->addr_size = saved_ctx.addr_size;
1218   ctx->offset = saved_ctx.offset;
1219   ctx->baton = saved_ctx.baton;
1220 }
1221
1222 /* Callback function for dwarf2_evaluate_loc_desc.
1223    Fetch the address indexed by DW_OP_GNU_addr_index.  */
1224
1225 static CORE_ADDR
1226 dwarf_expr_get_addr_index (void *baton, unsigned int index)
1227 {
1228   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1229
1230   return dwarf2_read_addr_index (debaton->per_cu, index);
1231 }
1232
1233 /* VALUE must be of type lval_computed with entry_data_value_funcs.  Perform
1234    the indirect method on it, that is use its stored target value, the sole
1235    purpose of entry_data_value_funcs..  */
1236
1237 static struct value *
1238 entry_data_value_coerce_ref (const struct value *value)
1239 {
1240   struct type *checked_type = check_typedef (value_type (value));
1241   struct value *target_val;
1242
1243   if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1244     return NULL;
1245
1246   target_val = value_computed_closure (value);
1247   value_incref (target_val);
1248   return target_val;
1249 }
1250
1251 /* Implement copy_closure.  */
1252
1253 static void *
1254 entry_data_value_copy_closure (const struct value *v)
1255 {
1256   struct value *target_val = value_computed_closure (v);
1257
1258   value_incref (target_val);
1259   return target_val;
1260 }
1261
1262 /* Implement free_closure.  */
1263
1264 static void
1265 entry_data_value_free_closure (struct value *v)
1266 {
1267   struct value *target_val = value_computed_closure (v);
1268
1269   value_free (target_val);
1270 }
1271
1272 /* Vector for methods for an entry value reference where the referenced value
1273    is stored in the caller.  On the first dereference use
1274    DW_AT_GNU_call_site_data_value in the caller.  */
1275
1276 static const struct lval_funcs entry_data_value_funcs =
1277 {
1278   NULL, /* read */
1279   NULL, /* write */
1280   NULL, /* check_validity */
1281   NULL, /* check_any_valid */
1282   NULL, /* indirect */
1283   entry_data_value_coerce_ref,
1284   NULL, /* check_synthetic_pointer */
1285   entry_data_value_copy_closure,
1286   entry_data_value_free_closure
1287 };
1288
1289 /* Read parameter of TYPE at (callee) FRAME's function entry.  KIND and KIND_U
1290    are used to match DW_AT_location at the caller's
1291    DW_TAG_GNU_call_site_parameter.
1292
1293    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
1294    cannot resolve the parameter for any reason.  */
1295
1296 static struct value *
1297 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1298                           enum call_site_parameter_kind kind,
1299                           union call_site_parameter_u kind_u)
1300 {
1301   struct type *checked_type = check_typedef (type);
1302   struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1303   struct frame_info *caller_frame = get_prev_frame (frame);
1304   struct value *outer_val, *target_val, *val;
1305   struct call_site_parameter *parameter;
1306   struct dwarf2_per_cu_data *caller_per_cu;
1307   CORE_ADDR addr;
1308
1309   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1310                                                  &caller_per_cu);
1311
1312   outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1313                                               type, caller_frame,
1314                                               caller_per_cu);
1315
1316   /* Check if DW_AT_GNU_call_site_data_value cannot be used.  If it should be
1317      used and it is not available do not fall back to OUTER_VAL - dereferencing
1318      TYPE_CODE_REF with non-entry data value would give current value - not the
1319      entry value.  */
1320
1321   if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1322       || TYPE_TARGET_TYPE (checked_type) == NULL)
1323     return outer_val;
1324
1325   target_val = dwarf_entry_parameter_to_value (parameter,
1326                                                TYPE_LENGTH (target_type),
1327                                                target_type, caller_frame,
1328                                                caller_per_cu);
1329
1330   /* value_as_address dereferences TYPE_CODE_REF.  */
1331   addr = extract_typed_address (value_contents (outer_val), checked_type);
1332
1333   /* The target entry value has artificial address of the entry value
1334      reference.  */
1335   VALUE_LVAL (target_val) = lval_memory;
1336   set_value_address (target_val, addr);
1337
1338   release_value (target_val);
1339   val = allocate_computed_value (type, &entry_data_value_funcs,
1340                                  target_val /* closure */);
1341
1342   /* Copy the referencing pointer to the new computed value.  */
1343   memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1344           TYPE_LENGTH (checked_type));
1345   set_value_lazy (val, 0);
1346
1347   return val;
1348 }
1349
1350 /* Read parameter of TYPE at (callee) FRAME's function entry.  DATA and
1351    SIZE are DWARF block used to match DW_AT_location at the caller's
1352    DW_TAG_GNU_call_site_parameter.
1353
1354    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
1355    cannot resolve the parameter for any reason.  */
1356
1357 static struct value *
1358 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1359                             const gdb_byte *block, size_t block_len)
1360 {
1361   union call_site_parameter_u kind_u;
1362
1363   kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1364   if (kind_u.dwarf_reg != -1)
1365     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1366                                      kind_u);
1367
1368   if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1369     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1370                                      kind_u);
1371
1372   /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1373      suppressed during normal operation.  The expression can be arbitrary if
1374      there is no caller-callee entry value binding expected.  */
1375   throw_error (NO_ENTRY_VALUE_ERROR,
1376                _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1377                  "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1378 }
1379
1380 struct piece_closure
1381 {
1382   /* Reference count.  */
1383   int refc;
1384
1385   /* The CU from which this closure's expression came.  */
1386   struct dwarf2_per_cu_data *per_cu;
1387
1388   /* The number of pieces used to describe this variable.  */
1389   int n_pieces;
1390
1391   /* The target address size, used only for DWARF_VALUE_STACK.  */
1392   int addr_size;
1393
1394   /* The pieces themselves.  */
1395   struct dwarf_expr_piece *pieces;
1396 };
1397
1398 /* Allocate a closure for a value formed from separately-described
1399    PIECES.  */
1400
1401 static struct piece_closure *
1402 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1403                         int n_pieces, struct dwarf_expr_piece *pieces,
1404                         int addr_size)
1405 {
1406   struct piece_closure *c = XZALLOC (struct piece_closure);
1407   int i;
1408
1409   c->refc = 1;
1410   c->per_cu = per_cu;
1411   c->n_pieces = n_pieces;
1412   c->addr_size = addr_size;
1413   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
1414
1415   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1416   for (i = 0; i < n_pieces; ++i)
1417     if (c->pieces[i].location == DWARF_VALUE_STACK)
1418       value_incref (c->pieces[i].v.value);
1419
1420   return c;
1421 }
1422
1423 /* The lowest-level function to extract bits from a byte buffer.
1424    SOURCE is the buffer.  It is updated if we read to the end of a
1425    byte.
1426    SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
1427    updated to reflect the number of bits actually read.
1428    NBITS is the number of bits we want to read.  It is updated to
1429    reflect the number of bits actually read.  This function may read
1430    fewer bits.
1431    BITS_BIG_ENDIAN is taken directly from gdbarch.
1432    This function returns the extracted bits.  */
1433
1434 static unsigned int
1435 extract_bits_primitive (const gdb_byte **source,
1436                         unsigned int *source_offset_bits,
1437                         int *nbits, int bits_big_endian)
1438 {
1439   unsigned int avail, mask, datum;
1440
1441   gdb_assert (*source_offset_bits < 8);
1442
1443   avail = 8 - *source_offset_bits;
1444   if (avail > *nbits)
1445     avail = *nbits;
1446
1447   mask = (1 << avail) - 1;
1448   datum = **source;
1449   if (bits_big_endian)
1450     datum >>= 8 - (*source_offset_bits + *nbits);
1451   else
1452     datum >>= *source_offset_bits;
1453   datum &= mask;
1454
1455   *nbits -= avail;
1456   *source_offset_bits += avail;
1457   if (*source_offset_bits >= 8)
1458     {
1459       *source_offset_bits -= 8;
1460       ++*source;
1461     }
1462
1463   return datum;
1464 }
1465
1466 /* Extract some bits from a source buffer and move forward in the
1467    buffer.
1468    
1469    SOURCE is the source buffer.  It is updated as bytes are read.
1470    SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
1471    bits are read.
1472    NBITS is the number of bits to read.
1473    BITS_BIG_ENDIAN is taken directly from gdbarch.
1474    
1475    This function returns the bits that were read.  */
1476
1477 static unsigned int
1478 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1479               int nbits, int bits_big_endian)
1480 {
1481   unsigned int datum;
1482
1483   gdb_assert (nbits > 0 && nbits <= 8);
1484
1485   datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1486                                   bits_big_endian);
1487   if (nbits > 0)
1488     {
1489       unsigned int more;
1490
1491       more = extract_bits_primitive (source, source_offset_bits, &nbits,
1492                                      bits_big_endian);
1493       if (bits_big_endian)
1494         datum <<= nbits;
1495       else
1496         more <<= nbits;
1497       datum |= more;
1498     }
1499
1500   return datum;
1501 }
1502
1503 /* Write some bits into a buffer and move forward in the buffer.
1504    
1505    DATUM is the bits to write.  The low-order bits of DATUM are used.
1506    DEST is the destination buffer.  It is updated as bytes are
1507    written.
1508    DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1509    done.
1510    NBITS is the number of valid bits in DATUM.
1511    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
1512
1513 static void
1514 insert_bits (unsigned int datum,
1515              gdb_byte *dest, unsigned int dest_offset_bits,
1516              int nbits, int bits_big_endian)
1517 {
1518   unsigned int mask;
1519
1520   gdb_assert (dest_offset_bits + nbits <= 8);
1521
1522   mask = (1 << nbits) - 1;
1523   if (bits_big_endian)
1524     {
1525       datum <<= 8 - (dest_offset_bits + nbits);
1526       mask <<= 8 - (dest_offset_bits + nbits);
1527     }
1528   else
1529     {
1530       datum <<= dest_offset_bits;
1531       mask <<= dest_offset_bits;
1532     }
1533
1534   gdb_assert ((datum & ~mask) == 0);
1535
1536   *dest = (*dest & ~mask) | datum;
1537 }
1538
1539 /* Copy bits from a source to a destination.
1540    
1541    DEST is where the bits should be written.
1542    DEST_OFFSET_BITS is the bit offset into DEST.
1543    SOURCE is the source of bits.
1544    SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1545    BIT_COUNT is the number of bits to copy.
1546    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
1547
1548 static void
1549 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1550               const gdb_byte *source, unsigned int source_offset_bits,
1551               unsigned int bit_count,
1552               int bits_big_endian)
1553 {
1554   unsigned int dest_avail;
1555   int datum;
1556
1557   /* Reduce everything to byte-size pieces.  */
1558   dest += dest_offset_bits / 8;
1559   dest_offset_bits %= 8;
1560   source += source_offset_bits / 8;
1561   source_offset_bits %= 8;
1562
1563   dest_avail = 8 - dest_offset_bits % 8;
1564
1565   /* See if we can fill the first destination byte.  */
1566   if (dest_avail < bit_count)
1567     {
1568       datum = extract_bits (&source, &source_offset_bits, dest_avail,
1569                             bits_big_endian);
1570       insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1571       ++dest;
1572       dest_offset_bits = 0;
1573       bit_count -= dest_avail;
1574     }
1575
1576   /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1577      than 8 bits remaining.  */
1578   gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1579   for (; bit_count >= 8; bit_count -= 8)
1580     {
1581       datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1582       *dest++ = (gdb_byte) datum;
1583     }
1584
1585   /* Finally, we may have a few leftover bits.  */
1586   gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1587   if (bit_count > 0)
1588     {
1589       datum = extract_bits (&source, &source_offset_bits, bit_count,
1590                             bits_big_endian);
1591       insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1592     }
1593 }
1594
1595 static void
1596 read_pieced_value (struct value *v)
1597 {
1598   int i;
1599   long offset = 0;
1600   ULONGEST bits_to_skip;
1601   gdb_byte *contents;
1602   struct piece_closure *c
1603     = (struct piece_closure *) value_computed_closure (v);
1604   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1605   size_t type_len;
1606   size_t buffer_size = 0;
1607   gdb_byte *buffer = NULL;
1608   struct cleanup *cleanup;
1609   int bits_big_endian
1610     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1611
1612   if (value_type (v) != value_enclosing_type (v))
1613     internal_error (__FILE__, __LINE__,
1614                     _("Should not be able to create a lazy value with "
1615                       "an enclosing type"));
1616
1617   cleanup = make_cleanup (free_current_contents, &buffer);
1618
1619   contents = value_contents_raw (v);
1620   bits_to_skip = 8 * value_offset (v);
1621   if (value_bitsize (v))
1622     {
1623       bits_to_skip += value_bitpos (v);
1624       type_len = value_bitsize (v);
1625     }
1626   else
1627     type_len = 8 * TYPE_LENGTH (value_type (v));
1628
1629   for (i = 0; i < c->n_pieces && offset < type_len; i++)
1630     {
1631       struct dwarf_expr_piece *p = &c->pieces[i];
1632       size_t this_size, this_size_bits;
1633       long dest_offset_bits, source_offset_bits, source_offset;
1634       const gdb_byte *intermediate_buffer;
1635
1636       /* Compute size, source, and destination offsets for copying, in
1637          bits.  */
1638       this_size_bits = p->size;
1639       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1640         {
1641           bits_to_skip -= this_size_bits;
1642           continue;
1643         }
1644       if (bits_to_skip > 0)
1645         {
1646           dest_offset_bits = 0;
1647           source_offset_bits = bits_to_skip;
1648           this_size_bits -= bits_to_skip;
1649           bits_to_skip = 0;
1650         }
1651       else
1652         {
1653           dest_offset_bits = offset;
1654           source_offset_bits = 0;
1655         }
1656       if (this_size_bits > type_len - offset)
1657         this_size_bits = type_len - offset;
1658
1659       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1660       source_offset = source_offset_bits / 8;
1661       if (buffer_size < this_size)
1662         {
1663           buffer_size = this_size;
1664           buffer = xrealloc (buffer, buffer_size);
1665         }
1666       intermediate_buffer = buffer;
1667
1668       /* Copy from the source to DEST_BUFFER.  */
1669       switch (p->location)
1670         {
1671         case DWARF_VALUE_REGISTER:
1672           {
1673             struct gdbarch *arch = get_frame_arch (frame);
1674             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1675             int reg_offset = source_offset;
1676
1677             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1678                 && this_size < register_size (arch, gdb_regnum))
1679               {
1680                 /* Big-endian, and we want less than full size.  */
1681                 reg_offset = register_size (arch, gdb_regnum) - this_size;
1682                 /* We want the lower-order THIS_SIZE_BITS of the bytes
1683                    we extract from the register.  */
1684                 source_offset_bits += 8 * this_size - this_size_bits;
1685               }
1686
1687             if (gdb_regnum != -1)
1688               {
1689                 int optim, unavail;
1690
1691                 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1692                                                this_size, buffer,
1693                                                &optim, &unavail))
1694                   {
1695                     /* Just so garbage doesn't ever shine through.  */
1696                     memset (buffer, 0, this_size);
1697
1698                     if (optim)
1699                       set_value_optimized_out (v, 1);
1700                     if (unavail)
1701                       mark_value_bytes_unavailable (v, offset, this_size);
1702                   }
1703               }
1704             else
1705               {
1706                 error (_("Unable to access DWARF register number %s"),
1707                        paddress (arch, p->v.regno));
1708               }
1709           }
1710           break;
1711
1712         case DWARF_VALUE_MEMORY:
1713           read_value_memory (v, offset,
1714                              p->v.mem.in_stack_memory,
1715                              p->v.mem.addr + source_offset,
1716                              buffer, this_size);
1717           break;
1718
1719         case DWARF_VALUE_STACK:
1720           {
1721             size_t n = this_size;
1722
1723             if (n > c->addr_size - source_offset)
1724               n = (c->addr_size >= source_offset
1725                    ? c->addr_size - source_offset
1726                    : 0);
1727             if (n == 0)
1728               {
1729                 /* Nothing.  */
1730               }
1731             else
1732               {
1733                 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1734
1735                 intermediate_buffer = val_bytes + source_offset;
1736               }
1737           }
1738           break;
1739
1740         case DWARF_VALUE_LITERAL:
1741           {
1742             size_t n = this_size;
1743
1744             if (n > p->v.literal.length - source_offset)
1745               n = (p->v.literal.length >= source_offset
1746                    ? p->v.literal.length - source_offset
1747                    : 0);
1748             if (n != 0)
1749               intermediate_buffer = p->v.literal.data + source_offset;
1750           }
1751           break;
1752
1753           /* These bits show up as zeros -- but do not cause the value
1754              to be considered optimized-out.  */
1755         case DWARF_VALUE_IMPLICIT_POINTER:
1756           break;
1757
1758         case DWARF_VALUE_OPTIMIZED_OUT:
1759           set_value_optimized_out (v, 1);
1760           break;
1761
1762         default:
1763           internal_error (__FILE__, __LINE__, _("invalid location type"));
1764         }
1765
1766       if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1767           && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1768         copy_bitwise (contents, dest_offset_bits,
1769                       intermediate_buffer, source_offset_bits % 8,
1770                       this_size_bits, bits_big_endian);
1771
1772       offset += this_size_bits;
1773     }
1774
1775   do_cleanups (cleanup);
1776 }
1777
1778 static void
1779 write_pieced_value (struct value *to, struct value *from)
1780 {
1781   int i;
1782   long offset = 0;
1783   ULONGEST bits_to_skip;
1784   const gdb_byte *contents;
1785   struct piece_closure *c
1786     = (struct piece_closure *) value_computed_closure (to);
1787   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1788   size_t type_len;
1789   size_t buffer_size = 0;
1790   gdb_byte *buffer = NULL;
1791   struct cleanup *cleanup;
1792   int bits_big_endian
1793     = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1794
1795   if (frame == NULL)
1796     {
1797       set_value_optimized_out (to, 1);
1798       return;
1799     }
1800
1801   cleanup = make_cleanup (free_current_contents, &buffer);
1802
1803   contents = value_contents (from);
1804   bits_to_skip = 8 * value_offset (to);
1805   if (value_bitsize (to))
1806     {
1807       bits_to_skip += value_bitpos (to);
1808       type_len = value_bitsize (to);
1809     }
1810   else
1811     type_len = 8 * TYPE_LENGTH (value_type (to));
1812
1813   for (i = 0; i < c->n_pieces && offset < type_len; i++)
1814     {
1815       struct dwarf_expr_piece *p = &c->pieces[i];
1816       size_t this_size_bits, this_size;
1817       long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1818       int need_bitwise;
1819       const gdb_byte *source_buffer;
1820
1821       this_size_bits = p->size;
1822       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1823         {
1824           bits_to_skip -= this_size_bits;
1825           continue;
1826         }
1827       if (this_size_bits > type_len - offset)
1828         this_size_bits = type_len - offset;
1829       if (bits_to_skip > 0)
1830         {
1831           dest_offset_bits = bits_to_skip;
1832           source_offset_bits = 0;
1833           this_size_bits -= bits_to_skip;
1834           bits_to_skip = 0;
1835         }
1836       else
1837         {
1838           dest_offset_bits = 0;
1839           source_offset_bits = offset;
1840         }
1841
1842       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1843       source_offset = source_offset_bits / 8;
1844       dest_offset = dest_offset_bits / 8;
1845       if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1846         {
1847           source_buffer = contents + source_offset;
1848           need_bitwise = 0;
1849         }
1850       else
1851         {
1852           if (buffer_size < this_size)
1853             {
1854               buffer_size = this_size;
1855               buffer = xrealloc (buffer, buffer_size);
1856             }
1857           source_buffer = buffer;
1858           need_bitwise = 1;
1859         }
1860
1861       switch (p->location)
1862         {
1863         case DWARF_VALUE_REGISTER:
1864           {
1865             struct gdbarch *arch = get_frame_arch (frame);
1866             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1867             int reg_offset = dest_offset;
1868
1869             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1870                 && this_size <= register_size (arch, gdb_regnum))
1871               /* Big-endian, and we want less than full size.  */
1872               reg_offset = register_size (arch, gdb_regnum) - this_size;
1873
1874             if (gdb_regnum != -1)
1875               {
1876                 if (need_bitwise)
1877                   {
1878                     int optim, unavail;
1879
1880                     if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1881                                                    this_size, buffer,
1882                                                    &optim, &unavail))
1883                       {
1884                         if (optim)
1885                           error (_("Can't do read-modify-write to "
1886                                    "update bitfield; containing word has been "
1887                                    "optimized out"));
1888                         if (unavail)
1889                           throw_error (NOT_AVAILABLE_ERROR,
1890                                        _("Can't do read-modify-write to update "
1891                                          "bitfield; containing word "
1892                                          "is unavailable"));
1893                       }
1894                     copy_bitwise (buffer, dest_offset_bits,
1895                                   contents, source_offset_bits,
1896                                   this_size_bits,
1897                                   bits_big_endian);
1898                   }
1899
1900                 put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
1901                                           this_size, source_buffer);
1902               }
1903             else
1904               {
1905                 error (_("Unable to write to DWARF register number %s"),
1906                        paddress (arch, p->v.regno));
1907               }
1908           }
1909           break;
1910         case DWARF_VALUE_MEMORY:
1911           if (need_bitwise)
1912             {
1913               /* Only the first and last bytes can possibly have any
1914                  bits reused.  */
1915               read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1916               read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1917                            buffer + this_size - 1, 1);
1918               copy_bitwise (buffer, dest_offset_bits,
1919                             contents, source_offset_bits,
1920                             this_size_bits,
1921                             bits_big_endian);
1922             }
1923
1924           write_memory (p->v.mem.addr + dest_offset,
1925                         source_buffer, this_size);
1926           break;
1927         default:
1928           set_value_optimized_out (to, 1);
1929           break;
1930         }
1931       offset += this_size_bits;
1932     }
1933
1934   do_cleanups (cleanup);
1935 }
1936
1937 /* A helper function that checks bit validity in a pieced value.
1938    CHECK_FOR indicates the kind of validity checking.
1939    DWARF_VALUE_MEMORY means to check whether any bit is valid.
1940    DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1941    optimized out.
1942    DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1943    implicit pointer.  */
1944
1945 static int
1946 check_pieced_value_bits (const struct value *value, int bit_offset,
1947                          int bit_length,
1948                          enum dwarf_value_location check_for)
1949 {
1950   struct piece_closure *c
1951     = (struct piece_closure *) value_computed_closure (value);
1952   int i;
1953   int validity = (check_for == DWARF_VALUE_MEMORY
1954                   || check_for == DWARF_VALUE_IMPLICIT_POINTER);
1955
1956   bit_offset += 8 * value_offset (value);
1957   if (value_bitsize (value))
1958     bit_offset += value_bitpos (value);
1959
1960   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1961     {
1962       struct dwarf_expr_piece *p = &c->pieces[i];
1963       size_t this_size_bits = p->size;
1964
1965       if (bit_offset > 0)
1966         {
1967           if (bit_offset >= this_size_bits)
1968             {
1969               bit_offset -= this_size_bits;
1970               continue;
1971             }
1972
1973           bit_length -= this_size_bits - bit_offset;
1974           bit_offset = 0;
1975         }
1976       else
1977         bit_length -= this_size_bits;
1978
1979       if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
1980         {
1981           if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1982             return 0;
1983         }
1984       else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
1985                || p->location == DWARF_VALUE_IMPLICIT_POINTER)
1986         {
1987           if (validity)
1988             return 0;
1989         }
1990       else
1991         {
1992           if (!validity)
1993             return 1;
1994         }
1995     }
1996
1997   return validity;
1998 }
1999
2000 static int
2001 check_pieced_value_validity (const struct value *value, int bit_offset,
2002                              int bit_length)
2003 {
2004   return check_pieced_value_bits (value, bit_offset, bit_length,
2005                                   DWARF_VALUE_MEMORY);
2006 }
2007
2008 static int
2009 check_pieced_value_invalid (const struct value *value)
2010 {
2011   return check_pieced_value_bits (value, 0,
2012                                   8 * TYPE_LENGTH (value_type (value)),
2013                                   DWARF_VALUE_OPTIMIZED_OUT);
2014 }
2015
2016 /* An implementation of an lval_funcs method to see whether a value is
2017    a synthetic pointer.  */
2018
2019 static int
2020 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
2021                                 int bit_length)
2022 {
2023   return check_pieced_value_bits (value, bit_offset, bit_length,
2024                                   DWARF_VALUE_IMPLICIT_POINTER);
2025 }
2026
2027 /* A wrapper function for get_frame_address_in_block.  */
2028
2029 static CORE_ADDR
2030 get_frame_address_in_block_wrapper (void *baton)
2031 {
2032   return get_frame_address_in_block (baton);
2033 }
2034
2035 /* An implementation of an lval_funcs method to indirect through a
2036    pointer.  This handles the synthetic pointer case when needed.  */
2037
2038 static struct value *
2039 indirect_pieced_value (struct value *value)
2040 {
2041   struct piece_closure *c
2042     = (struct piece_closure *) value_computed_closure (value);
2043   struct type *type;
2044   struct frame_info *frame;
2045   struct dwarf2_locexpr_baton baton;
2046   int i, bit_offset, bit_length;
2047   struct dwarf_expr_piece *piece = NULL;
2048   LONGEST byte_offset;
2049
2050   type = check_typedef (value_type (value));
2051   if (TYPE_CODE (type) != TYPE_CODE_PTR)
2052     return NULL;
2053
2054   bit_length = 8 * TYPE_LENGTH (type);
2055   bit_offset = 8 * value_offset (value);
2056   if (value_bitsize (value))
2057     bit_offset += value_bitpos (value);
2058
2059   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2060     {
2061       struct dwarf_expr_piece *p = &c->pieces[i];
2062       size_t this_size_bits = p->size;
2063
2064       if (bit_offset > 0)
2065         {
2066           if (bit_offset >= this_size_bits)
2067             {
2068               bit_offset -= this_size_bits;
2069               continue;
2070             }
2071
2072           bit_length -= this_size_bits - bit_offset;
2073           bit_offset = 0;
2074         }
2075       else
2076         bit_length -= this_size_bits;
2077
2078       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2079         return NULL;
2080
2081       if (bit_length != 0)
2082         error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2083
2084       piece = p;
2085       break;
2086     }
2087
2088   frame = get_selected_frame (_("No frame selected."));
2089
2090   /* This is an offset requested by GDB, such as value subscripts.
2091      However, due to how synthetic pointers are implemented, this is
2092      always presented to us as a pointer type.  This means we have to
2093      sign-extend it manually as appropriate.  */
2094   byte_offset = value_as_address (value);
2095   if (TYPE_LENGTH (value_type (value)) < sizeof (LONGEST))
2096     byte_offset = gdb_sign_extend (byte_offset,
2097                                    8 * TYPE_LENGTH (value_type (value)));
2098   byte_offset += piece->v.ptr.offset;
2099
2100   gdb_assert (piece);
2101   baton
2102     = dwarf2_fetch_die_loc_sect_off (piece->v.ptr.die, c->per_cu,
2103                                      get_frame_address_in_block_wrapper,
2104                                      frame);
2105
2106   if (baton.data != NULL)
2107     return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
2108                                           baton.data, baton.size, baton.per_cu,
2109                                           byte_offset);
2110
2111   {
2112     struct obstack temp_obstack;
2113     struct cleanup *cleanup;
2114     const gdb_byte *bytes;
2115     LONGEST len;
2116     struct value *result;
2117
2118     obstack_init (&temp_obstack);
2119     cleanup = make_cleanup_obstack_free (&temp_obstack);
2120
2121     bytes = dwarf2_fetch_constant_bytes (piece->v.ptr.die, c->per_cu,
2122                                          &temp_obstack, &len);
2123     if (bytes == NULL)
2124       result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
2125     else
2126       {
2127         if (byte_offset < 0
2128             || byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > len)
2129           invalid_synthetic_pointer ();
2130         bytes += byte_offset;
2131         result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
2132       }
2133
2134     do_cleanups (cleanup);
2135     return result;
2136   }
2137 }
2138
2139 static void *
2140 copy_pieced_value_closure (const struct value *v)
2141 {
2142   struct piece_closure *c
2143     = (struct piece_closure *) value_computed_closure (v);
2144   
2145   ++c->refc;
2146   return c;
2147 }
2148
2149 static void
2150 free_pieced_value_closure (struct value *v)
2151 {
2152   struct piece_closure *c
2153     = (struct piece_closure *) value_computed_closure (v);
2154
2155   --c->refc;
2156   if (c->refc == 0)
2157     {
2158       int i;
2159
2160       for (i = 0; i < c->n_pieces; ++i)
2161         if (c->pieces[i].location == DWARF_VALUE_STACK)
2162           value_free (c->pieces[i].v.value);
2163
2164       xfree (c->pieces);
2165       xfree (c);
2166     }
2167 }
2168
2169 /* Functions for accessing a variable described by DW_OP_piece.  */
2170 static const struct lval_funcs pieced_value_funcs = {
2171   read_pieced_value,
2172   write_pieced_value,
2173   check_pieced_value_validity,
2174   check_pieced_value_invalid,
2175   indirect_pieced_value,
2176   NULL, /* coerce_ref */
2177   check_pieced_synthetic_pointer,
2178   copy_pieced_value_closure,
2179   free_pieced_value_closure
2180 };
2181
2182 /* Virtual method table for dwarf2_evaluate_loc_desc_full below.  */
2183
2184 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
2185 {
2186   dwarf_expr_read_reg,
2187   dwarf_expr_read_mem,
2188   dwarf_expr_frame_base,
2189   dwarf_expr_frame_cfa,
2190   dwarf_expr_frame_pc,
2191   dwarf_expr_tls_address,
2192   dwarf_expr_dwarf_call,
2193   dwarf_expr_get_base_type,
2194   dwarf_expr_push_dwarf_reg_entry_value,
2195   dwarf_expr_get_addr_index
2196 };
2197
2198 /* Evaluate a location description, starting at DATA and with length
2199    SIZE, to find the current location of variable of TYPE in the
2200    context of FRAME.  BYTE_OFFSET is applied after the contents are
2201    computed.  */
2202
2203 static struct value *
2204 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2205                                const gdb_byte *data, size_t size,
2206                                struct dwarf2_per_cu_data *per_cu,
2207                                LONGEST byte_offset)
2208 {
2209   struct value *retval;
2210   struct dwarf_expr_baton baton;
2211   struct dwarf_expr_context *ctx;
2212   struct cleanup *old_chain, *value_chain;
2213   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2214   volatile struct gdb_exception ex;
2215
2216   if (byte_offset < 0)
2217     invalid_synthetic_pointer ();
2218
2219   if (size == 0)
2220     return allocate_optimized_out_value (type);
2221
2222   baton.frame = frame;
2223   baton.per_cu = per_cu;
2224
2225   ctx = new_dwarf_expr_context ();
2226   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2227   value_chain = make_cleanup_value_free_to_mark (value_mark ());
2228
2229   ctx->gdbarch = get_objfile_arch (objfile);
2230   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2231   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2232   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2233   ctx->baton = &baton;
2234   ctx->funcs = &dwarf_expr_ctx_funcs;
2235
2236   TRY_CATCH (ex, RETURN_MASK_ERROR)
2237     {
2238       dwarf_expr_eval (ctx, data, size);
2239     }
2240   if (ex.reason < 0)
2241     {
2242       if (ex.error == NOT_AVAILABLE_ERROR)
2243         {
2244           do_cleanups (old_chain);
2245           retval = allocate_value (type);
2246           mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2247           return retval;
2248         }
2249       else if (ex.error == NO_ENTRY_VALUE_ERROR)
2250         {
2251           if (entry_values_debug)
2252             exception_print (gdb_stdout, ex);
2253           do_cleanups (old_chain);
2254           return allocate_optimized_out_value (type);
2255         }
2256       else
2257         throw_exception (ex);
2258     }
2259
2260   if (ctx->num_pieces > 0)
2261     {
2262       struct piece_closure *c;
2263       struct frame_id frame_id = get_frame_id (frame);
2264       ULONGEST bit_size = 0;
2265       int i;
2266
2267       for (i = 0; i < ctx->num_pieces; ++i)
2268         bit_size += ctx->pieces[i].size;
2269       if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2270         invalid_synthetic_pointer ();
2271
2272       c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
2273                                   ctx->addr_size);
2274       /* We must clean up the value chain after creating the piece
2275          closure but before allocating the result.  */
2276       do_cleanups (value_chain);
2277       retval = allocate_computed_value (type, &pieced_value_funcs, c);
2278       VALUE_FRAME_ID (retval) = frame_id;
2279       set_value_offset (retval, byte_offset);
2280     }
2281   else
2282     {
2283       switch (ctx->location)
2284         {
2285         case DWARF_VALUE_REGISTER:
2286           {
2287             struct gdbarch *arch = get_frame_arch (frame);
2288             ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
2289             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
2290
2291             if (byte_offset != 0)
2292               error (_("cannot use offset on synthetic pointer to register"));
2293             do_cleanups (value_chain);
2294             if (gdb_regnum != -1)
2295               retval = value_from_register (type, gdb_regnum, frame);
2296             else
2297               error (_("Unable to access DWARF register number %s"),
2298                      paddress (arch, dwarf_regnum));
2299           }
2300           break;
2301
2302         case DWARF_VALUE_MEMORY:
2303           {
2304             CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
2305             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
2306
2307             do_cleanups (value_chain);
2308             retval = allocate_value_lazy (type);
2309             VALUE_LVAL (retval) = lval_memory;
2310             if (in_stack_memory)
2311               set_value_stack (retval, 1);
2312             set_value_address (retval, address + byte_offset);
2313           }
2314           break;
2315
2316         case DWARF_VALUE_STACK:
2317           {
2318             struct value *value = dwarf_expr_fetch (ctx, 0);
2319             gdb_byte *contents;
2320             const gdb_byte *val_bytes;
2321             size_t n = TYPE_LENGTH (value_type (value));
2322
2323             if (byte_offset + TYPE_LENGTH (type) > n)
2324               invalid_synthetic_pointer ();
2325
2326             val_bytes = value_contents_all (value);
2327             val_bytes += byte_offset;
2328             n -= byte_offset;
2329
2330             /* Preserve VALUE because we are going to free values back
2331                to the mark, but we still need the value contents
2332                below.  */
2333             value_incref (value);
2334             do_cleanups (value_chain);
2335             make_cleanup_value_free (value);
2336
2337             retval = allocate_value (type);
2338             contents = value_contents_raw (retval);
2339             if (n > TYPE_LENGTH (type))
2340               {
2341                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2342
2343                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2344                   val_bytes += n - TYPE_LENGTH (type);
2345                 n = TYPE_LENGTH (type);
2346               }
2347             memcpy (contents, val_bytes, n);
2348           }
2349           break;
2350
2351         case DWARF_VALUE_LITERAL:
2352           {
2353             bfd_byte *contents;
2354             const bfd_byte *ldata;
2355             size_t n = ctx->len;
2356
2357             if (byte_offset + TYPE_LENGTH (type) > n)
2358               invalid_synthetic_pointer ();
2359
2360             do_cleanups (value_chain);
2361             retval = allocate_value (type);
2362             contents = value_contents_raw (retval);
2363
2364             ldata = ctx->data + byte_offset;
2365             n -= byte_offset;
2366
2367             if (n > TYPE_LENGTH (type))
2368               {
2369                 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2370
2371                 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2372                   ldata += n - TYPE_LENGTH (type);
2373                 n = TYPE_LENGTH (type);
2374               }
2375             memcpy (contents, ldata, n);
2376           }
2377           break;
2378
2379         case DWARF_VALUE_OPTIMIZED_OUT:
2380           do_cleanups (value_chain);
2381           retval = allocate_optimized_out_value (type);
2382           break;
2383
2384           /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2385              operation by execute_stack_op.  */
2386         case DWARF_VALUE_IMPLICIT_POINTER:
2387           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2388              it can only be encountered when making a piece.  */
2389         default:
2390           internal_error (__FILE__, __LINE__, _("invalid location type"));
2391         }
2392     }
2393
2394   set_value_initialized (retval, ctx->initialized);
2395
2396   do_cleanups (old_chain);
2397
2398   return retval;
2399 }
2400
2401 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2402    passes 0 as the byte_offset.  */
2403
2404 struct value *
2405 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2406                           const gdb_byte *data, size_t size,
2407                           struct dwarf2_per_cu_data *per_cu)
2408 {
2409   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2410 }
2411
2412 \f
2413 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
2414
2415 struct needs_frame_baton
2416 {
2417   int needs_frame;
2418   struct dwarf2_per_cu_data *per_cu;
2419 };
2420
2421 /* Reads from registers do require a frame.  */
2422 static CORE_ADDR
2423 needs_frame_read_reg (void *baton, int regnum)
2424 {
2425   struct needs_frame_baton *nf_baton = baton;
2426
2427   nf_baton->needs_frame = 1;
2428   return 1;
2429 }
2430
2431 /* Reads from memory do not require a frame.  */
2432 static void
2433 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2434 {
2435   memset (buf, 0, len);
2436 }
2437
2438 /* Frame-relative accesses do require a frame.  */
2439 static void
2440 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
2441 {
2442   static gdb_byte lit0 = DW_OP_lit0;
2443   struct needs_frame_baton *nf_baton = baton;
2444
2445   *start = &lit0;
2446   *length = 1;
2447
2448   nf_baton->needs_frame = 1;
2449 }
2450
2451 /* CFA accesses require a frame.  */
2452
2453 static CORE_ADDR
2454 needs_frame_frame_cfa (void *baton)
2455 {
2456   struct needs_frame_baton *nf_baton = baton;
2457
2458   nf_baton->needs_frame = 1;
2459   return 1;
2460 }
2461
2462 /* Thread-local accesses do require a frame.  */
2463 static CORE_ADDR
2464 needs_frame_tls_address (void *baton, CORE_ADDR offset)
2465 {
2466   struct needs_frame_baton *nf_baton = baton;
2467
2468   nf_baton->needs_frame = 1;
2469   return 1;
2470 }
2471
2472 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame.  */
2473
2474 static void
2475 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2476 {
2477   struct needs_frame_baton *nf_baton = ctx->baton;
2478
2479   per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2480                      ctx->funcs->get_frame_pc, ctx->baton);
2481 }
2482
2483 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame.  */
2484
2485 static void
2486 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2487                              enum call_site_parameter_kind kind,
2488                              union call_site_parameter_u kind_u, int deref_size)
2489 {
2490   struct needs_frame_baton *nf_baton = ctx->baton;
2491
2492   nf_baton->needs_frame = 1;
2493
2494   /* The expression may require some stub values on DWARF stack.  */
2495   dwarf_expr_push_address (ctx, 0, 0);
2496 }
2497
2498 /* DW_OP_GNU_addr_index doesn't require a frame.  */
2499
2500 static CORE_ADDR
2501 needs_get_addr_index (void *baton, unsigned int index)
2502 {
2503   /* Nothing to do.  */
2504   return 1;
2505 }
2506
2507 /* Virtual method table for dwarf2_loc_desc_needs_frame below.  */
2508
2509 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2510 {
2511   needs_frame_read_reg,
2512   needs_frame_read_mem,
2513   needs_frame_frame_base,
2514   needs_frame_frame_cfa,
2515   needs_frame_frame_cfa,        /* get_frame_pc */
2516   needs_frame_tls_address,
2517   needs_frame_dwarf_call,
2518   NULL,                         /* get_base_type */
2519   needs_dwarf_reg_entry_value,
2520   needs_get_addr_index
2521 };
2522
2523 /* Return non-zero iff the location expression at DATA (length SIZE)
2524    requires a frame to evaluate.  */
2525
2526 static int
2527 dwarf2_loc_desc_needs_frame (const gdb_byte *data, size_t size,
2528                              struct dwarf2_per_cu_data *per_cu)
2529 {
2530   struct needs_frame_baton baton;
2531   struct dwarf_expr_context *ctx;
2532   int in_reg;
2533   struct cleanup *old_chain;
2534   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2535
2536   baton.needs_frame = 0;
2537   baton.per_cu = per_cu;
2538
2539   ctx = new_dwarf_expr_context ();
2540   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2541   make_cleanup_value_free_to_mark (value_mark ());
2542
2543   ctx->gdbarch = get_objfile_arch (objfile);
2544   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2545   ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2546   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2547   ctx->baton = &baton;
2548   ctx->funcs = &needs_frame_ctx_funcs;
2549
2550   dwarf_expr_eval (ctx, data, size);
2551
2552   in_reg = ctx->location == DWARF_VALUE_REGISTER;
2553
2554   if (ctx->num_pieces > 0)
2555     {
2556       int i;
2557
2558       /* If the location has several pieces, and any of them are in
2559          registers, then we will need a frame to fetch them from.  */
2560       for (i = 0; i < ctx->num_pieces; i++)
2561         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2562           in_reg = 1;
2563     }
2564
2565   do_cleanups (old_chain);
2566
2567   return baton.needs_frame || in_reg;
2568 }
2569
2570 /* A helper function that throws an unimplemented error mentioning a
2571    given DWARF operator.  */
2572
2573 static void
2574 unimplemented (unsigned int op)
2575 {
2576   const char *name = get_DW_OP_name (op);
2577
2578   if (name)
2579     error (_("DWARF operator %s cannot be translated to an agent expression"),
2580            name);
2581   else
2582     error (_("Unknown DWARF operator 0x%02x cannot be translated "
2583              "to an agent expression"),
2584            op);
2585 }
2586
2587 /* A helper function to convert a DWARF register to an arch register.
2588    ARCH is the architecture.
2589    DWARF_REG is the register.
2590    This will throw an exception if the DWARF register cannot be
2591    translated to an architecture register.  */
2592
2593 static int
2594 translate_register (struct gdbarch *arch, int dwarf_reg)
2595 {
2596   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2597   if (reg == -1)
2598     error (_("Unable to access DWARF register number %d"), dwarf_reg);
2599   return reg;
2600 }
2601
2602 /* A helper function that emits an access to memory.  ARCH is the
2603    target architecture.  EXPR is the expression which we are building.
2604    NBITS is the number of bits we want to read.  This emits the
2605    opcodes needed to read the memory and then extract the desired
2606    bits.  */
2607
2608 static void
2609 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2610 {
2611   ULONGEST nbytes = (nbits + 7) / 8;
2612
2613   gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2614
2615   if (expr->tracing)
2616     ax_trace_quick (expr, nbytes);
2617
2618   if (nbits <= 8)
2619     ax_simple (expr, aop_ref8);
2620   else if (nbits <= 16)
2621     ax_simple (expr, aop_ref16);
2622   else if (nbits <= 32)
2623     ax_simple (expr, aop_ref32);
2624   else
2625     ax_simple (expr, aop_ref64);
2626
2627   /* If we read exactly the number of bytes we wanted, we're done.  */
2628   if (8 * nbytes == nbits)
2629     return;
2630
2631   if (gdbarch_bits_big_endian (arch))
2632     {
2633       /* On a bits-big-endian machine, we want the high-order
2634          NBITS.  */
2635       ax_const_l (expr, 8 * nbytes - nbits);
2636       ax_simple (expr, aop_rsh_unsigned);
2637     }
2638   else
2639     {
2640       /* On a bits-little-endian box, we want the low-order NBITS.  */
2641       ax_zero_ext (expr, nbits);
2642     }
2643 }
2644
2645 /* A helper function to return the frame's PC.  */
2646
2647 static CORE_ADDR
2648 get_ax_pc (void *baton)
2649 {
2650   struct agent_expr *expr = baton;
2651
2652   return expr->scope;
2653 }
2654
2655 /* Compile a DWARF location expression to an agent expression.
2656    
2657    EXPR is the agent expression we are building.
2658    LOC is the agent value we modify.
2659    ARCH is the architecture.
2660    ADDR_SIZE is the size of addresses, in bytes.
2661    OP_PTR is the start of the location expression.
2662    OP_END is one past the last byte of the location expression.
2663    
2664    This will throw an exception for various kinds of errors -- for
2665    example, if the expression cannot be compiled, or if the expression
2666    is invalid.  */
2667
2668 void
2669 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2670                            struct gdbarch *arch, unsigned int addr_size,
2671                            const gdb_byte *op_ptr, const gdb_byte *op_end,
2672                            struct dwarf2_per_cu_data *per_cu)
2673 {
2674   struct cleanup *cleanups;
2675   int i, *offsets;
2676   VEC(int) *dw_labels = NULL, *patches = NULL;
2677   const gdb_byte * const base = op_ptr;
2678   const gdb_byte *previous_piece = op_ptr;
2679   enum bfd_endian byte_order = gdbarch_byte_order (arch);
2680   ULONGEST bits_collected = 0;
2681   unsigned int addr_size_bits = 8 * addr_size;
2682   int bits_big_endian = gdbarch_bits_big_endian (arch);
2683
2684   offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
2685   cleanups = make_cleanup (xfree, offsets);
2686
2687   for (i = 0; i < op_end - op_ptr; ++i)
2688     offsets[i] = -1;
2689
2690   make_cleanup (VEC_cleanup (int), &dw_labels);
2691   make_cleanup (VEC_cleanup (int), &patches);
2692
2693   /* By default we are making an address.  */
2694   loc->kind = axs_lvalue_memory;
2695
2696   while (op_ptr < op_end)
2697     {
2698       enum dwarf_location_atom op = *op_ptr;
2699       uint64_t uoffset, reg;
2700       int64_t offset;
2701       int i;
2702
2703       offsets[op_ptr - base] = expr->len;
2704       ++op_ptr;
2705
2706       /* Our basic approach to code generation is to map DWARF
2707          operations directly to AX operations.  However, there are
2708          some differences.
2709
2710          First, DWARF works on address-sized units, but AX always uses
2711          LONGEST.  For most operations we simply ignore this
2712          difference; instead we generate sign extensions as needed
2713          before division and comparison operations.  It would be nice
2714          to omit the sign extensions, but there is no way to determine
2715          the size of the target's LONGEST.  (This code uses the size
2716          of the host LONGEST in some cases -- that is a bug but it is
2717          difficult to fix.)
2718
2719          Second, some DWARF operations cannot be translated to AX.
2720          For these we simply fail.  See
2721          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
2722       switch (op)
2723         {
2724         case DW_OP_lit0:
2725         case DW_OP_lit1:
2726         case DW_OP_lit2:
2727         case DW_OP_lit3:
2728         case DW_OP_lit4:
2729         case DW_OP_lit5:
2730         case DW_OP_lit6:
2731         case DW_OP_lit7:
2732         case DW_OP_lit8:
2733         case DW_OP_lit9:
2734         case DW_OP_lit10:
2735         case DW_OP_lit11:
2736         case DW_OP_lit12:
2737         case DW_OP_lit13:
2738         case DW_OP_lit14:
2739         case DW_OP_lit15:
2740         case DW_OP_lit16:
2741         case DW_OP_lit17:
2742         case DW_OP_lit18:
2743         case DW_OP_lit19:
2744         case DW_OP_lit20:
2745         case DW_OP_lit21:
2746         case DW_OP_lit22:
2747         case DW_OP_lit23:
2748         case DW_OP_lit24:
2749         case DW_OP_lit25:
2750         case DW_OP_lit26:
2751         case DW_OP_lit27:
2752         case DW_OP_lit28:
2753         case DW_OP_lit29:
2754         case DW_OP_lit30:
2755         case DW_OP_lit31:
2756           ax_const_l (expr, op - DW_OP_lit0);
2757           break;
2758
2759         case DW_OP_addr:
2760           uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
2761           op_ptr += addr_size;
2762           /* Some versions of GCC emit DW_OP_addr before
2763              DW_OP_GNU_push_tls_address.  In this case the value is an
2764              index, not an address.  We don't support things like
2765              branching between the address and the TLS op.  */
2766           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
2767             uoffset += dwarf2_per_cu_text_offset (per_cu);
2768           ax_const_l (expr, uoffset);
2769           break;
2770
2771         case DW_OP_const1u:
2772           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2773           op_ptr += 1;
2774           break;
2775         case DW_OP_const1s:
2776           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2777           op_ptr += 1;
2778           break;
2779         case DW_OP_const2u:
2780           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2781           op_ptr += 2;
2782           break;
2783         case DW_OP_const2s:
2784           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2785           op_ptr += 2;
2786           break;
2787         case DW_OP_const4u:
2788           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2789           op_ptr += 4;
2790           break;
2791         case DW_OP_const4s:
2792           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2793           op_ptr += 4;
2794           break;
2795         case DW_OP_const8u:
2796           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2797           op_ptr += 8;
2798           break;
2799         case DW_OP_const8s:
2800           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2801           op_ptr += 8;
2802           break;
2803         case DW_OP_constu:
2804           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2805           ax_const_l (expr, uoffset);
2806           break;
2807         case DW_OP_consts:
2808           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2809           ax_const_l (expr, offset);
2810           break;
2811
2812         case DW_OP_reg0:
2813         case DW_OP_reg1:
2814         case DW_OP_reg2:
2815         case DW_OP_reg3:
2816         case DW_OP_reg4:
2817         case DW_OP_reg5:
2818         case DW_OP_reg6:
2819         case DW_OP_reg7:
2820         case DW_OP_reg8:
2821         case DW_OP_reg9:
2822         case DW_OP_reg10:
2823         case DW_OP_reg11:
2824         case DW_OP_reg12:
2825         case DW_OP_reg13:
2826         case DW_OP_reg14:
2827         case DW_OP_reg15:
2828         case DW_OP_reg16:
2829         case DW_OP_reg17:
2830         case DW_OP_reg18:
2831         case DW_OP_reg19:
2832         case DW_OP_reg20:
2833         case DW_OP_reg21:
2834         case DW_OP_reg22:
2835         case DW_OP_reg23:
2836         case DW_OP_reg24:
2837         case DW_OP_reg25:
2838         case DW_OP_reg26:
2839         case DW_OP_reg27:
2840         case DW_OP_reg28:
2841         case DW_OP_reg29:
2842         case DW_OP_reg30:
2843         case DW_OP_reg31:
2844           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2845           loc->u.reg = translate_register (arch, op - DW_OP_reg0);
2846           loc->kind = axs_lvalue_register;
2847           break;
2848
2849         case DW_OP_regx:
2850           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
2851           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2852           loc->u.reg = translate_register (arch, reg);
2853           loc->kind = axs_lvalue_register;
2854           break;
2855
2856         case DW_OP_implicit_value:
2857           {
2858             uint64_t len;
2859
2860             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
2861             if (op_ptr + len > op_end)
2862               error (_("DW_OP_implicit_value: too few bytes available."));
2863             if (len > sizeof (ULONGEST))
2864               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2865                      (int) len);
2866
2867             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
2868                                                         byte_order));
2869             op_ptr += len;
2870             dwarf_expr_require_composition (op_ptr, op_end,
2871                                             "DW_OP_implicit_value");
2872
2873             loc->kind = axs_rvalue;
2874           }
2875           break;
2876
2877         case DW_OP_stack_value:
2878           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
2879           loc->kind = axs_rvalue;
2880           break;
2881
2882         case DW_OP_breg0:
2883         case DW_OP_breg1:
2884         case DW_OP_breg2:
2885         case DW_OP_breg3:
2886         case DW_OP_breg4:
2887         case DW_OP_breg5:
2888         case DW_OP_breg6:
2889         case DW_OP_breg7:
2890         case DW_OP_breg8:
2891         case DW_OP_breg9:
2892         case DW_OP_breg10:
2893         case DW_OP_breg11:
2894         case DW_OP_breg12:
2895         case DW_OP_breg13:
2896         case DW_OP_breg14:
2897         case DW_OP_breg15:
2898         case DW_OP_breg16:
2899         case DW_OP_breg17:
2900         case DW_OP_breg18:
2901         case DW_OP_breg19:
2902         case DW_OP_breg20:
2903         case DW_OP_breg21:
2904         case DW_OP_breg22:
2905         case DW_OP_breg23:
2906         case DW_OP_breg24:
2907         case DW_OP_breg25:
2908         case DW_OP_breg26:
2909         case DW_OP_breg27:
2910         case DW_OP_breg28:
2911         case DW_OP_breg29:
2912         case DW_OP_breg30:
2913         case DW_OP_breg31:
2914           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2915           i = translate_register (arch, op - DW_OP_breg0);
2916           ax_reg (expr, i);
2917           if (offset != 0)
2918             {
2919               ax_const_l (expr, offset);
2920               ax_simple (expr, aop_add);
2921             }
2922           break;
2923         case DW_OP_bregx:
2924           {
2925             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
2926             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2927             i = translate_register (arch, reg);
2928             ax_reg (expr, i);
2929             if (offset != 0)
2930               {
2931                 ax_const_l (expr, offset);
2932                 ax_simple (expr, aop_add);
2933               }
2934           }
2935           break;
2936         case DW_OP_fbreg:
2937           {
2938             const gdb_byte *datastart;
2939             size_t datalen;
2940             struct block *b;
2941             struct symbol *framefunc;
2942
2943             b = block_for_pc (expr->scope);
2944
2945             if (!b)
2946               error (_("No block found for address"));
2947
2948             framefunc = block_linkage_function (b);
2949
2950             if (!framefunc)
2951               error (_("No function found for block"));
2952
2953             dwarf_expr_frame_base_1 (framefunc, expr->scope,
2954                                      &datastart, &datalen);
2955
2956             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2957             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2958                                        datastart + datalen, per_cu);
2959             if (loc->kind == axs_lvalue_register)
2960               require_rvalue (expr, loc);
2961
2962             if (offset != 0)
2963               {
2964                 ax_const_l (expr, offset);
2965                 ax_simple (expr, aop_add);
2966               }
2967
2968             loc->kind = axs_lvalue_memory;
2969           }
2970           break;
2971
2972         case DW_OP_dup:
2973           ax_simple (expr, aop_dup);
2974           break;
2975
2976         case DW_OP_drop:
2977           ax_simple (expr, aop_pop);
2978           break;
2979
2980         case DW_OP_pick:
2981           offset = *op_ptr++;
2982           ax_pick (expr, offset);
2983           break;
2984           
2985         case DW_OP_swap:
2986           ax_simple (expr, aop_swap);
2987           break;
2988
2989         case DW_OP_over:
2990           ax_pick (expr, 1);
2991           break;
2992
2993         case DW_OP_rot:
2994           ax_simple (expr, aop_rot);
2995           break;
2996
2997         case DW_OP_deref:
2998         case DW_OP_deref_size:
2999           {
3000             int size;
3001
3002             if (op == DW_OP_deref_size)
3003               size = *op_ptr++;
3004             else
3005               size = addr_size;
3006
3007             if (size != 1 && size != 2 && size != 4 && size != 8)
3008               error (_("Unsupported size %d in %s"),
3009                      size, get_DW_OP_name (op));
3010             access_memory (arch, expr, size * TARGET_CHAR_BIT);
3011           }
3012           break;
3013
3014         case DW_OP_abs:
3015           /* Sign extend the operand.  */
3016           ax_ext (expr, addr_size_bits);
3017           ax_simple (expr, aop_dup);
3018           ax_const_l (expr, 0);
3019           ax_simple (expr, aop_less_signed);
3020           ax_simple (expr, aop_log_not);
3021           i = ax_goto (expr, aop_if_goto);
3022           /* We have to emit 0 - X.  */
3023           ax_const_l (expr, 0);
3024           ax_simple (expr, aop_swap);
3025           ax_simple (expr, aop_sub);
3026           ax_label (expr, i, expr->len);
3027           break;
3028
3029         case DW_OP_neg:
3030           /* No need to sign extend here.  */
3031           ax_const_l (expr, 0);
3032           ax_simple (expr, aop_swap);
3033           ax_simple (expr, aop_sub);
3034           break;
3035
3036         case DW_OP_not:
3037           /* Sign extend the operand.  */
3038           ax_ext (expr, addr_size_bits);
3039           ax_simple (expr, aop_bit_not);
3040           break;
3041
3042         case DW_OP_plus_uconst:
3043           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3044           /* It would be really weird to emit `DW_OP_plus_uconst 0',
3045              but we micro-optimize anyhow.  */
3046           if (reg != 0)
3047             {
3048               ax_const_l (expr, reg);
3049               ax_simple (expr, aop_add);
3050             }
3051           break;
3052
3053         case DW_OP_and:
3054           ax_simple (expr, aop_bit_and);
3055           break;
3056
3057         case DW_OP_div:
3058           /* Sign extend the operands.  */
3059           ax_ext (expr, addr_size_bits);
3060           ax_simple (expr, aop_swap);
3061           ax_ext (expr, addr_size_bits);
3062           ax_simple (expr, aop_swap);
3063           ax_simple (expr, aop_div_signed);
3064           break;
3065
3066         case DW_OP_minus:
3067           ax_simple (expr, aop_sub);
3068           break;
3069
3070         case DW_OP_mod:
3071           ax_simple (expr, aop_rem_unsigned);
3072           break;
3073
3074         case DW_OP_mul:
3075           ax_simple (expr, aop_mul);
3076           break;
3077
3078         case DW_OP_or:
3079           ax_simple (expr, aop_bit_or);
3080           break;
3081
3082         case DW_OP_plus:
3083           ax_simple (expr, aop_add);
3084           break;
3085
3086         case DW_OP_shl:
3087           ax_simple (expr, aop_lsh);
3088           break;
3089
3090         case DW_OP_shr:
3091           ax_simple (expr, aop_rsh_unsigned);
3092           break;
3093
3094         case DW_OP_shra:
3095           ax_simple (expr, aop_rsh_signed);
3096           break;
3097
3098         case DW_OP_xor:
3099           ax_simple (expr, aop_bit_xor);
3100           break;
3101
3102         case DW_OP_le:
3103           /* Sign extend the operands.  */
3104           ax_ext (expr, addr_size_bits);
3105           ax_simple (expr, aop_swap);
3106           ax_ext (expr, addr_size_bits);
3107           /* Note no swap here: A <= B is !(B < A).  */
3108           ax_simple (expr, aop_less_signed);
3109           ax_simple (expr, aop_log_not);
3110           break;
3111
3112         case DW_OP_ge:
3113           /* Sign extend the operands.  */
3114           ax_ext (expr, addr_size_bits);
3115           ax_simple (expr, aop_swap);
3116           ax_ext (expr, addr_size_bits);
3117           ax_simple (expr, aop_swap);
3118           /* A >= B is !(A < B).  */
3119           ax_simple (expr, aop_less_signed);
3120           ax_simple (expr, aop_log_not);
3121           break;
3122
3123         case DW_OP_eq:
3124           /* Sign extend the operands.  */
3125           ax_ext (expr, addr_size_bits);
3126           ax_simple (expr, aop_swap);
3127           ax_ext (expr, addr_size_bits);
3128           /* No need for a second swap here.  */
3129           ax_simple (expr, aop_equal);
3130           break;
3131
3132         case DW_OP_lt:
3133           /* Sign extend the operands.  */
3134           ax_ext (expr, addr_size_bits);
3135           ax_simple (expr, aop_swap);
3136           ax_ext (expr, addr_size_bits);
3137           ax_simple (expr, aop_swap);
3138           ax_simple (expr, aop_less_signed);
3139           break;
3140
3141         case DW_OP_gt:
3142           /* Sign extend the operands.  */
3143           ax_ext (expr, addr_size_bits);
3144           ax_simple (expr, aop_swap);
3145           ax_ext (expr, addr_size_bits);
3146           /* Note no swap here: A > B is B < A.  */
3147           ax_simple (expr, aop_less_signed);
3148           break;
3149
3150         case DW_OP_ne:
3151           /* Sign extend the operands.  */
3152           ax_ext (expr, addr_size_bits);
3153           ax_simple (expr, aop_swap);
3154           ax_ext (expr, addr_size_bits);
3155           /* No need for a swap here.  */
3156           ax_simple (expr, aop_equal);
3157           ax_simple (expr, aop_log_not);
3158           break;
3159
3160         case DW_OP_call_frame_cfa:
3161           dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
3162           loc->kind = axs_lvalue_memory;
3163           break;
3164
3165         case DW_OP_GNU_push_tls_address:
3166           unimplemented (op);
3167           break;
3168
3169         case DW_OP_skip:
3170           offset = extract_signed_integer (op_ptr, 2, byte_order);
3171           op_ptr += 2;
3172           i = ax_goto (expr, aop_goto);
3173           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3174           VEC_safe_push (int, patches, i);
3175           break;
3176
3177         case DW_OP_bra:
3178           offset = extract_signed_integer (op_ptr, 2, byte_order);
3179           op_ptr += 2;
3180           /* Zero extend the operand.  */
3181           ax_zero_ext (expr, addr_size_bits);
3182           i = ax_goto (expr, aop_if_goto);
3183           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3184           VEC_safe_push (int, patches, i);
3185           break;
3186
3187         case DW_OP_nop:
3188           break;
3189
3190         case DW_OP_piece:
3191         case DW_OP_bit_piece:
3192           {
3193             uint64_t size, offset;
3194
3195             if (op_ptr - 1 == previous_piece)
3196               error (_("Cannot translate empty pieces to agent expressions"));
3197             previous_piece = op_ptr - 1;
3198
3199             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3200             if (op == DW_OP_piece)
3201               {
3202                 size *= 8;
3203                 offset = 0;
3204               }
3205             else
3206               op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3207
3208             if (bits_collected + size > 8 * sizeof (LONGEST))
3209               error (_("Expression pieces exceed word size"));
3210
3211             /* Access the bits.  */
3212             switch (loc->kind)
3213               {
3214               case axs_lvalue_register:
3215                 ax_reg (expr, loc->u.reg);
3216                 break;
3217
3218               case axs_lvalue_memory:
3219                 /* Offset the pointer, if needed.  */
3220                 if (offset > 8)
3221                   {
3222                     ax_const_l (expr, offset / 8);
3223                     ax_simple (expr, aop_add);
3224                     offset %= 8;
3225                   }
3226                 access_memory (arch, expr, size);
3227                 break;
3228               }
3229
3230             /* For a bits-big-endian target, shift up what we already
3231                have.  For a bits-little-endian target, shift up the
3232                new data.  Note that there is a potential bug here if
3233                the DWARF expression leaves multiple values on the
3234                stack.  */
3235             if (bits_collected > 0)
3236               {
3237                 if (bits_big_endian)
3238                   {
3239                     ax_simple (expr, aop_swap);
3240                     ax_const_l (expr, size);
3241                     ax_simple (expr, aop_lsh);
3242                     /* We don't need a second swap here, because
3243                        aop_bit_or is symmetric.  */
3244                   }
3245                 else
3246                   {
3247                     ax_const_l (expr, size);
3248                     ax_simple (expr, aop_lsh);
3249                   }
3250                 ax_simple (expr, aop_bit_or);
3251               }
3252
3253             bits_collected += size;
3254             loc->kind = axs_rvalue;
3255           }
3256           break;
3257
3258         case DW_OP_GNU_uninit:
3259           unimplemented (op);
3260
3261         case DW_OP_call2:
3262         case DW_OP_call4:
3263           {
3264             struct dwarf2_locexpr_baton block;
3265             int size = (op == DW_OP_call2 ? 2 : 4);
3266             cu_offset offset;
3267
3268             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3269             op_ptr += size;
3270
3271             offset.cu_off = uoffset;
3272             block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3273                                                  get_ax_pc, expr);
3274
3275             /* DW_OP_call_ref is currently not supported.  */
3276             gdb_assert (block.per_cu == per_cu);
3277
3278             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3279                                        block.data, block.data + block.size,
3280                                        per_cu);
3281           }
3282           break;
3283
3284         case DW_OP_call_ref:
3285           unimplemented (op);
3286
3287         default:
3288           unimplemented (op);
3289         }
3290     }
3291
3292   /* Patch all the branches we emitted.  */
3293   for (i = 0; i < VEC_length (int, patches); ++i)
3294     {
3295       int targ = offsets[VEC_index (int, dw_labels, i)];
3296       if (targ == -1)
3297         internal_error (__FILE__, __LINE__, _("invalid label"));
3298       ax_label (expr, VEC_index (int, patches, i), targ);
3299     }
3300
3301   do_cleanups (cleanups);
3302 }
3303
3304 \f
3305 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3306    evaluator to calculate the location.  */
3307 static struct value *
3308 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3309 {
3310   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3311   struct value *val;
3312
3313   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3314                                   dlbaton->size, dlbaton->per_cu);
3315
3316   return val;
3317 }
3318
3319 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3320    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3321    will be thrown.  */
3322
3323 static struct value *
3324 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3325 {
3326   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3327
3328   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3329                                      dlbaton->size);
3330 }
3331
3332 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
3333 static int
3334 locexpr_read_needs_frame (struct symbol *symbol)
3335 {
3336   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3337
3338   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3339                                       dlbaton->per_cu);
3340 }
3341
3342 /* Return true if DATA points to the end of a piece.  END is one past
3343    the last byte in the expression.  */
3344
3345 static int
3346 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3347 {
3348   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3349 }
3350
3351 /* Helper for locexpr_describe_location_piece that finds the name of a
3352    DWARF register.  */
3353
3354 static const char *
3355 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3356 {
3357   int regnum;
3358
3359   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
3360   return gdbarch_register_name (gdbarch, regnum);
3361 }
3362
3363 /* Nicely describe a single piece of a location, returning an updated
3364    position in the bytecode sequence.  This function cannot recognize
3365    all locations; if a location is not recognized, it simply returns
3366    DATA.  If there is an error during reading, e.g. we run off the end
3367    of the buffer, an error is thrown.  */
3368
3369 static const gdb_byte *
3370 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3371                                  CORE_ADDR addr, struct objfile *objfile,
3372                                  struct dwarf2_per_cu_data *per_cu,
3373                                  const gdb_byte *data, const gdb_byte *end,
3374                                  unsigned int addr_size)
3375 {
3376   struct gdbarch *gdbarch = get_objfile_arch (objfile);
3377   size_t leb128_size;
3378
3379   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3380     {
3381       fprintf_filtered (stream, _("a variable in $%s"),
3382                         locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3383       data += 1;
3384     }
3385   else if (data[0] == DW_OP_regx)
3386     {
3387       uint64_t reg;
3388
3389       data = safe_read_uleb128 (data + 1, end, &reg);
3390       fprintf_filtered (stream, _("a variable in $%s"),
3391                         locexpr_regname (gdbarch, reg));
3392     }
3393   else if (data[0] == DW_OP_fbreg)
3394     {
3395       struct block *b;
3396       struct symbol *framefunc;
3397       int frame_reg = 0;
3398       int64_t frame_offset;
3399       const gdb_byte *base_data, *new_data, *save_data = data;
3400       size_t base_size;
3401       int64_t base_offset = 0;
3402
3403       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3404       if (!piece_end_p (new_data, end))
3405         return data;
3406       data = new_data;
3407
3408       b = block_for_pc (addr);
3409
3410       if (!b)
3411         error (_("No block found for address for symbol \"%s\"."),
3412                SYMBOL_PRINT_NAME (symbol));
3413
3414       framefunc = block_linkage_function (b);
3415
3416       if (!framefunc)
3417         error (_("No function found for block for symbol \"%s\"."),
3418                SYMBOL_PRINT_NAME (symbol));
3419
3420       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3421
3422       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3423         {
3424           const gdb_byte *buf_end;
3425           
3426           frame_reg = base_data[0] - DW_OP_breg0;
3427           buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3428                                        &base_offset);
3429           if (buf_end != base_data + base_size)
3430             error (_("Unexpected opcode after "
3431                      "DW_OP_breg%u for symbol \"%s\"."),
3432                    frame_reg, SYMBOL_PRINT_NAME (symbol));
3433         }
3434       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3435         {
3436           /* The frame base is just the register, with no offset.  */
3437           frame_reg = base_data[0] - DW_OP_reg0;
3438           base_offset = 0;
3439         }
3440       else
3441         {
3442           /* We don't know what to do with the frame base expression,
3443              so we can't trace this variable; give up.  */
3444           return save_data;
3445         }
3446
3447       fprintf_filtered (stream,
3448                         _("a variable at frame base reg $%s offset %s+%s"),
3449                         locexpr_regname (gdbarch, frame_reg),
3450                         plongest (base_offset), plongest (frame_offset));
3451     }
3452   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3453            && piece_end_p (data, end))
3454     {
3455       int64_t offset;
3456
3457       data = safe_read_sleb128 (data + 1, end, &offset);
3458
3459       fprintf_filtered (stream,
3460                         _("a variable at offset %s from base reg $%s"),
3461                         plongest (offset),
3462                         locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3463     }
3464
3465   /* The location expression for a TLS variable looks like this (on a
3466      64-bit LE machine):
3467
3468      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3469                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3470
3471      0x3 is the encoding for DW_OP_addr, which has an operand as long
3472      as the size of an address on the target machine (here is 8
3473      bytes).  Note that more recent version of GCC emit DW_OP_const4u
3474      or DW_OP_const8u, depending on address size, rather than
3475      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3476      The operand represents the offset at which the variable is within
3477      the thread local storage.  */
3478
3479   else if (data + 1 + addr_size < end
3480            && (data[0] == DW_OP_addr
3481                || (addr_size == 4 && data[0] == DW_OP_const4u)
3482                || (addr_size == 8 && data[0] == DW_OP_const8u))
3483            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3484            && piece_end_p (data + 2 + addr_size, end))
3485     {
3486       ULONGEST offset;
3487       offset = extract_unsigned_integer (data + 1, addr_size,
3488                                          gdbarch_byte_order (gdbarch));
3489
3490       fprintf_filtered (stream, 
3491                         _("a thread-local variable at offset 0x%s "
3492                           "in the thread-local storage for `%s'"),
3493                         phex_nz (offset, addr_size), objfile->name);
3494
3495       data += 1 + addr_size + 1;
3496     }
3497
3498   /* With -gsplit-dwarf a TLS variable can also look like this:
3499      DW_AT_location    : 3 byte block: fc 4 e0
3500                         (DW_OP_GNU_const_index: 4;
3501                          DW_OP_GNU_push_tls_address)  */
3502   else if (data + 3 <= end
3503            && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3504            && data[0] == DW_OP_GNU_const_index
3505            && leb128_size > 0
3506            && data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3507            && piece_end_p (data + 2 + leb128_size, end))
3508     {
3509       uint64_t offset;
3510
3511       data = safe_read_uleb128 (data + 1, end, &offset);
3512       offset = dwarf2_read_addr_index (per_cu, offset);
3513       fprintf_filtered (stream, 
3514                         _("a thread-local variable at offset 0x%s "
3515                           "in the thread-local storage for `%s'"),
3516                         phex_nz (offset, addr_size), objfile->name);
3517       ++data;
3518     }
3519
3520   else if (data[0] >= DW_OP_lit0
3521            && data[0] <= DW_OP_lit31
3522            && data + 1 < end
3523            && data[1] == DW_OP_stack_value)
3524     {
3525       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3526       data += 2;
3527     }
3528
3529   return data;
3530 }
3531
3532 /* Disassemble an expression, stopping at the end of a piece or at the
3533    end of the expression.  Returns a pointer to the next unread byte
3534    in the input expression.  If ALL is nonzero, then this function
3535    will keep going until it reaches the end of the expression.
3536    If there is an error during reading, e.g. we run off the end
3537    of the buffer, an error is thrown.  */
3538
3539 static const gdb_byte *
3540 disassemble_dwarf_expression (struct ui_file *stream,
3541                               struct gdbarch *arch, unsigned int addr_size,
3542                               int offset_size, const gdb_byte *start,
3543                               const gdb_byte *data, const gdb_byte *end,
3544                               int indent, int all,
3545                               struct dwarf2_per_cu_data *per_cu)
3546 {
3547   while (data < end
3548          && (all
3549              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3550     {
3551       enum dwarf_location_atom op = *data++;
3552       uint64_t ul;
3553       int64_t l;
3554       const char *name;
3555
3556       name = get_DW_OP_name (op);
3557
3558       if (!name)
3559         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3560                op, (long) (data - 1 - start));
3561       fprintf_filtered (stream, "  %*ld: %s", indent + 4,
3562                         (long) (data - 1 - start), name);
3563
3564       switch (op)
3565         {
3566         case DW_OP_addr:
3567           ul = extract_unsigned_integer (data, addr_size,
3568                                          gdbarch_byte_order (arch));
3569           data += addr_size;
3570           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3571           break;
3572
3573         case DW_OP_const1u:
3574           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3575           data += 1;
3576           fprintf_filtered (stream, " %s", pulongest (ul));
3577           break;
3578         case DW_OP_const1s:
3579           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3580           data += 1;
3581           fprintf_filtered (stream, " %s", plongest (l));
3582           break;
3583         case DW_OP_const2u:
3584           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3585           data += 2;
3586           fprintf_filtered (stream, " %s", pulongest (ul));
3587           break;
3588         case DW_OP_const2s:
3589           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3590           data += 2;
3591           fprintf_filtered (stream, " %s", plongest (l));
3592           break;
3593         case DW_OP_const4u:
3594           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3595           data += 4;
3596           fprintf_filtered (stream, " %s", pulongest (ul));
3597           break;
3598         case DW_OP_const4s:
3599           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3600           data += 4;
3601           fprintf_filtered (stream, " %s", plongest (l));
3602           break;
3603         case DW_OP_const8u:
3604           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3605           data += 8;
3606           fprintf_filtered (stream, " %s", pulongest (ul));
3607           break;
3608         case DW_OP_const8s:
3609           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3610           data += 8;
3611           fprintf_filtered (stream, " %s", plongest (l));
3612           break;
3613         case DW_OP_constu:
3614           data = safe_read_uleb128 (data, end, &ul);
3615           fprintf_filtered (stream, " %s", pulongest (ul));
3616           break;
3617         case DW_OP_consts:
3618           data = safe_read_sleb128 (data, end, &l);
3619           fprintf_filtered (stream, " %s", plongest (l));
3620           break;
3621
3622         case DW_OP_reg0:
3623         case DW_OP_reg1:
3624         case DW_OP_reg2:
3625         case DW_OP_reg3:
3626         case DW_OP_reg4:
3627         case DW_OP_reg5:
3628         case DW_OP_reg6:
3629         case DW_OP_reg7:
3630         case DW_OP_reg8:
3631         case DW_OP_reg9:
3632         case DW_OP_reg10:
3633         case DW_OP_reg11:
3634         case DW_OP_reg12:
3635         case DW_OP_reg13:
3636         case DW_OP_reg14:
3637         case DW_OP_reg15:
3638         case DW_OP_reg16:
3639         case DW_OP_reg17:
3640         case DW_OP_reg18:
3641         case DW_OP_reg19:
3642         case DW_OP_reg20:
3643         case DW_OP_reg21:
3644         case DW_OP_reg22:
3645         case DW_OP_reg23:
3646         case DW_OP_reg24:
3647         case DW_OP_reg25:
3648         case DW_OP_reg26:
3649         case DW_OP_reg27:
3650         case DW_OP_reg28:
3651         case DW_OP_reg29:
3652         case DW_OP_reg30:
3653         case DW_OP_reg31:
3654           fprintf_filtered (stream, " [$%s]",
3655                             locexpr_regname (arch, op - DW_OP_reg0));
3656           break;
3657
3658         case DW_OP_regx:
3659           data = safe_read_uleb128 (data, end, &ul);
3660           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3661                             locexpr_regname (arch, (int) ul));
3662           break;
3663
3664         case DW_OP_implicit_value:
3665           data = safe_read_uleb128 (data, end, &ul);
3666           data += ul;
3667           fprintf_filtered (stream, " %s", pulongest (ul));
3668           break;
3669
3670         case DW_OP_breg0:
3671         case DW_OP_breg1:
3672         case DW_OP_breg2:
3673         case DW_OP_breg3:
3674         case DW_OP_breg4:
3675         case DW_OP_breg5:
3676         case DW_OP_breg6:
3677         case DW_OP_breg7:
3678         case DW_OP_breg8:
3679         case DW_OP_breg9:
3680         case DW_OP_breg10:
3681         case DW_OP_breg11:
3682         case DW_OP_breg12:
3683         case DW_OP_breg13:
3684         case DW_OP_breg14:
3685         case DW_OP_breg15:
3686         case DW_OP_breg16:
3687         case DW_OP_breg17:
3688         case DW_OP_breg18:
3689         case DW_OP_breg19:
3690         case DW_OP_breg20:
3691         case DW_OP_breg21:
3692         case DW_OP_breg22:
3693         case DW_OP_breg23:
3694         case DW_OP_breg24:
3695         case DW_OP_breg25:
3696         case DW_OP_breg26:
3697         case DW_OP_breg27:
3698         case DW_OP_breg28:
3699         case DW_OP_breg29:
3700         case DW_OP_breg30:
3701         case DW_OP_breg31:
3702           data = safe_read_sleb128 (data, end, &l);
3703           fprintf_filtered (stream, " %s [$%s]", plongest (l),
3704                             locexpr_regname (arch, op - DW_OP_breg0));
3705           break;
3706
3707         case DW_OP_bregx:
3708           data = safe_read_uleb128 (data, end, &ul);
3709           data = safe_read_sleb128 (data, end, &l);
3710           fprintf_filtered (stream, " register %s [$%s] offset %s",
3711                             pulongest (ul),
3712                             locexpr_regname (arch, (int) ul),
3713                             plongest (l));
3714           break;
3715
3716         case DW_OP_fbreg:
3717           data = safe_read_sleb128 (data, end, &l);
3718           fprintf_filtered (stream, " %s", plongest (l));
3719           break;
3720
3721         case DW_OP_xderef_size:
3722         case DW_OP_deref_size:
3723         case DW_OP_pick:
3724           fprintf_filtered (stream, " %d", *data);
3725           ++data;
3726           break;
3727
3728         case DW_OP_plus_uconst:
3729           data = safe_read_uleb128 (data, end, &ul);
3730           fprintf_filtered (stream, " %s", pulongest (ul));
3731           break;
3732
3733         case DW_OP_skip:
3734           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3735           data += 2;
3736           fprintf_filtered (stream, " to %ld",
3737                             (long) (data + l - start));
3738           break;
3739
3740         case DW_OP_bra:
3741           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3742           data += 2;
3743           fprintf_filtered (stream, " %ld",
3744                             (long) (data + l - start));
3745           break;
3746
3747         case DW_OP_call2:
3748           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3749           data += 2;
3750           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3751           break;
3752
3753         case DW_OP_call4:
3754           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3755           data += 4;
3756           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3757           break;
3758
3759         case DW_OP_call_ref:
3760           ul = extract_unsigned_integer (data, offset_size,
3761                                          gdbarch_byte_order (arch));
3762           data += offset_size;
3763           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3764           break;
3765
3766         case DW_OP_piece:
3767           data = safe_read_uleb128 (data, end, &ul);
3768           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3769           break;
3770
3771         case DW_OP_bit_piece:
3772           {
3773             uint64_t offset;
3774
3775             data = safe_read_uleb128 (data, end, &ul);
3776             data = safe_read_uleb128 (data, end, &offset);
3777             fprintf_filtered (stream, " size %s offset %s (bits)",
3778                               pulongest (ul), pulongest (offset));
3779           }
3780           break;
3781
3782         case DW_OP_GNU_implicit_pointer:
3783           {
3784             ul = extract_unsigned_integer (data, offset_size,
3785                                            gdbarch_byte_order (arch));
3786             data += offset_size;
3787
3788             data = safe_read_sleb128 (data, end, &l);
3789
3790             fprintf_filtered (stream, " DIE %s offset %s",
3791                               phex_nz (ul, offset_size),
3792                               plongest (l));
3793           }
3794           break;
3795
3796         case DW_OP_GNU_deref_type:
3797           {
3798             int addr_size = *data++;
3799             cu_offset offset;
3800             struct type *type;
3801
3802             data = safe_read_uleb128 (data, end, &ul);
3803             offset.cu_off = ul;
3804             type = dwarf2_get_die_type (offset, per_cu);
3805             fprintf_filtered (stream, "<");
3806             type_print (type, "", stream, -1);
3807             fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
3808                               addr_size);
3809           }
3810           break;
3811
3812         case DW_OP_GNU_const_type:
3813           {
3814             cu_offset type_die;
3815             struct type *type;
3816
3817             data = safe_read_uleb128 (data, end, &ul);
3818             type_die.cu_off = ul;
3819             type = dwarf2_get_die_type (type_die, per_cu);
3820             fprintf_filtered (stream, "<");
3821             type_print (type, "", stream, -1);
3822             fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3823           }
3824           break;
3825
3826         case DW_OP_GNU_regval_type:
3827           {
3828             uint64_t reg;
3829             cu_offset type_die;
3830             struct type *type;
3831
3832             data = safe_read_uleb128 (data, end, &reg);
3833             data = safe_read_uleb128 (data, end, &ul);
3834             type_die.cu_off = ul;
3835
3836             type = dwarf2_get_die_type (type_die, per_cu);
3837             fprintf_filtered (stream, "<");
3838             type_print (type, "", stream, -1);
3839             fprintf_filtered (stream, " [0x%s]> [$%s]",
3840                               phex_nz (type_die.cu_off, 0),
3841                               locexpr_regname (arch, reg));
3842           }
3843           break;
3844
3845         case DW_OP_GNU_convert:
3846         case DW_OP_GNU_reinterpret:
3847           {
3848             cu_offset type_die;
3849
3850             data = safe_read_uleb128 (data, end, &ul);
3851             type_die.cu_off = ul;
3852
3853             if (type_die.cu_off == 0)
3854               fprintf_filtered (stream, "<0>");
3855             else
3856               {
3857                 struct type *type;
3858
3859                 type = dwarf2_get_die_type (type_die, per_cu);
3860                 fprintf_filtered (stream, "<");
3861                 type_print (type, "", stream, -1);
3862                 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3863               }
3864           }
3865           break;
3866
3867         case DW_OP_GNU_entry_value:
3868           data = safe_read_uleb128 (data, end, &ul);
3869           fputc_filtered ('\n', stream);
3870           disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
3871                                         start, data, data + ul, indent + 2,
3872                                         all, per_cu);
3873           data += ul;
3874           continue;
3875
3876         case DW_OP_GNU_parameter_ref:
3877           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3878           data += 4;
3879           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3880           break;
3881
3882         case DW_OP_GNU_addr_index:
3883           data = safe_read_uleb128 (data, end, &ul);
3884           ul = dwarf2_read_addr_index (per_cu, ul);
3885           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3886           break;
3887         case DW_OP_GNU_const_index:
3888           data = safe_read_uleb128 (data, end, &ul);
3889           ul = dwarf2_read_addr_index (per_cu, ul);
3890           fprintf_filtered (stream, " %s", pulongest (ul));
3891           break;
3892         }
3893
3894       fprintf_filtered (stream, "\n");
3895     }
3896
3897   return data;
3898 }
3899
3900 /* Describe a single location, which may in turn consist of multiple
3901    pieces.  */
3902
3903 static void
3904 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
3905                              struct ui_file *stream,
3906                              const gdb_byte *data, size_t size,
3907                              struct objfile *objfile, unsigned int addr_size,
3908                              int offset_size, struct dwarf2_per_cu_data *per_cu)
3909 {
3910   const gdb_byte *end = data + size;
3911   int first_piece = 1, bad = 0;
3912
3913   while (data < end)
3914     {
3915       const gdb_byte *here = data;
3916       int disassemble = 1;
3917
3918       if (first_piece)
3919         first_piece = 0;
3920       else
3921         fprintf_filtered (stream, _(", and "));
3922
3923       if (!dwarf2_always_disassemble)
3924         {
3925           data = locexpr_describe_location_piece (symbol, stream,
3926                                                   addr, objfile, per_cu,
3927                                                   data, end, addr_size);
3928           /* If we printed anything, or if we have an empty piece,
3929              then don't disassemble.  */
3930           if (data != here
3931               || data[0] == DW_OP_piece
3932               || data[0] == DW_OP_bit_piece)
3933             disassemble = 0;
3934         }
3935       if (disassemble)
3936         {
3937           fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3938           data = disassemble_dwarf_expression (stream,
3939                                                get_objfile_arch (objfile),
3940                                                addr_size, offset_size, data,
3941                                                data, end, 0,
3942                                                dwarf2_always_disassemble,
3943                                                per_cu);
3944         }
3945
3946       if (data < end)
3947         {
3948           int empty = data == here;
3949               
3950           if (disassemble)
3951             fprintf_filtered (stream, "   ");
3952           if (data[0] == DW_OP_piece)
3953             {
3954               uint64_t bytes;
3955
3956               data = safe_read_uleb128 (data + 1, end, &bytes);
3957
3958               if (empty)
3959                 fprintf_filtered (stream, _("an empty %s-byte piece"),
3960                                   pulongest (bytes));
3961               else
3962                 fprintf_filtered (stream, _(" [%s-byte piece]"),
3963                                   pulongest (bytes));
3964             }
3965           else if (data[0] == DW_OP_bit_piece)
3966             {
3967               uint64_t bits, offset;
3968
3969               data = safe_read_uleb128 (data + 1, end, &bits);
3970               data = safe_read_uleb128 (data, end, &offset);
3971
3972               if (empty)
3973                 fprintf_filtered (stream,
3974                                   _("an empty %s-bit piece"),
3975                                   pulongest (bits));
3976               else
3977                 fprintf_filtered (stream,
3978                                   _(" [%s-bit piece, offset %s bits]"),
3979                                   pulongest (bits), pulongest (offset));
3980             }
3981           else
3982             {
3983               bad = 1;
3984               break;
3985             }
3986         }
3987     }
3988
3989   if (bad || data > end)
3990     error (_("Corrupted DWARF2 expression for \"%s\"."),
3991            SYMBOL_PRINT_NAME (symbol));
3992 }
3993
3994 /* Print a natural-language description of SYMBOL to STREAM.  This
3995    version is for a symbol with a single location.  */
3996
3997 static void
3998 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3999                            struct ui_file *stream)
4000 {
4001   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4002   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4003   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4004   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4005
4006   locexpr_describe_location_1 (symbol, addr, stream,
4007                                dlbaton->data, dlbaton->size,
4008                                objfile, addr_size, offset_size,
4009                                dlbaton->per_cu);
4010 }
4011
4012 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4013    any necessary bytecode in AX.  */
4014
4015 static void
4016 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4017                             struct agent_expr *ax, struct axs_value *value)
4018 {
4019   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4020   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4021
4022   if (dlbaton->size == 0)
4023     value->optimized_out = 1;
4024   else
4025     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
4026                                dlbaton->data, dlbaton->data + dlbaton->size,
4027                                dlbaton->per_cu);
4028 }
4029
4030 /* The set of location functions used with the DWARF-2 expression
4031    evaluator.  */
4032 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4033   locexpr_read_variable,
4034   locexpr_read_variable_at_entry,
4035   locexpr_read_needs_frame,
4036   locexpr_describe_location,
4037   0,    /* location_has_loclist */
4038   locexpr_tracepoint_var_ref
4039 };
4040
4041
4042 /* Wrapper functions for location lists.  These generally find
4043    the appropriate location expression and call something above.  */
4044
4045 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4046    evaluator to calculate the location.  */
4047 static struct value *
4048 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4049 {
4050   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4051   struct value *val;
4052   const gdb_byte *data;
4053   size_t size;
4054   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4055
4056   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4057   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4058                                   dlbaton->per_cu);
4059
4060   return val;
4061 }
4062
4063 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4064    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4065    will be thrown.
4066
4067    Function always returns non-NULL value, it may be marked optimized out if
4068    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
4069    if it cannot resolve the parameter for any reason.  */
4070
4071 static struct value *
4072 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4073 {
4074   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4075   const gdb_byte *data;
4076   size_t size;
4077   CORE_ADDR pc;
4078
4079   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4080     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4081
4082   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4083   if (data == NULL)
4084     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4085
4086   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4087 }
4088
4089 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
4090 static int
4091 loclist_read_needs_frame (struct symbol *symbol)
4092 {
4093   /* If there's a location list, then assume we need to have a frame
4094      to choose the appropriate location expression.  With tracking of
4095      global variables this is not necessarily true, but such tracking
4096      is disabled in GCC at the moment until we figure out how to
4097      represent it.  */
4098
4099   return 1;
4100 }
4101
4102 /* Print a natural-language description of SYMBOL to STREAM.  This
4103    version applies when there is a list of different locations, each
4104    with a specified address range.  */
4105
4106 static void
4107 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4108                            struct ui_file *stream)
4109 {
4110   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4111   const gdb_byte *loc_ptr, *buf_end;
4112   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4113   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4114   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4115   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4116   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4117   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4118   /* Adjust base_address for relocatable objects.  */
4119   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4120   CORE_ADDR base_address = dlbaton->base_address + base_offset;
4121   int done = 0;
4122
4123   loc_ptr = dlbaton->data;
4124   buf_end = dlbaton->data + dlbaton->size;
4125
4126   fprintf_filtered (stream, _("multi-location:\n"));
4127
4128   /* Iterate through locations until we run out.  */
4129   while (!done)
4130     {
4131       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4132       int length;
4133       enum debug_loc_kind kind;
4134       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4135
4136       if (dlbaton->from_dwo)
4137         kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4138                                                loc_ptr, buf_end, &new_ptr,
4139                                                &low, &high, byte_order);
4140       else
4141         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4142                                            &low, &high,
4143                                            byte_order, addr_size,
4144                                            signed_addr_p);
4145       loc_ptr = new_ptr;
4146       switch (kind)
4147         {
4148         case DEBUG_LOC_END_OF_LIST:
4149           done = 1;
4150           continue;
4151         case DEBUG_LOC_BASE_ADDRESS:
4152           base_address = high + base_offset;
4153           fprintf_filtered (stream, _("  Base address %s"),
4154                             paddress (gdbarch, base_address));
4155           continue;
4156         case DEBUG_LOC_START_END:
4157         case DEBUG_LOC_START_LENGTH:
4158           break;
4159         case DEBUG_LOC_BUFFER_OVERFLOW:
4160         case DEBUG_LOC_INVALID_ENTRY:
4161           error (_("Corrupted DWARF expression for symbol \"%s\"."),
4162                  SYMBOL_PRINT_NAME (symbol));
4163         default:
4164           gdb_assert_not_reached ("bad debug_loc_kind");
4165         }
4166
4167       /* Otherwise, a location expression entry.  */
4168       low += base_address;
4169       high += base_address;
4170
4171       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4172       loc_ptr += 2;
4173
4174       /* (It would improve readability to print only the minimum
4175          necessary digits of the second number of the range.)  */
4176       fprintf_filtered (stream, _("  Range %s-%s: "),
4177                         paddress (gdbarch, low), paddress (gdbarch, high));
4178
4179       /* Now describe this particular location.  */
4180       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4181                                    objfile, addr_size, offset_size,
4182                                    dlbaton->per_cu);
4183
4184       fprintf_filtered (stream, "\n");
4185
4186       loc_ptr += length;
4187     }
4188 }
4189
4190 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4191    any necessary bytecode in AX.  */
4192 static void
4193 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4194                             struct agent_expr *ax, struct axs_value *value)
4195 {
4196   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4197   const gdb_byte *data;
4198   size_t size;
4199   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4200
4201   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4202   if (size == 0)
4203     value->optimized_out = 1;
4204   else
4205     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4206                                dlbaton->per_cu);
4207 }
4208
4209 /* The set of location functions used with the DWARF-2 expression
4210    evaluator and location lists.  */
4211 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4212   loclist_read_variable,
4213   loclist_read_variable_at_entry,
4214   loclist_read_needs_frame,
4215   loclist_describe_location,
4216   1,    /* location_has_loclist */
4217   loclist_tracepoint_var_ref
4218 };
4219
4220 /* Provide a prototype to silence -Wmissing-prototypes.  */
4221 extern initialize_file_ftype _initialize_dwarf2loc;
4222
4223 void
4224 _initialize_dwarf2loc (void)
4225 {
4226   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4227                              &entry_values_debug,
4228                              _("Set entry values and tail call frames "
4229                                "debugging."),
4230                              _("Show entry values and tail call frames "
4231                                "debugging."),
4232                              _("When non-zero, the process of determining "
4233                                "parameter values from function entry point "
4234                                "and tail call frames will be printed."),
4235                              NULL,
4236                              show_entry_values_debug,
4237                              &setdebuglist, &showdebuglist);
4238 }