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