1 /* dwarf2dbg.c - DWARF2 debug support 2 Copyright (C) 1999-2016 Free Software Foundation, Inc. 3 Contributed by David Mosberger-Tang <davidm (at) 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 34 #ifdef HAVE_LIMITS_H 35 #include <limits.h> 36 #else 37 #ifdef HAVE_SYS_PARAM_H 38 #include <sys/param.h> 39 #endif 40 #ifndef INT_MAX 41 #define INT_MAX (int) (((unsigned) (-1)) >> 1) 42 #endif 43 #endif 44 45 #include "dwarf2dbg.h" 46 #include <filenames.h> 47 #include <stdint.h> 48 49 #include "hash.h" 50 51 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 52 /* We need to decide which character to use as a directory separator. 53 Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not 54 necessarily mean that the backslash character is the one to use. 55 Some environments, eg Cygwin, can support both naming conventions. 56 So we use the heuristic that we only need to use the backslash if 57 the path is an absolute path starting with a DOS style drive 58 selector. eg C: or D: */ 59 # define INSERT_DIR_SEPARATOR(string, offset) \ 60 do \ 61 { \ 62 if (offset > 1 \ 63 && string[0] != 0 \ 64 && string[1] == ':') \ 65 string [offset] = '\\'; \ 66 else \ 67 string [offset] = '/'; \ 68 } \ 69 while (0) 70 #else 71 # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/' 72 #endif 73 74 #ifndef DWARF2_FORMAT 75 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit 76 #endif 77 78 #ifndef DWARF2_ADDR_SIZE 79 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8) 80 #endif 81 82 #ifndef DWARF2_FILE_NAME 83 #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME 84 #endif 85 86 #ifndef DWARF2_FILE_TIME_NAME 87 #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0 88 #endif 89 90 #ifndef DWARF2_FILE_SIZE_NAME 91 #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0 92 #endif 93 94 #ifndef DWARF2_VERSION 95 #define DWARF2_VERSION 2 96 #endif 97 98 /* The .debug_aranges version has been 2 in DWARF version 2, 3 and 4. */ 99 #ifndef DWARF2_ARANGES_VERSION 100 #define DWARF2_ARANGES_VERSION 2 101 #endif 102 103 /* This implementation output version 2 .debug_line information. */ 104 #ifndef DWARF2_LINE_VERSION 105 #define DWARF2_LINE_VERSION 2 106 #endif 107 /* If we see .lloc directives, generate an experimental version 6. */ 108 #ifndef DWARF2_LINE_EXPERIMENTAL_VERSION 109 #define DWARF2_LINE_EXPERIMENTAL_VERSION 0xf006 110 #endif 111 112 #include "subsegs.h" 113 114 #include "dwarf2.h" 115 116 /* Since we can't generate the prolog until the body is complete, we 117 use three different subsegments for .debug_line: one holding the 118 prolog, one for the directory and filename info, and one for the 119 body ("statement program"). */ 120 #define DL_PROLOG 0 121 #define DL_FILES 1 122 #define DL_BODY 2 123 124 /* If linker relaxation might change offsets in the code, the DWARF special 125 opcodes and variable-length operands cannot be used. If this macro is 126 nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */ 127 #ifndef DWARF2_USE_FIXED_ADVANCE_PC 128 # define DWARF2_USE_FIXED_ADVANCE_PC linkrelax 129 #endif 130 131 /* First special line opcde - leave room for the standard opcodes. 132 Note: If you want to change this, you'll have to update the 133 "standard_opcode_lengths" table that is emitted below in 134 out_debug_line(). */ 135 #define DWARF2_LINE_OPCODE_BASE 13 136 #define DWARF5_EXPERIMENTAL_LINE_OPCODE_BASE 16 137 138 static int opcode_base; 139 static int line_base; 140 static unsigned int line_range; 141 142 #ifndef DWARF2_LINE_BASE 143 /* Minimum line offset in a special line info. opcode. This value 144 was chosen to give a reasonable range of values. */ 145 # define DWARF2_LINE_BASE -5 146 #endif 147 148 /* Range of line offsets in a special line info. opcode. */ 149 #ifndef DWARF2_LINE_RANGE 150 # define DWARF2_LINE_RANGE 14 151 #endif 152 153 /* For two-level line tables, these values work a bit better. */ 154 #define DWARF5_EXPERIMENTAL_LINE_BASE -3 155 #define DWARF5_EXPERIMENTAL_LINE_RANGE 10 156 157 #ifndef DWARF2_LINE_MIN_INSN_LENGTH 158 /* Define the architecture-dependent minimum instruction length (in 159 bytes). This value should be rather too small than too big. */ 160 # define DWARF2_LINE_MIN_INSN_LENGTH 1 161 #endif 162 163 #ifndef DWARF2_LINE_MAX_OPS_PER_INSN 164 # define DWARF2_LINE_MAX_OPS_PER_INSN 1 165 #endif 166 167 /* Flag that indicates the initial value of the is_stmt_start flag. */ 168 #define DWARF2_LINE_DEFAULT_IS_STMT 1 169 170 /* Given a special op, return the line skip amount. */ 171 #define SPECIAL_LINE(op) \ 172 (((op) - opcode_base) % line_range + line_base) 173 174 /* Given a special op, return the address skip amount (in units of 175 DWARF2_LINE_MIN_INSN_LENGTH. */ 176 #define SPECIAL_ADDR(op) (((op) - opcode_base) / line_range) 177 178 /* The maximum address skip amount that can be encoded with a special op. */ 179 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) 180 181 #ifndef TC_PARSE_CONS_RETURN_NONE 182 #define TC_PARSE_CONS_RETURN_NONE BFD_RELOC_NONE 183 #endif 184 185 struct line_entry { 186 struct line_entry *next; 187 symbolS *label; 188 struct dwarf2_line_info loc; 189 }; 190 191 struct line_subseg { 192 struct line_subseg *next; 193 subsegT subseg; 194 struct line_entry *head; 195 struct line_entry **ptail; 196 struct line_entry **pmove_tail; 197 }; 198 199 struct line_seg { 200 struct line_seg *next; 201 segT seg; 202 struct line_subseg *head; 203 symbolS *text_start; 204 symbolS *text_end; 205 }; 206 207 /* Collects data for all line table entries during assembly. */ 208 static struct line_seg *all_segs; 209 static struct line_seg **last_seg_ptr; 210 211 struct file_entry { 212 const char *filename; 213 unsigned int dir; 214 }; 215 216 /* Table of files used by .debug_line. */ 217 static struct file_entry *files; 218 static unsigned int files_in_use; 219 static unsigned int files_allocated; 220 221 /* Table of directories used by .debug_line. */ 222 static char **dirs; 223 static unsigned int dirs_in_use; 224 static unsigned int dirs_allocated; 225 226 /* Experimental DWARF-5 Extension: Table of subprograms. */ 227 struct subprog_entry { 228 const char *subpname; 229 unsigned int filenum; 230 unsigned int line; 231 }; 232 233 static struct subprog_entry *subprogs; 234 static unsigned int subprogs_in_use; 235 static unsigned int subprogs_allocated; 236 237 /* Experimental DWARF-5 Extension: Logicals table. */ 238 struct logicals_entry { 239 segT seg; 240 symbolS *label; 241 /* A logical row doesn't use every field in this struct, but using it 242 here makes the code for writing the line number program simpler. */ 243 struct dwarf2_line_info loc; 244 unsigned int context; 245 unsigned int subprog; 246 }; 247 248 static struct logicals_entry *logicals; 249 static unsigned int logicals_in_use; 250 static unsigned int logicals_allocated = 0; 251 static unsigned int logicals_with_labels = 0; 252 253 /* DWARF-5: .debug_line_str string table. */ 254 struct string_table { 255 struct hash_control *hashtab; 256 const char **strings; 257 unsigned int strings_in_use; 258 unsigned int strings_allocated; 259 offsetT next_offset; 260 }; 261 262 static struct string_table debug_line_str_table; 263 264 /* TRUE when we've seen a .loc directive recently. Used to avoid 265 doing work when there's nothing to do. */ 266 bfd_boolean dwarf2_loc_directive_seen; 267 268 /* TRUE when we're supposed to set the basic block mark whenever a 269 label is seen. */ 270 bfd_boolean dwarf2_loc_mark_labels; 271 272 /* Current location as indicated by the most recent .loc directive. */ 273 static struct dwarf2_line_info current = { 274 1, 1, 0, 0, /* filenum, line, column, isa */ 275 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, /* flags */ 276 0, 0 /* discriminator, logical */ 277 }; 278 279 /* The size of an address on the target. */ 280 static unsigned int sizeof_address; 281 282 static unsigned int get_filenum (const char *, unsigned int); 284 285 #ifndef TC_DWARF2_EMIT_OFFSET 286 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset 287 288 /* Create an offset to .dwarf2_*. */ 289 290 static void 291 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size) 292 { 293 expressionS exp; 294 295 exp.X_op = O_symbol; 296 exp.X_add_symbol = symbol; 297 exp.X_add_number = 0; 298 emit_expr (&exp, size); 299 } 300 #endif 301 302 /* Find or create (if CREATE_P) an entry for SEG+SUBSEG in ALL_SEGS. */ 303 304 static struct line_subseg * 305 get_line_subseg (segT seg, subsegT subseg, bfd_boolean create_p) 306 { 307 struct line_seg *s = seg_info (seg)->dwarf2_line_seg; 308 struct line_subseg **pss, *lss; 309 310 if (s == NULL) 311 { 312 if (!create_p) 313 return NULL; 314 315 s = XNEW (struct line_seg); 316 s->next = NULL; 317 s->seg = seg; 318 s->head = NULL; 319 *last_seg_ptr = s; 320 last_seg_ptr = &s->next; 321 seg_info (seg)->dwarf2_line_seg = s; 322 } 323 gas_assert (seg == s->seg); 324 325 for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next) 326 { 327 if (lss->subseg == subseg) 328 goto found_subseg; 329 if (lss->subseg > subseg) 330 break; 331 } 332 333 lss = XNEW (struct line_subseg); 334 lss->next = *pss; 335 lss->subseg = subseg; 336 lss->head = NULL; 337 lss->ptail = &lss->head; 338 lss->pmove_tail = &lss->head; 339 *pss = lss; 340 341 found_subseg: 342 return lss; 343 } 344 345 /* Record an entry for LOC occurring at LABEL. */ 346 347 static void 348 dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc) 349 { 350 struct line_subseg *lss; 351 struct line_entry *e; 352 353 e = XNEW (struct line_entry); 354 e->next = NULL; 355 e->label = label; 356 e->loc = *loc; 357 358 lss = get_line_subseg (now_seg, now_subseg, TRUE); 359 *lss->ptail = e; 360 lss->ptail = &e->next; 361 } 362 363 /* Record an entry for LOC occurring at OFS within the current fragment. */ 364 365 void 366 dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc) 367 { 368 static unsigned int line = -1; 369 static unsigned int filenum = -1; 370 371 symbolS *sym; 372 373 /* Early out for as-yet incomplete location information. */ 374 if (loc->filenum == 0 || loc->line == 0) 375 return; 376 377 /* Don't emit sequences of line symbols for the same line when the 378 symbols apply to assembler code. It is necessary to emit 379 duplicate line symbols when a compiler asks for them, because GDB 380 uses them to determine the end of the prologue. */ 381 if (debug_type == DEBUG_DWARF2 382 && line == loc->line && filenum == loc->filenum) 383 return; 384 385 line = loc->line; 386 filenum = loc->filenum; 387 388 if (linkrelax) 389 { 390 char name[120]; 391 392 /* Use a non-fake name for the line number location, 393 so that it can be referred to by relocations. */ 394 sprintf (name, ".Loc.%u.%u", line, filenum); 395 sym = symbol_new (name, now_seg, ofs, frag_now); 396 } 397 else 398 sym = symbol_temp_new (now_seg, ofs, frag_now); 399 dwarf2_gen_line_info_1 (sym, loc); 400 401 /* Record the current symbol with all logical rows created since 402 the last emitted instruction. */ 403 while (logicals_with_labels < logicals_in_use) 404 { 405 logicals[logicals_with_labels].label = sym; 406 logicals[logicals_with_labels].seg = now_seg; 407 logicals_with_labels++; 408 } 409 } 410 411 /* Returns the current source information. If .file directives have 412 been encountered, the info for the corresponding source file is 413 returned. Otherwise, the info for the assembly source file is 414 returned. */ 415 416 void 417 dwarf2_where (struct dwarf2_line_info *line) 418 { 419 if (debug_type == DEBUG_DWARF2) 420 { 421 const char *filename = as_where (&line->line); 422 line->filenum = get_filenum (filename, 0); 423 line->column = 0; 424 line->flags = DWARF2_FLAG_IS_STMT; 425 line->isa = current.isa; 426 line->discriminator = current.discriminator; 427 line->logical = 0; 428 } 429 else 430 *line = current; 431 } 432 433 /* A hook to allow the target backend to inform the line number state 434 machine of isa changes when assembler debug info is enabled. */ 435 436 void 437 dwarf2_set_isa (unsigned int isa) 438 { 439 current.isa = isa; 440 } 441 442 /* Called for each machine instruction, or relatively atomic group of 443 machine instructions (ie built-in macro). The instruction or group 444 is SIZE bytes in length. If dwarf2 line number generation is called 445 for, emit a line statement appropriately. */ 446 447 void 448 dwarf2_emit_insn (int size) 449 { 450 struct dwarf2_line_info loc; 451 452 if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2) 453 return; 454 455 dwarf2_where (&loc); 456 457 dwarf2_gen_line_info (frag_now_fix () - size, &loc); 458 dwarf2_consume_line_info (); 459 } 460 461 /* Move all previously-emitted line entries for the current position by 462 DELTA bytes. This function cannot be used to move the same entries 463 twice. */ 464 465 void 466 dwarf2_move_insn (int delta) 467 { 468 struct line_subseg *lss; 469 struct line_entry *e; 470 valueT now; 471 472 if (delta == 0) 473 return; 474 475 lss = get_line_subseg (now_seg, now_subseg, FALSE); 476 if (!lss) 477 return; 478 479 now = frag_now_fix (); 480 while ((e = *lss->pmove_tail)) 481 { 482 if (S_GET_VALUE (e->label) == now) 483 S_SET_VALUE (e->label, now + delta); 484 lss->pmove_tail = &e->next; 485 } 486 } 487 488 /* Called after the current line information has been either used with 489 dwarf2_gen_line_info or saved with a machine instruction for later use. 490 This resets the state of the line number information to reflect that 491 it has been used. */ 492 493 void 494 dwarf2_consume_line_info (void) 495 { 496 /* Unless we generate DWARF2 debugging information for each 497 assembler line, we only emit one line symbol for one LOC. */ 498 dwarf2_loc_directive_seen = FALSE; 499 500 current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK 501 | DWARF2_FLAG_PROLOGUE_END 502 | DWARF2_FLAG_EPILOGUE_BEGIN); 503 current.discriminator = 0; 504 } 505 506 /* Called for each (preferably code) label. If dwarf2_loc_mark_labels 507 is enabled, emit a basic block marker. */ 508 509 void 510 dwarf2_emit_label (symbolS *label) 511 { 512 struct dwarf2_line_info loc; 513 514 if (!dwarf2_loc_mark_labels) 515 return; 516 if (S_GET_SEGMENT (label) != now_seg) 517 return; 518 if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE)) 519 return; 520 if (files_in_use == 0 && debug_type != DEBUG_DWARF2) 521 return; 522 523 dwarf2_where (&loc); 524 525 loc.flags |= DWARF2_FLAG_BASIC_BLOCK; 526 527 dwarf2_gen_line_info_1 (label, &loc); 528 dwarf2_consume_line_info (); 529 } 530 531 /* Get a .debug_line file number for FILENAME. If NUM is nonzero, 532 allocate it on that file table slot, otherwise return the first 533 empty one. */ 534 535 static unsigned int 536 get_filenum (const char *filename, unsigned int num) 537 { 538 static unsigned int last_used, last_used_dir_len; 539 const char *file; 540 size_t dir_len; 541 unsigned int i, dir; 542 543 if (num == 0 && last_used) 544 { 545 if (! files[last_used].dir 546 && filename_cmp (filename, files[last_used].filename) == 0) 547 return last_used; 548 if (files[last_used].dir 549 && filename_ncmp (filename, dirs[files[last_used].dir], 550 last_used_dir_len) == 0 551 && IS_DIR_SEPARATOR (filename [last_used_dir_len]) 552 && filename_cmp (filename + last_used_dir_len + 1, 553 files[last_used].filename) == 0) 554 return last_used; 555 } 556 557 file = lbasename (filename); 558 /* Don't make empty string from / or A: from A:/ . */ 559 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 560 if (file <= filename + 3) 561 file = filename; 562 #else 563 if (file == filename + 1) 564 file = filename; 565 #endif 566 dir_len = file - filename; 567 568 dir = 0; 569 if (dir_len) 570 { 571 #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 572 --dir_len; 573 #endif 574 for (dir = 1; dir < dirs_in_use; ++dir) 575 if (filename_ncmp (filename, dirs[dir], dir_len) == 0 576 && dirs[dir][dir_len] == '\0') 577 break; 578 579 if (dir >= dirs_in_use) 580 { 581 if (dir >= dirs_allocated) 582 { 583 dirs_allocated = dir + 32; 584 dirs = XRESIZEVEC (char *, dirs, dirs_allocated); 585 } 586 587 dirs[dir] = xmemdup0 (filename, dir_len); 588 dirs_in_use = dir + 1; 589 } 590 } 591 592 if (num == 0) 593 { 594 for (i = 1; i < files_in_use; ++i) 595 if (files[i].dir == dir 596 && files[i].filename 597 && filename_cmp (file, files[i].filename) == 0) 598 { 599 last_used = i; 600 last_used_dir_len = dir_len; 601 return i; 602 } 603 } 604 else 605 i = num; 606 607 if (i >= files_allocated) 608 { 609 unsigned int old = files_allocated; 610 611 files_allocated = i + 32; 612 files = XRESIZEVEC (struct file_entry, files, files_allocated); 613 614 memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry)); 615 } 616 617 files[i].filename = num ? file : xstrdup (file); 618 files[i].dir = dir; 619 if (files_in_use < i + 1) 620 files_in_use = i + 1; 621 last_used = i; 622 last_used_dir_len = dir_len; 623 624 return i; 625 } 626 627 /* Make a new entry in the subprograms table. */ 628 629 static void 630 make_subprog_entry (unsigned int num, char *subpname, int filenum, int line) 631 { 632 if (subprogs_allocated == 0) 633 { 634 subprogs_allocated = 4; 635 subprogs = (struct subprog_entry *) 636 xcalloc (subprogs_allocated, sizeof (struct subprog_entry)); 637 } 638 if (num > subprogs_allocated) 639 { 640 unsigned int old = subprogs_allocated; 641 642 subprogs_allocated *= 2; 643 if (num > subprogs_allocated) 644 subprogs_allocated = num; 645 subprogs = (struct subprog_entry *) 646 xrealloc (subprogs, 647 subprogs_allocated * sizeof (struct subprog_entry)); 648 memset (subprogs + old, 0, 649 (subprogs_allocated - old) * sizeof (struct subprog_entry)); 650 } 651 if (subprogs_in_use < num) 652 subprogs_in_use = num; 653 subprogs[num - 1].subpname = xstrdup (subpname); 654 subprogs[num - 1].filenum = filenum; 655 subprogs[num - 1].line = line; 656 } 657 658 /* Make a new entry in the logicals table. */ 659 660 static void 661 make_logical (unsigned int logical, int context, int subprog) 662 { 663 if (logicals_allocated == 0) 664 { 665 logicals_allocated = 4; 666 logicals = (struct logicals_entry *) 667 xcalloc (logicals_allocated, sizeof (struct logicals_entry)); 668 } 669 if (logical > logicals_allocated) 670 { 671 unsigned int old = logicals_allocated; 672 673 logicals_allocated *= 2; 674 if (logical > logicals_allocated) 675 logicals_allocated = logical; 676 logicals = (struct logicals_entry *) 677 xrealloc (logicals, 678 logicals_allocated * sizeof (struct logicals_entry)); 679 memset (logicals + old, 0, 680 (logicals_allocated - old) * sizeof (struct logicals_entry)); 681 } 682 logicals[logical - 1].loc = current; 683 logicals[logical - 1].context = context; 684 logicals[logical - 1].subprog = subprog; 685 if (logical > logicals_in_use) 686 logicals_in_use = logical; 687 } 688 689 /* Handle two forms of .file directive: 690 - Pass .file "source.c" to s_app_file 691 - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table 692 693 If an entry is added to the file table, return a pointer to the filename. */ 694 695 char * 696 dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED) 697 { 698 offsetT num; 699 char *filename; 700 int filename_len; 701 702 /* Continue to accept a bare string and pass it off. */ 703 SKIP_WHITESPACE (); 704 if (*input_line_pointer == '"') 705 { 706 s_app_file (0); 707 return NULL; 708 } 709 710 num = get_absolute_expression (); 711 filename = demand_copy_C_string (&filename_len); 712 if (filename == NULL) 713 return NULL; 714 demand_empty_rest_of_line (); 715 716 if (num < 1) 717 { 718 as_bad (_("file number less than one")); 719 return NULL; 720 } 721 722 /* A .file directive implies compiler generated debug information is 723 being supplied. Turn off gas generated debug info. */ 724 debug_type = DEBUG_NONE; 725 726 if (num < (int) files_in_use && files[num].filename != 0) 727 { 728 as_bad (_("file number %ld already allocated"), (long) num); 729 return NULL; 730 } 731 732 get_filenum (filename, num); 733 734 return filename; 735 } 736 737 /* Experimental DWARF-5 extension: 738 Implements the .subprog SUBPNO ["SUBPROG" [FILENO LINENO]] directive. 739 FILENO is the file number, LINENO the line number and the 740 (optional) COLUMN the column of the source code that the following 741 instruction corresponds to. FILENO can be 0 to indicate that the 742 filename specified by the textually most recent .file directive 743 should be used. */ 744 void 745 dwarf2_directive_subprog (int dummy ATTRIBUTE_UNUSED) 746 { 747 offsetT num, filenum, line; 748 char *subpname; 749 int subpname_len; 750 751 num = get_absolute_expression (); 752 subpname = demand_copy_C_string (&subpname_len); 753 if (subpname == NULL) 754 return; 755 756 SKIP_WHITESPACE (); 757 filenum = get_absolute_expression (); 758 SKIP_WHITESPACE (); 759 line = get_absolute_expression (); 760 demand_empty_rest_of_line (); 761 762 if (num < 1) 763 { 764 as_bad (_("subprogram number less than one")); 765 return; 766 } 767 768 /* A .subprog directive implies compiler generated debug information is 769 being supplied. Turn off gas generated debug info. */ 770 debug_type = DEBUG_NONE; 771 772 if (num < (int) subprogs_in_use && subprogs[num].subpname != NULL) 773 { 774 as_bad (_("subprogram number %ld already allocated"), (long) num); 775 return; 776 } 777 778 make_subprog_entry (num, subpname, filenum, line); 779 } 780 781 void 782 dwarf2_directive_loc (int is_lloc) 783 { 784 offsetT filenum, line; 785 offsetT logical = 0; 786 offsetT context = 0; 787 offsetT subprog = 0; 788 bfd_boolean is_new_logical = FALSE; 789 bfd_boolean is_actual = FALSE; 790 static bfd_boolean saw_loc = FALSE; 791 static bfd_boolean saw_lloc = FALSE; 792 static bfd_boolean saw_both = FALSE; 793 794 if ((is_lloc && saw_loc) || (!is_lloc && saw_lloc)) 795 { 796 if (!saw_both) 797 as_bad (_(".loc and .lloc cannot both be used")); 798 saw_both = TRUE; 799 return; 800 } 801 802 if (is_lloc) 803 { 804 saw_lloc = TRUE; 805 logical = get_absolute_expression (); 806 SKIP_WHITESPACE (); 807 808 if (ISDIGIT (*input_line_pointer)) 809 is_new_logical = TRUE; 810 else 811 is_actual = TRUE; 812 813 if (logical < 1) 814 { 815 as_bad (_("logical row less than one")); 816 return; 817 } 818 if (is_actual && 819 ((unsigned int) logical > logicals_in_use 820 || logicals[logical - 1].loc.line == 0)) 821 { 822 as_bad (_("unassigned logical row %ld"), (long) logical); 823 return; 824 } 825 } 826 else 827 saw_loc = TRUE; 828 829 /* If we see two .loc directives in a row, force the first one to be 830 output now. */ 831 if (dwarf2_loc_directive_seen) 832 dwarf2_emit_insn (0); 833 834 if (is_lloc && !is_new_logical) 835 { 836 filenum = logicals[logical - 1].loc.filenum; 837 line = logicals[logical - 1].loc.line; 838 } 839 else 840 { 841 filenum = get_absolute_expression (); 842 SKIP_WHITESPACE (); 843 line = get_absolute_expression (); 844 845 if (filenum < 1) 846 { 847 as_bad (_("file number less than one")); 848 return; 849 } 850 if (filenum >= (int) files_in_use || files[filenum].filename == 0) 851 { 852 as_bad (_("unassigned file number %ld"), (long) filenum); 853 return; 854 } 855 } 856 857 current.filenum = filenum; 858 current.line = line; 859 current.discriminator = 0; 860 current.logical = logical; 861 862 #ifndef NO_LISTING 863 if (listing) 864 { 865 if (files[filenum].dir) 866 { 867 size_t dir_len = strlen (dirs[files[filenum].dir]); 868 size_t file_len = strlen (files[filenum].filename); 869 char *cp = XNEWVEC (char, dir_len + 1 + file_len + 1); 870 871 memcpy (cp, dirs[files[filenum].dir], dir_len); 872 INSERT_DIR_SEPARATOR (cp, dir_len); 873 memcpy (cp + dir_len + 1, files[filenum].filename, file_len); 874 cp[dir_len + file_len + 1] = '\0'; 875 listing_source_file (cp); 876 free (cp); 877 } 878 else 879 listing_source_file (files[filenum].filename); 880 listing_source_line (line); 881 } 882 #endif 883 884 SKIP_WHITESPACE (); 885 if (ISDIGIT (*input_line_pointer)) 886 { 887 current.column = get_absolute_expression (); 888 SKIP_WHITESPACE (); 889 } 890 891 while (ISALPHA (*input_line_pointer)) 892 { 893 char *p, c; 894 offsetT value; 895 896 c = get_symbol_name (& p); 897 898 if (strcmp (p, "basic_block") == 0) 899 { 900 current.flags |= DWARF2_FLAG_BASIC_BLOCK; 901 *input_line_pointer = c; 902 } 903 else if (!is_actual && strcmp (p, "prologue_end") == 0) 904 { 905 current.flags |= DWARF2_FLAG_PROLOGUE_END; 906 *input_line_pointer = c; 907 } 908 else if (!is_actual && strcmp (p, "epilogue_begin") == 0) 909 { 910 current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN; 911 *input_line_pointer = c; 912 } 913 else if (!is_actual && strcmp (p, "is_stmt") == 0) 914 { 915 (void) restore_line_pointer (c); 916 value = get_absolute_expression (); 917 if (value == 0) 918 current.flags &= ~DWARF2_FLAG_IS_STMT; 919 else if (value == 1) 920 current.flags |= DWARF2_FLAG_IS_STMT; 921 else 922 { 923 as_bad (_("is_stmt value not 0 or 1")); 924 return; 925 } 926 } 927 else if (strcmp (p, "isa") == 0) 928 { 929 (void) restore_line_pointer (c); 930 value = get_absolute_expression (); 931 if (value >= 0) 932 current.isa = value; 933 else 934 { 935 as_bad (_("isa number less than zero")); 936 return; 937 } 938 } 939 else if (!is_actual && strcmp (p, "discriminator") == 0) 940 { 941 (void) restore_line_pointer (c); 942 value = get_absolute_expression (); 943 if (value >= 0) 944 current.discriminator = value; 945 else 946 { 947 as_bad (_("discriminator less than zero")); 948 return; 949 } 950 } 951 else if (!is_actual && strcmp (p, "context") == 0) 952 { 953 *input_line_pointer = c; 954 value = get_absolute_expression (); 955 if (value >= 0) 956 context = value; 957 else 958 { 959 as_bad (_("context less than zero")); 960 return; 961 } 962 } 963 else if (!is_actual && strcmp (p, "subprog") == 0) 964 { 965 *input_line_pointer = c; 966 value = get_absolute_expression (); 967 if (value >= 0) 968 subprog = value; 969 else 970 { 971 as_bad (_("subprog number less than zero")); 972 return; 973 } 974 } 975 else 976 { 977 as_bad (_("unknown .loc sub-directive `%s'"), p); 978 (void) restore_line_pointer (c); 979 return; 980 } 981 982 SKIP_WHITESPACE_AFTER_NAME (); 983 } 984 985 demand_empty_rest_of_line (); 986 dwarf2_loc_directive_seen = TRUE; 987 debug_type = DEBUG_NONE; 988 989 if (is_new_logical) 990 make_logical (logical, context, subprog); 991 } 992 993 void 994 dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED) 995 { 996 offsetT value = get_absolute_expression (); 997 998 if (value != 0 && value != 1) 999 { 1000 as_bad (_("expected 0 or 1")); 1001 ignore_rest_of_line (); 1002 } 1003 else 1004 { 1005 dwarf2_loc_mark_labels = value != 0; 1006 demand_empty_rest_of_line (); 1007 } 1008 } 1009 1010 static struct frag * 1012 first_frag_for_seg (segT seg) 1013 { 1014 return seg_info (seg)->frchainP->frch_root; 1015 } 1016 1017 static struct frag * 1018 last_frag_for_seg (segT seg) 1019 { 1020 frchainS *f = seg_info (seg)->frchainP; 1021 1022 while (f->frch_next != NULL) 1023 f = f->frch_next; 1024 1025 return f->frch_last; 1026 } 1027 1028 /* Emit a single byte into the current segment. */ 1030 1031 static inline void 1032 out_byte (int byte) 1033 { 1034 FRAG_APPEND_1_CHAR (byte); 1035 } 1036 1037 /* Emit a statement program opcode into the current segment. */ 1038 1039 static inline void 1040 out_opcode (int opc) 1041 { 1042 out_byte (opc); 1043 } 1044 1045 /* Emit a two-byte word into the current segment. */ 1046 1047 static inline void 1048 out_two (int data) 1049 { 1050 md_number_to_chars (frag_more (2), data, 2); 1051 } 1052 1053 /* Emit a four byte word into the current segment. */ 1054 1055 static inline void 1056 out_four (int data) 1057 { 1058 md_number_to_chars (frag_more (4), data, 4); 1059 } 1060 1061 /* Emit an unsigned "little-endian base 128" number. */ 1062 1063 static void 1064 out_uleb128 (addressT value) 1065 { 1066 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0); 1067 } 1068 1069 /* Emit a signed "little-endian base 128" number. */ 1070 1071 static void 1072 out_leb128 (addressT value) 1073 { 1074 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1); 1075 } 1076 1077 /* Emit a tuple for .debug_abbrev. */ 1078 1079 static inline void 1080 out_abbrev (int name, int form) 1081 { 1082 out_uleb128 (name); 1083 out_uleb128 (form); 1084 } 1085 1086 /* Get the size of a fragment. */ 1087 1088 static offsetT 1089 get_frag_fix (fragS *frag, segT seg) 1090 { 1091 frchainS *fr; 1092 1093 if (frag->fr_next) 1094 return frag->fr_fix; 1095 1096 /* If a fragment is the last in the chain, special measures must be 1097 taken to find its size before relaxation, since it may be pending 1098 on some subsegment chain. */ 1099 for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next) 1100 if (fr->frch_last == frag) 1101 return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal; 1102 1103 abort (); 1104 } 1105 1106 /* Set an absolute address (may result in a relocation entry). */ 1107 1108 static void 1109 out_set_addr (symbolS *sym) 1110 { 1111 expressionS exp; 1112 1113 out_opcode (DW_LNS_extended_op); 1114 out_uleb128 (sizeof_address + 1); 1115 1116 out_opcode (DW_LNE_set_address); 1117 exp.X_op = O_symbol; 1118 exp.X_add_symbol = sym; 1119 exp.X_add_number = 0; 1120 emit_expr (&exp, sizeof_address); 1121 } 1122 1123 /* Set the address from a logicals table entry. */ 1124 1125 static void 1126 out_set_addr_from_logical (int logical_delta) 1127 { 1128 out_opcode (DW_LNS_set_address_from_logical); 1129 out_leb128 (logical_delta); 1130 } 1131 1132 static void scale_addr_delta (addressT *); 1133 1134 static void 1135 scale_addr_delta (addressT *addr_delta) 1136 { 1137 static int printed_this = 0; 1138 if (DWARF2_LINE_MIN_INSN_LENGTH > 1) 1139 { 1140 if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0 && !printed_this) 1141 { 1142 as_bad("unaligned opcodes detected in executable segment"); 1143 printed_this = 1; 1144 } 1145 *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH; 1146 } 1147 } 1148 1149 /* Encode a pair of line and address skips as efficiently as possible. 1150 Note that the line skip is signed, whereas the address skip is unsigned. 1151 1152 The following two routines *must* be kept in sync. This is 1153 enforced by making emit_inc_line_addr abort if we do not emit 1154 exactly the expected number of bytes. */ 1155 1156 static int 1157 size_inc_line_addr (int line_delta, addressT addr_delta) 1158 { 1159 unsigned int tmp, opcode; 1160 int len = 0; 1161 1162 /* Scale the address delta by the minimum instruction length. */ 1163 scale_addr_delta (&addr_delta); 1164 1165 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. 1166 We cannot use special opcodes here, since we want the end_sequence 1167 to emit the matrix entry. */ 1168 if (line_delta == INT_MAX) 1169 { 1170 if (addr_delta == (unsigned int) MAX_SPECIAL_ADDR_DELTA) 1171 len = 1; 1172 else 1173 len = 1 + sizeof_leb128 (addr_delta, 0); 1174 return len + 3; 1175 } 1176 1177 /* Bias the line delta by the base. */ 1178 tmp = line_delta - line_base; 1179 1180 /* If the line increment is out of range of a special opcode, we 1181 must encode it with DW_LNS_advance_line. */ 1182 if (tmp >= line_range) 1183 { 1184 len = 1 + sizeof_leb128 (line_delta, 1); 1185 line_delta = 0; 1186 tmp = 0 - line_base; 1187 } 1188 1189 /* Bias the opcode by the special opcode base. */ 1190 tmp += opcode_base; 1191 1192 /* Avoid overflow when addr_delta is large. */ 1193 if (addr_delta < (unsigned int) (256 + MAX_SPECIAL_ADDR_DELTA)) 1194 { 1195 /* Try using a special opcode. */ 1196 opcode = tmp + addr_delta * line_range; 1197 if (opcode <= 255) 1198 return len + 1; 1199 1200 /* Try using DW_LNS_const_add_pc followed by special op. */ 1201 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * line_range; 1202 if (opcode <= 255) 1203 return len + 2; 1204 } 1205 1206 /* Otherwise use DW_LNS_advance_pc. */ 1207 len += 1 + sizeof_leb128 (addr_delta, 0); 1208 1209 /* DW_LNS_copy or special opcode. */ 1210 len += 1; 1211 1212 return len; 1213 } 1214 1215 static void 1216 emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len) 1217 { 1218 unsigned int tmp, opcode; 1219 int need_copy = 0; 1220 char *end = p + len; 1221 1222 /* Line number sequences cannot go backward in addresses. This means 1223 we've incorrectly ordered the statements in the sequence. */ 1224 gas_assert ((offsetT) addr_delta >= 0); 1225 1226 /* Scale the address delta by the minimum instruction length. */ 1227 scale_addr_delta (&addr_delta); 1228 1229 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. 1230 We cannot use special opcodes here, since we want the end_sequence 1231 to emit the matrix entry. */ 1232 if (line_delta == INT_MAX) 1233 { 1234 if (addr_delta == (unsigned int) MAX_SPECIAL_ADDR_DELTA) 1235 *p++ = DW_LNS_const_add_pc; 1236 else 1237 { 1238 *p++ = DW_LNS_advance_pc; 1239 p += output_leb128 (p, addr_delta, 0); 1240 } 1241 1242 *p++ = DW_LNS_extended_op; 1243 *p++ = 1; 1244 *p++ = DW_LNE_end_sequence; 1245 goto done; 1246 } 1247 1248 /* Bias the line delta by the base. */ 1249 tmp = line_delta - line_base; 1250 1251 /* If the line increment is out of range of a special opcode, we 1252 must encode it with DW_LNS_advance_line. */ 1253 if (tmp >= line_range) 1254 { 1255 *p++ = DW_LNS_advance_line; 1256 p += output_leb128 (p, line_delta, 1); 1257 1258 line_delta = 0; 1259 tmp = 0 - line_base; 1260 need_copy = 1; 1261 } 1262 1263 /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0" 1264 special opcode. */ 1265 if (line_delta == 0 && addr_delta == 0) 1266 { 1267 *p++ = DW_LNS_copy; 1268 goto done; 1269 } 1270 1271 /* Bias the opcode by the special opcode base. */ 1272 tmp += opcode_base; 1273 1274 /* Avoid overflow when addr_delta is large. */ 1275 if (addr_delta < (unsigned int) (256 + MAX_SPECIAL_ADDR_DELTA)) 1276 { 1277 /* Try using a special opcode. */ 1278 opcode = tmp + addr_delta * line_range; 1279 if (opcode <= 255) 1280 { 1281 *p++ = opcode; 1282 goto done; 1283 } 1284 1285 /* Try using DW_LNS_const_add_pc followed by special op. */ 1286 opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * line_range; 1287 if (opcode <= 255) 1288 { 1289 *p++ = DW_LNS_const_add_pc; 1290 *p++ = opcode; 1291 goto done; 1292 } 1293 } 1294 1295 /* Otherwise use DW_LNS_advance_pc. */ 1296 *p++ = DW_LNS_advance_pc; 1297 p += output_leb128 (p, addr_delta, 0); 1298 1299 if (need_copy) 1300 *p++ = DW_LNS_copy; 1301 else 1302 *p++ = tmp; 1303 1304 done: 1305 gas_assert (p == end); 1306 } 1307 1308 /* Handy routine to combine calls to the above two routines. */ 1309 1310 static void 1311 out_inc_line_addr (int line_delta, addressT addr_delta) 1312 { 1313 int len = size_inc_line_addr (line_delta, addr_delta); 1314 emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len); 1315 } 1316 1317 /* Write out an alternative form of line and address skips using 1318 DW_LNS_fixed_advance_pc opcodes. This uses more space than the default 1319 line and address information, but it is required if linker relaxation 1320 could change the code offsets. The following two routines *must* be 1321 kept in sync. */ 1322 #define ADDR_DELTA_LIMIT 50000 1323 1324 static int 1325 size_fixed_inc_line_addr (int line_delta, addressT addr_delta) 1326 { 1327 int len = 0; 1328 1329 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ 1330 if (line_delta != INT_MAX) 1331 len = 1 + sizeof_leb128 (line_delta, 1); 1332 1333 if (addr_delta > ADDR_DELTA_LIMIT) 1334 { 1335 /* DW_LNS_extended_op */ 1336 len += 1 + sizeof_leb128 (sizeof_address + 1, 0); 1337 /* DW_LNE_set_address */ 1338 len += 1 + sizeof_address; 1339 } 1340 else 1341 /* DW_LNS_fixed_advance_pc */ 1342 len += 3; 1343 1344 if (line_delta == INT_MAX) 1345 /* DW_LNS_extended_op + DW_LNE_end_sequence */ 1346 len += 3; 1347 else 1348 /* DW_LNS_copy */ 1349 len += 1; 1350 1351 return len; 1352 } 1353 1354 static void 1355 emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag, 1356 char *p, int len) 1357 { 1358 expressionS *pexp; 1359 char *end = p + len; 1360 1361 /* Line number sequences cannot go backward in addresses. This means 1362 we've incorrectly ordered the statements in the sequence. */ 1363 gas_assert ((offsetT) addr_delta >= 0); 1364 1365 /* Verify that we have kept in sync with size_fixed_inc_line_addr. */ 1366 gas_assert (len == size_fixed_inc_line_addr (line_delta, addr_delta)); 1367 1368 /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ 1369 if (line_delta != INT_MAX) 1370 { 1371 *p++ = DW_LNS_advance_line; 1372 p += output_leb128 (p, line_delta, 1); 1373 } 1374 1375 pexp = symbol_get_value_expression (frag->fr_symbol); 1376 1377 /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can 1378 advance the address by at most 64K. Linker relaxation (without 1379 which this function would not be used) could change the operand by 1380 an unknown amount. If the address increment is getting close to 1381 the limit, just reset the address. */ 1382 if (addr_delta > ADDR_DELTA_LIMIT) 1383 { 1384 symbolS *to_sym; 1385 expressionS exp; 1386 1387 gas_assert (pexp->X_op == O_subtract); 1388 to_sym = pexp->X_add_symbol; 1389 1390 *p++ = DW_LNS_extended_op; 1391 p += output_leb128 (p, sizeof_address + 1, 0); 1392 *p++ = DW_LNE_set_address; 1393 exp.X_op = O_symbol; 1394 exp.X_add_symbol = to_sym; 1395 exp.X_add_number = 0; 1396 emit_expr_fix (&exp, sizeof_address, frag, p, TC_PARSE_CONS_RETURN_NONE); 1397 p += sizeof_address; 1398 } 1399 else 1400 { 1401 *p++ = DW_LNS_fixed_advance_pc; 1402 emit_expr_fix (pexp, 2, frag, p, TC_PARSE_CONS_RETURN_NONE); 1403 p += 2; 1404 } 1405 1406 if (line_delta == INT_MAX) 1407 { 1408 *p++ = DW_LNS_extended_op; 1409 *p++ = 1; 1410 *p++ = DW_LNE_end_sequence; 1411 } 1412 else 1413 *p++ = DW_LNS_copy; 1414 1415 gas_assert (p == end); 1416 } 1417 1418 /* Generate a variant frag that we can use to relax address/line 1419 increments between fragments of the target segment. */ 1420 1421 static void 1422 relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym) 1423 { 1424 expressionS exp; 1425 int max_chars; 1426 1427 exp.X_op = O_subtract; 1428 exp.X_add_symbol = to_sym; 1429 exp.X_op_symbol = from_sym; 1430 exp.X_add_number = 0; 1431 1432 /* The maximum size of the frag is the line delta with a maximum 1433 sized address delta. */ 1434 if (DWARF2_USE_FIXED_ADVANCE_PC) 1435 max_chars = size_fixed_inc_line_addr (line_delta, 1436 -DWARF2_LINE_MIN_INSN_LENGTH); 1437 else 1438 max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH); 1439 1440 frag_var (rs_dwarf2dbg, max_chars, max_chars, 1, 1441 make_expr_symbol (&exp), line_delta, NULL); 1442 } 1443 1444 /* The function estimates the size of a rs_dwarf2dbg variant frag 1445 based on the current values of the symbols. It is called before 1446 the relaxation loop. We set fr_subtype to the expected length. */ 1447 1448 int 1449 dwarf2dbg_estimate_size_before_relax (fragS *frag) 1450 { 1451 offsetT addr_delta; 1452 int size; 1453 1454 addr_delta = resolve_symbol_value (frag->fr_symbol); 1455 if (DWARF2_USE_FIXED_ADVANCE_PC) 1456 size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta); 1457 else 1458 size = size_inc_line_addr (frag->fr_offset, addr_delta); 1459 1460 frag->fr_subtype = size; 1461 1462 return size; 1463 } 1464 1465 /* This function relaxes a rs_dwarf2dbg variant frag based on the 1466 current values of the symbols. fr_subtype is the current length 1467 of the frag. This returns the change in frag length. */ 1468 1469 int 1470 dwarf2dbg_relax_frag (fragS *frag) 1471 { 1472 int old_size, new_size; 1473 1474 old_size = frag->fr_subtype; 1475 new_size = dwarf2dbg_estimate_size_before_relax (frag); 1476 1477 return new_size - old_size; 1478 } 1479 1480 /* This function converts a rs_dwarf2dbg variant frag into a normal 1481 fill frag. This is called after all relaxation has been done. 1482 fr_subtype will be the desired length of the frag. */ 1483 1484 void 1485 dwarf2dbg_convert_frag (fragS *frag) 1486 { 1487 offsetT addr_diff; 1488 1489 if (DWARF2_USE_FIXED_ADVANCE_PC) 1490 { 1491 /* If linker relaxation is enabled then the distance bewteen the two 1492 symbols in the frag->fr_symbol expression might change. Hence we 1493 cannot rely upon the value computed by resolve_symbol_value. 1494 Instead we leave the expression unfinalized and allow 1495 emit_fixed_inc_line_addr to create a fixup (which later becomes a 1496 relocation) that will allow the linker to correctly compute the 1497 actual address difference. We have to use a fixed line advance for 1498 this as we cannot (easily) relocate leb128 encoded values. */ 1499 int saved_finalize_syms = finalize_syms; 1500 1501 finalize_syms = 0; 1502 addr_diff = resolve_symbol_value (frag->fr_symbol); 1503 finalize_syms = saved_finalize_syms; 1504 } 1505 else 1506 addr_diff = resolve_symbol_value (frag->fr_symbol); 1507 1508 /* fr_var carries the max_chars that we created the fragment with. 1509 fr_subtype carries the current expected length. We must, of 1510 course, have allocated enough memory earlier. */ 1511 gas_assert (frag->fr_var >= (int) frag->fr_subtype); 1512 1513 if (DWARF2_USE_FIXED_ADVANCE_PC) 1514 emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag, 1515 frag->fr_literal + frag->fr_fix, 1516 frag->fr_subtype); 1517 else 1518 emit_inc_line_addr (frag->fr_offset, addr_diff, 1519 frag->fr_literal + frag->fr_fix, frag->fr_subtype); 1520 1521 frag->fr_fix += frag->fr_subtype; 1522 frag->fr_type = rs_fill; 1523 frag->fr_var = 0; 1524 frag->fr_offset = 0; 1525 } 1526 1527 /* Generate .debug_line content for the logicals table rows. */ 1528 1529 static void 1530 emit_logicals (void) 1531 { 1532 unsigned logical; 1533 unsigned filenum = 1; 1534 unsigned line = 1; 1535 unsigned column = 0; 1536 unsigned discriminator; 1537 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 1538 unsigned context = 0; 1539 unsigned subprog = 0; 1540 segT last_seg = NULL; 1541 fragS *last_frag = NULL, *frag; 1542 addressT last_frag_ofs = 0, frag_ofs; 1543 symbolS *last_lab = NULL, *lab; 1544 1545 for (logical = 1; logical <= logicals_in_use; ++logical) 1546 { 1547 int line_delta; 1548 int context_delta; 1549 struct logicals_entry *e = &logicals[logical - 1]; 1550 1551 discriminator = 0; 1552 1553 if (context != e->context || subprog != e->subprog) 1554 { 1555 unsigned int caller = context; 1556 unsigned int npop = 0; 1557 1558 // See if a sequence of DW_LNS_pop_context ops will get 1559 // to the state we want. 1560 while (caller > 0 && caller <= logicals_in_use) 1561 { 1562 ++npop; 1563 if (logicals[caller - 1].subprog == e->subprog) 1564 break; 1565 caller = logicals[caller - 1].context; 1566 } 1567 if (caller > 0 && caller <= logicals_in_use && npop < 10) 1568 { 1569 while (npop-- > 0) 1570 out_opcode (DW_LNS_pop_context); 1571 filenum = logicals[caller - 1].loc.filenum; 1572 line = logicals[caller - 1].loc.line; 1573 column = logicals[caller - 1].loc.column; 1574 discriminator = logicals[caller - 1].loc.discriminator; 1575 flags = logicals[caller - 1].loc.flags; 1576 context = logicals[caller - 1].context; 1577 subprog = logicals[caller - 1].subprog; 1578 } 1579 if (context != e->context && e->context == 0) 1580 { 1581 context = 0; 1582 subprog = e->subprog; 1583 out_opcode (DW_LNS_set_subprogram); 1584 out_uleb128 (subprog); 1585 } 1586 else if (context != e->context || subprog != e->subprog) 1587 { 1588 context_delta = e->context - (logical - 1); 1589 context = e->context; 1590 subprog = e->subprog; 1591 out_opcode (DW_LNS_inlined_call); 1592 out_leb128 (context_delta); 1593 out_uleb128 (subprog); 1594 } 1595 } 1596 1597 if (filenum != e->loc.filenum) 1598 { 1599 filenum = e->loc.filenum; 1600 out_opcode (DW_LNS_set_file); 1601 out_uleb128 (filenum); 1602 } 1603 1604 if (column != e->loc.column) 1605 { 1606 column = e->loc.column; 1607 out_opcode (DW_LNS_set_column); 1608 out_uleb128 (column); 1609 } 1610 1611 if (e->loc.discriminator != discriminator) 1612 { 1613 out_opcode (DW_LNS_extended_op); 1614 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0)); 1615 out_opcode (DW_LNE_set_discriminator); 1616 out_uleb128 (e->loc.discriminator); 1617 } 1618 1619 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT) 1620 { 1621 flags = e->loc.flags; 1622 out_opcode (DW_LNS_negate_stmt); 1623 } 1624 1625 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END) 1626 out_opcode (DW_LNS_set_prologue_end); 1627 1628 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN) 1629 out_opcode (DW_LNS_set_epilogue_begin); 1630 1631 line_delta = e->loc.line - line; 1632 if (e->label == NULL) 1633 { 1634 out_inc_line_addr (line_delta, 0); 1635 } 1636 else 1637 { 1638 lab = e->label; 1639 frag = symbol_get_frag (lab); 1640 frag_ofs = S_GET_VALUE (lab); 1641 1642 if (last_frag == NULL || e->seg != last_seg) 1643 { 1644 out_set_addr (lab); 1645 out_inc_line_addr (line_delta, 0); 1646 } 1647 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) 1648 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs); 1649 else 1650 relax_inc_line_addr (line_delta, lab, last_lab); 1651 1652 line = e->loc.line; 1653 last_seg = e->seg; 1654 last_lab = lab; 1655 last_frag = frag; 1656 last_frag_ofs = frag_ofs; 1657 } 1658 } 1659 } 1660 1661 /* Generate .debug_line content for the chain of line number entries 1662 beginning at E, for segment SEG. */ 1663 1664 static void 1665 process_entries (segT seg, struct line_entry *e) 1666 { 1667 unsigned filenum = 1; 1668 unsigned line = 1; 1669 unsigned column = 0; 1670 unsigned isa = 0; 1671 unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 1672 fragS *last_frag = NULL, *frag; 1673 addressT last_frag_ofs = 0, frag_ofs; 1674 symbolS *last_lab = NULL, *lab; 1675 struct line_entry *next; 1676 1677 if (flag_dwarf_sections) 1678 { 1679 char * name; 1680 const char * sec_name; 1681 1682 /* Switch to the relevent sub-section before we start to emit 1683 the line number table. 1684 1685 FIXME: These sub-sections do not have a normal Line Number 1686 Program Header, thus strictly speaking they are not valid 1687 DWARF sections. Unfortunately the DWARF standard assumes 1688 a one-to-one relationship between compilation units and 1689 line number tables. Thus we have to have a .debug_line 1690 section, as well as our sub-sections, and we have to ensure 1691 that all of the sub-sections are merged into a proper 1692 .debug_line section before a debugger sees them. */ 1693 1694 sec_name = bfd_get_section_name (stdoutput, seg); 1695 if (strcmp (sec_name, ".text") != 0) 1696 { 1697 name = concat (".debug_line", sec_name, (char *) NULL); 1698 subseg_set (subseg_get (name, FALSE), 0); 1699 } 1700 else 1701 /* Don't create a .debug_line.text section - 1702 that is redundant. Instead just switch back to the 1703 normal .debug_line section. */ 1704 subseg_set (subseg_get (".debug_line", FALSE), 0); 1705 } 1706 1707 do 1708 { 1709 int line_delta; 1710 1711 if (logicals_in_use == 0) 1712 { 1713 if (filenum != e->loc.filenum) 1714 { 1715 filenum = e->loc.filenum; 1716 out_opcode (DW_LNS_set_file); 1717 out_uleb128 (filenum); 1718 } 1719 1720 if (column != e->loc.column) 1721 { 1722 column = e->loc.column; 1723 out_opcode (DW_LNS_set_column); 1724 out_uleb128 (column); 1725 } 1726 1727 if (e->loc.discriminator != 0) 1728 { 1729 out_opcode (DW_LNS_extended_op); 1730 out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0)); 1731 out_opcode (DW_LNE_set_discriminator); 1732 out_uleb128 (e->loc.discriminator); 1733 } 1734 } 1735 1736 if (isa != e->loc.isa) 1737 { 1738 isa = e->loc.isa; 1739 out_opcode (DW_LNS_set_isa); 1740 out_uleb128 (isa); 1741 } 1742 1743 if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK) 1744 out_opcode (DW_LNS_set_basic_block); 1745 1746 if (logicals_in_use == 0) 1747 { 1748 if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT) 1749 { 1750 flags = e->loc.flags; 1751 out_opcode (DW_LNS_negate_stmt); 1752 } 1753 1754 if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END) 1755 out_opcode (DW_LNS_set_prologue_end); 1756 1757 if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN) 1758 out_opcode (DW_LNS_set_epilogue_begin); 1759 } 1760 1761 /* Don't try to optimize away redundant entries; gdb wants two 1762 entries for a function where the code starts on the same line as 1763 the {, and there's no way to identify that case here. Trust gcc 1764 to optimize appropriately. */ 1765 if (logicals_in_use == 0) 1766 line_delta = e->loc.line - line; 1767 else 1768 line_delta = e->loc.logical - line; 1769 lab = e->label; 1770 frag = symbol_get_frag (lab); 1771 frag_ofs = S_GET_VALUE (lab); 1772 1773 if (last_frag == NULL) 1774 { 1775 if (logicals_in_use > 0 && logicals[e->loc.logical - 1].label == lab) 1776 { 1777 out_set_addr_from_logical (line_delta); 1778 out_opcode (DW_LNS_copy); 1779 } 1780 else 1781 { 1782 out_set_addr (lab); 1783 out_inc_line_addr (line_delta, 0); 1784 } 1785 } 1786 else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) 1787 out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs); 1788 else 1789 relax_inc_line_addr (line_delta, lab, last_lab); 1790 1791 if (logicals_in_use == 0) 1792 line = e->loc.line; 1793 else 1794 line = e->loc.logical; 1795 last_lab = lab; 1796 last_frag = frag; 1797 last_frag_ofs = frag_ofs; 1798 1799 next = e->next; 1800 free (e); 1801 e = next; 1802 } 1803 while (e); 1804 1805 /* Emit a DW_LNE_end_sequence for the end of the section. */ 1806 frag = last_frag_for_seg (seg); 1807 frag_ofs = get_frag_fix (frag, seg); 1808 if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) 1809 out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs); 1810 else 1811 { 1812 lab = symbol_temp_new (seg, frag_ofs, frag); 1813 relax_inc_line_addr (INT_MAX, lab, last_lab); 1814 } 1815 } 1816 1817 /* Emit the directory and file tables for .debug_line. */ 1818 1819 static void 1820 out_file_list (void) 1821 { 1822 size_t size; 1823 const char *dir; 1824 char *cp; 1825 unsigned int i; 1826 1827 /* Emit directory list. */ 1828 for (i = 1; i < dirs_in_use; ++i) 1829 { 1830 dir = remap_debug_filename (dirs[i]); 1831 size = strlen (dir) + 1; 1832 cp = frag_more (size); 1833 memcpy (cp, dir, size); 1834 } 1835 /* Terminate it. */ 1836 out_byte ('\0'); 1837 1838 for (i = 1; i < files_in_use; ++i) 1839 { 1840 const char *fullfilename; 1841 1842 if (files[i].filename == NULL) 1843 { 1844 as_bad (_("unassigned file number %ld"), (long) i); 1845 /* Prevent a crash later, particularly for file 1. */ 1846 files[i].filename = ""; 1847 continue; 1848 } 1849 1850 fullfilename = DWARF2_FILE_NAME (files[i].filename, 1851 files[i].dir ? dirs [files [i].dir] : ""); 1852 size = strlen (fullfilename) + 1; 1853 cp = frag_more (size); 1854 memcpy (cp, fullfilename, size); 1855 1856 out_uleb128 (files[i].dir); /* directory number */ 1857 /* Output the last modification timestamp. */ 1858 out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename, 1859 files[i].dir ? dirs [files [i].dir] : "")); 1860 /* Output the filesize. */ 1861 out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename, 1862 files[i].dir ? dirs [files [i].dir] : "")); 1863 } 1864 1865 /* Terminate filename list. */ 1866 out_byte (0); 1867 } 1868 1869 /* Add a string to the string table. */ 1870 1871 static offsetT 1872 add_to_string_table (struct string_table *strtab, const char *str) 1873 { 1874 const char *key; 1875 offsetT val; 1876 1877 if (strtab->strings_allocated == 0) 1878 { 1879 strtab->strings_allocated = 4; 1880 strtab->strings = (const char **) 1881 xcalloc (strtab->strings_allocated, sizeof(char *)); 1882 strtab->hashtab = hash_new (); 1883 } 1884 1885 val = (offsetT) (uintptr_t)hash_find (strtab->hashtab, str); 1886 if (val != 0) 1887 return val; 1888 1889 if (strtab->strings_in_use >= strtab->strings_allocated) 1890 { 1891 unsigned int old = strtab->strings_allocated; 1892 1893 strtab->strings_allocated *= 2; 1894 strtab->strings = (const char **) 1895 xrealloc (strtab->strings, 1896 strtab->strings_allocated * sizeof (char *)); 1897 memset (strtab->strings + old, 0, 1898 (strtab->strings_allocated - old) * sizeof (char *)); 1899 } 1900 1901 key = xstrdup (str); 1902 val = strtab->next_offset; 1903 hash_insert (strtab->hashtab, key, (void *) (uintptr_t)val); 1904 strtab->strings[strtab->strings_in_use++] = key; 1905 strtab->next_offset += strlen(key) + 1; 1906 return val; 1907 } 1908 1909 /* Output the string table STRTAB to the section STR_SEG. 1910 In a debug string table, the first byte is always '\0', 1911 and valid indexes begin at 1. */ 1912 1913 static void 1914 out_string_table (segT str_seg, struct string_table *strtab) 1915 { 1916 unsigned int i; 1917 size_t size; 1918 char *cp; 1919 1920 subseg_set (str_seg, 0); 1921 out_byte (0); 1922 for (i = 0; i < strtab->strings_in_use; i++) 1923 { 1924 size = strlen (strtab->strings[i]) + 1; 1925 cp = frag_more (size); 1926 memcpy (cp, strtab->strings[i], size); 1927 } 1928 } 1929 1930 static void 1931 out_dwarf5_file_list (segT str_seg, int sizeof_offset) 1932 { 1933 const char *dir; 1934 offsetT strp; 1935 unsigned int i; 1936 expressionS exp; 1937 unsigned int dir_count = dirs_in_use > 0 ? dirs_in_use - 1 : 0; 1938 unsigned int file_count = files_in_use > 0 ? files_in_use - 1 : 0; 1939 1940 exp.X_op = O_symbol; 1941 exp.X_add_symbol = section_symbol (str_seg); 1942 1943 out_byte (1); /* directory_entry_format_count */ 1944 out_uleb128 (DW_LNCT_path); /* directory_entry_format[0].content_type */ 1945 out_uleb128 (DW_FORM_line_strp); /* directory_entry_format[0].form */ 1946 out_uleb128 (dir_count); /* directories_count */ 1947 1948 /* Emit directories list. */ 1949 for (i = 1; i < dirs_in_use; ++i) 1950 { 1951 dir = remap_debug_filename (dirs[i]); 1952 strp = add_to_string_table (&debug_line_str_table, dir); 1953 exp.X_add_number = strp; 1954 emit_expr (&exp, sizeof_offset); 1955 } 1956 1957 out_byte (2); /* file_name_entry_format_count */ 1958 out_uleb128 (DW_LNCT_path); /* file_name_entry_format[0].type */ 1959 out_uleb128 (DW_FORM_line_strp); /* file_name_entry_format[0].form */ 1960 out_uleb128 (DW_LNCT_directory_index); /* file_name_entry_format[0].type */ 1961 out_uleb128 (DW_FORM_udata); /* file_name_entry_format[0].form */ 1962 out_uleb128 (file_count); /* file_names_count */ 1963 1964 /* Emit file_names list. */ 1965 for (i = 1; i < files_in_use; ++i) 1966 { 1967 const char *fullfilename; 1968 1969 if (files[i].filename == NULL) 1970 { 1971 as_bad (_("unassigned file number %ld"), (long) i); 1972 /* Prevent a crash later, particularly for file 1. */ 1973 files[i].filename = ""; 1974 } 1975 1976 fullfilename = DWARF2_FILE_NAME (files[i].filename, 1977 files[i].dir ? dirs [files [i].dir] : ""); 1978 strp = add_to_string_table (&debug_line_str_table, fullfilename); 1979 exp.X_add_number = strp; 1980 emit_expr (&exp, sizeof_offset); 1981 out_uleb128 (files[i].dir); /* directory number */ 1982 } 1983 } 1984 1985 static void 1986 out_subprog_list (segT str_seg, int sizeof_offset) 1987 { 1988 const char *name; 1989 offsetT strp; 1990 unsigned int i; 1991 expressionS exp; 1992 1993 exp.X_op = O_symbol; 1994 exp.X_add_symbol = section_symbol (str_seg); 1995 1996 out_byte (3); /* subprogram_entry_format_count */ 1997 out_uleb128 (DW_LNCT_subprogram_name); /* subprogram_entry_format[0].type */ 1998 out_uleb128 (DW_FORM_line_strp); /* subprogram_entry_format[0].form */ 1999 out_uleb128 (DW_LNCT_decl_file); /* subprogram_entry_format[1].type */ 2000 out_uleb128 (DW_FORM_udata); /* subprogram_entry_format[1].form */ 2001 out_uleb128 (DW_LNCT_decl_line); /* subprogram_entry_format[2].type */ 2002 out_uleb128 (DW_FORM_udata); /* subprogram_entry_format[2].form */ 2003 out_uleb128 (subprogs_in_use); /* subprograms_count */ 2004 2005 /* Emit subprograms list. */ 2006 for (i = 0; i < subprogs_in_use; ++i) 2007 { 2008 name = subprogs[i].subpname; 2009 if (name == NULL) 2010 { 2011 as_bad (_("unassigned subprogram number %ld"), (long) i); 2012 strp = 0; 2013 } 2014 else 2015 strp = add_to_string_table (&debug_line_str_table, name); 2016 exp.X_add_number = strp; 2017 emit_expr (&exp, sizeof_offset); 2018 out_uleb128 (subprogs[i].filenum); 2019 out_uleb128 (subprogs[i].line); 2020 } 2021 } 2022 2023 /* Switch to SEC and output a header length field. Return the size of 2024 offsets used in SEC. The caller must set EXPR->X_add_symbol value 2025 to the end of the section. EXPR->X_add_number will be set to the 2026 negative size of the header. */ 2027 2028 static int 2029 out_header (asection *sec, expressionS *exp) 2030 { 2031 symbolS *start_sym; 2032 symbolS *end_sym; 2033 2034 subseg_set (sec, 0); 2035 2036 if (flag_dwarf_sections) 2037 { 2038 /* If we are going to put the start and end symbols in different 2039 sections, then we need real symbols, not just fake, local ones. */ 2040 frag_now_fix (); 2041 start_sym = symbol_make (".Ldebug_line_start"); 2042 end_sym = symbol_make (".Ldebug_line_end"); 2043 symbol_set_value_now (start_sym); 2044 } 2045 else 2046 { 2047 start_sym = symbol_temp_new_now (); 2048 end_sym = symbol_temp_make (); 2049 } 2050 2051 /* Total length of the information. */ 2052 exp->X_op = O_subtract; 2053 exp->X_add_symbol = end_sym; 2054 exp->X_op_symbol = start_sym; 2055 2056 switch (DWARF2_FORMAT (sec)) 2057 { 2058 case dwarf2_format_32bit: 2059 exp->X_add_number = -4; 2060 emit_expr (exp, 4); 2061 return 4; 2062 2063 case dwarf2_format_64bit: 2064 exp->X_add_number = -12; 2065 out_four (-1); 2066 emit_expr (exp, 8); 2067 return 8; 2068 2069 case dwarf2_format_64bit_irix: 2070 exp->X_add_number = -8; 2071 emit_expr (exp, 8); 2072 return 8; 2073 } 2074 2075 as_fatal (_("internal error: unknown dwarf2 format")); 2076 return 0; 2077 } 2078 2079 /* Emit the collected .debug_line data. */ 2080 2081 static void 2082 out_debug_line (segT line_seg, segT str_seg) 2083 { 2084 expressionS exp; 2085 symbolS *prologue_start, *prologue_end, *logicals_start, *actuals_start; 2086 symbolS *line_end; 2087 struct line_seg *s; 2088 int sizeof_offset; 2089 unsigned int version; 2090 2091 if (logicals_in_use == 0) 2092 { 2093 version = DWARF2_LINE_VERSION; 2094 opcode_base = DWARF2_LINE_OPCODE_BASE; 2095 line_base = DWARF2_LINE_BASE; 2096 line_range = DWARF2_LINE_RANGE; 2097 } 2098 else 2099 { 2100 version = DWARF2_LINE_EXPERIMENTAL_VERSION; 2101 opcode_base = DWARF5_EXPERIMENTAL_LINE_OPCODE_BASE; 2102 line_base = DWARF5_EXPERIMENTAL_LINE_BASE; 2103 line_range = DWARF5_EXPERIMENTAL_LINE_RANGE; 2104 } 2105 2106 sizeof_offset = out_header (line_seg, &exp); 2107 line_end = exp.X_add_symbol; 2108 2109 /* Version. */ 2110 out_two (version); 2111 2112 /* Length of the prologue following this length. */ 2113 prologue_start = symbol_temp_make (); 2114 prologue_end = symbol_temp_make (); 2115 exp.X_op = O_subtract; 2116 exp.X_add_symbol = prologue_end; 2117 exp.X_op_symbol = prologue_start; 2118 exp.X_add_number = 0; 2119 emit_expr (&exp, sizeof_offset); 2120 symbol_set_value_now (prologue_start); 2121 2122 /* Parameters of the state machine. */ 2123 out_byte (DWARF2_LINE_MIN_INSN_LENGTH); 2124 if (version >= 4) 2125 out_byte (DWARF2_LINE_MAX_OPS_PER_INSN); 2126 out_byte (DWARF2_LINE_DEFAULT_IS_STMT); 2127 out_byte (line_base); 2128 out_byte (line_range); 2129 out_byte (opcode_base); 2130 2131 /* Standard opcode lengths. */ 2132 out_byte (0); /* DW_LNS_copy */ 2133 out_byte (1); /* DW_LNS_advance_pc */ 2134 out_byte (1); /* DW_LNS_advance_line */ 2135 out_byte (1); /* DW_LNS_set_file */ 2136 out_byte (1); /* DW_LNS_set_column */ 2137 out_byte (0); /* DW_LNS_negate_stmt */ 2138 out_byte (0); /* DW_LNS_set_basic_block */ 2139 out_byte (0); /* DW_LNS_const_add_pc */ 2140 out_byte (1); /* DW_LNS_fixed_advance_pc */ 2141 out_byte (0); /* DW_LNS_set_prologue_end */ 2142 out_byte (0); /* DW_LNS_set_epilogue_begin */ 2143 out_byte (1); /* DW_LNS_set_isa */ 2144 if (opcode_base == DWARF5_EXPERIMENTAL_LINE_OPCODE_BASE) 2145 { 2146 out_byte (1); /* DW_LNS_set_subprogram/DW_LNS_set_address_from_logical */ 2147 out_byte (2); /* DW_LNS_inlined_call */ 2148 out_byte (0); /* DW_LNS_pop_context */ 2149 } 2150 2151 if (version == DWARF2_LINE_EXPERIMENTAL_VERSION) 2152 { 2153 /* Fake empty version 4 directory and filename lists, to fool 2154 old consumers who don't check the version number. */ 2155 out_byte (0); 2156 out_byte (0); 2157 2158 symbol_set_value_now (prologue_end); 2159 2160 /* Now wrap the remainder of the section inside a fake 2161 extended opcode, so old consumers will see just the single 2162 extended opcode, and will not try to read anything else. 2163 For simplicity, we simply output a very large number for 2164 the size of the extended op. */ 2165 out_opcode (DW_LNS_extended_op); 2166 out_byte (255); /* 3-byte LEB128 for 0x1fffff. */ 2167 out_byte (255); 2168 out_byte (127); 2169 out_byte (127); /* Fake extended opcode. */ 2170 2171 /* Logicals table offset. */ 2172 logicals_start = symbol_temp_make (); 2173 exp.X_add_symbol = logicals_start; 2174 emit_expr (&exp, sizeof_offset); 2175 2176 /* Actuals table offset. */ 2177 actuals_start = symbol_temp_make (); 2178 exp.X_add_symbol = actuals_start; 2179 emit_expr (&exp, sizeof_offset); 2180 2181 /* Directory and filename lists. */ 2182 out_dwarf5_file_list (str_seg, sizeof_offset); 2183 2184 /* Subprogram list. */ 2185 out_subprog_list (str_seg, sizeof_offset); 2186 2187 symbol_set_value_now (logicals_start); 2188 emit_logicals (); 2189 symbol_set_value_now (actuals_start); 2190 } 2191 else if (version >= 5) 2192 { 2193 out_dwarf5_file_list (str_seg, sizeof_offset); 2194 symbol_set_value_now (prologue_end); 2195 } 2196 else 2197 { 2198 out_file_list (); 2199 symbol_set_value_now (prologue_end); 2200 } 2201 2202 /* For each section, emit a statement program. */ 2203 for (s = all_segs; s; s = s->next) 2204 if (SEG_NORMAL (s->seg)) 2205 process_entries (s->seg, s->head->head); 2206 else 2207 as_warn ("dwarf line number information for %s ignored", 2208 segment_name (s->seg)); 2209 2210 if (flag_dwarf_sections) 2211 /* We have to switch to the special .debug_line_end section 2212 before emitting the end-of-debug_line symbol. The linker 2213 script arranges for this section to be placed after all the 2214 (potentially garbage collected) .debug_line.<foo> sections. 2215 This section contains the line_end symbol which is used to 2216 compute the size of the linked .debug_line section, as seen 2217 in the DWARF Line Number header. */ 2218 subseg_set (subseg_get (".debug_line_end", FALSE), 0); 2219 2220 symbol_set_value_now (line_end); 2221 } 2222 2223 static void 2224 out_debug_ranges (segT ranges_seg) 2225 { 2226 unsigned int addr_size = sizeof_address; 2227 struct line_seg *s; 2228 expressionS exp; 2229 unsigned int i; 2230 2231 subseg_set (ranges_seg, 0); 2232 2233 /* Base Address Entry. */ 2234 for (i = 0; i < addr_size; i++) 2235 out_byte (0xff); 2236 for (i = 0; i < addr_size; i++) 2237 out_byte (0); 2238 2239 /* Range List Entry. */ 2240 for (s = all_segs; s; s = s->next) 2241 { 2242 fragS *frag; 2243 symbolS *beg, *end; 2244 2245 frag = first_frag_for_seg (s->seg); 2246 beg = symbol_temp_new (s->seg, 0, frag); 2247 s->text_start = beg; 2248 2249 frag = last_frag_for_seg (s->seg); 2250 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag); 2251 s->text_end = end; 2252 2253 exp.X_op = O_symbol; 2254 exp.X_add_symbol = beg; 2255 exp.X_add_number = 0; 2256 emit_expr (&exp, addr_size); 2257 2258 exp.X_op = O_symbol; 2259 exp.X_add_symbol = end; 2260 exp.X_add_number = 0; 2261 emit_expr (&exp, addr_size); 2262 } 2263 2264 /* End of Range Entry. */ 2265 for (i = 0; i < addr_size; i++) 2266 out_byte (0); 2267 for (i = 0; i < addr_size; i++) 2268 out_byte (0); 2269 } 2270 2271 /* Emit data for .debug_aranges. */ 2272 2273 static void 2274 out_debug_aranges (segT aranges_seg, segT info_seg) 2275 { 2276 unsigned int addr_size = sizeof_address; 2277 offsetT size; 2278 struct line_seg *s; 2279 expressionS exp; 2280 symbolS *aranges_end; 2281 char *p; 2282 int sizeof_offset; 2283 2284 sizeof_offset = out_header (aranges_seg, &exp); 2285 aranges_end = exp.X_add_symbol; 2286 size = -exp.X_add_number; 2287 2288 /* Version. */ 2289 out_two (DWARF2_ARANGES_VERSION); 2290 size += 2; 2291 2292 /* Offset to .debug_info. */ 2293 TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset); 2294 size += sizeof_offset; 2295 2296 /* Size of an address (offset portion). */ 2297 out_byte (addr_size); 2298 size++; 2299 2300 /* Size of a segment descriptor. */ 2301 out_byte (0); 2302 size++; 2303 2304 /* Align the header. */ 2305 while ((size++ % (2 * addr_size)) > 0) 2306 out_byte (0); 2307 2308 for (s = all_segs; s; s = s->next) 2309 { 2310 fragS *frag; 2311 symbolS *beg, *end; 2312 2313 frag = first_frag_for_seg (s->seg); 2314 beg = symbol_temp_new (s->seg, 0, frag); 2315 s->text_start = beg; 2316 2317 frag = last_frag_for_seg (s->seg); 2318 end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag); 2319 s->text_end = end; 2320 2321 exp.X_op = O_symbol; 2322 exp.X_add_symbol = beg; 2323 exp.X_add_number = 0; 2324 emit_expr (&exp, addr_size); 2325 2326 exp.X_op = O_subtract; 2327 exp.X_add_symbol = end; 2328 exp.X_op_symbol = beg; 2329 exp.X_add_number = 0; 2330 emit_expr (&exp, addr_size); 2331 } 2332 2333 p = frag_more (2 * addr_size); 2334 md_number_to_chars (p, 0, addr_size); 2335 md_number_to_chars (p + addr_size, 0, addr_size); 2336 2337 symbol_set_value_now (aranges_end); 2338 } 2339 2340 /* Emit data for .debug_abbrev. Note that this must be kept in 2341 sync with out_debug_info below. */ 2342 2343 static void 2344 out_debug_abbrev (segT abbrev_seg, 2345 segT info_seg ATTRIBUTE_UNUSED, 2346 segT line_seg ATTRIBUTE_UNUSED) 2347 { 2348 subseg_set (abbrev_seg, 0); 2349 2350 out_uleb128 (1); 2351 out_uleb128 (DW_TAG_compile_unit); 2352 out_byte (DW_CHILDREN_no); 2353 if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit) 2354 out_abbrev (DW_AT_stmt_list, DW_FORM_data4); 2355 else 2356 out_abbrev (DW_AT_stmt_list, DW_FORM_data8); 2357 if (all_segs->next == NULL) 2358 { 2359 out_abbrev (DW_AT_low_pc, DW_FORM_addr); 2360 if (DWARF2_VERSION < 4) 2361 out_abbrev (DW_AT_high_pc, DW_FORM_addr); 2362 else 2363 out_abbrev (DW_AT_high_pc, (sizeof_address == 4 2364 ? DW_FORM_data4 : DW_FORM_data8)); 2365 } 2366 else 2367 { 2368 if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit) 2369 out_abbrev (DW_AT_ranges, DW_FORM_data4); 2370 else 2371 out_abbrev (DW_AT_ranges, DW_FORM_data8); 2372 } 2373 out_abbrev (DW_AT_name, DW_FORM_string); 2374 out_abbrev (DW_AT_comp_dir, DW_FORM_string); 2375 out_abbrev (DW_AT_producer, DW_FORM_string); 2376 out_abbrev (DW_AT_language, DW_FORM_data2); 2377 out_abbrev (0, 0); 2378 2379 /* Terminate the abbreviations for this compilation unit. */ 2380 out_byte (0); 2381 } 2382 2383 /* Emit a description of this compilation unit for .debug_info. */ 2384 2385 static void 2386 out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg) 2387 { 2388 char producer[128]; 2389 const char *comp_dir; 2390 const char *dirname; 2391 expressionS exp; 2392 symbolS *info_end; 2393 char *p; 2394 int len; 2395 int sizeof_offset; 2396 2397 sizeof_offset = out_header (info_seg, &exp); 2398 info_end = exp.X_add_symbol; 2399 2400 /* DWARF version. */ 2401 out_two (DWARF2_VERSION); 2402 2403 /* .debug_abbrev offset */ 2404 TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset); 2405 2406 /* Target address size. */ 2407 out_byte (sizeof_address); 2408 2409 /* DW_TAG_compile_unit DIE abbrev */ 2410 out_uleb128 (1); 2411 2412 /* DW_AT_stmt_list */ 2413 TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 2414 (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit 2415 ? 4 : 8)); 2416 2417 /* These two attributes are emitted if all of the code is contiguous. */ 2418 if (all_segs->next == NULL) 2419 { 2420 /* DW_AT_low_pc */ 2421 exp.X_op = O_symbol; 2422 exp.X_add_symbol = all_segs->text_start; 2423 exp.X_add_number = 0; 2424 emit_expr (&exp, sizeof_address); 2425 2426 /* DW_AT_high_pc */ 2427 if (DWARF2_VERSION < 4) 2428 exp.X_op = O_symbol; 2429 else 2430 { 2431 exp.X_op = O_subtract; 2432 exp.X_op_symbol = all_segs->text_start; 2433 } 2434 exp.X_add_symbol = all_segs->text_end; 2435 exp.X_add_number = 0; 2436 emit_expr (&exp, sizeof_address); 2437 } 2438 else 2439 { 2440 /* This attribute is emitted if the code is disjoint. */ 2441 /* DW_AT_ranges. */ 2442 TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset); 2443 } 2444 2445 /* DW_AT_name. We don't have the actual file name that was present 2446 on the command line, so assume files[1] is the main input file. 2447 We're not supposed to get called unless at least one line number 2448 entry was emitted, so this should always be defined. */ 2449 if (files_in_use == 0) 2450 abort (); 2451 if (files[1].dir) 2452 { 2453 dirname = remap_debug_filename (dirs[files[1].dir]); 2454 len = strlen (dirname); 2455 #ifdef TE_VMS 2456 /* Already has trailing slash. */ 2457 p = frag_more (len); 2458 memcpy (p, dirname, len); 2459 #else 2460 p = frag_more (len + 1); 2461 memcpy (p, dirname, len); 2462 INSERT_DIR_SEPARATOR (p, len); 2463 #endif 2464 } 2465 len = strlen (files[1].filename) + 1; 2466 p = frag_more (len); 2467 memcpy (p, files[1].filename, len); 2468 2469 /* DW_AT_comp_dir */ 2470 comp_dir = remap_debug_filename (getpwd ()); 2471 len = strlen (comp_dir) + 1; 2472 p = frag_more (len); 2473 memcpy (p, comp_dir, len); 2474 2475 /* DW_AT_producer */ 2476 sprintf (producer, "GNU AS %s", VERSION); 2477 len = strlen (producer) + 1; 2478 p = frag_more (len); 2479 memcpy (p, producer, len); 2480 2481 /* DW_AT_language. Yes, this is probably not really MIPS, but the 2482 dwarf2 draft has no standard code for assembler. */ 2483 out_two (DW_LANG_Mips_Assembler); 2484 2485 symbol_set_value_now (info_end); 2486 } 2487 2488 void 2489 dwarf2_init (void) 2490 { 2491 last_seg_ptr = &all_segs; 2492 } 2493 2494 2495 /* Finish the dwarf2 debug sections. We emit .debug.line if there 2496 were any .file/.loc directives, or --gdwarf2 was given, or if the 2497 file has a non-empty .debug_info section and an empty .debug_line 2498 section. If we emit .debug_line, and the .debug_info section is 2499 empty, we also emit .debug_info, .debug_aranges and .debug_abbrev. 2500 ALL_SEGS will be non-null if there were any .file/.loc directives, 2501 or --gdwarf2 was given and there were any located instructions 2502 emitted. */ 2503 2504 void 2505 dwarf2_finish (void) 2506 { 2507 segT line_seg; 2508 struct line_seg *s; 2509 segT info_seg; 2510 segT str_seg = NULL; 2511 int emit_other_sections = 0; 2512 int empty_debug_line = 0; 2513 2514 info_seg = bfd_get_section_by_name (stdoutput, ".debug_info"); 2515 emit_other_sections = ((info_seg == NULL || !seg_not_empty_p (info_seg)) 2516 && logicals_in_use == 0); 2517 2518 line_seg = bfd_get_section_by_name (stdoutput, ".debug_line"); 2519 empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg); 2520 2521 /* We can't construct a new debug_line section if we already have one. 2522 Give an error. */ 2523 if (all_segs && !empty_debug_line) 2524 as_fatal ("duplicate .debug_line sections"); 2525 2526 if ((!all_segs && emit_other_sections) 2527 || (!emit_other_sections && !empty_debug_line)) 2528 /* If there is no line information and no non-empty .debug_info 2529 section, or if there is both a non-empty .debug_info and a non-empty 2530 .debug_line, then we do nothing. */ 2531 return; 2532 2533 /* Calculate the size of an address for the target machine. */ 2534 sizeof_address = DWARF2_ADDR_SIZE (stdoutput); 2535 2536 /* Create and switch to the line number section. */ 2537 line_seg = subseg_new (".debug_line", 0); 2538 bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING); 2539 2540 /* For each subsection, chain the debug entries together. */ 2541 for (s = all_segs; s; s = s->next) 2542 { 2543 struct line_subseg *lss = s->head; 2544 struct line_entry **ptail = lss->ptail; 2545 2546 while ((lss = lss->next) != NULL) 2547 { 2548 *ptail = lss->head; 2549 ptail = lss->ptail; 2550 } 2551 } 2552 2553 if (logicals_in_use > 0) 2554 { 2555 str_seg = subseg_new (".debug_line_str", 0); 2556 bfd_set_section_flags (stdoutput, str_seg, 2557 (SEC_READONLY | SEC_DEBUGGING 2558 | SEC_MERGE | SEC_STRINGS)); 2559 str_seg->entsize = 1; 2560 debug_line_str_table.strings = NULL; 2561 debug_line_str_table.strings_in_use = 0; 2562 debug_line_str_table.strings_allocated = 0; 2563 debug_line_str_table.next_offset = 1; 2564 } 2565 2566 out_debug_line (line_seg, str_seg); 2567 2568 if (str_seg != NULL) 2569 out_string_table (str_seg, &debug_line_str_table); 2570 2571 /* If this is assembler generated line info, and there is no 2572 debug_info already, we need .debug_info and .debug_abbrev 2573 sections as well. */ 2574 if (emit_other_sections) 2575 { 2576 segT abbrev_seg; 2577 segT aranges_seg; 2578 segT ranges_seg; 2579 2580 gas_assert (all_segs); 2581 2582 info_seg = subseg_new (".debug_info", 0); 2583 abbrev_seg = subseg_new (".debug_abbrev", 0); 2584 aranges_seg = subseg_new (".debug_aranges", 0); 2585 2586 bfd_set_section_flags (stdoutput, info_seg, 2587 SEC_READONLY | SEC_DEBUGGING); 2588 bfd_set_section_flags (stdoutput, abbrev_seg, 2589 SEC_READONLY | SEC_DEBUGGING); 2590 bfd_set_section_flags (stdoutput, aranges_seg, 2591 SEC_READONLY | SEC_DEBUGGING); 2592 2593 record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1); 2594 2595 if (all_segs->next == NULL) 2596 ranges_seg = NULL; 2597 else 2598 { 2599 ranges_seg = subseg_new (".debug_ranges", 0); 2600 bfd_set_section_flags (stdoutput, ranges_seg, 2601 SEC_READONLY | SEC_DEBUGGING); 2602 record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1); 2603 out_debug_ranges (ranges_seg); 2604 } 2605 2606 out_debug_aranges (aranges_seg, info_seg); 2607 out_debug_abbrev (abbrev_seg, info_seg, line_seg); 2608 out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg); 2609 } 2610 } 2611