1/* BFD back-end for binary objects.
2   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3   2004, 2005, 2006 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 "bfd.h"
36#include "sysdep.h"
37#include "safe-ctype.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/* Set by external programs - specifies the BFD architecture and
45   machine number to be uses when creating binary BFDs.  */
46enum bfd_architecture  bfd_external_binary_architecture = bfd_arch_unknown;
47unsigned long          bfd_external_machine = 0;
48
49/* Create a binary object.  Invoked via bfd_set_format.  */
50
51static bfd_boolean
52binary_mkobject (bfd *abfd ATTRIBUTE_UNUSED)
53{
54  return TRUE;
55}
56
57/* Any file may be considered to be a binary file, provided the target
58   was not defaulted.  That is, it must be explicitly specified as
59   being binary.  */
60
61static const bfd_target *
62binary_object_p (bfd *abfd)
63{
64  struct stat statbuf;
65  asection *sec;
66  flagword flags;
67
68  if (abfd->target_defaulted)
69    {
70      bfd_set_error (bfd_error_wrong_format);
71      return NULL;
72    }
73
74  abfd->symcount = BIN_SYMS;
75
76  /* Find the file size.  */
77  if (bfd_stat (abfd, &statbuf) < 0)
78    {
79      bfd_set_error (bfd_error_system_call);
80      return NULL;
81    }
82
83  /* One data section.  */
84  flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS;
85  sec = bfd_make_section_with_flags (abfd, ".data", flags);
86  if (sec == NULL)
87    return NULL;
88  sec->vma = 0;
89  sec->size = statbuf.st_size;
90  sec->filepos = 0;
91
92  abfd->tdata.any = (void *) sec;
93
94  if (bfd_get_arch_info (abfd) != NULL)
95    {
96      if ((bfd_get_arch_info (abfd)->arch == bfd_arch_unknown)
97          && (bfd_external_binary_architecture != bfd_arch_unknown))
98        bfd_set_arch_info (abfd, bfd_lookup_arch
99			   (bfd_external_binary_architecture, bfd_external_machine));
100    }
101
102  return abfd->xvec;
103}
104
105#define binary_close_and_cleanup     _bfd_generic_close_and_cleanup
106#define binary_bfd_free_cached_info  _bfd_generic_bfd_free_cached_info
107#define binary_new_section_hook      _bfd_generic_new_section_hook
108
109/* Get contents of the only section.  */
110
111static bfd_boolean
112binary_get_section_contents (bfd *abfd,
113			     asection *section ATTRIBUTE_UNUSED,
114			     void * location,
115			     file_ptr offset,
116			     bfd_size_type count)
117{
118  if (bfd_seek (abfd, offset, SEEK_SET) != 0
119      || bfd_bread (location, count, abfd) != count)
120    return FALSE;
121  return TRUE;
122}
123
124/* Return the amount of memory needed to read the symbol table.  */
125
126static long
127binary_get_symtab_upper_bound (bfd *abfd ATTRIBUTE_UNUSED)
128{
129  return (BIN_SYMS + 1) * sizeof (asymbol *);
130}
131
132/* Create a symbol name based on the bfd's filename.  */
133
134static char *
135mangle_name (bfd *abfd, char *suffix)
136{
137  bfd_size_type size;
138  char *buf;
139  char *p;
140
141  size = (strlen (bfd_get_filename (abfd))
142	  + strlen (suffix)
143	  + sizeof "_binary__");
144
145  buf = bfd_alloc (abfd, size);
146  if (buf == NULL)
147    return "";
148
149  sprintf (buf, "_binary_%s_%s", bfd_get_filename (abfd), suffix);
150
151  /* Change any non-alphanumeric characters to underscores.  */
152  for (p = buf; *p; p++)
153    if (! ISALNUM (*p))
154      *p = '_';
155
156  return buf;
157}
158
159/* Return the symbol table.  */
160
161static long
162binary_canonicalize_symtab (bfd *abfd, asymbol **alocation)
163{
164  asection *sec = (asection *) abfd->tdata.any;
165  asymbol *syms;
166  unsigned int i;
167  bfd_size_type amt = BIN_SYMS * sizeof (asymbol);
168
169  syms = bfd_alloc (abfd, amt);
170  if (syms == NULL)
171    return 0;
172
173  /* Start symbol.  */
174  syms[0].the_bfd = abfd;
175  syms[0].name = mangle_name (abfd, "start");
176  syms[0].value = 0;
177  syms[0].flags = BSF_GLOBAL;
178  syms[0].section = sec;
179  syms[0].udata.p = NULL;
180
181  /* End symbol.  */
182  syms[1].the_bfd = abfd;
183  syms[1].name = mangle_name (abfd, "end");
184  syms[1].value = sec->size;
185  syms[1].flags = BSF_GLOBAL;
186  syms[1].section = sec;
187  syms[1].udata.p = NULL;
188
189  /* Size symbol.  */
190  syms[2].the_bfd = abfd;
191  syms[2].name = mangle_name (abfd, "size");
192  syms[2].value = sec->size;
193  syms[2].flags = BSF_GLOBAL;
194  syms[2].section = bfd_abs_section_ptr;
195  syms[2].udata.p = NULL;
196
197  for (i = 0; i < BIN_SYMS; i++)
198    *alocation++ = syms++;
199  *alocation = NULL;
200
201  return BIN_SYMS;
202}
203
204#define binary_make_empty_symbol  _bfd_generic_make_empty_symbol
205#define binary_print_symbol       _bfd_nosymbols_print_symbol
206
207/* Get information about a symbol.  */
208
209static void
210binary_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
211			asymbol *symbol,
212			symbol_info *ret)
213{
214  bfd_symbol_info (symbol, ret);
215}
216
217#define binary_bfd_is_local_label_name      bfd_generic_is_local_label_name
218#define binary_get_lineno                  _bfd_nosymbols_get_lineno
219#define binary_find_nearest_line           _bfd_nosymbols_find_nearest_line
220#define binary_find_inliner_info           _bfd_nosymbols_find_inliner_info
221#define binary_bfd_make_debug_symbol       _bfd_nosymbols_bfd_make_debug_symbol
222#define binary_read_minisymbols            _bfd_generic_read_minisymbols
223#define binary_minisymbol_to_symbol        _bfd_generic_minisymbol_to_symbol
224#define binary_bfd_reloc_type_lookup       _bfd_norelocs_bfd_reloc_type_lookup
225#define binary_get_reloc_upper_bound        ((long (*) (bfd *, asection *)) bfd_0l)
226#define binary_canonicalize_reloc           ((long (*) (bfd *, asection *, arelent **, asymbol **)) bfd_0l)
227#define binary_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
228
229/* Set the architecture of a binary file.  */
230#define binary_set_arch_mach _bfd_generic_set_arch_mach
231
232/* Write section contents of a binary file.  */
233
234static bfd_boolean
235binary_set_section_contents (bfd *abfd,
236			     asection *sec,
237			     const void * data,
238			     file_ptr offset,
239			     bfd_size_type size)
240{
241  if (size == 0)
242    return TRUE;
243
244  if (! abfd->output_has_begun)
245    {
246      bfd_boolean found_low;
247      bfd_vma low;
248      asection *s;
249
250      /* The lowest section LMA sets the virtual address of the start
251         of the file.  We use this to set the file position of all the
252         sections.  */
253      found_low = FALSE;
254      low = 0;
255      for (s = abfd->sections; s != NULL; s = s->next)
256	if (((s->flags
257	      & (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_NEVER_LOAD))
258	     == (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC))
259	    && (s->size > 0)
260	    && (! found_low || s->lma < low))
261	  {
262	    low = s->lma;
263	    found_low = TRUE;
264	  }
265
266      for (s = abfd->sections; s != NULL; s = s->next)
267	{
268	  s->filepos = s->lma - low;
269
270	  /* Skip following warning check for sections that will not
271	     occupy file space.  */
272	  if ((s->flags
273	       & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_NEVER_LOAD))
274	      != (SEC_HAS_CONTENTS | SEC_ALLOC)
275	      || (s->size == 0))
276	    continue;
277
278	  /* If attempting to generate a binary file from a bfd with
279	     LMA's all over the place, huge (sparse?) binary files may
280	     result.  This condition attempts to detect this situation
281	     and print a warning.  Better heuristics would be nice to
282	     have.  */
283
284	  if (s->filepos < 0)
285	    (*_bfd_error_handler)
286	      (_("Warning: Writing section `%s' to huge (ie negative) file offset 0x%lx."),
287	       bfd_get_section_name (abfd, s),
288	       (unsigned long) s->filepos);
289	}
290
291      abfd->output_has_begun = TRUE;
292    }
293
294  /* We don't want to output anything for a section that is neither
295     loaded nor allocated.  The contents of such a section are not
296     meaningful in the binary format.  */
297  if ((sec->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
298    return TRUE;
299  if ((sec->flags & SEC_NEVER_LOAD) != 0)
300    return TRUE;
301
302  return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
303}
304
305/* No space is required for header information.  */
306
307static int
308binary_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
309		       struct bfd_link_info *info ATTRIBUTE_UNUSED)
310{
311  return 0;
312}
313
314#define binary_bfd_get_relocated_section_contents  bfd_generic_get_relocated_section_contents
315#define binary_bfd_relax_section                   bfd_generic_relax_section
316#define binary_bfd_gc_sections                     bfd_generic_gc_sections
317#define binary_bfd_merge_sections                  bfd_generic_merge_sections
318#define binary_bfd_is_group_section                bfd_generic_is_group_section
319#define binary_bfd_discard_group                   bfd_generic_discard_group
320#define binary_section_already_linked             _bfd_generic_section_already_linked
321#define binary_bfd_link_hash_table_create         _bfd_generic_link_hash_table_create
322#define binary_bfd_link_hash_table_free           _bfd_generic_link_hash_table_free
323#define binary_bfd_link_just_syms                 _bfd_generic_link_just_syms
324#define binary_bfd_link_add_symbols               _bfd_generic_link_add_symbols
325#define binary_bfd_final_link                     _bfd_generic_final_link
326#define binary_bfd_link_split_section             _bfd_generic_link_split_section
327#define binary_get_section_contents_in_window     _bfd_generic_get_section_contents_in_window
328
329const bfd_target binary_vec =
330{
331  "binary",			/* name */
332  bfd_target_unknown_flavour,	/* flavour */
333  BFD_ENDIAN_UNKNOWN,		/* byteorder */
334  BFD_ENDIAN_UNKNOWN,		/* header_byteorder */
335  EXEC_P,			/* object_flags */
336  (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
337   | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
338  0,				/* symbol_leading_char */
339  ' ',				/* ar_pad_char */
340  16,				/* ar_max_namelen */
341  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
342  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
343  bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* data */
344  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
345  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
346  bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* hdrs */
347  {				/* bfd_check_format */
348    _bfd_dummy_target,
349    binary_object_p,
350    _bfd_dummy_target,
351    _bfd_dummy_target,
352  },
353  {				/* bfd_set_format */
354    bfd_false,
355    binary_mkobject,
356    bfd_false,
357    bfd_false,
358  },
359  {				/* bfd_write_contents */
360    bfd_false,
361    bfd_true,
362    bfd_false,
363    bfd_false,
364  },
365
366  BFD_JUMP_TABLE_GENERIC (binary),
367  BFD_JUMP_TABLE_COPY (_bfd_generic),
368  BFD_JUMP_TABLE_CORE (_bfd_nocore),
369  BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
370  BFD_JUMP_TABLE_SYMBOLS (binary),
371  BFD_JUMP_TABLE_RELOCS (binary),
372  BFD_JUMP_TABLE_WRITE (binary),
373  BFD_JUMP_TABLE_LINK (binary),
374  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
375
376  NULL,
377
378  NULL
379};
380