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