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