* ldver.c: Increase version number to 1.97, for consistency with ../binutils.
[deliverable/binutils-gdb.git] / ld / ldmisc.c
CommitLineData
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
6This file is part of GLD, the Gnu Linker.
7
8GLD is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
c611e285 10the Free Software Foundation; either version 2, or (at your option)
2fa0b342
DHW
11any later version.
12
13GLD is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GLD; see the file COPYING. If not, write to
20the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22/*
c611e285 23$Id$
2fa0b342 24
2fa0b342 25
c611e285 26 */
2fa0b342 27
c611e285 28#include "bfd.h"
2fa0b342
DHW
29#include "sysdep.h"
30#include <varargs.h>
2fa0b342
DHW
31
32#include "ld.h"
33#include "ldmisc.h"
34#include "ldlang.h"
1418c83b 35#include "ldlex.h"
2fa0b342
DHW
36/* IMPORTS */
37
38extern char *program_name;
39
40extern FILE *ldlex_input_stack;
41extern char *ldfile_input_filename;
42extern ld_config_type config;
43
44void
45yyerror(arg)
46char *arg;
47{
c611e285 48 einfo("%P%F: %S %s\n",arg);
2fa0b342
DHW
49}
50
51extern int errno;
52extern int sys_nerr;
53extern 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
c611e285 66 %R info about a relent
2fa0b342
DHW
67 %
68*/
c611e285
SC
69static void
70vfinfo(fp, fmt, arg)
71 FILE *fp;
72 char *fmt;
73 va_list arg;
2fa0b342 74{
9d1fe8a4 75 extern char *cplus_demangle();
2fa0b342 76 boolean fatal = false;
9d1fe8a4
SC
77 while (*fmt)
78 {
79 while (*fmt != '%' && *fmt != '\0')
80 {
c611e285 81 putc(*fmt, fp);
2fa0b342
DHW
82 fmt++;
83 }
9d1fe8a4
SC
84 if (*fmt == '%')
85 {
2fa0b342 86 fmt ++;
9d1fe8a4
SC
87 switch (*fmt++)
88 {
2fa0b342
DHW
89 case 'X':
90 config.make_executable = false;
91 break;
92 case 'V':
9d1fe8a4
SC
93 {
94 bfd_vma value = va_arg(arg, bfd_vma);
95 fprintf_vma(fp, value);
96 }
2fa0b342
DHW
97 break;
98 case 'T':
9d1fe8a4
SC
99 {
100 asymbol *symbol = va_arg(arg, asymbol *);
101 if (symbol)
2fa0b342 102 {
9d1fe8a4
SC
103
104
105 asection *section = symbol->section;
106 char *cplusname = cplus_demangle(symbol->name, 1);
107 CONST char *section_name = section->name;
108 if (section != &bfd_und_section)
c611e285 109 {
9d1fe8a4
SC
110 fprintf(fp,"%s (%s)", cplusname ? cplusname :
111 symbol->name, section_name);
2fa0b342 112 }
c611e285
SC
113 else
114 {
9d1fe8a4 115 fprintf(fp,"%s", cplusname ? cplusname : symbol->name);
2fa0b342 116 }
9d1fe8a4
SC
117
118 if (cplusname)
119 {
120 free(cplusname);
121 }
122
2fa0b342 123 }
9d1fe8a4
SC
124 else
125 {
126 fprintf(fp,"no symbol");
127 }
128 }
2fa0b342
DHW
129 break;
130 case 'B':
9d1fe8a4
SC
131 {
132 bfd *abfd = va_arg(arg, bfd *);
133 if (abfd->my_archive) {
c611e285 134 fprintf(fp,"%s(%s)", abfd->my_archive->filename,
2fa0b342
DHW
135 abfd->filename);
136 }
9d1fe8a4 137 else {
c611e285 138 fprintf(fp,"%s", abfd->filename);
2fa0b342
DHW
139
140 }
9d1fe8a4 141 }
2fa0b342
DHW
142 break;
143 case 'F':
144 fatal = true;
145 break;
146 case 'P':
c611e285 147 fprintf(fp,"%s", program_name);
2fa0b342
DHW
148 break;
149 case 'E':
150 /* Replace with the most recent errno explanation */
151
152
c611e285 153 fprintf(fp, bfd_errmsg(bfd_error));
2fa0b342
DHW
154
155
156 break;
157 case 'I':
9d1fe8a4
SC
158 {
159 lang_input_statement_type *i =
160 va_arg(arg,lang_input_statement_type *);
2fa0b342 161
9d1fe8a4
SC
162 fprintf(fp,"%s", i->local_sym_name);
163 }
2fa0b342
DHW
164 break;
165 case 'S':
166 /* Print source script file and line number */
167
9d1fe8a4
SC
168 {
169
170
171 extern unsigned int lineno;
172 if (ldfile_input_filename == (char *)NULL) {
173 fprintf(fp,"command line");
174 }
175 else {
176 fprintf(fp,"%s:%u", ldfile_input_filename, lineno );
177 }
7ca04d28 178 }
9d1fe8a4 179
2fa0b342 180 break;
c611e285
SC
181
182 case 'R':
183 /* Print all that's interesting about a relent */
184 {
185 arelent *relent = va_arg(arg, arelent *);
186
187 fprintf(fp,"%s+0x%x (type %s)",
188 (*(relent->sym_ptr_ptr))->name,
189 relent->addend,
190 relent->howto->name);
191
192
193 }
194 break;
195
196
197
198
2fa0b342 199 case 'C':
9d1fe8a4
SC
200 {
201 CONST char *filename;
202 CONST char *functionname;
203 char *cplus_name;
204
205 unsigned int linenumber;
206 bfd *abfd = va_arg(arg, bfd *);
207 asection *section = va_arg(arg, asection *);
208 asymbol **symbols = va_arg(arg, asymbol **);
209 bfd_vma offset = va_arg(arg, bfd_vma);
2fa0b342 210
9d1fe8a4
SC
211 if (bfd_find_nearest_line(abfd,
212 section,
213 symbols,
214 offset,
215 &filename,
216 &functionname,
217 &linenumber))
218 {
219 if (filename == (char *)NULL)
220 filename = abfd->filename;
221 if (functionname != (char *)NULL)
222 {
223 cplus_name = cplus_demangle(functionname, 1);
224 fprintf(fp,"%s:%u: (%s)", filename, linenumber,
225 cplus_name? cplus_name: functionname);
226 if (cplus_name)
227 free(cplus_name);
228
229
230 }
231
232 else if (linenumber != 0)
233 fprintf(fp,"%s:%u", filename, linenumber);
234 else
235 fprintf(fp,"%s(%s+%0x)", filename,
236 section->name,
237 offset);
238
239 }
240 else {
c611e285
SC
241 fprintf(fp,"%s(%s+%0x)", abfd->filename,
242 section->name,
243 offset);
2fa0b342 244 }
9d1fe8a4 245 }
2fa0b342
DHW
246 break;
247
248 case 's':
c611e285 249 fprintf(fp,"%s", va_arg(arg, char *));
2fa0b342
DHW
250 break;
251 case 'd':
c611e285 252 fprintf(fp,"%d", va_arg(arg, int));
2fa0b342
DHW
253 break;
254 default:
c611e285 255 fprintf(fp,"%s", va_arg(arg, char *));
2fa0b342
DHW
256 break;
257 }
258 }
259 }
260 if (fatal == true) {
9d1fe8a4
SC
261 extern char *output_filename;
262 if (output_filename)
263 unlink(output_filename);
264 exit(1);
265 }
c611e285
SC
266}
267
268/* Format info message and print on stdout. */
269
270void info(va_alist)
271va_dcl
272{
273 char *fmt;
274 va_list arg;
275 va_start(arg);
276 fmt = va_arg(arg, char *);
277 vfinfo(stdout, fmt, arg);
2fa0b342
DHW
278 va_end(arg);
279}
280
c611e285
SC
281/* ('e' for error.) Format info message and print on stderr. */
282
283void einfo(va_alist)
284va_dcl
285{
286 char *fmt;
287 va_list arg;
288 va_start(arg);
289 fmt = va_arg(arg, char *);
290 vfinfo(stderr, fmt, arg);
291 va_end(arg);
292}
2fa0b342
DHW
293
294void
295info_assert(file, line)
296char *file;
297unsigned int line;
298{
c611e285 299 einfo("%F%P internal error %s %d\n", file,line);
2fa0b342
DHW
300}
301
302/* Return a newly-allocated string
303 whose contents concatenate those of S1, S2, S3. */
304
305char *
99fe4553
SC
306DEFUN(concat, (s1, s2, s3),
307 CONST char *s1 AND
308 CONST char *s2 AND
309 CONST char *s3)
2fa0b342 310{
c611e285
SC
311 bfd_size_type len1 = strlen (s1);
312 bfd_size_type len2 = strlen (s2);
313 bfd_size_type len3 = strlen (s3);
2fa0b342
DHW
314 char *result = ldmalloc (len1 + len2 + len3 + 1);
315
316 if (len1 != 0)
317 memcpy(result, s1, len1);
318 if (len2 != 0)
319 memcpy(result+len1, s2, len2);
320 if (len3 != 0)
321 memcpy(result+len1+len2, s2, len3);
322 *(result + len1 + len2 + len3) = 0;
323
324 return result;
325}
326
327
328
c611e285
SC
329PTR
330DEFUN(ldmalloc, (size),
331bfd_size_type size)
2fa0b342 332{
c611e285 333 PTR result = malloc ((int)size);
2fa0b342
DHW
334
335 if (result == (char *)NULL && size != 0)
c611e285 336 einfo("%F%P virtual memory exhausted\n");
2fa0b342
DHW
337
338 return result;
339}
340
341
9d1fe8a4
SC
342PTR
343DEFUN(ldrealloc, (ptr, size),
344PTR ptr AND
345bfd_size_type size)
346{
347 PTR result = realloc (ptr, (int)size);
348
349 if (result == (char *)NULL && size != 0)
350 einfo("%F%P virtual memory exhausted\n");
351
352 return result;
353}
354
355
2fa0b342 356
1418c83b
SC
357char *DEFUN(buystring,(x),
358 CONST char *CONST x)
2fa0b342 359{
c611e285 360 bfd_size_type l = strlen(x)+1;
2fa0b342
DHW
361 char *r = ldmalloc(l);
362 memcpy(r, x,l);
363 return r;
364}
c611e285
SC
365
366
9d1fe8a4
SC
367/* ('m' for map) Format info message and print on map. */
368
369void minfo(va_alist)
370va_dcl
371{
372 char *fmt;
373 va_list arg;
374 va_start(arg);
375 fmt = va_arg(arg, char *);
376 vfinfo(config.map_file, fmt, arg);
377 va_end(arg);
378}
379
380
381
382
383
384
c611e285
SC
385/*----------------------------------------------------------------------
386 Functions to print the link map
387 */
388
389void
390DEFUN_VOID(print_space)
391{
9d1fe8a4 392 fprintf(config.map_file, " ");
c611e285
SC
393}
394void
395DEFUN_VOID(print_nl)
396{
9d1fe8a4 397 fprintf(config.map_file, "\n");
c611e285
SC
398}
399void
400DEFUN(print_address,(value),
401 bfd_vma value)
402{
9d1fe8a4 403 fprintf_vma(config.map_file, value);
c611e285 404}
This page took 0.064005 seconds and 4 git commands to generate.