New lexer.
[external/binutils.git] / ld / ldmisc.c
1 /* ldmisc.c
2    Copyright (C) 1991 Free Software Foundation, Inc.
3
4    Written by Steve Chamberlain of Cygnus Support.
5
6 This file is part of GLD, the Gnu Linker.
7
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING.  If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 /*
23 $Id$ 
24
25
26  */
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include <varargs.h>
31
32 #include "ld.h"
33 #include "ldmisc.h"
34 #include "ldlang.h"
35 #include "ldlex.h"
36 /* IMPORTS */
37
38 extern char *program_name;
39
40 extern FILE *ldlex_input_stack;
41 extern char *ldfile_input_filename;
42 extern ld_config_type config;
43
44 void
45 yyerror(arg) 
46 char *arg;
47
48   einfo("%P%F: %S %s\n",arg);
49 }
50
51 extern int errno;
52 extern   int  sys_nerr;
53 extern char *sys_errlist[];
54
55 /*
56  %F error is fatal
57  %P print progam name
58  %S print script file and linenumber
59  %E current bfd error or errno
60  %I filename from a lang_input_statement_type
61  %B filename from a bfd
62  %T symbol table entry
63  %X no object output, fail return
64  %V hex bfd_vma
65  %C Clever filename:linenumber 
66  %R info about a relent
67  %
68 */
69 static void
70 vfinfo(fp, fmt, arg)
71      FILE *fp;
72      char *fmt;
73      va_list arg;
74 {
75   extern char *cplus_demangle();
76   boolean fatal = false;
77   while (*fmt) 
78   {
79     while (*fmt != '%' && *fmt != '\0') 
80     {
81       putc(*fmt, fp);
82       fmt++;
83     }
84     if (*fmt == '%') 
85     {
86       fmt ++;
87       switch (*fmt++) 
88       {
89       case 'X':
90         config.make_executable = false;
91         break;
92       case 'V':
93       {
94         bfd_vma value = va_arg(arg, bfd_vma);
95         fprintf_vma(fp, value);
96       }
97         break;
98       case 'T':
99       {
100         asymbol *symbol = va_arg(arg, asymbol *);
101         if (symbol) 
102         {
103
104             
105           asection *section = symbol->section;
106           char *cplusname = cplus_demangle(symbol->name, 1);
107           CONST char *section_name =  section->name;
108           if (section != &bfd_und_section) 
109           {
110             fprintf(fp,"%s (%s)", cplusname ? cplusname :
111                     symbol->name, section_name);
112           }
113           else 
114           {
115             fprintf(fp,"%s", cplusname ? cplusname : symbol->name);
116           }
117             
118           if (cplusname) 
119           {
120             free(cplusname);
121           }
122             
123         }
124         else 
125         {
126           fprintf(fp,"no symbol");
127         }
128       }
129         break;
130       case 'B':
131       { 
132         bfd *abfd = va_arg(arg, bfd *);
133         if (abfd->my_archive) {
134             fprintf(fp,"%s(%s)", abfd->my_archive->filename,
135                     abfd->filename);
136           }
137         else {
138             fprintf(fp,"%s", abfd->filename);
139
140           }
141       }
142         break;
143       case 'F':
144         fatal = true;
145         break;
146       case 'P':
147         fprintf(fp,"%s", program_name);
148         break;
149       case 'E':
150         /* Replace with the most recent errno explanation */
151
152
153         fprintf(fp, bfd_errmsg(bfd_error));
154
155
156         break;
157       case 'I':
158       {
159         lang_input_statement_type *i =
160          va_arg(arg,lang_input_statement_type *);
161         
162         fprintf(fp,"%s", i->local_sym_name);
163       }
164         break;
165       case 'S':
166         /* Print source script file and line number */
167
168       {
169         
170          
171             extern unsigned int lineno;
172             if (ldfile_input_filename == (char *)NULL) {
173                 fprintf(fp,"command line");
174               }
175             else {
176                 fprintf(fp,"%s:%u", ldfile_input_filename, lineno );
177               }
178           }
179         
180         break;
181
182       case 'R':
183         /* Print all that's interesting about a relent */
184       {
185         arelent *relent = va_arg(arg, arelent *);
186         
187         fprintf(fp,"%s+0x%x (type %s)",
188                 (*(relent->sym_ptr_ptr))->name,
189                 relent->addend,
190                 relent->howto->name);
191         
192
193       }
194         break;
195         
196
197
198         
199       case 'C':
200       {
201         CONST char *filename;
202         CONST char *functionname;
203         char *cplus_name;
204          
205         unsigned int linenumber;
206         bfd *abfd = va_arg(arg, bfd *);
207         asection *section = va_arg(arg, asection *);
208         asymbol **symbols = va_arg(arg, asymbol **);
209         bfd_vma offset = va_arg(arg, bfd_vma);
210          
211         if (bfd_find_nearest_line(abfd,
212                                   section,
213                                   symbols,
214                                   offset,
215                                   &filename,
216                                   &functionname,
217                                   &linenumber))
218         {
219           if (filename == (char *)NULL)         
220            filename = abfd->filename;
221           if (functionname != (char *)NULL) 
222           {
223             cplus_name = cplus_demangle(functionname, 1);
224             fprintf(fp,"%s:%u: (%s)", filename, linenumber,
225                     cplus_name? cplus_name: functionname);
226             if (cplus_name) 
227              free(cplus_name);
228                   
229
230           }
231                 
232           else if (linenumber != 0) 
233            fprintf(fp,"%s:%u", filename, linenumber);
234           else
235            fprintf(fp,"%s(%s+%0x)", filename,
236                    section->name,
237                    offset);
238
239         }
240         else {
241             fprintf(fp,"%s(%s+%0x)", abfd->filename,
242                     section->name,
243                     offset);
244           }
245       }
246         break;
247                 
248       case 's':
249         fprintf(fp,"%s", va_arg(arg, char *));
250         break;
251       case 'd':
252         fprintf(fp,"%d", va_arg(arg, int));
253         break;
254       default:
255         fprintf(fp,"%s", va_arg(arg, char *));
256         break;
257       }
258     }
259   }
260   if (fatal == true) {
261       extern char *output_filename;
262       if (output_filename)
263        unlink(output_filename);
264       exit(1);
265     }
266 }
267
268 /* Format info message and print on stdout. */
269
270 void info(va_alist)
271 va_dcl
272 {
273   char *fmt;
274   va_list arg;
275   va_start(arg);
276   fmt = va_arg(arg, char *);
277   vfinfo(stdout, fmt, arg);
278   va_end(arg);
279 }
280
281 /* ('e' for error.) Format info message and print on stderr. */
282
283 void einfo(va_alist)
284 va_dcl
285 {
286   char *fmt;
287   va_list arg;
288   va_start(arg);
289   fmt = va_arg(arg, char *);
290   vfinfo(stderr, fmt, arg);
291   va_end(arg);
292 }
293
294 void 
295 info_assert(file, line)
296 char *file;
297 unsigned int line;
298 {
299   einfo("%F%P internal error %s %d\n", file,line);
300 }
301
302 /* Return a newly-allocated string
303    whose contents concatenate those of S1, S2, S3.  */
304
305 char *
306 DEFUN(concat, (s1, s2, s3),
307       CONST char *s1 AND
308       CONST char *s2 AND
309       CONST char *s3)
310 {
311   bfd_size_type len1 = strlen (s1);
312   bfd_size_type len2 = strlen (s2);
313   bfd_size_type len3 = strlen (s3);
314   char *result = ldmalloc (len1 + len2 + len3 + 1);
315
316   if (len1 != 0)
317     memcpy(result, s1, len1);
318   if (len2 != 0)
319     memcpy(result+len1, s2, len2);
320   if (len3 != 0)
321     memcpy(result+len1+len2, s2, len3);
322   *(result + len1 + len2 + len3) = 0;
323
324   return result;
325 }
326
327
328
329 PTR
330 DEFUN(ldmalloc, (size),
331 bfd_size_type size)
332 {
333   PTR result =  malloc ((int)size);
334
335   if (result == (char *)NULL && size != 0)
336     einfo("%F%P virtual memory exhausted\n");
337
338   return result;
339
340
341
342 PTR
343 DEFUN(ldrealloc, (ptr, size),
344 PTR ptr AND
345 bfd_size_type size)
346 {
347   PTR result =  realloc (ptr, (int)size);
348
349   if (result == (char *)NULL && size != 0)
350     einfo("%F%P virtual memory exhausted\n");
351
352   return result;
353
354
355
356
357 char *DEFUN(buystring,(x),
358             CONST char *CONST x)
359 {
360   bfd_size_type  l = strlen(x)+1;
361   char *r = ldmalloc(l);
362   memcpy(r, x,l);
363   return r;
364 }
365
366
367 /* ('m' for map) Format info message and print on map. */
368
369 void minfo(va_alist)
370 va_dcl
371 {
372   char *fmt;
373   va_list arg;
374   va_start(arg);
375   fmt = va_arg(arg, char *);
376   vfinfo(config.map_file, fmt, arg);
377   va_end(arg);
378 }
379
380
381
382
383
384
385 /*----------------------------------------------------------------------
386   Functions to print the link map 
387  */
388
389 void 
390 DEFUN_VOID(print_space)
391 {
392   fprintf(config.map_file, " ");
393 }
394 void 
395 DEFUN_VOID(print_nl)
396 {
397   fprintf(config.map_file, "\n");
398 }
399 void 
400 DEFUN(print_address,(value),
401       bfd_vma value)
402 {
403   fprintf_vma(config.map_file, value);
404 }