gdb
[external/binutils.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3    Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
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 /* A helper function for dealing with location lists.  Given a
52    symbol baton (BATON) and a pc value (PC), find the appropriate
53    location expression, set *LOCEXPR_LENGTH, and return a pointer
54    to the beginning of the expression.  Returns NULL on failure.
55
56    For now, only return the first matching location expression; there
57    can be more than one in the list.  */
58
59 static const gdb_byte *
60 find_location_expression (struct dwarf2_loclist_baton *baton,
61                           size_t *locexpr_length, CORE_ADDR pc)
62 {
63   CORE_ADDR low, high;
64   const gdb_byte *loc_ptr, *buf_end;
65   int length;
66   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
67   struct gdbarch *gdbarch = get_objfile_arch (objfile);
68   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
69   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
70   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
71   /* Adjust base_address for relocatable objects.  */
72   CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
73                                     SECT_OFF_TEXT (objfile));
74   CORE_ADDR base_address = baton->base_address + base_offset;
75
76   loc_ptr = baton->data;
77   buf_end = baton->data + baton->size;
78
79   while (1)
80     {
81       if (buf_end - loc_ptr < 2 * addr_size)
82         error (_("find_location_expression: Corrupted DWARF expression."));
83
84       low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
85       loc_ptr += addr_size;
86
87       /* A base-address-selection entry.  */
88       if (low == base_mask)
89         {
90           base_address = dwarf2_read_address (gdbarch,
91                                               loc_ptr, buf_end, addr_size);
92           loc_ptr += addr_size;
93           continue;
94         }
95
96       high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
97       loc_ptr += addr_size;
98
99       /* An end-of-list entry.  */
100       if (low == 0 && high == 0)
101         return NULL;
102
103       /* Otherwise, a location expression entry.  */
104       low += base_address;
105       high += base_address;
106
107       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
108       loc_ptr += 2;
109
110       if (pc >= low && pc < high)
111         {
112           *locexpr_length = length;
113           return loc_ptr;
114         }
115
116       loc_ptr += length;
117     }
118 }
119
120 /* This is the baton used when performing dwarf2 expression
121    evaluation.  */
122 struct dwarf_expr_baton
123 {
124   struct frame_info *frame;
125   struct objfile *objfile;
126 };
127
128 /* Helper functions for dwarf2_evaluate_loc_desc.  */
129
130 /* Using the frame specified in BATON, return the value of register
131    REGNUM, treated as a pointer.  */
132 static CORE_ADDR
133 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
134 {
135   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
136   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
137   CORE_ADDR result;
138   int regnum;
139
140   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
141   result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
142                                   regnum, debaton->frame);
143   return result;
144 }
145
146 /* Read memory at ADDR (length LEN) into BUF.  */
147
148 static void
149 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
150 {
151   read_memory (addr, buf, len);
152 }
153
154 /* Using the frame specified in BATON, find the location expression
155    describing the frame base.  Return a pointer to it in START and
156    its length in LENGTH.  */
157 static void
158 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
159 {
160   /* FIXME: cagney/2003-03-26: This code should be using
161      get_frame_base_address(), and then implement a dwarf2 specific
162      this_base method.  */
163   struct symbol *framefunc;
164   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
165
166   /* Use block_linkage_function, which returns a real (not inlined)
167      function, instead of get_frame_function, which may return an
168      inlined function.  */
169   framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
170
171   /* If we found a frame-relative symbol then it was certainly within
172      some function associated with a frame. If we can't find the frame,
173      something has gone wrong.  */
174   gdb_assert (framefunc != NULL);
175
176   dwarf_expr_frame_base_1 (framefunc,
177                            get_frame_address_in_block (debaton->frame),
178                            start, length);
179 }
180
181 static void
182 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
183                          const gdb_byte **start, size_t *length)
184 {
185   if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
186     *start = NULL;
187   else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
188     {
189       struct dwarf2_loclist_baton *symbaton;
190
191       symbaton = SYMBOL_LOCATION_BATON (framefunc);
192       *start = find_location_expression (symbaton, length, pc);
193     }
194   else
195     {
196       struct dwarf2_locexpr_baton *symbaton;
197
198       symbaton = SYMBOL_LOCATION_BATON (framefunc);
199       if (symbaton != NULL)
200         {
201           *length = symbaton->size;
202           *start = symbaton->data;
203         }
204       else
205         *start = NULL;
206     }
207
208   if (*start == NULL)
209     error (_("Could not find the frame base for \"%s\"."),
210            SYMBOL_NATURAL_NAME (framefunc));
211 }
212
213 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
214    the frame in BATON.  */
215
216 static CORE_ADDR
217 dwarf_expr_frame_cfa (void *baton)
218 {
219   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
220
221   return dwarf2_frame_cfa (debaton->frame);
222 }
223
224 /* Using the objfile specified in BATON, find the address for the
225    current thread's thread-local storage with offset OFFSET.  */
226 static CORE_ADDR
227 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
228 {
229   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
230
231   return target_translate_tls_address (debaton->objfile, offset);
232 }
233
234 struct piece_closure
235 {
236   /* Reference count.  */
237   int refc;
238
239   /* The number of pieces used to describe this variable.  */
240   int n_pieces;
241
242   /* The target address size, used only for DWARF_VALUE_STACK.  */
243   int addr_size;
244
245   /* The pieces themselves.  */
246   struct dwarf_expr_piece *pieces;
247 };
248
249 /* Allocate a closure for a value formed from separately-described
250    PIECES.  */
251
252 static struct piece_closure *
253 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
254                         int addr_size)
255 {
256   struct piece_closure *c = XZALLOC (struct piece_closure);
257
258   c->refc = 1;
259   c->n_pieces = n_pieces;
260   c->addr_size = addr_size;
261   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
262
263   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
264
265   return c;
266 }
267
268 /* The lowest-level function to extract bits from a byte buffer.
269    SOURCE is the buffer.  It is updated if we read to the end of a
270    byte.
271    SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
272    updated to reflect the number of bits actually read.
273    NBITS is the number of bits we want to read.  It is updated to
274    reflect the number of bits actually read.  This function may read
275    fewer bits.
276    BITS_BIG_ENDIAN is taken directly from gdbarch.
277    This function returns the extracted bits.  */
278
279 static unsigned int
280 extract_bits_primitive (const gdb_byte **source,
281                         unsigned int *source_offset_bits,
282                         int *nbits, int bits_big_endian)
283 {
284   unsigned int avail, mask, datum;
285
286   gdb_assert (*source_offset_bits < 8);
287
288   avail = 8 - *source_offset_bits;
289   if (avail > *nbits)
290     avail = *nbits;
291
292   mask = (1 << avail) - 1;
293   datum = **source;
294   if (bits_big_endian)
295     datum >>= 8 - (*source_offset_bits + *nbits);
296   else
297     datum >>= *source_offset_bits;
298   datum &= mask;
299
300   *nbits -= avail;
301   *source_offset_bits += avail;
302   if (*source_offset_bits >= 8)
303     {
304       *source_offset_bits -= 8;
305       ++*source;
306     }
307
308   return datum;
309 }
310
311 /* Extract some bits from a source buffer and move forward in the
312    buffer.
313    
314    SOURCE is the source buffer.  It is updated as bytes are read.
315    SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
316    bits are read.
317    NBITS is the number of bits to read.
318    BITS_BIG_ENDIAN is taken directly from gdbarch.
319    
320    This function returns the bits that were read.  */
321
322 static unsigned int
323 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
324               int nbits, int bits_big_endian)
325 {
326   unsigned int datum;
327
328   gdb_assert (nbits > 0 && nbits <= 8);
329
330   datum = extract_bits_primitive (source, source_offset_bits, &nbits,
331                                   bits_big_endian);
332   if (nbits > 0)
333     {
334       unsigned int more;
335
336       more = extract_bits_primitive (source, source_offset_bits, &nbits,
337                                      bits_big_endian);
338       if (bits_big_endian)
339         datum <<= nbits;
340       else
341         more <<= nbits;
342       datum |= more;
343     }
344
345   return datum;
346 }
347
348 /* Write some bits into a buffer and move forward in the buffer.
349    
350    DATUM is the bits to write.  The low-order bits of DATUM are used.
351    DEST is the destination buffer.  It is updated as bytes are
352    written.
353    DEST_OFFSET_BITS is the bit offset in DEST at which writing is
354    done.
355    NBITS is the number of valid bits in DATUM.
356    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
357
358 static void
359 insert_bits (unsigned int datum,
360              gdb_byte *dest, unsigned int dest_offset_bits,
361              int nbits, int bits_big_endian)
362 {
363   unsigned int mask;
364
365   gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
366
367   mask = (1 << nbits) - 1;
368   if (bits_big_endian)
369     {
370       datum <<= 8 - (dest_offset_bits + nbits);
371       mask <<= 8 - (dest_offset_bits + nbits);
372     }
373   else
374     {
375       datum <<= dest_offset_bits;
376       mask <<= dest_offset_bits;
377     }
378
379   gdb_assert ((datum & ~mask) == 0);
380
381   *dest = (*dest & ~mask) | datum;
382 }
383
384 /* Copy bits from a source to a destination.
385    
386    DEST is where the bits should be written.
387    DEST_OFFSET_BITS is the bit offset into DEST.
388    SOURCE is the source of bits.
389    SOURCE_OFFSET_BITS is the bit offset into SOURCE.
390    BIT_COUNT is the number of bits to copy.
391    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
392
393 static void
394 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
395               const gdb_byte *source, unsigned int source_offset_bits,
396               unsigned int bit_count,
397               int bits_big_endian)
398 {
399   unsigned int dest_avail;
400   int datum;
401
402   /* Reduce everything to byte-size pieces.  */
403   dest += dest_offset_bits / 8;
404   dest_offset_bits %= 8;
405   source += source_offset_bits / 8;
406   source_offset_bits %= 8;
407
408   dest_avail = 8 - dest_offset_bits % 8;
409
410   /* See if we can fill the first destination byte.  */
411   if (dest_avail < bit_count)
412     {
413       datum = extract_bits (&source, &source_offset_bits, dest_avail,
414                             bits_big_endian);
415       insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
416       ++dest;
417       dest_offset_bits = 0;
418       bit_count -= dest_avail;
419     }
420
421   /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
422      than 8 bits remaining.  */
423   gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
424   for (; bit_count >= 8; bit_count -= 8)
425     {
426       datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
427       *dest++ = (gdb_byte) datum;
428     }
429
430   /* Finally, we may have a few leftover bits.  */
431   gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
432   if (bit_count > 0)
433     {
434       datum = extract_bits (&source, &source_offset_bits, bit_count,
435                             bits_big_endian);
436       insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
437     }
438 }
439
440 static void
441 read_pieced_value (struct value *v)
442 {
443   int i;
444   long offset = 0;
445   ULONGEST bits_to_skip;
446   gdb_byte *contents;
447   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
448   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
449   size_t type_len;
450   size_t buffer_size = 0;
451   char *buffer = NULL;
452   struct cleanup *cleanup;
453   int bits_big_endian
454     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
455
456   if (value_type (v) != value_enclosing_type (v))
457     internal_error (__FILE__, __LINE__,
458                     _("Should not be able to create a lazy value with "
459                       "an enclosing type"));
460
461   cleanup = make_cleanup (free_current_contents, &buffer);
462
463   contents = value_contents_raw (v);
464   bits_to_skip = 8 * value_offset (v);
465   type_len = 8 * TYPE_LENGTH (value_type (v));
466
467   for (i = 0; i < c->n_pieces && offset < type_len; i++)
468     {
469       struct dwarf_expr_piece *p = &c->pieces[i];
470       size_t this_size, this_size_bits;
471       long dest_offset_bits, source_offset_bits, source_offset;
472       const gdb_byte *intermediate_buffer;
473
474       /* Compute size, source, and destination offsets for copying, in
475          bits.  */
476       this_size_bits = p->size;
477       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
478         {
479           bits_to_skip -= this_size_bits;
480           continue;
481         }
482       if (this_size_bits > type_len - offset)
483         this_size_bits = type_len - offset;
484       if (bits_to_skip > 0)
485         {
486           dest_offset_bits = 0;
487           source_offset_bits = bits_to_skip;
488           this_size_bits -= bits_to_skip;
489           bits_to_skip = 0;
490         }
491       else
492         {
493           dest_offset_bits = offset;
494           source_offset_bits = 0;
495         }
496
497       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
498       source_offset = source_offset_bits / 8;
499       if (buffer_size < this_size)
500         {
501           buffer_size = this_size;
502           buffer = xrealloc (buffer, buffer_size);
503         }
504       intermediate_buffer = buffer;
505
506       /* Copy from the source to DEST_BUFFER.  */
507       switch (p->location)
508         {
509         case DWARF_VALUE_REGISTER:
510           {
511             struct gdbarch *arch = get_frame_arch (frame);
512             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
513                                                            p->v.expr.value);
514             int reg_offset = source_offset;
515
516             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
517                 && this_size < register_size (arch, gdb_regnum))
518               {
519                 /* Big-endian, and we want less than full size.  */
520                 reg_offset = register_size (arch, gdb_regnum) - this_size;
521                 /* We want the lower-order THIS_SIZE_BITS of the bytes
522                    we extract from the register.  */
523                 source_offset_bits += 8 * this_size - this_size_bits;
524               }
525
526             if (gdb_regnum != -1)
527               {
528                 get_frame_register_bytes (frame, gdb_regnum, reg_offset, 
529                                           this_size, buffer);
530               }
531             else
532               {
533                 error (_("Unable to access DWARF register number %s"),
534                        paddress (arch, p->v.expr.value));
535               }
536           }
537           break;
538
539         case DWARF_VALUE_MEMORY:
540           if (p->v.expr.in_stack_memory)
541             read_stack (p->v.expr.value + source_offset, buffer, this_size);
542           else
543             read_memory (p->v.expr.value + source_offset, buffer, this_size);
544           break;
545
546         case DWARF_VALUE_STACK:
547           {
548             struct gdbarch *gdbarch = get_type_arch (value_type (v));
549             size_t n = this_size;
550
551             if (n > c->addr_size - source_offset)
552               n = (c->addr_size >= source_offset
553                    ? c->addr_size - source_offset
554                    : 0);
555             if (n == 0)
556               {
557                 /* Nothing.  */
558               }
559             else if (source_offset == 0)
560               store_unsigned_integer (buffer, n,
561                                       gdbarch_byte_order (gdbarch),
562                                       p->v.expr.value);
563             else
564               {
565                 gdb_byte bytes[sizeof (ULONGEST)];
566
567                 store_unsigned_integer (bytes, n + source_offset,
568                                         gdbarch_byte_order (gdbarch),
569                                         p->v.expr.value);
570                 memcpy (buffer, bytes + source_offset, n);
571               }
572           }
573           break;
574
575         case DWARF_VALUE_LITERAL:
576           {
577             size_t n = this_size;
578
579             if (n > p->v.literal.length - source_offset)
580               n = (p->v.literal.length >= source_offset
581                    ? p->v.literal.length - source_offset
582                    : 0);
583             if (n != 0)
584               intermediate_buffer = p->v.literal.data + source_offset;
585           }
586           break;
587
588         case DWARF_VALUE_OPTIMIZED_OUT:
589           /* We just leave the bits empty for now.  This is not ideal
590              but gdb currently does not have a nice way to represent
591              optimized-out pieces.  */
592           warning (_("bits %ld-%ld in computed object were optimized out; "
593                      "replacing with zeroes"),
594                    offset,
595                    offset + (long) this_size_bits);
596           break;
597
598         default:
599           internal_error (__FILE__, __LINE__, _("invalid location type"));
600         }
601
602       if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
603         copy_bitwise (contents, dest_offset_bits,
604                       intermediate_buffer, source_offset_bits % 8,
605                       this_size_bits, bits_big_endian);
606
607       offset += this_size_bits;
608     }
609
610   do_cleanups (cleanup);
611 }
612
613 static void
614 write_pieced_value (struct value *to, struct value *from)
615 {
616   int i;
617   long offset = 0;
618   ULONGEST bits_to_skip;
619   const gdb_byte *contents;
620   struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
621   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
622   size_t type_len;
623   size_t buffer_size = 0;
624   char *buffer = NULL;
625   struct cleanup *cleanup;
626   int bits_big_endian
627     = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
628
629   if (frame == NULL)
630     {
631       set_value_optimized_out (to, 1);
632       return;
633     }
634
635   cleanup = make_cleanup (free_current_contents, &buffer);
636
637   contents = value_contents (from);
638   bits_to_skip = 8 * value_offset (to);
639   type_len = 8 * TYPE_LENGTH (value_type (to));
640   for (i = 0; i < c->n_pieces && offset < type_len; i++)
641     {
642       struct dwarf_expr_piece *p = &c->pieces[i];
643       size_t this_size_bits, this_size;
644       long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
645       int need_bitwise;
646       const gdb_byte *source_buffer;
647
648       this_size_bits = p->size;
649       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
650         {
651           bits_to_skip -= this_size_bits;
652           continue;
653         }
654       if (this_size_bits > type_len - offset)
655         this_size_bits = type_len - offset;
656       if (bits_to_skip > 0)
657         {
658           dest_offset_bits = bits_to_skip;
659           source_offset_bits = 0;
660           this_size_bits -= bits_to_skip;
661           bits_to_skip = 0;
662         }
663       else
664         {
665           dest_offset_bits = 0;
666           source_offset_bits = offset;
667         }
668
669       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
670       source_offset = source_offset_bits / 8;
671       dest_offset = dest_offset_bits / 8;
672       if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
673         {
674           source_buffer = contents + source_offset;
675           need_bitwise = 0;
676         }
677       else
678         {
679           if (buffer_size < this_size)
680             {
681               buffer_size = this_size;
682               buffer = xrealloc (buffer, buffer_size);
683             }
684           source_buffer = buffer;
685           need_bitwise = 1;
686         }
687
688       switch (p->location)
689         {
690         case DWARF_VALUE_REGISTER:
691           {
692             struct gdbarch *arch = get_frame_arch (frame);
693             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
694             int reg_offset = dest_offset;
695
696             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
697                 && this_size <= register_size (arch, gdb_regnum))
698               /* Big-endian, and we want less than full size.  */
699               reg_offset = register_size (arch, gdb_regnum) - this_size;
700
701             if (gdb_regnum != -1)
702               {
703                 if (need_bitwise)
704                   {
705                     get_frame_register_bytes (frame, gdb_regnum, reg_offset,
706                                               this_size, buffer);
707                     copy_bitwise (buffer, dest_offset_bits,
708                                   contents, source_offset_bits,
709                                   this_size_bits,
710                                   bits_big_endian);
711                   }
712
713                 put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
714                                           this_size, source_buffer);
715               }
716             else
717               {
718                 error (_("Unable to write to DWARF register number %s"),
719                        paddress (arch, p->v.expr.value));
720               }
721           }
722           break;
723         case DWARF_VALUE_MEMORY:
724           if (need_bitwise)
725             {
726               /* Only the first and last bytes can possibly have any
727                  bits reused.  */
728               read_memory (p->v.expr.value + dest_offset, buffer, 1);
729               read_memory (p->v.expr.value + dest_offset + this_size - 1,
730                            buffer + this_size - 1, 1);
731               copy_bitwise (buffer, dest_offset_bits,
732                             contents, source_offset_bits,
733                             this_size_bits,
734                             bits_big_endian);
735             }
736
737           write_memory (p->v.expr.value + dest_offset,
738                         source_buffer, this_size);
739           break;
740         default:
741           set_value_optimized_out (to, 1);
742           goto done;
743         }
744       offset += this_size_bits;
745     }
746
747  done:
748   do_cleanups (cleanup);
749 }
750
751 static void *
752 copy_pieced_value_closure (struct value *v)
753 {
754   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
755   
756   ++c->refc;
757   return c;
758 }
759
760 static void
761 free_pieced_value_closure (struct value *v)
762 {
763   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
764
765   --c->refc;
766   if (c->refc == 0)
767     {
768       xfree (c->pieces);
769       xfree (c);
770     }
771 }
772
773 /* Functions for accessing a variable described by DW_OP_piece.  */
774 static struct lval_funcs pieced_value_funcs = {
775   read_pieced_value,
776   write_pieced_value,
777   copy_pieced_value_closure,
778   free_pieced_value_closure
779 };
780
781 /* Evaluate a location description, starting at DATA and with length
782    SIZE, to find the current location of variable of TYPE in the context
783    of FRAME.  */
784
785 static struct value *
786 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
787                           const gdb_byte *data, unsigned short size,
788                           struct dwarf2_per_cu_data *per_cu)
789 {
790   struct value *retval;
791   struct dwarf_expr_baton baton;
792   struct dwarf_expr_context *ctx;
793   struct cleanup *old_chain;
794
795   if (size == 0)
796     {
797       retval = allocate_value (type);
798       VALUE_LVAL (retval) = not_lval;
799       set_value_optimized_out (retval, 1);
800       return retval;
801     }
802
803   baton.frame = frame;
804   baton.objfile = dwarf2_per_cu_objfile (per_cu);
805
806   ctx = new_dwarf_expr_context ();
807   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
808
809   ctx->gdbarch = get_objfile_arch (baton.objfile);
810   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
811   ctx->baton = &baton;
812   ctx->read_reg = dwarf_expr_read_reg;
813   ctx->read_mem = dwarf_expr_read_mem;
814   ctx->get_frame_base = dwarf_expr_frame_base;
815   ctx->get_frame_cfa = dwarf_expr_frame_cfa;
816   ctx->get_tls_address = dwarf_expr_tls_address;
817
818   dwarf_expr_eval (ctx, data, size);
819   if (ctx->num_pieces > 0)
820     {
821       struct piece_closure *c;
822       struct frame_id frame_id = get_frame_id (frame);
823
824       c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
825                                   ctx->addr_size);
826       retval = allocate_computed_value (type, &pieced_value_funcs, c);
827       VALUE_FRAME_ID (retval) = frame_id;
828     }
829   else
830     {
831       switch (ctx->location)
832         {
833         case DWARF_VALUE_REGISTER:
834           {
835             struct gdbarch *arch = get_frame_arch (frame);
836             CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
837             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
838
839             if (gdb_regnum != -1)
840               retval = value_from_register (type, gdb_regnum, frame);
841             else
842               error (_("Unable to access DWARF register number %s"),
843                      paddress (arch, dwarf_regnum));
844           }
845           break;
846
847         case DWARF_VALUE_MEMORY:
848           {
849             CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
850             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
851
852             retval = allocate_value (type);
853             VALUE_LVAL (retval) = lval_memory;
854             set_value_lazy (retval, 1);
855             if (in_stack_memory)
856               set_value_stack (retval, 1);
857             set_value_address (retval, address);
858           }
859           break;
860
861         case DWARF_VALUE_STACK:
862           {
863             ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
864             bfd_byte *contents;
865             size_t n = ctx->addr_size;
866
867             retval = allocate_value (type);
868             contents = value_contents_raw (retval);
869             if (n > TYPE_LENGTH (type))
870               n = TYPE_LENGTH (type);
871             store_unsigned_integer (contents, n,
872                                     gdbarch_byte_order (ctx->gdbarch),
873                                     value);
874           }
875           break;
876
877         case DWARF_VALUE_LITERAL:
878           {
879             bfd_byte *contents;
880             size_t n = ctx->len;
881
882             retval = allocate_value (type);
883             contents = value_contents_raw (retval);
884             if (n > TYPE_LENGTH (type))
885               n = TYPE_LENGTH (type);
886             memcpy (contents, ctx->data, n);
887           }
888           break;
889
890           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
891              it can only be encountered when making a piece.  */
892         case DWARF_VALUE_OPTIMIZED_OUT:
893         default:
894           internal_error (__FILE__, __LINE__, _("invalid location type"));
895         }
896     }
897
898   set_value_initialized (retval, ctx->initialized);
899
900   do_cleanups (old_chain);
901
902   return retval;
903 }
904 \f
905 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
906
907 struct needs_frame_baton
908 {
909   int needs_frame;
910 };
911
912 /* Reads from registers do require a frame.  */
913 static CORE_ADDR
914 needs_frame_read_reg (void *baton, int regnum)
915 {
916   struct needs_frame_baton *nf_baton = baton;
917
918   nf_baton->needs_frame = 1;
919   return 1;
920 }
921
922 /* Reads from memory do not require a frame.  */
923 static void
924 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
925 {
926   memset (buf, 0, len);
927 }
928
929 /* Frame-relative accesses do require a frame.  */
930 static void
931 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
932 {
933   static gdb_byte lit0 = DW_OP_lit0;
934   struct needs_frame_baton *nf_baton = baton;
935
936   *start = &lit0;
937   *length = 1;
938
939   nf_baton->needs_frame = 1;
940 }
941
942 /* CFA accesses require a frame.  */
943
944 static CORE_ADDR
945 needs_frame_frame_cfa (void *baton)
946 {
947   struct needs_frame_baton *nf_baton = baton;
948
949   nf_baton->needs_frame = 1;
950   return 1;
951 }
952
953 /* Thread-local accesses do require a frame.  */
954 static CORE_ADDR
955 needs_frame_tls_address (void *baton, CORE_ADDR offset)
956 {
957   struct needs_frame_baton *nf_baton = baton;
958
959   nf_baton->needs_frame = 1;
960   return 1;
961 }
962
963 /* Return non-zero iff the location expression at DATA (length SIZE)
964    requires a frame to evaluate.  */
965
966 static int
967 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
968                              struct dwarf2_per_cu_data *per_cu)
969 {
970   struct needs_frame_baton baton;
971   struct dwarf_expr_context *ctx;
972   int in_reg;
973   struct cleanup *old_chain;
974
975   baton.needs_frame = 0;
976
977   ctx = new_dwarf_expr_context ();
978   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
979
980   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
981   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
982   ctx->baton = &baton;
983   ctx->read_reg = needs_frame_read_reg;
984   ctx->read_mem = needs_frame_read_mem;
985   ctx->get_frame_base = needs_frame_frame_base;
986   ctx->get_frame_cfa = needs_frame_frame_cfa;
987   ctx->get_tls_address = needs_frame_tls_address;
988
989   dwarf_expr_eval (ctx, data, size);
990
991   in_reg = ctx->location == DWARF_VALUE_REGISTER;
992
993   if (ctx->num_pieces > 0)
994     {
995       int i;
996
997       /* If the location has several pieces, and any of them are in
998          registers, then we will need a frame to fetch them from.  */
999       for (i = 0; i < ctx->num_pieces; i++)
1000         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1001           in_reg = 1;
1002     }
1003
1004   do_cleanups (old_chain);
1005
1006   return baton.needs_frame || in_reg;
1007 }
1008
1009 /* This struct keeps track of the pieces that make up a multi-location
1010    object, for use in agent expression generation.  It is
1011    superficially similar to struct dwarf_expr_piece, but
1012    dwarf_expr_piece is designed for use in immediate evaluation, and
1013    does not, for example, have a way to record both base register and
1014    offset.  */
1015
1016 struct axs_var_loc
1017 {
1018   /* Memory vs register, etc */
1019   enum axs_lvalue_kind kind;
1020
1021   /* If non-zero, number of bytes in this fragment */
1022   unsigned bytes;
1023
1024   /* (GDB-numbered) reg, or base reg if >= 0 */
1025   int reg;
1026
1027   /* offset from reg */
1028   LONGEST offset;
1029 };
1030
1031 static const gdb_byte *
1032 dwarf2_tracepoint_var_loc (struct symbol *symbol,
1033                            struct agent_expr *ax,
1034                            struct axs_var_loc *loc,
1035                            struct gdbarch *gdbarch,
1036                            const gdb_byte *data, const gdb_byte *end)
1037 {
1038   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1039     {
1040       loc->kind = axs_lvalue_register;
1041       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1042       data += 1;
1043     }
1044   else if (data[0] == DW_OP_regx)
1045     {
1046       ULONGEST reg;
1047
1048       data = read_uleb128 (data + 1, end, &reg);
1049       loc->kind = axs_lvalue_register;
1050       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1051     }
1052   else if (data[0] == DW_OP_fbreg)
1053     {
1054       struct block *b;
1055       struct symbol *framefunc;
1056       int frame_reg = 0;
1057       LONGEST frame_offset;
1058       const gdb_byte *base_data;
1059       size_t base_size;
1060       LONGEST base_offset = 0;
1061
1062       b = block_for_pc (ax->scope);
1063
1064       if (!b)
1065         error (_("No block found for address"));
1066
1067       framefunc = block_linkage_function (b);
1068
1069       if (!framefunc)
1070         error (_("No function found for block"));
1071
1072       dwarf_expr_frame_base_1 (framefunc, ax->scope,
1073                                &base_data, &base_size);
1074
1075       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1076         {
1077           const gdb_byte *buf_end;
1078
1079           frame_reg = base_data[0] - DW_OP_breg0;
1080           buf_end = read_sleb128 (base_data + 1,
1081                                   base_data + base_size, &base_offset);
1082           if (buf_end != base_data + base_size)
1083             error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1084                    frame_reg, SYMBOL_PRINT_NAME (symbol));
1085         }
1086       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1087         {
1088           /* The frame base is just the register, with no offset.  */
1089           frame_reg = base_data[0] - DW_OP_reg0;
1090           base_offset = 0;
1091         }
1092       else
1093         {
1094           /* We don't know what to do with the frame base expression,
1095              so we can't trace this variable; give up.  */
1096           error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
1097                  SYMBOL_PRINT_NAME (symbol), base_data[0]);
1098         }
1099
1100       data = read_sleb128 (data + 1, end, &frame_offset);
1101
1102       loc->kind = axs_lvalue_memory;
1103       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1104       loc->offset = base_offset + frame_offset;
1105     }
1106   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
1107     {
1108       unsigned int reg;
1109       LONGEST offset;
1110
1111       reg = data[0] - DW_OP_breg0;
1112       data = read_sleb128 (data + 1, end, &offset);
1113
1114       loc->kind = axs_lvalue_memory;
1115       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1116       loc->offset = offset;
1117     }
1118   else
1119     error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
1120            data[0], SYMBOL_PRINT_NAME (symbol));
1121   
1122   return data;
1123 }
1124
1125 /* Given the location of a piece, issue bytecodes that will access it.  */
1126
1127 static void
1128 dwarf2_tracepoint_var_access (struct agent_expr *ax,
1129                               struct axs_value *value,
1130                               struct axs_var_loc *loc)
1131 {
1132   value->kind = loc->kind;
1133   
1134   switch (loc->kind)
1135     {
1136     case axs_lvalue_register:
1137       value->u.reg = loc->reg;
1138       break;
1139       
1140     case axs_lvalue_memory:
1141       ax_reg (ax, loc->reg);
1142       if (loc->offset)
1143         {
1144           ax_const_l (ax, loc->offset);
1145           ax_simple (ax, aop_add);
1146         }
1147       break;
1148       
1149     default:
1150       internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
1151     }
1152 }
1153
1154 static void
1155 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1156                            struct agent_expr *ax, struct axs_value *value,
1157                            const gdb_byte *data, int size)
1158 {
1159   const gdb_byte *end = data + size;
1160   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1161   /* In practice, a variable is not going to be spread across
1162      dozens of registers or memory locations.  If someone comes up
1163      with a real-world example, revisit this.  */
1164 #define MAX_FRAGS 16
1165   struct axs_var_loc fragments[MAX_FRAGS];
1166   int nfrags = 0, frag;
1167   int length = 0;
1168   int piece_ok = 0;
1169   int bad = 0;
1170   int first = 1;
1171       
1172   if (!data || size == 0)
1173     {
1174       value->optimized_out = 1;
1175       return;
1176     }
1177
1178   while (data < end)
1179     {
1180       if (!piece_ok)
1181         {
1182           if (nfrags == MAX_FRAGS)
1183             error (_("Too many pieces in location for \"%s\"."),
1184                    SYMBOL_PRINT_NAME (symbol));
1185
1186           fragments[nfrags].bytes = 0;
1187           data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
1188                                             gdbarch, data, end);
1189           nfrags++;
1190           piece_ok = 1;
1191         }
1192       else if (data[0] == DW_OP_piece)
1193         {
1194           ULONGEST bytes;
1195               
1196           data = read_uleb128 (data + 1, end, &bytes);
1197           /* Only deal with 4 byte fragments for now.  */
1198           if (bytes != 4)
1199             error (_("DW_OP_piece %s not supported in location for \"%s\"."),
1200                    pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
1201           fragments[nfrags - 1].bytes = bytes;
1202           length += bytes;
1203           piece_ok = 0;
1204         }
1205       else
1206         {
1207           bad = 1;
1208           break;
1209         }
1210     }
1211
1212   if (bad || data > end)
1213     error (_("Corrupted DWARF expression for \"%s\"."),
1214            SYMBOL_PRINT_NAME (symbol));
1215
1216   /* If single expression, no pieces, convert to external format.  */
1217   if (length == 0)
1218     {
1219       dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
1220       return;
1221     }
1222
1223   if (length != TYPE_LENGTH (value->type))
1224     error (_("Inconsistent piece information for \"%s\"."),
1225            SYMBOL_PRINT_NAME (symbol));
1226
1227   /* Emit bytecodes to assemble the pieces into a single stack entry.  */
1228
1229   for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
1230        nfrags--;
1231        (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
1232     {
1233       if (!first)
1234         {
1235           /* shift the previous fragment up 32 bits */
1236           ax_const_l (ax, 32);
1237           ax_simple (ax, aop_lsh);
1238         }
1239
1240       dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
1241
1242       switch (value->kind)
1243         {
1244         case axs_lvalue_register:
1245           ax_reg (ax, value->u.reg);
1246           break;
1247
1248         case axs_lvalue_memory:
1249           {
1250             extern int trace_kludge;  /* Ugh. */
1251
1252             gdb_assert (fragments[frag].bytes == 4);
1253             if (trace_kludge)
1254               ax_trace_quick (ax, 4);
1255             ax_simple (ax, aop_ref32);
1256           }
1257           break;
1258         }
1259
1260       if (!first)
1261         {
1262           /* or the new fragment into the previous */
1263           ax_zero_ext (ax, 32);
1264           ax_simple (ax, aop_bit_or);
1265         }
1266       first = 0;
1267     }
1268   value->kind = axs_rvalue;
1269 }
1270
1271 \f
1272 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1273    evaluator to calculate the location.  */
1274 static struct value *
1275 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1276 {
1277   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1278   struct value *val;
1279
1280   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1281                                   dlbaton->size, dlbaton->per_cu);
1282
1283   return val;
1284 }
1285
1286 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
1287 static int
1288 locexpr_read_needs_frame (struct symbol *symbol)
1289 {
1290   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1291
1292   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1293                                       dlbaton->per_cu);
1294 }
1295
1296 /* Return true if DATA points to the end of a piece.  END is one past
1297    the last byte in the expression.  */
1298
1299 static int
1300 piece_end_p (const gdb_byte *data, const gdb_byte *end)
1301 {
1302   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1303 }
1304
1305 /* Nicely describe a single piece of a location, returning an updated
1306    position in the bytecode sequence.  This function cannot recognize
1307    all locations; if a location is not recognized, it simply returns
1308    DATA.  */
1309
1310 static const gdb_byte *
1311 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1312                                  CORE_ADDR addr, struct objfile *objfile,
1313                                  const gdb_byte *data, const gdb_byte *end,
1314                                  unsigned int addr_size)
1315 {
1316   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1317   int regno;
1318
1319   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1320     {
1321       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1322       fprintf_filtered (stream, _("a variable in $%s"),
1323                         gdbarch_register_name (gdbarch, regno));
1324       data += 1;
1325     }
1326   else if (data[0] == DW_OP_regx)
1327     {
1328       ULONGEST reg;
1329
1330       data = read_uleb128 (data + 1, end, &reg);
1331       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1332       fprintf_filtered (stream, _("a variable in $%s"),
1333                         gdbarch_register_name (gdbarch, regno));
1334     }
1335   else if (data[0] == DW_OP_fbreg)
1336     {
1337       struct block *b;
1338       struct symbol *framefunc;
1339       int frame_reg = 0;
1340       LONGEST frame_offset;
1341       const gdb_byte *base_data, *new_data;
1342       size_t base_size;
1343       LONGEST base_offset = 0;
1344
1345       new_data = read_sleb128 (data + 1, end, &frame_offset);
1346       if (!piece_end_p (new_data, end))
1347         return data;
1348       data = new_data;
1349
1350       b = block_for_pc (addr);
1351
1352       if (!b)
1353         error (_("No block found for address for symbol \"%s\"."),
1354                SYMBOL_PRINT_NAME (symbol));
1355
1356       framefunc = block_linkage_function (b);
1357
1358       if (!framefunc)
1359         error (_("No function found for block for symbol \"%s\"."),
1360                SYMBOL_PRINT_NAME (symbol));
1361
1362       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1363
1364       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1365         {
1366           const gdb_byte *buf_end;
1367           
1368           frame_reg = base_data[0] - DW_OP_breg0;
1369           buf_end = read_sleb128 (base_data + 1,
1370                                   base_data + base_size, &base_offset);
1371           if (buf_end != base_data + base_size)
1372             error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1373                    frame_reg, SYMBOL_PRINT_NAME (symbol));
1374         }
1375       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1376         {
1377           /* The frame base is just the register, with no offset.  */
1378           frame_reg = base_data[0] - DW_OP_reg0;
1379           base_offset = 0;
1380         }
1381       else
1382         {
1383           /* We don't know what to do with the frame base expression,
1384              so we can't trace this variable; give up.  */
1385           error (_("Cannot describe location of symbol \"%s\"; "
1386                    "DWARF 2 encoding not handled, "
1387                    "first opcode in base data is 0x%x."),
1388                  SYMBOL_PRINT_NAME (symbol), base_data[0]);
1389         }
1390
1391       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1392
1393       fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1394                         gdbarch_register_name (gdbarch, regno),
1395                         plongest (base_offset), plongest (frame_offset));
1396     }
1397   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1398            && piece_end_p (data, end))
1399     {
1400       LONGEST offset;
1401
1402       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1403
1404       data = read_sleb128 (data + 1, end, &offset);
1405
1406       fprintf_filtered (stream,
1407                         _("a variable at offset %s from base reg $%s"),
1408                         plongest (offset),
1409                         gdbarch_register_name (gdbarch, regno));
1410     }
1411
1412   /* The location expression for a TLS variable looks like this (on a
1413      64-bit LE machine):
1414
1415      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1416                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1417      
1418      0x3 is the encoding for DW_OP_addr, which has an operand as long
1419      as the size of an address on the target machine (here is 8
1420      bytes).  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1421      The operand represents the offset at which the variable is within
1422      the thread local storage.  */
1423
1424   else if (data + 1 + addr_size < end
1425            && data[0] == DW_OP_addr
1426            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
1427            && piece_end_p (data + 2 + addr_size, end))
1428     {
1429       CORE_ADDR offset = dwarf2_read_address (gdbarch,
1430                                               data + 1,
1431                                               end,
1432                                               addr_size);
1433
1434       fprintf_filtered (stream, 
1435                         _("a thread-local variable at offset %s "
1436                           "in the thread-local storage for `%s'"),
1437                         paddress (gdbarch, offset), objfile->name);
1438
1439       data += 1 + addr_size + 1;
1440     }
1441   else if (data[0] >= DW_OP_lit0
1442            && data[0] <= DW_OP_lit31
1443            && data + 1 < end
1444            && data[1] == DW_OP_stack_value)
1445     {
1446       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
1447       data += 2;
1448     }
1449
1450   return data;
1451 }
1452
1453 /* Disassemble an expression, stopping at the end of a piece or at the
1454    end of the expression.  Returns a pointer to the next unread byte
1455    in the input expression.  If ALL is nonzero, then this function
1456    will keep going until it reaches the end of the expression.  */
1457
1458 static const gdb_byte *
1459 disassemble_dwarf_expression (struct ui_file *stream,
1460                               struct gdbarch *arch, unsigned int addr_size,
1461                               int offset_size,
1462                               const gdb_byte *data, const gdb_byte *end,
1463                               int all)
1464 {
1465   const gdb_byte *start = data;
1466
1467   fprintf_filtered (stream, _("a complex DWARF expression:\n"));
1468
1469   while (data < end
1470          && (all
1471              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
1472     {
1473       enum dwarf_location_atom op = *data++;
1474       CORE_ADDR addr;
1475       ULONGEST ul;
1476       LONGEST l;
1477       const char *name;
1478
1479       name = dwarf_stack_op_name (op, 0);
1480
1481       if (!name)
1482         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
1483                op, (long) (data - start));
1484       fprintf_filtered (stream, "  % 4ld: %s", (long) (data - start), name);
1485
1486       switch (op)
1487         {
1488         case DW_OP_addr:
1489           addr = dwarf2_read_address (arch, data, end, addr_size);
1490           data += addr_size;
1491           fprintf_filtered (stream, " %s", paddress (arch, addr));
1492           break;
1493
1494         case DW_OP_const1u:
1495           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
1496           data += 1;
1497           fprintf_filtered (stream, " %s", pulongest (ul));
1498           break;
1499         case DW_OP_const1s:
1500           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
1501           data += 1;
1502           fprintf_filtered (stream, " %s", plongest (l));
1503           break;
1504         case DW_OP_const2u:
1505           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1506           data += 2;
1507           fprintf_filtered (stream, " %s", pulongest (ul));
1508           break;
1509         case DW_OP_const2s:
1510           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1511           data += 2;
1512           fprintf_filtered (stream, " %s", plongest (l));
1513           break;
1514         case DW_OP_const4u:
1515           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1516           data += 4;
1517           fprintf_filtered (stream, " %s", pulongest (ul));
1518           break;
1519         case DW_OP_const4s:
1520           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
1521           data += 4;
1522           fprintf_filtered (stream, " %s", plongest (l));
1523           break;
1524         case DW_OP_const8u:
1525           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
1526           data += 8;
1527           fprintf_filtered (stream, " %s", pulongest (ul));
1528           break;
1529         case DW_OP_const8s:
1530           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
1531           data += 8;
1532           fprintf_filtered (stream, " %s", plongest (l));
1533           break;
1534         case DW_OP_constu:
1535           data = read_uleb128 (data, end, &ul);
1536           fprintf_filtered (stream, " %s", pulongest (ul));
1537           break;
1538         case DW_OP_consts:
1539           data = read_sleb128 (data, end, &ul);
1540           fprintf_filtered (stream, " %s", plongest (l));
1541           break;
1542
1543         case DW_OP_reg0:
1544         case DW_OP_reg1:
1545         case DW_OP_reg2:
1546         case DW_OP_reg3:
1547         case DW_OP_reg4:
1548         case DW_OP_reg5:
1549         case DW_OP_reg6:
1550         case DW_OP_reg7:
1551         case DW_OP_reg8:
1552         case DW_OP_reg9:
1553         case DW_OP_reg10:
1554         case DW_OP_reg11:
1555         case DW_OP_reg12:
1556         case DW_OP_reg13:
1557         case DW_OP_reg14:
1558         case DW_OP_reg15:
1559         case DW_OP_reg16:
1560         case DW_OP_reg17:
1561         case DW_OP_reg18:
1562         case DW_OP_reg19:
1563         case DW_OP_reg20:
1564         case DW_OP_reg21:
1565         case DW_OP_reg22:
1566         case DW_OP_reg23:
1567         case DW_OP_reg24:
1568         case DW_OP_reg25:
1569         case DW_OP_reg26:
1570         case DW_OP_reg27:
1571         case DW_OP_reg28:
1572         case DW_OP_reg29:
1573         case DW_OP_reg30:
1574         case DW_OP_reg31:
1575           fprintf_filtered (stream, " [$%s]",
1576                             gdbarch_register_name (arch, op - DW_OP_reg0));
1577           break;
1578
1579         case DW_OP_regx:
1580           data = read_uleb128 (data, end, &ul);
1581           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1582                             gdbarch_register_name (arch, (int) ul));
1583           break;
1584
1585         case DW_OP_implicit_value:
1586           data = read_uleb128 (data, end, &ul);
1587           data += ul;
1588           fprintf_filtered (stream, " %s", pulongest (ul));
1589           break;
1590
1591         case DW_OP_breg0:
1592         case DW_OP_breg1:
1593         case DW_OP_breg2:
1594         case DW_OP_breg3:
1595         case DW_OP_breg4:
1596         case DW_OP_breg5:
1597         case DW_OP_breg6:
1598         case DW_OP_breg7:
1599         case DW_OP_breg8:
1600         case DW_OP_breg9:
1601         case DW_OP_breg10:
1602         case DW_OP_breg11:
1603         case DW_OP_breg12:
1604         case DW_OP_breg13:
1605         case DW_OP_breg14:
1606         case DW_OP_breg15:
1607         case DW_OP_breg16:
1608         case DW_OP_breg17:
1609         case DW_OP_breg18:
1610         case DW_OP_breg19:
1611         case DW_OP_breg20:
1612         case DW_OP_breg21:
1613         case DW_OP_breg22:
1614         case DW_OP_breg23:
1615         case DW_OP_breg24:
1616         case DW_OP_breg25:
1617         case DW_OP_breg26:
1618         case DW_OP_breg27:
1619         case DW_OP_breg28:
1620         case DW_OP_breg29:
1621         case DW_OP_breg30:
1622         case DW_OP_breg31:
1623           data = read_sleb128 (data, end, &ul);
1624           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1625                             gdbarch_register_name (arch, op - DW_OP_breg0));
1626           break;
1627
1628         case DW_OP_bregx:
1629           {
1630             ULONGEST offset;
1631
1632             data = read_uleb128 (data, end, &ul);
1633             data = read_sleb128 (data, end, &offset);
1634             fprintf_filtered (stream, " register %s [$%s] offset %s",
1635                               pulongest (ul),
1636                               gdbarch_register_name (arch, (int) ul),
1637                               pulongest (offset));
1638           }
1639           break;
1640
1641         case DW_OP_fbreg:
1642           data = read_sleb128 (data, end, &ul);
1643           fprintf_filtered (stream, " %s", pulongest (ul));
1644           break;
1645
1646         case DW_OP_xderef_size:
1647         case DW_OP_deref_size:
1648         case DW_OP_pick:
1649           fprintf_filtered (stream, " %d", *data);
1650           ++data;
1651           break;
1652
1653         case DW_OP_plus_uconst:
1654           data = read_uleb128 (data, end, &ul);
1655           fprintf_filtered (stream, " %s", pulongest (ul));
1656           break;
1657
1658         case DW_OP_skip:
1659           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1660           data += 2;
1661           fprintf_filtered (stream, " to %ld",
1662                             (long) (data + l - start));
1663           break;
1664
1665         case DW_OP_bra:
1666           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1667           data += 2;
1668           fprintf_filtered (stream, " %ld",
1669                             (long) (data + l - start));
1670           break;
1671
1672         case DW_OP_call2:
1673           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1674           data += 2;
1675           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
1676           break;
1677
1678         case DW_OP_call4:
1679           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1680           data += 4;
1681           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
1682           break;
1683
1684         case DW_OP_call_ref:
1685           ul = extract_unsigned_integer (data, offset_size,
1686                                          gdbarch_byte_order (arch));
1687           data += offset_size;
1688           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
1689           break;
1690
1691         case DW_OP_piece:
1692           data = read_uleb128 (data, end, &ul);
1693           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
1694           break;
1695
1696         case DW_OP_bit_piece:
1697           {
1698             ULONGEST offset;
1699
1700             data = read_uleb128 (data, end, &ul);
1701             data = read_uleb128 (data, end, &offset);
1702             fprintf_filtered (stream, " size %s offset %s (bits)",
1703                               pulongest (ul), pulongest (offset));
1704           }
1705           break;
1706         }
1707
1708       fprintf_filtered (stream, "\n");
1709     }
1710
1711   return data;
1712 }
1713
1714 /* Describe a single location, which may in turn consist of multiple
1715    pieces.  */
1716
1717 static void
1718 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
1719                              struct ui_file *stream,
1720                              const gdb_byte *data, int size,
1721                              struct objfile *objfile, unsigned int addr_size,
1722                              int offset_size)
1723 {
1724   const gdb_byte *end = data + size;
1725   int first_piece = 1, bad = 0;
1726
1727   while (data < end)
1728     {
1729       const gdb_byte *here = data;
1730       int disassemble = 1;
1731
1732       if (first_piece)
1733         first_piece = 0;
1734       else
1735         fprintf_filtered (stream, _(", and "));
1736
1737       if (!dwarf2_always_disassemble)
1738         {
1739           data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
1740                                                   data, end, addr_size);
1741           /* If we printed anything, or if we have an empty piece,
1742              then don't disassemble.  */
1743           if (data != here
1744               || data[0] == DW_OP_piece
1745               || data[0] == DW_OP_bit_piece)
1746             disassemble = 0;
1747         }
1748       if (disassemble)
1749         data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
1750                                              addr_size, offset_size, data, end,
1751                                              dwarf2_always_disassemble);
1752
1753       if (data < end)
1754         {
1755           int empty = data == here;
1756               
1757           if (disassemble)
1758             fprintf_filtered (stream, "   ");
1759           if (data[0] == DW_OP_piece)
1760             {
1761               ULONGEST bytes;
1762
1763               data = read_uleb128 (data + 1, end, &bytes);
1764
1765               if (empty)
1766                 fprintf_filtered (stream, _("an empty %s-byte piece"),
1767                                   pulongest (bytes));
1768               else
1769                 fprintf_filtered (stream, _(" [%s-byte piece]"),
1770                                   pulongest (bytes));
1771             }
1772           else if (data[0] == DW_OP_bit_piece)
1773             {
1774               ULONGEST bits, offset;
1775
1776               data = read_uleb128 (data + 1, end, &bits);
1777               data = read_uleb128 (data, end, &offset);
1778
1779               if (empty)
1780                 fprintf_filtered (stream,
1781                                   _("an empty %s-bit piece"),
1782                                   pulongest (bits));
1783               else
1784                 fprintf_filtered (stream,
1785                                   _(" [%s-bit piece, offset %s bits]"),
1786                                   pulongest (bits), pulongest (offset));
1787             }
1788           else
1789             {
1790               bad = 1;
1791               break;
1792             }
1793         }
1794     }
1795
1796   if (bad || data > end)
1797     error (_("Corrupted DWARF2 expression for \"%s\"."),
1798            SYMBOL_PRINT_NAME (symbol));
1799 }
1800
1801 /* Print a natural-language description of SYMBOL to STREAM.  This
1802    version is for a symbol with a single location.  */
1803
1804 static void
1805 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1806                            struct ui_file *stream)
1807 {
1808   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1809   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1810   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1811   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
1812
1813   locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
1814                                objfile, addr_size, offset_size);
1815 }
1816
1817 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1818    any necessary bytecode in AX.  */
1819
1820 static void
1821 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1822                             struct agent_expr *ax, struct axs_value *value)
1823 {
1824   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1825
1826   dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1827                              dlbaton->data, dlbaton->size);
1828 }
1829
1830 /* The set of location functions used with the DWARF-2 expression
1831    evaluator.  */
1832 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
1833   locexpr_read_variable,
1834   locexpr_read_needs_frame,
1835   locexpr_describe_location,
1836   locexpr_tracepoint_var_ref
1837 };
1838
1839
1840 /* Wrapper functions for location lists.  These generally find
1841    the appropriate location expression and call something above.  */
1842
1843 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1844    evaluator to calculate the location.  */
1845 static struct value *
1846 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1847 {
1848   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1849   struct value *val;
1850   const gdb_byte *data;
1851   size_t size;
1852
1853   data = find_location_expression (dlbaton, &size,
1854                                    frame ? get_frame_address_in_block (frame)
1855                                    : 0);
1856   if (data == NULL)
1857     {
1858       val = allocate_value (SYMBOL_TYPE (symbol));
1859       VALUE_LVAL (val) = not_lval;
1860       set_value_optimized_out (val, 1);
1861     }
1862   else
1863     val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
1864                                     dlbaton->per_cu);
1865
1866   return val;
1867 }
1868
1869 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
1870 static int
1871 loclist_read_needs_frame (struct symbol *symbol)
1872 {
1873   /* If there's a location list, then assume we need to have a frame
1874      to choose the appropriate location expression.  With tracking of
1875      global variables this is not necessarily true, but such tracking
1876      is disabled in GCC at the moment until we figure out how to
1877      represent it.  */
1878
1879   return 1;
1880 }
1881
1882 /* Print a natural-language description of SYMBOL to STREAM.  This
1883    version applies when there is a list of different locations, each
1884    with a specified address range.  */
1885
1886 static void
1887 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1888                            struct ui_file *stream)
1889 {
1890   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1891   CORE_ADDR low, high;
1892   const gdb_byte *loc_ptr, *buf_end;
1893   int length, first = 1;
1894   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1895   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1896   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1897   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1898   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
1899   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1900   /* Adjust base_address for relocatable objects.  */
1901   CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1902                                     SECT_OFF_TEXT (objfile));
1903   CORE_ADDR base_address = dlbaton->base_address + base_offset;
1904
1905   loc_ptr = dlbaton->data;
1906   buf_end = dlbaton->data + dlbaton->size;
1907
1908   fprintf_filtered (stream, _("multi-location:\n"));
1909
1910   /* Iterate through locations until we run out.  */
1911   while (1)
1912     {
1913       if (buf_end - loc_ptr < 2 * addr_size)
1914         error (_("Corrupted DWARF expression for symbol \"%s\"."),
1915                SYMBOL_PRINT_NAME (symbol));
1916
1917       low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1918       loc_ptr += addr_size;
1919
1920       /* A base-address-selection entry.  */
1921       if (low == base_mask)
1922         {
1923           base_address = dwarf2_read_address (gdbarch,
1924                                               loc_ptr, buf_end, addr_size);
1925           fprintf_filtered (stream, _("  Base address %s"),
1926                             paddress (gdbarch, base_address));
1927           loc_ptr += addr_size;
1928           continue;
1929         }
1930
1931       high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1932       loc_ptr += addr_size;
1933
1934       /* An end-of-list entry.  */
1935       if (low == 0 && high == 0)
1936         break;
1937
1938       /* Otherwise, a location expression entry.  */
1939       low += base_address;
1940       high += base_address;
1941
1942       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1943       loc_ptr += 2;
1944
1945       /* (It would improve readability to print only the minimum
1946          necessary digits of the second number of the range.)  */
1947       fprintf_filtered (stream, _("  Range %s-%s: "),
1948                         paddress (gdbarch, low), paddress (gdbarch, high));
1949
1950       /* Now describe this particular location.  */
1951       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
1952                                    objfile, addr_size, offset_size);
1953
1954       fprintf_filtered (stream, "\n");
1955
1956       loc_ptr += length;
1957     }
1958 }
1959
1960 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1961    any necessary bytecode in AX.  */
1962 static void
1963 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1964                             struct agent_expr *ax, struct axs_value *value)
1965 {
1966   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1967   const gdb_byte *data;
1968   size_t size;
1969
1970   data = find_location_expression (dlbaton, &size, ax->scope);
1971
1972   dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
1973 }
1974
1975 /* The set of location functions used with the DWARF-2 expression
1976    evaluator and location lists.  */
1977 const struct symbol_computed_ops dwarf2_loclist_funcs = {
1978   loclist_read_variable,
1979   loclist_read_needs_frame,
1980   loclist_describe_location,
1981   loclist_tracepoint_var_ref
1982 };