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