1/* dwarf2dbg.c - DWARF2 debug support 2 Copyright (C) 1999-2022 Free Software Foundation, Inc. 3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com> 4 5 This file is part of GAS, the GNU Assembler. 6 7 GAS is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 GAS is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GAS; see the file COPYING. If not, write to the Free 19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 20 02110-1301, USA. */ 21 22/* Logical line numbers can be controlled by the compiler via the 23 following directives: 24 25 .file FILENO "file.c" 26 .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \ 27 [epilogue_begin] [is_stmt VALUE] [isa VALUE] \ 28 [discriminator VALUE] 29*/ 30 31#include "as.h" 32#include "safe-ctype.h" 33#include <limits.h> 34#include "dwarf2dbg.h" 35#include <filenames.h> 36 37#ifdef HAVE_DOS_BASED_FILE_SYSTEM 38/* We need to decide which character to use as a directory separator. 39 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not 40 necessarily mean that the backslash character is the one to use. 41 Some environments, eg Cygwin, can support both naming conventions. 42 So we use the heuristic that we only need to use the backslash if 43 the path is an absolute path starting with a DOS style drive 44 selector. eg C: or D: */ 45# define INSERT_DIR_SEPARATOR(string, offset) \ 46 do \ 47 { \ 48 if (offset > 1 \ 49 && string[0] != 0 \ 50 && string[1] == ':') \ 51 string [offset] = '\\'; \ 52 else \ 53 string [offset] = '/'; \ 54 } \ 55 while (0) 56#else 57# define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/' 58#endif 59 60#ifndef DWARF2_FORMAT 61# define DWARF2_FORMAT(SEC) dwarf2_format_32bit 62#endif 63 64#ifndef DWARF2_ADDR_SIZE 65# define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8) 66#endif 67 68#ifndef DWARF2_FILE_NAME 69#define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME 70#endif 71 72#ifndef DWARF2_FILE_TIME_NAME 73#define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) -1 74#endif 75 76#ifndef DWARF2_FILE_SIZE_NAME 77#define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) -1 78#endif 79 80#ifndef DWARF2_VERSION 81#define DWARF2_VERSION dwarf_level 82#endif 83 84/* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */ 85#ifndef DWARF2_ARANGES_VERSION 86#define DWARF2_ARANGES_VERSION 2 87#endif 88 89/* This implementation outputs version 3 .debug_line information. */ 90#ifndef DWARF2_LINE_VERSION 91#define DWARF2_LINE_VERSION (dwarf_level > 3 ? dwarf_level : 3) 92#endif 93 94/* The .debug_rnglists has only been in DWARF version 5. */ 95#ifndef DWARF2_RNGLISTS_VERSION 96#define DWARF2_RNGLISTS_VERSION 5 97#endif 98 99#include "subsegs.h" 100 101#include "dwarf2.h" 102 103/* Since we can't generate the prolog until the body is complete, we 104 use three different subsegments for .debug_line: one holding the 105 prolog, one for the directory and filename info, and one for the 106 body ("statement program"). */ 107#define DL_PROLOG 0 108#define DL_FILES 1 109#define DL_BODY 2 110 111/* If linker relaxation might change offsets in the code, the DWARF special 112 opcodes and variable-length operands cannot be used. If this macro is 113 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */ 114#ifndef DWARF2_USE_FIXED_ADVANCE_PC 115# define DWARF2_USE_FIXED_ADVANCE_PC linkrelax 116#endif 117 118/* First special line opcode - leave room for the standard opcodes. 119 Note: If you want to change this, you'll have to update the 120 "standard_opcode_lengths" table that is emitted below in 121 out_debug_line(). */ 122#define DWARF2_LINE_OPCODE_BASE 13 123 124#ifndef DWARF2_LINE_BASE 125 /* Minimum line offset in a special line info. opcode. This value 126 was chosen to give a reasonable range of values. */ 127# define DWARF2_LINE_BASE -5 128#endif 129 130/* Range of line offsets in a special line info. opcode. */ 131#ifndef DWARF2_LINE_RANGE 132# define DWARF2_LINE_RANGE 14 133#endif 134 135#ifndef DWARF2_LINE_MIN_INSN_LENGTH 136 /* Define the architecture-dependent minimum instruction length (in 137 bytes). This value should be rather too small than too big. */ 138# define DWARF2_LINE_MIN_INSN_LENGTH 1 139#endif 140 141/* Flag that indicates the initial value of the is_stmt_start flag. */ 142#define DWARF2_LINE_DEFAULT_IS_STMT 1 143 144#ifndef DWARF2_LINE_MAX_OPS_PER_INSN 145#define DWARF2_LINE_MAX_OPS_PER_INSN 1 146#endif 147 148/* Given a special op, return the line skip amount. */ 149#define SPECIAL_LINE(op) \ 150 (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE) 151 152/* Given a special op, return the address skip amount (in units of 153 DWARF2_LINE_MIN_INSN_LENGTH. */ 154#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) 155 156/* The maximum address skip amount that can be encoded with a special op. */ 157#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) 158 159#ifndef TC_PARSE_CONS_RETURN_NONE 160#define TC_PARSE_CONS_RETURN_NONE BFD_RELOC_NONE 161#endif 162 163struct line_entry 164{ 165 struct line_entry *next; 166 symbolS *label; 167 struct dwarf2_line_info loc; 168}; 169 170/* Don't change the offset of next in line_entry. set_or_check_view 171 calls in dwarf2_gen_line_info_1 depend on it. */ 172static char unused[offsetof(struct line_entry, next) ? -1 : 1] 173ATTRIBUTE_UNUSED; 174 175struct line_subseg 176{ 177 struct line_subseg *next; 178 subsegT subseg; 179 struct line_entry *head; 180 struct line_entry **ptail; 181 struct line_entry **pmove_tail; 182}; 183 184struct line_seg 185{ 186 struct line_seg *next; 187 segT seg; 188 struct line_subseg *head; 189 symbolS *text_start; 190 symbolS *text_end; 191}; 192 193/* Collects data for all line table entries during assembly. */ 194static struct line_seg *all_segs; 195static struct line_seg **last_seg_ptr; 196 197#define NUM_MD5_BYTES 16 198 199struct file_entry 200{ 201 const char * filename; 202 unsigned int dir; 203 unsigned char md5[NUM_MD5_BYTES]; 204}; 205 206/* Table of files used by .debug_line. */ 207static struct file_entry *files; 208static unsigned int files_in_use; 209static unsigned int files_allocated; 210 211/* Table of directories used by .debug_line. */ 212static char ** dirs; 213static unsigned int dirs_in_use; 214static unsigned int dirs_allocated; 215 216/* TRUE when we've seen a .loc directive recently. Used to avoid 217 doing work when there's nothing to do. Will be reset by 218 dwarf2_consume_line_info. */ 219bool dwarf2_loc_directive_seen; 220 221/* TRUE when we've seen any .loc directive at any time during parsing. 222 Indicates the user wants us to generate a .debug_line section. 223 Used in dwarf2_finish as sanity check. */ 224static bool dwarf2_any_loc_directive_seen; 225 226/* TRUE when we're supposed to set the basic block mark whenever a 227 label is seen. */ 228bool dwarf2_loc_mark_labels; 229 230/* Current location as indicated by the most recent .loc directive. */ 231static struct dwarf2_line_info current; 232 233/* This symbol is used to recognize view number forced resets in loc 234 lists. */ 235static symbolS *force_reset_view; 236 237/* This symbol evaluates to an expression that, if nonzero, indicates 238 some view assert check failed. */ 239static symbolS *view_assert_failed; 240 241/* The size of an address on the target. */ 242static unsigned int sizeof_address; 243 244#ifndef TC_DWARF2_EMIT_OFFSET 245#define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset 246 247/* Create an offset to .dwarf2_*. */ 248 249static void 250generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size) 251{ 252 expressionS exp; 253 254 memset (&exp, 0, sizeof exp); 255 exp.X_op = O_symbol; 256 exp.X_add_symbol = symbol; 257 exp.X_add_number = 0; 258 emit_expr (&exp, size); 259} 260#endif 261 262/* Find or create (if CREATE_P) an entry for SEG+SUBSEG in ALL_SEGS. */ 263 264static struct line_subseg * 265get_line_subseg (segT seg, subsegT subseg, bool create_p) 266{ 267 struct line_seg *s = seg_info (seg)->dwarf2_line_seg; 268 struct line_subseg **pss, *lss; 269 270 if (s == NULL) 271 { 272 if (!create_p) 273 return NULL; 274 275 s = XNEW (struct line_seg); 276 s->next = NULL; 277 s->seg = seg; 278 s->head = NULL; 279 *last_seg_ptr = s; 280 last_seg_ptr = &s->next; 281 seg_info (seg)->dwarf2_line_seg = s; 282 } 283 284 gas_assert (seg == s->seg); 285 286 for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next) 287 { 288 if (lss->subseg == subseg) 289 goto found_subseg; 290 if (lss->subseg > subseg) 291 break; 292 } 293 294 lss = XNEW (struct line_subseg); 295 lss->next = *pss; 296 lss->subseg = subseg; 297 lss->head = NULL; 298 lss->ptail = &lss->head; 299 lss->pmove_tail = &lss->head; 300 *pss = lss; 301 302 found_subseg: 303 return lss; 304} 305 306/* (Un)reverse the line_entry list starting from H. */ 307 308static struct line_entry * 309reverse_line_entry_list (struct line_entry *h) 310{ 311 struct line_entry *p = NULL, *e, *n; 312 313 for (e = h; e; e = n) 314 { 315 n = e->next; 316 e->next = p; 317 p = e; 318 } 319 return p; 320} 321 322/* Compute the view for E based on the previous entry P. If we 323 introduce an (undefined) view symbol for P, and H is given (P must 324 be the tail in this case), introduce view symbols for earlier list 325 entries as well, until one of them is constant. */ 326 327static void 328set_or_check_view (struct line_entry *e, struct line_entry *p, 329 struct line_entry *h) 330{ 331 expressionS viewx; 332 333 memset (&viewx, 0, sizeof (viewx)); 334 viewx.X_unsigned = 1; 335 336 /* First, compute !(E->label > P->label), to tell whether or not 337 we're to reset the view number. If we can't resolve it to a 338 constant, keep it symbolic. */ 339 if (!p || (e->loc.u.view == force_reset_view && force_reset_view)) 340 { 341 viewx.X_op = O_constant; 342 viewx.X_add_number = 0; 343 viewx.X_add_symbol = NULL; 344 viewx.X_op_symbol = NULL; 345 } 346 else 347 { 348 viewx.X_op = O_gt; 349 viewx.X_add_number = 0; 350 viewx.X_add_symbol = e->label; 351 viewx.X_op_symbol = p->label; 352 resolve_expression (&viewx); 353 if (viewx.X_op == O_constant) 354 viewx.X_add_number = !viewx.X_add_number; 355 else 356 { 357 viewx.X_add_symbol = make_expr_symbol (&viewx); 358 viewx.X_add_number = 0; 359 viewx.X_op_symbol = NULL; 360 viewx.X_op = O_logical_not; 361 } 362 } 363 364 if (S_IS_DEFINED (e->loc.u.view) && symbol_constant_p (e->loc.u.view)) 365 { 366 expressionS *value = symbol_get_value_expression (e->loc.u.view); 367 /* We can't compare the view numbers at this point, because in 368 VIEWX we've only determined whether we're to reset it so 369 far. */ 370 if (viewx.X_op == O_constant) 371 { 372 if (!value->X_add_number != !viewx.X_add_number) 373 as_bad (_("view number mismatch")); 374 } 375 /* Record the expression to check it later. It is the result of 376 a logical not, thus 0 or 1. We just add up all such deferred 377 expressions, and resolve it at the end. */ 378 else if (!value->X_add_number) 379 { 380 symbolS *deferred = make_expr_symbol (&viewx); 381 if (view_assert_failed) 382 { 383 expressionS chk; 384 385 memset (&chk, 0, sizeof (chk)); 386 chk.X_unsigned = 1; 387 chk.X_op = O_add; 388 chk.X_add_number = 0; 389 chk.X_add_symbol = view_assert_failed; 390 chk.X_op_symbol = deferred; 391 deferred = make_expr_symbol (&chk); 392 } 393 view_assert_failed = deferred; 394 } 395 } 396 397 if (viewx.X_op != O_constant || viewx.X_add_number) 398 { 399 expressionS incv; 400 expressionS *p_view; 401 402 if (!p->loc.u.view) 403 p->loc.u.view = symbol_temp_make (); 404 405 memset (&incv, 0, sizeof (incv)); 406 incv.X_unsigned = 1; 407 incv.X_op = O_symbol; 408 incv.X_add_symbol = p->loc.u.view; 409 incv.X_add_number = 1; 410 p_view = symbol_get_value_expression (p->loc.u.view); 411 if (p_view->X_op == O_constant || p_view->X_op == O_symbol) 412 { 413 /* If we can, constant fold increments so that a chain of 414 expressions v + 1 + 1 ... + 1 is not created. 415 resolve_expression isn't ideal for this purpose. The 416 base v might not be resolvable until later. */ 417 incv.X_op = p_view->X_op; 418 incv.X_add_symbol = p_view->X_add_symbol; 419 incv.X_add_number = p_view->X_add_number + 1; 420 } 421 422 if (viewx.X_op == O_constant) 423 { 424 gas_assert (viewx.X_add_number == 1); 425 viewx = incv; 426 } 427 else 428 { 429 viewx.X_add_symbol = make_expr_symbol (&viewx); 430 viewx.X_add_number = 0; 431 viewx.X_op_symbol = make_expr_symbol (&incv); 432 viewx.X_op = O_multiply; 433 } 434 } 435 436 if (!S_IS_DEFINED (e->loc.u.view)) 437 { 438 symbol_set_value_expression (e->loc.u.view, &viewx); 439 S_SET_SEGMENT (e->loc.u.view, expr_section); 440 symbol_set_frag (e->loc.u.view, &zero_address_frag); 441 } 442 443 /* Define and attempt to simplify any earlier views needed to 444 compute E's. */ 445 if (h && p && p->loc.u.view && !S_IS_DEFINED (p->loc.u.view)) 446 { 447 struct line_entry *h2; 448 /* Reverse the list to avoid quadratic behavior going backwards 449 in a single-linked list. */ 450 struct line_entry *r = reverse_line_entry_list (h); 451 452 gas_assert (r == p); 453 /* Set or check views until we find a defined or absent view. */ 454 do 455 { 456 /* Do not define the head of a (sub?)segment view while 457 handling others. It would be defined too early, without 458 regard to the last view of other subsegments. 459 set_or_check_view will be called for every head segment 460 that needs it. */ 461 if (r == h) 462 break; 463 set_or_check_view (r, r->next, NULL); 464 } 465 while (r->next 466 && r->next->loc.u.view 467 && !S_IS_DEFINED (r->next->loc.u.view) 468 && (r = r->next)); 469 470 /* Unreverse the list, so that we can go forward again. */ 471 h2 = reverse_line_entry_list (p); 472 gas_assert (h2 == h); 473 474 /* Starting from the last view we just defined, attempt to 475 simplify the view expressions, until we do so to P. */ 476 do 477 { 478 /* The head view of a subsegment may remain undefined while 479 handling other elements, before it is linked to the last 480 view of the previous subsegment. */ 481 if (r == h) 482 continue; 483 gas_assert (S_IS_DEFINED (r->loc.u.view)); 484 resolve_expression (symbol_get_value_expression (r->loc.u.view)); 485 } 486 while (r != p && (r = r->next)); 487 488 /* Now that we've defined and computed all earlier views that might 489 be needed to compute E's, attempt to simplify it. */ 490 resolve_expression (symbol_get_value_expression (e->loc.u.view)); 491 } 492} 493 494/* Record an entry for LOC occurring at LABEL. */ 495 496static void 497dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc) 498{ 499 struct line_subseg *lss; 500 struct line_entry *e; 501 flagword need_flags = SEC_LOAD | SEC_CODE; 502 503 /* PR 26850: Do not record LOCs in non-executable or non-loaded 504 sections. SEC_ALLOC isn't tested for non-ELF because obj-coff.c 505 obj_coff_section is careless in setting SEC_ALLOC. */ 506 if (IS_ELF) 507 need_flags |= SEC_ALLOC; 508 if ((now_seg->flags & need_flags) != need_flags) 509 { 510 /* FIXME: Add code to suppress multiple warnings ? */ 511 if (debug_type != DEBUG_DWARF2) 512 as_warn ("dwarf line number information for %s ignored", 513 segment_name (now_seg)); 514 return; 515 } 516 517 e = XNEW (struct line_entry); 518 e->next = NULL; 519 e->label = label; 520 e->loc = *loc; 521 522 lss = get_line_subseg (now_seg, now_subseg, true); 523 524 /* Subseg heads are chained to previous subsegs in 525 dwarf2_finish. */ 526 if (loc->filenum != -1u && loc->u.view && lss->head) 527 set_or_check_view (e, (struct line_entry *) lss->ptail, lss->head); 528 529 *lss->ptail = e; 530 lss->ptail = &e->next; 531} 532 533/* Record an entry for LOC occurring at OFS within the current fragment. */ 534 535void 536dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc) 537{ 538 symbolS *sym; 539 540 /* Early out for as-yet incomplete location information. */ 541 if (loc->line == 0) 542 return; 543 if (loc->filenum == 0) 544 { 545 if (dwarf_level < 5) 546 dwarf_level = 5; 547 if (DWARF2_LINE_VERSION < 5) 548 return; 549 } 550 551 /* Don't emit sequences of line symbols for the same line when the 552 symbols apply to assembler code. It is necessary to emit 553 duplicate line symbols when a compiler asks for them, because GDB 554 uses them to determine the end of the prologue. */ 555 if (debug_type == DEBUG_DWARF2) 556 { 557 static unsigned int line = -1; 558 static const char *filename = NULL; 559 560 if (line == loc->line) 561 { 562 if (filename == loc->u.filename) 563 return; 564 if (filename_cmp (filename, loc->u.filename) == 0) 565 { 566 filename = loc->u.filename; 567 return; 568 } 569 } 570 571 line = loc->line; 572 filename = loc->u.filename; 573 } 574 575 if (linkrelax) 576 { 577 static int label_num = 0; 578 char name[32]; 579 580 /* Use a non-fake name for the line number location, 581 so that it can be referred to by relocations. */ 582 sprintf (name, ".Loc.%u", label_num); 583 label_num++; 584 sym = symbol_new (name, now_seg, frag_now, ofs); 585 } 586 else 587 sym = symbol_temp_new (now_seg, frag_now, ofs); 588 dwarf2_gen_line_info_1 (sym, loc); 589} 590 591static const char * 592get_basename (const char * pathname) 593{ 594 const char * file; 595 596 file = lbasename (pathname); 597 /* Don't make empty string from / or A: from A:/ . */ 598#ifdef HAVE_DOS_BASED_FILE_SYSTEM 599 if (file <= pathname + 3) 600 file = pathname; 601#else 602 if (file == pathname + 1) 603 file = pathname; 604#endif 605 return file; 606} 607 608static unsigned int 609get_directory_table_entry (const char *dirname, 610 const char *file0_dirname, 611 size_t dirlen, 612 bool can_use_zero) 613{ 614 unsigned int d; 615 616 if (dirlen == 0) 617 return 0; 618 619#ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 620 if (IS_DIR_SEPARATOR (dirname[dirlen - 1])) 621 { 622 -- dirlen; 623 if (dirlen == 0) 624 return 0; 625 } 626#endif 627 628 for (d = 0; d < dirs_in_use; ++d) 629 { 630 if (dirs[d] != NULL 631 && filename_ncmp (dirname, dirs[d], dirlen) == 0 632 && dirs[d][dirlen] == '\0') 633 return d; 634 } 635 636 if (can_use_zero) 637 { 638 if (dirs == NULL || dirs[0] == NULL) 639 { 640 const char * pwd = file0_dirname ? file0_dirname : getpwd (); 641 642 if (dwarf_level >= 5 && filename_cmp (dirname, pwd) != 0) 643 { 644 /* In DWARF-5 the 0 entry in the directory table is 645 expected to be the same as the DW_AT_comp_dir (which 646 is set to the current build directory). Since we are 647 about to create a directory entry that is not the 648 same, allocate the current directory first. */ 649 (void) get_directory_table_entry (pwd, file0_dirname, 650 strlen (pwd), true); 651 d = 1; 652 } 653 else 654 d = 0; 655 } 656 } 657 else if (d == 0) 658 d = 1; 659 660 if (d >= dirs_allocated) 661 { 662 unsigned int old = dirs_allocated; 663#define DIR_TABLE_INCREMENT 32 664 dirs_allocated = d + DIR_TABLE_INCREMENT; 665 dirs = XRESIZEVEC (char *, dirs, dirs_allocated); 666 memset (dirs + old, 0, (dirs_allocated - old) * sizeof (char *)); 667 } 668 669 dirs[d] = xmemdup0 (dirname, dirlen); 670 if (dirs_in_use <= d) 671 dirs_in_use = d + 1; 672 673 return d; 674} 675 676static bool 677assign_file_to_slot (unsigned int i, const char *file, unsigned int dir) 678{ 679 if (i >= files_allocated) 680 { 681 unsigned int want = i + 32; 682 683 /* Catch wraparound. */ 684 if (want < files_allocated 685 || want < i 686 || want > UINT_MAX / sizeof (struct file_entry)) 687 { 688 as_bad (_("file number %u is too big"), i); 689 return false; 690 } 691 692 files = XRESIZEVEC (struct file_entry, files, want); 693 memset (files + files_allocated, 0, 694 (want - files_allocated) * sizeof (struct file_entry)); 695 files_allocated = want; 696 } 697 698 files[i].filename = file; 699 files[i].dir = dir; 700 memset (files[i].md5, 0, NUM_MD5_BYTES); 701 702 if (files_in_use < i + 1) 703 files_in_use = i + 1; 704 705 return true; 706} 707 708/* Get a .debug_line file number for PATHNAME. If there is a 709 directory component to PATHNAME, then this will be stored 710 in the directory table, if it is not already present. 711 Returns the slot number allocated to that filename or -1 712 if there was a problem. */ 713 714static signed int 715allocate_filenum (const char * pathname) 716{ 717 static signed int last_used = -1, last_used_dir_len = 0; 718 const char *file; 719 size_t dir_len; 720 unsigned int i, dir; 721 722 /* Short circuit the common case of adding the same pathname 723 as last time. */ 724 if (last_used != -1) 725 { 726 const char * dirname = NULL; 727 728 if (dirs != NULL) 729 dirname = dirs[files[last_used].dir]; 730 731 if (dirname == NULL) 732 { 733 if (filename_cmp (pathname, files[last_used].filename) == 0) 734 return last_used; 735 } 736 else 737 { 738 if (filename_ncmp (pathname, dirname, last_used_dir_len - 1) == 0 739 && IS_DIR_SEPARATOR (pathname [last_used_dir_len - 1]) 740 && filename_cmp (pathname + last_used_dir_len, 741 files[last_used].filename) == 0) 742 return last_used; 743 } 744 } 745 746 file = get_basename (pathname); 747 dir_len = file - pathname; 748 749 dir = get_directory_table_entry (pathname, NULL, dir_len, false); 750 751 /* Do not use slot-0. That is specifically reserved for use by 752 the '.file 0 "name"' directive. */ 753 for (i = 1; i < files_in_use; ++i) 754 if (files[i].dir == dir 755 && files[i].filename 756 && filename_cmp (file, files[i].filename) == 0) 757 { 758 last_used = i; 759 last_used_dir_len = dir_len; 760 return i; 761 } 762 763 if (!assign_file_to_slot (i, file, dir)) 764 return -1; 765 766 last_used = i; 767 last_used_dir_len = dir_len; 768 769 return i; 770} 771 772/* Run through the list of line entries starting at E, allocating 773 file entries for gas generated debug. */ 774 775static void 776do_allocate_filenum (struct line_entry *e) 777{ 778 do 779 { 780 if (e->loc.filenum == -1u) 781 { 782 e->loc.filenum = allocate_filenum (e->loc.u.filename); 783 e->loc.u.view = NULL; 784 } 785 e = e->next; 786 } 787 while (e); 788} 789 790/* Remove any generated line entries. These don't live comfortably 791 with compiler generated line info. If THELOT then remove 792 everything, freeing all list entries we have created. */ 793 794static void 795purge_generated_debug (bool thelot) 796{ 797 struct line_seg *s, *nexts; 798 799 for (s = all_segs; s; s = nexts) 800 { 801 struct line_subseg *lss, *nextlss; 802 803 for (lss = s->head; lss; lss = nextlss) 804 { 805 struct line_entry *e, *next; 806 807 for (e = lss->head; e; e = next) 808 { 809 if (!thelot) 810 know (e->loc.filenum == -1u); 811 next = e->next; 812 free (e); 813 } 814 815 lss->head = NULL; 816 lss->ptail = &lss->head; 817 lss->pmove_tail = &lss->head; 818 nextlss = lss->next; 819 if (thelot) 820 free (lss); 821 } 822 nexts = s->next; 823 if (thelot) 824 { 825 seg_info (s->seg)->dwarf2_line_seg = NULL; 826 free (s); 827 } 828 } 829} 830 831/* Allocate slot NUM in the .debug_line file table to FILENAME. 832 If DIRNAME is not NULL or there is a directory component to FILENAME 833 then this will be stored in the directory table, if not already present. 834 if WITH_MD5 is TRUE then there is a md5 value in generic_bignum. 835 Returns TRUE if allocation succeeded, FALSE otherwise. */ 836 837static bool 838allocate_filename_to_slot (const char *dirname, 839 const char *filename, 840 unsigned int num, 841 bool with_md5) 842{ 843 const char *file; 844 size_t dirlen; 845 unsigned int i, d; 846 const char *file0_dirname; 847 848 /* Short circuit the common case of adding the same pathname 849 as last time. */ 850 if (num < files_allocated && files[num].filename != NULL) 851 { 852 const char * dir = NULL; 853 854 if (dirs != NULL) 855 dir = dirs[files[num].dir]; 856 857 if (with_md5 858 && memcmp (generic_bignum, files[num].md5, NUM_MD5_BYTES) != 0) 859 goto fail; 860 861 if (dirname != NULL) 862 { 863 if (dir != NULL && filename_cmp (dir, dirname) != 0) 864 goto fail; 865 866 if (filename_cmp (filename, files[num].filename) != 0) 867 goto fail; 868 869 /* If the filenames match, but the directory table entry was 870 empty, then fill it with the provided directory name. */ 871 if (dir == NULL) 872 { 873 if (dirs == NULL) 874 { 875 dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT; 876 dirs = XCNEWVEC (char *, dirs_allocated); 877 } 878 879 dirs[files[num].dir] = xmemdup0 (dirname, strlen (dirname)); 880 } 881 882 return true; 883 } 884 else if (dir != NULL) 885 { 886 dirlen = strlen (dir); 887 if (filename_ncmp (filename, dir, dirlen) == 0 888 && IS_DIR_SEPARATOR (filename [dirlen]) 889 && filename_cmp (filename + dirlen + 1, files[num].filename) == 0) 890 return true; 891 } 892 else /* dir == NULL */ 893 { 894 file = get_basename (filename); 895 if (filename_cmp (file, files[num].filename) == 0) 896 { 897 /* The filenames match, but the directory table entry is empty. 898 Fill it with the provided directory name. */ 899 if (file > filename) 900 { 901 if (dirs == NULL) 902 { 903 dirs_allocated = files[num].dir + DIR_TABLE_INCREMENT; 904 dirs = XCNEWVEC (char *, dirs_allocated); 905 } 906 907 dirs[files[num].dir] = xmemdup0 (filename, file - filename); 908 } 909 return true; 910 } 911 } 912 913 fail: 914 as_bad (_("file table slot %u is already occupied by a different file (%s%s%s vs %s%s%s)"), 915 num, 916 dir == NULL ? "" : dir, 917 dir == NULL ? "" : "/", 918 files[num].filename, 919 dirname == NULL ? "" : dirname, 920 dirname == NULL ? "" : "/", 921 filename); 922 return false; 923 } 924 925 /* For file .0, the directory name is the current directory and the file 926 may be in another directory contained in the file name. */ 927 if (num == 0) 928 { 929 file0_dirname = dirname; 930 931 file = get_basename (filename); 932 933 if (dirname && file == filename) 934 dirlen = strlen (dirname); 935 else 936 { 937 dirname = filename; 938 dirlen = file - filename; 939 } 940 } 941 else 942 { 943 file0_dirname = NULL; 944 945 if (dirname == NULL) 946 { 947 dirname = filename; 948 file = get_basename (filename); 949 dirlen = file - filename; 950 } 951 else 952 { 953 dirlen = strlen (dirname); 954 file = filename; 955 } 956 } 957 958 d = get_directory_table_entry (dirname, file0_dirname, dirlen, num == 0); 959 i = num; 960 961 if (! assign_file_to_slot (i, file, d)) 962 return false; 963 964 if (with_md5) 965 { 966 if (target_big_endian) 967 { 968 /* md5's are stored in litte endian format. */ 969 unsigned int bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR; 970 unsigned int byte = NUM_MD5_BYTES; 971 unsigned int bignum_index = 0; 972 973 while (bits_remaining) 974 { 975 unsigned int bignum_bits_remaining = LITTLENUM_NUMBER_OF_BITS; 976 valueT bignum_value = generic_bignum [bignum_index]; 977 bignum_index ++; 978 979 while (bignum_bits_remaining) 980 { 981 files[i].md5[--byte] = bignum_value & 0xff; 982 bignum_value >>= 8; 983 bignum_bits_remaining -= 8; 984 bits_remaining -= 8; 985 } 986 } 987 } 988 else 989 { 990 unsigned int bits_remaining = NUM_MD5_BYTES * BITS_PER_CHAR; 991 unsigned int byte = 0; 992 unsigned int bignum_index = 0; 993 994 while (bits_remaining) 995 { 996 unsigned int bignum_bits_remaining = LITTLENUM_NUMBER_OF_BITS; 997 valueT bignum_value = generic_bignum [bignum_index]; 998 999 bignum_index ++; 1000 1001 while (bignum_bits_remaining) 1002 { 1003 files[i].md5[byte++] = bignum_value & 0xff; 1004 bignum_value >>= 8; 1005 bignum_bits_remaining -= 8; 1006 bits_remaining -= 8; 1007 } 1008 } 1009 } 1010 } 1011 else 1012 memset (files[i].md5, 0, NUM_MD5_BYTES); 1013 1014 return true; 1015} 1016 1017/* Returns the current source information. If .file directives have 1018 been encountered, the info for the corresponding source file is 1019 returned. Otherwise, the info for the assembly source file is 1020 returned. */ 1021 1022void 1023dwarf2_where (struct dwarf2_line_info *line) 1024{ 1025 if (debug_type == DEBUG_DWARF2) 1026 { 1027 line->u.filename = as_where (&line->line); 1028 line->filenum = -1u; 1029 line->column = 0; 1030 line->flags = DWARF2_FLAG_IS_STMT; 1031 line->isa = current.isa; 1032 line->discriminator = current.discriminator; 1033 } 1034 else 1035 *line = current; 1036} 1037 1038/* A hook to allow the target backend to inform the line number state 1039 machine of isa changes when assembler debug info is enabled. */ 1040 1041void 1042dwarf2_set_isa (unsigned int isa) 1043{ 1044 current.isa = isa; 1045} 1046 1047/* Called for each machine instruction, or relatively atomic group of 1048 machine instructions (ie built-in macro). The instruction or group 1049 is SIZE bytes in length. If dwarf2 line number generation is called 1050 for, emit a line statement appropriately. */ 1051 1052void 1053dwarf2_emit_insn (int size) 1054{ 1055 struct dwarf2_line_info loc; 1056 1057 if (debug_type != DEBUG_DWARF2 1058 ? !dwarf2_loc_directive_seen 1059 : !seen_at_least_1_file ()) 1060 return; 1061 1062 dwarf2_where (&loc); 1063 1064 dwarf2_gen_line_info ((frag_now_fix_octets () - size) / OCTETS_PER_BYTE, &loc); 1065 dwarf2_consume_line_info (); 1066} 1067 1068/* Move all previously-emitted line entries for the current position by 1069 DELTA bytes. This function cannot be used to move the same entries 1070 twice. */ 1071 1072void 1073dwarf2_move_insn (int delta) 1074{ 1075 struct line_subseg *lss; 1076 struct line_entry *e; 1077 valueT now; 1078 1079 if (delta == 0) 1080 return; 1081 1082 lss = get_line_subseg (now_seg, now_subseg, false); 1083 if (!lss) 1084 return; 1085 1086 now = frag_now_fix (); 1087 while ((e = *lss->pmove_tail)) 1088 { 1089 if (S_GET_VALUE (e->label) == now) 1090 S_SET_VALUE (e->label, now + delta); 1091 lss->pmove_tail = &e->next; 1092 } 1093} 1094 1095/* Called after the current line information has been either used with 1096 dwarf2_gen_line_info or saved with a machine instruction for later use. 1097 This resets the state of the line number information to reflect that 1098 it has been used. */ 1099 1100void 1101dwarf2_consume_line_info (void) 1102{ 1103 /* Unless we generate DWARF2 debugging information for each 1104 assembler line, we only emit one line symbol for one LOC. */ 1105 dwarf2_loc_directive_seen = false; 1106 1107 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK 1108 | DWARF2_FLAG_PROLOGUE_END 1109 | DWARF2_FLAG_EPILOGUE_BEGIN); 1110 current.discriminator = 0; 1111 current.u.view = NULL; 1112} 1113 1114/* Called for each (preferably code) label. If dwarf2_loc_mark_labels 1115 is enabled, emit a basic block marker. */ 1116 1117void 1118dwarf2_emit_label (symbolS *label) 1119{ 1120 struct dwarf2_line_info loc; 1121 1122 if (!dwarf2_loc_mark_labels) 1123 return; 1124 if (S_GET_SEGMENT (label) != now_seg) 1125 return; 1126 if (!(bfd_section_flags (now_seg) & SEC_CODE)) 1127 return; 1128 if (files_in_use == 0 && debug_type != DEBUG_DWARF2) 1129 return; 1130 1131 dwarf2_where (&loc); 1132 1133 loc.flags |= DWARF2_FLAG_BASIC_BLOCK; 1134 1135 dwarf2_gen_line_info_1 (label, &loc); 1136 dwarf2_consume_line_info (); 1137} 1138 1139/* Handle two forms of .file directive: 1140 - Pass .file "source.c" to s_file 1141 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table 1142 1143 If an entry is added to the file table, return a pointer to the filename. */ 1144 1145char * 1146dwarf2_directive_filename (void) 1147{ 1148 bool with_md5 = false; 1149 valueT num; 1150 char *filename; 1151 const char * dirname = NULL; 1152 int filename_len; 1153 1154 /* Continue to accept a bare string and pass it off. */ 1155 SKIP_WHITESPACE (); 1156 if (*input_line_pointer == '"') 1157 { 1158 s_file (0); 1159 return NULL; 1160 } 1161 1162 num = get_absolute_expression (); 1163 1164 if ((offsetT) num < 1) 1165 { 1166 if (num == 0 && dwarf_level < 5) 1167 dwarf_level = 5; 1168 if ((offsetT) num < 0 || DWARF2_LINE_VERSION < 5) 1169 { 1170 as_bad (_("file number less than one")); 1171 ignore_rest_of_line (); 1172 return NULL; 1173 } 1174 } 1175 1176 /* FIXME: Should we allow ".file <N>\n" as an expression meaning 1177 "switch back to the already allocated file <N> as the current 1178 file" ? */ 1179 1180 filename = demand_copy_C_string (&filename_len); 1181 if (filename == NULL) 1182 /* demand_copy_C_string will have already generated an error message. */ 1183 return NULL; 1184 1185 /* For DWARF-5 support we also accept: 1186 .file <NUM> ["<dir>"] "<file>" [md5 <NUM>] */ 1187 if (DWARF2_LINE_VERSION > 4) 1188 { 1189 SKIP_WHITESPACE (); 1190 if (*input_line_pointer == '"') 1191 { 1192 dirname = filename; 1193 filename = demand_copy_C_string (&filename_len); 1194 SKIP_WHITESPACE (); 1195 } 1196 1197 if (startswith (input_line_pointer, "md5")) 1198 { 1199 input_line_pointer += 3; 1200 SKIP_WHITESPACE (); 1201 1202 expressionS exp; 1203 expression_and_evaluate (& exp); 1204 if (exp.X_op != O_big) 1205 as_bad (_("md5 value too small or not a constant")); 1206 else 1207 with_md5 = true; 1208 } 1209 } 1210 1211 demand_empty_rest_of_line (); 1212 1213 /* A .file directive implies compiler generated debug information is 1214 being supplied. Turn off gas generated debug info. */ 1215 if (debug_type == DEBUG_DWARF2) 1216 purge_generated_debug (false); 1217 debug_type = DEBUG_NONE; 1218 1219 if (num != (unsigned int) num 1220 || num >= (size_t) -1 / sizeof (struct file_entry) - 32) 1221 { 1222 as_bad (_("file number %lu is too big"), (unsigned long) num); 1223 return NULL; 1224 } 1225 1226 if (! allocate_filename_to_slot (dirname, filename, (unsigned int) num, 1227 with_md5)) 1228 return NULL; 1229 1230 return filename; 1231} 1232 1233/* Calls dwarf2_directive_filename, but discards its result. 1234 Used in pseudo-op tables where the function result is ignored. */ 1235 1236void 1237dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED) 1238{ 1239 (void) dwarf2_directive_filename (); 1240} 1241 1242void 1243dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED) 1244{ 1245 offsetT filenum, line; 1246 1247 /* If we see two .loc directives in a row, force the first one to be 1248 output now. */ 1249 if (dwarf2_loc_directive_seen) 1250 dwarf2_emit_insn (0); 1251 1252 filenum = get_absolute_expression (); 1253 SKIP_WHITESPACE (); 1254 line = get_absolute_expression (); 1255 1256 if (filenum < 1) 1257 { 1258 if (filenum == 0 && dwarf_level < 5) 1259 dwarf_level = 5; 1260 if (filenum < 0 || DWARF2_LINE_VERSION < 5) 1261 { 1262 as_bad (_("file number less than one")); 1263 return; 1264 } 1265 } 1266 1267 if ((valueT) filenum >= files_in_use || files[filenum].filename == NULL) 1268 { 1269 as_bad (_("unassigned file number %ld"), (long) filenum); 1270 return; 1271 } 1272 1273 /* debug_type will be turned off by dwarf2_directive_filename, and 1274 if we don't have a dwarf style .file then files_in_use will be 1275 zero and the above error will trigger. */ 1276 gas_assert (debug_type == DEBUG_NONE); 1277 1278 current.filenum = filenum; 1279 current.line = line; 1280 current.discriminator = 0; 1281 1282#ifndef NO_LISTING 1283 if (listing) 1284 { 1285 if (files[filenum].dir) 1286 { 1287 size_t dir_len = strlen (dirs[files[filenum].dir]); 1288 size_t file_len = strlen (files[filenum].filename); 1289 char *cp = XNEWVEC (char, dir_len + 1 + file_len + 1); 1290 1291 memcpy (cp, dirs[files[filenum].dir], dir_len); 1292 INSERT_DIR_SEPARATOR (cp, dir_len); 1293 memcpy (cp + dir_len + 1, files[filenum].filename, file_len); 1294 cp[dir_len + file_len + 1] = '\0'; 1295 listing_source_file (cp); 1296 free (cp); 1297 } 1298 else 1299 listing_source_file (files[filenum].filename); 1300 listing_source_line (line); 1301 } 1302#endif 1303 1304 SKIP_WHITESPACE (); 1305 if (ISDIGIT (*input_line_pointer)) 1306 { 1307 current.column = get_absolute_expression (); 1308 SKIP_WHITESPACE (); 1309 } 1310 1311 while (ISALPHA (*input_line_pointer)) 1312 { 1313 char *p, c; 1314 offsetT value; 1315 1316 c = get_symbol_name (& p); 1317 1318 if (strcmp (p, "basic_block") == 0) 1319 { 1320 current.flags |= DWARF2_FLAG_BASIC_BLOCK; 1321 *input_line_pointer = c; 1322 } 1323 else if (strcmp (p, "prologue_end") == 0) 1324 { 1325 current.flags |= DWARF2_FLAG_PROLOGUE_END; 1326 *input_line_pointer = c; 1327 } 1328 else if (strcmp (p, "epilogue_begin") == 0) 1329 { 1330 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN; 1331 *input_line_pointer = c; 1332 } 1333 else if (strcmp (p, "is_stmt") == 0) 1334 { 1335 (void) restore_line_pointer (c); 1336 value = get_absolute_expression (); 1337 if (value == 0) 1338 current.flags &= ~DWARF2_FLAG_IS_STMT; 1339 else if (value == 1) 1340 current.flags |= DWARF2_FLAG_IS_STMT; 1341 else 1342 { 1343 as_bad (_("is_stmt value not 0 or 1")); 1344 return; 1345 } 1346 } 1347 else if (strcmp (p, "isa") == 0) 1348 { 1349 (void) restore_line_pointer (c); 1350 value = get_absolute_expression (); 1351 if (value >= 0) 1352 current.isa = value; 1353 else 1354 { 1355 as_bad (_("isa number less than zero")); 1356 return; 1357 } 1358 } 1359 else if (strcmp (p, "discriminator") == 0) 1360 { 1361 (void) restore_line_pointer (c); 1362 value = get_absolute_expression (); 1363 if (value >= 0) 1364 current.discriminator = value; 1365 else 1366 { 1367 as_bad (_("discriminator less than zero")); 1368 return; 1369 } 1370 } 1371 else if (strcmp (p, "view") == 0) 1372 { 1373 symbolS *sym; 1374 1375 (void) restore_line_pointer (c); 1376 SKIP_WHITESPACE (); 1377 1378 if (ISDIGIT (*input_line_pointer) 1379 || *input_line_pointer == '-') 1380 { 1381 bool force_reset = *input_line_pointer == '-'; 1382 1383 value = get_absolute_expression (); 1384 if (value != 0) 1385 { 1386 as_bad (_("numeric view can only be asserted to zero")); 1387 return; 1388 } 1389 if (force_reset && force_reset_view) 1390 sym = force_reset_view; 1391 else 1392 { 1393 sym = symbol_temp_new (absolute_section, &zero_address_frag, 1394 value); 1395 if (force_reset) 1396 force_reset_view = sym; 1397 } 1398 } 1399 else 1400 { 1401 char *name = read_symbol_name (); 1402 1403 if (!name) 1404 return; 1405 sym = symbol_find_or_make (name); 1406 free (name); 1407 if (S_IS_DEFINED (sym) || symbol_equated_p (sym)) 1408 { 1409 if (S_IS_VOLATILE (sym)) 1410 sym = symbol_clone (sym, 1); 1411 else if (!S_CAN_BE_REDEFINED (sym)) 1412 { 1413 as_bad (_("symbol `%s' is already defined"), 1414 S_GET_NAME (sym)); 1415 return; 1416 } 1417 } 1418 S_SET_SEGMENT (sym, undefined_section); 1419 S_SET_VALUE (sym, 0); 1420 symbol_set_frag (sym, &zero_address_frag); 1421 } 1422 current.u.view = sym; 1423 } 1424 else 1425 { 1426 as_bad (_("unknown .loc sub-directive `%s'"), p); 1427 (void) restore_line_pointer (c); 1428 return; 1429 } 1430 1431 SKIP_WHITESPACE_AFTER_NAME (); 1432 } 1433 1434 demand_empty_rest_of_line (); 1435 dwarf2_any_loc_directive_seen = dwarf2_loc_directive_seen = true; 1436 1437 /* If we were given a view id, emit the row right away. */ 1438 if (current.u.view) 1439 dwarf2_emit_insn (0); 1440} 1441 1442void 1443dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED) 1444{ 1445 offsetT value = get_absolute_expression (); 1446 1447 if (value != 0 && value != 1) 1448 { 1449 as_bad (_("expected 0 or 1")); 1450 ignore_rest_of_line (); 1451 } 1452 else 1453 { 1454 dwarf2_loc_mark_labels = value != 0; 1455 demand_empty_rest_of_line (); 1456 } 1457} 1458 1459static struct frag * 1460first_frag_for_seg (segT seg) 1461{ 1462 return seg_info (seg)->frchainP->frch_root; 1463} 1464 1465static struct frag * 1466last_frag_for_seg (segT seg) 1467{ 1468 frchainS *f = seg_info (seg)->frchainP; 1469 1470 while (f->frch_next != NULL) 1471 f = f->frch_next; 1472 1473 return f->frch_last; 1474} 1475 1476/* Emit a single byte into the current segment. */ 1477 1478static inline void 1479out_byte (int byte) 1480{ 1481 FRAG_APPEND_1_CHAR (byte); 1482} 1483 1484/* Emit a statement program opcode into the current segment. */ 1485 1486static inline void 1487out_opcode (int opc) 1488{ 1489 out_byte (opc); 1490} 1491 1492/* Emit a two-byte word into the current segment. */ 1493 1494static inline void 1495out_two (int data) 1496{ 1497 md_number_to_chars (frag_more (2), data, 2); 1498} 1499 1500/* Emit a four byte word into the current segment. */ 1501 1502static inline void 1503out_four (int data) 1504{ 1505 md_number_to_chars (frag_more (4), data, 4); 1506} 1507 1508/* Emit an unsigned "little-endian base 128" number. */ 1509 1510static void 1511out_uleb128 (addressT value) 1512{ 1513 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0); 1514} 1515 1516/* Emit a signed "little-endian base 128" number. */ 1517 1518static void 1519out_leb128 (addressT value) 1520{ 1521 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1); 1522} 1523 1524/* Emit a tuple for .debug_abbrev. */ 1525 1526static inline void 1527out_abbrev (int name, int form) 1528{ 1529 out_uleb128 (name); 1530 out_uleb128 (form); 1531} 1532 1533/* Get the size of a fragment. */ 1534 1535static offsetT 1536get_frag_fix (fragS *frag, segT seg) 1537{ 1538 frchainS *fr; 1539 1540 if (frag->fr_next) 1541 return frag->fr_fix; 1542 1543 /* If a fragment is the last in the chain, special measures must be 1544 taken to find its size before relaxation, since it may be pending 1545 on some subsegment chain. */ 1546 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next) 1547 if (fr->frch_last == frag) 1548 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal; 1549 1550 abort (); 1551} 1552 1553/* Set an absolute address (may result in a relocation entry). */ 1554 1555static void 1556out_set_addr (symbolS *sym) 1557{ 1558 expressionS exp; 1559 1560 memset (&exp, 0, sizeof exp); 1561 out_opcode (DW_LNS_extended_op); 1562 out_uleb128 (sizeof_address + 1); 1563 1564 out_opcode (DW_LNE_set_address); 1565 exp.X_op = O_symbol; 1566 exp.X_add_symbol = sym; 1567 exp.X_add_number = 0; 1568 emit_expr (&exp, sizeof_address); 1569} 1570 1571static void scale_addr_delta (addressT *); 1572 1573static void 1574scale_addr_delta (addressT *addr_delta) 1575{ 1576 static int printed_this = 0; 1577 if (DWARF2_LINE_MIN_INSN_LENGTH > 1) 1578 { 1579 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0 && !printed_this) 1580 { 1581 as_bad("unaligned opcodes detected in executable segment"); 1582 printed_this = 1; 1583 } 1584 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH; 1585 } 1586} 1587 1588/* Encode a pair of line and address skips as efficiently as possible. 1589 Note that the line skip is signed, whereas the address skip is unsigned. 1590 1591 The following two routines *must* be kept in sync. This is 1592 enforced by making emit_inc_line_addr abort if we do not emit 1593 exactly the expected number of bytes. */ 1594 1595static int 1596size_inc_line_addr (int line_delta, addressT addr_delta) 1597{ 1598 unsigned int tmp, opcode; 1599 int len = 0; 1600 1601 /* Scale the address delta by the minimum instruction length. */ 1602 scale_addr_delta (&addr_delta); 1603 1604 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. 1605 We cannot use special opcodes here, since we want the end_sequence 1606 to emit the matrix entry. */ 1607 if (line_delta == INT_MAX) 1608 { 1609 if (addr_delta == MAX_SPECIAL_ADDR_DELTA) 1610 len = 1; 1611 else if (addr_delta) 1612 len = 1 + sizeof_leb128 (addr_delta, 0); 1613 return len + 3; 1614 } 1615 1616 /* Bias the line delta by the base. */ 1617 tmp = line_delta - DWARF2_LINE_BASE; 1618 1619 /* If the line increment is out of range of a special opcode, we 1620 must encode it with DW_LNS_advance_line. */ 1621 if (tmp >= DWARF2_LINE_RANGE) 1622 { 1623 len = 1 + sizeof_leb128 (line_delta, 1); 1624 line_delta = 0; 1625 tmp = 0 - DWARF2_LINE_BASE; 1626 } 1627 1628 /* Bias the opcode by the special opcode base. */ 1629 tmp += DWARF2_LINE_OPCODE_BASE; 1630 1631 /* Avoid overflow when addr_delta is large. */ 1632 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA) 1633 { 1634 /* Try using a special opcode. */ 1635 opcode = tmp + addr_delta * DWARF2_LINE_RANGE; 1636 if (opcode <= 255) 1637 return len + 1; 1638 1639 /* Try using DW_LNS_const_add_pc followed by special op. */ 1640 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; 1641 if (opcode <= 255) 1642 return len + 2; 1643 } 1644 1645 /* Otherwise use DW_LNS_advance_pc. */ 1646 len += 1 + sizeof_leb128 (addr_delta, 0); 1647 1648 /* DW_LNS_copy or special opcode. */ 1649 len += 1; 1650 1651 return len; 1652} 1653 1654static void 1655emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len) 1656{ 1657 unsigned int tmp, opcode; 1658 int need_copy = 0; 1659 char *end = p + len; 1660 1661 /* Line number sequences cannot go backward in addresses. This means 1662 we've incorrectly ordered the statements in the sequence. */ 1663 gas_assert ((offsetT) addr_delta >= 0); 1664 1665 /* Scale the address delta by the minimum instruction length. */ 1666 scale_addr_delta (&addr_delta); 1667 1668 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. 1669 We cannot use special opcodes here, since we want the end_sequence 1670 to emit the matrix entry. */ 1671 if (line_delta == INT_MAX) 1672 { 1673 if (addr_delta == MAX_SPECIAL_ADDR_DELTA) 1674 *p++ = DW_LNS_const_add_pc; 1675 else if (addr_delta) 1676 { 1677 *p++ = DW_LNS_advance_pc; 1678 p += output_leb128 (p, addr_delta, 0); 1679 } 1680 1681 *p++ = DW_LNS_extended_op; 1682 *p++ = 1; 1683 *p++ = DW_LNE_end_sequence; 1684 goto done; 1685 } 1686 1687 /* Bias the line delta by the base. */ 1688 tmp = line_delta - DWARF2_LINE_BASE; 1689 1690 /* If the line increment is out of range of a special opcode, we 1691 must encode it with DW_LNS_advance_line. */ 1692 if (tmp >= DWARF2_LINE_RANGE) 1693 { 1694 *p++ = DW_LNS_advance_line; 1695 p += output_leb128 (p, line_delta, 1); 1696 1697 line_delta = 0; 1698 tmp = 0 - DWARF2_LINE_BASE; 1699 need_copy = 1; 1700 } 1701 1702 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0" 1703 special opcode. */ 1704 if (line_delta == 0 && addr_delta == 0) 1705 { 1706 *p++ = DW_LNS_copy; 1707 goto done; 1708 } 1709 1710 /* Bias the opcode by the special opcode base. */ 1711 tmp += DWARF2_LINE_OPCODE_BASE; 1712 1713 /* Avoid overflow when addr_delta is large. */ 1714 if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA) 1715 { 1716 /* Try using a special opcode. */ 1717 opcode = tmp + addr_delta * DWARF2_LINE_RANGE; 1718 if (opcode <= 255) 1719 { 1720 *p++ = opcode; 1721 goto done; 1722 } 1723 1724 /* Try using DW_LNS_const_add_pc followed by special op. */ 1725 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; 1726 if (opcode <= 255) 1727 { 1728 *p++ = DW_LNS_const_add_pc; 1729 *p++ = opcode; 1730 goto done; 1731 } 1732 } 1733 1734 /* Otherwise use DW_LNS_advance_pc. */ 1735 *p++ = DW_LNS_advance_pc; 1736 p += output_leb128 (p, addr_delta, 0); 1737 1738 if (need_copy) 1739 *p++ = DW_LNS_copy; 1740 else 1741 *p++ = tmp; 1742 1743 done: 1744 gas_assert (p == end); 1745} 1746 1747/* Handy routine to combine calls to the above two routines. */ 1748 1749static void 1750out_inc_line_addr (int line_delta, addressT addr_delta) 1751{ 1752 int len = size_inc_line_addr (line_delta, addr_delta); 1753 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len); 1754} 1755 1756/* Write out an alternative form of line and address skips using 1757 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default 1758 line and address information, but it is required if linker relaxation 1759 could change the code offsets. The following two routines *must* be 1760 kept in sync. */ 1761#define ADDR_DELTA_LIMIT 50000 1762 1763static int 1764size_fixed_inc_line_addr (int line_delta, addressT addr_delta) 1765{ 1766 int len = 0; 1767 1768 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ 1769 if (line_delta != INT_MAX) 1770 len = 1 + sizeof_leb128 (line_delta, 1); 1771 1772 if (addr_delta > ADDR_DELTA_LIMIT) 1773 { 1774 /* DW_LNS_extended_op */ 1775 len += 1 + sizeof_leb128 (sizeof_address + 1, 0); 1776 /* DW_LNE_set_address */ 1777 len += 1 + sizeof_address; 1778 } 1779 else 1780 /* DW_LNS_fixed_advance_pc */ 1781 len += 3; 1782 1783 if (line_delta == INT_MAX) 1784 /* DW_LNS_extended_op + DW_LNE_end_sequence */ 1785 len += 3; 1786 else 1787 /* DW_LNS_copy */ 1788 len += 1; 1789 1790 return len; 1791} 1792 1793static void 1794emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag, 1795 char *p, int len) 1796{ 1797 expressionS *pexp; 1798 char *end = p + len; 1799 1800 /* Line number sequences cannot go backward in addresses. This means 1801 we've incorrectly ordered the statements in the sequence. */ 1802 gas_assert ((offsetT) addr_delta >= 0); 1803 1804 /* Verify that we have kept in sync with size_fixed_inc_line_addr. */ 1805 gas_assert (len == size_fixed_inc_line_addr (line_delta, addr_delta)); 1806 1807 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ 1808 if (line_delta != INT_MAX) 1809 { 1810 *p++ = DW_LNS_advance_line; 1811 p += output_leb128 (p, line_delta, 1); 1812 } 1813 1814 pexp = symbol_get_value_expression (frag->fr_symbol); 1815 1816 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can 1817 advance the address by at most 64K. Linker relaxation (without 1818 which this function would not be used) could change the operand by 1819 an unknown amount. If the address increment is getting close to 1820 the limit, just reset the address. */ 1821 if (addr_delta > ADDR_DELTA_LIMIT) 1822 { 1823 symbolS *to_sym; 1824 expressionS exp; 1825 1826 memset (&exp, 0, sizeof exp); 1827 gas_assert (pexp->X_op == O_subtract); 1828 to_sym = pexp->X_add_symbol; 1829 1830 *p++ = DW_LNS_extended_op; 1831 p += output_leb128 (p, sizeof_address + 1, 0); 1832 *p++ = DW_LNE_set_address; 1833 exp.X_op = O_symbol; 1834 exp.X_add_symbol = to_sym; 1835 exp.X_add_number = 0; 1836 emit_expr_fix (&exp, sizeof_address, frag, p, TC_PARSE_CONS_RETURN_NONE); 1837 p += sizeof_address; 1838 } 1839 else 1840 { 1841 *p++ = DW_LNS_fixed_advance_pc; 1842 emit_expr_fix (pexp, 2, frag, p, TC_PARSE_CONS_RETURN_NONE); 1843 p += 2; 1844 } 1845 1846 if (line_delta == INT_MAX) 1847 { 1848 *p++ = DW_LNS_extended_op; 1849 *p++ = 1; 1850 *p++ = DW_LNE_end_sequence; 1851 } 1852 else 1853 *p++ = DW_LNS_copy; 1854 1855 gas_assert (p == end); 1856} 1857 1858/* Generate a variant frag that we can use to relax address/line 1859 increments between fragments of the target segment. */ 1860 1861static void 1862relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym) 1863{ 1864 expressionS exp; 1865 int max_chars; 1866 1867 memset (&exp, 0, sizeof exp); 1868 exp.X_op = O_subtract; 1869 exp.X_add_symbol = to_sym; 1870 exp.X_op_symbol = from_sym; 1871 exp.X_add_number = 0; 1872 1873 /* The maximum size of the frag is the line delta with a maximum 1874 sized address delta. */ 1875 if (DWARF2_USE_FIXED_ADVANCE_PC) 1876 max_chars = size_fixed_inc_line_addr (line_delta, 1877 -DWARF2_LINE_MIN_INSN_LENGTH); 1878 else 1879 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH); 1880 1881 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1, 1882 make_expr_symbol (&exp), line_delta, NULL); 1883} 1884 1885/* The function estimates the size of a rs_dwarf2dbg variant frag 1886 based on the current values of the symbols. It is called before 1887 the relaxation loop. We set fr_subtype to the expected length. */ 1888 1889int 1890dwarf2dbg_estimate_size_before_relax (fragS *frag) 1891{ 1892 offsetT addr_delta; 1893 int size; 1894 1895 addr_delta = resolve_symbol_value (frag->fr_symbol); 1896 if (DWARF2_USE_FIXED_ADVANCE_PC) 1897 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta); 1898 else 1899 size = size_inc_line_addr (frag->fr_offset, addr_delta); 1900 1901 frag->fr_subtype = size; 1902 1903 return size; 1904} 1905 1906/* This function relaxes a rs_dwarf2dbg variant frag based on the 1907 current values of the symbols. fr_subtype is the current length 1908 of the frag. This returns the change in frag length. */ 1909 1910int 1911dwarf2dbg_relax_frag (fragS *frag) 1912{ 1913 int old_size, new_size; 1914 1915 old_size = frag->fr_subtype; 1916 new_size = dwarf2dbg_estimate_size_before_relax (frag); 1917 1918 return new_size - old_size; 1919} 1920 1921/* This function converts a rs_dwarf2dbg variant frag into a normal 1922 fill frag. This is called after all relaxation has been done. 1923 fr_subtype will be the desired length of the frag. */ 1924 1925void 1926dwarf2dbg_convert_frag (fragS *frag) 1927{ 1928 offsetT addr_diff; 1929 1930 if (DWARF2_USE_FIXED_ADVANCE_PC) 1931 { 1932 /* If linker relaxation is enabled then the distance between the two 1933 symbols in the frag->fr_symbol expression might change. Hence we 1934 cannot rely upon the value computed by resolve_symbol_value. 1935 Instead we leave the expression unfinalized and allow 1936 emit_fixed_inc_line_addr to create a fixup (which later becomes a 1937 relocation) that will allow the linker to correctly compute the 1938 actual address difference. We have to use a fixed line advance for 1939 this as we cannot (easily) relocate leb128 encoded values. */ 1940 int saved_finalize_syms = finalize_syms; 1941 1942 finalize_syms = 0; 1943 addr_diff = resolve_symbol_value (frag->fr_symbol); 1944 finalize_syms = saved_finalize_syms; 1945 } 1946 else 1947 addr_diff = resolve_symbol_value (frag->fr_symbol); 1948 1949 /* fr_var carries the max_chars that we created the fragment with. 1950 fr_subtype carries the current expected length. We must, of 1951 course, have allocated enough memory earlier. */ 1952 gas_assert (frag->fr_var >= (int) frag->fr_subtype); 1953 1954 if (DWARF2_USE_FIXED_ADVANCE_PC) 1955 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag, 1956 frag->fr_literal + frag->fr_fix, 1957 frag->fr_subtype); 1958 else 1959 emit_inc_line_addr (frag->fr_offset, addr_diff, 1960 frag->fr_literal + frag->fr_fix, frag->fr_subtype); 1961 1962 frag->fr_fix += frag->fr_subtype; 1963 frag->fr_type = rs_fill; 1964 frag->fr_var = 0; 1965 frag->fr_offset = 0; 1966} 1967 1968/* Generate .debug_line content for the chain of line number entries 1969 beginning at E, for segment SEG. */ 1970 1971static void 1972process_entries (segT seg, struct line_entry *e) 1973{ 1974 unsigned filenum = 1; 1975 unsigned line = 1; 1976 unsigned column = 0; 1977 unsigned isa = 0; 1978 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 1979 fragS *last_frag = NULL, *frag; 1980 addressT last_frag_ofs = 0, frag_ofs; 1981 symbolS *last_lab = NULL, *lab; 1982 1983 if (flag_dwarf_sections) 1984 { 1985 char * name; 1986 const char * sec_name; 1987 1988 /* Switch to the relevant sub-section before we start to emit 1989 the line number table. 1990 1991 FIXME: These sub-sections do not have a normal Line Number 1992 Program Header, thus strictly speaking they are not valid 1993 DWARF sections. Unfortunately the DWARF standard assumes 1994 a one-to-one relationship between compilation units and 1995 line number tables. Thus we have to have a .debug_line 1996 section, as well as our sub-sections, and we have to ensure 1997 that all of the sub-sections are merged into a proper 1998 .debug_line section before a debugger sees them. */ 1999 2000 sec_name = bfd_section_name (seg); 2001 if (strcmp (sec_name, ".text") != 0) 2002 { 2003 name = concat (".debug_line", sec_name, (char *) NULL); 2004 subseg_set (subseg_get (name, false), 0); 2005 } 2006 else 2007 /* Don't create a .debug_line.text section - 2008 that is redundant. Instead just switch back to the 2009 normal .debug_line section. */ 2010 subseg_set (subseg_get (".debug_line", false), 0); 2011 } 2012 2013 do 2014 { 2015 int line_delta; 2016 2017 if (filenum != e->loc.filenum) 2018 { 2019 filenum = e->loc.filenum; 2020 out_opcode (DW_LNS_set_file); 2021 out_uleb128 (filenum); 2022 } 2023 2024 if (column != e->loc.column) 2025 { 2026 column = e->loc.column; 2027 out_opcode (DW_LNS_set_column); 2028 out_uleb128 (column); 2029 } 2030 2031 if (e->loc.discriminator != 0) 2032 { 2033 out_opcode (DW_LNS_extended_op); 2034 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0)); 2035 out_opcode (DW_LNE_set_discriminator); 2036 out_uleb128 (e->loc.discriminator); 2037 } 2038 2039 if (isa != e->loc.isa) 2040 { 2041 isa = e->loc.isa; 2042 out_opcode (DW_LNS_set_isa); 2043 out_uleb128 (isa); 2044 } 2045 2046 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT) 2047 { 2048 flags = e->loc.flags; 2049 out_opcode (DW_LNS_negate_stmt); 2050 } 2051 2052 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK) 2053 out_opcode (DW_LNS_set_basic_block); 2054 2055 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END) 2056 out_opcode (DW_LNS_set_prologue_end); 2057 2058 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN) 2059 out_opcode (DW_LNS_set_epilogue_begin); 2060 2061 /* Don't try to optimize away redundant entries; gdb wants two 2062 entries for a function where the code starts on the same line as 2063 the {, and there's no way to identify that case here. Trust gcc 2064 to optimize appropriately. */ 2065 line_delta = e->loc.line - line; 2066 lab = e->label; 2067 frag = symbol_get_frag (lab); 2068 frag_ofs = S_GET_VALUE (lab); 2069 2070 if (last_frag == NULL 2071 || (e->loc.u.view == force_reset_view && force_reset_view 2072 /* If we're going to reset the view, but we know we're 2073 advancing the PC, we don't have to force with 2074 set_address. We know we do when we're at the same 2075 address of the same frag, and we know we might when 2076 we're in the beginning of a frag, and we were at the 2077 end of the previous frag. */ 2078 && (frag == last_frag 2079 ? (last_frag_ofs == frag_ofs) 2080 : (frag_ofs == 0 2081 && ((offsetT)last_frag_ofs 2082 >= get_frag_fix (last_frag, seg)))))) 2083 { 2084 out_set_addr (lab); 2085 out_inc_line_addr (line_delta, 0); 2086 } 2087 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) 2088 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs); 2089 else 2090 relax_inc_line_addr (line_delta, lab, last_lab); 2091 2092 line = e->loc.line; 2093 last_lab = lab; 2094 last_frag = frag; 2095 last_frag_ofs = frag_ofs; 2096 2097 e = e->next; 2098 } 2099 while (e); 2100 2101 /* Emit a DW_LNE_end_sequence for the end of the section. */ 2102 frag = last_frag_for_seg (seg); 2103 frag_ofs = get_frag_fix (frag, seg); 2104 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) 2105 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs); 2106 else 2107 { 2108 lab = symbol_temp_new (seg, frag, frag_ofs); 2109 relax_inc_line_addr (INT_MAX, lab, last_lab); 2110 } 2111} 2112 2113/* Switch to LINE_STR_SEG and output the given STR. Return the 2114 symbol pointing to the new string in the section. */ 2115 2116static symbolS * 2117add_line_strp (segT line_str_seg, const char *str) 2118{ 2119 char *cp; 2120 size_t size; 2121 symbolS *sym; 2122 2123 subseg_set (line_str_seg, 0); 2124 2125 sym = symbol_temp_new_now_octets (); 2126 2127 size = strlen (str) + 1; 2128 cp = frag_more (size); 2129 memcpy (cp, str, size); 2130 2131 return sym; 2132} 2133 2134 2135/* Emit the directory and file tables for .debug_line. */ 2136 2137static void 2138out_dir_and_file_list (segT line_seg, int sizeof_offset) 2139{ 2140 size_t size; 2141 char *dir; 2142 char *cp; 2143 unsigned int i, j; 2144 bool emit_md5 = false; 2145 bool emit_timestamps = true; 2146 bool emit_filesize = true; 2147 segT line_str_seg = NULL; 2148 symbolS *line_strp, *file0_strp = NULL; 2149 2150 /* Output the Directory Table. */ 2151 if (DWARF2_LINE_VERSION >= 5) 2152 { 2153 /* We only have one column in the directory table. */ 2154 out_byte (1); 2155 2156 /* Describe the purpose and format of the column. */ 2157 out_uleb128 (DW_LNCT_path); 2158 /* Store these strings in the .debug_line_str section so they 2159 can be shared. */ 2160 out_uleb128 (DW_FORM_line_strp); 2161 2162 /* Now state how many rows there are in the table. We need at 2163 least 1 if there is one or more file names to store the 2164 "working directory". */ 2165 if (dirs_in_use == 0 && files_in_use > 0) 2166 out_uleb128 (1); 2167 else 2168 out_uleb128 (dirs_in_use); 2169 } 2170 2171 /* Emit directory list. */ 2172 if (DWARF2_LINE_VERSION >= 5 && (dirs_in_use > 0 || files_in_use > 0)) 2173 { 2174 line_str_seg = subseg_new (".debug_line_str", 0); 2175 bfd_set_section_flags (line_str_seg, 2176 SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS 2177 | SEC_MERGE | SEC_STRINGS); 2178 line_str_seg->entsize = 1; 2179 2180 /* DWARF5 uses slot zero, but that is only set explicitly 2181 using a .file 0 directive. Otherwise use pwd as main file 2182 directory. */ 2183 if (dirs_in_use > 0 && dirs[0] != NULL) 2184 dir = remap_debug_filename (dirs[0]); 2185 else 2186 dir = remap_debug_filename (getpwd ()); 2187 2188 line_strp = add_line_strp (line_str_seg, dir); 2189 free (dir); 2190 subseg_set (line_seg, 0); 2191 TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset); 2192 } 2193 for (i = 1; i < dirs_in_use; ++i) 2194 { 2195 dir = remap_debug_filename (dirs[i]); 2196 if (DWARF2_LINE_VERSION < 5) 2197 { 2198 size = strlen (dir) + 1; 2199 cp = frag_more (size); 2200 memcpy (cp, dir, size); 2201 } 2202 else 2203 { 2204 line_strp = add_line_strp (line_str_seg, dir); 2205 subseg_set (line_seg, 0); 2206 TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset); 2207 } 2208 free (dir); 2209 } 2210 2211 if (DWARF2_LINE_VERSION < 5) 2212 /* Terminate it. */ 2213 out_byte ('\0'); 2214 2215 /* Output the File Name Table. */ 2216 if (DWARF2_LINE_VERSION >= 5) 2217 { 2218 unsigned int columns = 4; 2219 2220 if (((unsigned long) DWARF2_FILE_TIME_NAME ("", "")) == -1UL) 2221 { 2222 emit_timestamps = false; 2223 -- columns; 2224 } 2225 2226 if (DWARF2_FILE_SIZE_NAME ("", "") == -1) 2227 { 2228 emit_filesize = false; 2229 -- columns; 2230 } 2231 2232 for (i = 0; i < files_in_use; ++i) 2233 if (files[i].md5[0] != 0) 2234 break; 2235 if (i < files_in_use) 2236 { 2237 emit_md5 = true; 2238 ++ columns; 2239 } 2240 2241 /* The number of format entries to follow. */ 2242 out_byte (columns); 2243 /* The format of the file name. */ 2244 out_uleb128 (DW_LNCT_path); 2245 /* Store these strings in the .debug_line_str section so they 2246 can be shared. */ 2247 out_uleb128 (DW_FORM_line_strp); 2248 2249 /* The format of the directory index. */ 2250 out_uleb128 (DW_LNCT_directory_index); 2251 out_uleb128 (DW_FORM_udata); 2252 2253 if (emit_timestamps) 2254 { 2255 /* The format of the timestamp. */ 2256 out_uleb128 (DW_LNCT_timestamp); 2257 out_uleb128 (DW_FORM_udata); 2258 } 2259 2260 if (emit_filesize) 2261 { 2262 /* The format of the file size. */ 2263 out_uleb128 (DW_LNCT_size); 2264 out_uleb128 (DW_FORM_udata); 2265 } 2266 2267 if (emit_md5) 2268 { 2269 /* The format of the MD5 sum. */ 2270 out_uleb128 (DW_LNCT_MD5); 2271 out_uleb128 (DW_FORM_data16); 2272 } 2273 2274 /* The number of entries in the table. */ 2275 out_uleb128 (files_in_use); 2276 } 2277 2278 for (i = DWARF2_LINE_VERSION > 4 ? 0 : 1; i < files_in_use; ++i) 2279 { 2280 const char *fullfilename; 2281 2282 if (files[i].filename == NULL) 2283 { 2284 if (DWARF2_LINE_VERSION < 5 || i != 0) 2285 { 2286 as_bad (_("unassigned file number %ld"), (long) i); 2287 continue; 2288 } 2289 /* DWARF5 uses slot zero, but that is only set explicitly using 2290 a .file 0 directive. If that isn't used, but file 1 is, then 2291 use that as main file name. */ 2292 if (files_in_use > 1 && files[1].filename != NULL) 2293 { 2294 files[0].filename = files[1].filename; 2295 files[0].dir = files[1].dir; 2296 if (emit_md5) 2297 for (j = 0; j < NUM_MD5_BYTES; ++j) 2298 files[0].md5[j] = files[1].md5[j]; 2299 } 2300 else 2301 files[0].filename = ""; 2302 } 2303 2304 fullfilename = DWARF2_FILE_NAME (files[i].filename, 2305 files[i].dir ? dirs [files [i].dir] : ""); 2306 if (DWARF2_LINE_VERSION < 5) 2307 { 2308 size = strlen (fullfilename) + 1; 2309 cp = frag_more (size); 2310 memcpy (cp, fullfilename, size); 2311 } 2312 else 2313 { 2314 if (!file0_strp) 2315 line_strp = add_line_strp (line_str_seg, fullfilename); 2316 else 2317 line_strp = file0_strp; 2318 subseg_set (line_seg, 0); 2319 TC_DWARF2_EMIT_OFFSET (line_strp, sizeof_offset); 2320 if (i == 0 && files_in_use > 1 2321 && files[0].filename == files[1].filename) 2322 file0_strp = line_strp; 2323 else 2324 file0_strp = NULL; 2325 } 2326 2327 /* Directory number. */ 2328 out_uleb128 (files[i].dir); 2329 2330 /* Output the last modification timestamp. */ 2331 if (emit_timestamps) 2332 { 2333 offsetT timestamp; 2334 2335 timestamp = DWARF2_FILE_TIME_NAME (files[i].filename, 2336 files[i].dir ? dirs [files [i].dir] : ""); 2337 if (timestamp == -1) 2338 timestamp = 0; 2339 out_uleb128 (timestamp); 2340 } 2341 2342 /* Output the filesize. */ 2343 if (emit_filesize) 2344 { 2345 offsetT filesize; 2346 filesize = DWARF2_FILE_SIZE_NAME (files[i].filename, 2347 files[i].dir ? dirs [files [i].dir] : ""); 2348 if (filesize == -1) 2349 filesize = 0; 2350 out_uleb128 (filesize); 2351 } 2352 2353 /* Output the md5 sum. */ 2354 if (emit_md5) 2355 { 2356 int b; 2357 2358 for (b = 0; b < NUM_MD5_BYTES; b++) 2359 out_byte (files[i].md5[b]); 2360 } 2361 } 2362 2363 if (DWARF2_LINE_VERSION < 5) 2364 /* Terminate filename list. */ 2365 out_byte (0); 2366} 2367 2368/* Switch to SEC and output a header length field. Return the size of 2369 offsets used in SEC. The caller must set EXPR->X_add_symbol value 2370 to the end of the section. EXPR->X_add_number will be set to the 2371 negative size of the header. */ 2372 2373static int 2374out_header (asection *sec, expressionS *exp) 2375{ 2376 symbolS *start_sym; 2377 symbolS *end_sym; 2378 2379 subseg_set (sec, 0); 2380 2381 if (flag_dwarf_sections) 2382 { 2383 /* If we are going to put the start and end symbols in different 2384 sections, then we need real symbols, not just fake, local ones. */ 2385 frag_now_fix (); 2386 start_sym = symbol_make (".Ldebug_line_start"); 2387 end_sym = symbol_make (".Ldebug_line_end"); 2388 symbol_set_value_now (start_sym); 2389 } 2390 else 2391 { 2392 start_sym = symbol_temp_new_now_octets (); 2393 end_sym = symbol_temp_make (); 2394 } 2395 2396 /* Total length of the information. */ 2397 exp->X_op = O_subtract; 2398 exp->X_add_symbol = end_sym; 2399 exp->X_op_symbol = start_sym; 2400 2401 switch (DWARF2_FORMAT (sec)) 2402 { 2403 case dwarf2_format_32bit: 2404 exp->X_add_number = -4; 2405 emit_expr (exp, 4); 2406 return 4; 2407 2408 case dwarf2_format_64bit: 2409 exp->X_add_number = -12; 2410 out_four (-1); 2411 emit_expr (exp, 8); 2412 return 8; 2413 2414 case dwarf2_format_64bit_irix: 2415 exp->X_add_number = -8; 2416 emit_expr (exp, 8); 2417 return 8; 2418 } 2419 2420 as_fatal (_("internal error: unknown dwarf2 format")); 2421 return 0; 2422} 2423 2424/* Emit the collected .debug_line data. */ 2425 2426static void 2427out_debug_line (segT line_seg) 2428{ 2429 expressionS exp; 2430 symbolS *prologue_start, *prologue_end; 2431 symbolS *line_end; 2432 struct line_seg *s; 2433 int sizeof_offset; 2434 2435 memset (&exp, 0, sizeof exp); 2436 sizeof_offset = out_header (line_seg, &exp); 2437 line_end = exp.X_add_symbol; 2438 2439 /* Version. */ 2440 out_two (DWARF2_LINE_VERSION); 2441 2442 if (DWARF2_LINE_VERSION >= 5) 2443 { 2444 out_byte (sizeof_address); 2445 out_byte (0); /* Segment Selector size. */ 2446 } 2447 /* Length of the prologue following this length. */ 2448 prologue_start = symbol_temp_make (); 2449 prologue_end = symbol_temp_make (); 2450 exp.X_op = O_subtract; 2451 exp.X_add_symbol = prologue_end; 2452 exp.X_op_symbol = prologue_start; 2453 exp.X_add_number = 0; 2454 emit_expr (&exp, sizeof_offset); 2455 symbol_set_value_now (prologue_start); 2456 2457 /* Parameters of the state machine. */ 2458 out_byte (DWARF2_LINE_MIN_INSN_LENGTH); 2459 if (DWARF2_LINE_VERSION >= 4) 2460 out_byte (DWARF2_LINE_MAX_OPS_PER_INSN); 2461 out_byte (DWARF2_LINE_DEFAULT_IS_STMT); 2462 out_byte (DWARF2_LINE_BASE); 2463 out_byte (DWARF2_LINE_RANGE); 2464 out_byte (DWARF2_LINE_OPCODE_BASE); 2465 2466 /* Standard opcode lengths. */ 2467 out_byte (0); /* DW_LNS_copy */ 2468 out_byte (1); /* DW_LNS_advance_pc */ 2469 out_byte (1); /* DW_LNS_advance_line */ 2470 out_byte (1); /* DW_LNS_set_file */ 2471 out_byte (1); /* DW_LNS_set_column */ 2472 out_byte (0); /* DW_LNS_negate_stmt */ 2473 out_byte (0); /* DW_LNS_set_basic_block */ 2474 out_byte (0); /* DW_LNS_const_add_pc */ 2475 out_byte (1); /* DW_LNS_fixed_advance_pc */ 2476 out_byte (0); /* DW_LNS_set_prologue_end */ 2477 out_byte (0); /* DW_LNS_set_epilogue_begin */ 2478 out_byte (1); /* DW_LNS_set_isa */ 2479 /* We have emitted 12 opcode lengths, so make that this 2480 matches up to the opcode base value we have been using. */ 2481 gas_assert (DWARF2_LINE_OPCODE_BASE == 13); 2482 2483 out_dir_and_file_list (line_seg, sizeof_offset); 2484 2485 symbol_set_value_now (prologue_end); 2486 2487 /* For each section, emit a statement program. */ 2488 for (s = all_segs; s; s = s->next) 2489 /* Paranoia - this check should have already have 2490 been handled in dwarf2_gen_line_info_1(). */ 2491 if (s->head->head && SEG_NORMAL (s->seg)) 2492 process_entries (s->seg, s->head->head); 2493 2494 if (flag_dwarf_sections) 2495 /* We have to switch to the special .debug_line_end section 2496 before emitting the end-of-debug_line symbol. The linker 2497 script arranges for this section to be placed after all the 2498 (potentially garbage collected) .debug_line.<foo> sections. 2499 This section contains the line_end symbol which is used to 2500 compute the size of the linked .debug_line section, as seen 2501 in the DWARF Line Number header. */ 2502 subseg_set (subseg_get (".debug_line_end", false), 0); 2503 2504 symbol_set_value_now (line_end); 2505} 2506 2507static void 2508out_debug_ranges (segT ranges_seg, symbolS **ranges_sym) 2509{ 2510 unsigned int addr_size = sizeof_address; 2511 struct line_seg *s; 2512 expressionS exp; 2513 unsigned int i; 2514 2515 memset (&exp, 0, sizeof exp); 2516 subseg_set (ranges_seg, 0); 2517 2518 /* For DW_AT_ranges to point at (there is no header, so really start 2519 of section, but see out_debug_rnglists). */ 2520 *ranges_sym = symbol_temp_new_now_octets (); 2521 2522 /* Base Address Entry. */ 2523 for (i = 0; i < addr_size; i++) 2524 out_byte (0xff); 2525 for (i = 0; i < addr_size; i++) 2526 out_byte (0); 2527 2528 /* Range List Entry. */ 2529 for (s = all_segs; s; s = s->next) 2530 { 2531 fragS *frag; 2532 symbolS *beg, *end; 2533 2534 frag = first_frag_for_seg (s->seg); 2535 beg = symbol_temp_new (s->seg, frag, 0); 2536 s->text_start = beg; 2537 2538 frag = last_frag_for_seg (s->seg); 2539 end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg)); 2540 s->text_end = end; 2541 2542 exp.X_op = O_symbol; 2543 exp.X_add_symbol = beg; 2544 exp.X_add_number = 0; 2545 emit_expr (&exp, addr_size); 2546 2547 exp.X_op = O_symbol; 2548 exp.X_add_symbol = end; 2549 exp.X_add_number = 0; 2550 emit_expr (&exp, addr_size); 2551 } 2552 2553 /* End of Range Entry. */ 2554 for (i = 0; i < addr_size; i++) 2555 out_byte (0); 2556 for (i = 0; i < addr_size; i++) 2557 out_byte (0); 2558} 2559 2560static void 2561out_debug_rnglists (segT ranges_seg, symbolS **ranges_sym) 2562{ 2563 expressionS exp; 2564 symbolS *ranges_end; 2565 struct line_seg *s; 2566 2567 /* Unit length. */ 2568 memset (&exp, 0, sizeof exp); 2569 out_header (ranges_seg, &exp); 2570 ranges_end = exp.X_add_symbol; 2571 2572 out_two (DWARF2_RNGLISTS_VERSION); 2573 out_byte (sizeof_address); 2574 out_byte (0); /* Segment Selector size. */ 2575 out_four (0); /* Offset entry count. */ 2576 2577 /* For DW_AT_ranges to point at (must be after the header). */ 2578 *ranges_sym = symbol_temp_new_now_octets (); 2579 2580 for (s = all_segs; s; s = s->next) 2581 { 2582 fragS *frag; 2583 symbolS *beg, *end; 2584 2585 out_byte (DW_RLE_start_length); 2586 2587 frag = first_frag_for_seg (s->seg); 2588 beg = symbol_temp_new (s->seg, frag, 0); 2589 s->text_start = beg; 2590 2591 frag = last_frag_for_seg (s->seg); 2592 end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg)); 2593 s->text_end = end; 2594 2595 exp.X_op = O_symbol; 2596 exp.X_add_symbol = beg; 2597 exp.X_add_number = 0; 2598 emit_expr (&exp, sizeof_address); 2599 2600 exp.X_op = O_symbol; 2601 exp.X_add_symbol = end; 2602 exp.X_add_number = 0; 2603 emit_leb128_expr (&exp, 0); 2604 } 2605 2606 out_byte (DW_RLE_end_of_list); 2607 2608 symbol_set_value_now (ranges_end); 2609} 2610 2611/* Emit data for .debug_aranges. */ 2612 2613static void 2614out_debug_aranges (segT aranges_seg, segT info_seg) 2615{ 2616 unsigned int addr_size = sizeof_address; 2617 offsetT size; 2618 struct line_seg *s; 2619 expressionS exp; 2620 symbolS *aranges_end; 2621 char *p; 2622 int sizeof_offset; 2623 2624 memset (&exp, 0, sizeof exp); 2625 sizeof_offset = out_header (aranges_seg, &exp); 2626 aranges_end = exp.X_add_symbol; 2627 size = -exp.X_add_number; 2628 2629 /* Version. */ 2630 out_two (DWARF2_ARANGES_VERSION); 2631 size += 2; 2632 2633 /* Offset to .debug_info. */ 2634 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset); 2635 size += sizeof_offset; 2636 2637 /* Size of an address (offset portion). */ 2638 out_byte (addr_size); 2639 size++; 2640 2641 /* Size of a segment descriptor. */ 2642 out_byte (0); 2643 size++; 2644 2645 /* Align the header. */ 2646 while ((size++ % (2 * addr_size)) > 0) 2647 out_byte (0); 2648 2649 for (s = all_segs; s; s = s->next) 2650 { 2651 fragS *frag; 2652 symbolS *beg, *end; 2653 2654 frag = first_frag_for_seg (s->seg); 2655 beg = symbol_temp_new (s->seg, frag, 0); 2656 s->text_start = beg; 2657 2658 frag = last_frag_for_seg (s->seg); 2659 end = symbol_temp_new (s->seg, frag, get_frag_fix (frag, s->seg)); 2660 s->text_end = end; 2661 2662 exp.X_op = O_symbol; 2663 exp.X_add_symbol = beg; 2664 exp.X_add_number = 0; 2665 emit_expr (&exp, addr_size); 2666 2667 exp.X_op = O_subtract; 2668 exp.X_add_symbol = end; 2669 exp.X_op_symbol = beg; 2670 exp.X_add_number = 0; 2671 emit_expr (&exp, addr_size); 2672 } 2673 2674 p = frag_more (2 * addr_size); 2675 md_number_to_chars (p, 0, addr_size); 2676 md_number_to_chars (p + addr_size, 0, addr_size); 2677 2678 symbol_set_value_now (aranges_end); 2679} 2680 2681/* Emit data for .debug_abbrev. Note that this must be kept in 2682 sync with out_debug_info below. */ 2683 2684static void 2685out_debug_abbrev (segT abbrev_seg, 2686 segT info_seg ATTRIBUTE_UNUSED, 2687 segT line_seg ATTRIBUTE_UNUSED, 2688 unsigned char *func_formP) 2689{ 2690 int secoff_form; 2691 bool have_efunc = false, have_lfunc = false; 2692 2693 /* Check the symbol table for function symbols which also have their size 2694 specified. */ 2695 if (symbol_rootP) 2696 { 2697 symbolS *symp; 2698 2699 for (symp = symbol_rootP; symp; symp = symbol_next (symp)) 2700 { 2701 /* A warning construct is a warning symbol followed by the 2702 symbol warned about. Skip this and the following symbol. */ 2703 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING) 2704 { 2705 symp = symbol_next (symp); 2706 if (!symp) 2707 break; 2708 continue; 2709 } 2710 2711 if (!S_IS_DEFINED (symp) || !S_IS_FUNCTION (symp)) 2712 continue; 2713 2714#if defined (OBJ_ELF) /* || defined (OBJ_MAYBE_ELF) */ 2715 if (S_GET_SIZE (symp) == 0) 2716 { 2717 if (!IS_ELF || symbol_get_obj (symp)->size == NULL) 2718 continue; 2719 } 2720#else 2721 continue; 2722#endif 2723 2724 if (S_IS_EXTERNAL (symp)) 2725 have_efunc = true; 2726 else 2727 have_lfunc = true; 2728 } 2729 } 2730 2731 subseg_set (abbrev_seg, 0); 2732 2733 out_uleb128 (1); 2734 out_uleb128 (DW_TAG_compile_unit); 2735 out_byte (have_efunc || have_lfunc ? DW_CHILDREN_yes : DW_CHILDREN_no); 2736 if (DWARF2_VERSION < 4) 2737 { 2738 if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit) 2739 secoff_form = DW_FORM_data4; 2740 else 2741 secoff_form = DW_FORM_data8; 2742 } 2743 else 2744 secoff_form = DW_FORM_sec_offset; 2745 out_abbrev (DW_AT_stmt_list, secoff_form); 2746 if (all_segs->next == NULL) 2747 { 2748 out_abbrev (DW_AT_low_pc, DW_FORM_addr); 2749 if (DWARF2_VERSION < 4) 2750 out_abbrev (DW_AT_high_pc, DW_FORM_addr); 2751 else 2752 out_abbrev (DW_AT_high_pc, DW_FORM_udata); 2753 } 2754 else 2755 out_abbrev (DW_AT_ranges, secoff_form); 2756 out_abbrev (DW_AT_name, DW_FORM_strp); 2757 out_abbrev (DW_AT_comp_dir, DW_FORM_strp); 2758 out_abbrev (DW_AT_producer, DW_FORM_strp); 2759 out_abbrev (DW_AT_language, DW_FORM_data2); 2760 out_abbrev (0, 0); 2761 2762 if (have_efunc || have_lfunc) 2763 { 2764 out_uleb128 (2); 2765 out_uleb128 (DW_TAG_subprogram); 2766 out_byte (DW_CHILDREN_no); 2767 out_abbrev (DW_AT_name, DW_FORM_strp); 2768 if (have_efunc) 2769 { 2770 if (have_lfunc || DWARF2_VERSION < 4) 2771 *func_formP = DW_FORM_flag; 2772 else 2773 *func_formP = DW_FORM_flag_present; 2774 out_abbrev (DW_AT_external, *func_formP); 2775 } 2776 else 2777 /* Any non-zero value other than DW_FORM_flag will do. */ 2778 *func_formP = DW_FORM_block; 2779 out_abbrev (DW_AT_low_pc, DW_FORM_addr); 2780 out_abbrev (DW_AT_high_pc, 2781 DWARF2_VERSION < 4 ? DW_FORM_addr : DW_FORM_udata); 2782 out_abbrev (0, 0); 2783 } 2784 2785 /* Terminate the abbreviations for this compilation unit. */ 2786 out_byte (0); 2787} 2788 2789/* Emit a description of this compilation unit for .debug_info. */ 2790 2791static void 2792out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT str_seg, 2793 symbolS *ranges_sym, symbolS *name_sym, 2794 symbolS *comp_dir_sym, symbolS *producer_sym, 2795 unsigned char func_form) 2796{ 2797 expressionS exp; 2798 symbolS *info_end; 2799 int sizeof_offset; 2800 2801 memset (&exp, 0, sizeof exp); 2802 sizeof_offset = out_header (info_seg, &exp); 2803 info_end = exp.X_add_symbol; 2804 2805 /* DWARF version. */ 2806 out_two (DWARF2_VERSION); 2807 2808 if (DWARF2_VERSION < 5) 2809 { 2810 /* .debug_abbrev offset */ 2811 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset); 2812 } 2813 else 2814 { 2815 /* unit (header) type */ 2816 out_byte (DW_UT_compile); 2817 } 2818 2819 /* Target address size. */ 2820 out_byte (sizeof_address); 2821 2822 if (DWARF2_VERSION >= 5) 2823 { 2824 /* .debug_abbrev offset */ 2825 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset); 2826 } 2827 2828 /* DW_TAG_compile_unit DIE abbrev */ 2829 out_uleb128 (1); 2830 2831 /* DW_AT_stmt_list */ 2832 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 2833 (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit 2834 ? 4 : 8)); 2835 2836 /* These two attributes are emitted if all of the code is contiguous. */ 2837 if (all_segs->next == NULL) 2838 { 2839 /* DW_AT_low_pc */ 2840 exp.X_op = O_symbol; 2841 exp.X_add_symbol = all_segs->text_start; 2842 exp.X_add_number = 0; 2843 emit_expr (&exp, sizeof_address); 2844 2845 /* DW_AT_high_pc */ 2846 if (DWARF2_VERSION < 4) 2847 exp.X_op = O_symbol; 2848 else 2849 { 2850 exp.X_op = O_subtract; 2851 exp.X_op_symbol = all_segs->text_start; 2852 } 2853 exp.X_add_symbol = all_segs->text_end; 2854 exp.X_add_number = 0; 2855 if (DWARF2_VERSION < 4) 2856 emit_expr (&exp, sizeof_address); 2857 else 2858 emit_leb128_expr (&exp, 0); 2859 } 2860 else 2861 { 2862 /* This attribute is emitted if the code is disjoint. */ 2863 /* DW_AT_ranges. */ 2864 TC_DWARF2_EMIT_OFFSET (ranges_sym, sizeof_offset); 2865 } 2866 2867 /* DW_AT_name, DW_AT_comp_dir and DW_AT_producer. Symbols in .debug_str 2868 setup in out_debug_str below. */ 2869 TC_DWARF2_EMIT_OFFSET (name_sym, sizeof_offset); 2870 TC_DWARF2_EMIT_OFFSET (comp_dir_sym, sizeof_offset); 2871 TC_DWARF2_EMIT_OFFSET (producer_sym, sizeof_offset); 2872 2873 /* DW_AT_language. Yes, this is probably not really MIPS, but the 2874 dwarf2 draft has no standard code for assembler. */ 2875 out_two (DW_LANG_Mips_Assembler); 2876 2877 if (func_form) 2878 { 2879 symbolS *symp; 2880 2881 for (symp = symbol_rootP; symp; symp = symbol_next (symp)) 2882 { 2883 const char *name; 2884 size_t len; 2885 2886 /* Skip warning constructs (see above). */ 2887 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING) 2888 { 2889 symp = symbol_next (symp); 2890 if (!symp) 2891 break; 2892 continue; 2893 } 2894 2895 if (!S_IS_DEFINED (symp) || !S_IS_FUNCTION (symp)) 2896 continue; 2897 2898 subseg_set (str_seg, 0); 2899 name_sym = symbol_temp_new_now_octets (); 2900 name = S_GET_NAME (symp); 2901 len = strlen (name) + 1; 2902 memcpy (frag_more (len), name, len); 2903 2904 subseg_set (info_seg, 0); 2905 2906 /* DW_TAG_subprogram DIE abbrev */ 2907 out_uleb128 (2); 2908 2909 /* DW_AT_name */ 2910 TC_DWARF2_EMIT_OFFSET (name_sym, sizeof_offset); 2911 2912 /* DW_AT_external. */ 2913 if (func_form == DW_FORM_flag) 2914 out_byte (S_IS_EXTERNAL (symp)); 2915 2916 /* DW_AT_low_pc */ 2917 exp.X_op = O_symbol; 2918 exp.X_add_symbol = symp; 2919 exp.X_add_number = 0; 2920 emit_expr (&exp, sizeof_address); 2921 2922 /* DW_AT_high_pc */ 2923 exp.X_op = O_constant; 2924#if defined (OBJ_ELF) /* || defined (OBJ_MAYBE_ELF) */ 2925 exp.X_add_number = S_GET_SIZE (symp); 2926 if (exp.X_add_number == 0 && IS_ELF 2927 && symbol_get_obj (symp)->size != NULL) 2928 { 2929 exp.X_op = O_add; 2930 exp.X_op_symbol = make_expr_symbol (symbol_get_obj (symp)->size); 2931 } 2932#else 2933 exp.X_add_number = 0; 2934#endif 2935 if (DWARF2_VERSION < 4) 2936 { 2937 if (exp.X_op == O_constant) 2938 exp.X_op = O_symbol; 2939 exp.X_add_symbol = symp; 2940 emit_expr (&exp, sizeof_address); 2941 } 2942 else if (exp.X_op == O_constant) 2943 out_uleb128 (exp.X_add_number); 2944 else 2945 emit_leb128_expr (symbol_get_value_expression (exp.X_op_symbol), 0); 2946 } 2947 2948 /* End of children. */ 2949 out_leb128 (0); 2950 } 2951 2952 symbol_set_value_now (info_end); 2953} 2954 2955/* Emit the three debug strings needed in .debug_str and setup symbols 2956 to them for use in out_debug_info. */ 2957static void 2958out_debug_str (segT str_seg, symbolS **name_sym, symbolS **comp_dir_sym, 2959 symbolS **producer_sym) 2960{ 2961 char producer[128]; 2962 char *p; 2963 int len; 2964 int first_file = DWARF2_LINE_VERSION > 4 ? 0 : 1; 2965 2966 subseg_set (str_seg, 0); 2967 2968 /* DW_AT_name. We don't have the actual file name that was present 2969 on the command line, so assume files[first_file] is the main input file. 2970 We're not supposed to get called unless at least one line number 2971 entry was emitted, so this should always be defined. */ 2972 *name_sym = symbol_temp_new_now_octets (); 2973 if (files_in_use == 0) 2974 abort (); 2975 if (files[first_file].dir) 2976 { 2977 char *dirname = remap_debug_filename (dirs[files[first_file].dir]); 2978 len = strlen (dirname); 2979#ifdef TE_VMS 2980 /* Already has trailing slash. */ 2981 p = frag_more (len); 2982 memcpy (p, dirname, len); 2983#else 2984 p = frag_more (len + 1); 2985 memcpy (p, dirname, len); 2986 INSERT_DIR_SEPARATOR (p, len); 2987#endif 2988 free (dirname); 2989 } 2990 len = strlen (files[first_file].filename) + 1; 2991 p = frag_more (len); 2992 memcpy (p, files[first_file].filename, len); 2993 2994 /* DW_AT_comp_dir */ 2995 *comp_dir_sym = symbol_temp_new_now_octets (); 2996 char *comp_dir = remap_debug_filename (getpwd ()); 2997 len = strlen (comp_dir) + 1; 2998 p = frag_more (len); 2999 memcpy (p, comp_dir, len); 3000 free (comp_dir); 3001 3002 /* DW_AT_producer */ 3003 *producer_sym = symbol_temp_new_now_octets (); 3004 sprintf (producer, "GNU AS %s", VERSION); 3005 len = strlen (producer) + 1; 3006 p = frag_more (len); 3007 memcpy (p, producer, len); 3008} 3009 3010void 3011dwarf2_init (void) 3012{ 3013 all_segs = NULL; 3014 last_seg_ptr = &all_segs; 3015 files = NULL; 3016 files_in_use = 0; 3017 files_allocated = 0; 3018 dirs = NULL; 3019 dirs_in_use = 0; 3020 dirs_allocated = 0; 3021 dwarf2_loc_directive_seen = false; 3022 dwarf2_any_loc_directive_seen = false; 3023 dwarf2_loc_mark_labels = false; 3024 current.filenum = 1; 3025 current.line = 1; 3026 current.column = 0; 3027 current.isa = 0; 3028 current.flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 3029 current.discriminator = 0; 3030 current.u.view = NULL; 3031 force_reset_view = NULL; 3032 view_assert_failed = NULL; 3033 3034 /* Select the default CIE version to produce here. The global 3035 starts with a value of -1 and will be modified to a valid value 3036 either by the user providing a command line option, or some 3037 targets will select their own default in md_after_parse_args. If 3038 we get here and the global still contains -1 then it is up to us 3039 to pick a sane default. The default we choose is 1, this is the 3040 CIE version gas has produced for a long time, and there seems no 3041 reason to change it yet. */ 3042 if (flag_dwarf_cie_version == -1) 3043 flag_dwarf_cie_version = 1; 3044} 3045 3046static void 3047dwarf2_cleanup (void) 3048{ 3049 purge_generated_debug (true); 3050 free (files); 3051 free (dirs); 3052} 3053 3054/* Finish the dwarf2 debug sections. We emit .debug.line if there 3055 were any .file/.loc directives, or --gdwarf2 was given, and if the 3056 file has a non-empty .debug_info section and an empty .debug_line 3057 section. If we emit .debug_line, and the .debug_info section is 3058 empty, we also emit .debug_info, .debug_aranges and .debug_abbrev. 3059 ALL_SEGS will be non-null if there were any .file/.loc directives, 3060 or --gdwarf2 was given and there were any located instructions 3061 emitted. */ 3062 3063void 3064dwarf2_finish (void) 3065{ 3066 segT line_seg; 3067 struct line_seg *s; 3068 segT info_seg; 3069 int emit_other_sections = 0; 3070 int empty_debug_line = 0; 3071 3072 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info"); 3073 emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg); 3074 3075 line_seg = bfd_get_section_by_name (stdoutput, ".debug_line"); 3076 empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg); 3077 3078 /* We can't construct a new debug_line section if we already have one. 3079 Give an error if we have seen any .loc, otherwise trust the user 3080 knows what they are doing and want to generate the .debug_line 3081 (and all other debug sections) themselves. */ 3082 if (all_segs && !empty_debug_line && dwarf2_any_loc_directive_seen) 3083 as_fatal ("duplicate .debug_line sections"); 3084 3085 if ((!all_segs && emit_other_sections) 3086 || (!emit_other_sections && !empty_debug_line)) 3087 /* If there is no line information and no non-empty .debug_info 3088 section, or if there is both a non-empty .debug_info and a non-empty 3089 .debug_line, then we do nothing. */ 3090 { 3091 dwarf2_cleanup (); 3092 return; 3093 } 3094 3095 /* Calculate the size of an address for the target machine. */ 3096 sizeof_address = DWARF2_ADDR_SIZE (stdoutput); 3097 3098 /* Create and switch to the line number section. */ 3099 if (empty_debug_line) 3100 { 3101 line_seg = subseg_new (".debug_line", 0); 3102 bfd_set_section_flags (line_seg, 3103 SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); 3104 } 3105 3106 for (s = all_segs; s; s = s->next) 3107 { 3108 struct line_subseg *lss; 3109 3110 for (lss = s->head; lss; lss = lss->next) 3111 if (lss->head) 3112 do_allocate_filenum (lss->head); 3113 } 3114 3115 /* For each subsection, chain the debug entries together. */ 3116 for (s = all_segs; s; s = s->next) 3117 { 3118 struct line_subseg *lss = s->head; 3119 struct line_entry **ptail = lss->ptail; 3120 3121 /* Reset the initial view of the first subsection of the 3122 section. */ 3123 if (lss->head && lss->head->loc.u.view) 3124 set_or_check_view (lss->head, NULL, NULL); 3125 3126 while ((lss = lss->next) != NULL) 3127 { 3128 /* Link the first view of subsequent subsections to the 3129 previous view. */ 3130 if (lss->head && lss->head->loc.u.view) 3131 set_or_check_view (lss->head, 3132 !s->head ? NULL : (struct line_entry *)ptail, 3133 s->head ? s->head->head : NULL); 3134 *ptail = lss->head; 3135 lss->head = NULL; 3136 ptail = lss->ptail; 3137 } 3138 } 3139 3140 if (empty_debug_line) 3141 out_debug_line (line_seg); 3142 3143 /* If this is assembler generated line info, and there is no 3144 debug_info already, we need .debug_info, .debug_abbrev and 3145 .debug_str sections as well. */ 3146 if (emit_other_sections) 3147 { 3148 segT abbrev_seg; 3149 segT aranges_seg; 3150 segT str_seg; 3151 symbolS *name_sym, *comp_dir_sym, *producer_sym, *ranges_sym; 3152 unsigned char func_form = 0; 3153 3154 gas_assert (all_segs); 3155 3156 info_seg = subseg_new (".debug_info", 0); 3157 abbrev_seg = subseg_new (".debug_abbrev", 0); 3158 aranges_seg = subseg_new (".debug_aranges", 0); 3159 str_seg = subseg_new (".debug_str", 0); 3160 3161 bfd_set_section_flags (info_seg, 3162 SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); 3163 bfd_set_section_flags (abbrev_seg, 3164 SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); 3165 bfd_set_section_flags (aranges_seg, 3166 SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS); 3167 bfd_set_section_flags (str_seg, 3168 SEC_READONLY | SEC_DEBUGGING | SEC_OCTETS 3169 | SEC_MERGE | SEC_STRINGS); 3170 str_seg->entsize = 1; 3171 3172 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1); 3173 3174 if (all_segs->next == NULL) 3175 ranges_sym = NULL; 3176 else 3177 { 3178 if (DWARF2_VERSION < 5) 3179 { 3180 segT ranges_seg = subseg_new (".debug_ranges", 0); 3181 bfd_set_section_flags (ranges_seg, (SEC_READONLY 3182 | SEC_DEBUGGING 3183 | SEC_OCTETS)); 3184 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1); 3185 out_debug_ranges (ranges_seg, &ranges_sym); 3186 } 3187 else 3188 { 3189 segT rnglists_seg = subseg_new (".debug_rnglists", 0); 3190 bfd_set_section_flags (rnglists_seg, (SEC_READONLY 3191 | SEC_DEBUGGING 3192 | SEC_OCTETS)); 3193 out_debug_rnglists (rnglists_seg, &ranges_sym); 3194 } 3195 } 3196 3197 out_debug_aranges (aranges_seg, info_seg); 3198 out_debug_abbrev (abbrev_seg, info_seg, line_seg, &func_form); 3199 out_debug_str (str_seg, &name_sym, &comp_dir_sym, &producer_sym); 3200 out_debug_info (info_seg, abbrev_seg, line_seg, str_seg, 3201 ranges_sym, name_sym, comp_dir_sym, producer_sym, 3202 func_form); 3203 } 3204 dwarf2_cleanup (); 3205} 3206 3207/* Perform any deferred checks pertaining to debug information. */ 3208 3209void 3210dwarf2dbg_final_check (void) 3211{ 3212 /* Perform reset-view checks. Don't evaluate view_assert_failed 3213 recursively: it could be very deep. It's a chain of adds, with 3214 each chain element pointing to the next in X_add_symbol, and 3215 holding the check value in X_op_symbol. */ 3216 while (view_assert_failed) 3217 { 3218 expressionS *exp; 3219 symbolS *sym; 3220 offsetT failed; 3221 3222 gas_assert (!symbol_resolved_p (view_assert_failed)); 3223 3224 exp = symbol_get_value_expression (view_assert_failed); 3225 sym = view_assert_failed; 3226 3227 /* If view_assert_failed looks like a compound check in the 3228 chain, break it up. */ 3229 if (exp->X_op == O_add && exp->X_add_number == 0 && exp->X_unsigned) 3230 { 3231 view_assert_failed = exp->X_add_symbol; 3232 sym = exp->X_op_symbol; 3233 } 3234 else 3235 view_assert_failed = NULL; 3236 3237 failed = resolve_symbol_value (sym); 3238 if (!symbol_resolved_p (sym) || failed) 3239 { 3240 as_bad (_("view number mismatch")); 3241 break; 3242 } 3243 } 3244} 3245