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