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 | ||
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 | |
c611e285 | 55 | %R info about a relent |
2fa0b342 DHW |
56 | % |
57 | */ | |
c611e285 SC |
58 | static void |
59 | vfinfo(fp, fmt, arg) | |
60 | FILE *fp; | |
61 | char *fmt; | |
62 | va_list arg; | |
2fa0b342 | 63 | { |
2fa0b342 | 64 | boolean fatal = false; |
9d1fe8a4 SC |
65 | while (*fmt) |
66 | { | |
67 | while (*fmt != '%' && *fmt != '\0') | |
68 | { | |
c611e285 | 69 | putc(*fmt, fp); |
2fa0b342 DHW |
70 | fmt++; |
71 | } | |
9d1fe8a4 SC |
72 | if (*fmt == '%') |
73 | { | |
2fa0b342 | 74 | fmt ++; |
9d1fe8a4 SC |
75 | switch (*fmt++) |
76 | { | |
6bf2e3a7 | 77 | case 'X': |
2fa0b342 DHW |
78 | config.make_executable = false; |
79 | break; | |
6bf2e3a7 SC |
80 | case 'V': |
81 | { | |
82 | bfd_vma value = va_arg(arg, bfd_vma); | |
83 | fprintf_vma(fp, value); | |
84 | } | |
2fa0b342 | 85 | break; |
6bf2e3a7 SC |
86 | case 'T': |
87 | { | |
88 | asymbol *symbol = va_arg(arg, asymbol *); | |
89 | if (symbol) | |
90 | { | |
9d1fe8a4 SC |
91 | |
92 | ||
6bf2e3a7 | 93 | asection *section = symbol->section; |
fe2b6209 PB |
94 | char *cplusname = |
95 | cplus_demangle(symbol->name, DMGL_ANSI|DMGL_PARAMS); | |
6bf2e3a7 SC |
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 | } | |
9d1fe8a4 | 106 | |
6bf2e3a7 SC |
107 | if (cplusname) |
108 | { | |
109 | free(cplusname); | |
110 | } | |
9d1fe8a4 | 111 | |
6bf2e3a7 SC |
112 | } |
113 | else | |
114 | { | |
115 | fprintf(fp,"no symbol"); | |
116 | } | |
117 | } | |
2fa0b342 | 118 | break; |
6bf2e3a7 SC |
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 | } | |
2fa0b342 | 131 | break; |
6bf2e3a7 | 132 | case 'F': |
2fa0b342 DHW |
133 | fatal = true; |
134 | break; | |
6bf2e3a7 | 135 | case 'P': |
c611e285 | 136 | fprintf(fp,"%s", program_name); |
2fa0b342 | 137 | break; |
6bf2e3a7 | 138 | case 'E': |
2fa0b342 DHW |
139 | /* Replace with the most recent errno explanation */ |
140 | ||
141 | ||
c611e285 | 142 | fprintf(fp, bfd_errmsg(bfd_error)); |
2fa0b342 DHW |
143 | |
144 | ||
145 | break; | |
6bf2e3a7 SC |
146 | case 'I': |
147 | { | |
148 | lang_input_statement_type *i = | |
149 | va_arg(arg,lang_input_statement_type *); | |
2fa0b342 | 150 | |
6bf2e3a7 SC |
151 | fprintf(fp,"%s", i->local_sym_name); |
152 | } | |
2fa0b342 | 153 | break; |
6bf2e3a7 | 154 | case 'S': |
2fa0b342 DHW |
155 | /* Print source script file and line number */ |
156 | ||
6bf2e3a7 | 157 | { |
9d1fe8a4 SC |
158 | |
159 | ||
6bf2e3a7 SC |
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 | } | |
9d1fe8a4 | 168 | |
2fa0b342 | 169 | break; |
c611e285 | 170 | |
6bf2e3a7 | 171 | case 'R': |
c611e285 | 172 | /* Print all that's interesting about a relent */ |
6bf2e3a7 SC |
173 | { |
174 | arelent *relent = va_arg(arg, arelent *); | |
c611e285 | 175 | |
6bf2e3a7 SC |
176 | fprintf(fp,"%s+0x%x (type %s)", |
177 | (*(relent->sym_ptr_ptr))->name, | |
178 | relent->addend, | |
179 | relent->howto->name); | |
c611e285 SC |
180 | |
181 | ||
6bf2e3a7 | 182 | } |
c611e285 SC |
183 | break; |
184 | ||
185 | ||
186 | ||
187 | ||
6bf2e3a7 SC |
188 | case 'C': |
189 | { | |
190 | CONST char *filename; | |
191 | CONST char *functionname; | |
192 | char *cplus_name; | |
9d1fe8a4 | 193 | |
6bf2e3a7 SC |
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); | |
2fa0b342 | 199 | |
6bf2e3a7 SC |
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 | { | |
fe2b6209 | 212 | cplus_name = cplus_demangle(functionname, DMGL_ANSI|DMGL_PARAMS); |
6bf2e3a7 SC |
213 | fprintf(fp,"%s:%u: (%s)", filename, linenumber, |
214 | cplus_name? cplus_name: functionname); | |
215 | if (cplus_name) | |
216 | free(cplus_name); | |
9d1fe8a4 SC |
217 | |
218 | ||
6bf2e3a7 | 219 | } |
9d1fe8a4 | 220 | |
6bf2e3a7 SC |
221 | else if (linenumber != 0) |
222 | fprintf(fp,"%s:%u", filename, linenumber); | |
223 | else | |
224 | fprintf(fp,"%s(%s+%0x)", filename, | |
c611e285 SC |
225 | section->name, |
226 | offset); | |
6bf2e3a7 SC |
227 | |
228 | } | |
229 | else { | |
230 | fprintf(fp,"%s(%s+%0x)", abfd->filename, | |
231 | section->name, | |
232 | offset); | |
233 | } | |
234 | } | |
2fa0b342 DHW |
235 | break; |
236 | ||
6bf2e3a7 | 237 | case 's': |
c611e285 | 238 | fprintf(fp,"%s", va_arg(arg, char *)); |
2fa0b342 | 239 | break; |
6bf2e3a7 | 240 | case 'd': |
c611e285 | 241 | fprintf(fp,"%d", va_arg(arg, int)); |
2fa0b342 | 242 | break; |
6bf2e3a7 | 243 | default: |
c611e285 | 244 | fprintf(fp,"%s", va_arg(arg, char *)); |
2fa0b342 DHW |
245 | break; |
246 | } | |
247 | } | |
248 | } | |
6bf2e3a7 SC |
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); | |
9d1fe8a4 | 261 | } |
6bf2e3a7 SC |
262 | exit(1); |
263 | } | |
c611e285 SC |
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); | |
2fa0b342 DHW |
276 | va_end(arg); |
277 | } | |
278 | ||
c611e285 SC |
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 | } | |
2fa0b342 DHW |
291 | |
292 | void | |
293 | info_assert(file, line) | |
294 | char *file; | |
295 | unsigned int line; | |
296 | { | |
c611e285 | 297 | einfo("%F%P internal error %s %d\n", file,line); |
2fa0b342 DHW |
298 | } |
299 | ||
300 | /* Return a newly-allocated string | |
301 | whose contents concatenate those of S1, S2, S3. */ | |
302 | ||
303 | char * | |
99fe4553 SC |
304 | DEFUN(concat, (s1, s2, s3), |
305 | CONST char *s1 AND | |
306 | CONST char *s2 AND | |
307 | CONST char *s3) | |
2fa0b342 | 308 | { |
c611e285 SC |
309 | bfd_size_type len1 = strlen (s1); |
310 | bfd_size_type len2 = strlen (s2); | |
311 | bfd_size_type len3 = strlen (s3); | |
2fa0b342 DHW |
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 | ||
c611e285 SC |
326 | PTR |
327 | DEFUN(ldmalloc, (size), | |
328 | bfd_size_type size) | |
2fa0b342 | 329 | { |
c611e285 | 330 | PTR result = malloc ((int)size); |
2fa0b342 DHW |
331 | |
332 | if (result == (char *)NULL && size != 0) | |
c611e285 | 333 | einfo("%F%P virtual memory exhausted\n"); |
2fa0b342 DHW |
334 | |
335 | return result; | |
336 | } | |
337 | ||
6bf2e3a7 SC |
338 | PTR |
339 | DEFUN(xmalloc,(size), | |
340 | int size) | |
341 | { | |
342 | return ldmalloc(size); | |
343 | } | |
344 | ||
2fa0b342 | 345 | |
9d1fe8a4 SC |
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 | ||
2fa0b342 | 360 | |
1418c83b SC |
361 | char *DEFUN(buystring,(x), |
362 | CONST char *CONST x) | |
2fa0b342 | 363 | { |
c611e285 | 364 | bfd_size_type l = strlen(x)+1; |
2fa0b342 DHW |
365 | char *r = ldmalloc(l); |
366 | memcpy(r, x,l); | |
367 | return r; | |
368 | } | |
c611e285 SC |
369 | |
370 | ||
9d1fe8a4 SC |
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 | ||
c611e285 SC |
389 | /*---------------------------------------------------------------------- |
390 | Functions to print the link map | |
391 | */ | |
392 | ||
393 | void | |
394 | DEFUN_VOID(print_space) | |
395 | { | |
9d1fe8a4 | 396 | fprintf(config.map_file, " "); |
c611e285 SC |
397 | } |
398 | void | |
399 | DEFUN_VOID(print_nl) | |
400 | { | |
9d1fe8a4 | 401 | fprintf(config.map_file, "\n"); |
c611e285 SC |
402 | } |
403 | void | |
404 | DEFUN(print_address,(value), | |
405 | bfd_vma value) | |
406 | { | |
9d1fe8a4 | 407 | fprintf_vma(config.map_file, value); |
c611e285 | 408 | } |