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