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