Commit | Line | Data |
---|---|---|
436d9e46 | 1 | /* listing.c - maintain assembly listings |
4b95cf5c | 2 | Copyright (C) 1991-2014 Free Software Foundation, Inc. |
252b5132 | 3 | |
5a1964ec | 4 | This file is part of GAS, the GNU Assembler. |
252b5132 | 5 | |
5a1964ec NC |
6 | GAS is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
ec2655a6 | 8 | the Free Software Foundation; either version 3, or (at your option) |
5a1964ec | 9 | any later version. |
252b5132 | 10 | |
5a1964ec NC |
11 | GAS is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
5a1964ec NC |
16 | You should have received a copy of the GNU General Public License |
17 | along with GAS; see the file COPYING. If not, write to the Free | |
4b4da160 NC |
18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 | 20 | |
5a1964ec | 21 | /* Contributed by Steve Chamberlain <sac@cygnus.com> |
252b5132 RH |
22 | |
23 | A listing page looks like: | |
24 | ||
25 | LISTING_HEADER sourcefilename pagenumber | |
26 | TITLE LINE | |
27 | SUBTITLE LINE | |
28 | linenumber address data source | |
29 | linenumber address data source | |
30 | linenumber address data source | |
31 | linenumber address data source | |
32 | ||
33 | If not overridden, the listing commands are: | |
34 | ||
35 | .title "stuff" | |
36 | Put "stuff" onto the title line | |
37 | .sbttl "stuff" | |
38 | Put stuff onto the subtitle line | |
39 | ||
40 | If these commands come within 10 lines of the top of the page, they | |
41 | will affect the page they are on, as well as any subsequent page | |
42 | ||
43 | .eject | |
44 | Thow a page | |
45 | .list | |
46 | Increment the enable listing counter | |
47 | .nolist | |
48 | Decrement the enable listing counter | |
49 | ||
50 | .psize Y[,X] | |
51 | Set the paper size to X wide and Y high. Setting a psize Y of | |
52 | zero will suppress form feeds except where demanded by .eject | |
53 | ||
54 | If the counter goes below zero, listing is suppressed. | |
55 | ||
252b5132 RH |
56 | Listings are a maintained by read calling various listing_<foo> |
57 | functions. What happens most is that the macro NO_LISTING is not | |
58 | defined (from the Makefile), then the macro LISTING_NEWLINE expands | |
59 | into a call to listing_newline. The call is done from read.c, every | |
60 | time it sees a newline, and -l is on the command line. | |
61 | ||
62 | The function listing_newline remembers the frag associated with the | |
63 | newline, and creates a new frag - note that this is wasteful, but not | |
64 | a big deal, since listing slows things down a lot anyway. The | |
47eebc20 | 65 | function also remembers when the filename changes. |
252b5132 RH |
66 | |
67 | When all the input has finished, and gas has had a chance to settle | |
68 | down, the listing is output. This is done by running down the list of | |
69 | frag/source file records, and opening the files as needed and printing | |
70 | out the bytes and chars associated with them. | |
71 | ||
72 | The only things which the architecture can change about the listing | |
73 | are defined in these macros: | |
74 | ||
75 | LISTING_HEADER The name of the architecture | |
76 | LISTING_WORD_SIZE The make of the number of bytes in a word, this determines | |
77 | the clumping of the output data. eg a value of | |
78 | 2 makes words look like 1234 5678, whilst 1 | |
79 | would make the same value look like 12 34 56 | |
80 | 78 | |
81 | LISTING_LHS_WIDTH Number of words of above size for the lhs | |
82 | ||
83 | LISTING_LHS_WIDTH_SECOND Number of words for the data on the lhs | |
84 | for the second line | |
85 | ||
47eebc20 | 86 | LISTING_LHS_CONT_LINES Max number of lines to use up for a continuation |
252b5132 | 87 | LISTING_RHS_WIDTH Number of chars from the input file to print |
5a1964ec | 88 | on a line. */ |
252b5132 | 89 | |
252b5132 | 90 | #include "as.h" |
8b6efd89 | 91 | #include "filenames.h" |
29589b0c | 92 | #include "obstack.h" |
3882b010 | 93 | #include "safe-ctype.h" |
252b5132 RH |
94 | #include "input-file.h" |
95 | #include "subsegs.h" | |
83f10cb2 NC |
96 | #include "bfdver.h" |
97 | #include <time.h> | |
752d5da4 | 98 | #include <stdarg.h> |
252b5132 RH |
99 | |
100 | #ifndef NO_LISTING | |
101 | ||
102 | #ifndef LISTING_HEADER | |
103 | #define LISTING_HEADER "GAS LISTING" | |
104 | #endif | |
105 | #ifndef LISTING_WORD_SIZE | |
106 | #define LISTING_WORD_SIZE 4 | |
107 | #endif | |
108 | #ifndef LISTING_LHS_WIDTH | |
224de7a5 | 109 | #define LISTING_LHS_WIDTH ((LISTING_WORD_SIZE) > 4 ? 1 : 4 / (LISTING_WORD_SIZE)) |
252b5132 RH |
110 | #endif |
111 | #ifndef LISTING_LHS_WIDTH_SECOND | |
224de7a5 | 112 | #define LISTING_LHS_WIDTH_SECOND LISTING_LHS_WIDTH |
252b5132 RH |
113 | #endif |
114 | #ifndef LISTING_RHS_WIDTH | |
115 | #define LISTING_RHS_WIDTH 100 | |
116 | #endif | |
117 | #ifndef LISTING_LHS_CONT_LINES | |
118 | #define LISTING_LHS_CONT_LINES 4 | |
119 | #endif | |
83f10cb2 | 120 | #define MAX_DATELEN 30 |
252b5132 | 121 | |
cf39a089 | 122 | /* This structure remembers which .s were used. */ |
5a1964ec NC |
123 | typedef struct file_info_struct |
124 | { | |
252b5132 RH |
125 | struct file_info_struct * next; |
126 | char * filename; | |
127 | long pos; | |
128 | unsigned int linenum; | |
129 | int at_end; | |
ef99799a | 130 | } file_info_type; |
252b5132 | 131 | |
1e9cc1c2 NC |
132 | enum edict_enum |
133 | { | |
134 | EDICT_NONE, | |
135 | EDICT_SBTTL, | |
136 | EDICT_TITLE, | |
137 | EDICT_NOLIST, | |
138 | EDICT_LIST, | |
139 | EDICT_NOLIST_NEXT, | |
140 | EDICT_EJECT | |
141 | }; | |
142 | ||
143 | ||
c71bb85d SB |
144 | struct list_message |
145 | { | |
146 | char *message; | |
147 | struct list_message *next; | |
148 | }; | |
149 | ||
47eebc20 | 150 | /* This structure remembers which line from which file goes into which |
cf39a089 | 151 | frag. */ |
5a1964ec NC |
152 | struct list_info_struct |
153 | { | |
cf39a089 KH |
154 | /* Frag which this line of source is nearest to. */ |
155 | fragS *frag; | |
252b5132 | 156 | |
cf39a089 | 157 | /* The actual line in the source file. */ |
252b5132 | 158 | unsigned int line; |
5a1964ec | 159 | |
252b5132 | 160 | /* Pointer to the file info struct for the file which this line |
cf39a089 KH |
161 | belongs to. */ |
162 | file_info_type *file; | |
252b5132 RH |
163 | |
164 | /* The expanded text of any macro that may have been executing. */ | |
cf39a089 | 165 | char *line_contents; |
252b5132 | 166 | |
cf39a089 KH |
167 | /* Next in list. */ |
168 | struct list_info_struct *next; | |
252b5132 RH |
169 | |
170 | /* Pointer to the file info struct for the high level language | |
cf39a089 KH |
171 | source line that belongs here. */ |
172 | file_info_type *hll_file; | |
5a1964ec | 173 | |
cf39a089 | 174 | /* High level language source line. */ |
252b5132 RH |
175 | unsigned int hll_line; |
176 | ||
c71bb85d SB |
177 | /* Pointers to linked list of messages associated with this line. */ |
178 | struct list_message *messages, *last_message; | |
252b5132 | 179 | |
1e9cc1c2 | 180 | enum edict_enum edict; |
cf39a089 | 181 | char *edict_arg; |
252b5132 RH |
182 | |
183 | /* Nonzero if this line is to be omitted because it contains | |
184 | debugging information. This can become a flags field if we come | |
185 | up with more information to store here. */ | |
186 | int debugging; | |
187 | }; | |
188 | ||
189 | typedef struct list_info_struct list_info_type; | |
190 | ||
252b5132 RH |
191 | int listing_lhs_width = LISTING_LHS_WIDTH; |
192 | int listing_lhs_width_second = LISTING_LHS_WIDTH_SECOND; | |
193 | int listing_lhs_cont_lines = LISTING_LHS_CONT_LINES; | |
194 | int listing_rhs_width = LISTING_RHS_WIDTH; | |
195 | ||
196 | struct list_info_struct * listing_tail; | |
197 | ||
198 | static file_info_type * file_info_head; | |
199 | static file_info_type * last_open_file_info; | |
200 | static FILE * last_open_file; | |
201 | static struct list_info_struct * head; | |
202 | static int paper_width = 200; | |
203 | static int paper_height = 60; | |
204 | ||
205 | extern int listing; | |
206 | ||
207 | /* File to output listings to. */ | |
ef99799a | 208 | static FILE *list_file; |
252b5132 RH |
209 | |
210 | /* This static array is used to keep the text of data to be printed | |
211 | before the start of the line. */ | |
212 | ||
213 | #define MAX_BYTES \ | |
214 | (((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width \ | |
215 | + ((((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second) \ | |
216 | * listing_lhs_cont_lines) \ | |
217 | + 20) | |
218 | ||
cf39a089 | 219 | static char *data_buffer; |
252b5132 RH |
220 | |
221 | /* Prototypes. */ | |
5a1964ec NC |
222 | static void listing_message (const char *, const char *); |
223 | static file_info_type *file_info (const char *); | |
254d758c | 224 | static void new_frag (void); |
5a1964ec NC |
225 | static void listing_page (list_info_type *); |
226 | static unsigned int calc_hex (list_info_type *); | |
227 | static void print_lines (list_info_type *, unsigned int, char *, unsigned int); | |
254d758c | 228 | static void list_symbol_table (void); |
254d758c | 229 | static int debugging_pseudo (list_info_type *, const char *); |
5a1964ec | 230 | static void listing_listing (char *); |
252b5132 | 231 | |
252b5132 | 232 | static void |
254d758c | 233 | listing_message (const char *name, const char *message) |
252b5132 | 234 | { |
252b5132 RH |
235 | if (listing_tail != (list_info_type *) NULL) |
236 | { | |
a735d1cd L |
237 | unsigned int l = strlen (name) + strlen (message) + 1; |
238 | char *n = (char *) xmalloc (l); | |
c71bb85d | 239 | struct list_message *lm = xmalloc (sizeof *lm); |
a735d1cd L |
240 | strcpy (n, name); |
241 | strcat (n, message); | |
c71bb85d SB |
242 | lm->message = n; |
243 | lm->next = NULL; | |
244 | ||
245 | if (listing_tail->last_message) | |
246 | listing_tail->last_message->next = lm; | |
247 | else | |
248 | listing_tail->messages = lm; | |
249 | listing_tail->last_message = lm; | |
252b5132 RH |
250 | } |
251 | } | |
252 | ||
253 | void | |
254d758c | 254 | listing_warning (const char *message) |
252b5132 RH |
255 | { |
256 | listing_message (_("Warning:"), message); | |
257 | } | |
258 | ||
259 | void | |
254d758c | 260 | listing_error (const char *message) |
252b5132 RH |
261 | { |
262 | listing_message (_("Error:"), message); | |
263 | } | |
264 | ||
265 | static file_info_type * | |
254d758c | 266 | file_info (const char *file_name) |
252b5132 | 267 | { |
cf39a089 | 268 | /* Find an entry with this file name. */ |
252b5132 RH |
269 | file_info_type *p = file_info_head; |
270 | ||
271 | while (p != (file_info_type *) NULL) | |
272 | { | |
8b6efd89 | 273 | if (filename_cmp (p->filename, file_name) == 0) |
252b5132 RH |
274 | return p; |
275 | p = p->next; | |
276 | } | |
277 | ||
cf39a089 | 278 | /* Make new entry. */ |
1e9cc1c2 | 279 | p = (file_info_type *) xmalloc (sizeof (file_info_type)); |
252b5132 RH |
280 | p->next = file_info_head; |
281 | file_info_head = p; | |
a2c36061 | 282 | p->filename = xstrdup (file_name); |
252b5132 RH |
283 | p->pos = 0; |
284 | p->linenum = 0; | |
285 | p->at_end = 0; | |
286 | ||
287 | return p; | |
288 | } | |
289 | ||
252b5132 | 290 | static void |
254d758c | 291 | new_frag (void) |
252b5132 | 292 | { |
252b5132 RH |
293 | frag_wane (frag_now); |
294 | frag_new (0); | |
252b5132 RH |
295 | } |
296 | ||
297 | void | |
254d758c | 298 | listing_newline (char *ps) |
252b5132 RH |
299 | { |
300 | char *file; | |
301 | unsigned int line; | |
302 | static unsigned int last_line = 0xffff; | |
303 | static char *last_file = NULL; | |
d3ce72d0 | 304 | list_info_type *new_i = NULL; |
252b5132 RH |
305 | |
306 | if (listing == 0) | |
307 | return; | |
308 | ||
309 | if (now_seg == absolute_section) | |
310 | return; | |
311 | ||
312 | #ifdef OBJ_ELF | |
313 | /* In ELF, anything in a section beginning with .debug or .line is | |
314 | considered to be debugging information. This includes the | |
315 | statement which switches us into the debugging section, which we | |
316 | can only set after we are already in the debugging section. */ | |
317 | if ((listing & LISTING_NODEBUG) != 0 | |
318 | && listing_tail != NULL | |
319 | && ! listing_tail->debugging) | |
320 | { | |
321 | const char *segname; | |
322 | ||
323 | segname = segment_name (now_seg); | |
324 | if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0 | |
325 | || strncmp (segname, ".line", sizeof ".line" - 1) == 0) | |
326 | listing_tail->debugging = 1; | |
327 | } | |
328 | #endif | |
329 | ||
330 | as_where (&file, &line); | |
331 | if (ps == NULL) | |
332 | { | |
cf39a089 | 333 | if (line == last_line |
8b6efd89 | 334 | && !(last_file && file && filename_cmp (file, last_file))) |
252b5132 RH |
335 | return; |
336 | ||
d3ce72d0 | 337 | new_i = (list_info_type *) xmalloc (sizeof (list_info_type)); |
252b5132 RH |
338 | |
339 | /* Detect if we are reading from stdin by examining the file | |
340 | name returned by as_where(). | |
341 | ||
342 | [FIXME: We rely upon the name in the strcmp below being the | |
343 | same as the one used by input_scrub_new_file(), if that is | |
344 | not true, then this code will fail]. | |
345 | ||
cf39a089 KH |
346 | If we are reading from stdin, then we need to save each input |
347 | line here (assuming of course that we actually have a line of | |
348 | input to read), so that it can be displayed in the listing | |
349 | that is produced at the end of the assembly. */ | |
252b5132 RH |
350 | if (strcmp (file, _("{standard input}")) == 0 |
351 | && input_line_pointer != NULL) | |
352 | { | |
cf39a089 | 353 | char *copy; |
252b5132 RH |
354 | int len; |
355 | int seen_quote = 0; | |
d67ffd56 | 356 | int seen_slash = 0; |
252b5132 | 357 | |
f19df8f7 | 358 | for (copy = input_line_pointer; |
cf39a089 | 359 | *copy && (seen_quote |
d67ffd56 | 360 | || is_end_of_line [(unsigned char) *copy] != 1); |
cf39a089 | 361 | copy++) |
d67ffd56 | 362 | { |
4199fe12 AM |
363 | if (seen_slash) |
364 | seen_slash = 0; | |
365 | else if (*copy == '\\') | |
366 | seen_slash = 1; | |
367 | else if (*copy == '"') | |
368 | seen_quote = !seen_quote; | |
d67ffd56 | 369 | } |
252b5132 | 370 | |
f19df8f7 | 371 | len = copy - input_line_pointer + 1; |
252b5132 | 372 | |
1e9cc1c2 | 373 | copy = (char *) xmalloc (len); |
252b5132 RH |
374 | |
375 | if (copy != NULL) | |
376 | { | |
f19df8f7 | 377 | char *src = input_line_pointer; |
cf39a089 KH |
378 | char *dest = copy; |
379 | ||
252b5132 RH |
380 | while (--len) |
381 | { | |
cf39a089 | 382 | unsigned char c = *src++; |
252b5132 RH |
383 | |
384 | /* Omit control characters in the listing. */ | |
3882b010 | 385 | if (!ISCNTRL (c)) |
cf39a089 | 386 | *dest++ = c; |
252b5132 | 387 | } |
cf39a089 | 388 | |
252b5132 RH |
389 | *dest = 0; |
390 | } | |
cf39a089 | 391 | |
d3ce72d0 | 392 | new_i->line_contents = copy; |
252b5132 RH |
393 | } |
394 | else | |
d3ce72d0 | 395 | new_i->line_contents = NULL; |
252b5132 RH |
396 | } |
397 | else | |
398 | { | |
d3ce72d0 NC |
399 | new_i = (list_info_type *) xmalloc (sizeof (list_info_type)); |
400 | new_i->line_contents = ps; | |
252b5132 RH |
401 | } |
402 | ||
403 | last_line = line; | |
404 | last_file = file; | |
cf39a089 | 405 | |
252b5132 RH |
406 | new_frag (); |
407 | ||
408 | if (listing_tail) | |
d3ce72d0 | 409 | listing_tail->next = new_i; |
252b5132 | 410 | else |
d3ce72d0 | 411 | head = new_i; |
cf39a089 | 412 | |
d3ce72d0 | 413 | listing_tail = new_i; |
252b5132 | 414 | |
d3ce72d0 NC |
415 | new_i->frag = frag_now; |
416 | new_i->line = line; | |
417 | new_i->file = file_info (file); | |
418 | new_i->next = (list_info_type *) NULL; | |
c71bb85d SB |
419 | new_i->messages = NULL; |
420 | new_i->last_message = NULL; | |
d3ce72d0 NC |
421 | new_i->edict = EDICT_NONE; |
422 | new_i->hll_file = (file_info_type *) NULL; | |
423 | new_i->hll_line = 0; | |
424 | new_i->debugging = 0; | |
cf39a089 | 425 | |
252b5132 RH |
426 | new_frag (); |
427 | ||
428 | #ifdef OBJ_ELF | |
429 | /* In ELF, anything in a section beginning with .debug or .line is | |
430 | considered to be debugging information. */ | |
431 | if ((listing & LISTING_NODEBUG) != 0) | |
432 | { | |
433 | const char *segname; | |
434 | ||
435 | segname = segment_name (now_seg); | |
436 | if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0 | |
437 | || strncmp (segname, ".line", sizeof ".line" - 1) == 0) | |
d3ce72d0 | 438 | new_i->debugging = 1; |
252b5132 RH |
439 | } |
440 | #endif | |
441 | } | |
442 | ||
443 | /* Attach all current frags to the previous line instead of the | |
444 | current line. This is called by the MIPS backend when it discovers | |
445 | that it needs to add some NOP instructions; the added NOP | |
446 | instructions should go with the instruction that has the delay, not | |
447 | with the new instruction. */ | |
448 | ||
449 | void | |
254d758c | 450 | listing_prev_line (void) |
252b5132 RH |
451 | { |
452 | list_info_type *l; | |
453 | fragS *f; | |
454 | ||
455 | if (head == (list_info_type *) NULL | |
456 | || head == listing_tail) | |
457 | return; | |
458 | ||
459 | new_frag (); | |
460 | ||
461 | for (l = head; l->next != listing_tail; l = l->next) | |
462 | ; | |
463 | ||
464 | for (f = frchain_now->frch_root; f != (fragS *) NULL; f = f->fr_next) | |
465 | if (f->line == listing_tail) | |
466 | f->line = l; | |
467 | ||
468 | listing_tail->frag = frag_now; | |
469 | new_frag (); | |
470 | } | |
471 | ||
cf39a089 KH |
472 | /* This function returns the next source line from the file supplied, |
473 | truncated to size. It appends a fake line to the end of each input | |
752d5da4 | 474 | file to make using the returned buffer simpler. */ |
252b5132 RH |
475 | |
476 | static char * | |
254d758c | 477 | buffer_line (file_info_type *file, char *line, unsigned int size) |
252b5132 RH |
478 | { |
479 | unsigned int count = 0; | |
480 | int c; | |
252b5132 RH |
481 | char *p = line; |
482 | ||
cf39a089 | 483 | /* If we couldn't open the file, return an empty line. */ |
252b5132 RH |
484 | if (file->at_end) |
485 | return ""; | |
486 | ||
487 | /* Check the cache and see if we last used this file. */ | |
488 | if (!last_open_file_info || file != last_open_file_info) | |
489 | { | |
490 | if (last_open_file) | |
491 | { | |
492 | last_open_file_info->pos = ftell (last_open_file); | |
493 | fclose (last_open_file); | |
494 | } | |
495 | ||
7e66d8ac KH |
496 | /* Open the file in the binary mode so that ftell above can |
497 | return a reliable value that we can feed to fseek below. */ | |
252b5132 | 498 | last_open_file_info = file; |
7e66d8ac | 499 | last_open_file = fopen (file->filename, FOPEN_RB); |
252b5132 RH |
500 | if (last_open_file == NULL) |
501 | { | |
502 | file->at_end = 1; | |
503 | return ""; | |
504 | } | |
cf39a089 | 505 | |
252b5132 RH |
506 | /* Seek to where we were last time this file was open. */ |
507 | if (file->pos) | |
cf39a089 | 508 | fseek (last_open_file, file->pos, SEEK_SET); |
252b5132 RH |
509 | } |
510 | ||
cf39a089 KH |
511 | /* Leave room for null. */ |
512 | size -= 1; | |
252b5132 | 513 | |
752d5da4 NC |
514 | c = fgetc (last_open_file); |
515 | ||
7e66d8ac | 516 | while (c != EOF && c != '\n' && c != '\r') |
252b5132 RH |
517 | { |
518 | if (count < size) | |
519 | *p++ = c; | |
520 | count++; | |
521 | ||
522 | c = fgetc (last_open_file); | |
252b5132 | 523 | } |
7e66d8ac KH |
524 | |
525 | /* If '\r' is followed by '\n', swallow that. Likewise, if '\n' | |
526 | is followed by '\r', swallow that as well. */ | |
527 | if (c == '\r' || c == '\n') | |
528 | { | |
529 | int next = fgetc (last_open_file); | |
752d5da4 | 530 | |
7e66d8ac KH |
531 | if ((c == '\r' && next != '\n') |
532 | || (c == '\n' && next != '\r')) | |
533 | ungetc (next, last_open_file); | |
534 | } | |
535 | ||
252b5132 RH |
536 | if (c == EOF) |
537 | { | |
538 | file->at_end = 1; | |
97735a42 AM |
539 | if (count + 2 < size) |
540 | { | |
541 | *p++ = '.'; | |
542 | *p++ = '.'; | |
543 | *p++ = '.'; | |
544 | } | |
252b5132 RH |
545 | } |
546 | file->linenum++; | |
547 | *p++ = 0; | |
548 | return line; | |
549 | } | |
550 | ||
752d5da4 NC |
551 | |
552 | /* This function rewinds the requested file back to the line requested, | |
553 | reads it in again into the buffer provided and then restores the file | |
9b978282 NC |
554 | back to its original location. Returns the buffer pointer upon success |
555 | or an empty string if an error occurs. */ | |
752d5da4 NC |
556 | |
557 | static char * | |
558 | rebuffer_line (file_info_type * file, | |
559 | unsigned int linenum, | |
560 | char * buffer, | |
561 | unsigned int size) | |
562 | { | |
563 | unsigned int count = 0; | |
9b978282 | 564 | unsigned int current_line; |
752d5da4 NC |
565 | char * p = buffer; |
566 | long pos; | |
9b978282 | 567 | long pos2; |
752d5da4 | 568 | int c; |
9b978282 | 569 | bfd_boolean found = FALSE; |
752d5da4 NC |
570 | |
571 | /* Sanity checks. */ | |
9b978282 | 572 | if (file == NULL || buffer == NULL || size <= 1 || file->linenum <= linenum) |
752d5da4 NC |
573 | return ""; |
574 | ||
575 | /* Check the cache and see if we last used this file. */ | |
576 | if (last_open_file_info == NULL || file != last_open_file_info) | |
577 | { | |
578 | if (last_open_file) | |
579 | { | |
580 | last_open_file_info->pos = ftell (last_open_file); | |
581 | fclose (last_open_file); | |
582 | } | |
583 | ||
584 | /* Open the file in the binary mode so that ftell above can | |
585 | return a reliable value that we can feed to fseek below. */ | |
586 | last_open_file_info = file; | |
587 | last_open_file = fopen (file->filename, FOPEN_RB); | |
588 | if (last_open_file == NULL) | |
589 | { | |
590 | file->at_end = 1; | |
591 | return ""; | |
592 | } | |
593 | ||
594 | /* Seek to where we were last time this file was open. */ | |
595 | if (file->pos) | |
596 | fseek (last_open_file, file->pos, SEEK_SET); | |
597 | } | |
598 | ||
599 | /* Remember where we are in the current file. */ | |
9b978282 NC |
600 | pos2 = pos = ftell (last_open_file); |
601 | if (pos < 3) | |
602 | return ""; | |
603 | current_line = file->linenum; | |
604 | ||
605 | /* Leave room for the nul at the end of the buffer. */ | |
606 | size -= 1; | |
607 | buffer[size] = 0; | |
752d5da4 | 608 | |
9b978282 NC |
609 | /* Increment the current line count by one. |
610 | This is to allow for the fact that we are searching for the | |
611 | start of a previous line, but we do this by detecting end-of-line | |
612 | character(s) not start-of-line characters. */ | |
613 | ++ current_line; | |
752d5da4 | 614 | |
9b978282 | 615 | while (pos2 > 0 && ! found) |
752d5da4 | 616 | { |
9b978282 NC |
617 | char * ptr; |
618 | ||
619 | /* Move backwards through the file, looking for earlier lines. */ | |
620 | pos2 = (long) size > pos2 ? 0 : pos2 - size; | |
621 | fseek (last_open_file, pos2, SEEK_SET); | |
622 | ||
623 | /* Our caller has kindly provided us with a buffer, so we use it. */ | |
624 | if (fread (buffer, 1, size, last_open_file) != size) | |
752d5da4 | 625 | { |
9b978282 NC |
626 | as_warn (_("unable to rebuffer file: %s\n"), file->filename); |
627 | return ""; | |
752d5da4 | 628 | } |
752d5da4 | 629 | |
9b978282 | 630 | for (ptr = buffer + size; ptr >= buffer; -- ptr) |
752d5da4 | 631 | { |
9b978282 NC |
632 | if (*ptr == '\n') |
633 | { | |
634 | -- current_line; | |
752d5da4 | 635 | |
9b978282 NC |
636 | if (current_line == linenum) |
637 | { | |
638 | /* We have found the start of the line we seek. */ | |
639 | found = TRUE; | |
640 | ||
641 | /* FIXME: We could skip the read-in-the-line code | |
642 | below if we know that we already have the whole | |
643 | line in the buffer. */ | |
644 | ||
645 | /* Advance pos2 to the newline character we have just located. */ | |
646 | pos2 += (ptr - buffer); | |
647 | ||
648 | /* Skip the newline and, if present, the carriage return. */ | |
649 | if (ptr + 1 == buffer + size) | |
650 | { | |
651 | ++pos2; | |
652 | if (fgetc (last_open_file) == '\r') | |
653 | ++ pos2; | |
654 | } | |
655 | else | |
656 | pos2 += (ptr[1] == '\r' ? 2 : 1); | |
657 | ||
658 | /* Move the file pointer to this location. */ | |
659 | fseek (last_open_file, pos2, SEEK_SET); | |
660 | break; | |
661 | } | |
662 | } | |
752d5da4 NC |
663 | } |
664 | } | |
665 | ||
752d5da4 NC |
666 | /* Read in the line. */ |
667 | c = fgetc (last_open_file); | |
668 | ||
669 | while (c != EOF && c != '\n' && c != '\r') | |
670 | { | |
671 | if (count < size) | |
672 | *p++ = c; | |
673 | count++; | |
674 | ||
675 | c = fgetc (last_open_file); | |
676 | } | |
677 | ||
678 | /* If '\r' is followed by '\n', swallow that. Likewise, if '\n' | |
679 | is followed by '\r', swallow that as well. */ | |
680 | if (c == '\r' || c == '\n') | |
681 | { | |
682 | int next = fgetc (last_open_file); | |
683 | ||
684 | if ((c == '\r' && next != '\n') | |
685 | || (c == '\n' && next != '\r')) | |
686 | ungetc (next, last_open_file); | |
687 | } | |
688 | ||
689 | /* Terminate the line. */ | |
690 | *p++ = 0; | |
691 | ||
692 | /* Reset the file position. */ | |
693 | fseek (last_open_file, pos, SEEK_SET); | |
694 | ||
695 | return buffer; | |
696 | } | |
697 | ||
252b5132 RH |
698 | static const char *fn; |
699 | ||
700 | static unsigned int eject; /* Eject pending */ | |
701 | static unsigned int page; /* Current page number */ | |
cf39a089 KH |
702 | static char *title; /* Current title */ |
703 | static char *subtitle; /* Current subtitle */ | |
704 | static unsigned int on_page; /* Number of lines printed on current page */ | |
252b5132 RH |
705 | |
706 | static void | |
254d758c | 707 | listing_page (list_info_type *list) |
252b5132 RH |
708 | { |
709 | /* Grope around, see if we can see a title or subtitle edict coming up | |
cf39a089 KH |
710 | soon. (we look down 10 lines of the page and see if it's there) */ |
711 | if ((eject || (on_page >= (unsigned int) paper_height)) | |
712 | && paper_height != 0) | |
252b5132 RH |
713 | { |
714 | unsigned int c = 10; | |
715 | int had_title = 0; | |
716 | int had_subtitle = 0; | |
717 | ||
718 | page++; | |
719 | ||
720 | while (c != 0 && list) | |
721 | { | |
722 | if (list->edict == EDICT_SBTTL && !had_subtitle) | |
723 | { | |
724 | had_subtitle = 1; | |
725 | subtitle = list->edict_arg; | |
726 | } | |
727 | if (list->edict == EDICT_TITLE && !had_title) | |
728 | { | |
729 | had_title = 1; | |
730 | title = list->edict_arg; | |
731 | } | |
732 | list = list->next; | |
733 | c--; | |
734 | } | |
735 | ||
252b5132 RH |
736 | if (page > 1) |
737 | { | |
738 | fprintf (list_file, "\f"); | |
739 | } | |
740 | ||
741 | fprintf (list_file, "%s %s \t\t\tpage %d\n", LISTING_HEADER, fn, page); | |
742 | fprintf (list_file, "%s\n", title); | |
743 | fprintf (list_file, "%s\n", subtitle); | |
744 | on_page = 3; | |
745 | eject = 0; | |
746 | } | |
747 | } | |
748 | ||
752d5da4 NC |
749 | /* Print a line into the list_file. Update the line count |
750 | and if necessary start a new page. */ | |
751 | ||
752 | static void | |
753 | emit_line (list_info_type * list, const char * format, ...) | |
754 | { | |
755 | va_list args; | |
756 | ||
757 | va_start (args, format); | |
758 | ||
759 | vfprintf (list_file, format, args); | |
760 | on_page++; | |
761 | listing_page (list); | |
762 | ||
763 | va_end (args); | |
764 | } | |
765 | ||
252b5132 | 766 | static unsigned int |
254d758c | 767 | calc_hex (list_info_type *list) |
252b5132 RH |
768 | { |
769 | int data_buffer_size; | |
770 | list_info_type *first = list; | |
cf39a089 | 771 | unsigned int address = ~(unsigned int) 0; |
252b5132 RH |
772 | fragS *frag; |
773 | fragS *frag_ptr; | |
bea9907b | 774 | unsigned int octet_in_frag; |
252b5132 | 775 | |
cf39a089 | 776 | /* Find first frag which says it belongs to this line. */ |
252b5132 RH |
777 | frag = list->frag; |
778 | while (frag && frag->line != list) | |
779 | frag = frag->fr_next; | |
780 | ||
781 | frag_ptr = frag; | |
782 | ||
783 | data_buffer_size = 0; | |
784 | ||
cf39a089 | 785 | /* Dump all the frags which belong to this line. */ |
252b5132 RH |
786 | while (frag_ptr != (fragS *) NULL && frag_ptr->line == first) |
787 | { | |
cf39a089 | 788 | /* Print as many bytes from the fixed part as is sensible. */ |
bea9907b TW |
789 | octet_in_frag = 0; |
790 | while ((offsetT) octet_in_frag < frag_ptr->fr_fix | |
252b5132 RH |
791 | && data_buffer_size < MAX_BYTES - 3) |
792 | { | |
cf39a089 | 793 | if (address == ~(unsigned int) 0) |
5a1964ec | 794 | address = frag_ptr->fr_address / OCTETS_PER_BYTE; |
252b5132 RH |
795 | |
796 | sprintf (data_buffer + data_buffer_size, | |
797 | "%02X", | |
bea9907b | 798 | (frag_ptr->fr_literal[octet_in_frag]) & 0xff); |
252b5132 | 799 | data_buffer_size += 2; |
bea9907b | 800 | octet_in_frag++; |
252b5132 | 801 | } |
411863a4 KH |
802 | if (frag_ptr->fr_type == rs_fill) |
803 | { | |
804 | unsigned int var_rep_max = octet_in_frag; | |
805 | unsigned int var_rep_idx = octet_in_frag; | |
806 | ||
807 | /* Print as many bytes from the variable part as is sensible. */ | |
808 | while (((offsetT) octet_in_frag | |
809 | < (frag_ptr->fr_fix + frag_ptr->fr_var * frag_ptr->fr_offset)) | |
810 | && data_buffer_size < MAX_BYTES - 3) | |
811 | { | |
812 | if (address == ~(unsigned int) 0) | |
5a1964ec NC |
813 | address = frag_ptr->fr_address / OCTETS_PER_BYTE; |
814 | ||
411863a4 KH |
815 | sprintf (data_buffer + data_buffer_size, |
816 | "%02X", | |
817 | (frag_ptr->fr_literal[var_rep_idx]) & 0xff); | |
411863a4 | 818 | data_buffer_size += 2; |
252b5132 | 819 | |
411863a4 KH |
820 | var_rep_idx++; |
821 | octet_in_frag++; | |
252b5132 | 822 | |
411863a4 KH |
823 | if ((offsetT) var_rep_idx >= frag_ptr->fr_fix + frag_ptr->fr_var) |
824 | var_rep_idx = var_rep_max; | |
825 | } | |
826 | } | |
252b5132 RH |
827 | |
828 | frag_ptr = frag_ptr->fr_next; | |
829 | } | |
830 | data_buffer[data_buffer_size] = '\0'; | |
831 | return address; | |
832 | } | |
833 | ||
252b5132 | 834 | static void |
254d758c KH |
835 | print_lines (list_info_type *list, unsigned int lineno, |
836 | char *string, unsigned int address) | |
252b5132 RH |
837 | { |
838 | unsigned int idx; | |
839 | unsigned int nchars; | |
840 | unsigned int lines; | |
bea9907b | 841 | unsigned int octet_in_word = 0; |
252b5132 | 842 | char *src = data_buffer; |
bea9907b | 843 | int cur; |
c71bb85d | 844 | struct list_message *msg; |
252b5132 | 845 | |
cf39a089 | 846 | /* Print the stuff on the first line. */ |
252b5132 RH |
847 | listing_page (list); |
848 | nchars = (LISTING_WORD_SIZE * 2 + 1) * listing_lhs_width; | |
cf39a089 KH |
849 | |
850 | /* Print the hex for the first line. */ | |
851 | if (address == ~(unsigned int) 0) | |
252b5132 RH |
852 | { |
853 | fprintf (list_file, "% 4d ", lineno); | |
854 | for (idx = 0; idx < nchars; idx++) | |
855 | fprintf (list_file, " "); | |
856 | ||
752d5da4 | 857 | emit_line (NULL, "\t%s\n", string ? string : ""); |
252b5132 RH |
858 | return; |
859 | } | |
860 | ||
861 | if (had_errors ()) | |
862 | fprintf (list_file, "% 4d ???? ", lineno); | |
863 | else | |
864 | fprintf (list_file, "% 4d %04x ", lineno, address); | |
865 | ||
cf39a089 | 866 | /* And the data to go along with it. */ |
252b5132 | 867 | idx = 0; |
bea9907b TW |
868 | cur = 0; |
869 | while (src[cur] && idx < nchars) | |
252b5132 | 870 | { |
bea9907b | 871 | int offset; |
bea9907b | 872 | offset = cur; |
cf39a089 | 873 | fprintf (list_file, "%c%c", src[offset], src[offset + 1]); |
bea9907b TW |
874 | cur += 2; |
875 | octet_in_word++; | |
cf39a089 | 876 | |
bea9907b | 877 | if (octet_in_word == LISTING_WORD_SIZE) |
252b5132 RH |
878 | { |
879 | fprintf (list_file, " "); | |
880 | idx++; | |
bea9907b | 881 | octet_in_word = 0; |
252b5132 | 882 | } |
cf39a089 | 883 | |
252b5132 RH |
884 | idx += 2; |
885 | } | |
cf39a089 | 886 | |
252b5132 RH |
887 | for (; idx < nchars; idx++) |
888 | fprintf (list_file, " "); | |
cf39a089 | 889 | |
752d5da4 | 890 | emit_line (list, "\t%s\n", string ? string : ""); |
cf39a089 | 891 | |
c71bb85d SB |
892 | for (msg = list->messages; msg; msg = msg->next) |
893 | emit_line (list, "**** %s\n", msg->message); | |
cf39a089 | 894 | |
252b5132 RH |
895 | for (lines = 0; |
896 | lines < (unsigned int) listing_lhs_cont_lines | |
bea9907b | 897 | && src[cur]; |
cf39a089 | 898 | lines++) |
252b5132 | 899 | { |
cf39a089 | 900 | nchars = ((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second - 1; |
252b5132 | 901 | idx = 0; |
cf39a089 KH |
902 | |
903 | /* Print any more lines of data, but more compactly. */ | |
252b5132 | 904 | fprintf (list_file, "% 4d ", lineno); |
cf39a089 | 905 | |
bea9907b | 906 | while (src[cur] && idx < nchars) |
252b5132 | 907 | { |
cf39a089 KH |
908 | int offset; |
909 | offset = cur; | |
910 | fprintf (list_file, "%c%c", src[offset], src[offset + 1]); | |
bea9907b | 911 | cur += 2; |
252b5132 | 912 | idx += 2; |
bea9907b | 913 | octet_in_word++; |
cf39a089 | 914 | |
bea9907b | 915 | if (octet_in_word == LISTING_WORD_SIZE) |
252b5132 RH |
916 | { |
917 | fprintf (list_file, " "); | |
918 | idx++; | |
bea9907b | 919 | octet_in_word = 0; |
252b5132 RH |
920 | } |
921 | } | |
cf39a089 | 922 | |
752d5da4 | 923 | emit_line (list, "\n"); |
252b5132 RH |
924 | } |
925 | } | |
926 | ||
252b5132 | 927 | static void |
254d758c | 928 | list_symbol_table (void) |
252b5132 RH |
929 | { |
930 | extern symbolS *symbol_rootP; | |
931 | int got_some = 0; | |
932 | ||
933 | symbolS *ptr; | |
934 | eject = 1; | |
752d5da4 | 935 | listing_page (NULL); |
252b5132 RH |
936 | |
937 | for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr)) | |
938 | { | |
939 | if (SEG_NORMAL (S_GET_SEGMENT (ptr)) | |
940 | || S_GET_SEGMENT (ptr) == absolute_section) | |
941 | { | |
252b5132 | 942 | /* Don't report section symbols. They are not interesting. */ |
49309057 | 943 | if (symbol_section_p (ptr)) |
252b5132 | 944 | continue; |
7be1c489 | 945 | |
252b5132 RH |
946 | if (S_GET_NAME (ptr)) |
947 | { | |
948 | char buf[30], fmt[8]; | |
949 | valueT val = S_GET_VALUE (ptr); | |
950 | ||
951 | /* @@ Note that this is dependent on the compilation options, | |
952 | not solely on the target characteristics. */ | |
953 | if (sizeof (val) == 4 && sizeof (int) == 4) | |
954 | sprintf (buf, "%08lx", (unsigned long) val); | |
955 | else if (sizeof (val) <= sizeof (unsigned long)) | |
956 | { | |
957 | sprintf (fmt, "%%0%lulx", | |
958 | (unsigned long) (sizeof (val) * 2)); | |
959 | sprintf (buf, fmt, (unsigned long) val); | |
960 | } | |
961 | #if defined (BFD64) | |
962 | else if (sizeof (val) > 4) | |
963 | sprintf_vma (buf, val); | |
964 | #endif | |
965 | else | |
966 | abort (); | |
967 | ||
968 | if (!got_some) | |
969 | { | |
970 | fprintf (list_file, "DEFINED SYMBOLS\n"); | |
971 | on_page++; | |
972 | got_some = 1; | |
973 | } | |
974 | ||
49309057 | 975 | if (symbol_get_frag (ptr) && symbol_get_frag (ptr)->line) |
252b5132 RH |
976 | { |
977 | fprintf (list_file, "%20s:%-5d %s:%s %s\n", | |
49309057 ILT |
978 | symbol_get_frag (ptr)->line->file->filename, |
979 | symbol_get_frag (ptr)->line->line, | |
252b5132 RH |
980 | segment_name (S_GET_SEGMENT (ptr)), |
981 | buf, S_GET_NAME (ptr)); | |
982 | } | |
983 | else | |
984 | { | |
985 | fprintf (list_file, "%33s:%s %s\n", | |
986 | segment_name (S_GET_SEGMENT (ptr)), | |
987 | buf, S_GET_NAME (ptr)); | |
988 | } | |
989 | ||
cf39a089 | 990 | on_page++; |
752d5da4 | 991 | listing_page (NULL); |
252b5132 RH |
992 | } |
993 | } | |
994 | ||
995 | } | |
996 | if (!got_some) | |
997 | { | |
998 | fprintf (list_file, "NO DEFINED SYMBOLS\n"); | |
999 | on_page++; | |
1000 | } | |
752d5da4 | 1001 | emit_line (NULL, "\n"); |
252b5132 RH |
1002 | |
1003 | got_some = 0; | |
1004 | ||
1005 | for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr)) | |
1006 | { | |
1007 | if (S_GET_NAME (ptr) && strlen (S_GET_NAME (ptr)) != 0) | |
1008 | { | |
1009 | if (S_GET_SEGMENT (ptr) == undefined_section) | |
1010 | { | |
1011 | if (!got_some) | |
1012 | { | |
1013 | got_some = 1; | |
752d5da4 NC |
1014 | |
1015 | emit_line (NULL, "UNDEFINED SYMBOLS\n"); | |
252b5132 | 1016 | } |
752d5da4 NC |
1017 | |
1018 | emit_line (NULL, "%s\n", S_GET_NAME (ptr)); | |
252b5132 RH |
1019 | } |
1020 | } | |
1021 | } | |
752d5da4 | 1022 | |
252b5132 | 1023 | if (!got_some) |
752d5da4 | 1024 | emit_line (NULL, "NO UNDEFINED SYMBOLS\n"); |
252b5132 RH |
1025 | } |
1026 | ||
752d5da4 NC |
1027 | typedef struct cached_line |
1028 | { | |
1029 | file_info_type * file; | |
1030 | unsigned int line; | |
1031 | char buffer [LISTING_RHS_WIDTH]; | |
1032 | } cached_line; | |
1033 | ||
252b5132 | 1034 | static void |
752d5da4 NC |
1035 | print_source (file_info_type * current_file, |
1036 | list_info_type * list, | |
1037 | unsigned int width) | |
252b5132 | 1038 | { |
752d5da4 NC |
1039 | #define NUM_CACHE_LINES 3 |
1040 | static cached_line cached_lines[NUM_CACHE_LINES]; | |
1041 | static int next_free_line = 0; | |
3726e6c5 | 1042 | cached_line * cache = NULL; |
752d5da4 NC |
1043 | |
1044 | if (current_file->linenum > list->hll_line | |
1045 | && list->hll_line > 0) | |
1046 | { | |
1047 | /* This can happen with modern optimizing compilers. The source | |
1048 | lines from the high level language input program are split up | |
1049 | and interleaved, meaning the line number we want to display | |
1050 | (list->hll_line) can have already been displayed. We have | |
1051 | three choices: | |
1052 | ||
1053 | a. Do nothing, since we have already displayed the source | |
1054 | line. This was the old behaviour. | |
1055 | ||
1056 | b. Display the particular line requested again, but only | |
1057 | that line. This is the new behaviour. | |
1058 | ||
1059 | c. Display the particular line requested again and reset | |
1060 | the current_file->line_num value so that we redisplay | |
1061 | all the following lines as well the next time we | |
1062 | encounter a larger line number. */ | |
1063 | int i; | |
1064 | ||
1065 | /* Check the cache, maybe we already have the line saved. */ | |
1066 | for (i = 0; i < NUM_CACHE_LINES; i++) | |
1067 | if (cached_lines[i].file == current_file | |
1068 | && cached_lines[i].line == list->hll_line) | |
1069 | { | |
1070 | cache = cached_lines + i; | |
1071 | break; | |
1072 | } | |
1073 | ||
1074 | if (i == NUM_CACHE_LINES) | |
1075 | { | |
1076 | cache = cached_lines + next_free_line; | |
1077 | next_free_line ++; | |
1078 | if (next_free_line == NUM_CACHE_LINES) | |
1079 | next_free_line = 0; | |
1080 | ||
1081 | cache->file = current_file; | |
1082 | cache->line = list->hll_line; | |
1083 | cache->buffer[0] = 0; | |
1084 | rebuffer_line (current_file, cache->line, cache->buffer, width); | |
1085 | } | |
1086 | ||
1087 | emit_line (list, "%4u:%-13s **** %s\n", | |
1088 | cache->line, cache->file->filename, cache->buffer); | |
1089 | return; | |
1090 | } | |
1091 | ||
252b5132 RH |
1092 | if (!current_file->at_end) |
1093 | { | |
752d5da4 NC |
1094 | int num_lines_shown = 0; |
1095 | ||
252b5132 RH |
1096 | while (current_file->linenum < list->hll_line |
1097 | && !current_file->at_end) | |
1098 | { | |
752d5da4 NC |
1099 | char *p; |
1100 | ||
91d6fa6a | 1101 | cache = cached_lines + next_free_line; |
752d5da4 | 1102 | cache->file = current_file; |
cc761f75 | 1103 | cache->line = current_file->linenum + 1; |
752d5da4 NC |
1104 | cache->buffer[0] = 0; |
1105 | p = buffer_line (current_file, cache->buffer, width); | |
1106 | ||
1107 | /* Cache optimization: If printing a group of lines | |
1108 | cache the first and last lines in the group. */ | |
1109 | if (num_lines_shown == 0) | |
1110 | { | |
1111 | next_free_line ++; | |
1112 | if (next_free_line == NUM_CACHE_LINES) | |
1113 | next_free_line = 0; | |
1114 | } | |
5a1964ec | 1115 | |
752d5da4 NC |
1116 | emit_line (list, "%4u:%-13s **** %s\n", |
1117 | cache->line, cache->file->filename, p); | |
1118 | num_lines_shown ++; | |
252b5132 RH |
1119 | } |
1120 | } | |
1121 | } | |
1122 | ||
1123 | /* Sometimes the user doesn't want to be bothered by the debugging | |
1124 | records inserted by the compiler, see if the line is suspicious. */ | |
1125 | ||
1126 | static int | |
254d758c | 1127 | debugging_pseudo (list_info_type *list, const char *line) |
252b5132 | 1128 | { |
87975d2a | 1129 | #ifdef OBJ_ELF |
252b5132 RH |
1130 | static int in_debug; |
1131 | int was_debug; | |
87975d2a | 1132 | #endif |
252b5132 RH |
1133 | |
1134 | if (list->debugging) | |
1135 | { | |
87975d2a | 1136 | #ifdef OBJ_ELF |
252b5132 | 1137 | in_debug = 1; |
87975d2a | 1138 | #endif |
252b5132 RH |
1139 | return 1; |
1140 | } | |
87975d2a | 1141 | #ifdef OBJ_ELF |
252b5132 RH |
1142 | was_debug = in_debug; |
1143 | in_debug = 0; | |
87975d2a | 1144 | #endif |
252b5132 | 1145 | |
3882b010 | 1146 | while (ISSPACE (*line)) |
252b5132 RH |
1147 | line++; |
1148 | ||
1149 | if (*line != '.') | |
1150 | { | |
1151 | #ifdef OBJ_ELF | |
1152 | /* The ELF compiler sometimes emits blank lines after switching | |
1153 | out of a debugging section. If the next line drops us back | |
1154 | into debugging information, then don't print the blank line. | |
1155 | This is a hack for a particular compiler behaviour, not a | |
1156 | general case. */ | |
1157 | if (was_debug | |
1158 | && *line == '\0' | |
1159 | && list->next != NULL | |
1160 | && list->next->debugging) | |
1161 | { | |
1162 | in_debug = 1; | |
1163 | return 1; | |
1164 | } | |
1165 | #endif | |
1166 | ||
1167 | return 0; | |
1168 | } | |
1169 | ||
1170 | line++; | |
1171 | ||
1172 | if (strncmp (line, "def", 3) == 0) | |
1173 | return 1; | |
1174 | if (strncmp (line, "val", 3) == 0) | |
1175 | return 1; | |
1176 | if (strncmp (line, "scl", 3) == 0) | |
1177 | return 1; | |
1178 | if (strncmp (line, "line", 4) == 0) | |
1179 | return 1; | |
1180 | if (strncmp (line, "endef", 5) == 0) | |
1181 | return 1; | |
1182 | if (strncmp (line, "ln", 2) == 0) | |
1183 | return 1; | |
1184 | if (strncmp (line, "type", 4) == 0) | |
1185 | return 1; | |
1186 | if (strncmp (line, "size", 4) == 0) | |
1187 | return 1; | |
1188 | if (strncmp (line, "dim", 3) == 0) | |
1189 | return 1; | |
1190 | if (strncmp (line, "tag", 3) == 0) | |
1191 | return 1; | |
252b5132 RH |
1192 | if (strncmp (line, "stabs", 5) == 0) |
1193 | return 1; | |
1194 | if (strncmp (line, "stabn", 5) == 0) | |
1195 | return 1; | |
1196 | ||
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | static void | |
254d758c | 1201 | listing_listing (char *name ATTRIBUTE_UNUSED) |
252b5132 RH |
1202 | { |
1203 | list_info_type *list = head; | |
1204 | file_info_type *current_hll_file = (file_info_type *) NULL; | |
252b5132 RH |
1205 | char *buffer; |
1206 | char *p; | |
1207 | int show_listing = 1; | |
1208 | unsigned int width; | |
1209 | ||
1e9cc1c2 NC |
1210 | buffer = (char *) xmalloc (listing_rhs_width); |
1211 | data_buffer = (char *) xmalloc (MAX_BYTES); | |
252b5132 | 1212 | eject = 1; |
252b5132 RH |
1213 | list = head->next; |
1214 | ||
252b5132 RH |
1215 | while (list) |
1216 | { | |
ab9da554 | 1217 | unsigned int list_line; |
252b5132 RH |
1218 | |
1219 | width = listing_rhs_width > paper_width ? paper_width : | |
1220 | listing_rhs_width; | |
1221 | ||
1222 | list_line = list->line; | |
1223 | switch (list->edict) | |
1224 | { | |
1225 | case EDICT_LIST: | |
1226 | /* Skip all lines up to the current. */ | |
1227 | list_line--; | |
1228 | break; | |
1229 | case EDICT_NOLIST: | |
1230 | show_listing--; | |
1231 | break; | |
1232 | case EDICT_NOLIST_NEXT: | |
61b96bb4 AM |
1233 | if (show_listing == 0) |
1234 | list_line--; | |
252b5132 RH |
1235 | break; |
1236 | case EDICT_EJECT: | |
1237 | break; | |
1238 | case EDICT_NONE: | |
1239 | break; | |
1240 | case EDICT_TITLE: | |
1241 | title = list->edict_arg; | |
1242 | break; | |
1243 | case EDICT_SBTTL: | |
1244 | subtitle = list->edict_arg; | |
1245 | break; | |
1246 | default: | |
1247 | abort (); | |
1248 | } | |
1249 | ||
1250 | if (show_listing <= 0) | |
1251 | { | |
1252 | while (list->file->linenum < list_line | |
1253 | && !list->file->at_end) | |
1254 | p = buffer_line (list->file, buffer, width); | |
1255 | } | |
1256 | ||
61b96bb4 AM |
1257 | if (list->edict == EDICT_LIST |
1258 | || (list->edict == EDICT_NOLIST_NEXT && show_listing == 0)) | |
252b5132 RH |
1259 | { |
1260 | /* Enable listing for the single line that caused the enable. */ | |
1261 | list_line++; | |
1262 | show_listing++; | |
1263 | } | |
1264 | ||
1265 | if (show_listing > 0) | |
1266 | { | |
1267 | /* Scan down the list and print all the stuff which can be done | |
1268 | with this line (or lines). */ | |
252b5132 | 1269 | if (list->hll_file) |
5a1964ec | 1270 | current_hll_file = list->hll_file; |
252b5132 RH |
1271 | |
1272 | if (current_hll_file && list->hll_line && (listing & LISTING_HLL)) | |
752d5da4 | 1273 | print_source (current_hll_file, list, width); |
252b5132 RH |
1274 | |
1275 | if (list->line_contents) | |
1276 | { | |
1277 | if (!((listing & LISTING_NODEBUG) | |
1278 | && debugging_pseudo (list, list->line_contents))) | |
5a1964ec NC |
1279 | print_lines (list, |
1280 | list->file->linenum == 0 ? list->line : list->file->linenum, | |
1281 | list->line_contents, calc_hex (list)); | |
1282 | ||
252b5132 RH |
1283 | free (list->line_contents); |
1284 | list->line_contents = NULL; | |
1285 | } | |
1286 | else | |
1287 | { | |
1288 | while (list->file->linenum < list_line | |
1289 | && !list->file->at_end) | |
1290 | { | |
1291 | unsigned int address; | |
1292 | ||
1293 | p = buffer_line (list->file, buffer, width); | |
1294 | ||
1295 | if (list->file->linenum < list_line) | |
cf39a089 | 1296 | address = ~(unsigned int) 0; |
252b5132 RH |
1297 | else |
1298 | address = calc_hex (list); | |
1299 | ||
1300 | if (!((listing & LISTING_NODEBUG) | |
1301 | && debugging_pseudo (list, p))) | |
1302 | print_lines (list, list->file->linenum, p, address); | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | if (list->edict == EDICT_EJECT) | |
5a1964ec | 1307 | eject = 1; |
252b5132 RH |
1308 | } |
1309 | ||
61b96bb4 | 1310 | if (list->edict == EDICT_NOLIST_NEXT && show_listing == 1) |
252b5132 RH |
1311 | --show_listing; |
1312 | ||
1313 | list = list->next; | |
1314 | } | |
1315 | ||
1316 | free (buffer); | |
1317 | free (data_buffer); | |
1318 | data_buffer = NULL; | |
1319 | } | |
1320 | ||
83f10cb2 NC |
1321 | /* Print time stamp in ISO format: yyyy-mm-ddThh:mm:ss.ss+/-zzzz. */ |
1322 | ||
1323 | static void | |
1324 | print_timestamp (void) | |
1325 | { | |
1326 | const time_t now = time (NULL); | |
d5a35a55 | 1327 | struct tm * timestamp; |
83f10cb2 NC |
1328 | char stampstr[MAX_DATELEN]; |
1329 | ||
1330 | /* Any portable way to obtain subsecond values??? */ | |
d5a35a55 NC |
1331 | timestamp = localtime (&now); |
1332 | strftime (stampstr, MAX_DATELEN, "%Y-%m-%dT%H:%M:%S.000%z", timestamp); | |
83f10cb2 NC |
1333 | fprintf (list_file, _("\n time stamp \t: %s\n\n"), stampstr); |
1334 | } | |
1335 | ||
1336 | static void | |
1337 | print_single_option (char * opt, int *pos) | |
1338 | { | |
1339 | int opt_len = strlen (opt); | |
1340 | ||
1341 | if ((*pos + opt_len) < paper_width) | |
1342 | { | |
1343 | fprintf (list_file, _("%s "), opt); | |
1344 | *pos = *pos + opt_len; | |
1345 | } | |
1346 | else | |
1347 | { | |
1348 | fprintf (list_file, _("\n\t%s "), opt); | |
1349 | *pos = opt_len; | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | /* Print options passed to as. */ | |
1354 | ||
1355 | static void | |
1356 | print_options (char ** argv) | |
1357 | { | |
1358 | const char *field_name = _("\n options passed\t: "); | |
1359 | int pos = strlen (field_name); | |
1360 | char **p; | |
1361 | ||
ead47374 | 1362 | fputs (field_name, list_file); |
83f10cb2 NC |
1363 | for (p = &argv[1]; *p != NULL; p++) |
1364 | if (**p == '-') | |
1365 | { | |
1366 | /* Ignore these. */ | |
1367 | if (strcmp (*p, "-o") == 0) | |
1368 | { | |
1369 | if (p[1] != NULL) | |
1370 | p++; | |
1371 | continue; | |
1372 | } | |
1373 | if (strcmp (*p, "-v") == 0) | |
1374 | continue; | |
1375 | ||
1376 | print_single_option (*p, &pos); | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | /* Print a first section with basic info like file names, as version, | |
1381 | options passed, target, and timestamp. | |
1382 | The format of this section is as follows: | |
1383 | ||
1384 | AS VERSION | |
1385 | ||
1386 | fieldname TAB ':' fieldcontents | |
1387 | { TAB fieldcontents-cont } */ | |
1388 | ||
1389 | static void | |
1390 | listing_general_info (char ** argv) | |
1391 | { | |
1392 | /* Print the stuff on the first line. */ | |
1393 | eject = 1; | |
752d5da4 | 1394 | listing_page (NULL); |
83f10cb2 NC |
1395 | |
1396 | fprintf (list_file, | |
1397 | _(" GNU assembler version %s (%s)\n\t using BFD version %s."), | |
1398 | VERSION, TARGET_ALIAS, BFD_VERSION_STRING); | |
1399 | print_options (argv); | |
1400 | fprintf (list_file, _("\n input file \t: %s"), fn); | |
1401 | fprintf (list_file, _("\n output file \t: %s"), out_file_name); | |
1402 | fprintf (list_file, _("\n target \t: %s"), TARGET_CANONICAL); | |
1403 | print_timestamp (); | |
1404 | } | |
1405 | ||
252b5132 | 1406 | void |
83f10cb2 | 1407 | listing_print (char *name, char **argv) |
252b5132 RH |
1408 | { |
1409 | int using_stdout; | |
cf39a089 | 1410 | |
252b5132 RH |
1411 | title = ""; |
1412 | subtitle = ""; | |
1413 | ||
1414 | if (name == NULL) | |
1415 | { | |
1416 | list_file = stdout; | |
1417 | using_stdout = 1; | |
1418 | } | |
1419 | else | |
1420 | { | |
f740e790 | 1421 | list_file = fopen (name, FOPEN_WT); |
252b5132 RH |
1422 | if (list_file != NULL) |
1423 | using_stdout = 0; | |
1424 | else | |
1425 | { | |
885afe7b | 1426 | as_warn (_("can't open %s: %s"), name, xstrerror (errno)); |
252b5132 RH |
1427 | list_file = stdout; |
1428 | using_stdout = 1; | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | if (listing & LISTING_NOFORM) | |
5a1964ec | 1433 | paper_height = 0; |
252b5132 | 1434 | |
83f10cb2 NC |
1435 | if (listing & LISTING_GENERAL) |
1436 | listing_general_info (argv); | |
1437 | ||
252b5132 | 1438 | if (listing & LISTING_LISTING) |
5a1964ec | 1439 | listing_listing (name); |
252b5132 RH |
1440 | |
1441 | if (listing & LISTING_SYMBOLS) | |
5a1964ec | 1442 | list_symbol_table (); |
252b5132 RH |
1443 | |
1444 | if (! using_stdout) | |
1445 | { | |
1446 | if (fclose (list_file) == EOF) | |
885afe7b | 1447 | as_warn (_("can't close %s: %s"), name, xstrerror (errno)); |
252b5132 RH |
1448 | } |
1449 | ||
1450 | if (last_open_file) | |
5a1964ec | 1451 | fclose (last_open_file); |
252b5132 RH |
1452 | } |
1453 | ||
252b5132 | 1454 | void |
254d758c | 1455 | listing_file (const char *name) |
252b5132 RH |
1456 | { |
1457 | fn = name; | |
1458 | } | |
1459 | ||
1460 | void | |
254d758c | 1461 | listing_eject (int ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
1462 | { |
1463 | if (listing) | |
1464 | listing_tail->edict = EDICT_EJECT; | |
1465 | } | |
1466 | ||
252b5132 RH |
1467 | /* Turn listing on or off. An argument of 0 means to turn off |
1468 | listing. An argument of 1 means to turn on listing. An argument | |
1469 | of 2 means to turn off listing, but as of the next line; that is, | |
1470 | the current line should be listed, but the next line should not. */ | |
1471 | ||
1472 | void | |
254d758c | 1473 | listing_list (int on) |
252b5132 RH |
1474 | { |
1475 | if (listing) | |
1476 | { | |
1477 | switch (on) | |
1478 | { | |
1479 | case 0: | |
1480 | if (listing_tail->edict == EDICT_LIST) | |
1481 | listing_tail->edict = EDICT_NONE; | |
1482 | else | |
1483 | listing_tail->edict = EDICT_NOLIST; | |
1484 | break; | |
1485 | case 1: | |
1486 | if (listing_tail->edict == EDICT_NOLIST | |
1487 | || listing_tail->edict == EDICT_NOLIST_NEXT) | |
1488 | listing_tail->edict = EDICT_NONE; | |
1489 | else | |
1490 | listing_tail->edict = EDICT_LIST; | |
1491 | break; | |
1492 | case 2: | |
1493 | listing_tail->edict = EDICT_NOLIST_NEXT; | |
1494 | break; | |
1495 | default: | |
1496 | abort (); | |
1497 | } | |
1498 | } | |
1499 | } | |
1500 | ||
252b5132 | 1501 | void |
254d758c | 1502 | listing_psize (int width_only) |
252b5132 RH |
1503 | { |
1504 | if (! width_only) | |
1505 | { | |
1506 | paper_height = get_absolute_expression (); | |
1507 | ||
1508 | if (paper_height < 0 || paper_height > 1000) | |
1509 | { | |
1510 | paper_height = 0; | |
1511 | as_warn (_("strange paper height, set to no form")); | |
1512 | } | |
1513 | ||
1514 | if (*input_line_pointer != ',') | |
1515 | { | |
1516 | demand_empty_rest_of_line (); | |
1517 | return; | |
1518 | } | |
1519 | ||
1520 | ++input_line_pointer; | |
1521 | } | |
1522 | ||
1523 | paper_width = get_absolute_expression (); | |
1524 | ||
1525 | demand_empty_rest_of_line (); | |
1526 | } | |
1527 | ||
1528 | void | |
254d758c | 1529 | listing_nopage (int ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
1530 | { |
1531 | paper_height = 0; | |
1532 | } | |
1533 | ||
1534 | void | |
254d758c | 1535 | listing_title (int depth) |
252b5132 RH |
1536 | { |
1537 | int quoted; | |
1538 | char *start; | |
1539 | char *ttl; | |
1540 | unsigned int length; | |
1541 | ||
1542 | SKIP_WHITESPACE (); | |
1543 | if (*input_line_pointer != '\"') | |
1544 | quoted = 0; | |
1545 | else | |
1546 | { | |
1547 | quoted = 1; | |
1548 | ++input_line_pointer; | |
1549 | } | |
1550 | ||
1551 | start = input_line_pointer; | |
1552 | ||
1553 | while (*input_line_pointer) | |
1554 | { | |
1555 | if (quoted | |
1556 | ? *input_line_pointer == '\"' | |
1557 | : is_end_of_line[(unsigned char) *input_line_pointer]) | |
1558 | { | |
1559 | if (listing) | |
1560 | { | |
1561 | length = input_line_pointer - start; | |
1e9cc1c2 | 1562 | ttl = (char *) xmalloc (length + 1); |
252b5132 RH |
1563 | memcpy (ttl, start, length); |
1564 | ttl[length] = 0; | |
1565 | listing_tail->edict = depth ? EDICT_SBTTL : EDICT_TITLE; | |
1566 | listing_tail->edict_arg = ttl; | |
1567 | } | |
1568 | if (quoted) | |
1569 | input_line_pointer++; | |
1570 | demand_empty_rest_of_line (); | |
1571 | return; | |
1572 | } | |
1573 | else if (*input_line_pointer == '\n') | |
1574 | { | |
0e389e77 | 1575 | as_bad (_("new line in title")); |
252b5132 RH |
1576 | demand_empty_rest_of_line (); |
1577 | return; | |
1578 | } | |
1579 | else | |
1580 | { | |
1581 | input_line_pointer++; | |
1582 | } | |
1583 | } | |
1584 | } | |
1585 | ||
252b5132 | 1586 | void |
254d758c | 1587 | listing_source_line (unsigned int line) |
252b5132 RH |
1588 | { |
1589 | if (listing) | |
1590 | { | |
1591 | new_frag (); | |
1592 | listing_tail->hll_line = line; | |
1593 | new_frag (); | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | void | |
254d758c | 1598 | listing_source_file (const char *file) |
252b5132 RH |
1599 | { |
1600 | if (listing) | |
1601 | listing_tail->hll_file = file_info (file); | |
1602 | } | |
1603 | ||
252b5132 RH |
1604 | #else |
1605 | ||
cf39a089 | 1606 | /* Dummy functions for when compiled without listing enabled. */ |
252b5132 | 1607 | |
cf39a089 | 1608 | void |
254d758c | 1609 | listing_list (int on) |
252b5132 RH |
1610 | { |
1611 | s_ignore (0); | |
1612 | } | |
1613 | ||
cf39a089 | 1614 | void |
254d758c | 1615 | listing_eject (int ignore) |
252b5132 RH |
1616 | { |
1617 | s_ignore (0); | |
1618 | } | |
1619 | ||
cf39a089 | 1620 | void |
254d758c | 1621 | listing_psize (int ignore) |
252b5132 RH |
1622 | { |
1623 | s_ignore (0); | |
1624 | } | |
1625 | ||
1626 | void | |
254d758c | 1627 | listing_nopage (int ignore) |
252b5132 RH |
1628 | { |
1629 | s_ignore (0); | |
1630 | } | |
1631 | ||
cf39a089 | 1632 | void |
254d758c | 1633 | listing_title (int depth) |
252b5132 RH |
1634 | { |
1635 | s_ignore (0); | |
1636 | } | |
1637 | ||
1638 | void | |
254d758c | 1639 | listing_file (const char *name) |
252b5132 | 1640 | { |
252b5132 RH |
1641 | } |
1642 | ||
cf39a089 | 1643 | void |
254d758c | 1644 | listing_newline (char *name) |
252b5132 | 1645 | { |
252b5132 RH |
1646 | } |
1647 | ||
cf39a089 | 1648 | void |
254d758c | 1649 | listing_source_line (unsigned int n) |
252b5132 | 1650 | { |
252b5132 | 1651 | } |
cf39a089 KH |
1652 | |
1653 | void | |
254d758c | 1654 | listing_source_file (const char *n) |
252b5132 | 1655 | { |
252b5132 RH |
1656 | } |
1657 | ||
1658 | #endif |