9fd8df04abcb8836ed6cf736fdc58361d73e2773
[external/binutils.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3    Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "value.h"
26 #include "frame.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "inferior.h"
30 #include "ax.h"
31 #include "ax-gdb.h"
32 #include "regcache.h"
33 #include "objfiles.h"
34 #include "exceptions.h"
35 #include "block.h"
36
37 #include "dwarf2.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
41
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44
45 extern int dwarf2_always_disassemble;
46
47 static void
48 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
49                          const gdb_byte **start, size_t *length);
50
51 static struct value *
52 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
53                                const gdb_byte *data, unsigned short size,
54                                struct dwarf2_per_cu_data *per_cu,
55                                LONGEST byte_offset);
56
57 /* A function for dealing with location lists.  Given a
58    symbol baton (BATON) and a pc value (PC), find the appropriate
59    location expression, set *LOCEXPR_LENGTH, and return a pointer
60    to the beginning of the expression.  Returns NULL on failure.
61
62    For now, only return the first matching location expression; there
63    can be more than one in the list.  */
64
65 const gdb_byte *
66 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
67                                  size_t *locexpr_length, CORE_ADDR pc)
68 {
69   CORE_ADDR low, high;
70   const gdb_byte *loc_ptr, *buf_end;
71   int length;
72   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
73   struct gdbarch *gdbarch = get_objfile_arch (objfile);
74   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
75   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
76   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
77   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
78   /* Adjust base_address for relocatable objects.  */
79   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
80   CORE_ADDR base_address = baton->base_address + base_offset;
81
82   loc_ptr = baton->data;
83   buf_end = baton->data + baton->size;
84
85   while (1)
86     {
87       if (buf_end - loc_ptr < 2 * addr_size)
88         error (_("dwarf2_find_location_expression: "
89                  "Corrupted DWARF expression."));
90
91       if (signed_addr_p)
92         low = extract_signed_integer (loc_ptr, addr_size, byte_order);
93       else
94         low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
95       loc_ptr += addr_size;
96
97       if (signed_addr_p)
98         high = extract_signed_integer (loc_ptr, addr_size, byte_order);
99       else
100         high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
101       loc_ptr += addr_size;
102
103       /* A base-address-selection entry.  */
104       if ((low & base_mask) == base_mask)
105         {
106           base_address = high + base_offset;
107           continue;
108         }
109
110       /* An end-of-list entry.  */
111       if (low == 0 && high == 0)
112         return NULL;
113
114       /* Otherwise, a location expression entry.  */
115       low += base_address;
116       high += base_address;
117
118       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
119       loc_ptr += 2;
120
121       if (pc >= low && pc < high)
122         {
123           *locexpr_length = length;
124           return loc_ptr;
125         }
126
127       loc_ptr += length;
128     }
129 }
130
131 /* This is the baton used when performing dwarf2 expression
132    evaluation.  */
133 struct dwarf_expr_baton
134 {
135   struct frame_info *frame;
136   struct dwarf2_per_cu_data *per_cu;
137 };
138
139 /* Helper functions for dwarf2_evaluate_loc_desc.  */
140
141 /* Using the frame specified in BATON, return the value of register
142    REGNUM, treated as a pointer.  */
143 static CORE_ADDR
144 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
145 {
146   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
147   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
148   CORE_ADDR result;
149   int regnum;
150
151   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
152   result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
153                                   regnum, debaton->frame);
154   return result;
155 }
156
157 /* Read memory at ADDR (length LEN) into BUF.  */
158
159 static void
160 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
161 {
162   read_memory (addr, buf, len);
163 }
164
165 /* Using the frame specified in BATON, find the location expression
166    describing the frame base.  Return a pointer to it in START and
167    its length in LENGTH.  */
168 static void
169 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
170 {
171   /* FIXME: cagney/2003-03-26: This code should be using
172      get_frame_base_address(), and then implement a dwarf2 specific
173      this_base method.  */
174   struct symbol *framefunc;
175   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
176
177   /* Use block_linkage_function, which returns a real (not inlined)
178      function, instead of get_frame_function, which may return an
179      inlined function.  */
180   framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
181
182   /* If we found a frame-relative symbol then it was certainly within
183      some function associated with a frame. If we can't find the frame,
184      something has gone wrong.  */
185   gdb_assert (framefunc != NULL);
186
187   dwarf_expr_frame_base_1 (framefunc,
188                            get_frame_address_in_block (debaton->frame),
189                            start, length);
190 }
191
192 static void
193 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
194                          const gdb_byte **start, size_t *length)
195 {
196   if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
197     *start = NULL;
198   else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
199     {
200       struct dwarf2_loclist_baton *symbaton;
201
202       symbaton = SYMBOL_LOCATION_BATON (framefunc);
203       *start = dwarf2_find_location_expression (symbaton, length, pc);
204     }
205   else
206     {
207       struct dwarf2_locexpr_baton *symbaton;
208
209       symbaton = SYMBOL_LOCATION_BATON (framefunc);
210       if (symbaton != NULL)
211         {
212           *length = symbaton->size;
213           *start = symbaton->data;
214         }
215       else
216         *start = NULL;
217     }
218
219   if (*start == NULL)
220     error (_("Could not find the frame base for \"%s\"."),
221            SYMBOL_NATURAL_NAME (framefunc));
222 }
223
224 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
225    the frame in BATON.  */
226
227 static CORE_ADDR
228 dwarf_expr_frame_cfa (void *baton)
229 {
230   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
231
232   return dwarf2_frame_cfa (debaton->frame);
233 }
234
235 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the PC for
236    the frame in BATON.  */
237
238 static CORE_ADDR
239 dwarf_expr_frame_pc (void *baton)
240 {
241   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
242
243   return get_frame_address_in_block (debaton->frame);
244 }
245
246 /* Using the objfile specified in BATON, find the address for the
247    current thread's thread-local storage with offset OFFSET.  */
248 static CORE_ADDR
249 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
250 {
251   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
252   struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
253
254   return target_translate_tls_address (objfile, offset);
255 }
256
257 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
258    current CU (as is PER_CU).  State of the CTX is not affected by the
259    call and return.  */
260
261 static void
262 per_cu_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset,
263                    struct dwarf2_per_cu_data *per_cu,
264                    CORE_ADDR (*get_frame_pc) (void *baton),
265                    void *baton)
266 {
267   struct dwarf2_locexpr_baton block;
268
269   block = dwarf2_fetch_die_location_block (die_offset, per_cu,
270                                            get_frame_pc, baton);
271
272   /* DW_OP_call_ref is currently not supported.  */
273   gdb_assert (block.per_cu == per_cu);
274
275   dwarf_expr_eval (ctx, block.data, block.size);
276 }
277
278 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc.  */
279
280 static void
281 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
282 {
283   struct dwarf_expr_baton *debaton = ctx->baton;
284
285   per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
286                      ctx->get_frame_pc, ctx->baton);
287 }
288
289 struct piece_closure
290 {
291   /* Reference count.  */
292   int refc;
293
294   /* The CU from which this closure's expression came.  */
295   struct dwarf2_per_cu_data *per_cu;
296
297   /* The number of pieces used to describe this variable.  */
298   int n_pieces;
299
300   /* The target address size, used only for DWARF_VALUE_STACK.  */
301   int addr_size;
302
303   /* The pieces themselves.  */
304   struct dwarf_expr_piece *pieces;
305 };
306
307 /* Allocate a closure for a value formed from separately-described
308    PIECES.  */
309
310 static struct piece_closure *
311 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
312                         int n_pieces, struct dwarf_expr_piece *pieces,
313                         int addr_size)
314 {
315   struct piece_closure *c = XZALLOC (struct piece_closure);
316
317   c->refc = 1;
318   c->per_cu = per_cu;
319   c->n_pieces = n_pieces;
320   c->addr_size = addr_size;
321   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
322
323   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
324
325   return c;
326 }
327
328 /* The lowest-level function to extract bits from a byte buffer.
329    SOURCE is the buffer.  It is updated if we read to the end of a
330    byte.
331    SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
332    updated to reflect the number of bits actually read.
333    NBITS is the number of bits we want to read.  It is updated to
334    reflect the number of bits actually read.  This function may read
335    fewer bits.
336    BITS_BIG_ENDIAN is taken directly from gdbarch.
337    This function returns the extracted bits.  */
338
339 static unsigned int
340 extract_bits_primitive (const gdb_byte **source,
341                         unsigned int *source_offset_bits,
342                         int *nbits, int bits_big_endian)
343 {
344   unsigned int avail, mask, datum;
345
346   gdb_assert (*source_offset_bits < 8);
347
348   avail = 8 - *source_offset_bits;
349   if (avail > *nbits)
350     avail = *nbits;
351
352   mask = (1 << avail) - 1;
353   datum = **source;
354   if (bits_big_endian)
355     datum >>= 8 - (*source_offset_bits + *nbits);
356   else
357     datum >>= *source_offset_bits;
358   datum &= mask;
359
360   *nbits -= avail;
361   *source_offset_bits += avail;
362   if (*source_offset_bits >= 8)
363     {
364       *source_offset_bits -= 8;
365       ++*source;
366     }
367
368   return datum;
369 }
370
371 /* Extract some bits from a source buffer and move forward in the
372    buffer.
373    
374    SOURCE is the source buffer.  It is updated as bytes are read.
375    SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
376    bits are read.
377    NBITS is the number of bits to read.
378    BITS_BIG_ENDIAN is taken directly from gdbarch.
379    
380    This function returns the bits that were read.  */
381
382 static unsigned int
383 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
384               int nbits, int bits_big_endian)
385 {
386   unsigned int datum;
387
388   gdb_assert (nbits > 0 && nbits <= 8);
389
390   datum = extract_bits_primitive (source, source_offset_bits, &nbits,
391                                   bits_big_endian);
392   if (nbits > 0)
393     {
394       unsigned int more;
395
396       more = extract_bits_primitive (source, source_offset_bits, &nbits,
397                                      bits_big_endian);
398       if (bits_big_endian)
399         datum <<= nbits;
400       else
401         more <<= nbits;
402       datum |= more;
403     }
404
405   return datum;
406 }
407
408 /* Write some bits into a buffer and move forward in the buffer.
409    
410    DATUM is the bits to write.  The low-order bits of DATUM are used.
411    DEST is the destination buffer.  It is updated as bytes are
412    written.
413    DEST_OFFSET_BITS is the bit offset in DEST at which writing is
414    done.
415    NBITS is the number of valid bits in DATUM.
416    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
417
418 static void
419 insert_bits (unsigned int datum,
420              gdb_byte *dest, unsigned int dest_offset_bits,
421              int nbits, int bits_big_endian)
422 {
423   unsigned int mask;
424
425   gdb_assert (dest_offset_bits + nbits <= 8);
426
427   mask = (1 << nbits) - 1;
428   if (bits_big_endian)
429     {
430       datum <<= 8 - (dest_offset_bits + nbits);
431       mask <<= 8 - (dest_offset_bits + nbits);
432     }
433   else
434     {
435       datum <<= dest_offset_bits;
436       mask <<= dest_offset_bits;
437     }
438
439   gdb_assert ((datum & ~mask) == 0);
440
441   *dest = (*dest & ~mask) | datum;
442 }
443
444 /* Copy bits from a source to a destination.
445    
446    DEST is where the bits should be written.
447    DEST_OFFSET_BITS is the bit offset into DEST.
448    SOURCE is the source of bits.
449    SOURCE_OFFSET_BITS is the bit offset into SOURCE.
450    BIT_COUNT is the number of bits to copy.
451    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
452
453 static void
454 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
455               const gdb_byte *source, unsigned int source_offset_bits,
456               unsigned int bit_count,
457               int bits_big_endian)
458 {
459   unsigned int dest_avail;
460   int datum;
461
462   /* Reduce everything to byte-size pieces.  */
463   dest += dest_offset_bits / 8;
464   dest_offset_bits %= 8;
465   source += source_offset_bits / 8;
466   source_offset_bits %= 8;
467
468   dest_avail = 8 - dest_offset_bits % 8;
469
470   /* See if we can fill the first destination byte.  */
471   if (dest_avail < bit_count)
472     {
473       datum = extract_bits (&source, &source_offset_bits, dest_avail,
474                             bits_big_endian);
475       insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
476       ++dest;
477       dest_offset_bits = 0;
478       bit_count -= dest_avail;
479     }
480
481   /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
482      than 8 bits remaining.  */
483   gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
484   for (; bit_count >= 8; bit_count -= 8)
485     {
486       datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
487       *dest++ = (gdb_byte) datum;
488     }
489
490   /* Finally, we may have a few leftover bits.  */
491   gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
492   if (bit_count > 0)
493     {
494       datum = extract_bits (&source, &source_offset_bits, bit_count,
495                             bits_big_endian);
496       insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
497     }
498 }
499
500 static void
501 read_pieced_value (struct value *v)
502 {
503   int i;
504   long offset = 0;
505   ULONGEST bits_to_skip;
506   gdb_byte *contents;
507   struct piece_closure *c
508     = (struct piece_closure *) value_computed_closure (v);
509   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
510   size_t type_len;
511   size_t buffer_size = 0;
512   char *buffer = NULL;
513   struct cleanup *cleanup;
514   int bits_big_endian
515     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
516
517   if (value_type (v) != value_enclosing_type (v))
518     internal_error (__FILE__, __LINE__,
519                     _("Should not be able to create a lazy value with "
520                       "an enclosing type"));
521
522   cleanup = make_cleanup (free_current_contents, &buffer);
523
524   contents = value_contents_raw (v);
525   bits_to_skip = 8 * value_offset (v);
526   if (value_bitsize (v))
527     {
528       bits_to_skip += value_bitpos (v);
529       type_len = value_bitsize (v);
530     }
531   else
532     type_len = 8 * TYPE_LENGTH (value_type (v));
533
534   for (i = 0; i < c->n_pieces && offset < type_len; i++)
535     {
536       struct dwarf_expr_piece *p = &c->pieces[i];
537       size_t this_size, this_size_bits;
538       long dest_offset_bits, source_offset_bits, source_offset;
539       const gdb_byte *intermediate_buffer;
540
541       /* Compute size, source, and destination offsets for copying, in
542          bits.  */
543       this_size_bits = p->size;
544       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
545         {
546           bits_to_skip -= this_size_bits;
547           continue;
548         }
549       if (this_size_bits > type_len - offset)
550         this_size_bits = type_len - offset;
551       if (bits_to_skip > 0)
552         {
553           dest_offset_bits = 0;
554           source_offset_bits = bits_to_skip;
555           this_size_bits -= bits_to_skip;
556           bits_to_skip = 0;
557         }
558       else
559         {
560           dest_offset_bits = offset;
561           source_offset_bits = 0;
562         }
563
564       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
565       source_offset = source_offset_bits / 8;
566       if (buffer_size < this_size)
567         {
568           buffer_size = this_size;
569           buffer = xrealloc (buffer, buffer_size);
570         }
571       intermediate_buffer = buffer;
572
573       /* Copy from the source to DEST_BUFFER.  */
574       switch (p->location)
575         {
576         case DWARF_VALUE_REGISTER:
577           {
578             struct gdbarch *arch = get_frame_arch (frame);
579             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.value);
580             int reg_offset = source_offset;
581
582             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
583                 && this_size < register_size (arch, gdb_regnum))
584               {
585                 /* Big-endian, and we want less than full size.  */
586                 reg_offset = register_size (arch, gdb_regnum) - this_size;
587                 /* We want the lower-order THIS_SIZE_BITS of the bytes
588                    we extract from the register.  */
589                 source_offset_bits += 8 * this_size - this_size_bits;
590               }
591
592             if (gdb_regnum != -1)
593               {
594                 int optim, unavail;
595
596                 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
597                                                this_size, buffer,
598                                                &optim, &unavail))
599                   {
600                     /* Just so garbage doesn't ever shine through.  */
601                     memset (buffer, 0, this_size);
602
603                     if (optim)
604                       set_value_optimized_out (v, 1);
605                     if (unavail)
606                       mark_value_bytes_unavailable (v, offset, this_size);
607                   }
608               }
609             else
610               {
611                 error (_("Unable to access DWARF register number %s"),
612                        paddress (arch, p->v.value));
613               }
614           }
615           break;
616
617         case DWARF_VALUE_MEMORY:
618           read_value_memory (v, offset,
619                              p->v.mem.in_stack_memory,
620                              p->v.mem.addr + source_offset,
621                              buffer, this_size);
622           break;
623
624         case DWARF_VALUE_STACK:
625           {
626             struct gdbarch *gdbarch = get_type_arch (value_type (v));
627             size_t n = this_size;
628
629             if (n > c->addr_size - source_offset)
630               n = (c->addr_size >= source_offset
631                    ? c->addr_size - source_offset
632                    : 0);
633             if (n == 0)
634               {
635                 /* Nothing.  */
636               }
637             else if (source_offset == 0)
638               store_unsigned_integer (buffer, n,
639                                       gdbarch_byte_order (gdbarch),
640                                       p->v.value);
641             else
642               {
643                 gdb_byte bytes[sizeof (ULONGEST)];
644
645                 store_unsigned_integer (bytes, n + source_offset,
646                                         gdbarch_byte_order (gdbarch),
647                                         p->v.value);
648                 memcpy (buffer, bytes + source_offset, n);
649               }
650           }
651           break;
652
653         case DWARF_VALUE_LITERAL:
654           {
655             size_t n = this_size;
656
657             if (n > p->v.literal.length - source_offset)
658               n = (p->v.literal.length >= source_offset
659                    ? p->v.literal.length - source_offset
660                    : 0);
661             if (n != 0)
662               intermediate_buffer = p->v.literal.data + source_offset;
663           }
664           break;
665
666           /* These bits show up as zeros -- but do not cause the value
667              to be considered optimized-out.  */
668         case DWARF_VALUE_IMPLICIT_POINTER:
669           break;
670
671         case DWARF_VALUE_OPTIMIZED_OUT:
672           set_value_optimized_out (v, 1);
673           break;
674
675         default:
676           internal_error (__FILE__, __LINE__, _("invalid location type"));
677         }
678
679       if (p->location != DWARF_VALUE_OPTIMIZED_OUT
680           && p->location != DWARF_VALUE_IMPLICIT_POINTER)
681         copy_bitwise (contents, dest_offset_bits,
682                       intermediate_buffer, source_offset_bits % 8,
683                       this_size_bits, bits_big_endian);
684
685       offset += this_size_bits;
686     }
687
688   do_cleanups (cleanup);
689 }
690
691 static void
692 write_pieced_value (struct value *to, struct value *from)
693 {
694   int i;
695   long offset = 0;
696   ULONGEST bits_to_skip;
697   const gdb_byte *contents;
698   struct piece_closure *c
699     = (struct piece_closure *) value_computed_closure (to);
700   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
701   size_t type_len;
702   size_t buffer_size = 0;
703   char *buffer = NULL;
704   struct cleanup *cleanup;
705   int bits_big_endian
706     = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
707
708   if (frame == NULL)
709     {
710       set_value_optimized_out (to, 1);
711       return;
712     }
713
714   cleanup = make_cleanup (free_current_contents, &buffer);
715
716   contents = value_contents (from);
717   bits_to_skip = 8 * value_offset (to);
718   if (value_bitsize (to))
719     {
720       bits_to_skip += value_bitpos (to);
721       type_len = value_bitsize (to);
722     }
723   else
724     type_len = 8 * TYPE_LENGTH (value_type (to));
725
726   for (i = 0; i < c->n_pieces && offset < type_len; i++)
727     {
728       struct dwarf_expr_piece *p = &c->pieces[i];
729       size_t this_size_bits, this_size;
730       long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
731       int need_bitwise;
732       const gdb_byte *source_buffer;
733
734       this_size_bits = p->size;
735       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
736         {
737           bits_to_skip -= this_size_bits;
738           continue;
739         }
740       if (this_size_bits > type_len - offset)
741         this_size_bits = type_len - offset;
742       if (bits_to_skip > 0)
743         {
744           dest_offset_bits = bits_to_skip;
745           source_offset_bits = 0;
746           this_size_bits -= bits_to_skip;
747           bits_to_skip = 0;
748         }
749       else
750         {
751           dest_offset_bits = 0;
752           source_offset_bits = offset;
753         }
754
755       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
756       source_offset = source_offset_bits / 8;
757       dest_offset = dest_offset_bits / 8;
758       if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
759         {
760           source_buffer = contents + source_offset;
761           need_bitwise = 0;
762         }
763       else
764         {
765           if (buffer_size < this_size)
766             {
767               buffer_size = this_size;
768               buffer = xrealloc (buffer, buffer_size);
769             }
770           source_buffer = buffer;
771           need_bitwise = 1;
772         }
773
774       switch (p->location)
775         {
776         case DWARF_VALUE_REGISTER:
777           {
778             struct gdbarch *arch = get_frame_arch (frame);
779             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.value);
780             int reg_offset = dest_offset;
781
782             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
783                 && this_size <= register_size (arch, gdb_regnum))
784               /* Big-endian, and we want less than full size.  */
785               reg_offset = register_size (arch, gdb_regnum) - this_size;
786
787             if (gdb_regnum != -1)
788               {
789                 if (need_bitwise)
790                   {
791                     int optim, unavail;
792
793                     if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
794                                                    this_size, buffer,
795                                                    &optim, &unavail))
796                       {
797                         if (optim)
798                           error (_("Can't do read-modify-write to "
799                                    "update bitfield; containing word has been "
800                                    "optimized out"));
801                         if (unavail)
802                           throw_error (NOT_AVAILABLE_ERROR,
803                                        _("Can't do read-modify-write to update "
804                                          "bitfield; containing word "
805                                          "is unavailable"));
806                       }
807                     copy_bitwise (buffer, dest_offset_bits,
808                                   contents, source_offset_bits,
809                                   this_size_bits,
810                                   bits_big_endian);
811                   }
812
813                 put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
814                                           this_size, source_buffer);
815               }
816             else
817               {
818                 error (_("Unable to write to DWARF register number %s"),
819                        paddress (arch, p->v.value));
820               }
821           }
822           break;
823         case DWARF_VALUE_MEMORY:
824           if (need_bitwise)
825             {
826               /* Only the first and last bytes can possibly have any
827                  bits reused.  */
828               read_memory (p->v.mem.addr + dest_offset, buffer, 1);
829               read_memory (p->v.mem.addr + dest_offset + this_size - 1,
830                            buffer + this_size - 1, 1);
831               copy_bitwise (buffer, dest_offset_bits,
832                             contents, source_offset_bits,
833                             this_size_bits,
834                             bits_big_endian);
835             }
836
837           write_memory (p->v.mem.addr + dest_offset,
838                         source_buffer, this_size);
839           break;
840         default:
841           set_value_optimized_out (to, 1);
842           break;
843         }
844       offset += this_size_bits;
845     }
846
847   do_cleanups (cleanup);
848 }
849
850 /* A helper function that checks bit validity in a pieced value.
851    CHECK_FOR indicates the kind of validity checking.
852    DWARF_VALUE_MEMORY means to check whether any bit is valid.
853    DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
854    optimized out.
855    DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
856    implicit pointer.  */
857
858 static int
859 check_pieced_value_bits (const struct value *value, int bit_offset,
860                          int bit_length,
861                          enum dwarf_value_location check_for)
862 {
863   struct piece_closure *c
864     = (struct piece_closure *) value_computed_closure (value);
865   int i;
866   int validity = (check_for == DWARF_VALUE_MEMORY
867                   || check_for == DWARF_VALUE_IMPLICIT_POINTER);
868
869   bit_offset += 8 * value_offset (value);
870   if (value_bitsize (value))
871     bit_offset += value_bitpos (value);
872
873   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
874     {
875       struct dwarf_expr_piece *p = &c->pieces[i];
876       size_t this_size_bits = p->size;
877
878       if (bit_offset > 0)
879         {
880           if (bit_offset >= this_size_bits)
881             {
882               bit_offset -= this_size_bits;
883               continue;
884             }
885
886           bit_length -= this_size_bits - bit_offset;
887           bit_offset = 0;
888         }
889       else
890         bit_length -= this_size_bits;
891
892       if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
893         {
894           if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
895             return 0;
896         }
897       else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
898                || p->location == DWARF_VALUE_IMPLICIT_POINTER)
899         {
900           if (validity)
901             return 0;
902         }
903       else
904         {
905           if (!validity)
906             return 1;
907         }
908     }
909
910   return validity;
911 }
912
913 static int
914 check_pieced_value_validity (const struct value *value, int bit_offset,
915                              int bit_length)
916 {
917   return check_pieced_value_bits (value, bit_offset, bit_length,
918                                   DWARF_VALUE_MEMORY);
919 }
920
921 static int
922 check_pieced_value_invalid (const struct value *value)
923 {
924   return check_pieced_value_bits (value, 0,
925                                   8 * TYPE_LENGTH (value_type (value)),
926                                   DWARF_VALUE_OPTIMIZED_OUT);
927 }
928
929 /* An implementation of an lval_funcs method to see whether a value is
930    a synthetic pointer.  */
931
932 static int
933 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
934                                 int bit_length)
935 {
936   return check_pieced_value_bits (value, bit_offset, bit_length,
937                                   DWARF_VALUE_IMPLICIT_POINTER);
938 }
939
940 /* A wrapper function for get_frame_address_in_block.  */
941
942 static CORE_ADDR
943 get_frame_address_in_block_wrapper (void *baton)
944 {
945   return get_frame_address_in_block (baton);
946 }
947
948 /* An implementation of an lval_funcs method to indirect through a
949    pointer.  This handles the synthetic pointer case when needed.  */
950
951 static struct value *
952 indirect_pieced_value (struct value *value)
953 {
954   struct piece_closure *c
955     = (struct piece_closure *) value_computed_closure (value);
956   struct type *type;
957   struct frame_info *frame;
958   struct dwarf2_locexpr_baton baton;
959   int i, bit_offset, bit_length;
960   struct dwarf_expr_piece *piece = NULL;
961   struct value *result;
962   LONGEST byte_offset;
963
964   type = value_type (value);
965   if (TYPE_CODE (type) != TYPE_CODE_PTR)
966     return NULL;
967
968   bit_length = 8 * TYPE_LENGTH (type);
969   bit_offset = 8 * value_offset (value);
970   if (value_bitsize (value))
971     bit_offset += value_bitpos (value);
972
973   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
974     {
975       struct dwarf_expr_piece *p = &c->pieces[i];
976       size_t this_size_bits = p->size;
977
978       if (bit_offset > 0)
979         {
980           if (bit_offset >= this_size_bits)
981             {
982               bit_offset -= this_size_bits;
983               continue;
984             }
985
986           bit_length -= this_size_bits - bit_offset;
987           bit_offset = 0;
988         }
989       else
990         bit_length -= this_size_bits;
991
992       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
993         return NULL;
994
995       if (bit_length != 0)
996         error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
997
998       piece = p;
999       break;
1000     }
1001
1002   frame = get_selected_frame (_("No frame selected."));
1003   byte_offset = value_as_address (value);
1004
1005   gdb_assert (piece);
1006   baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu,
1007                                            get_frame_address_in_block_wrapper,
1008                                            frame);
1009
1010   result = dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
1011                                           baton.data, baton.size, baton.per_cu,
1012                                           byte_offset);
1013
1014   return result;
1015 }
1016
1017 static void *
1018 copy_pieced_value_closure (const struct value *v)
1019 {
1020   struct piece_closure *c
1021     = (struct piece_closure *) value_computed_closure (v);
1022   
1023   ++c->refc;
1024   return c;
1025 }
1026
1027 static void
1028 free_pieced_value_closure (struct value *v)
1029 {
1030   struct piece_closure *c
1031     = (struct piece_closure *) value_computed_closure (v);
1032
1033   --c->refc;
1034   if (c->refc == 0)
1035     {
1036       xfree (c->pieces);
1037       xfree (c);
1038     }
1039 }
1040
1041 /* Functions for accessing a variable described by DW_OP_piece.  */
1042 static struct lval_funcs pieced_value_funcs = {
1043   read_pieced_value,
1044   write_pieced_value,
1045   check_pieced_value_validity,
1046   check_pieced_value_invalid,
1047   indirect_pieced_value,
1048   check_pieced_synthetic_pointer,
1049   copy_pieced_value_closure,
1050   free_pieced_value_closure
1051 };
1052
1053 /* Helper function which throws an error if a synthetic pointer is
1054    invalid.  */
1055
1056 static void
1057 invalid_synthetic_pointer (void)
1058 {
1059   error (_("access outside bounds of object "
1060            "referenced via synthetic pointer"));
1061 }
1062
1063 /* Evaluate a location description, starting at DATA and with length
1064    SIZE, to find the current location of variable of TYPE in the
1065    context of FRAME.  BYTE_OFFSET is applied after the contents are
1066    computed.  */
1067
1068 static struct value *
1069 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1070                                const gdb_byte *data, unsigned short size,
1071                                struct dwarf2_per_cu_data *per_cu,
1072                                LONGEST byte_offset)
1073 {
1074   struct value *retval;
1075   struct dwarf_expr_baton baton;
1076   struct dwarf_expr_context *ctx;
1077   struct cleanup *old_chain;
1078   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1079   volatile struct gdb_exception ex;
1080
1081   if (byte_offset < 0)
1082     invalid_synthetic_pointer ();
1083
1084   if (size == 0)
1085     {
1086       retval = allocate_value (type);
1087       VALUE_LVAL (retval) = not_lval;
1088       set_value_optimized_out (retval, 1);
1089       return retval;
1090     }
1091
1092   baton.frame = frame;
1093   baton.per_cu = per_cu;
1094
1095   ctx = new_dwarf_expr_context ();
1096   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1097
1098   ctx->gdbarch = get_objfile_arch (objfile);
1099   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1100   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1101   ctx->baton = &baton;
1102   ctx->read_reg = dwarf_expr_read_reg;
1103   ctx->read_mem = dwarf_expr_read_mem;
1104   ctx->get_frame_base = dwarf_expr_frame_base;
1105   ctx->get_frame_cfa = dwarf_expr_frame_cfa;
1106   ctx->get_frame_pc = dwarf_expr_frame_pc;
1107   ctx->get_tls_address = dwarf_expr_tls_address;
1108   ctx->dwarf_call = dwarf_expr_dwarf_call;
1109
1110   TRY_CATCH (ex, RETURN_MASK_ERROR)
1111     {
1112       dwarf_expr_eval (ctx, data, size);
1113     }
1114   if (ex.reason < 0)
1115     {
1116       if (ex.error == NOT_AVAILABLE_ERROR)
1117         {
1118           retval = allocate_value (type);
1119           mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
1120           return retval;
1121         }
1122       else
1123         throw_exception (ex);
1124     }
1125
1126   if (ctx->num_pieces > 0)
1127     {
1128       struct piece_closure *c;
1129       struct frame_id frame_id = get_frame_id (frame);
1130       ULONGEST bit_size = 0;
1131       int i;
1132
1133       for (i = 0; i < ctx->num_pieces; ++i)
1134         bit_size += ctx->pieces[i].size;
1135       if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
1136         invalid_synthetic_pointer ();
1137
1138       c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
1139                                   ctx->addr_size);
1140       retval = allocate_computed_value (type, &pieced_value_funcs, c);
1141       VALUE_FRAME_ID (retval) = frame_id;
1142       set_value_offset (retval, byte_offset);
1143     }
1144   else
1145     {
1146       switch (ctx->location)
1147         {
1148         case DWARF_VALUE_REGISTER:
1149           {
1150             struct gdbarch *arch = get_frame_arch (frame);
1151             ULONGEST dwarf_regnum = dwarf_expr_fetch (ctx, 0);
1152             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
1153
1154             if (byte_offset != 0)
1155               error (_("cannot use offset on synthetic pointer to register"));
1156             if (gdb_regnum != -1)
1157               retval = value_from_register (type, gdb_regnum, frame);
1158             else
1159               error (_("Unable to access DWARF register number %s"),
1160                      paddress (arch, dwarf_regnum));
1161           }
1162           break;
1163
1164         case DWARF_VALUE_MEMORY:
1165           {
1166             CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
1167             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
1168
1169             retval = allocate_value_lazy (type);
1170             VALUE_LVAL (retval) = lval_memory;
1171             if (in_stack_memory)
1172               set_value_stack (retval, 1);
1173             set_value_address (retval, address + byte_offset);
1174           }
1175           break;
1176
1177         case DWARF_VALUE_STACK:
1178           {
1179             ULONGEST value = dwarf_expr_fetch (ctx, 0);
1180             bfd_byte *contents, *tem;
1181             size_t n = ctx->addr_size;
1182
1183             if (byte_offset + TYPE_LENGTH (type) > n)
1184               invalid_synthetic_pointer ();
1185
1186             tem = alloca (n);
1187             store_unsigned_integer (tem, n,
1188                                     gdbarch_byte_order (ctx->gdbarch),
1189                                     value);
1190
1191             tem += byte_offset;
1192             n -= byte_offset;
1193
1194             retval = allocate_value (type);
1195             contents = value_contents_raw (retval);
1196             if (n > TYPE_LENGTH (type))
1197               n = TYPE_LENGTH (type);
1198             memcpy (contents, tem, n);
1199           }
1200           break;
1201
1202         case DWARF_VALUE_LITERAL:
1203           {
1204             bfd_byte *contents;
1205             const bfd_byte *ldata;
1206             size_t n = ctx->len;
1207
1208             if (byte_offset + TYPE_LENGTH (type) > n)
1209               invalid_synthetic_pointer ();
1210
1211             retval = allocate_value (type);
1212             contents = value_contents_raw (retval);
1213
1214             ldata = ctx->data + byte_offset;
1215             n -= byte_offset;
1216
1217             if (n > TYPE_LENGTH (type))
1218               n = TYPE_LENGTH (type);
1219             memcpy (contents, ldata, n);
1220           }
1221           break;
1222
1223           /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
1224              operation by execute_stack_op.  */
1225         case DWARF_VALUE_IMPLICIT_POINTER:
1226           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
1227              it can only be encountered when making a piece.  */
1228         case DWARF_VALUE_OPTIMIZED_OUT:
1229         default:
1230           internal_error (__FILE__, __LINE__, _("invalid location type"));
1231         }
1232     }
1233
1234   set_value_initialized (retval, ctx->initialized);
1235
1236   do_cleanups (old_chain);
1237
1238   return retval;
1239 }
1240
1241 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
1242    passes 0 as the byte_offset.  */
1243
1244 struct value *
1245 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
1246                           const gdb_byte *data, unsigned short size,
1247                           struct dwarf2_per_cu_data *per_cu)
1248 {
1249   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
1250 }
1251
1252 \f
1253 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
1254
1255 struct needs_frame_baton
1256 {
1257   int needs_frame;
1258   struct dwarf2_per_cu_data *per_cu;
1259 };
1260
1261 /* Reads from registers do require a frame.  */
1262 static CORE_ADDR
1263 needs_frame_read_reg (void *baton, int regnum)
1264 {
1265   struct needs_frame_baton *nf_baton = baton;
1266
1267   nf_baton->needs_frame = 1;
1268   return 1;
1269 }
1270
1271 /* Reads from memory do not require a frame.  */
1272 static void
1273 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
1274 {
1275   memset (buf, 0, len);
1276 }
1277
1278 /* Frame-relative accesses do require a frame.  */
1279 static void
1280 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
1281 {
1282   static gdb_byte lit0 = DW_OP_lit0;
1283   struct needs_frame_baton *nf_baton = baton;
1284
1285   *start = &lit0;
1286   *length = 1;
1287
1288   nf_baton->needs_frame = 1;
1289 }
1290
1291 /* CFA accesses require a frame.  */
1292
1293 static CORE_ADDR
1294 needs_frame_frame_cfa (void *baton)
1295 {
1296   struct needs_frame_baton *nf_baton = baton;
1297
1298   nf_baton->needs_frame = 1;
1299   return 1;
1300 }
1301
1302 /* Thread-local accesses do require a frame.  */
1303 static CORE_ADDR
1304 needs_frame_tls_address (void *baton, CORE_ADDR offset)
1305 {
1306   struct needs_frame_baton *nf_baton = baton;
1307
1308   nf_baton->needs_frame = 1;
1309   return 1;
1310 }
1311
1312 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame.  */
1313
1314 static void
1315 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
1316 {
1317   struct needs_frame_baton *nf_baton = ctx->baton;
1318
1319   per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
1320                      ctx->get_frame_pc, ctx->baton);
1321 }
1322
1323 /* Return non-zero iff the location expression at DATA (length SIZE)
1324    requires a frame to evaluate.  */
1325
1326 static int
1327 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
1328                              struct dwarf2_per_cu_data *per_cu)
1329 {
1330   struct needs_frame_baton baton;
1331   struct dwarf_expr_context *ctx;
1332   int in_reg;
1333   struct cleanup *old_chain;
1334   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1335
1336   baton.needs_frame = 0;
1337   baton.per_cu = per_cu;
1338
1339   ctx = new_dwarf_expr_context ();
1340   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1341
1342   ctx->gdbarch = get_objfile_arch (objfile);
1343   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1344   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1345   ctx->baton = &baton;
1346   ctx->read_reg = needs_frame_read_reg;
1347   ctx->read_mem = needs_frame_read_mem;
1348   ctx->get_frame_base = needs_frame_frame_base;
1349   ctx->get_frame_cfa = needs_frame_frame_cfa;
1350   ctx->get_frame_pc = needs_frame_frame_cfa;
1351   ctx->get_tls_address = needs_frame_tls_address;
1352   ctx->dwarf_call = needs_frame_dwarf_call;
1353
1354   dwarf_expr_eval (ctx, data, size);
1355
1356   in_reg = ctx->location == DWARF_VALUE_REGISTER;
1357
1358   if (ctx->num_pieces > 0)
1359     {
1360       int i;
1361
1362       /* If the location has several pieces, and any of them are in
1363          registers, then we will need a frame to fetch them from.  */
1364       for (i = 0; i < ctx->num_pieces; i++)
1365         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1366           in_reg = 1;
1367     }
1368
1369   do_cleanups (old_chain);
1370
1371   return baton.needs_frame || in_reg;
1372 }
1373
1374 /* A helper function that throws an unimplemented error mentioning a
1375    given DWARF operator.  */
1376
1377 static void
1378 unimplemented (unsigned int op)
1379 {
1380   const char *name = dwarf_stack_op_name (op);
1381
1382   if (name)
1383     error (_("DWARF operator %s cannot be translated to an agent expression"),
1384            name);
1385   else
1386     error (_("Unknown DWARF operator 0x%02x cannot be translated "
1387              "to an agent expression"),
1388            op);
1389 }
1390
1391 /* A helper function to convert a DWARF register to an arch register.
1392    ARCH is the architecture.
1393    DWARF_REG is the register.
1394    This will throw an exception if the DWARF register cannot be
1395    translated to an architecture register.  */
1396
1397 static int
1398 translate_register (struct gdbarch *arch, int dwarf_reg)
1399 {
1400   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
1401   if (reg == -1)
1402     error (_("Unable to access DWARF register number %d"), dwarf_reg);
1403   return reg;
1404 }
1405
1406 /* A helper function that emits an access to memory.  ARCH is the
1407    target architecture.  EXPR is the expression which we are building.
1408    NBITS is the number of bits we want to read.  This emits the
1409    opcodes needed to read the memory and then extract the desired
1410    bits.  */
1411
1412 static void
1413 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
1414 {
1415   ULONGEST nbytes = (nbits + 7) / 8;
1416
1417   gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
1418
1419   if (trace_kludge)
1420     ax_trace_quick (expr, nbytes);
1421
1422   if (nbits <= 8)
1423     ax_simple (expr, aop_ref8);
1424   else if (nbits <= 16)
1425     ax_simple (expr, aop_ref16);
1426   else if (nbits <= 32)
1427     ax_simple (expr, aop_ref32);
1428   else
1429     ax_simple (expr, aop_ref64);
1430
1431   /* If we read exactly the number of bytes we wanted, we're done.  */
1432   if (8 * nbytes == nbits)
1433     return;
1434
1435   if (gdbarch_bits_big_endian (arch))
1436     {
1437       /* On a bits-big-endian machine, we want the high-order
1438          NBITS.  */
1439       ax_const_l (expr, 8 * nbytes - nbits);
1440       ax_simple (expr, aop_rsh_unsigned);
1441     }
1442   else
1443     {
1444       /* On a bits-little-endian box, we want the low-order NBITS.  */
1445       ax_zero_ext (expr, nbits);
1446     }
1447 }
1448
1449 /* A helper function to return the frame's PC.  */
1450
1451 static CORE_ADDR
1452 get_ax_pc (void *baton)
1453 {
1454   struct agent_expr *expr = baton;
1455
1456   return expr->scope;
1457 }
1458
1459 /* Compile a DWARF location expression to an agent expression.
1460    
1461    EXPR is the agent expression we are building.
1462    LOC is the agent value we modify.
1463    ARCH is the architecture.
1464    ADDR_SIZE is the size of addresses, in bytes.
1465    OP_PTR is the start of the location expression.
1466    OP_END is one past the last byte of the location expression.
1467    
1468    This will throw an exception for various kinds of errors -- for
1469    example, if the expression cannot be compiled, or if the expression
1470    is invalid.  */
1471
1472 void
1473 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
1474                            struct gdbarch *arch, unsigned int addr_size,
1475                            const gdb_byte *op_ptr, const gdb_byte *op_end,
1476                            struct dwarf2_per_cu_data *per_cu)
1477 {
1478   struct cleanup *cleanups;
1479   int i, *offsets;
1480   VEC(int) *dw_labels = NULL, *patches = NULL;
1481   const gdb_byte * const base = op_ptr;
1482   const gdb_byte *previous_piece = op_ptr;
1483   enum bfd_endian byte_order = gdbarch_byte_order (arch);
1484   ULONGEST bits_collected = 0;
1485   unsigned int addr_size_bits = 8 * addr_size;
1486   int bits_big_endian = gdbarch_bits_big_endian (arch);
1487
1488   offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
1489   cleanups = make_cleanup (xfree, offsets);
1490
1491   for (i = 0; i < op_end - op_ptr; ++i)
1492     offsets[i] = -1;
1493
1494   make_cleanup (VEC_cleanup (int), &dw_labels);
1495   make_cleanup (VEC_cleanup (int), &patches);
1496
1497   /* By default we are making an address.  */
1498   loc->kind = axs_lvalue_memory;
1499
1500   while (op_ptr < op_end)
1501     {
1502       enum dwarf_location_atom op = *op_ptr;
1503       ULONGEST uoffset, reg;
1504       LONGEST offset;
1505       int i;
1506
1507       offsets[op_ptr - base] = expr->len;
1508       ++op_ptr;
1509
1510       /* Our basic approach to code generation is to map DWARF
1511          operations directly to AX operations.  However, there are
1512          some differences.
1513
1514          First, DWARF works on address-sized units, but AX always uses
1515          LONGEST.  For most operations we simply ignore this
1516          difference; instead we generate sign extensions as needed
1517          before division and comparison operations.  It would be nice
1518          to omit the sign extensions, but there is no way to determine
1519          the size of the target's LONGEST.  (This code uses the size
1520          of the host LONGEST in some cases -- that is a bug but it is
1521          difficult to fix.)
1522
1523          Second, some DWARF operations cannot be translated to AX.
1524          For these we simply fail.  See
1525          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
1526       switch (op)
1527         {
1528         case DW_OP_lit0:
1529         case DW_OP_lit1:
1530         case DW_OP_lit2:
1531         case DW_OP_lit3:
1532         case DW_OP_lit4:
1533         case DW_OP_lit5:
1534         case DW_OP_lit6:
1535         case DW_OP_lit7:
1536         case DW_OP_lit8:
1537         case DW_OP_lit9:
1538         case DW_OP_lit10:
1539         case DW_OP_lit11:
1540         case DW_OP_lit12:
1541         case DW_OP_lit13:
1542         case DW_OP_lit14:
1543         case DW_OP_lit15:
1544         case DW_OP_lit16:
1545         case DW_OP_lit17:
1546         case DW_OP_lit18:
1547         case DW_OP_lit19:
1548         case DW_OP_lit20:
1549         case DW_OP_lit21:
1550         case DW_OP_lit22:
1551         case DW_OP_lit23:
1552         case DW_OP_lit24:
1553         case DW_OP_lit25:
1554         case DW_OP_lit26:
1555         case DW_OP_lit27:
1556         case DW_OP_lit28:
1557         case DW_OP_lit29:
1558         case DW_OP_lit30:
1559         case DW_OP_lit31:
1560           ax_const_l (expr, op - DW_OP_lit0);
1561           break;
1562
1563         case DW_OP_addr:
1564           uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
1565           op_ptr += addr_size;
1566           /* Some versions of GCC emit DW_OP_addr before
1567              DW_OP_GNU_push_tls_address.  In this case the value is an
1568              index, not an address.  We don't support things like
1569              branching between the address and the TLS op.  */
1570           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
1571             uoffset += dwarf2_per_cu_text_offset (per_cu);
1572           ax_const_l (expr, uoffset);
1573           break;
1574
1575         case DW_OP_const1u:
1576           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
1577           op_ptr += 1;
1578           break;
1579         case DW_OP_const1s:
1580           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
1581           op_ptr += 1;
1582           break;
1583         case DW_OP_const2u:
1584           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
1585           op_ptr += 2;
1586           break;
1587         case DW_OP_const2s:
1588           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
1589           op_ptr += 2;
1590           break;
1591         case DW_OP_const4u:
1592           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
1593           op_ptr += 4;
1594           break;
1595         case DW_OP_const4s:
1596           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
1597           op_ptr += 4;
1598           break;
1599         case DW_OP_const8u:
1600           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
1601           op_ptr += 8;
1602           break;
1603         case DW_OP_const8s:
1604           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
1605           op_ptr += 8;
1606           break;
1607         case DW_OP_constu:
1608           op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
1609           ax_const_l (expr, uoffset);
1610           break;
1611         case DW_OP_consts:
1612           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1613           ax_const_l (expr, offset);
1614           break;
1615
1616         case DW_OP_reg0:
1617         case DW_OP_reg1:
1618         case DW_OP_reg2:
1619         case DW_OP_reg3:
1620         case DW_OP_reg4:
1621         case DW_OP_reg5:
1622         case DW_OP_reg6:
1623         case DW_OP_reg7:
1624         case DW_OP_reg8:
1625         case DW_OP_reg9:
1626         case DW_OP_reg10:
1627         case DW_OP_reg11:
1628         case DW_OP_reg12:
1629         case DW_OP_reg13:
1630         case DW_OP_reg14:
1631         case DW_OP_reg15:
1632         case DW_OP_reg16:
1633         case DW_OP_reg17:
1634         case DW_OP_reg18:
1635         case DW_OP_reg19:
1636         case DW_OP_reg20:
1637         case DW_OP_reg21:
1638         case DW_OP_reg22:
1639         case DW_OP_reg23:
1640         case DW_OP_reg24:
1641         case DW_OP_reg25:
1642         case DW_OP_reg26:
1643         case DW_OP_reg27:
1644         case DW_OP_reg28:
1645         case DW_OP_reg29:
1646         case DW_OP_reg30:
1647         case DW_OP_reg31:
1648           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1649           loc->u.reg = translate_register (arch, op - DW_OP_reg0);
1650           loc->kind = axs_lvalue_register;
1651           break;
1652
1653         case DW_OP_regx:
1654           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1655           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1656           loc->u.reg = translate_register (arch, reg);
1657           loc->kind = axs_lvalue_register;
1658           break;
1659
1660         case DW_OP_implicit_value:
1661           {
1662             ULONGEST len;
1663
1664             op_ptr = read_uleb128 (op_ptr, op_end, &len);
1665             if (op_ptr + len > op_end)
1666               error (_("DW_OP_implicit_value: too few bytes available."));
1667             if (len > sizeof (ULONGEST))
1668               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1669                      (int) len);
1670
1671             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
1672                                                         byte_order));
1673             op_ptr += len;
1674             dwarf_expr_require_composition (op_ptr, op_end,
1675                                             "DW_OP_implicit_value");
1676
1677             loc->kind = axs_rvalue;
1678           }
1679           break;
1680
1681         case DW_OP_stack_value:
1682           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1683           loc->kind = axs_rvalue;
1684           break;
1685
1686         case DW_OP_breg0:
1687         case DW_OP_breg1:
1688         case DW_OP_breg2:
1689         case DW_OP_breg3:
1690         case DW_OP_breg4:
1691         case DW_OP_breg5:
1692         case DW_OP_breg6:
1693         case DW_OP_breg7:
1694         case DW_OP_breg8:
1695         case DW_OP_breg9:
1696         case DW_OP_breg10:
1697         case DW_OP_breg11:
1698         case DW_OP_breg12:
1699         case DW_OP_breg13:
1700         case DW_OP_breg14:
1701         case DW_OP_breg15:
1702         case DW_OP_breg16:
1703         case DW_OP_breg17:
1704         case DW_OP_breg18:
1705         case DW_OP_breg19:
1706         case DW_OP_breg20:
1707         case DW_OP_breg21:
1708         case DW_OP_breg22:
1709         case DW_OP_breg23:
1710         case DW_OP_breg24:
1711         case DW_OP_breg25:
1712         case DW_OP_breg26:
1713         case DW_OP_breg27:
1714         case DW_OP_breg28:
1715         case DW_OP_breg29:
1716         case DW_OP_breg30:
1717         case DW_OP_breg31:
1718           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1719           i = translate_register (arch, op - DW_OP_breg0);
1720           ax_reg (expr, i);
1721           if (offset != 0)
1722             {
1723               ax_const_l (expr, offset);
1724               ax_simple (expr, aop_add);
1725             }
1726           break;
1727         case DW_OP_bregx:
1728           {
1729             op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1730             op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1731             i = translate_register (arch, reg);
1732             ax_reg (expr, i);
1733             if (offset != 0)
1734               {
1735                 ax_const_l (expr, offset);
1736                 ax_simple (expr, aop_add);
1737               }
1738           }
1739           break;
1740         case DW_OP_fbreg:
1741           {
1742             const gdb_byte *datastart;
1743             size_t datalen;
1744             unsigned int before_stack_len;
1745             struct block *b;
1746             struct symbol *framefunc;
1747             LONGEST base_offset = 0;
1748
1749             b = block_for_pc (expr->scope);
1750
1751             if (!b)
1752               error (_("No block found for address"));
1753
1754             framefunc = block_linkage_function (b);
1755
1756             if (!framefunc)
1757               error (_("No function found for block"));
1758
1759             dwarf_expr_frame_base_1 (framefunc, expr->scope,
1760                                      &datastart, &datalen);
1761
1762             op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1763             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
1764                                        datastart + datalen, per_cu);
1765
1766             if (offset != 0)
1767               {
1768                 ax_const_l (expr, offset);
1769                 ax_simple (expr, aop_add);
1770               }
1771
1772             loc->kind = axs_lvalue_memory;
1773           }
1774           break;
1775
1776         case DW_OP_dup:
1777           ax_simple (expr, aop_dup);
1778           break;
1779
1780         case DW_OP_drop:
1781           ax_simple (expr, aop_pop);
1782           break;
1783
1784         case DW_OP_pick:
1785           offset = *op_ptr++;
1786           ax_pick (expr, offset);
1787           break;
1788           
1789         case DW_OP_swap:
1790           ax_simple (expr, aop_swap);
1791           break;
1792
1793         case DW_OP_over:
1794           ax_pick (expr, 1);
1795           break;
1796
1797         case DW_OP_rot:
1798           ax_simple (expr, aop_rot);
1799           break;
1800
1801         case DW_OP_deref:
1802         case DW_OP_deref_size:
1803           {
1804             int size;
1805
1806             if (op == DW_OP_deref_size)
1807               size = *op_ptr++;
1808             else
1809               size = addr_size;
1810
1811             switch (size)
1812               {
1813               case 8:
1814                 ax_simple (expr, aop_ref8);
1815                 break;
1816               case 16:
1817                 ax_simple (expr, aop_ref16);
1818                 break;
1819               case 32:
1820                 ax_simple (expr, aop_ref32);
1821                 break;
1822               case 64:
1823                 ax_simple (expr, aop_ref64);
1824                 break;
1825               default:
1826                 /* Note that dwarf_stack_op_name will never return
1827                    NULL here.  */
1828                 error (_("Unsupported size %d in %s"),
1829                        size, dwarf_stack_op_name (op));
1830               }
1831           }
1832           break;
1833
1834         case DW_OP_abs:
1835           /* Sign extend the operand.  */
1836           ax_ext (expr, addr_size_bits);
1837           ax_simple (expr, aop_dup);
1838           ax_const_l (expr, 0);
1839           ax_simple (expr, aop_less_signed);
1840           ax_simple (expr, aop_log_not);
1841           i = ax_goto (expr, aop_if_goto);
1842           /* We have to emit 0 - X.  */
1843           ax_const_l (expr, 0);
1844           ax_simple (expr, aop_swap);
1845           ax_simple (expr, aop_sub);
1846           ax_label (expr, i, expr->len);
1847           break;
1848
1849         case DW_OP_neg:
1850           /* No need to sign extend here.  */
1851           ax_const_l (expr, 0);
1852           ax_simple (expr, aop_swap);
1853           ax_simple (expr, aop_sub);
1854           break;
1855
1856         case DW_OP_not:
1857           /* Sign extend the operand.  */
1858           ax_ext (expr, addr_size_bits);
1859           ax_simple (expr, aop_bit_not);
1860           break;
1861
1862         case DW_OP_plus_uconst:
1863           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1864           /* It would be really weird to emit `DW_OP_plus_uconst 0',
1865              but we micro-optimize anyhow.  */
1866           if (reg != 0)
1867             {
1868               ax_const_l (expr, reg);
1869               ax_simple (expr, aop_add);
1870             }
1871           break;
1872
1873         case DW_OP_and:
1874           ax_simple (expr, aop_bit_and);
1875           break;
1876
1877         case DW_OP_div:
1878           /* Sign extend the operands.  */
1879           ax_ext (expr, addr_size_bits);
1880           ax_simple (expr, aop_swap);
1881           ax_ext (expr, addr_size_bits);
1882           ax_simple (expr, aop_swap);
1883           ax_simple (expr, aop_div_signed);
1884           break;
1885
1886         case DW_OP_minus:
1887           ax_simple (expr, aop_sub);
1888           break;
1889
1890         case DW_OP_mod:
1891           ax_simple (expr, aop_rem_unsigned);
1892           break;
1893
1894         case DW_OP_mul:
1895           ax_simple (expr, aop_mul);
1896           break;
1897
1898         case DW_OP_or:
1899           ax_simple (expr, aop_bit_or);
1900           break;
1901
1902         case DW_OP_plus:
1903           ax_simple (expr, aop_add);
1904           break;
1905
1906         case DW_OP_shl:
1907           ax_simple (expr, aop_lsh);
1908           break;
1909
1910         case DW_OP_shr:
1911           ax_simple (expr, aop_rsh_unsigned);
1912           break;
1913
1914         case DW_OP_shra:
1915           ax_simple (expr, aop_rsh_signed);
1916           break;
1917
1918         case DW_OP_xor:
1919           ax_simple (expr, aop_bit_xor);
1920           break;
1921
1922         case DW_OP_le:
1923           /* Sign extend the operands.  */
1924           ax_ext (expr, addr_size_bits);
1925           ax_simple (expr, aop_swap);
1926           ax_ext (expr, addr_size_bits);
1927           /* Note no swap here: A <= B is !(B < A).  */
1928           ax_simple (expr, aop_less_signed);
1929           ax_simple (expr, aop_log_not);
1930           break;
1931
1932         case DW_OP_ge:
1933           /* Sign extend the operands.  */
1934           ax_ext (expr, addr_size_bits);
1935           ax_simple (expr, aop_swap);
1936           ax_ext (expr, addr_size_bits);
1937           ax_simple (expr, aop_swap);
1938           /* A >= B is !(A < B).  */
1939           ax_simple (expr, aop_less_signed);
1940           ax_simple (expr, aop_log_not);
1941           break;
1942
1943         case DW_OP_eq:
1944           /* Sign extend the operands.  */
1945           ax_ext (expr, addr_size_bits);
1946           ax_simple (expr, aop_swap);
1947           ax_ext (expr, addr_size_bits);
1948           /* No need for a second swap here.  */
1949           ax_simple (expr, aop_equal);
1950           break;
1951
1952         case DW_OP_lt:
1953           /* Sign extend the operands.  */
1954           ax_ext (expr, addr_size_bits);
1955           ax_simple (expr, aop_swap);
1956           ax_ext (expr, addr_size_bits);
1957           ax_simple (expr, aop_swap);
1958           ax_simple (expr, aop_less_signed);
1959           break;
1960
1961         case DW_OP_gt:
1962           /* Sign extend the operands.  */
1963           ax_ext (expr, addr_size_bits);
1964           ax_simple (expr, aop_swap);
1965           ax_ext (expr, addr_size_bits);
1966           /* Note no swap here: A > B is B < A.  */
1967           ax_simple (expr, aop_less_signed);
1968           break;
1969
1970         case DW_OP_ne:
1971           /* Sign extend the operands.  */
1972           ax_ext (expr, addr_size_bits);
1973           ax_simple (expr, aop_swap);
1974           ax_ext (expr, addr_size_bits);
1975           /* No need for a swap here.  */
1976           ax_simple (expr, aop_equal);
1977           ax_simple (expr, aop_log_not);
1978           break;
1979
1980         case DW_OP_call_frame_cfa:
1981           dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
1982           loc->kind = axs_lvalue_memory;
1983           break;
1984
1985         case DW_OP_GNU_push_tls_address:
1986           unimplemented (op);
1987           break;
1988
1989         case DW_OP_skip:
1990           offset = extract_signed_integer (op_ptr, 2, byte_order);
1991           op_ptr += 2;
1992           i = ax_goto (expr, aop_goto);
1993           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1994           VEC_safe_push (int, patches, i);
1995           break;
1996
1997         case DW_OP_bra:
1998           offset = extract_signed_integer (op_ptr, 2, byte_order);
1999           op_ptr += 2;
2000           /* Zero extend the operand.  */
2001           ax_zero_ext (expr, addr_size_bits);
2002           i = ax_goto (expr, aop_if_goto);
2003           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2004           VEC_safe_push (int, patches, i);
2005           break;
2006
2007         case DW_OP_nop:
2008           break;
2009
2010         case DW_OP_piece:
2011         case DW_OP_bit_piece:
2012           {
2013             ULONGEST size, offset;
2014
2015             if (op_ptr - 1 == previous_piece)
2016               error (_("Cannot translate empty pieces to agent expressions"));
2017             previous_piece = op_ptr - 1;
2018
2019             op_ptr = read_uleb128 (op_ptr, op_end, &size);
2020             if (op == DW_OP_piece)
2021               {
2022                 size *= 8;
2023                 offset = 0;
2024               }
2025             else
2026               op_ptr = read_uleb128 (op_ptr, op_end, &offset);
2027
2028             if (bits_collected + size > 8 * sizeof (LONGEST))
2029               error (_("Expression pieces exceed word size"));
2030
2031             /* Access the bits.  */
2032             switch (loc->kind)
2033               {
2034               case axs_lvalue_register:
2035                 ax_reg (expr, loc->u.reg);
2036                 break;
2037
2038               case axs_lvalue_memory:
2039                 /* Offset the pointer, if needed.  */
2040                 if (offset > 8)
2041                   {
2042                     ax_const_l (expr, offset / 8);
2043                     ax_simple (expr, aop_add);
2044                     offset %= 8;
2045                   }
2046                 access_memory (arch, expr, size);
2047                 break;
2048               }
2049
2050             /* For a bits-big-endian target, shift up what we already
2051                have.  For a bits-little-endian target, shift up the
2052                new data.  Note that there is a potential bug here if
2053                the DWARF expression leaves multiple values on the
2054                stack.  */
2055             if (bits_collected > 0)
2056               {
2057                 if (bits_big_endian)
2058                   {
2059                     ax_simple (expr, aop_swap);
2060                     ax_const_l (expr, size);
2061                     ax_simple (expr, aop_lsh);
2062                     /* We don't need a second swap here, because
2063                        aop_bit_or is symmetric.  */
2064                   }
2065                 else
2066                   {
2067                     ax_const_l (expr, size);
2068                     ax_simple (expr, aop_lsh);
2069                   }
2070                 ax_simple (expr, aop_bit_or);
2071               }
2072
2073             bits_collected += size;
2074             loc->kind = axs_rvalue;
2075           }
2076           break;
2077
2078         case DW_OP_GNU_uninit:
2079           unimplemented (op);
2080
2081         case DW_OP_call2:
2082         case DW_OP_call4:
2083           {
2084             struct dwarf2_locexpr_baton block;
2085             int size = (op == DW_OP_call2 ? 2 : 4);
2086
2087             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
2088             op_ptr += size;
2089
2090             block = dwarf2_fetch_die_location_block (uoffset, per_cu,
2091                                                      get_ax_pc, expr);
2092
2093             /* DW_OP_call_ref is currently not supported.  */
2094             gdb_assert (block.per_cu == per_cu);
2095
2096             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
2097                                        block.data, block.data + block.size,
2098                                        per_cu);
2099           }
2100           break;
2101
2102         case DW_OP_call_ref:
2103           unimplemented (op);
2104
2105         default:
2106           unimplemented (op);
2107         }
2108     }
2109
2110   /* Patch all the branches we emitted.  */
2111   for (i = 0; i < VEC_length (int, patches); ++i)
2112     {
2113       int targ = offsets[VEC_index (int, dw_labels, i)];
2114       if (targ == -1)
2115         internal_error (__FILE__, __LINE__, _("invalid label"));
2116       ax_label (expr, VEC_index (int, patches, i), targ);
2117     }
2118
2119   do_cleanups (cleanups);
2120 }
2121
2122 \f
2123 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2124    evaluator to calculate the location.  */
2125 static struct value *
2126 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
2127 {
2128   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2129   struct value *val;
2130
2131   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
2132                                   dlbaton->size, dlbaton->per_cu);
2133
2134   return val;
2135 }
2136
2137 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
2138 static int
2139 locexpr_read_needs_frame (struct symbol *symbol)
2140 {
2141   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2142
2143   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
2144                                       dlbaton->per_cu);
2145 }
2146
2147 /* Return true if DATA points to the end of a piece.  END is one past
2148    the last byte in the expression.  */
2149
2150 static int
2151 piece_end_p (const gdb_byte *data, const gdb_byte *end)
2152 {
2153   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
2154 }
2155
2156 /* Nicely describe a single piece of a location, returning an updated
2157    position in the bytecode sequence.  This function cannot recognize
2158    all locations; if a location is not recognized, it simply returns
2159    DATA.  */
2160
2161 static const gdb_byte *
2162 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
2163                                  CORE_ADDR addr, struct objfile *objfile,
2164                                  const gdb_byte *data, const gdb_byte *end,
2165                                  unsigned int addr_size)
2166 {
2167   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2168   int regno;
2169
2170   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
2171     {
2172       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
2173       fprintf_filtered (stream, _("a variable in $%s"),
2174                         gdbarch_register_name (gdbarch, regno));
2175       data += 1;
2176     }
2177   else if (data[0] == DW_OP_regx)
2178     {
2179       ULONGEST reg;
2180
2181       data = read_uleb128 (data + 1, end, &reg);
2182       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
2183       fprintf_filtered (stream, _("a variable in $%s"),
2184                         gdbarch_register_name (gdbarch, regno));
2185     }
2186   else if (data[0] == DW_OP_fbreg)
2187     {
2188       struct block *b;
2189       struct symbol *framefunc;
2190       int frame_reg = 0;
2191       LONGEST frame_offset;
2192       const gdb_byte *base_data, *new_data, *save_data = data;
2193       size_t base_size;
2194       LONGEST base_offset = 0;
2195
2196       new_data = read_sleb128 (data + 1, end, &frame_offset);
2197       if (!piece_end_p (new_data, end))
2198         return data;
2199       data = new_data;
2200
2201       b = block_for_pc (addr);
2202
2203       if (!b)
2204         error (_("No block found for address for symbol \"%s\"."),
2205                SYMBOL_PRINT_NAME (symbol));
2206
2207       framefunc = block_linkage_function (b);
2208
2209       if (!framefunc)
2210         error (_("No function found for block for symbol \"%s\"."),
2211                SYMBOL_PRINT_NAME (symbol));
2212
2213       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
2214
2215       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
2216         {
2217           const gdb_byte *buf_end;
2218           
2219           frame_reg = base_data[0] - DW_OP_breg0;
2220           buf_end = read_sleb128 (base_data + 1,
2221                                   base_data + base_size, &base_offset);
2222           if (buf_end != base_data + base_size)
2223             error (_("Unexpected opcode after "
2224                      "DW_OP_breg%u for symbol \"%s\"."),
2225                    frame_reg, SYMBOL_PRINT_NAME (symbol));
2226         }
2227       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
2228         {
2229           /* The frame base is just the register, with no offset.  */
2230           frame_reg = base_data[0] - DW_OP_reg0;
2231           base_offset = 0;
2232         }
2233       else
2234         {
2235           /* We don't know what to do with the frame base expression,
2236              so we can't trace this variable; give up.  */
2237           return save_data;
2238         }
2239
2240       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
2241
2242       fprintf_filtered (stream,
2243                         _("a variable at frame base reg $%s offset %s+%s"),
2244                         gdbarch_register_name (gdbarch, regno),
2245                         plongest (base_offset), plongest (frame_offset));
2246     }
2247   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
2248            && piece_end_p (data, end))
2249     {
2250       LONGEST offset;
2251
2252       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
2253
2254       data = read_sleb128 (data + 1, end, &offset);
2255
2256       fprintf_filtered (stream,
2257                         _("a variable at offset %s from base reg $%s"),
2258                         plongest (offset),
2259                         gdbarch_register_name (gdbarch, regno));
2260     }
2261
2262   /* The location expression for a TLS variable looks like this (on a
2263      64-bit LE machine):
2264
2265      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
2266                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
2267
2268      0x3 is the encoding for DW_OP_addr, which has an operand as long
2269      as the size of an address on the target machine (here is 8
2270      bytes).  Note that more recent version of GCC emit DW_OP_const4u
2271      or DW_OP_const8u, depending on address size, rather than
2272      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
2273      The operand represents the offset at which the variable is within
2274      the thread local storage.  */
2275
2276   else if (data + 1 + addr_size < end
2277            && (data[0] == DW_OP_addr
2278                || (addr_size == 4 && data[0] == DW_OP_const4u)
2279                || (addr_size == 8 && data[0] == DW_OP_const8u))
2280            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
2281            && piece_end_p (data + 2 + addr_size, end))
2282     {
2283       ULONGEST offset;
2284       offset = extract_unsigned_integer (data + 1, addr_size,
2285                                          gdbarch_byte_order (gdbarch));
2286
2287       fprintf_filtered (stream, 
2288                         _("a thread-local variable at offset 0x%s "
2289                           "in the thread-local storage for `%s'"),
2290                         phex_nz (offset, addr_size), objfile->name);
2291
2292       data += 1 + addr_size + 1;
2293     }
2294   else if (data[0] >= DW_OP_lit0
2295            && data[0] <= DW_OP_lit31
2296            && data + 1 < end
2297            && data[1] == DW_OP_stack_value)
2298     {
2299       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
2300       data += 2;
2301     }
2302
2303   return data;
2304 }
2305
2306 /* Disassemble an expression, stopping at the end of a piece or at the
2307    end of the expression.  Returns a pointer to the next unread byte
2308    in the input expression.  If ALL is nonzero, then this function
2309    will keep going until it reaches the end of the expression.  */
2310
2311 static const gdb_byte *
2312 disassemble_dwarf_expression (struct ui_file *stream,
2313                               struct gdbarch *arch, unsigned int addr_size,
2314                               int offset_size,
2315                               const gdb_byte *data, const gdb_byte *end,
2316                               int all)
2317 {
2318   const gdb_byte *start = data;
2319
2320   fprintf_filtered (stream, _("a complex DWARF expression:\n"));
2321
2322   while (data < end
2323          && (all
2324              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
2325     {
2326       enum dwarf_location_atom op = *data++;
2327       ULONGEST ul;
2328       LONGEST l;
2329       const char *name;
2330
2331       name = dwarf_stack_op_name (op);
2332
2333       if (!name)
2334         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2335                op, (long) (data - start));
2336       fprintf_filtered (stream, "  % 4ld: %s", (long) (data - start), name);
2337
2338       switch (op)
2339         {
2340         case DW_OP_addr:
2341           ul = extract_unsigned_integer (data, addr_size,
2342                                          gdbarch_byte_order (arch));
2343           data += addr_size;
2344           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
2345           break;
2346
2347         case DW_OP_const1u:
2348           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
2349           data += 1;
2350           fprintf_filtered (stream, " %s", pulongest (ul));
2351           break;
2352         case DW_OP_const1s:
2353           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
2354           data += 1;
2355           fprintf_filtered (stream, " %s", plongest (l));
2356           break;
2357         case DW_OP_const2u:
2358           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2359           data += 2;
2360           fprintf_filtered (stream, " %s", pulongest (ul));
2361           break;
2362         case DW_OP_const2s:
2363           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2364           data += 2;
2365           fprintf_filtered (stream, " %s", plongest (l));
2366           break;
2367         case DW_OP_const4u:
2368           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2369           data += 4;
2370           fprintf_filtered (stream, " %s", pulongest (ul));
2371           break;
2372         case DW_OP_const4s:
2373           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
2374           data += 4;
2375           fprintf_filtered (stream, " %s", plongest (l));
2376           break;
2377         case DW_OP_const8u:
2378           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
2379           data += 8;
2380           fprintf_filtered (stream, " %s", pulongest (ul));
2381           break;
2382         case DW_OP_const8s:
2383           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
2384           data += 8;
2385           fprintf_filtered (stream, " %s", plongest (l));
2386           break;
2387         case DW_OP_constu:
2388           data = read_uleb128 (data, end, &ul);
2389           fprintf_filtered (stream, " %s", pulongest (ul));
2390           break;
2391         case DW_OP_consts:
2392           data = read_sleb128 (data, end, &l);
2393           fprintf_filtered (stream, " %s", plongest (l));
2394           break;
2395
2396         case DW_OP_reg0:
2397         case DW_OP_reg1:
2398         case DW_OP_reg2:
2399         case DW_OP_reg3:
2400         case DW_OP_reg4:
2401         case DW_OP_reg5:
2402         case DW_OP_reg6:
2403         case DW_OP_reg7:
2404         case DW_OP_reg8:
2405         case DW_OP_reg9:
2406         case DW_OP_reg10:
2407         case DW_OP_reg11:
2408         case DW_OP_reg12:
2409         case DW_OP_reg13:
2410         case DW_OP_reg14:
2411         case DW_OP_reg15:
2412         case DW_OP_reg16:
2413         case DW_OP_reg17:
2414         case DW_OP_reg18:
2415         case DW_OP_reg19:
2416         case DW_OP_reg20:
2417         case DW_OP_reg21:
2418         case DW_OP_reg22:
2419         case DW_OP_reg23:
2420         case DW_OP_reg24:
2421         case DW_OP_reg25:
2422         case DW_OP_reg26:
2423         case DW_OP_reg27:
2424         case DW_OP_reg28:
2425         case DW_OP_reg29:
2426         case DW_OP_reg30:
2427         case DW_OP_reg31:
2428           fprintf_filtered (stream, " [$%s]",
2429                             gdbarch_register_name (arch, op - DW_OP_reg0));
2430           break;
2431
2432         case DW_OP_regx:
2433           data = read_uleb128 (data, end, &ul);
2434           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2435                             gdbarch_register_name (arch, (int) ul));
2436           break;
2437
2438         case DW_OP_implicit_value:
2439           data = read_uleb128 (data, end, &ul);
2440           data += ul;
2441           fprintf_filtered (stream, " %s", pulongest (ul));
2442           break;
2443
2444         case DW_OP_breg0:
2445         case DW_OP_breg1:
2446         case DW_OP_breg2:
2447         case DW_OP_breg3:
2448         case DW_OP_breg4:
2449         case DW_OP_breg5:
2450         case DW_OP_breg6:
2451         case DW_OP_breg7:
2452         case DW_OP_breg8:
2453         case DW_OP_breg9:
2454         case DW_OP_breg10:
2455         case DW_OP_breg11:
2456         case DW_OP_breg12:
2457         case DW_OP_breg13:
2458         case DW_OP_breg14:
2459         case DW_OP_breg15:
2460         case DW_OP_breg16:
2461         case DW_OP_breg17:
2462         case DW_OP_breg18:
2463         case DW_OP_breg19:
2464         case DW_OP_breg20:
2465         case DW_OP_breg21:
2466         case DW_OP_breg22:
2467         case DW_OP_breg23:
2468         case DW_OP_breg24:
2469         case DW_OP_breg25:
2470         case DW_OP_breg26:
2471         case DW_OP_breg27:
2472         case DW_OP_breg28:
2473         case DW_OP_breg29:
2474         case DW_OP_breg30:
2475         case DW_OP_breg31:
2476           data = read_sleb128 (data, end, &l);
2477           fprintf_filtered (stream, " %s [$%s]", plongest (l),
2478                             gdbarch_register_name (arch, op - DW_OP_breg0));
2479           break;
2480
2481         case DW_OP_bregx:
2482           data = read_uleb128 (data, end, &ul);
2483           data = read_sleb128 (data, end, &l);
2484           fprintf_filtered (stream, " register %s [$%s] offset %s",
2485                             pulongest (ul),
2486                             gdbarch_register_name (arch, (int) ul),
2487                             plongest (l));
2488           break;
2489
2490         case DW_OP_fbreg:
2491           data = read_sleb128 (data, end, &l);
2492           fprintf_filtered (stream, " %s", plongest (l));
2493           break;
2494
2495         case DW_OP_xderef_size:
2496         case DW_OP_deref_size:
2497         case DW_OP_pick:
2498           fprintf_filtered (stream, " %d", *data);
2499           ++data;
2500           break;
2501
2502         case DW_OP_plus_uconst:
2503           data = read_uleb128 (data, end, &ul);
2504           fprintf_filtered (stream, " %s", pulongest (ul));
2505           break;
2506
2507         case DW_OP_skip:
2508           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2509           data += 2;
2510           fprintf_filtered (stream, " to %ld",
2511                             (long) (data + l - start));
2512           break;
2513
2514         case DW_OP_bra:
2515           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2516           data += 2;
2517           fprintf_filtered (stream, " %ld",
2518                             (long) (data + l - start));
2519           break;
2520
2521         case DW_OP_call2:
2522           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2523           data += 2;
2524           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
2525           break;
2526
2527         case DW_OP_call4:
2528           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2529           data += 4;
2530           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
2531           break;
2532
2533         case DW_OP_call_ref:
2534           ul = extract_unsigned_integer (data, offset_size,
2535                                          gdbarch_byte_order (arch));
2536           data += offset_size;
2537           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
2538           break;
2539
2540         case DW_OP_piece:
2541           data = read_uleb128 (data, end, &ul);
2542           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
2543           break;
2544
2545         case DW_OP_bit_piece:
2546           {
2547             ULONGEST offset;
2548
2549             data = read_uleb128 (data, end, &ul);
2550             data = read_uleb128 (data, end, &offset);
2551             fprintf_filtered (stream, " size %s offset %s (bits)",
2552                               pulongest (ul), pulongest (offset));
2553           }
2554           break;
2555
2556         case DW_OP_GNU_implicit_pointer:
2557           {
2558             ul = extract_unsigned_integer (data, offset_size,
2559                                            gdbarch_byte_order (arch));
2560             data += offset_size;
2561
2562             data = read_sleb128 (data, end, &l);
2563
2564             fprintf_filtered (stream, " DIE %s offset %s",
2565                               phex_nz (ul, offset_size),
2566                               plongest (l));
2567           }
2568           break;
2569         }
2570
2571       fprintf_filtered (stream, "\n");
2572     }
2573
2574   return data;
2575 }
2576
2577 /* Describe a single location, which may in turn consist of multiple
2578    pieces.  */
2579
2580 static void
2581 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
2582                              struct ui_file *stream,
2583                              const gdb_byte *data, int size,
2584                              struct objfile *objfile, unsigned int addr_size,
2585                              int offset_size)
2586 {
2587   const gdb_byte *end = data + size;
2588   int first_piece = 1, bad = 0;
2589
2590   while (data < end)
2591     {
2592       const gdb_byte *here = data;
2593       int disassemble = 1;
2594
2595       if (first_piece)
2596         first_piece = 0;
2597       else
2598         fprintf_filtered (stream, _(", and "));
2599
2600       if (!dwarf2_always_disassemble)
2601         {
2602           data = locexpr_describe_location_piece (symbol, stream,
2603                                                   addr, objfile,
2604                                                   data, end, addr_size);
2605           /* If we printed anything, or if we have an empty piece,
2606              then don't disassemble.  */
2607           if (data != here
2608               || data[0] == DW_OP_piece
2609               || data[0] == DW_OP_bit_piece)
2610             disassemble = 0;
2611         }
2612       if (disassemble)
2613         data = disassemble_dwarf_expression (stream,
2614                                              get_objfile_arch (objfile),
2615                                              addr_size, offset_size, data, end,
2616                                              dwarf2_always_disassemble);
2617
2618       if (data < end)
2619         {
2620           int empty = data == here;
2621               
2622           if (disassemble)
2623             fprintf_filtered (stream, "   ");
2624           if (data[0] == DW_OP_piece)
2625             {
2626               ULONGEST bytes;
2627
2628               data = read_uleb128 (data + 1, end, &bytes);
2629
2630               if (empty)
2631                 fprintf_filtered (stream, _("an empty %s-byte piece"),
2632                                   pulongest (bytes));
2633               else
2634                 fprintf_filtered (stream, _(" [%s-byte piece]"),
2635                                   pulongest (bytes));
2636             }
2637           else if (data[0] == DW_OP_bit_piece)
2638             {
2639               ULONGEST bits, offset;
2640
2641               data = read_uleb128 (data + 1, end, &bits);
2642               data = read_uleb128 (data, end, &offset);
2643
2644               if (empty)
2645                 fprintf_filtered (stream,
2646                                   _("an empty %s-bit piece"),
2647                                   pulongest (bits));
2648               else
2649                 fprintf_filtered (stream,
2650                                   _(" [%s-bit piece, offset %s bits]"),
2651                                   pulongest (bits), pulongest (offset));
2652             }
2653           else
2654             {
2655               bad = 1;
2656               break;
2657             }
2658         }
2659     }
2660
2661   if (bad || data > end)
2662     error (_("Corrupted DWARF2 expression for \"%s\"."),
2663            SYMBOL_PRINT_NAME (symbol));
2664 }
2665
2666 /* Print a natural-language description of SYMBOL to STREAM.  This
2667    version is for a symbol with a single location.  */
2668
2669 static void
2670 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
2671                            struct ui_file *stream)
2672 {
2673   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2674   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2675   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2676   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2677
2678   locexpr_describe_location_1 (symbol, addr, stream,
2679                                dlbaton->data, dlbaton->size,
2680                                objfile, addr_size, offset_size);
2681 }
2682
2683 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2684    any necessary bytecode in AX.  */
2685
2686 static void
2687 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2688                             struct agent_expr *ax, struct axs_value *value)
2689 {
2690   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2691   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2692
2693   if (dlbaton->data == NULL || dlbaton->size == 0)
2694     value->optimized_out = 1;
2695   else
2696     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
2697                                dlbaton->data, dlbaton->data + dlbaton->size,
2698                                dlbaton->per_cu);
2699 }
2700
2701 /* The set of location functions used with the DWARF-2 expression
2702    evaluator.  */
2703 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
2704   locexpr_read_variable,
2705   locexpr_read_needs_frame,
2706   locexpr_describe_location,
2707   locexpr_tracepoint_var_ref
2708 };
2709
2710
2711 /* Wrapper functions for location lists.  These generally find
2712    the appropriate location expression and call something above.  */
2713
2714 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2715    evaluator to calculate the location.  */
2716 static struct value *
2717 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
2718 {
2719   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2720   struct value *val;
2721   const gdb_byte *data;
2722   size_t size;
2723   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
2724
2725   data = dwarf2_find_location_expression (dlbaton, &size, pc);
2726   if (data == NULL)
2727     {
2728       val = allocate_value (SYMBOL_TYPE (symbol));
2729       VALUE_LVAL (val) = not_lval;
2730       set_value_optimized_out (val, 1);
2731     }
2732   else
2733     val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
2734                                     dlbaton->per_cu);
2735
2736   return val;
2737 }
2738
2739 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
2740 static int
2741 loclist_read_needs_frame (struct symbol *symbol)
2742 {
2743   /* If there's a location list, then assume we need to have a frame
2744      to choose the appropriate location expression.  With tracking of
2745      global variables this is not necessarily true, but such tracking
2746      is disabled in GCC at the moment until we figure out how to
2747      represent it.  */
2748
2749   return 1;
2750 }
2751
2752 /* Print a natural-language description of SYMBOL to STREAM.  This
2753    version applies when there is a list of different locations, each
2754    with a specified address range.  */
2755
2756 static void
2757 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
2758                            struct ui_file *stream)
2759 {
2760   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2761   CORE_ADDR low, high;
2762   const gdb_byte *loc_ptr, *buf_end;
2763   int length, first = 1;
2764   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2765   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2766   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2767   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2768   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2769   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
2770   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2771   /* Adjust base_address for relocatable objects.  */
2772   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2773   CORE_ADDR base_address = dlbaton->base_address + base_offset;
2774
2775   loc_ptr = dlbaton->data;
2776   buf_end = dlbaton->data + dlbaton->size;
2777
2778   fprintf_filtered (stream, _("multi-location:\n"));
2779
2780   /* Iterate through locations until we run out.  */
2781   while (1)
2782     {
2783       if (buf_end - loc_ptr < 2 * addr_size)
2784         error (_("Corrupted DWARF expression for symbol \"%s\"."),
2785                SYMBOL_PRINT_NAME (symbol));
2786
2787       if (signed_addr_p)
2788         low = extract_signed_integer (loc_ptr, addr_size, byte_order);
2789       else
2790         low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2791       loc_ptr += addr_size;
2792
2793       if (signed_addr_p)
2794         high = extract_signed_integer (loc_ptr, addr_size, byte_order);
2795       else
2796         high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2797       loc_ptr += addr_size;
2798
2799       /* A base-address-selection entry.  */
2800       if ((low & base_mask) == base_mask)
2801         {
2802           base_address = high + base_offset;
2803           fprintf_filtered (stream, _("  Base address %s"),
2804                             paddress (gdbarch, base_address));
2805           continue;
2806         }
2807
2808       /* An end-of-list entry.  */
2809       if (low == 0 && high == 0)
2810         break;
2811
2812       /* Otherwise, a location expression entry.  */
2813       low += base_address;
2814       high += base_address;
2815
2816       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
2817       loc_ptr += 2;
2818
2819       /* (It would improve readability to print only the minimum
2820          necessary digits of the second number of the range.)  */
2821       fprintf_filtered (stream, _("  Range %s-%s: "),
2822                         paddress (gdbarch, low), paddress (gdbarch, high));
2823
2824       /* Now describe this particular location.  */
2825       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
2826                                    objfile, addr_size, offset_size);
2827
2828       fprintf_filtered (stream, "\n");
2829
2830       loc_ptr += length;
2831     }
2832 }
2833
2834 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2835    any necessary bytecode in AX.  */
2836 static void
2837 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2838                             struct agent_expr *ax, struct axs_value *value)
2839 {
2840   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2841   const gdb_byte *data;
2842   size_t size;
2843   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2844
2845   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
2846   if (data == NULL || size == 0)
2847     value->optimized_out = 1;
2848   else
2849     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
2850                                dlbaton->per_cu);
2851 }
2852
2853 /* The set of location functions used with the DWARF-2 expression
2854    evaluator and location lists.  */
2855 const struct symbol_computed_ops dwarf2_loclist_funcs = {
2856   loclist_read_variable,
2857   loclist_read_needs_frame,
2858   loclist_describe_location,
2859   loclist_tracepoint_var_ref
2860 };