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