* listing.c: Convert to ISO-C.
[external/binutils.git] / gas / listing.c
1 /* listing.c - maintain assembly listings
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002
4    Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 /*
24  Contributed by Steve Chamberlain <sac@cygnus.com>
25
26  A listing page looks like:
27
28  LISTING_HEADER  sourcefilename pagenumber
29  TITLE LINE
30  SUBTITLE LINE
31  linenumber address data  source
32  linenumber address data  source
33  linenumber address data  source
34  linenumber address data  source
35
36  If not overridden, the listing commands are:
37
38  .title  "stuff"
39         Put "stuff" onto the title line
40  .sbttl  "stuff"
41         Put stuff onto the subtitle line
42
43   If these commands come within 10 lines of the top of the page, they
44   will affect the page they are on, as well as any subsequent page
45
46  .eject
47         Thow a page
48  .list
49         Increment the enable listing counter
50  .nolist
51         Decrement the enable listing counter
52
53  .psize Y[,X]
54         Set the paper size to X wide and Y high. Setting a psize Y of
55         zero will suppress form feeds except where demanded by .eject
56
57  If the counter goes below zero, listing is suppressed.
58
59  Listings are a maintained by read calling various listing_<foo>
60  functions.  What happens most is that the macro NO_LISTING is not
61  defined (from the Makefile), then the macro LISTING_NEWLINE expands
62  into a call to listing_newline.  The call is done from read.c, every
63  time it sees a newline, and -l is on the command line.
64
65  The function listing_newline remembers the frag associated with the
66  newline, and creates a new frag - note that this is wasteful, but not
67  a big deal, since listing slows things down a lot anyway.  The
68  function also remembers when the filename changes.
69
70  When all the input has finished, and gas has had a chance to settle
71  down, the listing is output. This is done by running down the list of
72  frag/source file records, and opening the files as needed and printing
73  out the bytes and chars associated with them.
74
75  The only things which the architecture can change about the listing
76  are defined in these macros:
77
78  LISTING_HEADER         The name of the architecture
79  LISTING_WORD_SIZE      The make of the number of bytes in a word, this determines
80                         the clumping of the output data. eg a value of
81                         2 makes words look like 1234 5678, whilst 1
82                         would make the same value look like 12 34 56
83                         78
84  LISTING_LHS_WIDTH      Number of words of above size for the lhs
85
86  LISTING_LHS_WIDTH_SECOND   Number of words for the data on the lhs
87                         for the second line
88
89  LISTING_LHS_CONT_LINES Max number of lines to use up for a continuation
90  LISTING_RHS_WIDTH      Number of chars from the input file to print
91                         on a line
92 */
93
94 #include "as.h"
95 #include "obstack.h"
96 #include "safe-ctype.h"
97 #include "input-file.h"
98 #include "subsegs.h"
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
109 #define LISTING_LHS_WIDTH ((LISTING_WORD_SIZE) > 4 ? 1 : 4 / (LISTING_WORD_SIZE))
110 #endif
111 #ifndef LISTING_LHS_WIDTH_SECOND
112 #define LISTING_LHS_WIDTH_SECOND LISTING_LHS_WIDTH
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
120
121 /* This structure remembers which .s were used.  */
122 typedef struct file_info_struct {
123   struct file_info_struct * next;
124   char *                    filename;
125   long                      pos;
126   unsigned int              linenum;
127   int                       at_end;
128 } file_info_type;
129
130 /* This structure remembers which line from which file goes into which
131    frag.  */
132 struct list_info_struct {
133   /* Frag which this line of source is nearest to.  */
134   fragS *frag;
135
136   /* The actual line in the source file.  */
137   unsigned int line;
138   /* Pointer to the file info struct for the file which this line
139      belongs to.  */
140   file_info_type *file;
141
142   /* The expanded text of any macro that may have been executing.  */
143   char *line_contents;
144
145   /* Next in list.  */
146   struct list_info_struct *next;
147
148   /* Pointer to the file info struct for the high level language
149      source line that belongs here.  */
150   file_info_type *hll_file;
151   /* High level language source line.  */
152   unsigned int hll_line;
153
154   /* Pointer to any error message associated with this line.  */
155   char *message;
156
157   enum {
158     EDICT_NONE,
159     EDICT_SBTTL,
160     EDICT_TITLE,
161     EDICT_NOLIST,
162     EDICT_LIST,
163     EDICT_NOLIST_NEXT,
164     EDICT_EJECT
165   } edict;
166   char *edict_arg;
167
168   /* Nonzero if this line is to be omitted because it contains
169      debugging information.  This can become a flags field if we come
170      up with more information to store here.  */
171   int debugging;
172 };
173
174 typedef struct list_info_struct list_info_type;
175
176 int listing_lhs_width        = LISTING_LHS_WIDTH;
177 int listing_lhs_width_second = LISTING_LHS_WIDTH_SECOND;
178 int listing_lhs_cont_lines   = LISTING_LHS_CONT_LINES;
179 int listing_rhs_width        = LISTING_RHS_WIDTH;
180
181 struct list_info_struct *        listing_tail;
182
183 static file_info_type *          file_info_head;
184 static file_info_type *          last_open_file_info;
185 static FILE *                    last_open_file;
186 static struct list_info_struct * head;
187 static int                       paper_width = 200;
188 static int                       paper_height = 60;
189
190 extern int                       listing;
191
192 /* File to output listings to.  */
193 static FILE *list_file;
194
195 /* This static array is used to keep the text of data to be printed
196    before the start of the line.  */
197
198 #define MAX_BYTES                                                       \
199   (((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width                    \
200    + ((((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second)        \
201       * listing_lhs_cont_lines)                                         \
202    + 20)
203
204 static char *data_buffer;
205
206 /* Prototypes.  */
207 static void listing_message (const char *name, const char *message);
208 static file_info_type *file_info (const char *file_name);
209 static void new_frag (void);
210 static char *buffer_line (file_info_type *file, char *line, unsigned int size);
211 static void listing_page (list_info_type *list);
212 static unsigned int calc_hex (list_info_type *list);
213 static void print_lines (list_info_type *, unsigned int,
214                          char *, unsigned int);
215 static void list_symbol_table (void);
216 static void print_source (file_info_type *current_file, list_info_type *list,
217                           char *buffer, unsigned int width);
218 static int debugging_pseudo (list_info_type *, const char *);
219 static void listing_listing (char *name);
220
221 static void
222 listing_message (const char *name, const char *message)
223 {
224   if (listing_tail != (list_info_type *) NULL)
225     {
226       unsigned int l = strlen (name) + strlen (message) + 1;
227       char *n = (char *) xmalloc (l);
228       strcpy (n, name);
229       strcat (n, message);
230       listing_tail->message = n;
231     }
232 }
233
234 void
235 listing_warning (const char *message)
236 {
237   listing_message (_("Warning:"), message);
238 }
239
240 void
241 listing_error (const char *message)
242 {
243   listing_message (_("Error:"), message);
244 }
245
246 static file_info_type *
247 file_info (const char *file_name)
248 {
249   /* Find an entry with this file name.  */
250   file_info_type *p = file_info_head;
251
252   while (p != (file_info_type *) NULL)
253     {
254       if (strcmp (p->filename, file_name) == 0)
255         return p;
256       p = p->next;
257     }
258
259   /* Make new entry.  */
260
261   p = (file_info_type *) xmalloc (sizeof (file_info_type));
262   p->next = file_info_head;
263   file_info_head = p;
264   p->filename = xstrdup (file_name);
265   p->pos = 0;
266   p->linenum = 0;
267   p->at_end = 0;
268
269   return p;
270 }
271
272 static void
273 new_frag (void)
274 {
275
276   frag_wane (frag_now);
277   frag_new (0);
278
279 }
280
281 void
282 listing_newline (char *ps)
283 {
284   char *file;
285   unsigned int line;
286   static unsigned int last_line = 0xffff;
287   static char *last_file = NULL;
288   list_info_type *new = NULL;
289
290   if (listing == 0)
291     return;
292
293   if (now_seg == absolute_section)
294     return;
295
296 #ifdef OBJ_ELF
297   /* In ELF, anything in a section beginning with .debug or .line is
298      considered to be debugging information.  This includes the
299      statement which switches us into the debugging section, which we
300      can only set after we are already in the debugging section.  */
301   if ((listing & LISTING_NODEBUG) != 0
302       && listing_tail != NULL
303       && ! listing_tail->debugging)
304     {
305       const char *segname;
306
307       segname = segment_name (now_seg);
308       if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0
309           || strncmp (segname, ".line", sizeof ".line" - 1) == 0)
310         listing_tail->debugging = 1;
311     }
312 #endif
313
314   as_where (&file, &line);
315   if (ps == NULL)
316     {
317       if (line == last_line
318           && !(last_file && file && strcmp (file, last_file)))
319         return;
320
321       new = (list_info_type *) xmalloc (sizeof (list_info_type));
322
323       /* Detect if we are reading from stdin by examining the file
324          name returned by as_where().
325
326          [FIXME: We rely upon the name in the strcmp below being the
327          same as the one used by input_scrub_new_file(), if that is
328          not true, then this code will fail].
329
330          If we are reading from stdin, then we need to save each input
331          line here (assuming of course that we actually have a line of
332          input to read), so that it can be displayed in the listing
333          that is produced at the end of the assembly.  */
334       if (strcmp (file, _("{standard input}")) == 0
335           && input_line_pointer != NULL)
336         {
337           char *copy;
338           int len;
339           int seen_quote = 0;
340
341           for (copy = input_line_pointer - 1;
342                *copy && (seen_quote
343                          || (! is_end_of_line [(unsigned char) *copy]));
344                copy++)
345             if (*copy == '"' && copy[-1] != '\\')
346               seen_quote = ! seen_quote;
347
348           len = (copy - input_line_pointer) + 2;
349
350           copy = xmalloc (len);
351
352           if (copy != NULL)
353             {
354               char *src = input_line_pointer - 1;
355               char *dest = copy;
356
357               while (--len)
358                 {
359                   unsigned char c = *src++;
360
361                   /* Omit control characters in the listing.  */
362                   if (!ISCNTRL (c))
363                     *dest++ = c;
364                 }
365
366               *dest = 0;
367             }
368
369           new->line_contents = copy;
370         }
371       else
372         new->line_contents = NULL;
373     }
374   else
375     {
376       new = (list_info_type *) xmalloc (sizeof (list_info_type));
377       new->line_contents = ps;
378     }
379
380   last_line = line;
381   last_file = file;
382
383   new_frag ();
384
385   if (listing_tail)
386     listing_tail->next = new;
387   else
388     head = new;
389
390   listing_tail = new;
391
392   new->frag = frag_now;
393   new->line = line;
394   new->file = file_info (file);
395   new->next = (list_info_type *) NULL;
396   new->message = (char *) NULL;
397   new->edict = EDICT_NONE;
398   new->hll_file = (file_info_type *) NULL;
399   new->hll_line = 0;
400   new->debugging = 0;
401
402   new_frag ();
403
404 #ifdef OBJ_ELF
405   /* In ELF, anything in a section beginning with .debug or .line is
406      considered to be debugging information.  */
407   if ((listing & LISTING_NODEBUG) != 0)
408     {
409       const char *segname;
410
411       segname = segment_name (now_seg);
412       if (strncmp (segname, ".debug", sizeof ".debug" - 1) == 0
413           || strncmp (segname, ".line", sizeof ".line" - 1) == 0)
414         new->debugging = 1;
415     }
416 #endif
417 }
418
419 /* Attach all current frags to the previous line instead of the
420    current line.  This is called by the MIPS backend when it discovers
421    that it needs to add some NOP instructions; the added NOP
422    instructions should go with the instruction that has the delay, not
423    with the new instruction.  */
424
425 void
426 listing_prev_line (void)
427 {
428   list_info_type *l;
429   fragS *f;
430
431   if (head == (list_info_type *) NULL
432       || head == listing_tail)
433     return;
434
435   new_frag ();
436
437   for (l = head; l->next != listing_tail; l = l->next)
438     ;
439
440   for (f = frchain_now->frch_root; f != (fragS *) NULL; f = f->fr_next)
441     if (f->line == listing_tail)
442       f->line = l;
443
444   listing_tail->frag = frag_now;
445   new_frag ();
446 }
447
448 /* This function returns the next source line from the file supplied,
449    truncated to size.  It appends a fake line to the end of each input
450    file to make.  */
451
452 static char *
453 buffer_line (file_info_type *file, char *line, unsigned int size)
454 {
455   unsigned int count = 0;
456   int c;
457
458   char *p = line;
459
460   /* If we couldn't open the file, return an empty line.  */
461   if (file->at_end)
462     return "";
463
464   /* Check the cache and see if we last used this file.  */
465   if (!last_open_file_info || file != last_open_file_info)
466     {
467       if (last_open_file)
468         {
469           last_open_file_info->pos = ftell (last_open_file);
470           fclose (last_open_file);
471         }
472
473       last_open_file_info = file;
474       last_open_file = fopen (file->filename, FOPEN_RT);
475       if (last_open_file == NULL)
476         {
477           file->at_end = 1;
478           return "";
479         }
480
481       /* Seek to where we were last time this file was open.  */
482       if (file->pos)
483         fseek (last_open_file, file->pos, SEEK_SET);
484     }
485
486   c = fgetc (last_open_file);
487
488   /* Leave room for null.  */
489   size -= 1;
490
491   while (c != EOF && c != '\n')
492     {
493       if (count < size)
494         *p++ = c;
495       count++;
496
497       c = fgetc (last_open_file);
498
499     }
500   if (c == EOF)
501     {
502       file->at_end = 1;
503       if (count + 2 < size)
504         {
505           *p++ = '.';
506           *p++ = '.';
507           *p++ = '.';
508         }
509     }
510   file->linenum++;
511   *p++ = 0;
512   return line;
513 }
514
515 static const char *fn;
516
517 static unsigned int eject;      /* Eject pending */
518 static unsigned int page;       /* Current page number */
519 static char *title;             /* Current title */
520 static char *subtitle;          /* Current subtitle */
521 static unsigned int on_page;    /* Number of lines printed on current page */
522
523 static void
524 listing_page (list_info_type *list)
525 {
526   /* Grope around, see if we can see a title or subtitle edict coming up
527      soon.  (we look down 10 lines of the page and see if it's there)  */
528   if ((eject || (on_page >= (unsigned int) paper_height))
529       && paper_height != 0)
530     {
531       unsigned int c = 10;
532       int had_title = 0;
533       int had_subtitle = 0;
534
535       page++;
536
537       while (c != 0 && list)
538         {
539           if (list->edict == EDICT_SBTTL && !had_subtitle)
540             {
541               had_subtitle = 1;
542               subtitle = list->edict_arg;
543             }
544           if (list->edict == EDICT_TITLE && !had_title)
545             {
546               had_title = 1;
547               title = list->edict_arg;
548             }
549           list = list->next;
550           c--;
551         }
552
553       if (page > 1)
554         {
555           fprintf (list_file, "\f");
556         }
557
558       fprintf (list_file, "%s %s \t\t\tpage %d\n", LISTING_HEADER, fn, page);
559       fprintf (list_file, "%s\n", title);
560       fprintf (list_file, "%s\n", subtitle);
561       on_page = 3;
562       eject = 0;
563     }
564 }
565
566 static unsigned int
567 calc_hex (list_info_type *list)
568 {
569   int data_buffer_size;
570   list_info_type *first = list;
571   unsigned int address = ~(unsigned int) 0;
572   fragS *frag;
573   fragS *frag_ptr;
574   unsigned int octet_in_frag;
575
576   /* Find first frag which says it belongs to this line.  */
577   frag = list->frag;
578   while (frag && frag->line != list)
579     frag = frag->fr_next;
580
581   frag_ptr = frag;
582
583   data_buffer_size = 0;
584
585   /* Dump all the frags which belong to this line.  */
586   while (frag_ptr != (fragS *) NULL && frag_ptr->line == first)
587     {
588       /* Print as many bytes from the fixed part as is sensible.  */
589       octet_in_frag = 0;
590       while ((offsetT) octet_in_frag < frag_ptr->fr_fix
591              && data_buffer_size < MAX_BYTES - 3)
592         {
593           if (address == ~(unsigned int) 0)
594             {
595               address = frag_ptr->fr_address / OCTETS_PER_BYTE;
596             }
597
598           sprintf (data_buffer + data_buffer_size,
599                    "%02X",
600                    (frag_ptr->fr_literal[octet_in_frag]) & 0xff);
601           data_buffer_size += 2;
602           octet_in_frag++;
603         }
604       if (frag_ptr->fr_type == rs_fill)
605         {
606           unsigned int var_rep_max = octet_in_frag;
607           unsigned int var_rep_idx = octet_in_frag;
608
609           /* Print as many bytes from the variable part as is sensible.  */
610           while (((offsetT) octet_in_frag
611                   < (frag_ptr->fr_fix + frag_ptr->fr_var * frag_ptr->fr_offset))
612                  && data_buffer_size < MAX_BYTES - 3)
613             {
614               if (address == ~(unsigned int) 0)
615                 {
616                   address = frag_ptr->fr_address / OCTETS_PER_BYTE;
617                 }
618               sprintf (data_buffer + data_buffer_size,
619                        "%02X",
620                        (frag_ptr->fr_literal[var_rep_idx]) & 0xff);
621 #if 0
622               data_buffer[data_buffer_size++] = '*';
623               data_buffer[data_buffer_size++] = '*';
624 #endif
625               data_buffer_size += 2;
626
627               var_rep_idx++;
628               octet_in_frag++;
629
630               if ((offsetT) var_rep_idx >= frag_ptr->fr_fix + frag_ptr->fr_var)
631                 var_rep_idx = var_rep_max;
632             }
633         }
634
635       frag_ptr = frag_ptr->fr_next;
636     }
637   data_buffer[data_buffer_size] = '\0';
638   return address;
639 }
640
641 static void
642 print_lines (list_info_type *list, unsigned int lineno,
643              char *string, unsigned int address)
644 {
645   unsigned int idx;
646   unsigned int nchars;
647   unsigned int lines;
648   unsigned int octet_in_word = 0;
649   char *src = data_buffer;
650   int cur;
651
652   /* Print the stuff on the first line.  */
653   listing_page (list);
654   nchars = (LISTING_WORD_SIZE * 2 + 1) * listing_lhs_width;
655
656   /* Print the hex for the first line.  */
657   if (address == ~(unsigned int) 0)
658     {
659       fprintf (list_file, "% 4d     ", lineno);
660       for (idx = 0; idx < nchars; idx++)
661         fprintf (list_file, " ");
662
663       fprintf (list_file, "\t%s\n", string ? string : "");
664
665       on_page++;
666
667       listing_page (0);
668
669       return;
670     }
671
672   if (had_errors ())
673     fprintf (list_file, "% 4d ???? ", lineno);
674   else
675     fprintf (list_file, "% 4d %04x ", lineno, address);
676
677   /* And the data to go along with it.  */
678   idx = 0;
679   cur = 0;
680   while (src[cur] && idx < nchars)
681     {
682       int offset;
683       offset = cur;
684       fprintf (list_file, "%c%c", src[offset], src[offset + 1]);
685       cur += 2;
686       octet_in_word++;
687
688       if (octet_in_word == LISTING_WORD_SIZE)
689         {
690           fprintf (list_file, " ");
691           idx++;
692           octet_in_word = 0;
693         }
694
695       idx += 2;
696     }
697
698   for (; idx < nchars; idx++)
699     fprintf (list_file, " ");
700
701   fprintf (list_file, "\t%s\n", string ? string : "");
702   on_page++;
703   listing_page (list);
704
705   if (list->message)
706     {
707       fprintf (list_file, "****  %s\n", list->message);
708       listing_page (list);
709       on_page++;
710     }
711
712   for (lines = 0;
713        lines < (unsigned int) listing_lhs_cont_lines
714          && src[cur];
715        lines++)
716     {
717       nchars = ((LISTING_WORD_SIZE * 2) + 1) * listing_lhs_width_second - 1;
718       idx = 0;
719
720       /* Print any more lines of data, but more compactly.  */
721       fprintf (list_file, "% 4d      ", lineno);
722
723       while (src[cur] && idx < nchars)
724         {
725           int offset;
726           offset = cur;
727           fprintf (list_file, "%c%c", src[offset], src[offset + 1]);
728           cur += 2;
729           idx += 2;
730           octet_in_word++;
731
732           if (octet_in_word == LISTING_WORD_SIZE)
733             {
734               fprintf (list_file, " ");
735               idx++;
736               octet_in_word = 0;
737             }
738         }
739
740       fprintf (list_file, "\n");
741       on_page++;
742       listing_page (list);
743     }
744 }
745
746 static void
747 list_symbol_table (void)
748 {
749   extern symbolS *symbol_rootP;
750   int got_some = 0;
751
752   symbolS *ptr;
753   eject = 1;
754   listing_page (0);
755
756   for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
757     {
758       if (SEG_NORMAL (S_GET_SEGMENT (ptr))
759           || S_GET_SEGMENT (ptr) == absolute_section)
760         {
761 #ifdef BFD_ASSEMBLER
762           /* Don't report section symbols.  They are not interesting.  */
763           if (symbol_section_p (ptr))
764             continue;
765 #endif
766           if (S_GET_NAME (ptr))
767             {
768               char buf[30], fmt[8];
769               valueT val = S_GET_VALUE (ptr);
770
771               /* @@ Note that this is dependent on the compilation options,
772                  not solely on the target characteristics.  */
773               if (sizeof (val) == 4 && sizeof (int) == 4)
774                 sprintf (buf, "%08lx", (unsigned long) val);
775               else if (sizeof (val) <= sizeof (unsigned long))
776                 {
777                   sprintf (fmt, "%%0%lulx",
778                            (unsigned long) (sizeof (val) * 2));
779                   sprintf (buf, fmt, (unsigned long) val);
780                 }
781 #if defined (BFD64)
782               else if (sizeof (val) > 4)
783                 sprintf_vma (buf, val);
784 #endif
785               else
786                 abort ();
787
788               if (!got_some)
789                 {
790                   fprintf (list_file, "DEFINED SYMBOLS\n");
791                   on_page++;
792                   got_some = 1;
793                 }
794
795               if (symbol_get_frag (ptr) && symbol_get_frag (ptr)->line)
796                 {
797                   fprintf (list_file, "%20s:%-5d  %s:%s %s\n",
798                            symbol_get_frag (ptr)->line->file->filename,
799                            symbol_get_frag (ptr)->line->line,
800                            segment_name (S_GET_SEGMENT (ptr)),
801                            buf, S_GET_NAME (ptr));
802                 }
803               else
804                 {
805                   fprintf (list_file, "%33s:%s %s\n",
806                            segment_name (S_GET_SEGMENT (ptr)),
807                            buf, S_GET_NAME (ptr));
808                 }
809
810               on_page++;
811               listing_page (0);
812             }
813         }
814
815     }
816   if (!got_some)
817     {
818       fprintf (list_file, "NO DEFINED SYMBOLS\n");
819       on_page++;
820     }
821   fprintf (list_file, "\n");
822   on_page++;
823   listing_page (0);
824
825   got_some = 0;
826
827   for (ptr = symbol_rootP; ptr != (symbolS *) NULL; ptr = symbol_next (ptr))
828     {
829       if (S_GET_NAME (ptr) && strlen (S_GET_NAME (ptr)) != 0)
830         {
831           if (S_GET_SEGMENT (ptr) == undefined_section)
832             {
833               if (!got_some)
834                 {
835                   got_some = 1;
836                   fprintf (list_file, "UNDEFINED SYMBOLS\n");
837                   on_page++;
838                   listing_page (0);
839                 }
840               fprintf (list_file, "%s\n", S_GET_NAME (ptr));
841               on_page++;
842               listing_page (0);
843             }
844         }
845     }
846   if (!got_some)
847     {
848       fprintf (list_file, "NO UNDEFINED SYMBOLS\n");
849       on_page++;
850       listing_page (0);
851     }
852 }
853
854 static void
855 print_source (file_info_type *current_file, list_info_type *list,
856               char *buffer, unsigned int width)
857 {
858   if (!current_file->at_end)
859     {
860       while (current_file->linenum < list->hll_line
861              && !current_file->at_end)
862         {
863           char *p = buffer_line (current_file, buffer, width);
864           fprintf (list_file, "%4u:%-13s **** %s\n", current_file->linenum,
865                    current_file->filename, p);
866           on_page++;
867           listing_page (list);
868         }
869     }
870 }
871
872 /* Sometimes the user doesn't want to be bothered by the debugging
873    records inserted by the compiler, see if the line is suspicious.  */
874
875 static int
876 debugging_pseudo (list_info_type *list, const char *line)
877 {
878   static int in_debug;
879   int was_debug;
880
881   if (list->debugging)
882     {
883       in_debug = 1;
884       return 1;
885     }
886
887   was_debug = in_debug;
888   in_debug = 0;
889
890   while (ISSPACE (*line))
891     line++;
892
893   if (*line != '.')
894     {
895 #ifdef OBJ_ELF
896       /* The ELF compiler sometimes emits blank lines after switching
897          out of a debugging section.  If the next line drops us back
898          into debugging information, then don't print the blank line.
899          This is a hack for a particular compiler behaviour, not a
900          general case.  */
901       if (was_debug
902           && *line == '\0'
903           && list->next != NULL
904           && list->next->debugging)
905         {
906           in_debug = 1;
907           return 1;
908         }
909 #endif
910
911       return 0;
912     }
913
914   line++;
915
916   if (strncmp (line, "def", 3) == 0)
917     return 1;
918   if (strncmp (line, "val", 3) == 0)
919     return 1;
920   if (strncmp (line, "scl", 3) == 0)
921     return 1;
922   if (strncmp (line, "line", 4) == 0)
923     return 1;
924   if (strncmp (line, "endef", 5) == 0)
925     return 1;
926   if (strncmp (line, "ln", 2) == 0)
927     return 1;
928   if (strncmp (line, "type", 4) == 0)
929     return 1;
930   if (strncmp (line, "size", 4) == 0)
931     return 1;
932   if (strncmp (line, "dim", 3) == 0)
933     return 1;
934   if (strncmp (line, "tag", 3) == 0)
935     return 1;
936
937   if (strncmp (line, "stabs", 5) == 0)
938     return 1;
939   if (strncmp (line, "stabn", 5) == 0)
940     return 1;
941
942   return 0;
943 }
944
945 static void
946 listing_listing (char *name ATTRIBUTE_UNUSED)
947 {
948   list_info_type *list = head;
949   file_info_type *current_hll_file = (file_info_type *) NULL;
950   char *message;
951   char *buffer;
952   char *p;
953   int show_listing = 1;
954   unsigned int width;
955
956   buffer = xmalloc (listing_rhs_width);
957   data_buffer = xmalloc (MAX_BYTES);
958   eject = 1;
959   list = head;
960
961   while (list != (list_info_type *) NULL && 0)
962     {
963       if (list->next)
964         list->frag = list->next->frag;
965       list = list->next;
966
967     }
968
969   list = head->next;
970
971   while (list)
972     {
973       unsigned int list_line;
974
975       width = listing_rhs_width > paper_width ? paper_width :
976         listing_rhs_width;
977
978       list_line = list->line;
979       switch (list->edict)
980         {
981         case EDICT_LIST:
982           /* Skip all lines up to the current.  */
983           list_line--;
984           break;
985         case EDICT_NOLIST:
986           show_listing--;
987           break;
988         case EDICT_NOLIST_NEXT:
989           if (show_listing == 0)
990             list_line--;
991           break;
992         case EDICT_EJECT:
993           break;
994         case EDICT_NONE:
995           break;
996         case EDICT_TITLE:
997           title = list->edict_arg;
998           break;
999         case EDICT_SBTTL:
1000           subtitle = list->edict_arg;
1001           break;
1002         default:
1003           abort ();
1004         }
1005
1006       if (show_listing <= 0)
1007         {
1008           while (list->file->linenum < list_line
1009                  && !list->file->at_end)
1010             p = buffer_line (list->file, buffer, width);
1011         }
1012
1013       if (list->edict == EDICT_LIST
1014           || (list->edict == EDICT_NOLIST_NEXT && show_listing == 0))
1015         {
1016           /* Enable listing for the single line that caused the enable.  */
1017           list_line++;
1018           show_listing++;
1019         }
1020
1021       if (show_listing > 0)
1022         {
1023           /* Scan down the list and print all the stuff which can be done
1024              with this line (or lines).  */
1025           message = 0;
1026
1027           if (list->hll_file)
1028             {
1029               current_hll_file = list->hll_file;
1030             }
1031
1032           if (current_hll_file && list->hll_line && (listing & LISTING_HLL))
1033             {
1034               print_source (current_hll_file, list, buffer, width);
1035             }
1036
1037           if (list->line_contents)
1038             {
1039               if (!((listing & LISTING_NODEBUG)
1040                     && debugging_pseudo (list, list->line_contents)))
1041                 {
1042                   print_lines (list,
1043                                list->file->linenum == 0 ? list->line : list->file->linenum,
1044                                list->line_contents, calc_hex (list));
1045                 }
1046               free (list->line_contents);
1047               list->line_contents = NULL;
1048             }
1049           else
1050             {
1051               while (list->file->linenum < list_line
1052                      && !list->file->at_end)
1053                 {
1054                   unsigned int address;
1055
1056                   p = buffer_line (list->file, buffer, width);
1057
1058                   if (list->file->linenum < list_line)
1059                     address = ~(unsigned int) 0;
1060                   else
1061                     address = calc_hex (list);
1062
1063                   if (!((listing & LISTING_NODEBUG)
1064                         && debugging_pseudo (list, p)))
1065                     print_lines (list, list->file->linenum, p, address);
1066                 }
1067             }
1068
1069           if (list->edict == EDICT_EJECT)
1070             {
1071               eject = 1;
1072             }
1073         }
1074
1075       if (list->edict == EDICT_NOLIST_NEXT && show_listing == 1)
1076         --show_listing;
1077
1078       list = list->next;
1079     }
1080
1081   free (buffer);
1082   free (data_buffer);
1083   data_buffer = NULL;
1084 }
1085
1086 void
1087 listing_print (char *name)
1088 {
1089   int using_stdout;
1090
1091   title = "";
1092   subtitle = "";
1093
1094   if (name == NULL)
1095     {
1096       list_file = stdout;
1097       using_stdout = 1;
1098     }
1099   else
1100     {
1101       list_file = fopen (name, FOPEN_WT);
1102       if (list_file != NULL)
1103         using_stdout = 0;
1104       else
1105         {
1106           as_perror (_("can't open list file: %s"), name);
1107           list_file = stdout;
1108           using_stdout = 1;
1109         }
1110     }
1111
1112   if (listing & LISTING_NOFORM)
1113     {
1114       paper_height = 0;
1115     }
1116
1117   if (listing & LISTING_LISTING)
1118     {
1119       listing_listing (name);
1120     }
1121
1122   if (listing & LISTING_SYMBOLS)
1123     {
1124       list_symbol_table ();
1125     }
1126
1127   if (! using_stdout)
1128     {
1129       if (fclose (list_file) == EOF)
1130         as_perror (_("error closing list file: %s"), name);
1131     }
1132
1133   if (last_open_file)
1134     {
1135       fclose (last_open_file);
1136     }
1137 }
1138
1139 void
1140 listing_file (const char *name)
1141 {
1142   fn = name;
1143 }
1144
1145 void
1146 listing_eject (int ignore ATTRIBUTE_UNUSED)
1147 {
1148   if (listing)
1149     listing_tail->edict = EDICT_EJECT;
1150 }
1151
1152 void
1153 listing_flags (int ignore ATTRIBUTE_UNUSED)
1154 {
1155   while ((*input_line_pointer++) && (*input_line_pointer != '\n'))
1156     input_line_pointer++;
1157
1158 }
1159
1160 /* Turn listing on or off.  An argument of 0 means to turn off
1161    listing.  An argument of 1 means to turn on listing.  An argument
1162    of 2 means to turn off listing, but as of the next line; that is,
1163    the current line should be listed, but the next line should not.  */
1164
1165 void
1166 listing_list (int on)
1167 {
1168   if (listing)
1169     {
1170       switch (on)
1171         {
1172         case 0:
1173           if (listing_tail->edict == EDICT_LIST)
1174             listing_tail->edict = EDICT_NONE;
1175           else
1176             listing_tail->edict = EDICT_NOLIST;
1177           break;
1178         case 1:
1179           if (listing_tail->edict == EDICT_NOLIST
1180               || listing_tail->edict == EDICT_NOLIST_NEXT)
1181             listing_tail->edict = EDICT_NONE;
1182           else
1183             listing_tail->edict = EDICT_LIST;
1184           break;
1185         case 2:
1186           listing_tail->edict = EDICT_NOLIST_NEXT;
1187           break;
1188         default:
1189           abort ();
1190         }
1191     }
1192 }
1193
1194 void
1195 listing_psize (int width_only)
1196 {
1197   if (! width_only)
1198     {
1199       paper_height = get_absolute_expression ();
1200
1201       if (paper_height < 0 || paper_height > 1000)
1202         {
1203           paper_height = 0;
1204           as_warn (_("strange paper height, set to no form"));
1205         }
1206
1207       if (*input_line_pointer != ',')
1208         {
1209           demand_empty_rest_of_line ();
1210           return;
1211         }
1212
1213       ++input_line_pointer;
1214     }
1215
1216   paper_width = get_absolute_expression ();
1217
1218   demand_empty_rest_of_line ();
1219 }
1220
1221 void
1222 listing_nopage (int ignore ATTRIBUTE_UNUSED)
1223 {
1224   paper_height = 0;
1225 }
1226
1227 void
1228 listing_title (int depth)
1229 {
1230   int quoted;
1231   char *start;
1232   char *ttl;
1233   unsigned int length;
1234
1235   SKIP_WHITESPACE ();
1236   if (*input_line_pointer != '\"')
1237     quoted = 0;
1238   else
1239     {
1240       quoted = 1;
1241       ++input_line_pointer;
1242     }
1243
1244   start = input_line_pointer;
1245
1246   while (*input_line_pointer)
1247     {
1248       if (quoted
1249           ? *input_line_pointer == '\"'
1250           : is_end_of_line[(unsigned char) *input_line_pointer])
1251         {
1252           if (listing)
1253             {
1254               length = input_line_pointer - start;
1255               ttl = xmalloc (length + 1);
1256               memcpy (ttl, start, length);
1257               ttl[length] = 0;
1258               listing_tail->edict = depth ? EDICT_SBTTL : EDICT_TITLE;
1259               listing_tail->edict_arg = ttl;
1260             }
1261           if (quoted)
1262             input_line_pointer++;
1263           demand_empty_rest_of_line ();
1264           return;
1265         }
1266       else if (*input_line_pointer == '\n')
1267         {
1268           as_bad (_("new line in title"));
1269           demand_empty_rest_of_line ();
1270           return;
1271         }
1272       else
1273         {
1274           input_line_pointer++;
1275         }
1276     }
1277 }
1278
1279 void
1280 listing_source_line (unsigned int line)
1281 {
1282   if (listing)
1283     {
1284       new_frag ();
1285       listing_tail->hll_line = line;
1286       new_frag ();
1287     }
1288 }
1289
1290 void
1291 listing_source_file (const char *file)
1292 {
1293   if (listing)
1294     listing_tail->hll_file = file_info (file);
1295 }
1296
1297 #else
1298
1299 /* Dummy functions for when compiled without listing enabled.  */
1300
1301 void
1302 listing_flags (int ignore)
1303 {
1304   s_ignore (0);
1305 }
1306
1307 void
1308 listing_list (int on)
1309 {
1310   s_ignore (0);
1311 }
1312
1313 void
1314 listing_eject (int ignore)
1315 {
1316   s_ignore (0);
1317 }
1318
1319 void
1320 listing_psize (int ignore)
1321 {
1322   s_ignore (0);
1323 }
1324
1325 void
1326 listing_nopage (int ignore)
1327 {
1328   s_ignore (0);
1329 }
1330
1331 void
1332 listing_title (int depth)
1333 {
1334   s_ignore (0);
1335 }
1336
1337 void
1338 listing_file (const char *name)
1339 {
1340
1341 }
1342
1343 void
1344 listing_newline (char *name)
1345 {
1346
1347 }
1348
1349 void
1350 listing_source_line (unsigned int n)
1351 {
1352
1353 }
1354
1355 void
1356 listing_source_file (const char *n)
1357 {
1358
1359 }
1360
1361 #endif