Merges from Intel.
[external/binutils.git] / ld / ldmisc.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2
3 This file is part of GLD, the Gnu Linker.
4
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
8 any later version.
9
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING.  If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
18
19 /*
20  * $Id$ 
21  */
22
23 /*
24   ldmisc.c
25
26 */
27
28 #include "sysdep.h"
29 #include <varargs.h>
30 #include "bfd.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   info("%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  %
67 */
68 void info(va_alist)
69 va_dcl
70 {
71   char *fmt;
72   boolean fatal = false;
73   va_list arg;
74   va_start(arg);
75   fmt = va_arg(arg, char *);
76   while (*fmt) {
77     while (*fmt != '%' && *fmt != '\0') {
78       fputc(*fmt, stderr);
79       fmt++;
80     }
81     if (*fmt == '%') {
82       fmt ++;
83       switch (*fmt++) {
84       case 'X':
85         config.make_executable = false;
86         break;
87       case 'V':
88         fprintf(stderr,"%08lx", va_arg(arg, bfd_vma));
89         break;
90       case 'T':
91         {
92           asymbol *symbol = va_arg(arg, asymbol *);
93           if (symbol) {
94             asection *section = symbol->section;
95             if ((symbol->flags & BSF_UNDEFINED) == 0) {
96             CONST char *section_name = section == (asection *)NULL ?
97                 "absolute" : section->name;
98               fprintf(stderr,"%s (%s)", symbol->name, section_name);
99             }
100             else {
101               fprintf(stderr,"%s", symbol->name);
102             }
103           }
104           else {
105             fprintf(stderr,"no symbol");
106           }
107         }
108         break;
109       case 'B':
110         { 
111           bfd *abfd = va_arg(arg, bfd *);
112           if (abfd->my_archive) {
113             fprintf(stderr,"%s(%s)", abfd->my_archive->filename,
114                     abfd->filename);
115           }
116           else {
117             fprintf(stderr,"%s", abfd->filename);
118
119           }
120         }
121         break;
122       case 'F':
123         fatal = true;
124         break;
125       case 'P':
126         fprintf(stderr,"%s", program_name);
127         break;
128       case 'E':
129         /* Replace with the most recent errno explanation */
130
131
132         fprintf(stderr, bfd_errmsg(bfd_error));
133
134
135         break;
136       case 'I':
137         {
138           lang_input_statement_type *i =
139             va_arg(arg,lang_input_statement_type *);
140         
141           fprintf(stderr,"%s", i->local_sym_name);
142         }
143         break;
144       case 'S':
145         /* Print source script file and line number */
146
147         if (ldlex_input_stack) {
148           extern unsigned int lineno;
149           if (ldfile_input_filename == (char *)NULL) {
150             fprintf(stderr,"command line");
151           }
152           else {
153             fprintf(stderr,"%s:%u", ldfile_input_filename, lineno );
154           }
155         }
156         else {
157           int ch;
158           int n = 0;
159           fprintf(stderr,"command (just before \"");
160           ch = lex_input();
161           while (ch != 0 && n < 10) {
162             fprintf(stderr, "%c", ch);
163             ch = lex_input();
164             n++;
165           }
166           fprintf(stderr,"\")");
167             
168         }
169         break;
170       case 'C':
171         {
172          CONST char *filename;
173          CONST char *functionname;
174           unsigned int linenumber;
175           bfd *abfd = va_arg(arg, bfd *);
176           asection *section = va_arg(arg, asection *);
177           asymbol **symbols = va_arg(arg, asymbol **);
178           bfd_vma offset = va_arg(arg, bfd_vma);
179          
180           if (bfd_find_nearest_line(abfd,
181                                     section,
182                                     symbols,
183                                     offset,
184                                     &filename,
185                                     &functionname,
186                                     &linenumber))
187             {
188                 if (filename == (char *)NULL)   
189                     filename = abfd->filename;
190                 if (functionname != (char *)NULL)
191                     fprintf(stderr,"%s:%u: (%s)", filename, linenumber,  functionname);
192                 else if (linenumber != 0) 
193                     fprintf(stderr,"%s:%u", filename, linenumber);
194                 else
195                     fprintf(stderr,"%s", filename);
196
197             }
198           else {
199             fprintf(stderr,"%s", abfd->filename);
200           }
201         }
202         break;
203                 
204       case 's':
205         fprintf(stderr,"%s", va_arg(arg, char *));
206         break;
207       case 'd':
208         fprintf(stderr,"%d", va_arg(arg, int));
209         break;
210       default:
211         fprintf(stderr,"%s", va_arg(arg, char *));
212         break;
213       }
214     }
215   }
216   if (fatal == true) {
217     extern char *output_filename;
218     if (output_filename)
219       unlink(output_filename);
220     exit(1);
221   }
222   va_end(arg);
223 }
224
225
226 void 
227 info_assert(file, line)
228 char *file;
229 unsigned int line;
230 {
231   info("%F%P internal error %s %d\n", file,line);
232 }
233
234 /* Return a newly-allocated string
235    whose contents concatenate those of S1, S2, S3.  */
236
237 char *
238 DEFUN(concat, (s1, s2, s3),
239       CONST char *s1 AND
240       CONST char *s2 AND
241       CONST char *s3)
242 {
243   size_t len1 = strlen (s1);
244   size_t len2 = strlen (s2);
245   size_t len3 = strlen (s3);
246   char *result = ldmalloc (len1 + len2 + len3 + 1);
247
248   if (len1 != 0)
249     memcpy(result, s1, len1);
250   if (len2 != 0)
251     memcpy(result+len1, s2, len2);
252   if (len3 != 0)
253     memcpy(result+len1+len2, s2, len3);
254   *(result + len1 + len2 + len3) = 0;
255
256   return result;
257 }
258
259
260
261 char  *ldmalloc (size)
262 size_t size;
263 {
264   char * result =  malloc (size);
265
266   if (result == (char *)NULL && size != 0)
267     info("%F%P virtual memory exhausted\n");
268
269   return result;
270
271
272
273
274 char *DEFUN(buystring,(x),
275             CONST char *CONST x)
276 {
277   size_t  l = strlen(x)+1;
278   char *r = ldmalloc(l);
279   memcpy(r, x,l);
280   return r;
281 }