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