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