forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarf2loc.c
4543 lines (3825 loc) · 128 KB
/
dwarf2loc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* DWARF 2 location expression support for GDB.
Copyright (C) 2003-2015 Free Software Foundation, Inc.
Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "ui-out.h"
#include "value.h"
#include "frame.h"
#include "gdbcore.h"
#include "target.h"
#include "inferior.h"
#include "ax.h"
#include "ax-gdb.h"
#include "regcache.h"
#include "objfiles.h"
#include "block.h"
#include "gdbcmd.h"
#include "dwarf2.h"
#include "dwarf2expr.h"
#include "dwarf2loc.h"
#include "dwarf2-frame.h"
#include "compile/compile.h"
extern int dwarf_always_disassemble;
extern const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
struct frame_info *frame,
const gdb_byte *data,
size_t size,
struct dwarf2_per_cu_data *per_cu,
LONGEST byte_offset);
/* Until these have formal names, we define these here.
ref: http://gcc.gnu.org/wiki/DebugFission
Each entry in .debug_loc.dwo begins with a byte that describes the entry,
and is then followed by data specific to that entry. */
enum debug_loc_kind
{
/* Indicates the end of the list of entries. */
DEBUG_LOC_END_OF_LIST = 0,
/* This is followed by an unsigned LEB128 number that is an index into
.debug_addr and specifies the base address for all following entries. */
DEBUG_LOC_BASE_ADDRESS = 1,
/* This is followed by two unsigned LEB128 numbers that are indices into
.debug_addr and specify the beginning and ending addresses, and then
a normal location expression as in .debug_loc. */
DEBUG_LOC_START_END = 2,
/* This is followed by an unsigned LEB128 number that is an index into
.debug_addr and specifies the beginning address, and a 4 byte unsigned
number that specifies the length, and then a normal location expression
as in .debug_loc. */
DEBUG_LOC_START_LENGTH = 3,
/* An internal value indicating there is insufficient data. */
DEBUG_LOC_BUFFER_OVERFLOW = -1,
/* An internal value indicating an invalid kind of entry was found. */
DEBUG_LOC_INVALID_ENTRY = -2
};
/* Helper function which throws an error if a synthetic pointer is
invalid. */
static void
invalid_synthetic_pointer (void)
{
error (_("access outside bounds of object "
"referenced via synthetic pointer"));
}
/* Decode the addresses in a non-dwo .debug_loc entry.
A pointer to the next byte to examine is returned in *NEW_PTR.
The encoded low,high addresses are return in *LOW,*HIGH.
The result indicates the kind of entry found. */
static enum debug_loc_kind
decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
const gdb_byte **new_ptr,
CORE_ADDR *low, CORE_ADDR *high,
enum bfd_endian byte_order,
unsigned int addr_size,
int signed_addr_p)
{
CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
if (buf_end - loc_ptr < 2 * addr_size)
return DEBUG_LOC_BUFFER_OVERFLOW;
if (signed_addr_p)
*low = extract_signed_integer (loc_ptr, addr_size, byte_order);
else
*low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
loc_ptr += addr_size;
if (signed_addr_p)
*high = extract_signed_integer (loc_ptr, addr_size, byte_order);
else
*high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
loc_ptr += addr_size;
*new_ptr = loc_ptr;
/* A base-address-selection entry. */
if ((*low & base_mask) == base_mask)
return DEBUG_LOC_BASE_ADDRESS;
/* An end-of-list entry. */
if (*low == 0 && *high == 0)
return DEBUG_LOC_END_OF_LIST;
return DEBUG_LOC_START_END;
}
/* Decode the addresses in .debug_loc.dwo entry.
A pointer to the next byte to examine is returned in *NEW_PTR.
The encoded low,high addresses are return in *LOW,*HIGH.
The result indicates the kind of entry found. */
static enum debug_loc_kind
decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
const gdb_byte *loc_ptr,
const gdb_byte *buf_end,
const gdb_byte **new_ptr,
CORE_ADDR *low, CORE_ADDR *high,
enum bfd_endian byte_order)
{
uint64_t low_index, high_index;
if (loc_ptr == buf_end)
return DEBUG_LOC_BUFFER_OVERFLOW;
switch (*loc_ptr++)
{
case DEBUG_LOC_END_OF_LIST:
*new_ptr = loc_ptr;
return DEBUG_LOC_END_OF_LIST;
case DEBUG_LOC_BASE_ADDRESS:
*low = 0;
loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
if (loc_ptr == NULL)
return DEBUG_LOC_BUFFER_OVERFLOW;
*high = dwarf2_read_addr_index (per_cu, high_index);
*new_ptr = loc_ptr;
return DEBUG_LOC_BASE_ADDRESS;
case DEBUG_LOC_START_END:
loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
if (loc_ptr == NULL)
return DEBUG_LOC_BUFFER_OVERFLOW;
*low = dwarf2_read_addr_index (per_cu, low_index);
loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
if (loc_ptr == NULL)
return DEBUG_LOC_BUFFER_OVERFLOW;
*high = dwarf2_read_addr_index (per_cu, high_index);
*new_ptr = loc_ptr;
return DEBUG_LOC_START_END;
case DEBUG_LOC_START_LENGTH:
loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
if (loc_ptr == NULL)
return DEBUG_LOC_BUFFER_OVERFLOW;
*low = dwarf2_read_addr_index (per_cu, low_index);
if (loc_ptr + 4 > buf_end)
return DEBUG_LOC_BUFFER_OVERFLOW;
*high = *low;
*high += extract_unsigned_integer (loc_ptr, 4, byte_order);
*new_ptr = loc_ptr + 4;
return DEBUG_LOC_START_LENGTH;
default:
return DEBUG_LOC_INVALID_ENTRY;
}
}
/* A function for dealing with location lists. Given a
symbol baton (BATON) and a pc value (PC), find the appropriate
location expression, set *LOCEXPR_LENGTH, and return a pointer
to the beginning of the expression. Returns NULL on failure.
For now, only return the first matching location expression; there
can be more than one in the list. */
const gdb_byte *
dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
size_t *locexpr_length, CORE_ADDR pc)
{
struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
struct gdbarch *gdbarch = get_objfile_arch (objfile);
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
/* Adjust base_address for relocatable objects. */
CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
CORE_ADDR base_address = baton->base_address + base_offset;
const gdb_byte *loc_ptr, *buf_end;
loc_ptr = baton->data;
buf_end = baton->data + baton->size;
while (1)
{
CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
int length;
enum debug_loc_kind kind;
const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
if (baton->from_dwo)
kind = decode_debug_loc_dwo_addresses (baton->per_cu,
loc_ptr, buf_end, &new_ptr,
&low, &high, byte_order);
else
kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
&low, &high,
byte_order, addr_size,
signed_addr_p);
loc_ptr = new_ptr;
switch (kind)
{
case DEBUG_LOC_END_OF_LIST:
*locexpr_length = 0;
return NULL;
case DEBUG_LOC_BASE_ADDRESS:
base_address = high + base_offset;
continue;
case DEBUG_LOC_START_END:
case DEBUG_LOC_START_LENGTH:
break;
case DEBUG_LOC_BUFFER_OVERFLOW:
case DEBUG_LOC_INVALID_ENTRY:
error (_("dwarf2_find_location_expression: "
"Corrupted DWARF expression."));
default:
gdb_assert_not_reached ("bad debug_loc_kind");
}
/* Otherwise, a location expression entry.
If the entry is from a DWO, don't add base address: the entry is
from .debug_addr which has absolute addresses. */
if (! baton->from_dwo)
{
low += base_address;
high += base_address;
}
length = extract_unsigned_integer (loc_ptr, 2, byte_order);
loc_ptr += 2;
if (low == high && pc == low)
{
/* This is entry PC record present only at entry point
of a function. Verify it is really the function entry point. */
const struct block *pc_block = block_for_pc (pc);
struct symbol *pc_func = NULL;
if (pc_block)
pc_func = block_linkage_function (pc_block);
if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
{
*locexpr_length = length;
return loc_ptr;
}
}
if (pc >= low && pc < high)
{
*locexpr_length = length;
return loc_ptr;
}
loc_ptr += length;
}
}
/* This is the baton used when performing dwarf2 expression
evaluation. */
struct dwarf_expr_baton
{
struct frame_info *frame;
struct dwarf2_per_cu_data *per_cu;
CORE_ADDR obj_address;
};
/* Helper functions for dwarf2_evaluate_loc_desc. */
/* Using the frame specified in BATON, return the value of register
REGNUM, treated as a pointer. */
static CORE_ADDR
dwarf_expr_read_addr_from_reg (void *baton, int dwarf_regnum)
{
struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
return address_from_register (regnum, debaton->frame);
}
/* Implement struct dwarf_expr_context_funcs' "get_reg_value" callback. */
static struct value *
dwarf_expr_get_reg_value (void *baton, struct type *type, int dwarf_regnum)
{
struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
return value_from_register (type, regnum, debaton->frame);
}
/* Read memory at ADDR (length LEN) into BUF. */
static void
dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
{
read_memory (addr, buf, len);
}
/* Using the frame specified in BATON, find the location expression
describing the frame base. Return a pointer to it in START and
its length in LENGTH. */
static void
dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
{
/* FIXME: cagney/2003-03-26: This code should be using
get_frame_base_address(), and then implement a dwarf2 specific
this_base method. */
struct symbol *framefunc;
struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
const struct block *bl = get_frame_block (debaton->frame, NULL);
if (bl == NULL)
error (_("frame address is not available."));
/* Use block_linkage_function, which returns a real (not inlined)
function, instead of get_frame_function, which may return an
inlined function. */
framefunc = block_linkage_function (bl);
/* If we found a frame-relative symbol then it was certainly within
some function associated with a frame. If we can't find the frame,
something has gone wrong. */
gdb_assert (framefunc != NULL);
func_get_frame_base_dwarf_block (framefunc,
get_frame_address_in_block (debaton->frame),
start, length);
}
/* Implement find_frame_base_location method for LOC_BLOCK functions using
DWARF expression for its DW_AT_frame_base. */
static void
locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
const gdb_byte **start, size_t *length)
{
struct dwarf2_locexpr_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
*length = symbaton->size;
*start = symbaton->data;
}
/* Implement the struct symbol_block_ops::get_frame_base method. */
static CORE_ADDR
block_op_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
{
struct gdbarch *gdbarch;
struct type *type;
struct dwarf2_locexpr_baton *dlbaton;
const gdb_byte *start;
size_t length;
struct value *result;
/* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
Thus, it's supposed to provide the find_frame_base_location method as
well. */
gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
gdbarch = get_frame_arch (frame);
type = builtin_type (gdbarch)->builtin_data_ptr;
dlbaton = SYMBOL_LOCATION_BATON (framefunc);
SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
(framefunc, get_frame_pc (frame), &start, &length);
result = dwarf2_evaluate_loc_desc (type, frame, start, length,
dlbaton->per_cu);
/* The DW_AT_frame_base attribute contains a location description which
computes the base address itself. However, the call to
dwarf2_evaluate_loc_desc returns a value representing a variable at
that address. The frame base address is thus this variable's
address. */
return value_address (result);
}
/* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
function uses DWARF expression for its DW_AT_frame_base. */
const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
{
locexpr_find_frame_base_location,
block_op_get_frame_base
};
/* Implement find_frame_base_location method for LOC_BLOCK functions using
DWARF location list for its DW_AT_frame_base. */
static void
loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
const gdb_byte **start, size_t *length)
{
struct dwarf2_loclist_baton *symbaton = SYMBOL_LOCATION_BATON (framefunc);
*start = dwarf2_find_location_expression (symbaton, length, pc);
}
/* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
function uses DWARF location list for its DW_AT_frame_base. */
const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
{
loclist_find_frame_base_location,
block_op_get_frame_base
};
/* See dwarf2loc.h. */
void
func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
const gdb_byte **start, size_t *length)
{
if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
{
const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
ops_block->find_frame_base_location (framefunc, pc, start, length);
}
else
*length = 0;
if (*length == 0)
error (_("Could not find the frame base for \"%s\"."),
SYMBOL_NATURAL_NAME (framefunc));
}
/* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
the frame in BATON. */
static CORE_ADDR
dwarf_expr_frame_cfa (void *baton)
{
struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
return dwarf2_frame_cfa (debaton->frame);
}
/* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
the frame in BATON. */
static CORE_ADDR
dwarf_expr_frame_pc (void *baton)
{
struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
return get_frame_address_in_block (debaton->frame);
}
/* Using the objfile specified in BATON, find the address for the
current thread's thread-local storage with offset OFFSET. */
static CORE_ADDR
dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
{
struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
return target_translate_tls_address (objfile, offset);
}
/* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
current CU (as is PER_CU). State of the CTX is not affected by the
call and return. */
static void
per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
struct dwarf2_per_cu_data *per_cu,
CORE_ADDR (*get_frame_pc) (void *baton),
void *baton)
{
struct dwarf2_locexpr_baton block;
block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
/* DW_OP_call_ref is currently not supported. */
gdb_assert (block.per_cu == per_cu);
dwarf_expr_eval (ctx, block.data, block.size);
}
/* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
static void
dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
{
struct dwarf_expr_baton *debaton = ctx->baton;
per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
ctx->funcs->get_frame_pc, ctx->baton);
}
/* Callback function for dwarf2_evaluate_loc_desc. */
static struct type *
dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
cu_offset die_offset)
{
struct dwarf_expr_baton *debaton = ctx->baton;
return dwarf2_get_die_type (die_offset, debaton->per_cu);
}
/* See dwarf2loc.h. */
unsigned int entry_values_debug = 0;
/* Helper to set entry_values_debug. */
static void
show_entry_values_debug (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file,
_("Entry values and tail call frames debugging is %s.\n"),
value);
}
/* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
CALLER_FRAME (for registers) can be NULL if it is not known. This function
always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
static CORE_ADDR
call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
struct call_site *call_site,
struct frame_info *caller_frame)
{
switch (FIELD_LOC_KIND (call_site->target))
{
case FIELD_LOC_KIND_DWARF_BLOCK:
{
struct dwarf2_locexpr_baton *dwarf_block;
struct value *val;
struct type *caller_core_addr_type;
struct gdbarch *caller_arch;
dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
if (dwarf_block == NULL)
{
struct bound_minimal_symbol msym;
msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
throw_error (NO_ENTRY_VALUE_ERROR,
_("DW_AT_GNU_call_site_target is not specified "
"at %s in %s"),
paddress (call_site_gdbarch, call_site->pc),
(msym.minsym == NULL ? "???"
: MSYMBOL_PRINT_NAME (msym.minsym)));
}
if (caller_frame == NULL)
{
struct bound_minimal_symbol msym;
msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
throw_error (NO_ENTRY_VALUE_ERROR,
_("DW_AT_GNU_call_site_target DWARF block resolving "
"requires known frame which is currently not "
"available at %s in %s"),
paddress (call_site_gdbarch, call_site->pc),
(msym.minsym == NULL ? "???"
: MSYMBOL_PRINT_NAME (msym.minsym)));
}
caller_arch = get_frame_arch (caller_frame);
caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
dwarf_block->data, dwarf_block->size,
dwarf_block->per_cu);
/* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
location. */
if (VALUE_LVAL (val) == lval_memory)
return value_address (val);
else
return value_as_address (val);
}
case FIELD_LOC_KIND_PHYSNAME:
{
const char *physname;
struct bound_minimal_symbol msym;
physname = FIELD_STATIC_PHYSNAME (call_site->target);
/* Handle both the mangled and demangled PHYSNAME. */
msym = lookup_minimal_symbol (physname, NULL, NULL);
if (msym.minsym == NULL)
{
msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
throw_error (NO_ENTRY_VALUE_ERROR,
_("Cannot find function \"%s\" for a call site target "
"at %s in %s"),
physname, paddress (call_site_gdbarch, call_site->pc),
(msym.minsym == NULL ? "???"
: MSYMBOL_PRINT_NAME (msym.minsym)));
}
return BMSYMBOL_VALUE_ADDRESS (msym);
}
case FIELD_LOC_KIND_PHYSADDR:
return FIELD_STATIC_PHYSADDR (call_site->target);
default:
internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
}
}
/* Convert function entry point exact address ADDR to the function which is
compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
NO_ENTRY_VALUE_ERROR otherwise. */
static struct symbol *
func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
{
struct symbol *sym = find_pc_function (addr);
struct type *type;
if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
throw_error (NO_ENTRY_VALUE_ERROR,
_("DW_TAG_GNU_call_site resolving failed to find function "
"name for address %s"),
paddress (gdbarch, addr));
type = SYMBOL_TYPE (sym);
gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
return sym;
}
/* Verify function with entry point exact address ADDR can never call itself
via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
can call itself via tail calls.
If a funtion can tail call itself its entry value based parameters are
unreliable. There is no verification whether the value of some/all
parameters is unchanged through the self tail call, we expect if there is
a self tail call all the parameters can be modified. */
static void
func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
{
struct obstack addr_obstack;
struct cleanup *old_chain;
CORE_ADDR addr;
/* Track here CORE_ADDRs which were already visited. */
htab_t addr_hash;
/* The verification is completely unordered. Track here function addresses
which still need to be iterated. */
VEC (CORE_ADDR) *todo = NULL;
obstack_init (&addr_obstack);
old_chain = make_cleanup_obstack_free (&addr_obstack);
addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
&addr_obstack, hashtab_obstack_allocate,
NULL);
make_cleanup_htab_delete (addr_hash);
make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
VEC_safe_push (CORE_ADDR, todo, verify_addr);
while (!VEC_empty (CORE_ADDR, todo))
{
struct symbol *func_sym;
struct call_site *call_site;
addr = VEC_pop (CORE_ADDR, todo);
func_sym = func_addr_to_tail_call_list (gdbarch, addr);
for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
call_site; call_site = call_site->tail_call_next)
{
CORE_ADDR target_addr;
void **slot;
/* CALLER_FRAME with registers is not available for tail-call jumped
frames. */
target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
if (target_addr == verify_addr)
{
struct bound_minimal_symbol msym;
msym = lookup_minimal_symbol_by_pc (verify_addr);
throw_error (NO_ENTRY_VALUE_ERROR,
_("DW_OP_GNU_entry_value resolving has found "
"function \"%s\" at %s can call itself via tail "
"calls"),
(msym.minsym == NULL ? "???"
: MSYMBOL_PRINT_NAME (msym.minsym)),
paddress (gdbarch, verify_addr));
}
slot = htab_find_slot (addr_hash, &target_addr, INSERT);
if (*slot == NULL)
{
*slot = obstack_copy (&addr_obstack, &target_addr,
sizeof (target_addr));
VEC_safe_push (CORE_ADDR, todo, target_addr);
}
}
}
do_cleanups (old_chain);
}
/* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
ENTRY_VALUES_DEBUG. */
static void
tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
{
CORE_ADDR addr = call_site->pc;
struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
(msym.minsym == NULL ? "???"
: MSYMBOL_PRINT_NAME (msym.minsym)));
}
/* vec.h needs single word type name, typedef it. */
typedef struct call_site *call_sitep;
/* Define VEC (call_sitep) functions. */
DEF_VEC_P (call_sitep);
/* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
only top callers and bottom callees which are present in both. GDBARCH is
used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
no remaining possibilities to provide unambiguous non-trivial result.
RESULTP should point to NULL on the first (initialization) call. Caller is
responsible for xfree of any RESULTP data. */
static void
chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
VEC (call_sitep) *chain)
{
struct call_site_chain *result = *resultp;
long length = VEC_length (call_sitep, chain);
int callers, callees, idx;
if (result == NULL)
{
/* Create the initial chain containing all the passed PCs. */
result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
* (length - 1));
result->length = length;
result->callers = result->callees = length;
if (!VEC_empty (call_sitep, chain))
memcpy (result->call_site, VEC_address (call_sitep, chain),
sizeof (*result->call_site) * length);
*resultp = result;
if (entry_values_debug)
{
fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
for (idx = 0; idx < length; idx++)
tailcall_dump (gdbarch, result->call_site[idx]);
fputc_unfiltered ('\n', gdb_stdlog);
}
return;
}
if (entry_values_debug)
{
fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
for (idx = 0; idx < length; idx++)
tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
fputc_unfiltered ('\n', gdb_stdlog);
}
/* Intersect callers. */
callers = min (result->callers, length);
for (idx = 0; idx < callers; idx++)
if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
{
result->callers = idx;
break;
}
/* Intersect callees. */
callees = min (result->callees, length);
for (idx = 0; idx < callees; idx++)
if (result->call_site[result->length - 1 - idx]
!= VEC_index (call_sitep, chain, length - 1 - idx))
{
result->callees = idx;
break;
}
if (entry_values_debug)
{
fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
for (idx = 0; idx < result->callers; idx++)
tailcall_dump (gdbarch, result->call_site[idx]);
fputs_unfiltered (" |", gdb_stdlog);
for (idx = 0; idx < result->callees; idx++)
tailcall_dump (gdbarch, result->call_site[result->length
- result->callees + idx]);
fputc_unfiltered ('\n', gdb_stdlog);
}
if (result->callers == 0 && result->callees == 0)
{
/* There are no common callers or callees. It could be also a direct
call (which has length 0) with ambiguous possibility of an indirect
call - CALLERS == CALLEES == 0 is valid during the first allocation
but any subsequence processing of such entry means ambiguity. */
xfree (result);
*resultp = NULL;
return;
}
/* See call_site_find_chain_1 why there is no way to reach the bottom callee
PC again. In such case there must be two different code paths to reach
it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
gdb_assert (result->callers + result->callees <= result->length);
}
/* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
assumed frames between them use GDBARCH. Use depth first search so we can
keep single CHAIN of call_site's back to CALLER_PC. Function recursion
would have needless GDB stack overhead. Caller is responsible for xfree of
the returned result. Any unreliability results in thrown
NO_ENTRY_VALUE_ERROR. */
static struct call_site_chain *
call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
CORE_ADDR callee_pc)
{
CORE_ADDR save_callee_pc = callee_pc;
struct obstack addr_obstack;
struct cleanup *back_to_retval, *back_to_workdata;
struct call_site_chain *retval = NULL;
struct call_site *call_site;
/* Mark CALL_SITEs so we do not visit the same ones twice. */
htab_t addr_hash;
/* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
call_site nor any possible call_site at CALLEE_PC's function is there.
Any CALL_SITE in CHAIN will be iterated to its siblings - via
TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
VEC (call_sitep) *chain = NULL;
/* We are not interested in the specific PC inside the callee function. */
callee_pc = get_pc_function_start (callee_pc);
if (callee_pc == 0)
throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
paddress (gdbarch, save_callee_pc));
back_to_retval = make_cleanup (free_current_contents, &retval);
obstack_init (&addr_obstack);
back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
&addr_obstack, hashtab_obstack_allocate,
NULL);
make_cleanup_htab_delete (addr_hash);
make_cleanup (VEC_cleanup (call_sitep), &chain);
/* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
at the target's function. All the possible tail call sites in the
target's function will get iterated as already pushed into CHAIN via their
TAIL_CALL_NEXT. */
call_site = call_site_for_pc (gdbarch, caller_pc);
while (call_site)
{
CORE_ADDR target_func_addr;
struct call_site *target_call_site;
/* CALLER_FRAME with registers is not available for tail-call jumped
frames. */
target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
if (target_func_addr == callee_pc)
{
chain_candidate (gdbarch, &retval, chain);
if (retval == NULL)
break;
/* There is no way to reach CALLEE_PC again as we would prevent
entering it twice as being already marked in ADDR_HASH. */
target_call_site = NULL;
}
else
{
struct symbol *target_func;
target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
}
do
{
/* Attempt to visit TARGET_CALL_SITE. */
if (target_call_site)
{
void **slot;
slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
if (*slot == NULL)
{
/* Successfully entered TARGET_CALL_SITE. */
*slot = &target_call_site->pc;
VEC_safe_push (call_sitep, chain, target_call_site);
break;
}
}
/* Backtrack (without revisiting the originating call_site). Try the
callers's sibling; if there isn't any try the callers's callers's
sibling etc. */
target_call_site = NULL;
while (!VEC_empty (call_sitep, chain))
{
call_site = VEC_pop (call_sitep, chain);
gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
NO_INSERT) != NULL);
htab_remove_elt (addr_hash, &call_site->pc);
target_call_site = call_site->tail_call_next;
if (target_call_site)
break;
}
}
while (target_call_site);
if (VEC_empty (call_sitep, chain))
call_site = NULL;
else
call_site = VEC_last (call_sitep, chain);
}
if (retval == NULL)
{
struct bound_minimal_symbol msym_caller, msym_callee;
msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
throw_error (NO_ENTRY_VALUE_ERROR,
_("There are no unambiguously determinable intermediate "
"callers or callees between caller function \"%s\" at %s "
"and callee function \"%s\" at %s"),
(msym_caller.minsym == NULL
? "???" : MSYMBOL_PRINT_NAME (msym_caller.minsym)),