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