OSDN Git Service

Locale changes from Bruno Haible <haible@clisp.cons.org>.
[pf3gnuchains/pf3gnuchains3x.git] / bfd / binary.c
1 /* BFD back-end for binary objects.
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Written by Ian Lance Taylor, Cygnus Support, <ian@cygnus.com>
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program 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 of the License, or
11 (at your option) any later version.
12
13 This program 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 this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* This is a BFD backend which may be used to write binary objects.
23    It may only be used for output, not input.  The intention is that
24    this may be used as an output format for objcopy in order to
25    generate raw binary data.
26
27    This is very simple.  The only complication is that the real data
28    will start at some address X, and in some cases we will not want to
29    include X zeroes just to get to that point.  Since the start
30    address is not meaningful for this object file format, we use it
31    instead to indicate the number of zeroes to skip at the start of
32    the file.  objcopy cooperates by specially setting the start
33    address to zero by default.  */
34
35 #include "safe-ctype.h"
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "libbfd.h"
39
40 /* Any bfd we create by reading a binary file has three symbols:
41    a start symbol, an end symbol, and an absolute length symbol.  */
42 #define BIN_SYMS 3
43
44 static boolean binary_mkobject PARAMS ((bfd *));
45 static const bfd_target *binary_object_p PARAMS ((bfd *));
46 static boolean binary_get_section_contents
47   PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
48 static long binary_get_symtab_upper_bound PARAMS ((bfd *));
49 static char *mangle_name PARAMS ((bfd *, char *));
50 static long binary_get_symtab PARAMS ((bfd *, asymbol **));
51 static asymbol *binary_make_empty_symbol PARAMS ((bfd *));
52 static void binary_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
53 static boolean binary_set_section_contents
54   PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
55 static int binary_sizeof_headers PARAMS ((bfd *, boolean));
56
57 /* Set by external programs - specifies the BFD architecture
58    to use when creating binary BFDs.  */
59 enum bfd_architecture bfd_external_binary_architecture = bfd_arch_unknown;
60
61 /* Create a binary object.  Invoked via bfd_set_format.  */
62
63 static boolean
64 binary_mkobject (abfd)
65      bfd *abfd ATTRIBUTE_UNUSED;
66 {
67   return true;
68 }
69
70 /* Any file may be considered to be a binary file, provided the target
71    was not defaulted.  That is, it must be explicitly specified as
72    being binary.  */
73
74 static const bfd_target *
75 binary_object_p (abfd)
76      bfd *abfd;
77 {
78   struct stat statbuf;
79   asection *sec;
80
81   if (abfd->target_defaulted)
82     {
83       bfd_set_error (bfd_error_wrong_format);
84       return NULL;
85     }
86
87   abfd->symcount = BIN_SYMS;
88
89   /* Find the file size.  */
90   if (bfd_stat (abfd, &statbuf) < 0)
91     {
92       bfd_set_error (bfd_error_system_call);
93       return NULL;
94     }
95
96   /* One data section.  */
97   sec = bfd_make_section (abfd, ".data");
98   if (sec == NULL)
99     return NULL;
100   sec->flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS;
101   sec->vma = 0;
102   sec->_raw_size = statbuf.st_size;
103   sec->filepos = 0;
104
105   abfd->tdata.any = (PTR) sec;
106
107   if (bfd_get_arch_info (abfd) != NULL)
108     {
109       if ((bfd_get_arch_info (abfd)->arch == bfd_arch_unknown)
110           && (bfd_external_binary_architecture != bfd_arch_unknown))
111         bfd_set_arch_info (abfd, bfd_lookup_arch (bfd_external_binary_architecture, 0));
112     }
113
114   return abfd->xvec;
115 }
116
117 #define binary_close_and_cleanup _bfd_generic_close_and_cleanup
118 #define binary_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
119 #define binary_new_section_hook _bfd_generic_new_section_hook
120
121 /* Get contents of the only section.  */
122
123 static boolean
124 binary_get_section_contents (abfd, section, location, offset, count)
125      bfd *abfd;
126      asection *section ATTRIBUTE_UNUSED;
127      PTR location;
128      file_ptr offset;
129      bfd_size_type count;
130 {
131   if (bfd_seek (abfd, offset, SEEK_SET) != 0
132       || bfd_bread (location, count, abfd) != count)
133     return false;
134   return true;
135 }
136
137 /* Return the amount of memory needed to read the symbol table.  */
138
139 static long
140 binary_get_symtab_upper_bound (abfd)
141      bfd *abfd ATTRIBUTE_UNUSED;
142 {
143   return (BIN_SYMS + 1) * sizeof (asymbol *);
144 }
145
146 /* Create a symbol name based on the bfd's filename.  */
147
148 static char *
149 mangle_name (abfd, suffix)
150      bfd *abfd;
151      char *suffix;
152 {
153   bfd_size_type size;
154   char *buf;
155   char *p;
156
157   size = (strlen (bfd_get_filename (abfd))
158           + strlen (suffix)
159           + sizeof "_binary__");
160
161   buf = (char *) bfd_alloc (abfd, size);
162   if (buf == NULL)
163     return "";
164
165   sprintf (buf, "_binary_%s_%s", bfd_get_filename (abfd), suffix);
166
167   /* Change any non-alphanumeric characters to underscores.  */
168   for (p = buf; *p; p++)
169     if (! ISALNUM (*p))
170       *p = '_';
171
172   return buf;
173 }
174
175 /* Return the symbol table.  */
176
177 static long
178 binary_get_symtab (abfd, alocation)
179      bfd *abfd;
180      asymbol **alocation;
181 {
182   asection *sec = (asection *) abfd->tdata.any;
183   asymbol *syms;
184   unsigned int i;
185   bfd_size_type amt = BIN_SYMS * sizeof (asymbol);
186
187   syms = (asymbol *) bfd_alloc (abfd, amt);
188   if (syms == NULL)
189     return false;
190
191   /* Start symbol.  */
192   syms[0].the_bfd = abfd;
193   syms[0].name = mangle_name (abfd, "start");
194   syms[0].value = 0;
195   syms[0].flags = BSF_GLOBAL;
196   syms[0].section = sec;
197   syms[0].udata.p = NULL;
198
199   /* End symbol.  */
200   syms[1].the_bfd = abfd;
201   syms[1].name = mangle_name (abfd, "end");
202   syms[1].value = sec->_raw_size;
203   syms[1].flags = BSF_GLOBAL;
204   syms[1].section = sec;
205   syms[1].udata.p = NULL;
206
207   /* Size symbol.  */
208   syms[2].the_bfd = abfd;
209   syms[2].name = mangle_name (abfd, "size");
210   syms[2].value = sec->_raw_size;
211   syms[2].flags = BSF_GLOBAL;
212   syms[2].section = bfd_abs_section_ptr;
213   syms[2].udata.p = NULL;
214
215   for (i = 0; i < BIN_SYMS; i++)
216     *alocation++ = syms++;
217   *alocation = NULL;
218
219   return BIN_SYMS;
220 }
221
222 /* Make an empty symbol.  */
223
224 static asymbol *
225 binary_make_empty_symbol (abfd)
226      bfd *abfd;
227 {
228   return (asymbol *) bfd_alloc (abfd, (bfd_size_type) sizeof (asymbol));
229 }
230
231 #define binary_print_symbol _bfd_nosymbols_print_symbol
232
233 /* Get information about a symbol.  */
234
235 static void
236 binary_get_symbol_info (ignore_abfd, symbol, ret)
237      bfd *ignore_abfd ATTRIBUTE_UNUSED;
238      asymbol *symbol;
239      symbol_info *ret;
240 {
241   bfd_symbol_info (symbol, ret);
242 }
243
244 #define binary_bfd_is_local_label_name bfd_generic_is_local_label_name
245 #define binary_get_lineno _bfd_nosymbols_get_lineno
246 #define binary_find_nearest_line _bfd_nosymbols_find_nearest_line
247 #define binary_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
248 #define binary_read_minisymbols _bfd_generic_read_minisymbols
249 #define binary_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
250
251 #define binary_get_reloc_upper_bound \
252   ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
253 #define binary_canonicalize_reloc \
254   ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
255 #define binary_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
256
257 /* Set the architecture of a binary file.  */
258 #define binary_set_arch_mach _bfd_generic_set_arch_mach
259
260 /* Write section contents of a binary file.  */
261
262 static boolean
263 binary_set_section_contents (abfd, sec, data, offset, size)
264      bfd *abfd;
265      asection *sec;
266      PTR data;
267      file_ptr offset;
268      bfd_size_type size;
269 {
270   if (size == 0)
271     return true;
272
273   if (! abfd->output_has_begun)
274     {
275       boolean found_low;
276       bfd_vma low;
277       asection *s;
278
279       /* The lowest section LMA sets the virtual address of the start
280          of the file.  We use this to set the file position of all the
281          sections.  */
282       found_low = false;
283       low = 0;
284       for (s = abfd->sections; s != NULL; s = s->next)
285         if (((s->flags
286               & (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_NEVER_LOAD))
287              == (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC))
288             && (s->_raw_size > 0)
289             && (! found_low || s->lma < low))
290           {
291             low = s->lma;
292             found_low = true;
293           }
294
295       for (s = abfd->sections; s != NULL; s = s->next)
296         {
297           s->filepos = s->lma - low;
298
299           /* Skip following warning check for sections that will not
300              occupy file space.  */
301           if ((s->flags
302                & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_NEVER_LOAD))
303               != (SEC_HAS_CONTENTS | SEC_ALLOC)
304               || (s->_raw_size == 0))
305             continue;
306
307           /* If attempting to generate a binary file from a bfd with
308              LMA's all over the place, huge (sparse?) binary files may
309              result.  This condition attempts to detect this situation
310              and print a warning.  Better heuristics would be nice to
311              have.  */
312
313           if (s->filepos < 0)
314             (*_bfd_error_handler)
315               (_("Warning: Writing section `%s' to huge (ie negative) file offset 0x%lx."),
316                bfd_get_section_name (abfd, s),
317                (unsigned long) s->filepos);
318         }
319
320       abfd->output_has_begun = true;
321     }
322
323   /* We don't want to output anything for a section that is neither
324      loaded nor allocated.  The contents of such a section are not
325      meaningful in the binary format.  */
326   if ((sec->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
327     return true;
328   if ((sec->flags & SEC_NEVER_LOAD) != 0)
329     return true;
330
331   return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
332 }
333
334 /* No space is required for header information.  */
335
336 static int
337 binary_sizeof_headers (abfd, exec)
338      bfd *abfd ATTRIBUTE_UNUSED;
339      boolean exec ATTRIBUTE_UNUSED;
340 {
341   return 0;
342 }
343
344 #define binary_bfd_get_relocated_section_contents \
345   bfd_generic_get_relocated_section_contents
346 #define binary_bfd_relax_section bfd_generic_relax_section
347 #define binary_bfd_gc_sections bfd_generic_gc_sections
348 #define binary_bfd_merge_sections bfd_generic_merge_sections
349 #define binary_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
350 #define binary_bfd_link_add_symbols _bfd_generic_link_add_symbols
351 #define binary_bfd_final_link _bfd_generic_final_link
352 #define binary_bfd_link_split_section _bfd_generic_link_split_section
353 #define binary_get_section_contents_in_window \
354   _bfd_generic_get_section_contents_in_window
355
356 const bfd_target binary_vec =
357 {
358   "binary",                     /* name */
359   bfd_target_unknown_flavour,   /* flavour */
360   BFD_ENDIAN_UNKNOWN,           /* byteorder */
361   BFD_ENDIAN_UNKNOWN,           /* header_byteorder */
362   EXEC_P,                       /* object_flags */
363   (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
364    | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
365   0,                            /* symbol_leading_char */
366   ' ',                          /* ar_pad_char */
367   16,                           /* ar_max_namelen */
368   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
369   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
370   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
371   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
372   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
373   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
374   {                             /* bfd_check_format */
375     _bfd_dummy_target,
376     binary_object_p,            /* bfd_check_format */
377     _bfd_dummy_target,
378     _bfd_dummy_target,
379   },
380   {                             /* bfd_set_format */
381     bfd_false,
382     binary_mkobject,
383     bfd_false,
384     bfd_false,
385   },
386   {                             /* bfd_write_contents */
387     bfd_false,
388     bfd_true,
389     bfd_false,
390     bfd_false,
391   },
392
393   BFD_JUMP_TABLE_GENERIC (binary),
394   BFD_JUMP_TABLE_COPY (_bfd_generic),
395   BFD_JUMP_TABLE_CORE (_bfd_nocore),
396   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
397   BFD_JUMP_TABLE_SYMBOLS (binary),
398   BFD_JUMP_TABLE_RELOCS (binary),
399   BFD_JUMP_TABLE_WRITE (binary),
400   BFD_JUMP_TABLE_LINK (binary),
401   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
402
403   NULL,
404
405   NULL
406 };