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