Commit | Line | Data |
---|---|---|
c611e285 SC |
1 | /* ldmisc.c |
2 | Copyright (C) 1991 Free Software Foundation, Inc. | |
3 | ||
4 | Written by Steve Chamberlain of Cygnus Support. | |
2fa0b342 DHW |
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 | |
c611e285 | 10 | the Free Software Foundation; either version 2, or (at your option) |
2fa0b342 DHW |
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 | ||
c611e285 | 22 | #include "bfd.h" |
2fa0b342 DHW |
23 | #include "sysdep.h" |
24 | #include <varargs.h> | |
fe2b6209 | 25 | #include <demangle.h> |
2fa0b342 DHW |
26 | |
27 | #include "ld.h" | |
28 | #include "ldmisc.h" | |
29 | #include "ldlang.h" | |
1418c83b | 30 | #include "ldlex.h" |
2fa0b342 DHW |
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 | ||
2fa0b342 DHW |
39 | |
40 | extern int errno; | |
41 | extern int sys_nerr; | |
42 | extern char *sys_errlist[]; | |
43 | ||
9b0da7f4 KR |
44 | /* VARARGS*/ |
45 | static void finfo (); | |
46 | ||
2fa0b342 DHW |
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 | |
9b0da7f4 | 57 | %v hex bfd_vma, no leading zeros |
2fa0b342 | 58 | %C Clever filename:linenumber |
c611e285 | 59 | %R info about a relent |
2fa0b342 DHW |
60 | % |
61 | */ | |
9b0da7f4 KR |
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 | ||
c611e285 SC |
83 | static void |
84 | vfinfo(fp, fmt, arg) | |
85 | FILE *fp; | |
86 | char *fmt; | |
87 | va_list arg; | |
2fa0b342 | 88 | { |
2fa0b342 | 89 | boolean fatal = false; |
9d1fe8a4 SC |
90 | while (*fmt) |
91 | { | |
92 | while (*fmt != '%' && *fmt != '\0') | |
93 | { | |
c611e285 | 94 | putc(*fmt, fp); |
2fa0b342 DHW |
95 | fmt++; |
96 | } | |
9d1fe8a4 SC |
97 | if (*fmt == '%') |
98 | { | |
2fa0b342 | 99 | fmt ++; |
9d1fe8a4 SC |
100 | switch (*fmt++) |
101 | { | |
6bf2e3a7 | 102 | case 'X': |
2fa0b342 DHW |
103 | config.make_executable = false; |
104 | break; | |
6bf2e3a7 SC |
105 | case 'V': |
106 | { | |
107 | bfd_vma value = va_arg(arg, bfd_vma); | |
108 | fprintf_vma(fp, value); | |
109 | } | |
2fa0b342 | 110 | break; |
9b0da7f4 KR |
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; | |
6bf2e3a7 SC |
124 | case 'T': |
125 | { | |
126 | asymbol *symbol = va_arg(arg, asymbol *); | |
127 | if (symbol) | |
128 | { | |
9d1fe8a4 SC |
129 | |
130 | ||
6bf2e3a7 | 131 | asection *section = symbol->section; |
9b0da7f4 | 132 | char *cplusname = demangle(symbol->name, 1); |
6bf2e3a7 SC |
133 | CONST char *section_name = section->name; |
134 | if (section != &bfd_und_section) | |
135 | { | |
9b0da7f4 | 136 | fprintf(fp,"%s (%s)", cplusname, section_name); |
6bf2e3a7 SC |
137 | } |
138 | else | |
139 | { | |
9b0da7f4 | 140 | fprintf(fp,"%s", cplusname); |
6bf2e3a7 | 141 | } |
6bf2e3a7 SC |
142 | } |
143 | else | |
144 | { | |
145 | fprintf(fp,"no symbol"); | |
146 | } | |
147 | } | |
2fa0b342 | 148 | break; |
6bf2e3a7 SC |
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 | } | |
2fa0b342 | 161 | break; |
6bf2e3a7 | 162 | case 'F': |
2fa0b342 DHW |
163 | fatal = true; |
164 | break; | |
6bf2e3a7 | 165 | case 'P': |
c611e285 | 166 | fprintf(fp,"%s", program_name); |
2fa0b342 | 167 | break; |
6bf2e3a7 | 168 | case 'E': |
2fa0b342 DHW |
169 | /* Replace with the most recent errno explanation */ |
170 | ||
171 | ||
c611e285 | 172 | fprintf(fp, bfd_errmsg(bfd_error)); |
2fa0b342 DHW |
173 | |
174 | ||
175 | break; | |
6bf2e3a7 SC |
176 | case 'I': |
177 | { | |
178 | lang_input_statement_type *i = | |
179 | va_arg(arg,lang_input_statement_type *); | |
2fa0b342 | 180 | |
6bf2e3a7 SC |
181 | fprintf(fp,"%s", i->local_sym_name); |
182 | } | |
2fa0b342 | 183 | break; |
6bf2e3a7 | 184 | case 'S': |
2fa0b342 DHW |
185 | /* Print source script file and line number */ |
186 | ||
6bf2e3a7 | 187 | { |
9d1fe8a4 SC |
188 | |
189 | ||
6bf2e3a7 SC |
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 | } | |
9d1fe8a4 | 198 | |
2fa0b342 | 199 | break; |
c611e285 | 200 | |
6bf2e3a7 | 201 | case 'R': |
c611e285 | 202 | /* Print all that's interesting about a relent */ |
6bf2e3a7 SC |
203 | { |
204 | arelent *relent = va_arg(arg, arelent *); | |
c611e285 | 205 | |
9b0da7f4 KR |
206 | finfo (fp, "%s+0x%v (type %s)", |
207 | (*(relent->sym_ptr_ptr))->name, | |
208 | relent->addend, | |
209 | relent->howto->name); | |
6bf2e3a7 | 210 | } |
c611e285 SC |
211 | break; |
212 | ||
213 | ||
214 | ||
215 | ||
6bf2e3a7 SC |
216 | case 'C': |
217 | { | |
218 | CONST char *filename; | |
219 | CONST char *functionname; | |
220 | char *cplus_name; | |
9d1fe8a4 | 221 | |
6bf2e3a7 SC |
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); | |
2fa0b342 | 227 | |
6bf2e3a7 SC |
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 | { | |
9b0da7f4 KR |
240 | /* There is no initial '_' to remove here. */ |
241 | cplus_name = demangle(functionname, 0); | |
242 | fprintf(fp,"%s:%u: (%s)", filename, linenumber, cplus_name); | |
6bf2e3a7 | 243 | } |
9d1fe8a4 | 244 | |
6bf2e3a7 SC |
245 | else if (linenumber != 0) |
246 | fprintf(fp,"%s:%u", filename, linenumber); | |
247 | else | |
9b0da7f4 | 248 | finfo (fp, "%s(%s+0x%v)", filename, section->name, offset); |
6bf2e3a7 SC |
249 | |
250 | } | |
9b0da7f4 KR |
251 | else |
252 | finfo (fp, "%s(%s+0x%v)", abfd->filename, section->name, offset); | |
6bf2e3a7 | 253 | } |
2fa0b342 DHW |
254 | break; |
255 | ||
6bf2e3a7 | 256 | case 's': |
c611e285 | 257 | fprintf(fp,"%s", va_arg(arg, char *)); |
2fa0b342 | 258 | break; |
6bf2e3a7 | 259 | case 'd': |
c611e285 | 260 | fprintf(fp,"%d", va_arg(arg, int)); |
2fa0b342 | 261 | break; |
6bf2e3a7 | 262 | default: |
c611e285 | 263 | fprintf(fp,"%s", va_arg(arg, char *)); |
2fa0b342 DHW |
264 | break; |
265 | } | |
266 | } | |
267 | } | |
6bf2e3a7 SC |
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); | |
9d1fe8a4 | 280 | } |
6bf2e3a7 SC |
281 | exit(1); |
282 | } | |
c611e285 SC |
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); | |
2fa0b342 DHW |
295 | va_end(arg); |
296 | } | |
297 | ||
c611e285 SC |
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 | } | |
2fa0b342 DHW |
310 | |
311 | void | |
312 | info_assert(file, line) | |
313 | char *file; | |
314 | unsigned int line; | |
315 | { | |
c611e285 | 316 | einfo("%F%P internal error %s %d\n", file,line); |
2fa0b342 DHW |
317 | } |
318 | ||
319 | /* Return a newly-allocated string | |
320 | whose contents concatenate those of S1, S2, S3. */ | |
321 | ||
322 | char * | |
9b0da7f4 KR |
323 | concat (s1, s2, s3) |
324 | CONST char *s1; | |
325 | CONST char *s2; | |
326 | CONST char *s3; | |
2fa0b342 | 327 | { |
9b0da7f4 KR |
328 | size_t len1 = strlen (s1); |
329 | size_t len2 = strlen (s2); | |
330 | size_t len3 = strlen (s3); | |
2fa0b342 DHW |
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 | ||
c611e285 | 345 | PTR |
9b0da7f4 KR |
346 | ldmalloc (size) |
347 | size_t size; | |
2fa0b342 | 348 | { |
c611e285 | 349 | PTR result = malloc ((int)size); |
2fa0b342 DHW |
350 | |
351 | if (result == (char *)NULL && size != 0) | |
c611e285 | 352 | einfo("%F%P virtual memory exhausted\n"); |
2fa0b342 DHW |
353 | |
354 | return result; | |
355 | } | |
356 | ||
6bf2e3a7 | 357 | PTR |
9b0da7f4 KR |
358 | xmalloc (size) |
359 | int size; | |
6bf2e3a7 SC |
360 | { |
361 | return ldmalloc(size); | |
362 | } | |
363 | ||
2fa0b342 | 364 | |
9d1fe8a4 | 365 | PTR |
9b0da7f4 KR |
366 | ldrealloc (ptr, size) |
367 | PTR ptr; | |
368 | size_t size; | |
9d1fe8a4 SC |
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 | ||
9b0da7f4 KR |
378 | PTR |
379 | xrealloc (ptr, size) | |
380 | PTR ptr; | |
381 | size_t size; | |
382 | { | |
383 | return ldrealloc(ptr, size); | |
384 | } | |
9d1fe8a4 | 385 | |
2fa0b342 | 386 | |
9b0da7f4 KR |
387 | char * |
388 | buystring (x) | |
389 | CONST char *CONST x; | |
2fa0b342 | 390 | { |
9b0da7f4 | 391 | size_t l = strlen(x)+1; |
2fa0b342 DHW |
392 | char *r = ldmalloc(l); |
393 | memcpy(r, x,l); | |
394 | return r; | |
395 | } | |
c611e285 SC |
396 | |
397 | ||
9d1fe8a4 SC |
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 | ||
9b0da7f4 KR |
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 | } | |
9d1fe8a4 SC |
425 | |
426 | ||
427 | ||
c611e285 SC |
428 | /*---------------------------------------------------------------------- |
429 | Functions to print the link map | |
430 | */ | |
431 | ||
432 | void | |
9b0da7f4 | 433 | print_space () |
c611e285 | 434 | { |
9d1fe8a4 | 435 | fprintf(config.map_file, " "); |
c611e285 SC |
436 | } |
437 | void | |
9b0da7f4 | 438 | print_nl () |
c611e285 | 439 | { |
9d1fe8a4 | 440 | fprintf(config.map_file, "\n"); |
c611e285 SC |
441 | } |
442 | void | |
9b0da7f4 KR |
443 | print_address (value) |
444 | bfd_vma value; | |
c611e285 | 445 | { |
9d1fe8a4 | 446 | fprintf_vma(config.map_file, value); |
c611e285 | 447 | } |