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