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