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