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