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