Touches most files in bfd/, so likely will be blamed for everything..
[binutils.git] / bfd / binary.c
blob4d2fe03d6d879867f9fb35d5664d3742f82a8114
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>
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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.
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. */
35 #include <ctype.h>
37 #include "bfd.h"
38 #include "sysdep.h"
39 #include "libbfd.h"
41 /* Any bfd we create by reading a binary file has three symbols:
42 a start symbol, an end symbol, and an absolute length symbol. */
43 #define BIN_SYMS 3
45 static boolean binary_mkobject PARAMS ((bfd *));
46 static const bfd_target *binary_object_p PARAMS ((bfd *));
47 static boolean binary_get_section_contents
48 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
49 static long binary_get_symtab_upper_bound PARAMS ((bfd *));
50 static char *mangle_name PARAMS ((bfd *, char *));
51 static long binary_get_symtab PARAMS ((bfd *, asymbol **));
52 static asymbol *binary_make_empty_symbol PARAMS ((bfd *));
53 static void binary_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
54 static boolean binary_set_section_contents
55 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
56 static int binary_sizeof_headers PARAMS ((bfd *, boolean));
58 /* Set by external programs - specifies the BFD architecture
59 to use when creating binary BFDs. */
60 enum bfd_architecture bfd_external_binary_architecture = bfd_arch_unknown;
62 /* Create a binary object. Invoked via bfd_set_format. */
64 static boolean
65 binary_mkobject (abfd)
66 bfd *abfd ATTRIBUTE_UNUSED;
68 return true;
71 /* Any file may be considered to be a binary file, provided the target
72 was not defaulted. That is, it must be explicitly specified as
73 being binary. */
75 static const bfd_target *
76 binary_object_p (abfd)
77 bfd *abfd;
79 struct stat statbuf;
80 asection *sec;
82 if (abfd->target_defaulted)
84 bfd_set_error (bfd_error_wrong_format);
85 return NULL;
88 abfd->symcount = BIN_SYMS;
90 /* Find the file size. */
91 if (bfd_stat (abfd, &statbuf) < 0)
93 bfd_set_error (bfd_error_system_call);
94 return NULL;
97 /* One data section. */
98 sec = bfd_make_section (abfd, ".data");
99 if (sec == NULL)
100 return NULL;
101 sec->flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS;
102 sec->vma = 0;
103 sec->_raw_size = statbuf.st_size;
104 sec->filepos = 0;
106 abfd->tdata.any = (PTR) sec;
108 if (bfd_get_arch_info (abfd) != NULL)
110 if ((bfd_get_arch_info (abfd)->arch == bfd_arch_unknown)
111 && (bfd_external_binary_architecture != bfd_arch_unknown))
112 bfd_set_arch_info (abfd, bfd_lookup_arch (bfd_external_binary_architecture, 0));
115 return abfd->xvec;
118 #define binary_close_and_cleanup _bfd_generic_close_and_cleanup
119 #define binary_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
120 #define binary_new_section_hook _bfd_generic_new_section_hook
122 /* Get contents of the only section. */
124 static boolean
125 binary_get_section_contents (abfd, section, location, offset, count)
126 bfd *abfd;
127 asection *section ATTRIBUTE_UNUSED;
128 PTR location;
129 file_ptr offset;
130 bfd_size_type count;
132 if (bfd_seek (abfd, offset, SEEK_SET) != 0
133 || bfd_bread (location, count, abfd) != count)
134 return false;
135 return true;
138 /* Return the amount of memory needed to read the symbol table. */
140 static long
141 binary_get_symtab_upper_bound (abfd)
142 bfd *abfd ATTRIBUTE_UNUSED;
144 return (BIN_SYMS + 1) * sizeof (asymbol *);
147 /* Create a symbol name based on the bfd's filename. */
149 static char *
150 mangle_name (abfd, suffix)
151 bfd *abfd;
152 char *suffix;
154 bfd_size_type size;
155 char *buf;
156 char *p;
158 size = (strlen (bfd_get_filename (abfd))
159 + strlen (suffix)
160 + sizeof "_binary__");
162 buf = (char *) bfd_alloc (abfd, size);
163 if (buf == NULL)
164 return "";
166 sprintf (buf, "_binary_%s_%s", bfd_get_filename (abfd), suffix);
168 /* Change any non-alphanumeric characters to underscores. */
169 for (p = buf; *p; p++)
170 if (! isalnum ((unsigned char) *p))
171 *p = '_';
173 return buf;
176 /* Return the symbol table. */
178 static long
179 binary_get_symtab (abfd, alocation)
180 bfd *abfd;
181 asymbol **alocation;
183 asection *sec = (asection *) abfd->tdata.any;
184 asymbol *syms;
185 unsigned int i;
186 bfd_size_type amt = BIN_SYMS * sizeof (asymbol);
188 syms = (asymbol *) bfd_alloc (abfd, amt);
189 if (syms == NULL)
190 return false;
192 /* Start symbol. */
193 syms[0].the_bfd = abfd;
194 syms[0].name = mangle_name (abfd, "start");
195 syms[0].value = 0;
196 syms[0].flags = BSF_GLOBAL;
197 syms[0].section = sec;
198 syms[0].udata.p = NULL;
200 /* End symbol. */
201 syms[1].the_bfd = abfd;
202 syms[1].name = mangle_name (abfd, "end");
203 syms[1].value = sec->_raw_size;
204 syms[1].flags = BSF_GLOBAL;
205 syms[1].section = sec;
206 syms[1].udata.p = NULL;
208 /* Size symbol. */
209 syms[2].the_bfd = abfd;
210 syms[2].name = mangle_name (abfd, "size");
211 syms[2].value = sec->_raw_size;
212 syms[2].flags = BSF_GLOBAL;
213 syms[2].section = bfd_abs_section_ptr;
214 syms[2].udata.p = NULL;
216 for (i = 0; i < BIN_SYMS; i++)
217 *alocation++ = syms++;
218 *alocation = NULL;
220 return BIN_SYMS;
223 /* Make an empty symbol. */
225 static asymbol *
226 binary_make_empty_symbol (abfd)
227 bfd *abfd;
229 return (asymbol *) bfd_alloc (abfd, (bfd_size_type) sizeof (asymbol));
232 #define binary_print_symbol _bfd_nosymbols_print_symbol
234 /* Get information about a symbol. */
236 static void
237 binary_get_symbol_info (ignore_abfd, symbol, ret)
238 bfd *ignore_abfd ATTRIBUTE_UNUSED;
239 asymbol *symbol;
240 symbol_info *ret;
242 bfd_symbol_info (symbol, ret);
245 #define binary_bfd_is_local_label_name bfd_generic_is_local_label_name
246 #define binary_get_lineno _bfd_nosymbols_get_lineno
247 #define binary_find_nearest_line _bfd_nosymbols_find_nearest_line
248 #define binary_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
249 #define binary_read_minisymbols _bfd_generic_read_minisymbols
250 #define binary_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
252 #define binary_get_reloc_upper_bound \
253 ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
254 #define binary_canonicalize_reloc \
255 ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
256 #define binary_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
258 /* Set the architecture of a binary file. */
259 #define binary_set_arch_mach _bfd_generic_set_arch_mach
261 /* Write section contents of a binary file. */
263 static boolean
264 binary_set_section_contents (abfd, sec, data, offset, size)
265 bfd *abfd;
266 asection *sec;
267 PTR data;
268 file_ptr offset;
269 bfd_size_type size;
271 if (size == 0)
272 return true;
274 if (! abfd->output_has_begun)
276 boolean found_low;
277 bfd_vma low;
278 asection *s;
280 /* The lowest section LMA sets the virtual address of the start
281 of the file. We use this to set the file position of all the
282 sections. */
283 found_low = false;
284 low = 0;
285 for (s = abfd->sections; s != NULL; s = s->next)
286 if (((s->flags
287 & (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_NEVER_LOAD))
288 == (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC))
289 && (s->_raw_size > 0)
290 && (! found_low || s->lma < low))
292 low = s->lma;
293 found_low = true;
296 for (s = abfd->sections; s != NULL; s = s->next)
298 s->filepos = s->lma - low;
300 /* Skip following warning check for sections that will not
301 occupy file space. */
302 if ((s->flags
303 & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_NEVER_LOAD))
304 != (SEC_HAS_CONTENTS | SEC_ALLOC)
305 || (s->_raw_size == 0))
306 continue;
308 /* If attempting to generate a binary file from a bfd with
309 LMA's all over the place, huge (sparse?) binary files may
310 result. This condition attempts to detect this situation
311 and print a warning. Better heuristics would be nice to
312 have. */
314 if (s->filepos < 0)
315 (*_bfd_error_handler)
316 (_("Warning: Writing section `%s' to huge (ie negative) file offset 0x%lx."),
317 bfd_get_section_name (abfd, s),
318 (unsigned long) s->filepos);
321 abfd->output_has_begun = true;
324 /* We don't want to output anything for a section that is neither
325 loaded nor allocated. The contents of such a section are not
326 meaningful in the binary format. */
327 if ((sec->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
328 return true;
329 if ((sec->flags & SEC_NEVER_LOAD) != 0)
330 return true;
332 return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
335 /* No space is required for header information. */
337 static int
338 binary_sizeof_headers (abfd, exec)
339 bfd *abfd ATTRIBUTE_UNUSED;
340 boolean exec ATTRIBUTE_UNUSED;
342 return 0;
345 #define binary_bfd_get_relocated_section_contents \
346 bfd_generic_get_relocated_section_contents
347 #define binary_bfd_relax_section bfd_generic_relax_section
348 #define binary_bfd_gc_sections bfd_generic_gc_sections
349 #define binary_bfd_merge_sections bfd_generic_merge_sections
350 #define binary_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
351 #define binary_bfd_link_add_symbols _bfd_generic_link_add_symbols
352 #define binary_bfd_final_link _bfd_generic_final_link
353 #define binary_bfd_link_split_section _bfd_generic_link_split_section
354 #define binary_get_section_contents_in_window \
355 _bfd_generic_get_section_contents_in_window
357 const bfd_target binary_vec =
359 "binary", /* name */
360 bfd_target_unknown_flavour, /* flavour */
361 BFD_ENDIAN_UNKNOWN, /* byteorder */
362 BFD_ENDIAN_UNKNOWN, /* header_byteorder */
363 EXEC_P, /* object_flags */
364 (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
365 | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
366 0, /* symbol_leading_char */
367 ' ', /* ar_pad_char */
368 16, /* ar_max_namelen */
369 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
370 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
371 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
372 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
373 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
374 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
375 { /* bfd_check_format */
376 _bfd_dummy_target,
377 binary_object_p, /* bfd_check_format */
378 _bfd_dummy_target,
379 _bfd_dummy_target,
381 { /* bfd_set_format */
382 bfd_false,
383 binary_mkobject,
384 bfd_false,
385 bfd_false,
387 { /* bfd_write_contents */
388 bfd_false,
389 bfd_true,
390 bfd_false,
391 bfd_false,
394 BFD_JUMP_TABLE_GENERIC (binary),
395 BFD_JUMP_TABLE_COPY (_bfd_generic),
396 BFD_JUMP_TABLE_CORE (_bfd_nocore),
397 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
398 BFD_JUMP_TABLE_SYMBOLS (binary),
399 BFD_JUMP_TABLE_RELOCS (binary),
400 BFD_JUMP_TABLE_WRITE (binary),
401 BFD_JUMP_TABLE_LINK (binary),
402 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
404 NULL,
406 NULL