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