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