2001-02-09 Bo Thorsen <bo@suse.de>
[binutils.git] / bfd / binary.c
blob771e4312f730e542c0eae92f8c6b8a5b393fde9c
1 /* BFD back-end for binary objects.
2 Copyright 1994, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support, <ian@cygnus.com>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This is a BFD backend which may be used to write binary objects.
22 It may only be used for output, not input. The intention is that
23 this may be used as an output format for objcopy in order to
24 generate raw binary data.
26 This is very simple. The only complication is that the real data
27 will start at some address X, and in some cases we will not want to
28 include X zeroes just to get to that point. Since the start
29 address is not meaningful for this object file format, we use it
30 instead to indicate the number of zeroes to skip at the start of
31 the file. objcopy cooperates by specially setting the start
32 address to zero by default. */
34 #include <ctype.h>
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "libbfd.h"
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
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));
57 /* Create a binary object. Invoked via bfd_set_format. */
59 static boolean
60 binary_mkobject (abfd)
61 bfd *abfd ATTRIBUTE_UNUSED;
63 return true;
66 /* Any file may be considered to be a binary file, provided the target
67 was not defaulted. That is, it must be explicitly specified as
68 being binary. */
70 static const bfd_target *
71 binary_object_p (abfd)
72 bfd *abfd;
74 struct stat statbuf;
75 asection *sec;
77 if (abfd->target_defaulted)
79 bfd_set_error (bfd_error_wrong_format);
80 return NULL;
83 abfd->symcount = BIN_SYMS;
85 /* Find the file size. */
86 if (bfd_stat (abfd, &statbuf) < 0)
88 bfd_set_error (bfd_error_system_call);
89 return NULL;
92 /* One data section. */
93 sec = bfd_make_section (abfd, ".data");
94 if (sec == NULL)
95 return NULL;
96 sec->flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS;
97 sec->vma = 0;
98 sec->_raw_size = statbuf.st_size;
99 sec->filepos = 0;
101 abfd->tdata.any = (PTR) sec;
103 return abfd->xvec;
106 #define binary_close_and_cleanup _bfd_generic_close_and_cleanup
107 #define binary_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
108 #define binary_new_section_hook _bfd_generic_new_section_hook
110 /* Get contents of the only section. */
112 static boolean
113 binary_get_section_contents (abfd, section, location, offset, count)
114 bfd *abfd;
115 asection *section ATTRIBUTE_UNUSED;
116 PTR location;
117 file_ptr offset;
118 bfd_size_type count;
120 if (bfd_seek (abfd, offset, SEEK_SET) != 0
121 || bfd_read (location, 1, count, abfd) != count)
122 return false;
123 return true;
126 /* Return the amount of memory needed to read the symbol table. */
128 static long
129 binary_get_symtab_upper_bound (abfd)
130 bfd *abfd ATTRIBUTE_UNUSED;
132 return (BIN_SYMS + 1) * sizeof (asymbol *);
135 /* Create a symbol name based on the bfd's filename. */
137 static char *
138 mangle_name (abfd, suffix)
139 bfd *abfd;
140 char *suffix;
142 int size;
143 char *buf;
144 char *p;
146 size = (strlen (bfd_get_filename (abfd))
147 + strlen (suffix)
148 + sizeof "_binary__");
150 buf = (char *) bfd_alloc (abfd, size);
151 if (buf == NULL)
152 return "";
154 sprintf (buf, "_binary_%s_%s", bfd_get_filename (abfd), suffix);
156 /* Change any non-alphanumeric characters to underscores. */
157 for (p = buf; *p; p++)
158 if (! isalnum ((unsigned char) *p))
159 *p = '_';
161 return buf;
164 /* Return the symbol table. */
166 static long
167 binary_get_symtab (abfd, alocation)
168 bfd *abfd;
169 asymbol **alocation;
171 asection *sec = (asection *) abfd->tdata.any;
172 asymbol *syms;
173 unsigned int i;
175 syms = (asymbol *) bfd_alloc (abfd, BIN_SYMS * sizeof (asymbol));
176 if (syms == NULL)
177 return false;
179 /* Start symbol. */
180 syms[0].the_bfd = abfd;
181 syms[0].name = mangle_name (abfd, "start");
182 syms[0].value = 0;
183 syms[0].flags = BSF_GLOBAL;
184 syms[0].section = sec;
185 syms[0].udata.p = NULL;
187 /* End symbol. */
188 syms[1].the_bfd = abfd;
189 syms[1].name = mangle_name (abfd, "end");
190 syms[1].value = sec->_raw_size;
191 syms[1].flags = BSF_GLOBAL;
192 syms[1].section = sec;
193 syms[1].udata.p = NULL;
195 /* Size symbol. */
196 syms[2].the_bfd = abfd;
197 syms[2].name = mangle_name (abfd, "size");
198 syms[2].value = sec->_raw_size;
199 syms[2].flags = BSF_GLOBAL;
200 syms[2].section = bfd_abs_section_ptr;
201 syms[2].udata.p = NULL;
203 for (i = 0; i < BIN_SYMS; i++)
204 *alocation++ = syms++;
205 *alocation = NULL;
207 return BIN_SYMS;
210 /* Make an empty symbol. */
212 static asymbol *
213 binary_make_empty_symbol (abfd)
214 bfd *abfd;
216 return (asymbol *) bfd_alloc (abfd, sizeof (asymbol));
219 #define binary_print_symbol _bfd_nosymbols_print_symbol
221 /* Get information about a symbol. */
223 static void
224 binary_get_symbol_info (ignore_abfd, symbol, ret)
225 bfd *ignore_abfd ATTRIBUTE_UNUSED;
226 asymbol *symbol;
227 symbol_info *ret;
229 bfd_symbol_info (symbol, ret);
232 #define binary_bfd_is_local_label_name bfd_generic_is_local_label_name
233 #define binary_get_lineno _bfd_nosymbols_get_lineno
234 #define binary_find_nearest_line _bfd_nosymbols_find_nearest_line
235 #define binary_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
236 #define binary_read_minisymbols _bfd_generic_read_minisymbols
237 #define binary_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
239 #define binary_get_reloc_upper_bound \
240 ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
241 #define binary_canonicalize_reloc \
242 ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
243 #define binary_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
245 /* Set the architecture of a binary file. */
246 #define binary_set_arch_mach _bfd_generic_set_arch_mach
248 /* Write section contents of a binary file. */
250 static boolean
251 binary_set_section_contents (abfd, sec, data, offset, size)
252 bfd *abfd;
253 asection *sec;
254 PTR data;
255 file_ptr offset;
256 bfd_size_type size;
258 if (size == 0)
259 return true;
261 if (! abfd->output_has_begun)
263 boolean found_low;
264 bfd_vma low;
265 asection *s;
267 /* The lowest section LMA sets the virtual address of the start
268 of the file. We use this to set the file position of all the
269 sections. */
270 found_low = false;
271 low = 0;
272 for (s = abfd->sections; s != NULL; s = s->next)
273 if (((s->flags
274 & (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_NEVER_LOAD))
275 == (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC))
276 && (s->_raw_size > 0)
277 && (! found_low || s->lma < low))
279 low = s->lma;
280 found_low = true;
283 for (s = abfd->sections; s != NULL; s = s->next)
285 s->filepos = s->lma - low;
287 /* Skip following warning check for sections that will not
288 occupy file space. */
289 if ((s->flags
290 & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_NEVER_LOAD))
291 != (SEC_HAS_CONTENTS | SEC_ALLOC)
292 || (s->_raw_size == 0))
293 continue;
295 /* If attempting to generate a binary file from a bfd with
296 LMA's all over the place, huge (sparse?) binary files may
297 result. This condition attempts to detect this situation
298 and print a warning. Better heuristics would be nice to
299 have. */
301 if (s->filepos < 0)
302 (*_bfd_error_handler)
303 (_("Warning: Writing section `%s' to huge (ie negative) file offset 0x%lx."),
304 bfd_get_section_name (abfd, s),
305 (unsigned long) s->filepos);
308 abfd->output_has_begun = true;
311 /* We don't want to output anything for a section that is neither
312 loaded nor allocated. The contents of such a section are not
313 meaningful in the binary format. */
314 if ((sec->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
315 return true;
316 if ((sec->flags & SEC_NEVER_LOAD) != 0)
317 return true;
319 return _bfd_generic_set_section_contents (abfd, sec, data, offset, size);
322 /* No space is required for header information. */
324 static int
325 binary_sizeof_headers (abfd, exec)
326 bfd *abfd ATTRIBUTE_UNUSED;
327 boolean exec ATTRIBUTE_UNUSED;
329 return 0;
332 #define binary_bfd_get_relocated_section_contents \
333 bfd_generic_get_relocated_section_contents
334 #define binary_bfd_relax_section bfd_generic_relax_section
335 #define binary_bfd_gc_sections bfd_generic_gc_sections
336 #define binary_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
337 #define binary_bfd_link_add_symbols _bfd_generic_link_add_symbols
338 #define binary_bfd_final_link _bfd_generic_final_link
339 #define binary_bfd_link_split_section _bfd_generic_link_split_section
340 #define binary_get_section_contents_in_window \
341 _bfd_generic_get_section_contents_in_window
343 const bfd_target binary_vec =
345 "binary", /* name */
346 bfd_target_unknown_flavour, /* flavour */
347 BFD_ENDIAN_UNKNOWN, /* byteorder */
348 BFD_ENDIAN_UNKNOWN, /* header_byteorder */
349 EXEC_P, /* object_flags */
350 (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
351 | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
352 0, /* symbol_leading_char */
353 ' ', /* ar_pad_char */
354 16, /* ar_max_namelen */
355 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
356 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
357 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
358 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
359 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
360 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
361 { /* bfd_check_format */
362 _bfd_dummy_target,
363 binary_object_p, /* bfd_check_format */
364 _bfd_dummy_target,
365 _bfd_dummy_target,
367 { /* bfd_set_format */
368 bfd_false,
369 binary_mkobject,
370 bfd_false,
371 bfd_false,
373 { /* bfd_write_contents */
374 bfd_false,
375 bfd_true,
376 bfd_false,
377 bfd_false,
380 BFD_JUMP_TABLE_GENERIC (binary),
381 BFD_JUMP_TABLE_COPY (_bfd_generic),
382 BFD_JUMP_TABLE_CORE (_bfd_nocore),
383 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
384 BFD_JUMP_TABLE_SYMBOLS (binary),
385 BFD_JUMP_TABLE_RELOCS (binary),
386 BFD_JUMP_TABLE_WRITE (binary),
387 BFD_JUMP_TABLE_LINK (binary),
388 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
390 NULL,
392 NULL