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