| 1 | /* dwarf2dbg.c - DWARF2 debug support |
| 2 | Copyright (C) 1999, 2000 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 | |
| 32 | #include "as.h" |
| 33 | #include "dwarf2dbg.h" |
| 34 | #include "subsegs.h" |
| 35 | |
| 36 | #include "elf/dwarf2.h" |
| 37 | |
| 38 | /* Since we can't generate the prolog until the body is complete, we |
| 39 | use three different subsegments for .debug_line: one holding the |
| 40 | prolog, one for the directory and filename info, and one for the |
| 41 | body ("statement program"). */ |
| 42 | #define DL_PROLOG 0 |
| 43 | #define DL_FILES 1 |
| 44 | #define DL_BODY 2 |
| 45 | |
| 46 | /* First special line opcde - leave room for the standard opcodes. |
| 47 | Note: If you want to change this, you'll have to update the |
| 48 | "standard_opcode_lengths" table that is emitted below in |
| 49 | dwarf2_finish(). */ |
| 50 | #define DWARF2_LINE_OPCODE_BASE 10 |
| 51 | |
| 52 | #ifndef DWARF2_LINE_BASE |
| 53 | /* Minimum line offset in a special line info. opcode. This value |
| 54 | was chosen to give a reasonable range of values. */ |
| 55 | # define DWARF2_LINE_BASE -5 |
| 56 | #endif |
| 57 | |
| 58 | /* Range of line offsets in a special line info. opcode. */ |
| 59 | #ifndef DWARF2_LINE_RANGE |
| 60 | # define DWARF2_LINE_RANGE 14 |
| 61 | #endif |
| 62 | |
| 63 | #ifndef DWARF2_LINE_MIN_INSN_LENGTH |
| 64 | /* Define the architecture-dependent minimum instruction length (in |
| 65 | bytes). This value should be rather too small than too big. */ |
| 66 | # define DWARF2_LINE_MIN_INSN_LENGTH 4 |
| 67 | #endif |
| 68 | |
| 69 | /* Flag that indicates the initial value of the is_stmt_start flag. |
| 70 | In the present implementation, we do not mark any lines as |
| 71 | the beginning of a source statement, because that information |
| 72 | is not made available by the GCC front-end. */ |
| 73 | #define DWARF2_LINE_DEFAULT_IS_STMT 1 |
| 74 | |
| 75 | /* Flag that indicates the initial value of the is_stmt_start flag. |
| 76 | In the present implementation, we do not mark any lines as |
| 77 | the beginning of a source statement, because that information |
| 78 | is not made available by the GCC front-end. */ |
| 79 | #define DWARF2_LINE_DEFAULT_IS_STMT 1 |
| 80 | |
| 81 | /* Given a special op, return the line skip amount. */ |
| 82 | #define SPECIAL_LINE(op) \ |
| 83 | (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE) |
| 84 | |
| 85 | /* Given a special op, return the address skip amount (in units of |
| 86 | DWARF2_LINE_MIN_INSN_LENGTH. */ |
| 87 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) |
| 88 | |
| 89 | /* The maximum address skip amount that can be encoded with a special op. */ |
| 90 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
| 91 | |
| 92 | #define INITIAL_STATE \ |
| 93 | /* Initialize as per DWARF2.0 standard. */ \ |
| 94 | 0, /* address */ \ |
| 95 | 1, /* file */ \ |
| 96 | 1, /* line */ \ |
| 97 | 0, /* column */ \ |
| 98 | DWARF2_LINE_DEFAULT_IS_STMT, /* is_stmt */ \ |
| 99 | 0, /* basic_block */ \ |
| 100 | 1 /* empty_sequence */ |
| 101 | |
| 102 | static struct |
| 103 | { |
| 104 | /* state machine state as per DWARF2 manual: */ |
| 105 | struct dwarf2_sm |
| 106 | { |
| 107 | addressT addr; |
| 108 | unsigned int filenum; |
| 109 | unsigned int line; |
| 110 | unsigned int column; |
| 111 | unsigned int |
| 112 | is_stmt : 1, |
| 113 | basic_block : 1, |
| 114 | empty_sequence : 1; /* current code sequence has no DWARF2 directives? */ |
| 115 | } |
| 116 | sm; |
| 117 | |
| 118 | unsigned int |
| 119 | any_dwarf2_directives : 1; /* did we emit any DWARF2 line debug directives? */ |
| 120 | |
| 121 | fragS * frag; /* frag that "addr" is relative to */ |
| 122 | segT text_seg; /* text segment "addr" is relative to */ |
| 123 | subsegT text_subseg; |
| 124 | segT line_seg; /* ".debug_line" segment */ |
| 125 | int last_filename; /* index of last filename that was used */ |
| 126 | int num_filenames; /* index of last filename in use */ |
| 127 | int filename_len; /* length of the filename array */ |
| 128 | struct |
| 129 | { |
| 130 | int dir; /* valid after gen_dir_list() only */ |
| 131 | char *name; /* full path before gen_dir_list(), filename afterwards */ |
| 132 | } |
| 133 | *file; |
| 134 | |
| 135 | struct dwarf2_line_info current; /* current source info */ |
| 136 | |
| 137 | /* counters for statistical purposes */ |
| 138 | unsigned int num_line_entries; |
| 139 | unsigned int opcode_hist[256]; /* histogram of opcode frequencies */ |
| 140 | } |
| 141 | ls = |
| 142 | { |
| 143 | { |
| 144 | INITIAL_STATE |
| 145 | }, |
| 146 | 0, |
| 147 | 0, |
| 148 | 0, |
| 149 | 0, |
| 150 | 0, |
| 151 | 0, |
| 152 | 0, |
| 153 | 0, |
| 154 | NULL, |
| 155 | { NULL, 0, 0, 0, 0 }, |
| 156 | 0, |
| 157 | { |
| 158 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 160 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 161 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 162 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 164 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 166 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 167 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | /* Function prototypes. */ |
| 178 | static void out_uleb128 PARAMS ((addressT)); |
| 179 | static void out_sleb128 PARAMS ((offsetT)); |
| 180 | static void gen_addr_line PARAMS ((int, addressT)); |
| 181 | static void reset_state_machine PARAMS ((void)); |
| 182 | static void out_set_addr PARAMS ((addressT)); |
| 183 | static void out_end_sequence PARAMS ((void)); |
| 184 | static int get_filenum PARAMS ((int, char *)); |
| 185 | static void gen_dir_list PARAMS ((void)); |
| 186 | static void gen_file_list PARAMS ((void)); |
| 187 | static void print_stats PARAMS ((unsigned long)); |
| 188 | |
| 189 | #define out_byte(byte) FRAG_APPEND_1_CHAR(byte) |
| 190 | #define out_opcode(opc) (out_byte ((opc)), ++ls.opcode_hist[(opc) & 0xff]) |
| 191 | |
| 192 | /* Output an unsigned "little-endian base 128" number. */ |
| 193 | |
| 194 | static void |
| 195 | out_uleb128 (value) |
| 196 | addressT value; |
| 197 | { |
| 198 | unsigned char byte, more = 0x80; |
| 199 | |
| 200 | do |
| 201 | { |
| 202 | byte = value & 0x7f; |
| 203 | value >>= 7; |
| 204 | if (value == 0) |
| 205 | more = 0; |
| 206 | out_byte (more | byte); |
| 207 | } |
| 208 | while (more); |
| 209 | } |
| 210 | |
| 211 | /* Output a signed "little-endian base 128" number. */ |
| 212 | |
| 213 | static void |
| 214 | out_sleb128 (value) |
| 215 | offsetT value; |
| 216 | { |
| 217 | unsigned char byte, more = 0x80; |
| 218 | |
| 219 | do |
| 220 | { |
| 221 | byte = value & 0x7f; |
| 222 | value >>= 7; |
| 223 | if (((value == 0) && ((byte & 0x40) == 0)) |
| 224 | || ((value == -1) && ((byte & 0x40) != 0))) |
| 225 | more = 0; |
| 226 | out_byte (more | byte); |
| 227 | } |
| 228 | while (more); |
| 229 | } |
| 230 | |
| 231 | /* Encode a pair of line and address skips as efficiently as possible. |
| 232 | Note that the line skip is signed, whereas the address skip is |
| 233 | unsigned. */ |
| 234 | |
| 235 | static void |
| 236 | gen_addr_line (line_delta, addr_delta) |
| 237 | int line_delta; |
| 238 | addressT addr_delta; |
| 239 | { |
| 240 | unsigned int tmp, opcode; |
| 241 | |
| 242 | tmp = line_delta - DWARF2_LINE_BASE; |
| 243 | |
| 244 | if (tmp >= DWARF2_LINE_RANGE) |
| 245 | { |
| 246 | out_opcode (DW_LNS_advance_line); |
| 247 | out_sleb128 (line_delta); |
| 248 | tmp = 0 - DWARF2_LINE_BASE; |
| 249 | line_delta = 0; |
| 250 | } |
| 251 | |
| 252 | tmp += DWARF2_LINE_OPCODE_BASE; |
| 253 | |
| 254 | /* Try using a special opcode. */ |
| 255 | opcode = tmp + addr_delta*DWARF2_LINE_RANGE; |
| 256 | if (opcode <= 255) |
| 257 | { |
| 258 | out_opcode (opcode); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | /* Try using DW_LNS_const_add_pc followed by special op. */ |
| 263 | opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; |
| 264 | if (opcode <= 255) |
| 265 | { |
| 266 | out_opcode (DW_LNS_const_add_pc); |
| 267 | out_opcode (opcode); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | out_opcode (DW_LNS_advance_pc); |
| 272 | out_uleb128 (addr_delta); |
| 273 | |
| 274 | if (line_delta) |
| 275 | /* Output line-delta. */ |
| 276 | out_opcode (tmp); |
| 277 | else |
| 278 | /* Append new row with current info. */ |
| 279 | out_opcode (DW_LNS_copy); |
| 280 | } |
| 281 | |
| 282 | static void |
| 283 | reset_state_machine () |
| 284 | { |
| 285 | static const struct dwarf2_sm initial_state = { INITIAL_STATE }; |
| 286 | |
| 287 | ls.sm = initial_state; |
| 288 | } |
| 289 | |
| 290 | /* Set an absolute address (may results in a relocation entry). */ |
| 291 | |
| 292 | static void |
| 293 | out_set_addr (addr) |
| 294 | addressT addr; |
| 295 | { |
| 296 | subsegT saved_subseg; |
| 297 | segT saved_seg; |
| 298 | expressionS expr; |
| 299 | symbolS *sym; |
| 300 | int bytes_per_address; |
| 301 | |
| 302 | saved_seg = now_seg; |
| 303 | saved_subseg = now_subseg; |
| 304 | |
| 305 | subseg_set (ls.text_seg, ls.text_subseg); |
| 306 | sym = symbol_new (".L0\001", now_seg, addr, frag_now); |
| 307 | |
| 308 | subseg_set (saved_seg, saved_subseg); |
| 309 | |
| 310 | #ifdef BFD_ASSEMBLER |
| 311 | bytes_per_address = bfd_arch_bits_per_address (stdoutput) / 8; |
| 312 | #else |
| 313 | /* FIXME. */ |
| 314 | bytes_per_address = 4; |
| 315 | #endif |
| 316 | |
| 317 | out_opcode (DW_LNS_extended_op); |
| 318 | out_uleb128 (bytes_per_address + 1); |
| 319 | |
| 320 | out_opcode (DW_LNE_set_address); |
| 321 | expr.X_op = O_symbol; |
| 322 | expr.X_add_symbol = sym; |
| 323 | expr.X_add_number = 0; |
| 324 | emit_expr (&expr, bytes_per_address); |
| 325 | } |
| 326 | |
| 327 | /* Emit DW_LNS_end_sequence and reset state machine. Does not |
| 328 | preserve the current segment/sub-segment! */ |
| 329 | |
| 330 | static void |
| 331 | out_end_sequence () |
| 332 | { |
| 333 | addressT addr, delta; |
| 334 | fragS *text_frag; |
| 335 | |
| 336 | if (ls.text_seg) |
| 337 | { |
| 338 | subseg_set (ls.text_seg, ls.text_subseg); |
| 339 | #ifdef md_current_text_addr |
| 340 | addr = md_current_text_addr (); |
| 341 | #else |
| 342 | addr = frag_now_fix (); |
| 343 | #endif |
| 344 | text_frag = frag_now; |
| 345 | subseg_set (ls.line_seg, DL_BODY); |
| 346 | if (text_frag != ls.frag) |
| 347 | { |
| 348 | out_set_addr (addr); |
| 349 | ls.sm.addr = addr; |
| 350 | ls.frag = text_frag; |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | delta = (addr - ls.sm.addr) / DWARF2_LINE_MIN_INSN_LENGTH; |
| 355 | if (delta > 0) |
| 356 | { |
| 357 | /* Advance address without updating the line-debug |
| 358 | matrix---the end_sequence entry is used only to tell |
| 359 | the debugger the end of the sequence. */ |
| 360 | out_opcode (DW_LNS_advance_pc); |
| 361 | out_uleb128 (delta); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | else |
| 366 | subseg_set (ls.line_seg, DL_BODY); |
| 367 | |
| 368 | out_opcode (DW_LNS_extended_op); |
| 369 | out_uleb128 (1); |
| 370 | out_byte (DW_LNE_end_sequence); |
| 371 | |
| 372 | reset_state_machine (); |
| 373 | } |
| 374 | |
| 375 | /* Look up a filenumber either by filename or by filenumber. If both |
| 376 | a filenumber and a filename are specified, lookup by filename takes |
| 377 | precedence. If the filename cannot be found, it is added to the |
| 378 | filetable and the filenumber for the new entry is returned. */ |
| 379 | |
| 380 | static int |
| 381 | get_filenum (filenum, file) |
| 382 | int filenum; |
| 383 | char *file; |
| 384 | { |
| 385 | int i, last = filenum - 1; |
| 386 | char char0 = file[0]; |
| 387 | |
| 388 | /* If filenum is out of range of the filename table, then try using the |
| 389 | table entry returned from the previous call. */ |
| 390 | if (last >= ls.num_filenames || last < 0) |
| 391 | last = ls.last_filename; |
| 392 | |
| 393 | /* Do a quick check against the specified or previously used filenum. */ |
| 394 | if (ls.num_filenames > 0 && ls.file[last].name[0] == char0 |
| 395 | && strcmp (ls.file[last].name + 1, file + 1) == 0) |
| 396 | return last + 1; |
| 397 | |
| 398 | /* No match, fall back to simple linear scan. */ |
| 399 | for (i = 0; i < ls.num_filenames; ++i) |
| 400 | { |
| 401 | if (ls.file[i].name[0] == char0 |
| 402 | && strcmp (ls.file[i].name + 1, file + 1) == 0) |
| 403 | { |
| 404 | ls.last_filename = i; |
| 405 | return i + 1; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /* No match, enter new filename. */ |
| 410 | if (ls.num_filenames >= ls.filename_len) |
| 411 | { |
| 412 | ls.filename_len += 13; |
| 413 | ls.file = xrealloc (ls.file, ls.filename_len * sizeof (ls.file[0])); |
| 414 | } |
| 415 | ls.file[ls.num_filenames].dir = 0; |
| 416 | ls.file[ls.num_filenames].name = file; |
| 417 | ls.last_filename = ls.num_filenames; |
| 418 | return ++ls.num_filenames; |
| 419 | } |
| 420 | |
| 421 | /* Emit an entry in the line number table if the address or line has changed. |
| 422 | ADDR is relative to the current frag in the text section. */ |
| 423 | |
| 424 | void |
| 425 | dwarf2_gen_line_info (addr, l) |
| 426 | addressT addr; |
| 427 | struct dwarf2_line_info *l; |
| 428 | { |
| 429 | unsigned int filenum = l->filenum; |
| 430 | unsigned int any_output = 0; |
| 431 | subsegT saved_subseg; |
| 432 | segT saved_seg; |
| 433 | fragS *saved_frag; |
| 434 | |
| 435 | if (flag_debug) |
| 436 | fprintf (stderr, "line: addr %lx file `%s' line %u col %u flags %x\n", |
| 437 | (unsigned long) addr, l->filename, l->line, l->column, l->flags); |
| 438 | |
| 439 | if (filenum > 0 && !l->filename) |
| 440 | { |
| 441 | if (filenum >= (unsigned int) ls.num_filenames) |
| 442 | { |
| 443 | as_warn ("Encountered bad file number in line number debug info!"); |
| 444 | return; |
| 445 | } |
| 446 | } |
| 447 | else if (l->filename) |
| 448 | filenum = get_filenum (filenum, l->filename); |
| 449 | else |
| 450 | /* No filename, no filnum => no play. */ |
| 451 | return; |
| 452 | |
| 453 | /* Must save these before the subseg_new call, as that call will change |
| 454 | them. */ |
| 455 | saved_seg = now_seg; |
| 456 | saved_subseg = now_subseg; |
| 457 | saved_frag = frag_now; |
| 458 | |
| 459 | if (!ls.line_seg) |
| 460 | { |
| 461 | #ifdef BFD_ASSEMBLER |
| 462 | symbolS *secsym; |
| 463 | #endif |
| 464 | |
| 465 | ls.line_seg = subseg_new (".debug_line", 0); |
| 466 | |
| 467 | #ifdef BFD_ASSEMBLER |
| 468 | bfd_set_section_flags (stdoutput, ls.line_seg, SEC_READONLY); |
| 469 | |
| 470 | /* We're going to need this symbol. */ |
| 471 | secsym = symbol_find (".debug_line"); |
| 472 | if (secsym != NULL) |
| 473 | symbol_set_bfdsym (secsym, ls.line_seg->symbol); |
| 474 | else |
| 475 | symbol_table_insert (section_symbol (ls.line_seg)); |
| 476 | #endif |
| 477 | } |
| 478 | |
| 479 | subseg_set (ls.line_seg, DL_BODY); |
| 480 | |
| 481 | if (ls.text_seg != saved_seg || ls.text_subseg != saved_subseg) |
| 482 | { |
| 483 | if (!ls.sm.empty_sequence) |
| 484 | { |
| 485 | /* Terminate previous sequence. */ |
| 486 | out_end_sequence (); |
| 487 | ls.sm.empty_sequence = 1; |
| 488 | } |
| 489 | any_output = 1; |
| 490 | ls.text_seg = saved_seg; |
| 491 | ls.text_subseg = saved_subseg; |
| 492 | out_set_addr (addr); |
| 493 | ls.sm.addr = addr; |
| 494 | ls.frag = saved_frag; |
| 495 | } |
| 496 | |
| 497 | if (ls.sm.filenum != filenum) |
| 498 | { |
| 499 | any_output = 1; |
| 500 | out_opcode (DW_LNS_set_file); |
| 501 | out_uleb128 (filenum); |
| 502 | ls.sm.filenum = filenum; |
| 503 | } |
| 504 | |
| 505 | if (ls.sm.column != l->column) |
| 506 | { |
| 507 | any_output = 1; |
| 508 | out_opcode (DW_LNS_set_column); |
| 509 | out_uleb128 (l->column); |
| 510 | ls.sm.column = l->column; |
| 511 | } |
| 512 | |
| 513 | if (((l->flags & DWARF2_FLAG_BEGIN_STMT) != 0) != ls.sm.is_stmt) |
| 514 | { |
| 515 | any_output = 1; |
| 516 | out_opcode (DW_LNS_negate_stmt); |
| 517 | } |
| 518 | |
| 519 | if (l->flags & DWARF2_FLAG_BEGIN_BLOCK) |
| 520 | { |
| 521 | any_output = 1; |
| 522 | out_opcode (DW_LNS_set_basic_block); |
| 523 | } |
| 524 | |
| 525 | if (ls.sm.line != l->line) |
| 526 | { |
| 527 | any_output = 1; |
| 528 | if (saved_frag != ls.frag) |
| 529 | { |
| 530 | /* If a new frag got allocated (for whatever reason), then |
| 531 | deal with it by generating a reference symbol. Note: no |
| 532 | end_sequence needs to be generated because the address did |
| 533 | not really decrease (only the reference point changed). */ |
| 534 | out_set_addr (addr); |
| 535 | ls.sm.addr = addr; |
| 536 | ls.frag = saved_frag; |
| 537 | } |
| 538 | gen_addr_line (l->line - ls.sm.line, |
| 539 | (addr - ls.sm.addr) / DWARF2_LINE_MIN_INSN_LENGTH); |
| 540 | ls.sm.basic_block = 0; |
| 541 | ls.sm.line = l->line; |
| 542 | ls.sm.addr = addr; |
| 543 | } |
| 544 | |
| 545 | subseg_set (saved_seg, saved_subseg); |
| 546 | |
| 547 | ls.num_line_entries += any_output; |
| 548 | if (any_output) |
| 549 | ls.sm.empty_sequence = 0; |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | gen_dir_list () |
| 554 | { |
| 555 | char *str, *slash, *dir_list, *dp, *cp; |
| 556 | int i, j, num_dirs; |
| 557 | |
| 558 | dir_list = frag_more (0); |
| 559 | num_dirs = 0; |
| 560 | |
| 561 | for (i = 0; i < ls.num_filenames; ++i) |
| 562 | { |
| 563 | str = ls.file[i].name; |
| 564 | slash = strrchr (str, '/'); |
| 565 | if (slash) |
| 566 | { |
| 567 | *slash = '\0'; |
| 568 | for (j = 0, dp = dir_list; j < num_dirs; ++j) |
| 569 | { |
| 570 | if (strcmp (str, dp) == 0) |
| 571 | { |
| 572 | ls.file[i].dir = j + 1; |
| 573 | break; |
| 574 | } |
| 575 | dp += strlen (dp); |
| 576 | } |
| 577 | if (j >= num_dirs) |
| 578 | { |
| 579 | /* Didn't find this directory: append it to the list. */ |
| 580 | size_t size = strlen (str) + 1; |
| 581 | cp = frag_more (size); |
| 582 | memcpy (cp, str, size); |
| 583 | ls.file[i].dir = ++num_dirs; |
| 584 | } |
| 585 | *slash = '/'; |
| 586 | ls.file[i].name = slash + 1; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* Terminate directory list. */ |
| 591 | out_byte ('\0'); |
| 592 | } |
| 593 | |
| 594 | static void |
| 595 | gen_file_list () |
| 596 | { |
| 597 | size_t size; |
| 598 | char *cp; |
| 599 | int i; |
| 600 | |
| 601 | for (i = 0; i < ls.num_filenames; ++i) |
| 602 | { |
| 603 | size = strlen (ls.file[i].name) + 1; |
| 604 | cp = frag_more (size); |
| 605 | memcpy (cp, ls.file[i].name, size); |
| 606 | |
| 607 | out_uleb128 (ls.file[i].dir); /* directory number */ |
| 608 | out_uleb128 (0); /* last modification timestamp */ |
| 609 | out_uleb128 (0); /* filesize */ |
| 610 | } |
| 611 | |
| 612 | /* Terminate filename list. */ |
| 613 | out_byte (0); |
| 614 | } |
| 615 | |
| 616 | static void |
| 617 | print_stats (total_size) |
| 618 | unsigned long total_size; |
| 619 | { |
| 620 | static const char *opc_name[] = |
| 621 | { |
| 622 | "extended", "copy", "advance_pc", "advance_line", "set_file", |
| 623 | "set_column", "negate_stmt", "set_basic_block", "const_add_pc", |
| 624 | "fixed_advance_pc" |
| 625 | }; |
| 626 | size_t i; |
| 627 | int j; |
| 628 | |
| 629 | fprintf (stderr, "Average size: %g bytes/line\n", |
| 630 | total_size / (double) ls.num_line_entries); |
| 631 | |
| 632 | fprintf (stderr, "\nStandard opcode histogram:\n"); |
| 633 | |
| 634 | for (i = 0; i < sizeof (opc_name) / sizeof (opc_name[0]); ++i) |
| 635 | { |
| 636 | fprintf (stderr, "%s", opc_name[i]); |
| 637 | for (j = strlen (opc_name[i]); j < 16; ++j) |
| 638 | fprintf (stderr, " "); |
| 639 | fprintf (stderr, ": %u\n", ls.opcode_hist[i]); |
| 640 | } |
| 641 | |
| 642 | fprintf (stderr, "\nSpecial opcodes:\naddr\t\t\t\tline skip\n"); |
| 643 | |
| 644 | fprintf (stderr, "skip: "); |
| 645 | for (j = DWARF2_LINE_BASE; j < DWARF2_LINE_BASE + DWARF2_LINE_RANGE; ++j) |
| 646 | fprintf (stderr, "%3d", j); |
| 647 | fprintf (stderr, "\n-----"); |
| 648 | |
| 649 | for (; i < 256; ++i) |
| 650 | { |
| 651 | j = SPECIAL_LINE (i); |
| 652 | if (j == DWARF2_LINE_BASE) |
| 653 | fprintf (stderr, "\n%4u: ", |
| 654 | ((unsigned int) |
| 655 | DWARF2_LINE_MIN_INSN_LENGTH * SPECIAL_ADDR (i))); |
| 656 | fprintf (stderr, " %2u", ls.opcode_hist[i]); |
| 657 | } |
| 658 | fprintf (stderr, "\n"); |
| 659 | } |
| 660 | |
| 661 | void |
| 662 | dwarf2_finish () |
| 663 | { |
| 664 | addressT body_size, total_size, prolog_size; |
| 665 | subsegT saved_subseg; |
| 666 | segT saved_seg; |
| 667 | char *cp; |
| 668 | |
| 669 | if (!ls.line_seg) |
| 670 | /* No .debug_line segment, no work to do. */ |
| 671 | return; |
| 672 | |
| 673 | saved_seg = now_seg; |
| 674 | saved_subseg = now_subseg; |
| 675 | |
| 676 | if (!ls.sm.empty_sequence) |
| 677 | out_end_sequence (); |
| 678 | total_size = body_size = frag_now_fix (); |
| 679 | |
| 680 | /* Now generate the directory and file lists. */ |
| 681 | subseg_set (ls.line_seg, DL_FILES); |
| 682 | gen_dir_list (); |
| 683 | gen_file_list (); |
| 684 | total_size += frag_now_fix (); |
| 685 | |
| 686 | /* And now the header ("statement program prolog", in DWARF2 lingo...). */ |
| 687 | subseg_set (ls.line_seg, DL_PROLOG); |
| 688 | |
| 689 | cp = frag_more (15 + DWARF2_LINE_OPCODE_BASE - 1); |
| 690 | |
| 691 | total_size += frag_now_fix (); |
| 692 | prolog_size = total_size - body_size - 10; |
| 693 | |
| 694 | # define STUFF(val,size) md_number_to_chars (cp, val, size); cp += size; |
| 695 | STUFF (total_size - 4, 4); /* length */ |
| 696 | STUFF (2, 2); /* version */ |
| 697 | STUFF (prolog_size, 4); /* prologue_length */ |
| 698 | STUFF (DWARF2_LINE_MIN_INSN_LENGTH, 1); |
| 699 | STUFF (DWARF2_LINE_DEFAULT_IS_STMT, 1); |
| 700 | STUFF (DWARF2_LINE_BASE, 1); |
| 701 | STUFF (DWARF2_LINE_RANGE, 1); |
| 702 | STUFF (DWARF2_LINE_OPCODE_BASE, 1); |
| 703 | |
| 704 | /* standard_opcode_lengths: */ |
| 705 | STUFF (0, 1); /* DW_LNS_copy */ |
| 706 | STUFF (1, 1); /* DW_LNS_advance_pc */ |
| 707 | STUFF (1, 1); /* DW_LNS_advance_line */ |
| 708 | STUFF (1, 1); /* DW_LNS_set_file */ |
| 709 | STUFF (1, 1); /* DW_LNS_set_column */ |
| 710 | STUFF (0, 1); /* DW_LNS_negate_stmt */ |
| 711 | STUFF (0, 1); /* DW_LNS_set_basic_block */ |
| 712 | STUFF (0, 1); /* DW_LNS_const_add_pc */ |
| 713 | STUFF (1, 1); /* DW_LNS_fixed_advance_pc */ |
| 714 | |
| 715 | subseg_set (saved_seg, saved_subseg); |
| 716 | |
| 717 | if (flag_debug) |
| 718 | print_stats (total_size); |
| 719 | } |
| 720 | |
| 721 | void |
| 722 | dwarf2_directive_file (dummy) |
| 723 | int dummy ATTRIBUTE_UNUSED; |
| 724 | { |
| 725 | int len; |
| 726 | |
| 727 | /* Continue to accept a bare string and pass it off. */ |
| 728 | SKIP_WHITESPACE (); |
| 729 | if (*input_line_pointer == '"') |
| 730 | { |
| 731 | s_app_file (0); |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | ls.any_dwarf2_directives = 1; |
| 736 | |
| 737 | if (debug_type == DEBUG_NONE) |
| 738 | /* Automatically turn on DWARF2 debug info unless something else |
| 739 | has been selected. */ |
| 740 | debug_type = DEBUG_DWARF2; |
| 741 | |
| 742 | ls.current.filenum = get_absolute_expression (); |
| 743 | ls.current.filename = demand_copy_C_string (&len); |
| 744 | |
| 745 | demand_empty_rest_of_line (); |
| 746 | } |
| 747 | |
| 748 | void |
| 749 | dwarf2_directive_loc (dummy) |
| 750 | int dummy ATTRIBUTE_UNUSED; |
| 751 | { |
| 752 | ls.any_dwarf2_directives = 1; |
| 753 | |
| 754 | ls.current.filenum = get_absolute_expression (); |
| 755 | SKIP_WHITESPACE (); |
| 756 | ls.current.line = get_absolute_expression (); |
| 757 | SKIP_WHITESPACE (); |
| 758 | ls.current.column = get_absolute_expression (); |
| 759 | demand_empty_rest_of_line (); |
| 760 | |
| 761 | ls.current.flags = DWARF2_FLAG_BEGIN_STMT; |
| 762 | |
| 763 | #ifndef NO_LISTING |
| 764 | if (listing) |
| 765 | listing_source_line (ls.current.line); |
| 766 | #endif |
| 767 | } |
| 768 | |
| 769 | void |
| 770 | dwarf2_where (line) |
| 771 | struct dwarf2_line_info *line; |
| 772 | { |
| 773 | if (ls.any_dwarf2_directives) |
| 774 | *line = ls.current; |
| 775 | else |
| 776 | { |
| 777 | as_where (&line->filename, &line->line); |
| 778 | line->filenum = 0; |
| 779 | line->column = 0; |
| 780 | line->flags = DWARF2_FLAG_BEGIN_STMT; |
| 781 | } |
| 782 | } |