| 1 | /* dwarf2dbg.c - DWARF2 debug support |
| 2 | Copyright 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
| 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
| 5 | This file is part of GAS, the GNU Assembler. |
| 6 | |
| 7 | GAS is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2, 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, 59 Temple Place - Suite 330, Boston, MA |
| 20 | 02111-1307, USA. */ |
| 21 | |
| 22 | /* Logical line numbers can be controlled by the compiler via the |
| 23 | following two directives: |
| 24 | |
| 25 | .file FILENO "file.c" |
| 26 | .loc FILENO LINENO [COLUMN] |
| 27 | |
| 28 | FILENO is the filenumber. */ |
| 29 | |
| 30 | #include "ansidecl.h" |
| 31 | #include "as.h" |
| 32 | |
| 33 | #ifdef HAVE_LIMITS_H |
| 34 | #include <limits.h> |
| 35 | #else |
| 36 | #ifdef HAVE_SYS_PARAM_H |
| 37 | #include <sys/param.h> |
| 38 | #endif |
| 39 | #ifndef INT_MAX |
| 40 | #define INT_MAX (int) (((unsigned) (-1)) >> 1) |
| 41 | #endif |
| 42 | #endif |
| 43 | |
| 44 | #include "dwarf2dbg.h" |
| 45 | #include <filenames.h> |
| 46 | |
| 47 | #ifndef DWARF2_FORMAT |
| 48 | # define DWARF2_FORMAT() dwarf2_format_32bit |
| 49 | #endif |
| 50 | |
| 51 | #ifndef DWARF2_ADDR_SIZE |
| 52 | # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8) |
| 53 | #endif |
| 54 | |
| 55 | #ifdef BFD_ASSEMBLER |
| 56 | |
| 57 | #include "subsegs.h" |
| 58 | |
| 59 | #include "elf/dwarf2.h" |
| 60 | |
| 61 | /* Since we can't generate the prolog until the body is complete, we |
| 62 | use three different subsegments for .debug_line: one holding the |
| 63 | prolog, one for the directory and filename info, and one for the |
| 64 | body ("statement program"). */ |
| 65 | #define DL_PROLOG 0 |
| 66 | #define DL_FILES 1 |
| 67 | #define DL_BODY 2 |
| 68 | |
| 69 | /* First special line opcde - leave room for the standard opcodes. |
| 70 | Note: If you want to change this, you'll have to update the |
| 71 | "standard_opcode_lengths" table that is emitted below in |
| 72 | dwarf2_finish(). */ |
| 73 | #define DWARF2_LINE_OPCODE_BASE 10 |
| 74 | |
| 75 | #ifndef DWARF2_LINE_BASE |
| 76 | /* Minimum line offset in a special line info. opcode. This value |
| 77 | was chosen to give a reasonable range of values. */ |
| 78 | # define DWARF2_LINE_BASE -5 |
| 79 | #endif |
| 80 | |
| 81 | /* Range of line offsets in a special line info. opcode. */ |
| 82 | #ifndef DWARF2_LINE_RANGE |
| 83 | # define DWARF2_LINE_RANGE 14 |
| 84 | #endif |
| 85 | |
| 86 | #ifndef DWARF2_LINE_MIN_INSN_LENGTH |
| 87 | /* Define the architecture-dependent minimum instruction length (in |
| 88 | bytes). This value should be rather too small than too big. */ |
| 89 | # define DWARF2_LINE_MIN_INSN_LENGTH 1 |
| 90 | #endif |
| 91 | |
| 92 | /* Flag that indicates the initial value of the is_stmt_start flag. |
| 93 | In the present implementation, we do not mark any lines as |
| 94 | the beginning of a source statement, because that information |
| 95 | is not made available by the GCC front-end. */ |
| 96 | #define DWARF2_LINE_DEFAULT_IS_STMT 1 |
| 97 | |
| 98 | /* Given a special op, return the line skip amount. */ |
| 99 | #define SPECIAL_LINE(op) \ |
| 100 | (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE) |
| 101 | |
| 102 | /* Given a special op, return the address skip amount (in units of |
| 103 | DWARF2_LINE_MIN_INSN_LENGTH. */ |
| 104 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
| 105 | |
| 106 | /* The maximum address skip amount that can be encoded with a special op. */ |
| 107 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
| 108 | |
| 109 | struct line_entry { |
| 110 | struct line_entry *next; |
| 111 | fragS *frag; |
| 112 | addressT frag_ofs; |
| 113 | struct dwarf2_line_info loc; |
| 114 | }; |
| 115 | |
| 116 | struct line_subseg { |
| 117 | struct line_subseg *next; |
| 118 | subsegT subseg; |
| 119 | struct line_entry *head; |
| 120 | struct line_entry **ptail; |
| 121 | }; |
| 122 | |
| 123 | struct line_seg { |
| 124 | struct line_seg *next; |
| 125 | segT seg; |
| 126 | struct line_subseg *head; |
| 127 | symbolS *text_start; |
| 128 | symbolS *text_end; |
| 129 | }; |
| 130 | |
| 131 | /* Collects data for all line table entries during assembly. */ |
| 132 | static struct line_seg *all_segs; |
| 133 | |
| 134 | struct file_entry { |
| 135 | const char *filename; |
| 136 | unsigned int dir; |
| 137 | }; |
| 138 | |
| 139 | /* Table of files used by .debug_line. */ |
| 140 | static struct file_entry *files; |
| 141 | static unsigned int files_in_use; |
| 142 | static unsigned int files_allocated; |
| 143 | |
| 144 | /* Table of directories used by .debug_line. */ |
| 145 | static char **dirs; |
| 146 | static unsigned int dirs_in_use; |
| 147 | static unsigned int dirs_allocated; |
| 148 | |
| 149 | /* TRUE when we've seen a .loc directive recently. Used to avoid |
| 150 | doing work when there's nothing to do. */ |
| 151 | static bfd_boolean loc_directive_seen; |
| 152 | |
| 153 | /* Current location as indicated by the most recent .loc directive. */ |
| 154 | static struct dwarf2_line_info current; |
| 155 | |
| 156 | /* The size of an address on the target. */ |
| 157 | static unsigned int sizeof_address; |
| 158 | \f |
| 159 | static struct line_subseg *get_line_subseg (segT, subsegT); |
| 160 | static unsigned int get_filenum (const char *, unsigned int); |
| 161 | static struct frag *first_frag_for_seg (segT); |
| 162 | static struct frag *last_frag_for_seg (segT); |
| 163 | static void out_byte (int); |
| 164 | static void out_opcode (int); |
| 165 | static void out_two (int); |
| 166 | static void out_four (int); |
| 167 | static void out_abbrev (int, int); |
| 168 | static void out_uleb128 (addressT); |
| 169 | static offsetT get_frag_fix (fragS *); |
| 170 | static void out_set_addr (segT, fragS *, addressT); |
| 171 | static int size_inc_line_addr (int, addressT); |
| 172 | static void emit_inc_line_addr (int, addressT, char *, int); |
| 173 | static void out_inc_line_addr (int, addressT); |
| 174 | static void relax_inc_line_addr (int, segT, fragS *, addressT, |
| 175 | fragS *, addressT); |
| 176 | static void process_entries (segT, struct line_entry *); |
| 177 | static void out_file_list (void); |
| 178 | static void out_debug_line (segT); |
| 179 | static void out_debug_aranges (segT, segT); |
| 180 | static void out_debug_abbrev (segT); |
| 181 | static void out_debug_info (segT, segT, segT); |
| 182 | \f |
| 183 | #ifndef TC_DWARF2_EMIT_OFFSET |
| 184 | # define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset |
| 185 | static void generic_dwarf2_emit_offset (symbolS *, unsigned int); |
| 186 | |
| 187 | /* Create an offset to .dwarf2_*. */ |
| 188 | |
| 189 | static void |
| 190 | generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size) |
| 191 | { |
| 192 | expressionS expr; |
| 193 | |
| 194 | expr.X_op = O_symbol; |
| 195 | expr.X_add_symbol = symbol; |
| 196 | expr.X_add_number = 0; |
| 197 | emit_expr (&expr, size); |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */ |
| 202 | |
| 203 | static struct line_subseg * |
| 204 | get_line_subseg (segT seg, subsegT subseg) |
| 205 | { |
| 206 | static segT last_seg; |
| 207 | static subsegT last_subseg; |
| 208 | static struct line_subseg *last_line_subseg; |
| 209 | |
| 210 | struct line_seg *s; |
| 211 | struct line_subseg **pss, *ss; |
| 212 | |
| 213 | if (seg == last_seg && subseg == last_subseg) |
| 214 | return last_line_subseg; |
| 215 | |
| 216 | for (s = all_segs; s; s = s->next) |
| 217 | if (s->seg == seg) |
| 218 | goto found_seg; |
| 219 | |
| 220 | s = (struct line_seg *) xmalloc (sizeof (*s)); |
| 221 | s->next = all_segs; |
| 222 | s->seg = seg; |
| 223 | s->head = NULL; |
| 224 | all_segs = s; |
| 225 | |
| 226 | found_seg: |
| 227 | for (pss = &s->head; (ss = *pss) != NULL ; pss = &ss->next) |
| 228 | { |
| 229 | if (ss->subseg == subseg) |
| 230 | goto found_subseg; |
| 231 | if (ss->subseg > subseg) |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | ss = (struct line_subseg *) xmalloc (sizeof (*ss)); |
| 236 | ss->next = *pss; |
| 237 | ss->subseg = subseg; |
| 238 | ss->head = NULL; |
| 239 | ss->ptail = &ss->head; |
| 240 | *pss = ss; |
| 241 | |
| 242 | found_subseg: |
| 243 | last_seg = seg; |
| 244 | last_subseg = subseg; |
| 245 | last_line_subseg = ss; |
| 246 | |
| 247 | return ss; |
| 248 | } |
| 249 | |
| 250 | /* Record an entry for LOC occurring at OFS within the current fragment. */ |
| 251 | |
| 252 | void |
| 253 | dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc) |
| 254 | { |
| 255 | struct line_subseg *ss; |
| 256 | struct line_entry *e; |
| 257 | static unsigned int line = -1; |
| 258 | static unsigned int filenum = -1; |
| 259 | |
| 260 | /* Early out for as-yet incomplete location information. */ |
| 261 | if (loc->filenum == 0 || loc->line == 0) |
| 262 | return; |
| 263 | |
| 264 | /* Don't emit sequences of line symbols for the same line when the |
| 265 | symbols apply to assembler code. It is necessary to emit |
| 266 | duplicate line symbols when a compiler asks for them, because GDB |
| 267 | uses them to determine the end of the prologue. */ |
| 268 | if (debug_type == DEBUG_DWARF2 |
| 269 | && line == loc->line && filenum == loc->filenum) |
| 270 | return; |
| 271 | |
| 272 | line = loc->line; |
| 273 | filenum = loc->filenum; |
| 274 | |
| 275 | e = (struct line_entry *) xmalloc (sizeof (*e)); |
| 276 | e->next = NULL; |
| 277 | e->frag = frag_now; |
| 278 | e->frag_ofs = ofs; |
| 279 | e->loc = *loc; |
| 280 | |
| 281 | ss = get_line_subseg (now_seg, now_subseg); |
| 282 | *ss->ptail = e; |
| 283 | ss->ptail = &e->next; |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | dwarf2_where (struct dwarf2_line_info *line) |
| 288 | { |
| 289 | if (debug_type == DEBUG_DWARF2) |
| 290 | { |
| 291 | char *filename; |
| 292 | as_where (&filename, &line->line); |
| 293 | line->filenum = get_filenum (filename, 0); |
| 294 | line->column = 0; |
| 295 | line->flags = DWARF2_FLAG_BEGIN_STMT; |
| 296 | } |
| 297 | else |
| 298 | *line = current; |
| 299 | } |
| 300 | |
| 301 | /* Called for each machine instruction, or relatively atomic group of |
| 302 | machine instructions (ie built-in macro). The instruction or group |
| 303 | is SIZE bytes in length. If dwarf2 line number generation is called |
| 304 | for, emit a line statement appropriately. */ |
| 305 | |
| 306 | void |
| 307 | dwarf2_emit_insn (int size) |
| 308 | { |
| 309 | struct dwarf2_line_info loc; |
| 310 | |
| 311 | if (loc_directive_seen) |
| 312 | { |
| 313 | /* Use the last location established by a .loc directive, not |
| 314 | the value returned by dwarf2_where(). That calls as_where() |
| 315 | which will return either the logical input file name (foo.c) |
| 316 | or the physical input file name (foo.s) and not the file name |
| 317 | specified in the most recent .loc directive (eg foo.h). */ |
| 318 | loc = current; |
| 319 | |
| 320 | /* Unless we generate DWARF2 debugging information for each |
| 321 | assembler line, we only emit one line symbol for one LOC. */ |
| 322 | if (debug_type != DEBUG_DWARF2) |
| 323 | loc_directive_seen = FALSE; |
| 324 | } |
| 325 | else if (debug_type != DEBUG_DWARF2) |
| 326 | return; |
| 327 | else |
| 328 | dwarf2_where (& loc); |
| 329 | |
| 330 | dwarf2_gen_line_info (frag_now_fix () - size, &loc); |
| 331 | } |
| 332 | |
| 333 | /* Get a .debug_line file number for FILENAME. If NUM is nonzero, |
| 334 | allocate it on that file table slot, otherwise return the first |
| 335 | empty one. */ |
| 336 | |
| 337 | static unsigned int |
| 338 | get_filenum (const char *filename, unsigned int num) |
| 339 | { |
| 340 | static unsigned int last_used, last_used_dir_len; |
| 341 | const char *file; |
| 342 | size_t dir_len; |
| 343 | unsigned int i, dir; |
| 344 | |
| 345 | if (num == 0 && last_used) |
| 346 | { |
| 347 | if (! files[last_used].dir |
| 348 | && strcmp (filename, files[last_used].filename) == 0) |
| 349 | return last_used; |
| 350 | if (files[last_used].dir |
| 351 | && strncmp (filename, dirs[files[last_used].dir], |
| 352 | last_used_dir_len) == 0 |
| 353 | && IS_DIR_SEPARATOR (filename [last_used_dir_len]) |
| 354 | && strcmp (filename + last_used_dir_len + 1, |
| 355 | files[last_used].filename) == 0) |
| 356 | return last_used; |
| 357 | } |
| 358 | |
| 359 | file = lbasename (filename); |
| 360 | /* Don't make empty string from / or A: from A:/ . */ |
| 361 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
| 362 | if (file <= filename + 3) |
| 363 | file = filename; |
| 364 | #else |
| 365 | if (file == filename + 1) |
| 366 | file = filename; |
| 367 | #endif |
| 368 | dir_len = file - filename; |
| 369 | |
| 370 | dir = 0; |
| 371 | if (dir_len) |
| 372 | { |
| 373 | --dir_len; |
| 374 | for (dir = 1; dir < dirs_in_use; ++dir) |
| 375 | if (strncmp (filename, dirs[dir], dir_len) == 0 |
| 376 | && dirs[dir][dir_len] == '\0') |
| 377 | break; |
| 378 | |
| 379 | if (dir >= dirs_in_use) |
| 380 | { |
| 381 | if (dir >= dirs_allocated) |
| 382 | { |
| 383 | dirs_allocated = dir + 32; |
| 384 | dirs = (char **) |
| 385 | xrealloc (dirs, (dir + 32) * sizeof (const char *)); |
| 386 | } |
| 387 | |
| 388 | dirs[dir] = xmalloc (dir_len + 1); |
| 389 | memcpy (dirs[dir], filename, dir_len); |
| 390 | dirs[dir][dir_len] = '\0'; |
| 391 | dirs_in_use = dir + 1; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (num == 0) |
| 396 | { |
| 397 | for (i = 1; i < files_in_use; ++i) |
| 398 | if (files[i].dir == dir |
| 399 | && files[i].filename |
| 400 | && strcmp (file, files[i].filename) == 0) |
| 401 | { |
| 402 | last_used = i; |
| 403 | last_used_dir_len = dir_len; |
| 404 | return i; |
| 405 | } |
| 406 | } |
| 407 | else |
| 408 | i = num; |
| 409 | |
| 410 | if (i >= files_allocated) |
| 411 | { |
| 412 | unsigned int old = files_allocated; |
| 413 | |
| 414 | files_allocated = i + 32; |
| 415 | files = (struct file_entry *) |
| 416 | xrealloc (files, (i + 32) * sizeof (struct file_entry)); |
| 417 | |
| 418 | memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry)); |
| 419 | } |
| 420 | |
| 421 | files[i].filename = num ? file : xstrdup (file); |
| 422 | files[i].dir = dir; |
| 423 | files_in_use = i + 1; |
| 424 | last_used = i; |
| 425 | last_used_dir_len = dir_len; |
| 426 | |
| 427 | return i; |
| 428 | } |
| 429 | |
| 430 | /* Handle two forms of .file directive: |
| 431 | - Pass .file "source.c" to s_app_file |
| 432 | - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table |
| 433 | |
| 434 | If an entry is added to the file table, return a pointer to the filename. */ |
| 435 | |
| 436 | char * |
| 437 | dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED) |
| 438 | { |
| 439 | offsetT num; |
| 440 | char *filename; |
| 441 | int filename_len; |
| 442 | |
| 443 | /* Continue to accept a bare string and pass it off. */ |
| 444 | SKIP_WHITESPACE (); |
| 445 | if (*input_line_pointer == '"') |
| 446 | { |
| 447 | s_app_file (0); |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | num = get_absolute_expression (); |
| 452 | filename = demand_copy_C_string (&filename_len); |
| 453 | demand_empty_rest_of_line (); |
| 454 | |
| 455 | if (num < 1) |
| 456 | { |
| 457 | as_bad (_("file number less than one")); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | if (num < (int) files_in_use && files[num].filename != 0) |
| 462 | { |
| 463 | as_bad (_("file number %ld already allocated"), (long) num); |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | get_filenum (filename, num); |
| 468 | |
| 469 | return filename; |
| 470 | } |
| 471 | |
| 472 | void |
| 473 | dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED) |
| 474 | { |
| 475 | offsetT filenum, line, column; |
| 476 | |
| 477 | filenum = get_absolute_expression (); |
| 478 | SKIP_WHITESPACE (); |
| 479 | line = get_absolute_expression (); |
| 480 | SKIP_WHITESPACE (); |
| 481 | column = get_absolute_expression (); |
| 482 | demand_empty_rest_of_line (); |
| 483 | |
| 484 | if (filenum < 1) |
| 485 | { |
| 486 | as_bad (_("file number less than one")); |
| 487 | return; |
| 488 | } |
| 489 | if (filenum >= (int) files_in_use || files[filenum].filename == 0) |
| 490 | { |
| 491 | as_bad (_("unassigned file number %ld"), (long) filenum); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | current.filenum = filenum; |
| 496 | current.line = line; |
| 497 | current.column = column; |
| 498 | current.flags = DWARF2_FLAG_BEGIN_STMT; |
| 499 | |
| 500 | loc_directive_seen = TRUE; |
| 501 | |
| 502 | #ifndef NO_LISTING |
| 503 | if (listing) |
| 504 | { |
| 505 | if (files[filenum].dir) |
| 506 | { |
| 507 | size_t dir_len = strlen (dirs[files[filenum].dir]); |
| 508 | size_t file_len = strlen (files[filenum].filename); |
| 509 | char *cp = (char *) alloca (dir_len + 1 + file_len + 1); |
| 510 | |
| 511 | memcpy (cp, dirs[files[filenum].dir], dir_len); |
| 512 | cp[dir_len] = '/'; |
| 513 | memcpy (cp + dir_len + 1, files[filenum].filename, file_len); |
| 514 | cp[dir_len + file_len + 1] = '\0'; |
| 515 | listing_source_file (cp); |
| 516 | } |
| 517 | else |
| 518 | listing_source_file (files[filenum].filename); |
| 519 | listing_source_line (line); |
| 520 | } |
| 521 | #endif |
| 522 | } |
| 523 | \f |
| 524 | static struct frag * |
| 525 | first_frag_for_seg (segT seg) |
| 526 | { |
| 527 | frchainS *f, *first = NULL; |
| 528 | |
| 529 | for (f = frchain_root; f; f = f->frch_next) |
| 530 | if (f->frch_seg == seg |
| 531 | && (! first || first->frch_subseg > f->frch_subseg)) |
| 532 | first = f; |
| 533 | |
| 534 | return first ? first->frch_root : NULL; |
| 535 | } |
| 536 | |
| 537 | static struct frag * |
| 538 | last_frag_for_seg (segT seg) |
| 539 | { |
| 540 | frchainS *f, *last = NULL; |
| 541 | |
| 542 | for (f = frchain_root; f; f = f->frch_next) |
| 543 | if (f->frch_seg == seg |
| 544 | && (! last || last->frch_subseg < f->frch_subseg)) |
| 545 | last= f; |
| 546 | |
| 547 | return last ? last->frch_last : NULL; |
| 548 | } |
| 549 | \f |
| 550 | /* Emit a single byte into the current segment. */ |
| 551 | |
| 552 | static inline void |
| 553 | out_byte (int byte) |
| 554 | { |
| 555 | FRAG_APPEND_1_CHAR (byte); |
| 556 | } |
| 557 | |
| 558 | /* Emit a statement program opcode into the current segment. */ |
| 559 | |
| 560 | static inline void |
| 561 | out_opcode (int opc) |
| 562 | { |
| 563 | out_byte (opc); |
| 564 | } |
| 565 | |
| 566 | /* Emit a two-byte word into the current segment. */ |
| 567 | |
| 568 | static inline void |
| 569 | out_two (int data) |
| 570 | { |
| 571 | md_number_to_chars (frag_more (2), data, 2); |
| 572 | } |
| 573 | |
| 574 | /* Emit a four byte word into the current segment. */ |
| 575 | |
| 576 | static inline void |
| 577 | out_four (int data) |
| 578 | { |
| 579 | md_number_to_chars (frag_more (4), data, 4); |
| 580 | } |
| 581 | |
| 582 | /* Emit an unsigned "little-endian base 128" number. */ |
| 583 | |
| 584 | static void |
| 585 | out_uleb128 (addressT value) |
| 586 | { |
| 587 | output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0); |
| 588 | } |
| 589 | |
| 590 | /* Emit a tuple for .debug_abbrev. */ |
| 591 | |
| 592 | static inline void |
| 593 | out_abbrev (int name, int form) |
| 594 | { |
| 595 | out_uleb128 (name); |
| 596 | out_uleb128 (form); |
| 597 | } |
| 598 | |
| 599 | /* Get the size of a fragment. */ |
| 600 | |
| 601 | static offsetT |
| 602 | get_frag_fix (fragS *frag) |
| 603 | { |
| 604 | frchainS *fr; |
| 605 | |
| 606 | if (frag->fr_next) |
| 607 | return frag->fr_fix; |
| 608 | |
| 609 | /* If a fragment is the last in the chain, special measures must be |
| 610 | taken to find its size before relaxation, since it may be pending |
| 611 | on some subsegment chain. */ |
| 612 | for (fr = frchain_root; fr; fr = fr->frch_next) |
| 613 | if (fr->frch_last == frag) |
| 614 | return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal; |
| 615 | |
| 616 | abort (); |
| 617 | } |
| 618 | |
| 619 | /* Set an absolute address (may result in a relocation entry). */ |
| 620 | |
| 621 | static void |
| 622 | out_set_addr (segT seg, fragS *frag, addressT ofs) |
| 623 | { |
| 624 | expressionS expr; |
| 625 | symbolS *sym; |
| 626 | |
| 627 | sym = symbol_temp_new (seg, ofs, frag); |
| 628 | |
| 629 | out_opcode (DW_LNS_extended_op); |
| 630 | out_uleb128 (sizeof_address + 1); |
| 631 | |
| 632 | out_opcode (DW_LNE_set_address); |
| 633 | expr.X_op = O_symbol; |
| 634 | expr.X_add_symbol = sym; |
| 635 | expr.X_add_number = 0; |
| 636 | emit_expr (&expr, sizeof_address); |
| 637 | } |
| 638 | |
| 639 | #if DWARF2_LINE_MIN_INSN_LENGTH > 1 |
| 640 | static void scale_addr_delta (addressT *); |
| 641 | |
| 642 | static void |
| 643 | scale_addr_delta (addressT *addr_delta) |
| 644 | { |
| 645 | static int printed_this = 0; |
| 646 | if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0) |
| 647 | { |
| 648 | if (!printed_this) |
| 649 | as_bad("unaligned opcodes detected in executable segment"); |
| 650 | printed_this = 1; |
| 651 | } |
| 652 | *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH; |
| 653 | } |
| 654 | #else |
| 655 | #define scale_addr_delta(A) |
| 656 | #endif |
| 657 | |
| 658 | /* Encode a pair of line and address skips as efficiently as possible. |
| 659 | Note that the line skip is signed, whereas the address skip is unsigned. |
| 660 | |
| 661 | The following two routines *must* be kept in sync. This is |
| 662 | enforced by making emit_inc_line_addr abort if we do not emit |
| 663 | exactly the expected number of bytes. */ |
| 664 | |
| 665 | static int |
| 666 | size_inc_line_addr (int line_delta, addressT addr_delta) |
| 667 | { |
| 668 | unsigned int tmp, opcode; |
| 669 | int len = 0; |
| 670 | |
| 671 | /* Scale the address delta by the minimum instruction length. */ |
| 672 | scale_addr_delta (&addr_delta); |
| 673 | |
| 674 | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. |
| 675 | We cannot use special opcodes here, since we want the end_sequence |
| 676 | to emit the matrix entry. */ |
| 677 | if (line_delta == INT_MAX) |
| 678 | { |
| 679 | if (addr_delta == MAX_SPECIAL_ADDR_DELTA) |
| 680 | len = 1; |
| 681 | else |
| 682 | len = 1 + sizeof_leb128 (addr_delta, 0); |
| 683 | return len + 3; |
| 684 | } |
| 685 | |
| 686 | /* Bias the line delta by the base. */ |
| 687 | tmp = line_delta - DWARF2_LINE_BASE; |
| 688 | |
| 689 | /* If the line increment is out of range of a special opcode, we |
| 690 | must encode it with DW_LNS_advance_line. */ |
| 691 | if (tmp >= DWARF2_LINE_RANGE) |
| 692 | { |
| 693 | len = 1 + sizeof_leb128 (line_delta, 1); |
| 694 | line_delta = 0; |
| 695 | tmp = 0 - DWARF2_LINE_BASE; |
| 696 | } |
| 697 | |
| 698 | /* Bias the opcode by the special opcode base. */ |
| 699 | tmp += DWARF2_LINE_OPCODE_BASE; |
| 700 | |
| 701 | /* Avoid overflow when addr_delta is large. */ |
| 702 | if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA) |
| 703 | { |
| 704 | /* Try using a special opcode. */ |
| 705 | opcode = tmp + addr_delta * DWARF2_LINE_RANGE; |
| 706 | if (opcode <= 255) |
| 707 | return len + 1; |
| 708 | |
| 709 | /* Try using DW_LNS_const_add_pc followed by special op. */ |
| 710 | opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
| 711 | if (opcode <= 255) |
| 712 | return len + 2; |
| 713 | } |
| 714 | |
| 715 | /* Otherwise use DW_LNS_advance_pc. */ |
| 716 | len += 1 + sizeof_leb128 (addr_delta, 0); |
| 717 | |
| 718 | /* DW_LNS_copy or special opcode. */ |
| 719 | len += 1; |
| 720 | |
| 721 | return len; |
| 722 | } |
| 723 | |
| 724 | static void |
| 725 | emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len) |
| 726 | { |
| 727 | unsigned int tmp, opcode; |
| 728 | int need_copy = 0; |
| 729 | char *end = p + len; |
| 730 | |
| 731 | /* Scale the address delta by the minimum instruction length. */ |
| 732 | scale_addr_delta (&addr_delta); |
| 733 | |
| 734 | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. |
| 735 | We cannot use special opcodes here, since we want the end_sequence |
| 736 | to emit the matrix entry. */ |
| 737 | if (line_delta == INT_MAX) |
| 738 | { |
| 739 | if (addr_delta == MAX_SPECIAL_ADDR_DELTA) |
| 740 | *p++ = DW_LNS_const_add_pc; |
| 741 | else |
| 742 | { |
| 743 | *p++ = DW_LNS_advance_pc; |
| 744 | p += output_leb128 (p, addr_delta, 0); |
| 745 | } |
| 746 | |
| 747 | *p++ = DW_LNS_extended_op; |
| 748 | *p++ = 1; |
| 749 | *p++ = DW_LNE_end_sequence; |
| 750 | goto done; |
| 751 | } |
| 752 | |
| 753 | /* Bias the line delta by the base. */ |
| 754 | tmp = line_delta - DWARF2_LINE_BASE; |
| 755 | |
| 756 | /* If the line increment is out of range of a special opcode, we |
| 757 | must encode it with DW_LNS_advance_line. */ |
| 758 | if (tmp >= DWARF2_LINE_RANGE) |
| 759 | { |
| 760 | *p++ = DW_LNS_advance_line; |
| 761 | p += output_leb128 (p, line_delta, 1); |
| 762 | |
| 763 | /* Prettier, I think, to use DW_LNS_copy instead of a |
| 764 | "line +0, addr +0" special opcode. */ |
| 765 | if (addr_delta == 0) |
| 766 | { |
| 767 | *p++ = DW_LNS_copy; |
| 768 | goto done; |
| 769 | } |
| 770 | |
| 771 | line_delta = 0; |
| 772 | tmp = 0 - DWARF2_LINE_BASE; |
| 773 | need_copy = 1; |
| 774 | } |
| 775 | |
| 776 | /* Bias the opcode by the special opcode base. */ |
| 777 | tmp += DWARF2_LINE_OPCODE_BASE; |
| 778 | |
| 779 | /* Avoid overflow when addr_delta is large. */ |
| 780 | if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA) |
| 781 | { |
| 782 | /* Try using a special opcode. */ |
| 783 | opcode = tmp + addr_delta * DWARF2_LINE_RANGE; |
| 784 | if (opcode <= 255) |
| 785 | { |
| 786 | *p++ = opcode; |
| 787 | goto done; |
| 788 | } |
| 789 | |
| 790 | /* Try using DW_LNS_const_add_pc followed by special op. */ |
| 791 | opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
| 792 | if (opcode <= 255) |
| 793 | { |
| 794 | *p++ = DW_LNS_const_add_pc; |
| 795 | *p++ = opcode; |
| 796 | goto done; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /* Otherwise use DW_LNS_advance_pc. */ |
| 801 | *p++ = DW_LNS_advance_pc; |
| 802 | p += output_leb128 (p, addr_delta, 0); |
| 803 | |
| 804 | if (need_copy) |
| 805 | *p++ = DW_LNS_copy; |
| 806 | else |
| 807 | *p++ = tmp; |
| 808 | |
| 809 | done: |
| 810 | assert (p == end); |
| 811 | } |
| 812 | |
| 813 | /* Handy routine to combine calls to the above two routines. */ |
| 814 | |
| 815 | static void |
| 816 | out_inc_line_addr (int line_delta, addressT addr_delta) |
| 817 | { |
| 818 | int len = size_inc_line_addr (line_delta, addr_delta); |
| 819 | emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len); |
| 820 | } |
| 821 | |
| 822 | /* Generate a variant frag that we can use to relax address/line |
| 823 | increments between fragments of the target segment. */ |
| 824 | |
| 825 | static void |
| 826 | relax_inc_line_addr (int line_delta, segT seg, |
| 827 | fragS *to_frag, addressT to_ofs, |
| 828 | fragS *from_frag, addressT from_ofs) |
| 829 | { |
| 830 | symbolS *to_sym, *from_sym; |
| 831 | expressionS expr; |
| 832 | int max_chars; |
| 833 | |
| 834 | to_sym = symbol_temp_new (seg, to_ofs, to_frag); |
| 835 | from_sym = symbol_temp_new (seg, from_ofs, from_frag); |
| 836 | |
| 837 | expr.X_op = O_subtract; |
| 838 | expr.X_add_symbol = to_sym; |
| 839 | expr.X_op_symbol = from_sym; |
| 840 | expr.X_add_number = 0; |
| 841 | |
| 842 | /* The maximum size of the frag is the line delta with a maximum |
| 843 | sized address delta. */ |
| 844 | max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH); |
| 845 | |
| 846 | frag_var (rs_dwarf2dbg, max_chars, max_chars, 1, |
| 847 | make_expr_symbol (&expr), line_delta, NULL); |
| 848 | } |
| 849 | |
| 850 | /* The function estimates the size of a rs_dwarf2dbg variant frag |
| 851 | based on the current values of the symbols. It is called before |
| 852 | the relaxation loop. We set fr_subtype to the expected length. */ |
| 853 | |
| 854 | int |
| 855 | dwarf2dbg_estimate_size_before_relax (fragS *frag) |
| 856 | { |
| 857 | offsetT addr_delta; |
| 858 | int size; |
| 859 | |
| 860 | addr_delta = resolve_symbol_value (frag->fr_symbol); |
| 861 | size = size_inc_line_addr (frag->fr_offset, addr_delta); |
| 862 | |
| 863 | frag->fr_subtype = size; |
| 864 | |
| 865 | return size; |
| 866 | } |
| 867 | |
| 868 | /* This function relaxes a rs_dwarf2dbg variant frag based on the |
| 869 | current values of the symbols. fr_subtype is the current length |
| 870 | of the frag. This returns the change in frag length. */ |
| 871 | |
| 872 | int |
| 873 | dwarf2dbg_relax_frag (fragS *frag) |
| 874 | { |
| 875 | int old_size, new_size; |
| 876 | |
| 877 | old_size = frag->fr_subtype; |
| 878 | new_size = dwarf2dbg_estimate_size_before_relax (frag); |
| 879 | |
| 880 | return new_size - old_size; |
| 881 | } |
| 882 | |
| 883 | /* This function converts a rs_dwarf2dbg variant frag into a normal |
| 884 | fill frag. This is called after all relaxation has been done. |
| 885 | fr_subtype will be the desired length of the frag. */ |
| 886 | |
| 887 | void |
| 888 | dwarf2dbg_convert_frag (fragS *frag) |
| 889 | { |
| 890 | offsetT addr_diff; |
| 891 | |
| 892 | addr_diff = resolve_symbol_value (frag->fr_symbol); |
| 893 | |
| 894 | /* fr_var carries the max_chars that we created the fragment with. |
| 895 | fr_subtype carries the current expected length. We must, of |
| 896 | course, have allocated enough memory earlier. */ |
| 897 | assert (frag->fr_var >= (int) frag->fr_subtype); |
| 898 | |
| 899 | emit_inc_line_addr (frag->fr_offset, addr_diff, |
| 900 | frag->fr_literal + frag->fr_fix, frag->fr_subtype); |
| 901 | |
| 902 | frag->fr_fix += frag->fr_subtype; |
| 903 | frag->fr_type = rs_fill; |
| 904 | frag->fr_var = 0; |
| 905 | frag->fr_offset = 0; |
| 906 | } |
| 907 | |
| 908 | /* Generate .debug_line content for the chain of line number entries |
| 909 | beginning at E, for segment SEG. */ |
| 910 | |
| 911 | static void |
| 912 | process_entries (segT seg, struct line_entry *e) |
| 913 | { |
| 914 | unsigned filenum = 1; |
| 915 | unsigned line = 1; |
| 916 | unsigned column = 0; |
| 917 | unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_BEGIN_STMT : 0; |
| 918 | fragS *frag = NULL; |
| 919 | fragS *last_frag; |
| 920 | addressT frag_ofs = 0; |
| 921 | addressT last_frag_ofs; |
| 922 | struct line_entry *next; |
| 923 | |
| 924 | while (e) |
| 925 | { |
| 926 | int changed = 0; |
| 927 | |
| 928 | if (filenum != e->loc.filenum) |
| 929 | { |
| 930 | filenum = e->loc.filenum; |
| 931 | out_opcode (DW_LNS_set_file); |
| 932 | out_uleb128 (filenum); |
| 933 | changed = 1; |
| 934 | } |
| 935 | |
| 936 | if (column != e->loc.column) |
| 937 | { |
| 938 | column = e->loc.column; |
| 939 | out_opcode (DW_LNS_set_column); |
| 940 | out_uleb128 (column); |
| 941 | changed = 1; |
| 942 | } |
| 943 | |
| 944 | if ((e->loc.flags ^ flags) & DWARF2_FLAG_BEGIN_STMT) |
| 945 | { |
| 946 | flags = e->loc.flags; |
| 947 | out_opcode (DW_LNS_negate_stmt); |
| 948 | changed = 1; |
| 949 | } |
| 950 | |
| 951 | if (e->loc.flags & DWARF2_FLAG_BEGIN_BLOCK) |
| 952 | { |
| 953 | out_opcode (DW_LNS_set_basic_block); |
| 954 | changed = 1; |
| 955 | } |
| 956 | |
| 957 | /* Don't try to optimize away redundant entries; gdb wants two |
| 958 | entries for a function where the code starts on the same line as |
| 959 | the {, and there's no way to identify that case here. Trust gcc |
| 960 | to optimize appropriately. */ |
| 961 | if (1 /* line != e->loc.line || changed */) |
| 962 | { |
| 963 | int line_delta = e->loc.line - line; |
| 964 | if (frag == NULL) |
| 965 | { |
| 966 | out_set_addr (seg, e->frag, e->frag_ofs); |
| 967 | out_inc_line_addr (line_delta, 0); |
| 968 | } |
| 969 | else if (frag == e->frag) |
| 970 | out_inc_line_addr (line_delta, e->frag_ofs - frag_ofs); |
| 971 | else |
| 972 | relax_inc_line_addr (line_delta, seg, e->frag, e->frag_ofs, |
| 973 | frag, frag_ofs); |
| 974 | |
| 975 | frag = e->frag; |
| 976 | frag_ofs = e->frag_ofs; |
| 977 | line = e->loc.line; |
| 978 | } |
| 979 | else if (frag == NULL) |
| 980 | { |
| 981 | out_set_addr (seg, e->frag, e->frag_ofs); |
| 982 | frag = e->frag; |
| 983 | frag_ofs = e->frag_ofs; |
| 984 | } |
| 985 | |
| 986 | next = e->next; |
| 987 | free (e); |
| 988 | e = next; |
| 989 | } |
| 990 | |
| 991 | /* Emit a DW_LNE_end_sequence for the end of the section. */ |
| 992 | last_frag = last_frag_for_seg (seg); |
| 993 | last_frag_ofs = get_frag_fix (last_frag); |
| 994 | if (frag == last_frag) |
| 995 | out_inc_line_addr (INT_MAX, last_frag_ofs - frag_ofs); |
| 996 | else |
| 997 | relax_inc_line_addr (INT_MAX, seg, last_frag, last_frag_ofs, |
| 998 | frag, frag_ofs); |
| 999 | } |
| 1000 | |
| 1001 | /* Emit the directory and file tables for .debug_line. */ |
| 1002 | |
| 1003 | static void |
| 1004 | out_file_list (void) |
| 1005 | { |
| 1006 | size_t size; |
| 1007 | char *cp; |
| 1008 | unsigned int i; |
| 1009 | |
| 1010 | /* Emit directory list. */ |
| 1011 | for (i = 1; i < dirs_in_use; ++i) |
| 1012 | { |
| 1013 | size = strlen (dirs[i]) + 1; |
| 1014 | cp = frag_more (size); |
| 1015 | memcpy (cp, dirs[i], size); |
| 1016 | } |
| 1017 | /* Terminate it. */ |
| 1018 | out_byte ('\0'); |
| 1019 | |
| 1020 | for (i = 1; i < files_in_use; ++i) |
| 1021 | { |
| 1022 | if (files[i].filename == NULL) |
| 1023 | { |
| 1024 | as_bad (_("unassigned file number %ld"), (long) i); |
| 1025 | /* Prevent a crash later, particularly for file 1. */ |
| 1026 | files[i].filename = ""; |
| 1027 | continue; |
| 1028 | } |
| 1029 | |
| 1030 | size = strlen (files[i].filename) + 1; |
| 1031 | cp = frag_more (size); |
| 1032 | memcpy (cp, files[i].filename, size); |
| 1033 | |
| 1034 | out_uleb128 (files[i].dir); /* directory number */ |
| 1035 | out_uleb128 (0); /* last modification timestamp */ |
| 1036 | out_uleb128 (0); /* filesize */ |
| 1037 | } |
| 1038 | |
| 1039 | /* Terminate filename list. */ |
| 1040 | out_byte (0); |
| 1041 | } |
| 1042 | |
| 1043 | /* Emit the collected .debug_line data. */ |
| 1044 | |
| 1045 | static void |
| 1046 | out_debug_line (segT line_seg) |
| 1047 | { |
| 1048 | expressionS expr; |
| 1049 | symbolS *line_start; |
| 1050 | symbolS *prologue_end; |
| 1051 | symbolS *line_end; |
| 1052 | struct line_seg *s; |
| 1053 | enum dwarf2_format d2f; |
| 1054 | int sizeof_offset; |
| 1055 | |
| 1056 | subseg_set (line_seg, 0); |
| 1057 | |
| 1058 | line_start = symbol_temp_new_now (); |
| 1059 | prologue_end = symbol_temp_make (); |
| 1060 | line_end = symbol_temp_make (); |
| 1061 | |
| 1062 | /* Total length of the information for this compilation unit. */ |
| 1063 | expr.X_op = O_subtract; |
| 1064 | expr.X_add_symbol = line_end; |
| 1065 | expr.X_op_symbol = line_start; |
| 1066 | |
| 1067 | d2f = DWARF2_FORMAT (); |
| 1068 | if (d2f == dwarf2_format_32bit) |
| 1069 | { |
| 1070 | expr.X_add_number = -4; |
| 1071 | emit_expr (&expr, 4); |
| 1072 | sizeof_offset = 4; |
| 1073 | } |
| 1074 | else if (d2f == dwarf2_format_64bit) |
| 1075 | { |
| 1076 | expr.X_add_number = -12; |
| 1077 | out_four (-1); |
| 1078 | emit_expr (&expr, 8); |
| 1079 | sizeof_offset = 8; |
| 1080 | } |
| 1081 | else if (d2f == dwarf2_format_64bit_irix) |
| 1082 | { |
| 1083 | expr.X_add_number = -8; |
| 1084 | emit_expr (&expr, 8); |
| 1085 | sizeof_offset = 8; |
| 1086 | } |
| 1087 | else |
| 1088 | { |
| 1089 | as_fatal (_("internal error: unknown dwarf2 format")); |
| 1090 | } |
| 1091 | |
| 1092 | /* Version. */ |
| 1093 | out_two (2); |
| 1094 | |
| 1095 | /* Length of the prologue following this length. */ |
| 1096 | expr.X_op = O_subtract; |
| 1097 | expr.X_add_symbol = prologue_end; |
| 1098 | expr.X_op_symbol = line_start; |
| 1099 | expr.X_add_number = - (4 + 2 + 4); |
| 1100 | emit_expr (&expr, sizeof_offset); |
| 1101 | |
| 1102 | /* Parameters of the state machine. */ |
| 1103 | out_byte (DWARF2_LINE_MIN_INSN_LENGTH); |
| 1104 | out_byte (DWARF2_LINE_DEFAULT_IS_STMT); |
| 1105 | out_byte (DWARF2_LINE_BASE); |
| 1106 | out_byte (DWARF2_LINE_RANGE); |
| 1107 | out_byte (DWARF2_LINE_OPCODE_BASE); |
| 1108 | |
| 1109 | /* Standard opcode lengths. */ |
| 1110 | out_byte (0); /* DW_LNS_copy */ |
| 1111 | out_byte (1); /* DW_LNS_advance_pc */ |
| 1112 | out_byte (1); /* DW_LNS_advance_line */ |
| 1113 | out_byte (1); /* DW_LNS_set_file */ |
| 1114 | out_byte (1); /* DW_LNS_set_column */ |
| 1115 | out_byte (0); /* DW_LNS_negate_stmt */ |
| 1116 | out_byte (0); /* DW_LNS_set_basic_block */ |
| 1117 | out_byte (0); /* DW_LNS_const_add_pc */ |
| 1118 | out_byte (1); /* DW_LNS_fixed_advance_pc */ |
| 1119 | |
| 1120 | out_file_list (); |
| 1121 | |
| 1122 | symbol_set_value_now (prologue_end); |
| 1123 | |
| 1124 | /* For each section, emit a statement program. */ |
| 1125 | for (s = all_segs; s; s = s->next) |
| 1126 | process_entries (s->seg, s->head->head); |
| 1127 | |
| 1128 | symbol_set_value_now (line_end); |
| 1129 | } |
| 1130 | |
| 1131 | /* Emit data for .debug_aranges. */ |
| 1132 | |
| 1133 | static void |
| 1134 | out_debug_aranges (segT aranges_seg, segT info_seg) |
| 1135 | { |
| 1136 | unsigned int addr_size = sizeof_address; |
| 1137 | addressT size, skip; |
| 1138 | struct line_seg *s; |
| 1139 | expressionS expr; |
| 1140 | char *p; |
| 1141 | |
| 1142 | size = 4 + 2 + 4 + 1 + 1; |
| 1143 | |
| 1144 | skip = 2 * addr_size - (size & (2 * addr_size - 1)); |
| 1145 | if (skip == 2 * addr_size) |
| 1146 | skip = 0; |
| 1147 | size += skip; |
| 1148 | |
| 1149 | for (s = all_segs; s; s = s->next) |
| 1150 | size += 2 * addr_size; |
| 1151 | |
| 1152 | size += 2 * addr_size; |
| 1153 | |
| 1154 | subseg_set (aranges_seg, 0); |
| 1155 | |
| 1156 | /* Length of the compilation unit. */ |
| 1157 | out_four (size - 4); |
| 1158 | |
| 1159 | /* Version. */ |
| 1160 | out_two (2); |
| 1161 | |
| 1162 | /* Offset to .debug_info. */ |
| 1163 | /* ??? sizeof_offset */ |
| 1164 | TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), 4); |
| 1165 | |
| 1166 | /* Size of an address (offset portion). */ |
| 1167 | out_byte (addr_size); |
| 1168 | |
| 1169 | /* Size of a segment descriptor. */ |
| 1170 | out_byte (0); |
| 1171 | |
| 1172 | /* Align the header. */ |
| 1173 | if (skip) |
| 1174 | frag_align (ffs (2 * addr_size) - 1, 0, 0); |
| 1175 | |
| 1176 | for (s = all_segs; s; s = s->next) |
| 1177 | { |
| 1178 | fragS *frag; |
| 1179 | symbolS *beg, *end; |
| 1180 | |
| 1181 | frag = first_frag_for_seg (s->seg); |
| 1182 | beg = symbol_temp_new (s->seg, 0, frag); |
| 1183 | s->text_start = beg; |
| 1184 | |
| 1185 | frag = last_frag_for_seg (s->seg); |
| 1186 | end = symbol_temp_new (s->seg, get_frag_fix (frag), frag); |
| 1187 | s->text_end = end; |
| 1188 | |
| 1189 | expr.X_op = O_symbol; |
| 1190 | expr.X_add_symbol = beg; |
| 1191 | expr.X_add_number = 0; |
| 1192 | emit_expr (&expr, addr_size); |
| 1193 | |
| 1194 | expr.X_op = O_subtract; |
| 1195 | expr.X_add_symbol = end; |
| 1196 | expr.X_op_symbol = beg; |
| 1197 | expr.X_add_number = 0; |
| 1198 | emit_expr (&expr, addr_size); |
| 1199 | } |
| 1200 | |
| 1201 | p = frag_more (2 * addr_size); |
| 1202 | md_number_to_chars (p, 0, addr_size); |
| 1203 | md_number_to_chars (p + addr_size, 0, addr_size); |
| 1204 | } |
| 1205 | |
| 1206 | /* Emit data for .debug_abbrev. Note that this must be kept in |
| 1207 | sync with out_debug_info below. */ |
| 1208 | |
| 1209 | static void |
| 1210 | out_debug_abbrev (segT abbrev_seg) |
| 1211 | { |
| 1212 | subseg_set (abbrev_seg, 0); |
| 1213 | |
| 1214 | out_uleb128 (1); |
| 1215 | out_uleb128 (DW_TAG_compile_unit); |
| 1216 | out_byte (DW_CHILDREN_no); |
| 1217 | out_abbrev (DW_AT_stmt_list, DW_FORM_data4); |
| 1218 | if (all_segs->next == NULL) |
| 1219 | { |
| 1220 | out_abbrev (DW_AT_low_pc, DW_FORM_addr); |
| 1221 | out_abbrev (DW_AT_high_pc, DW_FORM_addr); |
| 1222 | } |
| 1223 | out_abbrev (DW_AT_name, DW_FORM_string); |
| 1224 | out_abbrev (DW_AT_comp_dir, DW_FORM_string); |
| 1225 | out_abbrev (DW_AT_producer, DW_FORM_string); |
| 1226 | out_abbrev (DW_AT_language, DW_FORM_data2); |
| 1227 | out_abbrev (0, 0); |
| 1228 | |
| 1229 | /* Terminate the abbreviations for this compilation unit. */ |
| 1230 | out_byte (0); |
| 1231 | } |
| 1232 | |
| 1233 | /* Emit a description of this compilation unit for .debug_info. */ |
| 1234 | |
| 1235 | static void |
| 1236 | out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg) |
| 1237 | { |
| 1238 | char producer[128]; |
| 1239 | char *comp_dir; |
| 1240 | expressionS expr; |
| 1241 | symbolS *info_start; |
| 1242 | symbolS *info_end; |
| 1243 | char *p; |
| 1244 | int len; |
| 1245 | enum dwarf2_format d2f; |
| 1246 | int sizeof_offset; |
| 1247 | |
| 1248 | subseg_set (info_seg, 0); |
| 1249 | |
| 1250 | info_start = symbol_temp_new_now (); |
| 1251 | info_end = symbol_temp_make (); |
| 1252 | |
| 1253 | /* Compilation Unit length. */ |
| 1254 | expr.X_op = O_subtract; |
| 1255 | expr.X_add_symbol = info_end; |
| 1256 | expr.X_op_symbol = info_start; |
| 1257 | |
| 1258 | d2f = DWARF2_FORMAT (); |
| 1259 | if (d2f == dwarf2_format_32bit) |
| 1260 | { |
| 1261 | expr.X_add_number = -4; |
| 1262 | emit_expr (&expr, 4); |
| 1263 | sizeof_offset = 4; |
| 1264 | } |
| 1265 | else if (d2f == dwarf2_format_64bit) |
| 1266 | { |
| 1267 | expr.X_add_number = -12; |
| 1268 | out_four (-1); |
| 1269 | emit_expr (&expr, 8); |
| 1270 | sizeof_offset = 8; |
| 1271 | } |
| 1272 | else if (d2f == dwarf2_format_64bit_irix) |
| 1273 | { |
| 1274 | expr.X_add_number = -8; |
| 1275 | emit_expr (&expr, 8); |
| 1276 | sizeof_offset = 8; |
| 1277 | } |
| 1278 | else |
| 1279 | { |
| 1280 | as_fatal (_("internal error: unknown dwarf2 format")); |
| 1281 | } |
| 1282 | |
| 1283 | /* DWARF version. */ |
| 1284 | out_two (2); |
| 1285 | |
| 1286 | /* .debug_abbrev offset */ |
| 1287 | TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset); |
| 1288 | |
| 1289 | /* Target address size. */ |
| 1290 | out_byte (sizeof_address); |
| 1291 | |
| 1292 | /* DW_TAG_compile_unit DIE abbrev */ |
| 1293 | out_uleb128 (1); |
| 1294 | |
| 1295 | /* DW_AT_stmt_list */ |
| 1296 | /* ??? sizeof_offset */ |
| 1297 | TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), 4); |
| 1298 | |
| 1299 | /* These two attributes may only be emitted if all of the code is |
| 1300 | contiguous. Multiple sections are not that. */ |
| 1301 | if (all_segs->next == NULL) |
| 1302 | { |
| 1303 | /* DW_AT_low_pc */ |
| 1304 | expr.X_op = O_symbol; |
| 1305 | expr.X_add_symbol = all_segs->text_start; |
| 1306 | expr.X_add_number = 0; |
| 1307 | emit_expr (&expr, sizeof_address); |
| 1308 | |
| 1309 | /* DW_AT_high_pc */ |
| 1310 | expr.X_op = O_symbol; |
| 1311 | expr.X_add_symbol = all_segs->text_end; |
| 1312 | expr.X_add_number = 0; |
| 1313 | emit_expr (&expr, sizeof_address); |
| 1314 | } |
| 1315 | |
| 1316 | /* DW_AT_name. We don't have the actual file name that was present |
| 1317 | on the command line, so assume files[1] is the main input file. |
| 1318 | We're not supposed to get called unless at least one line number |
| 1319 | entry was emitted, so this should always be defined. */ |
| 1320 | if (!files || files_in_use < 1) |
| 1321 | abort (); |
| 1322 | if (files[1].dir) |
| 1323 | { |
| 1324 | len = strlen (dirs[files[1].dir]); |
| 1325 | p = frag_more (len + 1); |
| 1326 | memcpy (p, dirs[files[1].dir], len); |
| 1327 | p[len] = '/'; |
| 1328 | } |
| 1329 | len = strlen (files[1].filename) + 1; |
| 1330 | p = frag_more (len); |
| 1331 | memcpy (p, files[1].filename, len); |
| 1332 | |
| 1333 | /* DW_AT_comp_dir */ |
| 1334 | comp_dir = getpwd (); |
| 1335 | len = strlen (comp_dir) + 1; |
| 1336 | p = frag_more (len); |
| 1337 | memcpy (p, comp_dir, len); |
| 1338 | |
| 1339 | /* DW_AT_producer */ |
| 1340 | sprintf (producer, "GNU AS %s", VERSION); |
| 1341 | len = strlen (producer) + 1; |
| 1342 | p = frag_more (len); |
| 1343 | memcpy (p, producer, len); |
| 1344 | |
| 1345 | /* DW_AT_language. Yes, this is probably not really MIPS, but the |
| 1346 | dwarf2 draft has no standard code for assembler. */ |
| 1347 | out_two (DW_LANG_Mips_Assembler); |
| 1348 | |
| 1349 | symbol_set_value_now (info_end); |
| 1350 | } |
| 1351 | |
| 1352 | void |
| 1353 | dwarf2_finish (void) |
| 1354 | { |
| 1355 | segT line_seg; |
| 1356 | struct line_seg *s; |
| 1357 | |
| 1358 | /* We don't need to do anything unless: |
| 1359 | - Some debug information was recorded via .file/.loc |
| 1360 | - or, we are generating DWARF2 information ourself (--gdwarf2) |
| 1361 | - or, there is a user-provided .debug_info section which could |
| 1362 | reference the file table in the .debug_line section we generate |
| 1363 | below. */ |
| 1364 | if (all_segs == NULL |
| 1365 | && debug_type != DEBUG_DWARF2 |
| 1366 | && bfd_get_section_by_name (stdoutput, ".debug_info") == NULL) |
| 1367 | return; |
| 1368 | |
| 1369 | /* Calculate the size of an address for the target machine. */ |
| 1370 | sizeof_address = DWARF2_ADDR_SIZE (stdoutput); |
| 1371 | |
| 1372 | /* Create and switch to the line number section. */ |
| 1373 | line_seg = subseg_new (".debug_line", 0); |
| 1374 | bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY); |
| 1375 | |
| 1376 | /* For each subsection, chain the debug entries together. */ |
| 1377 | for (s = all_segs; s; s = s->next) |
| 1378 | { |
| 1379 | struct line_subseg *ss = s->head; |
| 1380 | struct line_entry **ptail = ss->ptail; |
| 1381 | |
| 1382 | while ((ss = ss->next) != NULL) |
| 1383 | { |
| 1384 | *ptail = ss->head; |
| 1385 | ptail = ss->ptail; |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | out_debug_line (line_seg); |
| 1390 | |
| 1391 | /* If this is assembler generated line info, we need .debug_info |
| 1392 | and .debug_abbrev sections as well. */ |
| 1393 | if (all_segs != NULL && debug_type == DEBUG_DWARF2) |
| 1394 | { |
| 1395 | segT abbrev_seg; |
| 1396 | segT info_seg; |
| 1397 | segT aranges_seg; |
| 1398 | |
| 1399 | info_seg = subseg_new (".debug_info", 0); |
| 1400 | abbrev_seg = subseg_new (".debug_abbrev", 0); |
| 1401 | aranges_seg = subseg_new (".debug_aranges", 0); |
| 1402 | |
| 1403 | bfd_set_section_flags (stdoutput, info_seg, SEC_READONLY); |
| 1404 | bfd_set_section_flags (stdoutput, abbrev_seg, SEC_READONLY); |
| 1405 | bfd_set_section_flags (stdoutput, aranges_seg, SEC_READONLY); |
| 1406 | |
| 1407 | record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1); |
| 1408 | |
| 1409 | out_debug_aranges (aranges_seg, info_seg); |
| 1410 | out_debug_abbrev (abbrev_seg); |
| 1411 | out_debug_info (info_seg, abbrev_seg, line_seg); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | #else |
| 1416 | void |
| 1417 | dwarf2_finish () |
| 1418 | { |
| 1419 | } |
| 1420 | |
| 1421 | int |
| 1422 | dwarf2dbg_estimate_size_before_relax (frag) |
| 1423 | fragS *frag ATTRIBUTE_UNUSED; |
| 1424 | { |
| 1425 | as_fatal (_("dwarf2 is not supported for this object file format")); |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | int |
| 1430 | dwarf2dbg_relax_frag (frag) |
| 1431 | fragS *frag ATTRIBUTE_UNUSED; |
| 1432 | { |
| 1433 | as_fatal (_("dwarf2 is not supported for this object file format")); |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | void |
| 1438 | dwarf2dbg_convert_frag (frag) |
| 1439 | fragS *frag ATTRIBUTE_UNUSED; |
| 1440 | { |
| 1441 | as_fatal (_("dwarf2 is not supported for this object file format")); |
| 1442 | } |
| 1443 | |
| 1444 | void |
| 1445 | dwarf2_emit_insn (size) |
| 1446 | int size ATTRIBUTE_UNUSED; |
| 1447 | { |
| 1448 | } |
| 1449 | |
| 1450 | char * |
| 1451 | dwarf2_directive_file (dummy) |
| 1452 | int dummy ATTRIBUTE_UNUSED; |
| 1453 | { |
| 1454 | s_app_file (0); |
| 1455 | return NULL; |
| 1456 | } |
| 1457 | |
| 1458 | void |
| 1459 | dwarf2_directive_loc (dummy) |
| 1460 | int dummy ATTRIBUTE_UNUSED; |
| 1461 | { |
| 1462 | as_fatal (_("dwarf2 is not supported for this object file format")); |
| 1463 | } |
| 1464 | #endif /* BFD_ASSEMBLER */ |