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