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