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