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