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