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