Commit | Line | Data |
---|---|---|
fac0d250 | 1 | /* dwarf2dbg.c - DWARF2 debug support |
aa820537 | 2 | Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
2da5c037 | 3 | Free Software Foundation, Inc. |
fac0d250 RH |
4 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
5 | ||
6 | This file is part of GAS, the GNU Assembler. | |
7 | ||
8 | GAS is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
ec2655a6 | 10 | the Free Software Foundation; either version 3, or (at your option) |
fac0d250 RH |
11 | any later version. |
12 | ||
13 | GAS is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GAS; see the file COPYING. If not, write to the Free | |
4b4da160 NC |
20 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
21 | 02110-1301, USA. */ | |
fac0d250 | 22 | |
89b66cde | 23 | /* Logical line numbers can be controlled by the compiler via the |
bd0eb99b | 24 | following directives: |
fac0d250 RH |
25 | |
26 | .file FILENO "file.c" | |
ecea7679 | 27 | .loc FILENO LINENO [COLUMN] [basic_block] [prologue_end] \ |
92846e72 CC |
28 | [epilogue_begin] [is_stmt VALUE] [isa VALUE] \ |
29 | [discriminator VALUE] | |
bd0eb99b | 30 | */ |
fac0d250 | 31 | |
fac0d250 | 32 | #include "as.h" |
bd0eb99b | 33 | #include "safe-ctype.h" |
92eb7b32 | 34 | |
42dbf88c NC |
35 | #ifdef HAVE_LIMITS_H |
36 | #include <limits.h> | |
92625c16 | 37 | #else |
6717891c NC |
38 | #ifdef HAVE_SYS_PARAM_H |
39 | #include <sys/param.h> | |
40 | #endif | |
6256f9dd | 41 | #ifndef INT_MAX |
ee515fb7 | 42 | #define INT_MAX (int) (((unsigned) (-1)) >> 1) |
42dbf88c | 43 | #endif |
b8f080d6 | 44 | #endif |
42dbf88c | 45 | |
70658493 | 46 | #include "dwarf2dbg.h" |
a7ed1ca2 | 47 | #include <filenames.h> |
70658493 | 48 | |
56487c55 NC |
49 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
50 | /* We need to decide which character to use as a directory separator. | |
51 | Just because HAVE_DOS_BASED_FILE_SYSTEM is defined, it does not | |
52 | necessarily mean that the backslash character is the one to use. | |
53 | Some environments, eg Cygwin, can support both naming conventions. | |
54 | So we use the heuristic that we only need to use the backslash if | |
55 | the path is an absolute path starting with a DOS style drive | |
56 | selector. eg C: or D: */ | |
57 | # define INSERT_DIR_SEPARATOR(string, offset) \ | |
58 | do \ | |
59 | { \ | |
60 | if (offset > 1 \ | |
7fd3924a AM |
61 | && string[0] != 0 \ |
62 | && string[1] == ':') \ | |
56487c55 NC |
63 | string [offset] = '\\'; \ |
64 | else \ | |
65 | string [offset] = '/'; \ | |
66 | } \ | |
67 | while (0) | |
68 | #else | |
69 | # define INSERT_DIR_SEPARATOR(string, offset) string[offset] = '/' | |
70 | #endif | |
71 | ||
14e777e0 | 72 | #ifndef DWARF2_FORMAT |
413a266c | 73 | # define DWARF2_FORMAT(SEC) dwarf2_format_32bit |
14e777e0 KB |
74 | #endif |
75 | ||
9605f328 | 76 | #ifndef DWARF2_ADDR_SIZE |
e4475e39 | 77 | # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8) |
9605f328 AO |
78 | #endif |
79 | ||
01e1a5bc NC |
80 | #ifndef DWARF2_FILE_NAME |
81 | #define DWARF2_FILE_NAME(FILENAME, DIRNAME) FILENAME | |
82 | #endif | |
83 | ||
84 | #ifndef DWARF2_FILE_TIME_NAME | |
85 | #define DWARF2_FILE_TIME_NAME(FILENAME,DIRNAME) 0 | |
86 | #endif | |
87 | ||
88 | #ifndef DWARF2_FILE_SIZE_NAME | |
89 | #define DWARF2_FILE_SIZE_NAME(FILENAME,DIRNAME) 0 | |
90 | #endif | |
91 | ||
fc0eebac TG |
92 | #ifndef DWARF2_VERSION |
93 | #define DWARF2_VERSION 2 | |
94 | #endif | |
95 | ||
fac0d250 RH |
96 | #include "subsegs.h" |
97 | ||
fa8f86ff | 98 | #include "dwarf2.h" |
fac0d250 | 99 | |
fac0d250 RH |
100 | /* Since we can't generate the prolog until the body is complete, we |
101 | use three different subsegments for .debug_line: one holding the | |
102 | prolog, one for the directory and filename info, and one for the | |
103 | body ("statement program"). */ | |
104 | #define DL_PROLOG 0 | |
105 | #define DL_FILES 1 | |
106 | #define DL_BODY 2 | |
107 | ||
1737851b BW |
108 | /* If linker relaxation might change offsets in the code, the DWARF special |
109 | opcodes and variable-length operands cannot be used. If this macro is | |
110 | nonzero, use the DW_LNS_fixed_advance_pc opcode instead. */ | |
111 | #ifndef DWARF2_USE_FIXED_ADVANCE_PC | |
112 | # define DWARF2_USE_FIXED_ADVANCE_PC 0 | |
113 | #endif | |
114 | ||
fac0d250 RH |
115 | /* First special line opcde - leave room for the standard opcodes. |
116 | Note: If you want to change this, you'll have to update the | |
117 | "standard_opcode_lengths" table that is emitted below in | |
bd0eb99b RH |
118 | out_debug_line(). */ |
119 | #define DWARF2_LINE_OPCODE_BASE 13 | |
fac0d250 RH |
120 | |
121 | #ifndef DWARF2_LINE_BASE | |
122 | /* Minimum line offset in a special line info. opcode. This value | |
123 | was chosen to give a reasonable range of values. */ | |
124 | # define DWARF2_LINE_BASE -5 | |
125 | #endif | |
126 | ||
127 | /* Range of line offsets in a special line info. opcode. */ | |
128 | #ifndef DWARF2_LINE_RANGE | |
129 | # define DWARF2_LINE_RANGE 14 | |
130 | #endif | |
131 | ||
132 | #ifndef DWARF2_LINE_MIN_INSN_LENGTH | |
133 | /* Define the architecture-dependent minimum instruction length (in | |
134 | bytes). This value should be rather too small than too big. */ | |
4dc7ead9 | 135 | # define DWARF2_LINE_MIN_INSN_LENGTH 1 |
fac0d250 RH |
136 | #endif |
137 | ||
bd0eb99b | 138 | /* Flag that indicates the initial value of the is_stmt_start flag. */ |
fac0d250 RH |
139 | #define DWARF2_LINE_DEFAULT_IS_STMT 1 |
140 | ||
cb30237e | 141 | /* Given a special op, return the line skip amount. */ |
fac0d250 RH |
142 | #define SPECIAL_LINE(op) \ |
143 | (((op) - DWARF2_LINE_OPCODE_BASE)%DWARF2_LINE_RANGE + DWARF2_LINE_BASE) | |
144 | ||
145 | /* Given a special op, return the address skip amount (in units of | |
146 | DWARF2_LINE_MIN_INSN_LENGTH. */ | |
147 | #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) | |
148 | ||
cb30237e | 149 | /* The maximum address skip amount that can be encoded with a special op. */ |
fac0d250 RH |
150 | #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) |
151 | ||
ee515fb7 | 152 | struct line_entry { |
220e750f | 153 | struct line_entry *next; |
07a53e5c | 154 | symbolS *label; |
220e750f | 155 | struct dwarf2_line_info loc; |
e6c774b4 | 156 | }; |
fac0d250 | 157 | |
ee515fb7 | 158 | struct line_subseg { |
220e750f RH |
159 | struct line_subseg *next; |
160 | subsegT subseg; | |
161 | struct line_entry *head; | |
162 | struct line_entry **ptail; | |
163 | }; | |
353e2c69 | 164 | |
ee515fb7 | 165 | struct line_seg { |
220e750f RH |
166 | struct line_seg *next; |
167 | segT seg; | |
168 | struct line_subseg *head; | |
169 | symbolS *text_start; | |
170 | symbolS *text_end; | |
171 | }; | |
172 | ||
173 | /* Collects data for all line table entries during assembly. */ | |
174 | static struct line_seg *all_segs; | |
1e9cc1c2 NC |
175 | /* Hash used to quickly lookup a segment by name, avoiding the need to search |
176 | through the all_segs list. */ | |
177 | static struct hash_control *all_segs_hash; | |
178 | static struct line_seg **last_seg_ptr; | |
220e750f | 179 | |
ee515fb7 | 180 | struct file_entry { |
a7ed1ca2 | 181 | const char *filename; |
220e750f RH |
182 | unsigned int dir; |
183 | }; | |
184 | ||
185 | /* Table of files used by .debug_line. */ | |
186 | static struct file_entry *files; | |
187 | static unsigned int files_in_use; | |
188 | static unsigned int files_allocated; | |
189 | ||
a7ed1ca2 NC |
190 | /* Table of directories used by .debug_line. */ |
191 | static char **dirs; | |
192 | static unsigned int dirs_in_use; | |
193 | static unsigned int dirs_allocated; | |
194 | ||
b34976b6 | 195 | /* TRUE when we've seen a .loc directive recently. Used to avoid |
220e750f | 196 | doing work when there's nothing to do. */ |
1eee4adc | 197 | bfd_boolean dwarf2_loc_directive_seen; |
220e750f | 198 | |
07a53e5c RH |
199 | /* TRUE when we're supposed to set the basic block mark whenever a |
200 | label is seen. */ | |
201 | bfd_boolean dwarf2_loc_mark_labels; | |
202 | ||
220e750f | 203 | /* Current location as indicated by the most recent .loc directive. */ |
bd0eb99b RH |
204 | static struct dwarf2_line_info current = { |
205 | 1, 1, 0, 0, | |
92846e72 CC |
206 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, |
207 | 0 | |
bd0eb99b | 208 | }; |
220e750f | 209 | |
220e750f RH |
210 | /* The size of an address on the target. */ |
211 | static unsigned int sizeof_address; | |
212 | \f | |
a2e22468 | 213 | static unsigned int get_filenum (const char *, unsigned int); |
413a266c | 214 | |
c5c0a210 | 215 | #ifndef TC_DWARF2_EMIT_OFFSET |
802f5d9e | 216 | #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset |
c5c0a210 | 217 | |
6174d9c8 RH |
218 | /* Create an offset to .dwarf2_*. */ |
219 | ||
220 | static void | |
a2e22468 | 221 | generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size) |
6174d9c8 | 222 | { |
91d6fa6a | 223 | expressionS exp; |
6174d9c8 | 224 | |
91d6fa6a NC |
225 | exp.X_op = O_symbol; |
226 | exp.X_add_symbol = symbol; | |
227 | exp.X_add_number = 0; | |
228 | emit_expr (&exp, size); | |
6174d9c8 | 229 | } |
c5c0a210 | 230 | #endif |
6174d9c8 | 231 | |
220e750f RH |
232 | /* Find or create an entry for SEG+SUBSEG in ALL_SEGS. */ |
233 | ||
234 | static struct line_subseg * | |
a2e22468 | 235 | get_line_subseg (segT seg, subsegT subseg) |
220e750f RH |
236 | { |
237 | static segT last_seg; | |
238 | static subsegT last_subseg; | |
239 | static struct line_subseg *last_line_subseg; | |
240 | ||
1e9cc1c2 | 241 | struct line_seg *s; |
91d6fa6a | 242 | struct line_subseg **pss, *lss; |
fac0d250 | 243 | |
220e750f RH |
244 | if (seg == last_seg && subseg == last_subseg) |
245 | return last_line_subseg; | |
246 | ||
1e9cc1c2 NC |
247 | s = (struct line_seg *) hash_find (all_segs_hash, seg->name); |
248 | if (s == NULL) | |
249 | { | |
250 | s = (struct line_seg *) xmalloc (sizeof (*s)); | |
251 | s->next = NULL; | |
252 | s->seg = seg; | |
253 | s->head = NULL; | |
254 | *last_seg_ptr = s; | |
255 | last_seg_ptr = &s->next; | |
256 | hash_insert (all_segs_hash, seg->name, s); | |
257 | } | |
258 | gas_assert (seg == s->seg); | |
220e750f | 259 | |
91d6fa6a | 260 | for (pss = &s->head; (lss = *pss) != NULL ; pss = &lss->next) |
fac0d250 | 261 | { |
91d6fa6a | 262 | if (lss->subseg == subseg) |
ee515fb7 | 263 | goto found_subseg; |
91d6fa6a | 264 | if (lss->subseg > subseg) |
220e750f | 265 | break; |
fac0d250 | 266 | } |
220e750f | 267 | |
91d6fa6a NC |
268 | lss = (struct line_subseg *) xmalloc (sizeof (*lss)); |
269 | lss->next = *pss; | |
270 | lss->subseg = subseg; | |
271 | lss->head = NULL; | |
272 | lss->ptail = &lss->head; | |
273 | *pss = lss; | |
220e750f RH |
274 | |
275 | found_subseg: | |
276 | last_seg = seg; | |
277 | last_subseg = subseg; | |
91d6fa6a | 278 | last_line_subseg = lss; |
220e750f | 279 | |
91d6fa6a | 280 | return lss; |
fac0d250 RH |
281 | } |
282 | ||
07a53e5c RH |
283 | /* Record an entry for LOC occurring at LABEL. */ |
284 | ||
285 | static void | |
286 | dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc) | |
287 | { | |
91d6fa6a | 288 | struct line_subseg *lss; |
07a53e5c RH |
289 | struct line_entry *e; |
290 | ||
291 | e = (struct line_entry *) xmalloc (sizeof (*e)); | |
292 | e->next = NULL; | |
293 | e->label = label; | |
294 | e->loc = *loc; | |
295 | ||
91d6fa6a NC |
296 | lss = get_line_subseg (now_seg, now_subseg); |
297 | *lss->ptail = e; | |
298 | lss->ptail = &e->next; | |
07a53e5c RH |
299 | } |
300 | ||
436d9e46 | 301 | /* Record an entry for LOC occurring at OFS within the current fragment. */ |
353e2c69 | 302 | |
220e750f | 303 | void |
a2e22468 | 304 | dwarf2_gen_line_info (addressT ofs, struct dwarf2_line_info *loc) |
fac0d250 | 305 | { |
1ea5c325 MS |
306 | static unsigned int line = -1; |
307 | static unsigned int filenum = -1; | |
220e750f | 308 | |
07a53e5c RH |
309 | symbolS *sym; |
310 | ||
220e750f RH |
311 | /* Early out for as-yet incomplete location information. */ |
312 | if (loc->filenum == 0 || loc->line == 0) | |
313 | return; | |
314 | ||
ffa554ed GK |
315 | /* Don't emit sequences of line symbols for the same line when the |
316 | symbols apply to assembler code. It is necessary to emit | |
317 | duplicate line symbols when a compiler asks for them, because GDB | |
318 | uses them to determine the end of the prologue. */ | |
d1a6c242 | 319 | if (debug_type == DEBUG_DWARF2 |
ffa554ed | 320 | && line == loc->line && filenum == loc->filenum) |
1ea5c325 MS |
321 | return; |
322 | ||
323 | line = loc->line; | |
324 | filenum = loc->filenum; | |
325 | ||
07a53e5c RH |
326 | sym = symbol_temp_new (now_seg, ofs, frag_now); |
327 | dwarf2_gen_line_info_1 (sym, loc); | |
220e750f | 328 | } |
fac0d250 | 329 | |
ecea7679 RH |
330 | /* Returns the current source information. If .file directives have |
331 | been encountered, the info for the corresponding source file is | |
332 | returned. Otherwise, the info for the assembly source file is | |
333 | returned. */ | |
334 | ||
220e750f | 335 | void |
a2e22468 | 336 | dwarf2_where (struct dwarf2_line_info *line) |
220e750f RH |
337 | { |
338 | if (debug_type == DEBUG_DWARF2) | |
fac0d250 | 339 | { |
220e750f RH |
340 | char *filename; |
341 | as_where (&filename, &line->line); | |
a7ed1ca2 | 342 | line->filenum = get_filenum (filename, 0); |
220e750f | 343 | line->column = 0; |
bd0eb99b | 344 | line->flags = DWARF2_FLAG_IS_STMT; |
ecea7679 | 345 | line->isa = current.isa; |
92846e72 | 346 | line->discriminator = current.discriminator; |
fac0d250 | 347 | } |
220e750f RH |
348 | else |
349 | *line = current; | |
fac0d250 RH |
350 | } |
351 | ||
7fd3924a | 352 | /* A hook to allow the target backend to inform the line number state |
ecea7679 RH |
353 | machine of isa changes when assembler debug info is enabled. */ |
354 | ||
355 | void | |
356 | dwarf2_set_isa (unsigned int isa) | |
357 | { | |
358 | current.isa = isa; | |
359 | } | |
360 | ||
220e750f RH |
361 | /* Called for each machine instruction, or relatively atomic group of |
362 | machine instructions (ie built-in macro). The instruction or group | |
363 | is SIZE bytes in length. If dwarf2 line number generation is called | |
364 | for, emit a line statement appropriately. */ | |
353e2c69 | 365 | |
220e750f | 366 | void |
a2e22468 | 367 | dwarf2_emit_insn (int size) |
fac0d250 | 368 | { |
220e750f | 369 | struct dwarf2_line_info loc; |
fac0d250 | 370 | |
7fd3924a | 371 | if (!dwarf2_loc_directive_seen && debug_type != DEBUG_DWARF2) |
220e750f | 372 | return; |
7fd3924a AM |
373 | |
374 | dwarf2_where (&loc); | |
b6675117 | 375 | |
220e750f | 376 | dwarf2_gen_line_info (frag_now_fix () - size, &loc); |
661ba50f BW |
377 | dwarf2_consume_line_info (); |
378 | } | |
379 | ||
380 | /* Called after the current line information has been either used with | |
381 | dwarf2_gen_line_info or saved with a machine instruction for later use. | |
382 | This resets the state of the line number information to reflect that | |
383 | it has been used. */ | |
384 | ||
385 | void | |
386 | dwarf2_consume_line_info (void) | |
387 | { | |
388 | /* Unless we generate DWARF2 debugging information for each | |
389 | assembler line, we only emit one line symbol for one LOC. */ | |
7fd3924a | 390 | dwarf2_loc_directive_seen = FALSE; |
bd0eb99b RH |
391 | |
392 | current.flags &= ~(DWARF2_FLAG_BASIC_BLOCK | |
393 | | DWARF2_FLAG_PROLOGUE_END | |
394 | | DWARF2_FLAG_EPILOGUE_BEGIN); | |
92846e72 | 395 | current.discriminator = 0; |
220e750f | 396 | } |
fac0d250 | 397 | |
07a53e5c RH |
398 | /* Called for each (preferably code) label. If dwarf2_loc_mark_labels |
399 | is enabled, emit a basic block marker. */ | |
400 | ||
401 | void | |
402 | dwarf2_emit_label (symbolS *label) | |
403 | { | |
404 | struct dwarf2_line_info loc; | |
405 | ||
406 | if (!dwarf2_loc_mark_labels) | |
407 | return; | |
408 | if (S_GET_SEGMENT (label) != now_seg) | |
409 | return; | |
410 | if (!(bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE)) | |
411 | return; | |
7fd3924a AM |
412 | if (files_in_use == 0 && debug_type != DEBUG_DWARF2) |
413 | return; | |
414 | ||
415 | dwarf2_where (&loc); | |
07a53e5c RH |
416 | |
417 | loc.flags |= DWARF2_FLAG_BASIC_BLOCK; | |
418 | ||
07a53e5c | 419 | dwarf2_gen_line_info_1 (label, &loc); |
7fd3924a | 420 | dwarf2_consume_line_info (); |
07a53e5c RH |
421 | } |
422 | ||
a7ed1ca2 NC |
423 | /* Get a .debug_line file number for FILENAME. If NUM is nonzero, |
424 | allocate it on that file table slot, otherwise return the first | |
425 | empty one. */ | |
220e750f RH |
426 | |
427 | static unsigned int | |
a2e22468 | 428 | get_filenum (const char *filename, unsigned int num) |
220e750f | 429 | { |
a7ed1ca2 NC |
430 | static unsigned int last_used, last_used_dir_len; |
431 | const char *file; | |
432 | size_t dir_len; | |
433 | unsigned int i, dir; | |
220e750f | 434 | |
a7ed1ca2 NC |
435 | if (num == 0 && last_used) |
436 | { | |
437 | if (! files[last_used].dir | |
8b6efd89 | 438 | && filename_cmp (filename, files[last_used].filename) == 0) |
a7ed1ca2 NC |
439 | return last_used; |
440 | if (files[last_used].dir | |
8b6efd89 KT |
441 | && filename_ncmp (filename, dirs[files[last_used].dir], |
442 | last_used_dir_len) == 0 | |
a7ed1ca2 | 443 | && IS_DIR_SEPARATOR (filename [last_used_dir_len]) |
8b6efd89 KT |
444 | && filename_cmp (filename + last_used_dir_len + 1, |
445 | files[last_used].filename) == 0) | |
a7ed1ca2 NC |
446 | return last_used; |
447 | } | |
220e750f | 448 | |
a7ed1ca2 NC |
449 | file = lbasename (filename); |
450 | /* Don't make empty string from / or A: from A:/ . */ | |
451 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
452 | if (file <= filename + 3) | |
453 | file = filename; | |
454 | #else | |
455 | if (file == filename + 1) | |
456 | file = filename; | |
457 | #endif | |
458 | dir_len = file - filename; | |
459 | ||
460 | dir = 0; | |
461 | if (dir_len) | |
462 | { | |
01e1a5bc | 463 | #ifndef DWARF2_DIR_SHOULD_END_WITH_SEPARATOR |
a7ed1ca2 | 464 | --dir_len; |
01e1a5bc | 465 | #endif |
a7ed1ca2 | 466 | for (dir = 1; dir < dirs_in_use; ++dir) |
8b6efd89 | 467 | if (filename_ncmp (filename, dirs[dir], dir_len) == 0 |
a7ed1ca2 NC |
468 | && dirs[dir][dir_len] == '\0') |
469 | break; | |
470 | ||
471 | if (dir >= dirs_in_use) | |
472 | { | |
473 | if (dir >= dirs_allocated) | |
474 | { | |
475 | dirs_allocated = dir + 32; | |
476 | dirs = (char **) | |
477 | xrealloc (dirs, (dir + 32) * sizeof (const char *)); | |
478 | } | |
479 | ||
1e9cc1c2 | 480 | dirs[dir] = (char *) xmalloc (dir_len + 1); |
a7ed1ca2 NC |
481 | memcpy (dirs[dir], filename, dir_len); |
482 | dirs[dir][dir_len] = '\0'; | |
483 | dirs_in_use = dir + 1; | |
484 | } | |
485 | } | |
486 | ||
487 | if (num == 0) | |
488 | { | |
489 | for (i = 1; i < files_in_use; ++i) | |
490 | if (files[i].dir == dir | |
88b4ca40 | 491 | && files[i].filename |
8b6efd89 | 492 | && filename_cmp (file, files[i].filename) == 0) |
a7ed1ca2 NC |
493 | { |
494 | last_used = i; | |
495 | last_used_dir_len = dir_len; | |
496 | return i; | |
497 | } | |
498 | } | |
499 | else | |
500 | i = num; | |
220e750f RH |
501 | |
502 | if (i >= files_allocated) | |
fac0d250 | 503 | { |
249e3833 RH |
504 | unsigned int old = files_allocated; |
505 | ||
220e750f RH |
506 | files_allocated = i + 32; |
507 | files = (struct file_entry *) | |
ee515fb7 | 508 | xrealloc (files, (i + 32) * sizeof (struct file_entry)); |
249e3833 RH |
509 | |
510 | memset (files + old, 0, (i + 32 - old) * sizeof (struct file_entry)); | |
fac0d250 RH |
511 | } |
512 | ||
a7ed1ca2 NC |
513 | files[i].filename = num ? file : xstrdup (file); |
514 | files[i].dir = dir; | |
10cd14b4 AM |
515 | if (files_in_use < i + 1) |
516 | files_in_use = i + 1; | |
220e750f | 517 | last_used = i; |
a7ed1ca2 | 518 | last_used_dir_len = dir_len; |
220e750f RH |
519 | |
520 | return i; | |
521 | } | |
fac0d250 | 522 | |
ecb4347a DJ |
523 | /* Handle two forms of .file directive: |
524 | - Pass .file "source.c" to s_app_file | |
525 | - Handle .file 1 "source.c" by adding an entry to the DWARF-2 file table | |
220e750f | 526 | |
ecb4347a DJ |
527 | If an entry is added to the file table, return a pointer to the filename. */ |
528 | ||
529 | char * | |
a2e22468 | 530 | dwarf2_directive_file (int dummy ATTRIBUTE_UNUSED) |
220e750f RH |
531 | { |
532 | offsetT num; | |
e46d99eb | 533 | char *filename; |
220e750f RH |
534 | int filename_len; |
535 | ||
536 | /* Continue to accept a bare string and pass it off. */ | |
537 | SKIP_WHITESPACE (); | |
538 | if (*input_line_pointer == '"') | |
fac0d250 | 539 | { |
220e750f | 540 | s_app_file (0); |
ecb4347a | 541 | return NULL; |
fac0d250 RH |
542 | } |
543 | ||
220e750f RH |
544 | num = get_absolute_expression (); |
545 | filename = demand_copy_C_string (&filename_len); | |
bd0eb99b RH |
546 | if (filename == NULL) |
547 | return NULL; | |
220e750f RH |
548 | demand_empty_rest_of_line (); |
549 | ||
249e3833 | 550 | if (num < 1) |
fac0d250 | 551 | { |
0e389e77 | 552 | as_bad (_("file number less than one")); |
ecb4347a | 553 | return NULL; |
fac0d250 RH |
554 | } |
555 | ||
7cadeb2c AM |
556 | /* A .file directive implies compiler generated debug information is |
557 | being supplied. Turn off gas generated debug info. */ | |
558 | debug_type = DEBUG_NONE; | |
559 | ||
0e1a166b | 560 | if (num < (int) files_in_use && files[num].filename != 0) |
220e750f | 561 | { |
0e389e77 | 562 | as_bad (_("file number %ld already allocated"), (long) num); |
ecb4347a | 563 | return NULL; |
249e3833 | 564 | } |
220e750f | 565 | |
a7ed1ca2 | 566 | get_filenum (filename, num); |
ecb4347a DJ |
567 | |
568 | return filename; | |
fac0d250 RH |
569 | } |
570 | ||
220e750f | 571 | void |
a2e22468 | 572 | dwarf2_directive_loc (int dummy ATTRIBUTE_UNUSED) |
220e750f | 573 | { |
ecea7679 RH |
574 | offsetT filenum, line; |
575 | ||
851feff8 DJ |
576 | /* If we see two .loc directives in a row, force the first one to be |
577 | output now. */ | |
7cadeb2c | 578 | if (dwarf2_loc_directive_seen) |
851feff8 DJ |
579 | dwarf2_emit_insn (0); |
580 | ||
ecea7679 RH |
581 | filenum = get_absolute_expression (); |
582 | SKIP_WHITESPACE (); | |
583 | line = get_absolute_expression (); | |
584 | ||
585 | if (filenum < 1) | |
586 | { | |
587 | as_bad (_("file number less than one")); | |
588 | return; | |
589 | } | |
590 | if (filenum >= (int) files_in_use || files[filenum].filename == 0) | |
591 | { | |
592 | as_bad (_("unassigned file number %ld"), (long) filenum); | |
593 | return; | |
594 | } | |
595 | ||
596 | current.filenum = filenum; | |
597 | current.line = line; | |
92846e72 | 598 | current.discriminator = 0; |
ecea7679 RH |
599 | |
600 | #ifndef NO_LISTING | |
601 | if (listing) | |
602 | { | |
603 | if (files[filenum].dir) | |
604 | { | |
605 | size_t dir_len = strlen (dirs[files[filenum].dir]); | |
606 | size_t file_len = strlen (files[filenum].filename); | |
607 | char *cp = (char *) alloca (dir_len + 1 + file_len + 1); | |
608 | ||
609 | memcpy (cp, dirs[files[filenum].dir], dir_len); | |
56487c55 | 610 | INSERT_DIR_SEPARATOR (cp, dir_len); |
ecea7679 RH |
611 | memcpy (cp + dir_len + 1, files[filenum].filename, file_len); |
612 | cp[dir_len + file_len + 1] = '\0'; | |
613 | listing_source_file (cp); | |
614 | } | |
615 | else | |
616 | listing_source_file (files[filenum].filename); | |
617 | listing_source_line (line); | |
618 | } | |
619 | #endif | |
620 | ||
220e750f | 621 | SKIP_WHITESPACE (); |
ecea7679 RH |
622 | if (ISDIGIT (*input_line_pointer)) |
623 | { | |
624 | current.column = get_absolute_expression (); | |
625 | SKIP_WHITESPACE (); | |
626 | } | |
627 | ||
628 | while (ISALPHA (*input_line_pointer)) | |
220e750f | 629 | { |
bd0eb99b RH |
630 | char *p, c; |
631 | offsetT value; | |
632 | ||
633 | p = input_line_pointer; | |
634 | c = get_symbol_end (); | |
635 | ||
636 | if (strcmp (p, "basic_block") == 0) | |
637 | { | |
638 | current.flags |= DWARF2_FLAG_BASIC_BLOCK; | |
639 | *input_line_pointer = c; | |
640 | } | |
641 | else if (strcmp (p, "prologue_end") == 0) | |
642 | { | |
643 | current.flags |= DWARF2_FLAG_PROLOGUE_END; | |
644 | *input_line_pointer = c; | |
645 | } | |
646 | else if (strcmp (p, "epilogue_begin") == 0) | |
647 | { | |
648 | current.flags |= DWARF2_FLAG_EPILOGUE_BEGIN; | |
649 | *input_line_pointer = c; | |
650 | } | |
651 | else if (strcmp (p, "is_stmt") == 0) | |
652 | { | |
653 | *input_line_pointer = c; | |
654 | value = get_absolute_expression (); | |
655 | if (value == 0) | |
656 | current.flags &= ~DWARF2_FLAG_IS_STMT; | |
657 | else if (value == 1) | |
658 | current.flags |= DWARF2_FLAG_IS_STMT; | |
659 | else | |
ecea7679 RH |
660 | { |
661 | as_bad (_("is_stmt value not 0 or 1")); | |
662 | return; | |
663 | } | |
bd0eb99b RH |
664 | } |
665 | else if (strcmp (p, "isa") == 0) | |
666 | { | |
7fd3924a | 667 | *input_line_pointer = c; |
bd0eb99b | 668 | value = get_absolute_expression (); |
ecea7679 | 669 | if (value >= 0) |
bd0eb99b | 670 | current.isa = value; |
ecea7679 RH |
671 | else |
672 | { | |
673 | as_bad (_("isa number less than zero")); | |
674 | return; | |
675 | } | |
bd0eb99b | 676 | } |
92846e72 CC |
677 | else if (strcmp (p, "discriminator") == 0) |
678 | { | |
679 | *input_line_pointer = c; | |
680 | value = get_absolute_expression (); | |
681 | if (value >= 0) | |
682 | current.discriminator = value; | |
683 | else | |
684 | { | |
685 | as_bad (_("discriminator less than zero")); | |
686 | return; | |
687 | } | |
688 | } | |
bd0eb99b RH |
689 | else |
690 | { | |
ecea7679 | 691 | as_bad (_("unknown .loc sub-directive `%s'"), p); |
7fd3924a | 692 | *input_line_pointer = c; |
bd0eb99b RH |
693 | return; |
694 | } | |
695 | ||
ecea7679 | 696 | SKIP_WHITESPACE (); |
bd0eb99b RH |
697 | } |
698 | ||
699 | demand_empty_rest_of_line (); | |
1eee4adc | 700 | dwarf2_loc_directive_seen = TRUE; |
7cadeb2c | 701 | debug_type = DEBUG_NONE; |
220e750f | 702 | } |
07a53e5c RH |
703 | |
704 | void | |
705 | dwarf2_directive_loc_mark_labels (int dummy ATTRIBUTE_UNUSED) | |
706 | { | |
707 | offsetT value = get_absolute_expression (); | |
708 | ||
709 | if (value != 0 && value != 1) | |
710 | { | |
711 | as_bad (_("expected 0 or 1")); | |
712 | ignore_rest_of_line (); | |
713 | } | |
714 | else | |
715 | { | |
716 | dwarf2_loc_mark_labels = value != 0; | |
717 | demand_empty_rest_of_line (); | |
718 | } | |
719 | } | |
220e750f RH |
720 | \f |
721 | static struct frag * | |
a2e22468 | 722 | first_frag_for_seg (segT seg) |
220e750f | 723 | { |
c9049d30 | 724 | return seg_info (seg)->frchainP->frch_root; |
220e750f RH |
725 | } |
726 | ||
727 | static struct frag * | |
a2e22468 | 728 | last_frag_for_seg (segT seg) |
220e750f | 729 | { |
c9049d30 | 730 | frchainS *f = seg_info (seg)->frchainP; |
220e750f | 731 | |
c9049d30 AM |
732 | while (f->frch_next != NULL) |
733 | f = f->frch_next; | |
220e750f | 734 | |
c9049d30 | 735 | return f->frch_last; |
220e750f RH |
736 | } |
737 | \f | |
738 | /* Emit a single byte into the current segment. */ | |
739 | ||
740 | static inline void | |
a2e22468 | 741 | out_byte (int byte) |
220e750f RH |
742 | { |
743 | FRAG_APPEND_1_CHAR (byte); | |
744 | } | |
745 | ||
746 | /* Emit a statement program opcode into the current segment. */ | |
747 | ||
748 | static inline void | |
a2e22468 | 749 | out_opcode (int opc) |
220e750f RH |
750 | { |
751 | out_byte (opc); | |
752 | } | |
753 | ||
754 | /* Emit a two-byte word into the current segment. */ | |
755 | ||
756 | static inline void | |
a2e22468 | 757 | out_two (int data) |
220e750f RH |
758 | { |
759 | md_number_to_chars (frag_more (2), data, 2); | |
760 | } | |
761 | ||
762 | /* Emit a four byte word into the current segment. */ | |
763 | ||
764 | static inline void | |
a2e22468 | 765 | out_four (int data) |
220e750f RH |
766 | { |
767 | md_number_to_chars (frag_more (4), data, 4); | |
768 | } | |
769 | ||
770 | /* Emit an unsigned "little-endian base 128" number. */ | |
771 | ||
fac0d250 | 772 | static void |
a2e22468 | 773 | out_uleb128 (addressT value) |
220e750f RH |
774 | { |
775 | output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0); | |
776 | } | |
777 | ||
92846e72 CC |
778 | /* Emit a signed "little-endian base 128" number. */ |
779 | ||
780 | static void | |
781 | out_leb128 (addressT value) | |
782 | { | |
783 | output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1); | |
784 | } | |
785 | ||
220e750f RH |
786 | /* Emit a tuple for .debug_abbrev. */ |
787 | ||
788 | static inline void | |
a2e22468 | 789 | out_abbrev (int name, int form) |
fac0d250 | 790 | { |
220e750f RH |
791 | out_uleb128 (name); |
792 | out_uleb128 (form); | |
793 | } | |
fac0d250 | 794 | |
220e750f | 795 | /* Get the size of a fragment. */ |
fac0d250 | 796 | |
220e750f | 797 | static offsetT |
c9049d30 | 798 | get_frag_fix (fragS *frag, segT seg) |
220e750f RH |
799 | { |
800 | frchainS *fr; | |
801 | ||
802 | if (frag->fr_next) | |
803 | return frag->fr_fix; | |
804 | ||
805 | /* If a fragment is the last in the chain, special measures must be | |
806 | taken to find its size before relaxation, since it may be pending | |
807 | on some subsegment chain. */ | |
c9049d30 | 808 | for (fr = seg_info (seg)->frchainP; fr; fr = fr->frch_next) |
220e750f | 809 | if (fr->frch_last == frag) |
c5c0a210 | 810 | return (char *) obstack_next_free (&fr->frch_obstack) - frag->fr_literal; |
220e750f RH |
811 | |
812 | abort (); | |
813 | } | |
fac0d250 | 814 | |
220e750f | 815 | /* Set an absolute address (may result in a relocation entry). */ |
fac0d250 | 816 | |
220e750f | 817 | static void |
07a53e5c | 818 | out_set_addr (symbolS *sym) |
220e750f | 819 | { |
91d6fa6a | 820 | expressionS exp; |
9e3af0e7 | 821 | |
fac0d250 | 822 | out_opcode (DW_LNS_extended_op); |
220e750f | 823 | out_uleb128 (sizeof_address + 1); |
fac0d250 RH |
824 | |
825 | out_opcode (DW_LNE_set_address); | |
91d6fa6a NC |
826 | exp.X_op = O_symbol; |
827 | exp.X_add_symbol = sym; | |
828 | exp.X_add_number = 0; | |
829 | emit_expr (&exp, sizeof_address); | |
fac0d250 RH |
830 | } |
831 | ||
a3b75434 | 832 | #if DWARF2_LINE_MIN_INSN_LENGTH > 1 |
a2e22468 | 833 | static void scale_addr_delta (addressT *); |
c8970b4b | 834 | |
a3b75434 | 835 | static void |
d7342424 | 836 | scale_addr_delta (addressT *addr_delta) |
a3b75434 DD |
837 | { |
838 | static int printed_this = 0; | |
839 | if (*addr_delta % DWARF2_LINE_MIN_INSN_LENGTH != 0) | |
840 | { | |
841 | if (!printed_this) | |
842 | as_bad("unaligned opcodes detected in executable segment"); | |
843 | printed_this = 1; | |
844 | } | |
845 | *addr_delta /= DWARF2_LINE_MIN_INSN_LENGTH; | |
846 | } | |
847 | #else | |
848 | #define scale_addr_delta(A) | |
849 | #endif | |
850 | ||
220e750f RH |
851 | /* Encode a pair of line and address skips as efficiently as possible. |
852 | Note that the line skip is signed, whereas the address skip is unsigned. | |
353e2c69 | 853 | |
220e750f RH |
854 | The following two routines *must* be kept in sync. This is |
855 | enforced by making emit_inc_line_addr abort if we do not emit | |
856 | exactly the expected number of bytes. */ | |
857 | ||
858 | static int | |
a2e22468 | 859 | size_inc_line_addr (int line_delta, addressT addr_delta) |
fac0d250 | 860 | { |
220e750f RH |
861 | unsigned int tmp, opcode; |
862 | int len = 0; | |
fac0d250 | 863 | |
220e750f | 864 | /* Scale the address delta by the minimum instruction length. */ |
a3b75434 | 865 | scale_addr_delta (&addr_delta); |
220e750f RH |
866 | |
867 | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. | |
868 | We cannot use special opcodes here, since we want the end_sequence | |
869 | to emit the matrix entry. */ | |
870 | if (line_delta == INT_MAX) | |
fac0d250 | 871 | { |
220e750f RH |
872 | if (addr_delta == MAX_SPECIAL_ADDR_DELTA) |
873 | len = 1; | |
fac0d250 | 874 | else |
220e750f RH |
875 | len = 1 + sizeof_leb128 (addr_delta, 0); |
876 | return len + 3; | |
fac0d250 | 877 | } |
fac0d250 | 878 | |
220e750f RH |
879 | /* Bias the line delta by the base. */ |
880 | tmp = line_delta - DWARF2_LINE_BASE; | |
fac0d250 | 881 | |
220e750f RH |
882 | /* If the line increment is out of range of a special opcode, we |
883 | must encode it with DW_LNS_advance_line. */ | |
884 | if (tmp >= DWARF2_LINE_RANGE) | |
885 | { | |
886 | len = 1 + sizeof_leb128 (line_delta, 1); | |
887 | line_delta = 0; | |
888 | tmp = 0 - DWARF2_LINE_BASE; | |
889 | } | |
fac0d250 | 890 | |
220e750f RH |
891 | /* Bias the opcode by the special opcode base. */ |
892 | tmp += DWARF2_LINE_OPCODE_BASE; | |
353e2c69 | 893 | |
220e750f RH |
894 | /* Avoid overflow when addr_delta is large. */ |
895 | if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA) | |
896 | { | |
897 | /* Try using a special opcode. */ | |
898 | opcode = tmp + addr_delta * DWARF2_LINE_RANGE; | |
899 | if (opcode <= 255) | |
900 | return len + 1; | |
901 | ||
902 | /* Try using DW_LNS_const_add_pc followed by special op. */ | |
903 | opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; | |
904 | if (opcode <= 255) | |
905 | return len + 2; | |
906 | } | |
907 | ||
908 | /* Otherwise use DW_LNS_advance_pc. */ | |
909 | len += 1 + sizeof_leb128 (addr_delta, 0); | |
910 | ||
911 | /* DW_LNS_copy or special opcode. */ | |
912 | len += 1; | |
913 | ||
914 | return len; | |
915 | } | |
fac0d250 | 916 | |
220e750f | 917 | static void |
a2e22468 | 918 | emit_inc_line_addr (int line_delta, addressT addr_delta, char *p, int len) |
220e750f RH |
919 | { |
920 | unsigned int tmp, opcode; | |
921 | int need_copy = 0; | |
922 | char *end = p + len; | |
fac0d250 | 923 | |
07a53e5c RH |
924 | /* Line number sequences cannot go backward in addresses. This means |
925 | we've incorrectly ordered the statements in the sequence. */ | |
9c2799c2 | 926 | gas_assert ((offsetT) addr_delta >= 0); |
07a53e5c | 927 | |
220e750f | 928 | /* Scale the address delta by the minimum instruction length. */ |
a3b75434 DD |
929 | scale_addr_delta (&addr_delta); |
930 | ||
220e750f RH |
931 | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. |
932 | We cannot use special opcodes here, since we want the end_sequence | |
933 | to emit the matrix entry. */ | |
934 | if (line_delta == INT_MAX) | |
fac0d250 | 935 | { |
220e750f RH |
936 | if (addr_delta == MAX_SPECIAL_ADDR_DELTA) |
937 | *p++ = DW_LNS_const_add_pc; | |
938 | else | |
fac0d250 | 939 | { |
220e750f RH |
940 | *p++ = DW_LNS_advance_pc; |
941 | p += output_leb128 (p, addr_delta, 0); | |
fac0d250 | 942 | } |
220e750f RH |
943 | |
944 | *p++ = DW_LNS_extended_op; | |
945 | *p++ = 1; | |
946 | *p++ = DW_LNE_end_sequence; | |
947 | goto done; | |
fac0d250 RH |
948 | } |
949 | ||
220e750f RH |
950 | /* Bias the line delta by the base. */ |
951 | tmp = line_delta - DWARF2_LINE_BASE; | |
952 | ||
953 | /* If the line increment is out of range of a special opcode, we | |
954 | must encode it with DW_LNS_advance_line. */ | |
955 | if (tmp >= DWARF2_LINE_RANGE) | |
fac0d250 | 956 | { |
220e750f RH |
957 | *p++ = DW_LNS_advance_line; |
958 | p += output_leb128 (p, line_delta, 1); | |
fac0d250 | 959 | |
220e750f RH |
960 | line_delta = 0; |
961 | tmp = 0 - DWARF2_LINE_BASE; | |
962 | need_copy = 1; | |
963 | } | |
fac0d250 | 964 | |
bd0eb99b RH |
965 | /* Prettier, I think, to use DW_LNS_copy instead of a "line +0, addr +0" |
966 | special opcode. */ | |
967 | if (line_delta == 0 && addr_delta == 0) | |
968 | { | |
969 | *p++ = DW_LNS_copy; | |
970 | goto done; | |
971 | } | |
972 | ||
220e750f RH |
973 | /* Bias the opcode by the special opcode base. */ |
974 | tmp += DWARF2_LINE_OPCODE_BASE; | |
fac0d250 | 975 | |
220e750f RH |
976 | /* Avoid overflow when addr_delta is large. */ |
977 | if (addr_delta < 256 + MAX_SPECIAL_ADDR_DELTA) | |
fac0d250 | 978 | { |
220e750f RH |
979 | /* Try using a special opcode. */ |
980 | opcode = tmp + addr_delta * DWARF2_LINE_RANGE; | |
981 | if (opcode <= 255) | |
982 | { | |
983 | *p++ = opcode; | |
984 | goto done; | |
985 | } | |
986 | ||
987 | /* Try using DW_LNS_const_add_pc followed by special op. */ | |
988 | opcode = tmp + (addr_delta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; | |
989 | if (opcode <= 255) | |
fac0d250 | 990 | { |
220e750f RH |
991 | *p++ = DW_LNS_const_add_pc; |
992 | *p++ = opcode; | |
993 | goto done; | |
fac0d250 RH |
994 | } |
995 | } | |
220e750f RH |
996 | |
997 | /* Otherwise use DW_LNS_advance_pc. */ | |
998 | *p++ = DW_LNS_advance_pc; | |
999 | p += output_leb128 (p, addr_delta, 0); | |
1000 | ||
1001 | if (need_copy) | |
1002 | *p++ = DW_LNS_copy; | |
fac0d250 | 1003 | else |
220e750f | 1004 | *p++ = tmp; |
fac0d250 | 1005 | |
220e750f | 1006 | done: |
9c2799c2 | 1007 | gas_assert (p == end); |
220e750f | 1008 | } |
a8316fe2 | 1009 | |
220e750f | 1010 | /* Handy routine to combine calls to the above two routines. */ |
e1c05f12 | 1011 | |
220e750f | 1012 | static void |
a2e22468 | 1013 | out_inc_line_addr (int line_delta, addressT addr_delta) |
220e750f RH |
1014 | { |
1015 | int len = size_inc_line_addr (line_delta, addr_delta); | |
1016 | emit_inc_line_addr (line_delta, addr_delta, frag_more (len), len); | |
1017 | } | |
9de8d8f1 | 1018 | |
1737851b BW |
1019 | /* Write out an alternative form of line and address skips using |
1020 | DW_LNS_fixed_advance_pc opcodes. This uses more space than the default | |
7ddd14de BW |
1021 | line and address information, but it is required if linker relaxation |
1022 | could change the code offsets. The following two routines *must* be | |
1023 | kept in sync. */ | |
1737851b | 1024 | |
7ddd14de BW |
1025 | static int |
1026 | size_fixed_inc_line_addr (int line_delta, addressT addr_delta) | |
1737851b | 1027 | { |
7ddd14de | 1028 | int len = 0; |
1737851b BW |
1029 | |
1030 | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ | |
7ddd14de BW |
1031 | if (line_delta != INT_MAX) |
1032 | len = 1 + sizeof_leb128 (line_delta, 1); | |
1033 | ||
1034 | if (addr_delta > 50000) | |
1035 | { | |
1036 | /* DW_LNS_extended_op */ | |
1037 | len += 1 + sizeof_leb128 (sizeof_address + 1, 0); | |
1038 | /* DW_LNE_set_address */ | |
1039 | len += 1 + sizeof_address; | |
1040 | } | |
1041 | else | |
1042 | /* DW_LNS_fixed_advance_pc */ | |
1043 | len += 3; | |
1044 | ||
1737851b | 1045 | if (line_delta == INT_MAX) |
7ddd14de BW |
1046 | /* DW_LNS_extended_op + DW_LNE_end_sequence */ |
1047 | len += 3; | |
1048 | else | |
1049 | /* DW_LNS_copy */ | |
1050 | len += 1; | |
1051 | ||
1052 | return len; | |
1053 | } | |
1054 | ||
1055 | static void | |
1056 | emit_fixed_inc_line_addr (int line_delta, addressT addr_delta, fragS *frag, | |
1057 | char *p, int len) | |
1058 | { | |
91d6fa6a | 1059 | expressionS *pexp; |
7ddd14de BW |
1060 | segT line_seg; |
1061 | char *end = p + len; | |
1062 | ||
1063 | /* Line number sequences cannot go backward in addresses. This means | |
1064 | we've incorrectly ordered the statements in the sequence. */ | |
9c2799c2 | 1065 | gas_assert ((offsetT) addr_delta >= 0); |
7ddd14de BW |
1066 | |
1067 | /* INT_MAX is a signal that this is actually a DW_LNE_end_sequence. */ | |
1068 | if (line_delta != INT_MAX) | |
1069 | { | |
1070 | *p++ = DW_LNS_advance_line; | |
1071 | p += output_leb128 (p, line_delta, 1); | |
1072 | } | |
1073 | ||
91d6fa6a | 1074 | pexp = symbol_get_value_expression (frag->fr_symbol); |
7ddd14de BW |
1075 | line_seg = subseg_get (".debug_line", 0); |
1076 | ||
1077 | /* The DW_LNS_fixed_advance_pc opcode has a 2-byte operand so it can | |
1078 | advance the address by at most 64K. Linker relaxation (without | |
1079 | which this function would not be used) could change the operand by | |
1080 | an unknown amount. If the address increment is getting close to | |
1081 | the limit, just reset the address. */ | |
1082 | if (addr_delta > 50000) | |
1737851b | 1083 | { |
7ddd14de | 1084 | symbolS *to_sym; |
91d6fa6a | 1085 | expressionS exp; |
7ddd14de | 1086 | |
9f6db0d3 | 1087 | gas_assert (pexp->X_op == O_subtract); |
91d6fa6a | 1088 | to_sym = pexp->X_add_symbol; |
7ddd14de BW |
1089 | |
1090 | *p++ = DW_LNS_extended_op; | |
1091 | p += output_leb128 (p, sizeof_address + 1, 0); | |
1092 | *p++ = DW_LNE_set_address; | |
91d6fa6a NC |
1093 | exp.X_op = O_symbol; |
1094 | exp.X_add_symbol = to_sym; | |
1095 | exp.X_add_number = 0; | |
7ddd14de | 1096 | subseg_change (line_seg, 0); |
91d6fa6a | 1097 | emit_expr_fix (&exp, sizeof_address, frag, p); |
7ddd14de BW |
1098 | p += sizeof_address; |
1099 | } | |
1100 | else | |
1101 | { | |
1102 | *p++ = DW_LNS_fixed_advance_pc; | |
1103 | subseg_change (line_seg, 0); | |
91d6fa6a | 1104 | emit_expr_fix (pexp, 2, frag, p); |
7ddd14de | 1105 | p += 2; |
1737851b BW |
1106 | } |
1107 | ||
7ddd14de BW |
1108 | if (line_delta == INT_MAX) |
1109 | { | |
1110 | *p++ = DW_LNS_extended_op; | |
1111 | *p++ = 1; | |
1112 | *p++ = DW_LNE_end_sequence; | |
1113 | } | |
1114 | else | |
1115 | *p++ = DW_LNS_copy; | |
1737851b | 1116 | |
9c2799c2 | 1117 | gas_assert (p == end); |
1737851b BW |
1118 | } |
1119 | ||
220e750f RH |
1120 | /* Generate a variant frag that we can use to relax address/line |
1121 | increments between fragments of the target segment. */ | |
9e3af0e7 | 1122 | |
220e750f | 1123 | static void |
07a53e5c | 1124 | relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym) |
220e750f | 1125 | { |
91d6fa6a | 1126 | expressionS exp; |
220e750f | 1127 | int max_chars; |
6576f0b5 | 1128 | |
91d6fa6a NC |
1129 | exp.X_op = O_subtract; |
1130 | exp.X_add_symbol = to_sym; | |
1131 | exp.X_op_symbol = from_sym; | |
1132 | exp.X_add_number = 0; | |
fac0d250 | 1133 | |
220e750f RH |
1134 | /* The maximum size of the frag is the line delta with a maximum |
1135 | sized address delta. */ | |
7ddd14de BW |
1136 | if (DWARF2_USE_FIXED_ADVANCE_PC) |
1137 | max_chars = size_fixed_inc_line_addr (line_delta, | |
1138 | -DWARF2_LINE_MIN_INSN_LENGTH); | |
1139 | else | |
1140 | max_chars = size_inc_line_addr (line_delta, -DWARF2_LINE_MIN_INSN_LENGTH); | |
fac0d250 | 1141 | |
220e750f | 1142 | frag_var (rs_dwarf2dbg, max_chars, max_chars, 1, |
91d6fa6a | 1143 | make_expr_symbol (&exp), line_delta, NULL); |
220e750f | 1144 | } |
fac0d250 | 1145 | |
220e750f RH |
1146 | /* The function estimates the size of a rs_dwarf2dbg variant frag |
1147 | based on the current values of the symbols. It is called before | |
1148 | the relaxation loop. We set fr_subtype to the expected length. */ | |
fac0d250 | 1149 | |
220e750f | 1150 | int |
a2e22468 | 1151 | dwarf2dbg_estimate_size_before_relax (fragS *frag) |
220e750f RH |
1152 | { |
1153 | offsetT addr_delta; | |
1154 | int size; | |
fac0d250 | 1155 | |
6386f3a7 | 1156 | addr_delta = resolve_symbol_value (frag->fr_symbol); |
7ddd14de BW |
1157 | if (DWARF2_USE_FIXED_ADVANCE_PC) |
1158 | size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta); | |
1159 | else | |
1160 | size = size_inc_line_addr (frag->fr_offset, addr_delta); | |
fac0d250 | 1161 | |
220e750f | 1162 | frag->fr_subtype = size; |
fac0d250 | 1163 | |
220e750f RH |
1164 | return size; |
1165 | } | |
1166 | ||
1167 | /* This function relaxes a rs_dwarf2dbg variant frag based on the | |
1168 | current values of the symbols. fr_subtype is the current length | |
1169 | of the frag. This returns the change in frag length. */ | |
1170 | ||
1171 | int | |
a2e22468 | 1172 | dwarf2dbg_relax_frag (fragS *frag) |
220e750f RH |
1173 | { |
1174 | int old_size, new_size; | |
fac0d250 | 1175 | |
220e750f RH |
1176 | old_size = frag->fr_subtype; |
1177 | new_size = dwarf2dbg_estimate_size_before_relax (frag); | |
ee515fb7 | 1178 | |
220e750f | 1179 | return new_size - old_size; |
fac0d250 RH |
1180 | } |
1181 | ||
220e750f RH |
1182 | /* This function converts a rs_dwarf2dbg variant frag into a normal |
1183 | fill frag. This is called after all relaxation has been done. | |
1184 | fr_subtype will be the desired length of the frag. */ | |
1185 | ||
1186 | void | |
a2e22468 | 1187 | dwarf2dbg_convert_frag (fragS *frag) |
fac0d250 | 1188 | { |
220e750f RH |
1189 | offsetT addr_diff; |
1190 | ||
6386f3a7 | 1191 | addr_diff = resolve_symbol_value (frag->fr_symbol); |
fac0d250 | 1192 | |
220e750f RH |
1193 | /* fr_var carries the max_chars that we created the fragment with. |
1194 | fr_subtype carries the current expected length. We must, of | |
1195 | course, have allocated enough memory earlier. */ | |
9c2799c2 | 1196 | gas_assert (frag->fr_var >= (int) frag->fr_subtype); |
fac0d250 | 1197 | |
7ddd14de BW |
1198 | if (DWARF2_USE_FIXED_ADVANCE_PC) |
1199 | emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag, | |
1200 | frag->fr_literal + frag->fr_fix, | |
1201 | frag->fr_subtype); | |
1202 | else | |
1203 | emit_inc_line_addr (frag->fr_offset, addr_diff, | |
1204 | frag->fr_literal + frag->fr_fix, frag->fr_subtype); | |
220e750f RH |
1205 | |
1206 | frag->fr_fix += frag->fr_subtype; | |
1207 | frag->fr_type = rs_fill; | |
1208 | frag->fr_var = 0; | |
1209 | frag->fr_offset = 0; | |
1210 | } | |
1211 | ||
1212 | /* Generate .debug_line content for the chain of line number entries | |
1213 | beginning at E, for segment SEG. */ | |
1214 | ||
1215 | static void | |
a2e22468 | 1216 | process_entries (segT seg, struct line_entry *e) |
220e750f RH |
1217 | { |
1218 | unsigned filenum = 1; | |
1219 | unsigned line = 1; | |
1220 | unsigned column = 0; | |
bd0eb99b RH |
1221 | unsigned isa = 0; |
1222 | unsigned flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; | |
07a53e5c RH |
1223 | fragS *last_frag = NULL, *frag; |
1224 | addressT last_frag_ofs = 0, frag_ofs; | |
fead5cd9 | 1225 | symbolS *last_lab = NULL, *lab; |
220e750f RH |
1226 | struct line_entry *next; |
1227 | ||
fead5cd9 | 1228 | do |
fac0d250 | 1229 | { |
07a53e5c | 1230 | int line_delta; |
220e750f RH |
1231 | |
1232 | if (filenum != e->loc.filenum) | |
fac0d250 | 1233 | { |
220e750f RH |
1234 | filenum = e->loc.filenum; |
1235 | out_opcode (DW_LNS_set_file); | |
1236 | out_uleb128 (filenum); | |
220e750f RH |
1237 | } |
1238 | ||
1239 | if (column != e->loc.column) | |
1240 | { | |
1241 | column = e->loc.column; | |
1242 | out_opcode (DW_LNS_set_column); | |
1243 | out_uleb128 (column); | |
220e750f RH |
1244 | } |
1245 | ||
92846e72 CC |
1246 | if (e->loc.discriminator != 0) |
1247 | { | |
1248 | out_opcode (DW_LNS_extended_op); | |
1249 | out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0)); | |
1250 | out_opcode (DW_LNE_set_discriminator); | |
1251 | out_uleb128 (e->loc.discriminator); | |
1252 | } | |
1253 | ||
bd0eb99b RH |
1254 | if (isa != e->loc.isa) |
1255 | { | |
1256 | isa = e->loc.isa; | |
1257 | out_opcode (DW_LNS_set_isa); | |
1258 | out_uleb128 (isa); | |
bd0eb99b RH |
1259 | } |
1260 | ||
1261 | if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT) | |
220e750f RH |
1262 | { |
1263 | flags = e->loc.flags; | |
1264 | out_opcode (DW_LNS_negate_stmt); | |
220e750f RH |
1265 | } |
1266 | ||
bd0eb99b | 1267 | if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK) |
07a53e5c | 1268 | out_opcode (DW_LNS_set_basic_block); |
220e750f | 1269 | |
bd0eb99b | 1270 | if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END) |
07a53e5c | 1271 | out_opcode (DW_LNS_set_prologue_end); |
bd0eb99b RH |
1272 | |
1273 | if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN) | |
07a53e5c | 1274 | out_opcode (DW_LNS_set_epilogue_begin); |
bd0eb99b | 1275 | |
fb81275c JM |
1276 | /* Don't try to optimize away redundant entries; gdb wants two |
1277 | entries for a function where the code starts on the same line as | |
1278 | the {, and there's no way to identify that case here. Trust gcc | |
1279 | to optimize appropriately. */ | |
07a53e5c RH |
1280 | line_delta = e->loc.line - line; |
1281 | lab = e->label; | |
1282 | frag = symbol_get_frag (lab); | |
1283 | frag_ofs = S_GET_VALUE (lab); | |
220e750f | 1284 | |
07a53e5c | 1285 | if (last_frag == NULL) |
220e750f | 1286 | { |
07a53e5c RH |
1287 | out_set_addr (lab); |
1288 | out_inc_line_addr (line_delta, 0); | |
220e750f | 1289 | } |
7ddd14de | 1290 | else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) |
07a53e5c RH |
1291 | out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs); |
1292 | else | |
1293 | relax_inc_line_addr (line_delta, lab, last_lab); | |
1294 | ||
1295 | line = e->loc.line; | |
1296 | last_lab = lab; | |
1297 | last_frag = frag; | |
1298 | last_frag_ofs = frag_ofs; | |
220e750f RH |
1299 | |
1300 | next = e->next; | |
1301 | free (e); | |
1302 | e = next; | |
fac0d250 | 1303 | } |
fead5cd9 | 1304 | while (e); |
353e2c69 | 1305 | |
220e750f | 1306 | /* Emit a DW_LNE_end_sequence for the end of the section. */ |
07a53e5c | 1307 | frag = last_frag_for_seg (seg); |
c9049d30 | 1308 | frag_ofs = get_frag_fix (frag, seg); |
7ddd14de | 1309 | if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC) |
07a53e5c | 1310 | out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs); |
220e750f | 1311 | else |
07a53e5c RH |
1312 | { |
1313 | lab = symbol_temp_new (seg, frag_ofs, frag); | |
1314 | relax_inc_line_addr (INT_MAX, lab, last_lab); | |
1315 | } | |
fac0d250 RH |
1316 | } |
1317 | ||
220e750f RH |
1318 | /* Emit the directory and file tables for .debug_line. */ |
1319 | ||
fac0d250 | 1320 | static void |
a2e22468 | 1321 | out_file_list (void) |
fac0d250 RH |
1322 | { |
1323 | size_t size; | |
3d6b762c | 1324 | const char *dir; |
fac0d250 | 1325 | char *cp; |
220e750f RH |
1326 | unsigned int i; |
1327 | ||
a7ed1ca2 NC |
1328 | /* Emit directory list. */ |
1329 | for (i = 1; i < dirs_in_use; ++i) | |
1330 | { | |
3d6b762c JM |
1331 | dir = remap_debug_filename (dirs[i]); |
1332 | size = strlen (dir) + 1; | |
a7ed1ca2 | 1333 | cp = frag_more (size); |
3d6b762c | 1334 | memcpy (cp, dir, size); |
a7ed1ca2 NC |
1335 | } |
1336 | /* Terminate it. */ | |
220e750f | 1337 | out_byte ('\0'); |
fac0d250 | 1338 | |
220e750f | 1339 | for (i = 1; i < files_in_use; ++i) |
fac0d250 | 1340 | { |
01e1a5bc NC |
1341 | const char *fullfilename; |
1342 | ||
249e3833 RH |
1343 | if (files[i].filename == NULL) |
1344 | { | |
0e389e77 | 1345 | as_bad (_("unassigned file number %ld"), (long) i); |
88b4ca40 RH |
1346 | /* Prevent a crash later, particularly for file 1. */ |
1347 | files[i].filename = ""; | |
249e3833 RH |
1348 | continue; |
1349 | } | |
1350 | ||
01e1a5bc NC |
1351 | fullfilename = DWARF2_FILE_NAME (files[i].filename, |
1352 | files[i].dir ? dirs [files [i].dir] : ""); | |
1353 | size = strlen (fullfilename) + 1; | |
fac0d250 | 1354 | cp = frag_more (size); |
01e1a5bc | 1355 | memcpy (cp, fullfilename, size); |
fac0d250 | 1356 | |
220e750f | 1357 | out_uleb128 (files[i].dir); /* directory number */ |
01e1a5bc NC |
1358 | /* Output the last modification timestamp. */ |
1359 | out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename, | |
1360 | files[i].dir ? dirs [files [i].dir] : "")); | |
1361 | /* Output the filesize. */ | |
1362 | out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename, | |
1363 | files[i].dir ? dirs [files [i].dir] : "")); | |
fac0d250 | 1364 | } |
353e2c69 KH |
1365 | |
1366 | /* Terminate filename list. */ | |
1367 | out_byte (0); | |
fac0d250 RH |
1368 | } |
1369 | ||
413a266c AM |
1370 | /* Switch to SEC and output a header length field. Return the size of |
1371 | offsets used in SEC. The caller must set EXPR->X_add_symbol value | |
1372 | to the end of the section. */ | |
1373 | ||
1374 | static int | |
91d6fa6a | 1375 | out_header (asection *sec, expressionS *exp) |
413a266c AM |
1376 | { |
1377 | symbolS *start_sym; | |
1378 | symbolS *end_sym; | |
1379 | ||
1380 | subseg_set (sec, 0); | |
1381 | start_sym = symbol_temp_new_now ();; | |
1382 | end_sym = symbol_temp_make (); | |
1383 | ||
1384 | /* Total length of the information. */ | |
91d6fa6a NC |
1385 | exp->X_op = O_subtract; |
1386 | exp->X_add_symbol = end_sym; | |
1387 | exp->X_op_symbol = start_sym; | |
413a266c AM |
1388 | |
1389 | switch (DWARF2_FORMAT (sec)) | |
1390 | { | |
1391 | case dwarf2_format_32bit: | |
91d6fa6a NC |
1392 | exp->X_add_number = -4; |
1393 | emit_expr (exp, 4); | |
413a266c AM |
1394 | return 4; |
1395 | ||
1396 | case dwarf2_format_64bit: | |
91d6fa6a | 1397 | exp->X_add_number = -12; |
413a266c | 1398 | out_four (-1); |
91d6fa6a | 1399 | emit_expr (exp, 8); |
413a266c AM |
1400 | return 8; |
1401 | ||
1402 | case dwarf2_format_64bit_irix: | |
91d6fa6a NC |
1403 | exp->X_add_number = -8; |
1404 | emit_expr (exp, 8); | |
413a266c AM |
1405 | return 8; |
1406 | } | |
1407 | ||
1408 | as_fatal (_("internal error: unknown dwarf2 format")); | |
1409 | return 0; | |
1410 | } | |
1411 | ||
220e750f RH |
1412 | /* Emit the collected .debug_line data. */ |
1413 | ||
1414 | static void | |
a2e22468 | 1415 | out_debug_line (segT line_seg) |
220e750f | 1416 | { |
91d6fa6a | 1417 | expressionS exp; |
220e750f RH |
1418 | symbolS *prologue_end; |
1419 | symbolS *line_end; | |
1420 | struct line_seg *s; | |
14e777e0 | 1421 | int sizeof_offset; |
220e750f | 1422 | |
91d6fa6a NC |
1423 | sizeof_offset = out_header (line_seg, &exp); |
1424 | line_end = exp.X_add_symbol; | |
220e750f RH |
1425 | |
1426 | /* Version. */ | |
fc0eebac | 1427 | out_two (DWARF2_VERSION); |
220e750f RH |
1428 | |
1429 | /* Length of the prologue following this length. */ | |
413a266c | 1430 | prologue_end = symbol_temp_make (); |
91d6fa6a NC |
1431 | exp.X_add_symbol = prologue_end; |
1432 | exp.X_add_number = - (4 + 2 + 4); | |
1433 | emit_expr (&exp, sizeof_offset); | |
220e750f RH |
1434 | |
1435 | /* Parameters of the state machine. */ | |
1436 | out_byte (DWARF2_LINE_MIN_INSN_LENGTH); | |
1437 | out_byte (DWARF2_LINE_DEFAULT_IS_STMT); | |
1438 | out_byte (DWARF2_LINE_BASE); | |
1439 | out_byte (DWARF2_LINE_RANGE); | |
1440 | out_byte (DWARF2_LINE_OPCODE_BASE); | |
1441 | ||
1442 | /* Standard opcode lengths. */ | |
1443 | out_byte (0); /* DW_LNS_copy */ | |
1444 | out_byte (1); /* DW_LNS_advance_pc */ | |
1445 | out_byte (1); /* DW_LNS_advance_line */ | |
1446 | out_byte (1); /* DW_LNS_set_file */ | |
1447 | out_byte (1); /* DW_LNS_set_column */ | |
1448 | out_byte (0); /* DW_LNS_negate_stmt */ | |
1449 | out_byte (0); /* DW_LNS_set_basic_block */ | |
1450 | out_byte (0); /* DW_LNS_const_add_pc */ | |
1451 | out_byte (1); /* DW_LNS_fixed_advance_pc */ | |
bd0eb99b RH |
1452 | out_byte (0); /* DW_LNS_set_prologue_end */ |
1453 | out_byte (0); /* DW_LNS_set_epilogue_begin */ | |
1454 | out_byte (1); /* DW_LNS_set_isa */ | |
220e750f RH |
1455 | |
1456 | out_file_list (); | |
1457 | ||
b7d6ed97 | 1458 | symbol_set_value_now (prologue_end); |
220e750f RH |
1459 | |
1460 | /* For each section, emit a statement program. */ | |
ee515fb7 | 1461 | for (s = all_segs; s; s = s->next) |
220e750f RH |
1462 | process_entries (s->seg, s->head->head); |
1463 | ||
b7d6ed97 | 1464 | symbol_set_value_now (line_end); |
220e750f RH |
1465 | } |
1466 | ||
802f5d9e NC |
1467 | static void |
1468 | out_debug_ranges (segT ranges_seg) | |
1469 | { | |
1470 | unsigned int addr_size = sizeof_address; | |
1471 | struct line_seg *s; | |
91d6fa6a | 1472 | expressionS exp; |
802f5d9e NC |
1473 | unsigned int i; |
1474 | ||
1475 | subseg_set (ranges_seg, 0); | |
1476 | ||
1477 | /* Base Address Entry. */ | |
7fd3924a | 1478 | for (i = 0; i < addr_size; i++) |
802f5d9e | 1479 | out_byte (0xff); |
7fd3924a | 1480 | for (i = 0; i < addr_size; i++) |
802f5d9e NC |
1481 | out_byte (0); |
1482 | ||
1483 | /* Range List Entry. */ | |
1484 | for (s = all_segs; s; s = s->next) | |
1485 | { | |
1486 | fragS *frag; | |
1487 | symbolS *beg, *end; | |
1488 | ||
1489 | frag = first_frag_for_seg (s->seg); | |
1490 | beg = symbol_temp_new (s->seg, 0, frag); | |
1491 | s->text_start = beg; | |
1492 | ||
1493 | frag = last_frag_for_seg (s->seg); | |
1494 | end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag); | |
1495 | s->text_end = end; | |
1496 | ||
91d6fa6a NC |
1497 | exp.X_op = O_symbol; |
1498 | exp.X_add_symbol = beg; | |
1499 | exp.X_add_number = 0; | |
1500 | emit_expr (&exp, addr_size); | |
802f5d9e | 1501 | |
91d6fa6a NC |
1502 | exp.X_op = O_symbol; |
1503 | exp.X_add_symbol = end; | |
1504 | exp.X_add_number = 0; | |
1505 | emit_expr (&exp, addr_size); | |
802f5d9e NC |
1506 | } |
1507 | ||
1508 | /* End of Range Entry. */ | |
7fd3924a | 1509 | for (i = 0; i < addr_size; i++) |
802f5d9e | 1510 | out_byte (0); |
7fd3924a | 1511 | for (i = 0; i < addr_size; i++) |
802f5d9e NC |
1512 | out_byte (0); |
1513 | } | |
1514 | ||
220e750f RH |
1515 | /* Emit data for .debug_aranges. */ |
1516 | ||
58b5739a | 1517 | static void |
a2e22468 | 1518 | out_debug_aranges (segT aranges_seg, segT info_seg) |
fac0d250 | 1519 | { |
220e750f | 1520 | unsigned int addr_size = sizeof_address; |
220e750f | 1521 | struct line_seg *s; |
91d6fa6a | 1522 | expressionS exp; |
413a266c | 1523 | symbolS *aranges_end; |
220e750f | 1524 | char *p; |
413a266c | 1525 | int sizeof_offset; |
fac0d250 | 1526 | |
91d6fa6a NC |
1527 | sizeof_offset = out_header (aranges_seg, &exp); |
1528 | aranges_end = exp.X_add_symbol; | |
fac0d250 | 1529 | |
220e750f | 1530 | /* Version. */ |
fc0eebac | 1531 | out_two (DWARF2_VERSION); |
4dc7ead9 | 1532 | |
220e750f | 1533 | /* Offset to .debug_info. */ |
413a266c | 1534 | TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset); |
220e750f RH |
1535 | |
1536 | /* Size of an address (offset portion). */ | |
1537 | out_byte (addr_size); | |
1538 | ||
1539 | /* Size of a segment descriptor. */ | |
1540 | out_byte (0); | |
1541 | ||
1542 | /* Align the header. */ | |
413a266c | 1543 | frag_align (ffs (2 * addr_size) - 1, 0, 0); |
4dc7ead9 | 1544 | |
ee515fb7 | 1545 | for (s = all_segs; s; s = s->next) |
220e750f RH |
1546 | { |
1547 | fragS *frag; | |
1548 | symbolS *beg, *end; | |
1549 | ||
1550 | frag = first_frag_for_seg (s->seg); | |
b7d6ed97 | 1551 | beg = symbol_temp_new (s->seg, 0, frag); |
220e750f RH |
1552 | s->text_start = beg; |
1553 | ||
1554 | frag = last_frag_for_seg (s->seg); | |
c9049d30 | 1555 | end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag); |
220e750f RH |
1556 | s->text_end = end; |
1557 | ||
91d6fa6a NC |
1558 | exp.X_op = O_symbol; |
1559 | exp.X_add_symbol = beg; | |
1560 | exp.X_add_number = 0; | |
1561 | emit_expr (&exp, addr_size); | |
220e750f | 1562 | |
91d6fa6a NC |
1563 | exp.X_op = O_subtract; |
1564 | exp.X_add_symbol = end; | |
1565 | exp.X_op_symbol = beg; | |
1566 | exp.X_add_number = 0; | |
1567 | emit_expr (&exp, addr_size); | |
220e750f | 1568 | } |
4dc7ead9 | 1569 | |
220e750f RH |
1570 | p = frag_more (2 * addr_size); |
1571 | md_number_to_chars (p, 0, addr_size); | |
1572 | md_number_to_chars (p + addr_size, 0, addr_size); | |
413a266c AM |
1573 | |
1574 | symbol_set_value_now (aranges_end); | |
4dc7ead9 RH |
1575 | } |
1576 | ||
220e750f RH |
1577 | /* Emit data for .debug_abbrev. Note that this must be kept in |
1578 | sync with out_debug_info below. */ | |
fac0d250 | 1579 | |
220e750f | 1580 | static void |
413a266c AM |
1581 | out_debug_abbrev (segT abbrev_seg, |
1582 | segT info_seg ATTRIBUTE_UNUSED, | |
1583 | segT line_seg ATTRIBUTE_UNUSED) | |
220e750f RH |
1584 | { |
1585 | subseg_set (abbrev_seg, 0); | |
fac0d250 | 1586 | |
220e750f RH |
1587 | out_uleb128 (1); |
1588 | out_uleb128 (DW_TAG_compile_unit); | |
1589 | out_byte (DW_CHILDREN_no); | |
413a266c AM |
1590 | if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit) |
1591 | out_abbrev (DW_AT_stmt_list, DW_FORM_data4); | |
1592 | else | |
1593 | out_abbrev (DW_AT_stmt_list, DW_FORM_data8); | |
220e750f | 1594 | if (all_segs->next == NULL) |
4dc7ead9 | 1595 | { |
220e750f RH |
1596 | out_abbrev (DW_AT_low_pc, DW_FORM_addr); |
1597 | out_abbrev (DW_AT_high_pc, DW_FORM_addr); | |
1598 | } | |
802f5d9e NC |
1599 | else |
1600 | { | |
413a266c | 1601 | if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit) |
802f5d9e NC |
1602 | out_abbrev (DW_AT_ranges, DW_FORM_data4); |
1603 | else | |
1604 | out_abbrev (DW_AT_ranges, DW_FORM_data8); | |
1605 | } | |
48b91938 | 1606 | out_abbrev (DW_AT_name, DW_FORM_string); |
220e750f RH |
1607 | out_abbrev (DW_AT_comp_dir, DW_FORM_string); |
1608 | out_abbrev (DW_AT_producer, DW_FORM_string); | |
1609 | out_abbrev (DW_AT_language, DW_FORM_data2); | |
1610 | out_abbrev (0, 0); | |
a987bfc9 RH |
1611 | |
1612 | /* Terminate the abbreviations for this compilation unit. */ | |
1613 | out_byte (0); | |
220e750f | 1614 | } |
4dc7ead9 | 1615 | |
220e750f | 1616 | /* Emit a description of this compilation unit for .debug_info. */ |
4dc7ead9 | 1617 | |
220e750f | 1618 | static void |
802f5d9e | 1619 | out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg) |
220e750f RH |
1620 | { |
1621 | char producer[128]; | |
3d6b762c JM |
1622 | const char *comp_dir; |
1623 | const char *dirname; | |
91d6fa6a | 1624 | expressionS exp; |
220e750f RH |
1625 | symbolS *info_end; |
1626 | char *p; | |
1627 | int len; | |
14e777e0 | 1628 | int sizeof_offset; |
4dc7ead9 | 1629 | |
91d6fa6a NC |
1630 | sizeof_offset = out_header (info_seg, &exp); |
1631 | info_end = exp.X_add_symbol; | |
4dc7ead9 | 1632 | |
220e750f | 1633 | /* DWARF version. */ |
fc0eebac | 1634 | out_two (DWARF2_VERSION); |
4dc7ead9 | 1635 | |
220e750f | 1636 | /* .debug_abbrev offset */ |
6174d9c8 | 1637 | TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset); |
4dc7ead9 | 1638 | |
220e750f RH |
1639 | /* Target address size. */ |
1640 | out_byte (sizeof_address); | |
fac0d250 | 1641 | |
220e750f RH |
1642 | /* DW_TAG_compile_unit DIE abbrev */ |
1643 | out_uleb128 (1); | |
fac0d250 | 1644 | |
220e750f | 1645 | /* DW_AT_stmt_list */ |
413a266c AM |
1646 | TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg), |
1647 | (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit | |
1648 | ? 4 : 8)); | |
fac0d250 | 1649 | |
802f5d9e | 1650 | /* These two attributes are emitted if all of the code is contiguous. */ |
220e750f | 1651 | if (all_segs->next == NULL) |
58b5739a | 1652 | { |
220e750f | 1653 | /* DW_AT_low_pc */ |
91d6fa6a NC |
1654 | exp.X_op = O_symbol; |
1655 | exp.X_add_symbol = all_segs->text_start; | |
1656 | exp.X_add_number = 0; | |
1657 | emit_expr (&exp, sizeof_address); | |
220e750f RH |
1658 | |
1659 | /* DW_AT_high_pc */ | |
91d6fa6a NC |
1660 | exp.X_op = O_symbol; |
1661 | exp.X_add_symbol = all_segs->text_end; | |
1662 | exp.X_add_number = 0; | |
1663 | emit_expr (&exp, sizeof_address); | |
58b5739a | 1664 | } |
802f5d9e NC |
1665 | else |
1666 | { | |
eb1fe072 NC |
1667 | /* This attribute is emitted if the code is disjoint. */ |
1668 | /* DW_AT_ranges. */ | |
1669 | TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset); | |
802f5d9e | 1670 | } |
58b5739a | 1671 | |
48b91938 RH |
1672 | /* DW_AT_name. We don't have the actual file name that was present |
1673 | on the command line, so assume files[1] is the main input file. | |
1674 | We're not supposed to get called unless at least one line number | |
1675 | entry was emitted, so this should always be defined. */ | |
7fd3924a | 1676 | if (files_in_use == 0) |
48b91938 | 1677 | abort (); |
a7ed1ca2 NC |
1678 | if (files[1].dir) |
1679 | { | |
3d6b762c JM |
1680 | dirname = remap_debug_filename (dirs[files[1].dir]); |
1681 | len = strlen (dirname); | |
198f1251 TG |
1682 | #ifdef TE_VMS |
1683 | /* Already has trailing slash. */ | |
1684 | p = frag_more (len); | |
1685 | memcpy (p, dirname, len); | |
1686 | #else | |
a7ed1ca2 | 1687 | p = frag_more (len + 1); |
3d6b762c | 1688 | memcpy (p, dirname, len); |
56487c55 | 1689 | INSERT_DIR_SEPARATOR (p, len); |
198f1251 | 1690 | #endif |
a7ed1ca2 | 1691 | } |
48b91938 RH |
1692 | len = strlen (files[1].filename) + 1; |
1693 | p = frag_more (len); | |
1694 | memcpy (p, files[1].filename, len); | |
1695 | ||
220e750f | 1696 | /* DW_AT_comp_dir */ |
3d6b762c | 1697 | comp_dir = remap_debug_filename (getpwd ()); |
220e750f RH |
1698 | len = strlen (comp_dir) + 1; |
1699 | p = frag_more (len); | |
1700 | memcpy (p, comp_dir, len); | |
fac0d250 | 1701 | |
220e750f RH |
1702 | /* DW_AT_producer */ |
1703 | sprintf (producer, "GNU AS %s", VERSION); | |
1704 | len = strlen (producer) + 1; | |
1705 | p = frag_more (len); | |
1706 | memcpy (p, producer, len); | |
fac0d250 | 1707 | |
220e750f RH |
1708 | /* DW_AT_language. Yes, this is probably not really MIPS, but the |
1709 | dwarf2 draft has no standard code for assembler. */ | |
1710 | out_two (DW_LANG_Mips_Assembler); | |
1711 | ||
b7d6ed97 | 1712 | symbol_set_value_now (info_end); |
fac0d250 RH |
1713 | } |
1714 | ||
1e9cc1c2 NC |
1715 | void |
1716 | dwarf2_init (void) | |
1717 | { | |
1718 | all_segs_hash = hash_new (); | |
1719 | last_seg_ptr = &all_segs; | |
1720 | } | |
1721 | ||
1722 | ||
c6cb92c5 NS |
1723 | /* Finish the dwarf2 debug sections. We emit .debug.line if there |
1724 | were any .file/.loc directives, or --gdwarf2 was given, or if the | |
df1c40a7 L |
1725 | file has a non-empty .debug_info section and an empty .debug_line |
1726 | section. If we emit .debug_line, and the .debug_info section is | |
1727 | empty, we also emit .debug_info, .debug_aranges and .debug_abbrev. | |
1728 | ALL_SEGS will be non-null if there were any .file/.loc directives, | |
1729 | or --gdwarf2 was given and there were any located instructions | |
1730 | emitted. */ | |
c6cb92c5 | 1731 | |
fac0d250 | 1732 | void |
a2e22468 | 1733 | dwarf2_finish (void) |
fac0d250 | 1734 | { |
220e750f RH |
1735 | segT line_seg; |
1736 | struct line_seg *s; | |
c6cb92c5 NS |
1737 | segT info_seg; |
1738 | int emit_other_sections = 0; | |
df1c40a7 | 1739 | int empty_debug_line = 0; |
c6cb92c5 NS |
1740 | |
1741 | info_seg = bfd_get_section_by_name (stdoutput, ".debug_info"); | |
1742 | emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg); | |
fac0d250 | 1743 | |
df1c40a7 L |
1744 | line_seg = bfd_get_section_by_name (stdoutput, ".debug_line"); |
1745 | empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg); | |
1746 | ||
1747 | /* We can't construct a new debug_line section if we already have one. | |
1748 | Give an error. */ | |
1749 | if (all_segs && !empty_debug_line) | |
1750 | as_fatal ("duplicate .debug_line sections"); | |
1751 | ||
1752 | if ((!all_segs && emit_other_sections) | |
1753 | || (!emit_other_sections && !empty_debug_line)) | |
1754 | /* If there is no line information and no non-empty .debug_info | |
1755 | section, or if there is both a non-empty .debug_info and a non-empty | |
1756 | .debug_line, then we do nothing. */ | |
220e750f | 1757 | return; |
fac0d250 | 1758 | |
220e750f | 1759 | /* Calculate the size of an address for the target machine. */ |
9605f328 | 1760 | sizeof_address = DWARF2_ADDR_SIZE (stdoutput); |
fac0d250 | 1761 | |
220e750f RH |
1762 | /* Create and switch to the line number section. */ |
1763 | line_seg = subseg_new (".debug_line", 0); | |
8a7140c3 | 1764 | bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING); |
fac0d250 | 1765 | |
220e750f | 1766 | /* For each subsection, chain the debug entries together. */ |
ee515fb7 | 1767 | for (s = all_segs; s; s = s->next) |
fac0d250 | 1768 | { |
91d6fa6a NC |
1769 | struct line_subseg *lss = s->head; |
1770 | struct line_entry **ptail = lss->ptail; | |
220e750f | 1771 | |
91d6fa6a | 1772 | while ((lss = lss->next) != NULL) |
220e750f | 1773 | { |
91d6fa6a NC |
1774 | *ptail = lss->head; |
1775 | ptail = lss->ptail; | |
220e750f | 1776 | } |
fac0d250 | 1777 | } |
85a39694 | 1778 | |
220e750f | 1779 | out_debug_line (line_seg); |
85a39694 | 1780 | |
c6cb92c5 NS |
1781 | /* If this is assembler generated line info, and there is no |
1782 | debug_info already, we need .debug_info and .debug_abbrev | |
1783 | sections as well. */ | |
1784 | if (emit_other_sections) | |
220e750f RH |
1785 | { |
1786 | segT abbrev_seg; | |
220e750f | 1787 | segT aranges_seg; |
802f5d9e | 1788 | segT ranges_seg; |
4dc7ead9 | 1789 | |
9c2799c2 | 1790 | gas_assert (all_segs); |
7fd3924a | 1791 | |
220e750f RH |
1792 | info_seg = subseg_new (".debug_info", 0); |
1793 | abbrev_seg = subseg_new (".debug_abbrev", 0); | |
1794 | aranges_seg = subseg_new (".debug_aranges", 0); | |
ef99799a | 1795 | |
8a7140c3 NC |
1796 | bfd_set_section_flags (stdoutput, info_seg, |
1797 | SEC_READONLY | SEC_DEBUGGING); | |
1798 | bfd_set_section_flags (stdoutput, abbrev_seg, | |
1799 | SEC_READONLY | SEC_DEBUGGING); | |
1800 | bfd_set_section_flags (stdoutput, aranges_seg, | |
1801 | SEC_READONLY | SEC_DEBUGGING); | |
ef99799a | 1802 | |
ee515fb7 | 1803 | record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1); |
ef99799a | 1804 | |
802f5d9e NC |
1805 | if (all_segs->next == NULL) |
1806 | ranges_seg = NULL; | |
1807 | else | |
1808 | { | |
1809 | ranges_seg = subseg_new (".debug_ranges", 0); | |
7fd3924a | 1810 | bfd_set_section_flags (stdoutput, ranges_seg, |
802f5d9e NC |
1811 | SEC_READONLY | SEC_DEBUGGING); |
1812 | record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1); | |
1813 | out_debug_ranges (ranges_seg); | |
1814 | } | |
1815 | ||
220e750f | 1816 | out_debug_aranges (aranges_seg, info_seg); |
413a266c | 1817 | out_debug_abbrev (abbrev_seg, info_seg, line_seg); |
802f5d9e | 1818 | out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg); |
220e750f | 1819 | } |
85a39694 | 1820 | } |