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