forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarf2dbg.c
3315 lines (2808 loc) · 86.1 KB
/
dwarf2dbg.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
/* dwarf2dbg.c - DWARF2 debug support
Copyright (C) 1999-2025 Free Software Foundation, Inc.
Contributed by David Mosberger-Tang <[email protected]>
This file is part of GAS, the GNU Assembler.
GAS 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, or (at your option)
any later version.
GAS 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 GAS; see the file COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
/* Logical line numbers can be controlled by the compiler via the
following directives:
.file FILENO "file.c"
.loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \
[epilogue_begin] [is_stmt VALUE] [isa VALUE] \
[discriminator VALUE] [view VALUE]
*/
#include "as.h"
#include "safe-ctype.h"
#include <limits.h>
#include "dwarf2dbg.h"
#include <filenames.h>
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
/* We need to decide which character to use as a directory separator.
Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not
necessarily mean that the backslash character is the one to use.
Some environments, eg Cygwin, can support both naming conventions.
So we use the heuristic that we only need to use the backslash if
the path is an absolute path starting with a DOS style drive
selector. eg C: or D: */
# define INSERT_DIR_SEPARATOR(string, offset) \
do \
{ \
if (offset > 1 \
&& string[0] != 0 \
&& string[1] == ':') \
string [offset] = '\\'; \
else \
string [offset] = '/'; \
} \
while (0)
#else
# define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/'
#endif
#ifndef DWARF2_FORMAT
# define DWARF2_FORMAT(SEC) dwarf2_format_32bit
#endif
#ifndef DWARF2_ADDR_SIZE
# define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
#endif
#ifndef DWARF2_FILE_NAME
#define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME
#endif
#ifndef DWARF2_FILE_TIME_NAME
#define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) -1
#endif
#ifndef DWARF2_FILE_SIZE_NAME
#define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) -1
#endif
#ifndef DWARF2_VERSION
#define DWARF2_VERSION dwarf_level
#endif
/* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */
#ifndef DWARF2_ARANGES_VERSION
#define DWARF2_ARANGES_VERSION 2
#endif
/* The .debug_line version is the same as the .debug_info version. */
#ifndef DWARF2_LINE_VERSION
#define DWARF2_LINE_VERSION DWARF2_VERSION
#endif
/* The .debug_rnglists has only been in DWARF version 5. */
#ifndef DWARF2_RNGLISTS_VERSION
#define DWARF2_RNGLISTS_VERSION 5
#endif
#include "subsegs.h"
#include "dwarf2.h"
/* Since we can't generate the prolog until the body is complete, we
use three different subsegments for .debug_line: one holding the
prolog, one for the directory and filename info, and one for the
body ("statement program"). */
#define DL_PROLOG 0
#define DL_FILES 1
#define DL_BODY 2
/* If linker relaxation might change offsets in the code, the DWARF special
opcodes and variable-length operands cannot be used. If this macro is
nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */
#ifndef DWARF2_USE_FIXED_ADVANCE_PC
# define DWARF2_USE_FIXED_ADVANCE_PC linkrelax
#endif
/* First special line opcode - leave room for the standard opcodes.
Note: If you want to change this, you'll have to update the
"standard_opcode_lengths" table that is emitted below in
out_debug_line(). */
#define DWARF2_LINE_OPCODE_BASE (DWARF2_LINE_VERSION == 2 ? 10 : 13)
#ifndef DWARF2_LINE_BASE
/* Minimum line offset in a special line info. opcode. This value
was chosen to give a reasonable range of values. */
# define DWARF2_LINE_BASE -5
#endif
/* Range of line offsets in a special line info. opcode. */
#ifndef DWARF2_LINE_RANGE
# define DWARF2_LINE_RANGE 14
#endif
#ifndef DWARF2_LINE_MIN_INSN_LENGTH
/* Define the architecture-dependent minimum instruction length (in
bytes). This value should be rather too small than too big. */
# define DWARF2_LINE_MIN_INSN_LENGTH 1
#endif
/* Flag that indicates the initial value of the is_stmt_start flag. */
#define DWARF2_LINE_DEFAULT_IS_STMT 1
#ifndef DWARF2_LINE_MAX_OPS_PER_INSN
#define DWARF2_LINE_MAX_OPS_PER_INSN 1
#endif
/* Given a special op, return the line skip amount. */
#define SPECIAL_LINE(op) \
(((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE)
/* Given a special op, return the address skip amount (in units of
DWARF2_LINE_MIN_INSN_LENGTH. */
#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
/* The maximum address skip amount that can be encoded with a special op. */
#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
#ifndef TC_PARSE_CONS_RETURN_NONE
#define TC_PARSE_CONS_RETURN_NONE BFD_RELOC_NONE
#endif
#define GAS_ABBREV_COMP_UNIT 1
#define GAS_ABBREV_SUBPROG 2
#define GAS_ABBREV_NO_TYPE 3
struct line_entry
{
struct line_entry *next;
symbolS *label;
struct dwarf2_line_info loc;
};
/* Don't change the offset of next in line_entry. set_or_check_view
calls in dwarf2_gen_line_info_1 depend on it. */
static char unused[offsetof(struct line_entry, next) ? -1 : 1]
ATTRIBUTE_UNUSED;
struct line_subseg
{
struct line_subseg *next;
subsegT subseg;
struct line_entry *head;
struct line_entry **ptail;
struct line_entry **pmove_tail;
};
struct line_seg
{
struct line_seg *next;
segT seg;
struct line_subseg *head;
symbolS *text_start;
symbolS *text_end;
};
/* Collects data for all line table entries during assembly. */
static struct line_seg *all_segs;
static struct line_seg **last_seg_ptr;
#define NUM_MD5_BYTES 16
struct file_entry
{
const char * filename;
unsigned int dir;
unsigned char md5[NUM_MD5_BYTES];
};
/* Table of files used by .debug_line. */
static struct file_entry *files;
static unsigned int files_in_use;
static unsigned int files_allocated;
/* Table of directories used by .debug_line. */
static char ** dirs;
static unsigned int dirs_in_use;
static unsigned int dirs_allocated;
/* TRUE when we've seen a .loc directive recently. Used to avoid
doing work when there's nothing to do. Will be reset by
dwarf2_consume_line_info. */
bool dwarf2_loc_directive_seen;
/* TRUE when we've seen any .loc directive at any time during parsing.
Indicates the user wants us to generate a .debug_line section.
Used in dwarf2_finish as sanity check. */
static bool dwarf2_any_loc_directive_seen;
/* TRUE when we're supposed to set the basic block mark whenever a
label is seen. */
bool dwarf2_loc_mark_labels;
/* Current location as indicated by the most recent .loc directive. */
static struct dwarf2_line_info current;
/* This symbol is used to recognize view number forced resets in loc
lists. */
static symbolS *force_reset_view;
/* This symbol evaluates to an expression that, if nonzero, indicates
some view assert check failed. */
static symbolS *view_assert_failed;
/* The size of an address on the target. */
static unsigned int sizeof_address;
#ifndef TC_DWARF2_EMIT_OFFSET
#define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
/* Create an offset to .dwarf2_*. */
static void
generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
{
expressionS exp;
memset (&exp, 0, sizeof exp);
exp.X_op = O_symbol;
exp.X_add_symbol = symbol;
exp.X_add_number = 0;
emit_expr (&exp, size);
}
#endif
/* Find or create (if CREATE_P) an entry for SEG+SUBSEG in ALL_SEGS. */
static struct line_subseg *
get_line_subseg (segT seg, subsegT subseg, bool create_p)
{
struct line_seg *s = seg_info (seg)->dwarf2_line_seg;
struct line_subseg **pss, *lss;
if (s == NULL)
{
if (!create_p)
return NULL;
s = XNEW (struct line_seg);
s->next = NULL;
s->seg = seg;
s->head = NULL;
*last_seg_ptr = s;
last_seg_ptr = &s->next;
seg_info (seg)->dwarf2_line_seg = s;
}
gas_assert (seg == s->seg);
for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next)
{
if (lss->subseg == subseg)
goto found_subseg;
if (lss->subseg > subseg)
break;
}
lss = XNEW (struct line_subseg);
lss->next = *pss;
lss->subseg = subseg;
lss->head = NULL;
lss->ptail = &lss->head;
lss->pmove_tail = &lss->head;
*pss = lss;
found_subseg:
return lss;
}
/* (Un)reverse the line_entry list starting from H. */
static struct line_entry *
reverse_line_entry_list (struct line_entry *h)
{
struct line_entry *p = NULL, *e, *n;
for (e = h; e; e = n)
{
n = e->next;
e->next = p;
p = e;
}
return p;
}
/* Compute the view for E based on the previous entry P. If we
introduce an (undefined) view symbol for P, and H is given (P must
be the tail in this case), introduce view symbols for earlier list
entries as well, until one of them is constant. */
static void
set_or_check_view (struct line_entry *e, struct line_entry *p,
struct line_entry *h)
{
expressionS viewx;
memset (&viewx, 0, sizeof (viewx));
viewx.X_unsigned = 1;
/* First, compute !(E->label > P->label), to tell whether or not
we're to reset the view number. If we can't resolve it to a
constant, keep it symbolic. */
if (!p || (e->loc.u.view == force_reset_view && force_reset_view))
{
viewx.X_op = O_constant;
viewx.X_add_number = 0;
viewx.X_add_symbol = NULL;
viewx.X_op_symbol = NULL;
}
else
{
viewx.X_op = O_gt;
viewx.X_add_number = 0;
viewx.X_add_symbol = e->label;
viewx.X_op_symbol = p->label;
resolve_expression (&viewx);
if (viewx.X_op == O_constant)
viewx.X_add_number = !viewx.X_add_number;
else
{
viewx.X_add_symbol = make_expr_symbol (&viewx);
viewx.X_add_number = 0;
viewx.X_op_symbol = NULL;
viewx.X_op = O_logical_not;
}
}
if (S_IS_DEFINED (e->loc.u.view) && symbol_constant_p (e->loc.u.view))
{
expressionS *value = symbol_get_value_expression (e->loc.u.view);
/* We can't compare the view numbers at this point, because in
VIEWX we've only determined whether we're to reset it so
far. */
if (viewx.X_op == O_constant)
{
if (!value->X_add_number != !viewx.X_add_number)
as_bad (_("view number mismatch"));
}
/* Record the expression to check it later. It is the result of
a logical not, thus 0 or 1. We just add up all such deferred
expressions, and resolve it at the end. */
else if (!value->X_add_number)
{
symbolS *deferred = make_expr_symbol (&viewx);
if (view_assert_failed)
{
expressionS chk;
memset (&chk, 0, sizeof (chk));
chk.X_unsigned = 1;
chk.X_op = O_add;
chk.X_add_number = 0;
chk.X_add_symbol = view_assert_failed;
chk.X_op_symbol = deferred;
deferred = make_expr_symbol (&chk);
}
view_assert_failed = deferred;
}
}
if (viewx.X_op != O_constant || viewx.X_add_number)
{
expressionS incv;
expressionS *p_view;
if (!p->loc.u.view)
p->loc.u.view = symbol_temp_make ();
memset (&incv, 0, sizeof (incv));
incv.X_unsigned = 1;
incv.X_op = O_symbol;
incv.X_add_symbol = p->loc.u.view;
incv.X_add_number = 1;
p_view = symbol_get_value_expression (p->loc.u.view);
if (p_view->X_op == O_constant || p_view->X_op == O_symbol)
{
/* If we can, constant fold increments so that a chain of
expressions v + 1 + 1 ... + 1 is not created.
resolve_expression isn't ideal for this purpose. The
base v might not be resolvable until later. */
incv.X_op = p_view->X_op;
incv.X_add_symbol = p_view->X_add_symbol;
incv.X_add_number = p_view->X_add_number + 1;
}
if (viewx.X_op == O_constant)
{
gas_assert (viewx.X_add_number == 1);
viewx = incv;
}
else
{
viewx.X_add_symbol = make_expr_symbol (&viewx);
viewx.X_add_number = 0;
viewx.X_op_symbol = make_expr_symbol (&incv);
viewx.X_op = O_multiply;
}
}
if (!S_IS_DEFINED (e->loc.u.view))
{
symbol_set_value_expression (e->loc.u.view, &viewx);
S_SET_SEGMENT (e->loc.u.view, expr_section);
symbol_set_frag (e->loc.u.view, &zero_address_frag);
}
/* Define and attempt to simplify any earlier views needed to
compute E's. */
if (h && p && p->loc.u.view && !S_IS_DEFINED (p->loc.u.view))
{
struct line_entry *h2;
/* Reverse the list to avoid quadratic behavior going backwards
in a single-linked list. */
struct line_entry *r = reverse_line_entry_list (h);
gas_assert (r == p);
/* Set or check views until we find a defined or absent view. */
do
{
/* Do not define the head of a (sub?)segment view while
handling others. It would be defined too early, without
regard to the last view of other subsegments.
set_or_check_view will be called for every head segment
that needs it. */
if (r == h)
break;
set_or_check_view (r, r->next, NULL);
}
while (r->next
&& r->next->loc.u.view
&& !S_IS_DEFINED (r->next->loc.u.view)
&& (r = r->next));
/* Unreverse the list, so that we can go forward again. */
h2 = reverse_line_entry_list (p);
gas_assert (h2 == h);
/* Starting from the last view we just defined, attempt to
simplify the view expressions, until we do so to P. */
do
{
/* The head view of a subsegment may remain undefined while
handling other elements, before it is linked to the last
view of the previous subsegment. */
if (r == h)
continue;
gas_assert (S_IS_DEFINED (r->loc.u.view));
resolve_expression (symbol_get_value_expression (r->loc.u.view));
}
while (r != p && (r = r->next));
/* Now that we've defined and computed all earlier views that might
be needed to compute E's, attempt to simplify it. */
resolve_expression (symbol_get_value_expression (e->loc.u.view));
}
}
/* Record an entry for LOC occurring at LABEL. */
static void
dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
{
struct line_subseg *lss;
struct line_entry *e;
flagword need_flags = SEC_LOAD | SEC_CODE;
/* PR 26850: Do not record LOCs in non-executable or non-loaded
sections. SEC_ALLOC isn't tested for non-ELF because obj-coff.c
obj_coff_section is careless in setting SEC_ALLOC. */
if (IS_ELF)
need_flags |= SEC_ALLOC;
if ((now_seg->flags & need_flags) != need_flags)
{
/* FIXME: Add code to suppress multiple warnings ? */
if (debug_type != DEBUG_DWARF2)
as_warn ("dwarf line number information for %s ignored",
segment_name (now_seg));
return;
}
e = XNEW (struct line_entry);
e->next = NULL;
e->label = label;
e->loc = *loc;
lss = get_line_subseg (now_seg, now_subseg, true);
/* Subseg heads are chained to previous subsegs in
dwarf2_finish. */
if (loc->filenum != -1u && loc->u.view && lss->head)
set_or_check_view (e, (struct line_entry *) lss->ptail, lss->head);
*lss->ptail = e;
lss->ptail = &e->next;
}
/* Record an entry for LOC occurring at OFS within the current fragment. */
static unsigned int dw2_line;
static const char *dw2_filename;
static int label_num;
void
dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc)
{
symbolS *sym;
/* Early out for as-yet incomplete location information. */
if (loc->line == 0)
return;
if (loc->filenum == 0)
{
if (dwarf_level < 5)
dwarf_level = 5;
if (DWARF2_LINE_VERSION < 5)
return;
}
/* Don't emit sequences of line symbols for the same line when the
symbols apply to assembler code. It is necessary to emit
duplicate line symbols when a compiler asks for them, because GDB
uses them to determine the end of the prologue. */
if (debug_type == DEBUG_DWARF2)
{
if (dw2_line == loc->line)
{
if (dw2_filename == loc->u.filename)
return;
if (filename_cmp (dw2_filename, loc->u.filename) == 0)
{
dw2_filename = loc->u.filename;
return;
}
}
dw2_line = loc->line;
dw2_filename = loc->u.filename;
}
if (linkrelax)
{
char name[32];
/* Use a non-fake name for the line number location,
so that it can be referred to by relocations. */
sprintf (name, ".Loc.%u", label_num);
label_num++;
sym = symbol_new (name, now_seg, frag_now, ofs);
}
else
sym = symbol_temp_new (now_seg, frag_now, ofs);
dwarf2_gen_line_info_1 (sym, loc);
}
static const char *
get_basename (const char * pathname)
{
const char * file;
file = lbasename (pathname);
/* Don't make empty string from / or A: from A:/ . */
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
if (file <= pathname + 3)
file = pathname;
#else
if (file == pathname + 1)
file = pathname;
#endif
return file;
}
static unsigned int
get_directory_table_entry (const char *dirname,
const char *file0_dirname,
size_t dirlen,
bool can_use_zero)
{
unsigned int d;
if (dirlen == 0)
return 0;
#ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR
if (IS_DIR_SEPARATOR (dirname[dirlen - 1]))
{
-- dirlen;
if (dirlen == 0)
return 0;
}
#endif
for (d = 0; d < dirs_in_use; ++d)
{
if (dirs[d] != NULL
&& filename_ncmp (dirname, dirs[d], dirlen) == 0
&& dirs[d][dirlen] == '\0')
return d;
}
if (can_use_zero)
{
if (dirs == NULL || dirs[0] == NULL)
{
const char * pwd = file0_dirname ? file0_dirname : getpwd ();
if (dwarf_level >= 5 && filename_cmp (dirname, pwd) != 0)
{
/* In DWARF-5 the 0 entry in the directory table is
expected to be the same as the DW_AT_comp_dir (which
is set to the current build directory). Since we are
about to create a directory entry that is not the
same, allocate the current directory first. */
(void) get_directory_table_entry (pwd, file0_dirname,
strlen (pwd), true);
d = 1;
}
else
d = 0;
}
}
else if (d == 0)
d = 1;
if (d >= dirs_allocated)
{
unsigned int old = dirs_allocated;
#define DIR_TABLE_INCREMENT 32
dirs_allocated = d + DIR_TABLE_INCREMENT;
dirs = XRESIZEVEC (char *, dirs, dirs_allocated);
memset (dirs + old, 0, (dirs_allocated - old) * sizeof (char *));
}
dirs[d] = xmemdup0 (dirname, dirlen);
if (dirs_in_use <= d)
dirs_in_use = d + 1;
return d;
}
static bool
assign_file_to_slot (unsigned int i, const char *file, unsigned int dir)
{
if (i >= files_allocated)
{
unsigned int want = i + 32;
/* Catch wraparound. */
if (want < files_allocated
|| want < i
|| want > UINT_MAX / sizeof (struct file_entry))
{
as_bad (_("file number %u is too big"), i);
return false;
}
files = XRESIZEVEC (struct file_entry, files, want);
memset (files + files_allocated, 0,
(want - files_allocated) * sizeof (struct file_entry));
files_allocated = want;
}
files[i].filename = file;
files[i].dir = dir;
memset (files[i].md5, 0, NUM_MD5_BYTES);
if (files_in_use < i + 1)
files_in_use = i + 1;
return true;
}
/* Get a .debug_line file number for PATHNAME. If there is a
directory component to PATHNAME, then this will be stored
in the directory table, if it is not already present.
Returns the slot number allocated to that filename or -1
if there was a problem. */
static int last_used;
static int last_used_dir_len;
static signed int
allocate_filenum (const char * pathname)
{
const char *file;
size_t dir_len;
unsigned int i, dir;
/* Short circuit the common case of adding the same pathname
as last time. */
if (last_used != -1)
{
const char * dirname = NULL;
if (dirs != NULL)
dirname = dirs[files[last_used].dir];
if (dirname == NULL)
{
if (filename_cmp (pathname, files[last_used].filename) == 0)
return last_used;
}
else
{
if (filename_ncmp (pathname, dirname, last_used_dir_len - 1) == 0
&& IS_DIR_SEPARATOR (pathname [last_used_dir_len - 1])
&& filename_cmp (pathname + last_used_dir_len,
files[last_used].filename) == 0)
return last_used;
}
}
file = get_basename (pathname);
dir_len = file - pathname;
dir = get_directory_table_entry (pathname, NULL, dir_len, false);
/* Do not use slot-0. That is specifically reserved for use by
the '.file 0 "name"' directive. */
for (i = 1; i < files_in_use; ++i)
if (files[i].dir == dir
&& files[i].filename
&& filename_cmp (file, files[i].filename) == 0)
{
last_used = i;
last_used_dir_len = dir_len;
return i;
}
if (!assign_file_to_slot (i, file, dir))
return -1;
last_used = i;
last_used_dir_len = dir_len;
return i;
}
/* Run through the list of line entries starting at E, allocating
file entries for gas generated debug. */
static void
do_allocate_filenum (struct line_entry *e)
{
do
{
if (e->loc.filenum == -1u)
{
e->loc.filenum = allocate_filenum (e->loc.u.filename);
e->loc.u.view = NULL;
}
e = e->next;
}
while (e);
}
/* Remove any generated line entries. These don't live comfortably
with compiler generated line info. If THELOT then remove
everything, freeing all list entries we have created. */
static void
purge_generated_debug (bool thelot)
{
struct line_seg *s, *nexts;
for (s = all_segs; s; s = nexts)
{
struct line_subseg *lss, *nextlss;
for (lss = s->head; lss; lss = nextlss)
{
struct line_entry *e, *next;
for (e = lss->head; e; e = next)
{
if (!thelot)
know (e->loc.filenum == -1u);
next = e->next;
free (e);
}
lss->head = NULL;
lss->ptail = &lss->head;
lss->pmove_tail = &lss->head;
nextlss = lss->next;
if (thelot)
free (lss);
}
nexts = s->next;
if (thelot)
{
seg_info (s->seg)->dwarf2_line_seg = NULL;
free (s);
}
}
}
/* Allocate slot NUM in the .debug_line file table to FILENAME.
If DIRNAME is not NULL or there is a directory component to FILENAME
then this will be stored in the directory table, if not already present.
if WITH_MD5 is TRUE then there is a md5 value in generic_bignum.
Returns TRUE if allocation succeeded, FALSE otherwise. */
static bool
allocate_filename_to_slot (const char *dirname,
const char *filename,
unsigned int num,
bool with_md5)
{
const char *file;
size_t dirlen;
unsigned int i, d;
const char *file0_dirname;
/* Short circuit the common case of adding the same pathname
as last time. */
if (num < files_allocated && files[num].filename != NULL)
{
const char * dir = NULL;
if (dirs != NULL)
dir = dirs[files[num].dir];
if (with_md5
&& memcmp (generic_bignum, files[num].md5, NUM_MD5_BYTES) != 0)
goto fail;
if (dirname != NULL)
{
if (dir != NULL && filename_cmp (dir, dirname) != 0)
goto fail;
if (filename_cmp (filename, files[num].filename) != 0)
goto fail;
/* If the filenames match, but the directory table entry was
empty, then fill it with the provided directory name. */
if (dir == NULL)
{
if (dirs == NULL)
{
dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT;
dirs = XCNEWVEC (char *, dirs_allocated);
}
dirs[files[num].dir] = xmemdup0 (dirname, strlen (dirname));
if (dirs_in_use <= files[num].dir)
dirs_in_use = files[num].dir + 1;
}
return true;
}
else if (dir != NULL)
{
dirlen = strlen (dir);
if (filename_ncmp (filename, dir, dirlen) == 0
&& IS_DIR_SEPARATOR (filename [dirlen])
&& filename_cmp (filename + dirlen + 1, files[num].filename) == 0)
return true;
}
else /* dir == NULL */
{
file = get_basename (filename);
if (filename_cmp (file, files[num].filename) == 0)
{
/* The filenames match, but the directory table entry is empty.
Fill it with the provided directory name. */
if (file > filename)
{
if (dirs == NULL)
{
dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT;
dirs = XCNEWVEC (char *, dirs_allocated);
}
dirs[files[num].dir] = xmemdup0 (filename, file - filename);
if (dirs_in_use <= files[num].dir)
dirs_in_use = files[num].dir + 1;
}
return true;
}
}
fail:
as_bad (_("file table slot %u is already occupied by a different file (%s%s%s vs %s%s%s)"),
num,
dir == NULL ? "" : dir,
dir == NULL ? "" : "/",
files[num].filename,
dirname == NULL ? "" : dirname,
dirname == NULL ? "" : "/",
filename);
return false;
}
/* For file .0, the directory name is the current directory and the file
may be in another directory contained in the file name. */
if (num == 0)
{
file0_dirname = dirname;
file = get_basename (filename);
if (dirname && file == filename)
dirlen = strlen (dirname);
else
{
dirname = filename;
dirlen = file - filename;
}
}
else
{
file0_dirname = NULL;
if (dirname == NULL)
{
dirname = filename;
file = get_basename (filename);
dirlen = file - filename;
}
else
{
dirlen = strlen (dirname);
file = filename;
}
}
d = get_directory_table_entry (dirname, file0_dirname, dirlen, num == 0);
i = num;
if (! assign_file_to_slot (i, file, d))
return false;
if (with_md5)
{
if (target_big_endian)
{
/* md5's are stored in litte endian format. */
unsigned int bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR;
unsigned int byte = NUM_MD5_BYTES;
unsigned int bignum_index = 0;
while (bits_remaining)
{
unsigned int bignum_bits_remaining = LITTLENUM_NUMBER_OF_BITS;
valueT bignum_value = generic_bignum [bignum_index];
bignum_index ++;
while (bignum_bits_remaining)
{
files[i].md5[--byte] = bignum_value & 0xff;
bignum_value >>= 8;
bignum_bits_remaining -= 8;
bits_remaining -= 8;
}
}
}
else
{
unsigned int bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR;