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