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