2013-01-31 Aleksandar Ristovski <aristovski@qnx.com>
[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
2869             b = block_for_pc (expr->scope);
2870
2871             if (!b)
2872               error (_("No block found for address"));
2873
2874             framefunc = block_linkage_function (b);
2875
2876             if (!framefunc)
2877               error (_("No function found for block"));
2878
2879             dwarf_expr_frame_base_1 (framefunc, expr->scope,
2880                                      &datastart, &datalen);
2881
2882             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2883             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2884                                        datastart + datalen, per_cu);
2885             if (loc->kind == axs_lvalue_register)
2886               require_rvalue (expr, loc);
2887
2888             if (offset != 0)
2889               {
2890                 ax_const_l (expr, offset);
2891                 ax_simple (expr, aop_add);
2892               }
2893
2894             loc->kind = axs_lvalue_memory;
2895           }
2896           break;
2897
2898         case DW_OP_dup:
2899           ax_simple (expr, aop_dup);
2900           break;
2901
2902         case DW_OP_drop:
2903           ax_simple (expr, aop_pop);
2904           break;
2905
2906         case DW_OP_pick:
2907           offset = *op_ptr++;
2908           ax_pick (expr, offset);
2909           break;
2910           
2911         case DW_OP_swap:
2912           ax_simple (expr, aop_swap);
2913           break;
2914
2915         case DW_OP_over:
2916           ax_pick (expr, 1);
2917           break;
2918
2919         case DW_OP_rot:
2920           ax_simple (expr, aop_rot);
2921           break;
2922
2923         case DW_OP_deref:
2924         case DW_OP_deref_size:
2925           {
2926             int size;
2927
2928             if (op == DW_OP_deref_size)
2929               size = *op_ptr++;
2930             else
2931               size = addr_size;
2932
2933             switch (size)
2934               {
2935               case 8:
2936                 ax_simple (expr, aop_ref8);
2937                 break;
2938               case 16:
2939                 ax_simple (expr, aop_ref16);
2940                 break;
2941               case 32:
2942                 ax_simple (expr, aop_ref32);
2943                 break;
2944               case 64:
2945                 ax_simple (expr, aop_ref64);
2946                 break;
2947               default:
2948                 /* Note that get_DW_OP_name will never return
2949                    NULL here.  */
2950                 error (_("Unsupported size %d in %s"),
2951                        size, get_DW_OP_name (op));
2952               }
2953           }
2954           break;
2955
2956         case DW_OP_abs:
2957           /* Sign extend the operand.  */
2958           ax_ext (expr, addr_size_bits);
2959           ax_simple (expr, aop_dup);
2960           ax_const_l (expr, 0);
2961           ax_simple (expr, aop_less_signed);
2962           ax_simple (expr, aop_log_not);
2963           i = ax_goto (expr, aop_if_goto);
2964           /* We have to emit 0 - X.  */
2965           ax_const_l (expr, 0);
2966           ax_simple (expr, aop_swap);
2967           ax_simple (expr, aop_sub);
2968           ax_label (expr, i, expr->len);
2969           break;
2970
2971         case DW_OP_neg:
2972           /* No need to sign extend here.  */
2973           ax_const_l (expr, 0);
2974           ax_simple (expr, aop_swap);
2975           ax_simple (expr, aop_sub);
2976           break;
2977
2978         case DW_OP_not:
2979           /* Sign extend the operand.  */
2980           ax_ext (expr, addr_size_bits);
2981           ax_simple (expr, aop_bit_not);
2982           break;
2983
2984         case DW_OP_plus_uconst:
2985           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
2986           /* It would be really weird to emit `DW_OP_plus_uconst 0',
2987              but we micro-optimize anyhow.  */
2988           if (reg != 0)
2989             {
2990               ax_const_l (expr, reg);
2991               ax_simple (expr, aop_add);
2992             }
2993           break;
2994
2995         case DW_OP_and:
2996           ax_simple (expr, aop_bit_and);
2997           break;
2998
2999         case DW_OP_div:
3000           /* Sign extend the operands.  */
3001           ax_ext (expr, addr_size_bits);
3002           ax_simple (expr, aop_swap);
3003           ax_ext (expr, addr_size_bits);
3004           ax_simple (expr, aop_swap);
3005           ax_simple (expr, aop_div_signed);
3006           break;
3007
3008         case DW_OP_minus:
3009           ax_simple (expr, aop_sub);
3010           break;
3011
3012         case DW_OP_mod:
3013           ax_simple (expr, aop_rem_unsigned);
3014           break;
3015
3016         case DW_OP_mul:
3017           ax_simple (expr, aop_mul);
3018           break;
3019
3020         case DW_OP_or:
3021           ax_simple (expr, aop_bit_or);
3022           break;
3023
3024         case DW_OP_plus:
3025           ax_simple (expr, aop_add);
3026           break;
3027
3028         case DW_OP_shl:
3029           ax_simple (expr, aop_lsh);
3030           break;
3031
3032         case DW_OP_shr:
3033           ax_simple (expr, aop_rsh_unsigned);
3034           break;
3035
3036         case DW_OP_shra:
3037           ax_simple (expr, aop_rsh_signed);
3038           break;
3039
3040         case DW_OP_xor:
3041           ax_simple (expr, aop_bit_xor);
3042           break;
3043
3044         case DW_OP_le:
3045           /* Sign extend the operands.  */
3046           ax_ext (expr, addr_size_bits);
3047           ax_simple (expr, aop_swap);
3048           ax_ext (expr, addr_size_bits);
3049           /* Note no swap here: A <= B is !(B < A).  */
3050           ax_simple (expr, aop_less_signed);
3051           ax_simple (expr, aop_log_not);
3052           break;
3053
3054         case DW_OP_ge:
3055           /* Sign extend the operands.  */
3056           ax_ext (expr, addr_size_bits);
3057           ax_simple (expr, aop_swap);
3058           ax_ext (expr, addr_size_bits);
3059           ax_simple (expr, aop_swap);
3060           /* A >= B is !(A < B).  */
3061           ax_simple (expr, aop_less_signed);
3062           ax_simple (expr, aop_log_not);
3063           break;
3064
3065         case DW_OP_eq:
3066           /* Sign extend the operands.  */
3067           ax_ext (expr, addr_size_bits);
3068           ax_simple (expr, aop_swap);
3069           ax_ext (expr, addr_size_bits);
3070           /* No need for a second swap here.  */
3071           ax_simple (expr, aop_equal);
3072           break;
3073
3074         case DW_OP_lt:
3075           /* Sign extend the operands.  */
3076           ax_ext (expr, addr_size_bits);
3077           ax_simple (expr, aop_swap);
3078           ax_ext (expr, addr_size_bits);
3079           ax_simple (expr, aop_swap);
3080           ax_simple (expr, aop_less_signed);
3081           break;
3082
3083         case DW_OP_gt:
3084           /* Sign extend the operands.  */
3085           ax_ext (expr, addr_size_bits);
3086           ax_simple (expr, aop_swap);
3087           ax_ext (expr, addr_size_bits);
3088           /* Note no swap here: A > B is B < A.  */
3089           ax_simple (expr, aop_less_signed);
3090           break;
3091
3092         case DW_OP_ne:
3093           /* Sign extend the operands.  */
3094           ax_ext (expr, addr_size_bits);
3095           ax_simple (expr, aop_swap);
3096           ax_ext (expr, addr_size_bits);
3097           /* No need for a swap here.  */
3098           ax_simple (expr, aop_equal);
3099           ax_simple (expr, aop_log_not);
3100           break;
3101
3102         case DW_OP_call_frame_cfa:
3103           dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
3104           loc->kind = axs_lvalue_memory;
3105           break;
3106
3107         case DW_OP_GNU_push_tls_address:
3108           unimplemented (op);
3109           break;
3110
3111         case DW_OP_skip:
3112           offset = extract_signed_integer (op_ptr, 2, byte_order);
3113           op_ptr += 2;
3114           i = ax_goto (expr, aop_goto);
3115           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3116           VEC_safe_push (int, patches, i);
3117           break;
3118
3119         case DW_OP_bra:
3120           offset = extract_signed_integer (op_ptr, 2, byte_order);
3121           op_ptr += 2;
3122           /* Zero extend the operand.  */
3123           ax_zero_ext (expr, addr_size_bits);
3124           i = ax_goto (expr, aop_if_goto);
3125           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3126           VEC_safe_push (int, patches, i);
3127           break;
3128
3129         case DW_OP_nop:
3130           break;
3131
3132         case DW_OP_piece:
3133         case DW_OP_bit_piece:
3134           {
3135             uint64_t size, offset;
3136
3137             if (op_ptr - 1 == previous_piece)
3138               error (_("Cannot translate empty pieces to agent expressions"));
3139             previous_piece = op_ptr - 1;
3140
3141             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3142             if (op == DW_OP_piece)
3143               {
3144                 size *= 8;
3145                 offset = 0;
3146               }
3147             else
3148               op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3149
3150             if (bits_collected + size > 8 * sizeof (LONGEST))
3151               error (_("Expression pieces exceed word size"));
3152
3153             /* Access the bits.  */
3154             switch (loc->kind)
3155               {
3156               case axs_lvalue_register:
3157                 ax_reg (expr, loc->u.reg);
3158                 break;
3159
3160               case axs_lvalue_memory:
3161                 /* Offset the pointer, if needed.  */
3162                 if (offset > 8)
3163                   {
3164                     ax_const_l (expr, offset / 8);
3165                     ax_simple (expr, aop_add);
3166                     offset %= 8;
3167                   }
3168                 access_memory (arch, expr, size);
3169                 break;
3170               }
3171
3172             /* For a bits-big-endian target, shift up what we already
3173                have.  For a bits-little-endian target, shift up the
3174                new data.  Note that there is a potential bug here if
3175                the DWARF expression leaves multiple values on the
3176                stack.  */
3177             if (bits_collected > 0)
3178               {
3179                 if (bits_big_endian)
3180                   {
3181                     ax_simple (expr, aop_swap);
3182                     ax_const_l (expr, size);
3183                     ax_simple (expr, aop_lsh);
3184                     /* We don't need a second swap here, because
3185                        aop_bit_or is symmetric.  */
3186                   }
3187                 else
3188                   {
3189                     ax_const_l (expr, size);
3190                     ax_simple (expr, aop_lsh);
3191                   }
3192                 ax_simple (expr, aop_bit_or);
3193               }
3194
3195             bits_collected += size;
3196             loc->kind = axs_rvalue;
3197           }
3198           break;
3199
3200         case DW_OP_GNU_uninit:
3201           unimplemented (op);
3202
3203         case DW_OP_call2:
3204         case DW_OP_call4:
3205           {
3206             struct dwarf2_locexpr_baton block;
3207             int size = (op == DW_OP_call2 ? 2 : 4);
3208             cu_offset offset;
3209
3210             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3211             op_ptr += size;
3212
3213             offset.cu_off = uoffset;
3214             block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3215                                                  get_ax_pc, expr);
3216
3217             /* DW_OP_call_ref is currently not supported.  */
3218             gdb_assert (block.per_cu == per_cu);
3219
3220             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3221                                        block.data, block.data + block.size,
3222                                        per_cu);
3223           }
3224           break;
3225
3226         case DW_OP_call_ref:
3227           unimplemented (op);
3228
3229         default:
3230           unimplemented (op);
3231         }
3232     }
3233
3234   /* Patch all the branches we emitted.  */
3235   for (i = 0; i < VEC_length (int, patches); ++i)
3236     {
3237       int targ = offsets[VEC_index (int, dw_labels, i)];
3238       if (targ == -1)
3239         internal_error (__FILE__, __LINE__, _("invalid label"));
3240       ax_label (expr, VEC_index (int, patches, i), targ);
3241     }
3242
3243   do_cleanups (cleanups);
3244 }
3245
3246 \f
3247 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3248    evaluator to calculate the location.  */
3249 static struct value *
3250 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3251 {
3252   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3253   struct value *val;
3254
3255   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3256                                   dlbaton->size, dlbaton->per_cu);
3257
3258   return val;
3259 }
3260
3261 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3262    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3263    will be thrown.  */
3264
3265 static struct value *
3266 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3267 {
3268   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3269
3270   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3271                                      dlbaton->size);
3272 }
3273
3274 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
3275 static int
3276 locexpr_read_needs_frame (struct symbol *symbol)
3277 {
3278   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3279
3280   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3281                                       dlbaton->per_cu);
3282 }
3283
3284 /* Return true if DATA points to the end of a piece.  END is one past
3285    the last byte in the expression.  */
3286
3287 static int
3288 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3289 {
3290   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3291 }
3292
3293 /* Helper for locexpr_describe_location_piece that finds the name of a
3294    DWARF register.  */
3295
3296 static const char *
3297 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3298 {
3299   int regnum;
3300
3301   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
3302   return gdbarch_register_name (gdbarch, regnum);
3303 }
3304
3305 /* Nicely describe a single piece of a location, returning an updated
3306    position in the bytecode sequence.  This function cannot recognize
3307    all locations; if a location is not recognized, it simply returns
3308    DATA.  If there is an error during reading, e.g. we run off the end
3309    of the buffer, an error is thrown.  */
3310
3311 static const gdb_byte *
3312 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3313                                  CORE_ADDR addr, struct objfile *objfile,
3314                                  struct dwarf2_per_cu_data *per_cu,
3315                                  const gdb_byte *data, const gdb_byte *end,
3316                                  unsigned int addr_size)
3317 {
3318   struct gdbarch *gdbarch = get_objfile_arch (objfile);
3319   size_t leb128_size;
3320
3321   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3322     {
3323       fprintf_filtered (stream, _("a variable in $%s"),
3324                         locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3325       data += 1;
3326     }
3327   else if (data[0] == DW_OP_regx)
3328     {
3329       uint64_t reg;
3330
3331       data = safe_read_uleb128 (data + 1, end, &reg);
3332       fprintf_filtered (stream, _("a variable in $%s"),
3333                         locexpr_regname (gdbarch, reg));
3334     }
3335   else if (data[0] == DW_OP_fbreg)
3336     {
3337       struct block *b;
3338       struct symbol *framefunc;
3339       int frame_reg = 0;
3340       int64_t frame_offset;
3341       const gdb_byte *base_data, *new_data, *save_data = data;
3342       size_t base_size;
3343       int64_t base_offset = 0;
3344
3345       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3346       if (!piece_end_p (new_data, end))
3347         return data;
3348       data = new_data;
3349
3350       b = block_for_pc (addr);
3351
3352       if (!b)
3353         error (_("No block found for address for symbol \"%s\"."),
3354                SYMBOL_PRINT_NAME (symbol));
3355
3356       framefunc = block_linkage_function (b);
3357
3358       if (!framefunc)
3359         error (_("No function found for block for symbol \"%s\"."),
3360                SYMBOL_PRINT_NAME (symbol));
3361
3362       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3363
3364       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3365         {
3366           const gdb_byte *buf_end;
3367           
3368           frame_reg = base_data[0] - DW_OP_breg0;
3369           buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3370                                        &base_offset);
3371           if (buf_end != base_data + base_size)
3372             error (_("Unexpected opcode after "
3373                      "DW_OP_breg%u for symbol \"%s\"."),
3374                    frame_reg, SYMBOL_PRINT_NAME (symbol));
3375         }
3376       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3377         {
3378           /* The frame base is just the register, with no offset.  */
3379           frame_reg = base_data[0] - DW_OP_reg0;
3380           base_offset = 0;
3381         }
3382       else
3383         {
3384           /* We don't know what to do with the frame base expression,
3385              so we can't trace this variable; give up.  */
3386           return save_data;
3387         }
3388
3389       fprintf_filtered (stream,
3390                         _("a variable at frame base reg $%s offset %s+%s"),
3391                         locexpr_regname (gdbarch, frame_reg),
3392                         plongest (base_offset), plongest (frame_offset));
3393     }
3394   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3395            && piece_end_p (data, end))
3396     {
3397       int64_t offset;
3398
3399       data = safe_read_sleb128 (data + 1, end, &offset);
3400
3401       fprintf_filtered (stream,
3402                         _("a variable at offset %s from base reg $%s"),
3403                         plongest (offset),
3404                         locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3405     }
3406
3407   /* The location expression for a TLS variable looks like this (on a
3408      64-bit LE machine):
3409
3410      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3411                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3412
3413      0x3 is the encoding for DW_OP_addr, which has an operand as long
3414      as the size of an address on the target machine (here is 8
3415      bytes).  Note that more recent version of GCC emit DW_OP_const4u
3416      or DW_OP_const8u, depending on address size, rather than
3417      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3418      The operand represents the offset at which the variable is within
3419      the thread local storage.  */
3420
3421   else if (data + 1 + addr_size < end
3422            && (data[0] == DW_OP_addr
3423                || (addr_size == 4 && data[0] == DW_OP_const4u)
3424                || (addr_size == 8 && data[0] == DW_OP_const8u))
3425            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3426            && piece_end_p (data + 2 + addr_size, end))
3427     {
3428       ULONGEST offset;
3429       offset = extract_unsigned_integer (data + 1, addr_size,
3430                                          gdbarch_byte_order (gdbarch));
3431
3432       fprintf_filtered (stream, 
3433                         _("a thread-local variable at offset 0x%s "
3434                           "in the thread-local storage for `%s'"),
3435                         phex_nz (offset, addr_size), objfile->name);
3436
3437       data += 1 + addr_size + 1;
3438     }
3439
3440   /* With -gsplit-dwarf a TLS variable can also look like this:
3441      DW_AT_location    : 3 byte block: fc 4 e0
3442                         (DW_OP_GNU_const_index: 4;
3443                          DW_OP_GNU_push_tls_address)  */
3444   else if (data + 3 <= end
3445            && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3446            && data[0] == DW_OP_GNU_const_index
3447            && leb128_size > 0
3448            && data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3449            && piece_end_p (data + 2 + leb128_size, end))
3450     {
3451       uint64_t offset;
3452
3453       data = safe_read_uleb128 (data + 1, end, &offset);
3454       offset = dwarf2_read_addr_index (per_cu, offset);
3455       fprintf_filtered (stream, 
3456                         _("a thread-local variable at offset 0x%s "
3457                           "in the thread-local storage for `%s'"),
3458                         phex_nz (offset, addr_size), objfile->name);
3459       ++data;
3460     }
3461
3462   else if (data[0] >= DW_OP_lit0
3463            && data[0] <= DW_OP_lit31
3464            && data + 1 < end
3465            && data[1] == DW_OP_stack_value)
3466     {
3467       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3468       data += 2;
3469     }
3470
3471   return data;
3472 }
3473
3474 /* Disassemble an expression, stopping at the end of a piece or at the
3475    end of the expression.  Returns a pointer to the next unread byte
3476    in the input expression.  If ALL is nonzero, then this function
3477    will keep going until it reaches the end of the expression.
3478    If there is an error during reading, e.g. we run off the end
3479    of the buffer, an error is thrown.  */
3480
3481 static const gdb_byte *
3482 disassemble_dwarf_expression (struct ui_file *stream,
3483                               struct gdbarch *arch, unsigned int addr_size,
3484                               int offset_size, const gdb_byte *start,
3485                               const gdb_byte *data, const gdb_byte *end,
3486                               int indent, int all,
3487                               struct dwarf2_per_cu_data *per_cu)
3488 {
3489   while (data < end
3490          && (all
3491              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3492     {
3493       enum dwarf_location_atom op = *data++;
3494       uint64_t ul;
3495       int64_t l;
3496       const char *name;
3497
3498       name = get_DW_OP_name (op);
3499
3500       if (!name)
3501         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3502                op, (long) (data - 1 - start));
3503       fprintf_filtered (stream, "  %*ld: %s", indent + 4,
3504                         (long) (data - 1 - start), name);
3505
3506       switch (op)
3507         {
3508         case DW_OP_addr:
3509           ul = extract_unsigned_integer (data, addr_size,
3510                                          gdbarch_byte_order (arch));
3511           data += addr_size;
3512           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3513           break;
3514
3515         case DW_OP_const1u:
3516           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3517           data += 1;
3518           fprintf_filtered (stream, " %s", pulongest (ul));
3519           break;
3520         case DW_OP_const1s:
3521           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3522           data += 1;
3523           fprintf_filtered (stream, " %s", plongest (l));
3524           break;
3525         case DW_OP_const2u:
3526           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3527           data += 2;
3528           fprintf_filtered (stream, " %s", pulongest (ul));
3529           break;
3530         case DW_OP_const2s:
3531           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3532           data += 2;
3533           fprintf_filtered (stream, " %s", plongest (l));
3534           break;
3535         case DW_OP_const4u:
3536           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3537           data += 4;
3538           fprintf_filtered (stream, " %s", pulongest (ul));
3539           break;
3540         case DW_OP_const4s:
3541           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3542           data += 4;
3543           fprintf_filtered (stream, " %s", plongest (l));
3544           break;
3545         case DW_OP_const8u:
3546           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3547           data += 8;
3548           fprintf_filtered (stream, " %s", pulongest (ul));
3549           break;
3550         case DW_OP_const8s:
3551           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3552           data += 8;
3553           fprintf_filtered (stream, " %s", plongest (l));
3554           break;
3555         case DW_OP_constu:
3556           data = safe_read_uleb128 (data, end, &ul);
3557           fprintf_filtered (stream, " %s", pulongest (ul));
3558           break;
3559         case DW_OP_consts:
3560           data = safe_read_sleb128 (data, end, &l);
3561           fprintf_filtered (stream, " %s", plongest (l));
3562           break;
3563
3564         case DW_OP_reg0:
3565         case DW_OP_reg1:
3566         case DW_OP_reg2:
3567         case DW_OP_reg3:
3568         case DW_OP_reg4:
3569         case DW_OP_reg5:
3570         case DW_OP_reg6:
3571         case DW_OP_reg7:
3572         case DW_OP_reg8:
3573         case DW_OP_reg9:
3574         case DW_OP_reg10:
3575         case DW_OP_reg11:
3576         case DW_OP_reg12:
3577         case DW_OP_reg13:
3578         case DW_OP_reg14:
3579         case DW_OP_reg15:
3580         case DW_OP_reg16:
3581         case DW_OP_reg17:
3582         case DW_OP_reg18:
3583         case DW_OP_reg19:
3584         case DW_OP_reg20:
3585         case DW_OP_reg21:
3586         case DW_OP_reg22:
3587         case DW_OP_reg23:
3588         case DW_OP_reg24:
3589         case DW_OP_reg25:
3590         case DW_OP_reg26:
3591         case DW_OP_reg27:
3592         case DW_OP_reg28:
3593         case DW_OP_reg29:
3594         case DW_OP_reg30:
3595         case DW_OP_reg31:
3596           fprintf_filtered (stream, " [$%s]",
3597                             locexpr_regname (arch, op - DW_OP_reg0));
3598           break;
3599
3600         case DW_OP_regx:
3601           data = safe_read_uleb128 (data, end, &ul);
3602           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3603                             locexpr_regname (arch, (int) ul));
3604           break;
3605
3606         case DW_OP_implicit_value:
3607           data = safe_read_uleb128 (data, end, &ul);
3608           data += ul;
3609           fprintf_filtered (stream, " %s", pulongest (ul));
3610           break;
3611
3612         case DW_OP_breg0:
3613         case DW_OP_breg1:
3614         case DW_OP_breg2:
3615         case DW_OP_breg3:
3616         case DW_OP_breg4:
3617         case DW_OP_breg5:
3618         case DW_OP_breg6:
3619         case DW_OP_breg7:
3620         case DW_OP_breg8:
3621         case DW_OP_breg9:
3622         case DW_OP_breg10:
3623         case DW_OP_breg11:
3624         case DW_OP_breg12:
3625         case DW_OP_breg13:
3626         case DW_OP_breg14:
3627         case DW_OP_breg15:
3628         case DW_OP_breg16:
3629         case DW_OP_breg17:
3630         case DW_OP_breg18:
3631         case DW_OP_breg19:
3632         case DW_OP_breg20:
3633         case DW_OP_breg21:
3634         case DW_OP_breg22:
3635         case DW_OP_breg23:
3636         case DW_OP_breg24:
3637         case DW_OP_breg25:
3638         case DW_OP_breg26:
3639         case DW_OP_breg27:
3640         case DW_OP_breg28:
3641         case DW_OP_breg29:
3642         case DW_OP_breg30:
3643         case DW_OP_breg31:
3644           data = safe_read_sleb128 (data, end, &l);
3645           fprintf_filtered (stream, " %s [$%s]", plongest (l),
3646                             locexpr_regname (arch, op - DW_OP_breg0));
3647           break;
3648
3649         case DW_OP_bregx:
3650           data = safe_read_uleb128 (data, end, &ul);
3651           data = safe_read_sleb128 (data, end, &l);
3652           fprintf_filtered (stream, " register %s [$%s] offset %s",
3653                             pulongest (ul),
3654                             locexpr_regname (arch, (int) ul),
3655                             plongest (l));
3656           break;
3657
3658         case DW_OP_fbreg:
3659           data = safe_read_sleb128 (data, end, &l);
3660           fprintf_filtered (stream, " %s", plongest (l));
3661           break;
3662
3663         case DW_OP_xderef_size:
3664         case DW_OP_deref_size:
3665         case DW_OP_pick:
3666           fprintf_filtered (stream, " %d", *data);
3667           ++data;
3668           break;
3669
3670         case DW_OP_plus_uconst:
3671           data = safe_read_uleb128 (data, end, &ul);
3672           fprintf_filtered (stream, " %s", pulongest (ul));
3673           break;
3674
3675         case DW_OP_skip:
3676           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3677           data += 2;
3678           fprintf_filtered (stream, " to %ld",
3679                             (long) (data + l - start));
3680           break;
3681
3682         case DW_OP_bra:
3683           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3684           data += 2;
3685           fprintf_filtered (stream, " %ld",
3686                             (long) (data + l - start));
3687           break;
3688
3689         case DW_OP_call2:
3690           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3691           data += 2;
3692           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3693           break;
3694
3695         case DW_OP_call4:
3696           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3697           data += 4;
3698           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3699           break;
3700
3701         case DW_OP_call_ref:
3702           ul = extract_unsigned_integer (data, offset_size,
3703                                          gdbarch_byte_order (arch));
3704           data += offset_size;
3705           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3706           break;
3707
3708         case DW_OP_piece:
3709           data = safe_read_uleb128 (data, end, &ul);
3710           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3711           break;
3712
3713         case DW_OP_bit_piece:
3714           {
3715             uint64_t offset;
3716
3717             data = safe_read_uleb128 (data, end, &ul);
3718             data = safe_read_uleb128 (data, end, &offset);
3719             fprintf_filtered (stream, " size %s offset %s (bits)",
3720                               pulongest (ul), pulongest (offset));
3721           }
3722           break;
3723
3724         case DW_OP_GNU_implicit_pointer:
3725           {
3726             ul = extract_unsigned_integer (data, offset_size,
3727                                            gdbarch_byte_order (arch));
3728             data += offset_size;
3729
3730             data = safe_read_sleb128 (data, end, &l);
3731
3732             fprintf_filtered (stream, " DIE %s offset %s",
3733                               phex_nz (ul, offset_size),
3734                               plongest (l));
3735           }
3736           break;
3737
3738         case DW_OP_GNU_deref_type:
3739           {
3740             int addr_size = *data++;
3741             cu_offset offset;
3742             struct type *type;
3743
3744             data = safe_read_uleb128 (data, end, &ul);
3745             offset.cu_off = ul;
3746             type = dwarf2_get_die_type (offset, per_cu);
3747             fprintf_filtered (stream, "<");
3748             type_print (type, "", stream, -1);
3749             fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
3750                               addr_size);
3751           }
3752           break;
3753
3754         case DW_OP_GNU_const_type:
3755           {
3756             cu_offset type_die;
3757             struct type *type;
3758
3759             data = safe_read_uleb128 (data, end, &ul);
3760             type_die.cu_off = ul;
3761             type = dwarf2_get_die_type (type_die, per_cu);
3762             fprintf_filtered (stream, "<");
3763             type_print (type, "", stream, -1);
3764             fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3765           }
3766           break;
3767
3768         case DW_OP_GNU_regval_type:
3769           {
3770             uint64_t reg;
3771             cu_offset type_die;
3772             struct type *type;
3773
3774             data = safe_read_uleb128 (data, end, &reg);
3775             data = safe_read_uleb128 (data, end, &ul);
3776             type_die.cu_off = ul;
3777
3778             type = dwarf2_get_die_type (type_die, per_cu);
3779             fprintf_filtered (stream, "<");
3780             type_print (type, "", stream, -1);
3781             fprintf_filtered (stream, " [0x%s]> [$%s]",
3782                               phex_nz (type_die.cu_off, 0),
3783                               locexpr_regname (arch, reg));
3784           }
3785           break;
3786
3787         case DW_OP_GNU_convert:
3788         case DW_OP_GNU_reinterpret:
3789           {
3790             cu_offset type_die;
3791
3792             data = safe_read_uleb128 (data, end, &ul);
3793             type_die.cu_off = ul;
3794
3795             if (type_die.cu_off == 0)
3796               fprintf_filtered (stream, "<0>");
3797             else
3798               {
3799                 struct type *type;
3800
3801                 type = dwarf2_get_die_type (type_die, per_cu);
3802                 fprintf_filtered (stream, "<");
3803                 type_print (type, "", stream, -1);
3804                 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3805               }
3806           }
3807           break;
3808
3809         case DW_OP_GNU_entry_value:
3810           data = safe_read_uleb128 (data, end, &ul);
3811           fputc_filtered ('\n', stream);
3812           disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
3813                                         start, data, data + ul, indent + 2,
3814                                         all, per_cu);
3815           data += ul;
3816           continue;
3817
3818         case DW_OP_GNU_parameter_ref:
3819           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3820           data += 4;
3821           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3822           break;
3823
3824         case DW_OP_GNU_addr_index:
3825           data = safe_read_uleb128 (data, end, &ul);
3826           ul = dwarf2_read_addr_index (per_cu, ul);
3827           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3828           break;
3829         case DW_OP_GNU_const_index:
3830           data = safe_read_uleb128 (data, end, &ul);
3831           ul = dwarf2_read_addr_index (per_cu, ul);
3832           fprintf_filtered (stream, " %s", pulongest (ul));
3833           break;
3834         }
3835
3836       fprintf_filtered (stream, "\n");
3837     }
3838
3839   return data;
3840 }
3841
3842 /* Describe a single location, which may in turn consist of multiple
3843    pieces.  */
3844
3845 static void
3846 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
3847                              struct ui_file *stream,
3848                              const gdb_byte *data, size_t size,
3849                              struct objfile *objfile, unsigned int addr_size,
3850                              int offset_size, struct dwarf2_per_cu_data *per_cu)
3851 {
3852   const gdb_byte *end = data + size;
3853   int first_piece = 1, bad = 0;
3854
3855   while (data < end)
3856     {
3857       const gdb_byte *here = data;
3858       int disassemble = 1;
3859
3860       if (first_piece)
3861         first_piece = 0;
3862       else
3863         fprintf_filtered (stream, _(", and "));
3864
3865       if (!dwarf2_always_disassemble)
3866         {
3867           data = locexpr_describe_location_piece (symbol, stream,
3868                                                   addr, objfile, per_cu,
3869                                                   data, end, addr_size);
3870           /* If we printed anything, or if we have an empty piece,
3871              then don't disassemble.  */
3872           if (data != here
3873               || data[0] == DW_OP_piece
3874               || data[0] == DW_OP_bit_piece)
3875             disassemble = 0;
3876         }
3877       if (disassemble)
3878         {
3879           fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3880           data = disassemble_dwarf_expression (stream,
3881                                                get_objfile_arch (objfile),
3882                                                addr_size, offset_size, data,
3883                                                data, end, 0,
3884                                                dwarf2_always_disassemble,
3885                                                per_cu);
3886         }
3887
3888       if (data < end)
3889         {
3890           int empty = data == here;
3891               
3892           if (disassemble)
3893             fprintf_filtered (stream, "   ");
3894           if (data[0] == DW_OP_piece)
3895             {
3896               uint64_t bytes;
3897
3898               data = safe_read_uleb128 (data + 1, end, &bytes);
3899
3900               if (empty)
3901                 fprintf_filtered (stream, _("an empty %s-byte piece"),
3902                                   pulongest (bytes));
3903               else
3904                 fprintf_filtered (stream, _(" [%s-byte piece]"),
3905                                   pulongest (bytes));
3906             }
3907           else if (data[0] == DW_OP_bit_piece)
3908             {
3909               uint64_t bits, offset;
3910
3911               data = safe_read_uleb128 (data + 1, end, &bits);
3912               data = safe_read_uleb128 (data, end, &offset);
3913
3914               if (empty)
3915                 fprintf_filtered (stream,
3916                                   _("an empty %s-bit piece"),
3917                                   pulongest (bits));
3918               else
3919                 fprintf_filtered (stream,
3920                                   _(" [%s-bit piece, offset %s bits]"),
3921                                   pulongest (bits), pulongest (offset));
3922             }
3923           else
3924             {
3925               bad = 1;
3926               break;
3927             }
3928         }
3929     }
3930
3931   if (bad || data > end)
3932     error (_("Corrupted DWARF2 expression for \"%s\"."),
3933            SYMBOL_PRINT_NAME (symbol));
3934 }
3935
3936 /* Print a natural-language description of SYMBOL to STREAM.  This
3937    version is for a symbol with a single location.  */
3938
3939 static void
3940 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3941                            struct ui_file *stream)
3942 {
3943   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3944   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3945   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3946   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3947
3948   locexpr_describe_location_1 (symbol, addr, stream,
3949                                dlbaton->data, dlbaton->size,
3950                                objfile, addr_size, offset_size,
3951                                dlbaton->per_cu);
3952 }
3953
3954 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3955    any necessary bytecode in AX.  */
3956
3957 static void
3958 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3959                             struct agent_expr *ax, struct axs_value *value)
3960 {
3961   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3962   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3963
3964   if (dlbaton->size == 0)
3965     value->optimized_out = 1;
3966   else
3967     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
3968                                dlbaton->data, dlbaton->data + dlbaton->size,
3969                                dlbaton->per_cu);
3970 }
3971
3972 /* The set of location functions used with the DWARF-2 expression
3973    evaluator.  */
3974 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
3975   locexpr_read_variable,
3976   locexpr_read_variable_at_entry,
3977   locexpr_read_needs_frame,
3978   locexpr_describe_location,
3979   locexpr_tracepoint_var_ref
3980 };
3981
3982
3983 /* Wrapper functions for location lists.  These generally find
3984    the appropriate location expression and call something above.  */
3985
3986 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3987    evaluator to calculate the location.  */
3988 static struct value *
3989 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
3990 {
3991   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3992   struct value *val;
3993   const gdb_byte *data;
3994   size_t size;
3995   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
3996
3997   data = dwarf2_find_location_expression (dlbaton, &size, pc);
3998   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
3999                                   dlbaton->per_cu);
4000
4001   return val;
4002 }
4003
4004 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4005    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4006    will be thrown.
4007
4008    Function always returns non-NULL value, it may be marked optimized out if
4009    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
4010    if it cannot resolve the parameter for any reason.  */
4011
4012 static struct value *
4013 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4014 {
4015   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4016   const gdb_byte *data;
4017   size_t size;
4018   CORE_ADDR pc;
4019
4020   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4021     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4022
4023   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4024   if (data == NULL)
4025     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4026
4027   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4028 }
4029
4030 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
4031 static int
4032 loclist_read_needs_frame (struct symbol *symbol)
4033 {
4034   /* If there's a location list, then assume we need to have a frame
4035      to choose the appropriate location expression.  With tracking of
4036      global variables this is not necessarily true, but such tracking
4037      is disabled in GCC at the moment until we figure out how to
4038      represent it.  */
4039
4040   return 1;
4041 }
4042
4043 /* Print a natural-language description of SYMBOL to STREAM.  This
4044    version applies when there is a list of different locations, each
4045    with a specified address range.  */
4046
4047 static void
4048 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4049                            struct ui_file *stream)
4050 {
4051   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4052   const gdb_byte *loc_ptr, *buf_end;
4053   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4054   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4055   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4056   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4057   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4058   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4059   /* Adjust base_address for relocatable objects.  */
4060   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4061   CORE_ADDR base_address = dlbaton->base_address + base_offset;
4062   int done = 0;
4063
4064   loc_ptr = dlbaton->data;
4065   buf_end = dlbaton->data + dlbaton->size;
4066
4067   fprintf_filtered (stream, _("multi-location:\n"));
4068
4069   /* Iterate through locations until we run out.  */
4070   while (!done)
4071     {
4072       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4073       int length;
4074       enum debug_loc_kind kind;
4075       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4076
4077       if (dlbaton->from_dwo)
4078         kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4079                                                loc_ptr, buf_end, &new_ptr,
4080                                                &low, &high, byte_order);
4081       else
4082         kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4083                                            &low, &high,
4084                                            byte_order, addr_size,
4085                                            signed_addr_p);
4086       loc_ptr = new_ptr;
4087       switch (kind)
4088         {
4089         case DEBUG_LOC_END_OF_LIST:
4090           done = 1;
4091           continue;
4092         case DEBUG_LOC_BASE_ADDRESS:
4093           base_address = high + base_offset;
4094           fprintf_filtered (stream, _("  Base address %s"),
4095                             paddress (gdbarch, base_address));
4096           continue;
4097         case DEBUG_LOC_START_END:
4098         case DEBUG_LOC_START_LENGTH:
4099           break;
4100         case DEBUG_LOC_BUFFER_OVERFLOW:
4101         case DEBUG_LOC_INVALID_ENTRY:
4102           error (_("Corrupted DWARF expression for symbol \"%s\"."),
4103                  SYMBOL_PRINT_NAME (symbol));
4104         default:
4105           gdb_assert_not_reached ("bad debug_loc_kind");
4106         }
4107
4108       /* Otherwise, a location expression entry.  */
4109       low += base_address;
4110       high += base_address;
4111
4112       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4113       loc_ptr += 2;
4114
4115       /* (It would improve readability to print only the minimum
4116          necessary digits of the second number of the range.)  */
4117       fprintf_filtered (stream, _("  Range %s-%s: "),
4118                         paddress (gdbarch, low), paddress (gdbarch, high));
4119
4120       /* Now describe this particular location.  */
4121       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4122                                    objfile, addr_size, offset_size,
4123                                    dlbaton->per_cu);
4124
4125       fprintf_filtered (stream, "\n");
4126
4127       loc_ptr += length;
4128     }
4129 }
4130
4131 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4132    any necessary bytecode in AX.  */
4133 static void
4134 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4135                             struct agent_expr *ax, struct axs_value *value)
4136 {
4137   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4138   const gdb_byte *data;
4139   size_t size;
4140   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4141
4142   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4143   if (size == 0)
4144     value->optimized_out = 1;
4145   else
4146     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4147                                dlbaton->per_cu);
4148 }
4149
4150 /* The set of location functions used with the DWARF-2 expression
4151    evaluator and location lists.  */
4152 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4153   loclist_read_variable,
4154   loclist_read_variable_at_entry,
4155   loclist_read_needs_frame,
4156   loclist_describe_location,
4157   loclist_tracepoint_var_ref
4158 };
4159
4160 /* Provide a prototype to silence -Wmissing-prototypes.  */
4161 extern initialize_file_ftype _initialize_dwarf2loc;
4162
4163 void
4164 _initialize_dwarf2loc (void)
4165 {
4166   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4167                              &entry_values_debug,
4168                              _("Set entry values and tail call frames "
4169                                "debugging."),
4170                              _("Show entry values and tail call frames "
4171                                "debugging."),
4172                              _("When non-zero, the process of determining "
4173                                "parameter values from function entry point "
4174                                "and tail call frames will be printed."),
4175                              NULL,
4176                              show_entry_values_debug,
4177                              &setdebuglist, &showdebuglist);
4178 }