OSDN Git Service

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