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