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