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