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