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