/src/binutils-gdb/bfd/binary.c
Line | Count | Source |
1 | | /* BFD back-end for binary objects. |
2 | | Copyright (C) 1994-2026 Free Software Foundation, Inc. |
3 | | Written by Ian Lance Taylor, Cygnus Support, <ian@cygnus.com> |
4 | | |
5 | | This file is part of BFD, the Binary File Descriptor library. |
6 | | |
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 |
9 | | the Free Software Foundation; either version 3 of the License, or |
10 | | (at your option) any later version. |
11 | | |
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. |
16 | | |
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 |
19 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | | MA 02110-1301, USA. */ |
21 | | |
22 | | /* This is a BFD backend which may be used to read or write binary |
23 | | objects. Historically, it was used as an output format for objcopy |
24 | | in order to generate raw binary data, but is now used for other |
25 | | purposes as well. |
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 | | |
35 | | #include "sysdep.h" |
36 | | #include "bfd.h" |
37 | | #include "safe-ctype.h" |
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 | 0 | #define BIN_SYMS 3 |
43 | | |
44 | | char *bfd_binary_symbol_prefix = NULL; |
45 | | |
46 | | /* Create a binary object. Invoked via bfd_set_format. */ |
47 | | |
48 | | static bool |
49 | | binary_mkobject (bfd *abfd ATTRIBUTE_UNUSED) |
50 | 0 | { |
51 | 0 | return true; |
52 | 0 | } |
53 | | |
54 | | /* Any file may be considered to be a binary file, provided the target |
55 | | was not defaulted. That is, it must be explicitly specified as |
56 | | being binary. */ |
57 | | |
58 | | static bfd_cleanup |
59 | | binary_object_p (bfd *abfd) |
60 | 0 | { |
61 | 0 | struct stat statbuf; |
62 | 0 | asection *sec; |
63 | 0 | flagword flags; |
64 | |
|
65 | 0 | if (abfd->target_defaulted) |
66 | 0 | { |
67 | 0 | bfd_set_error (bfd_error_wrong_format); |
68 | 0 | return NULL; |
69 | 0 | } |
70 | | |
71 | 0 | abfd->symcount = BIN_SYMS; |
72 | | |
73 | | /* Find the file size. */ |
74 | 0 | if (bfd_stat (abfd, &statbuf) < 0) |
75 | 0 | { |
76 | 0 | bfd_set_error (bfd_error_system_call); |
77 | 0 | return NULL; |
78 | 0 | } |
79 | | |
80 | | /* One data section. */ |
81 | 0 | flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS; |
82 | 0 | sec = bfd_make_section_with_flags (abfd, ".data", flags); |
83 | 0 | if (sec == NULL) |
84 | 0 | return NULL; |
85 | 0 | sec->vma = 0; |
86 | 0 | sec->size = statbuf.st_size; |
87 | 0 | sec->filepos = 0; |
88 | |
|
89 | 0 | abfd->tdata.any = (void *) sec; |
90 | |
|
91 | 0 | return _bfd_no_cleanup; |
92 | 0 | } |
93 | | |
94 | | #define binary_close_and_cleanup _bfd_generic_close_and_cleanup |
95 | | #define binary_bfd_free_cached_info _bfd_generic_bfd_free_cached_info |
96 | | #define binary_new_section_hook _bfd_generic_new_section_hook |
97 | | |
98 | | /* Get contents of the only section. */ |
99 | | |
100 | | static bool |
101 | | binary_get_section_contents (bfd *abfd, |
102 | | asection *section, |
103 | | void * location, |
104 | | file_ptr offset, |
105 | | bfd_size_type count) |
106 | 0 | { |
107 | 0 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 |
108 | 0 | || bfd_read (location, count, abfd) != count) |
109 | 0 | return false; |
110 | 0 | return true; |
111 | 0 | } |
112 | | |
113 | | /* Return the amount of memory needed to read the symbol table. */ |
114 | | |
115 | | static long |
116 | | binary_get_symtab_upper_bound (bfd *abfd ATTRIBUTE_UNUSED) |
117 | 0 | { |
118 | 0 | return (BIN_SYMS + 1) * sizeof (asymbol *); |
119 | 0 | } |
120 | | |
121 | | /* Create a symbol name based on the bfd's filename. */ |
122 | | |
123 | | static char * |
124 | | mangle_name (bfd *abfd, char *suffix) |
125 | 0 | { |
126 | 0 | bfd_size_type size; |
127 | 0 | const char *prefix; |
128 | 0 | const char *prefix_amend = ""; |
129 | 0 | char *buf; |
130 | 0 | char *p; |
131 | |
|
132 | 0 | prefix = bfd_binary_symbol_prefix; |
133 | 0 | if (prefix == NULL) |
134 | 0 | { |
135 | 0 | prefix = "_binary_"; |
136 | 0 | prefix_amend = bfd_get_filename (abfd); |
137 | 0 | } |
138 | |
|
139 | 0 | size = strlen (prefix) + strlen (prefix_amend) + strlen (suffix) + 2; |
140 | |
|
141 | 0 | buf = (char *) bfd_alloc (abfd, size); |
142 | 0 | if (buf == NULL) |
143 | 0 | return ""; |
144 | | |
145 | 0 | sprintf (buf, "%s%s_%s", prefix, prefix_amend, suffix); |
146 | | |
147 | | /* Change any non-alphanumeric characters to underscores. */ |
148 | 0 | for (p = buf; *p; p++) |
149 | 0 | if (! ISALNUM (*p)) |
150 | 0 | *p = '_'; |
151 | |
|
152 | 0 | return buf; |
153 | 0 | } |
154 | | |
155 | | /* Return the symbol table. */ |
156 | | |
157 | | static long |
158 | | binary_canonicalize_symtab (bfd *abfd, asymbol **alocation) |
159 | 0 | { |
160 | 0 | asection *sec = (asection *) abfd->tdata.any; |
161 | 0 | asymbol *syms; |
162 | 0 | unsigned int i; |
163 | 0 | size_t amt = BIN_SYMS * sizeof (asymbol); |
164 | |
|
165 | 0 | syms = (asymbol *) bfd_alloc (abfd, amt); |
166 | 0 | if (syms == NULL) |
167 | 0 | return -1; |
168 | | |
169 | | /* Start symbol. */ |
170 | 0 | syms[0].the_bfd = abfd; |
171 | 0 | syms[0].name = mangle_name (abfd, "start"); |
172 | 0 | syms[0].value = 0; |
173 | 0 | syms[0].flags = BSF_GLOBAL; |
174 | 0 | syms[0].section = sec; |
175 | 0 | syms[0].udata.p = NULL; |
176 | | |
177 | | /* End symbol. */ |
178 | 0 | syms[1].the_bfd = abfd; |
179 | 0 | syms[1].name = mangle_name (abfd, "end"); |
180 | 0 | syms[1].value = sec->size; |
181 | 0 | syms[1].flags = BSF_GLOBAL; |
182 | 0 | syms[1].section = sec; |
183 | 0 | syms[1].udata.p = NULL; |
184 | | |
185 | | /* Size symbol. */ |
186 | 0 | syms[2].the_bfd = abfd; |
187 | 0 | syms[2].name = mangle_name (abfd, "size"); |
188 | 0 | syms[2].value = sec->size; |
189 | 0 | syms[2].flags = BSF_GLOBAL; |
190 | 0 | syms[2].section = bfd_abs_section_ptr; |
191 | 0 | syms[2].udata.p = NULL; |
192 | |
|
193 | 0 | for (i = 0; i < BIN_SYMS; i++) |
194 | 0 | *alocation++ = syms++; |
195 | 0 | *alocation = NULL; |
196 | |
|
197 | 0 | return BIN_SYMS; |
198 | 0 | } |
199 | | |
200 | | #define binary_make_empty_symbol _bfd_generic_make_empty_symbol |
201 | | #define binary_print_symbol _bfd_nosymbols_print_symbol |
202 | | #define binary_get_symbol_version_string \ |
203 | | _bfd_nosymbols_get_symbol_version_string |
204 | | |
205 | | /* Get information about a symbol. */ |
206 | | |
207 | | static void |
208 | | binary_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED, |
209 | | asymbol *symbol, |
210 | | symbol_info *ret) |
211 | 0 | { |
212 | 0 | bfd_symbol_info (symbol, ret); |
213 | 0 | } |
214 | | |
215 | | #define binary_bfd_is_local_label_name bfd_generic_is_local_label_name |
216 | | #define binary_get_lineno _bfd_nosymbols_get_lineno |
217 | | #define binary_find_nearest_line _bfd_nosymbols_find_nearest_line |
218 | | #define binary_find_nearest_line_with_alt _bfd_nosymbols_find_nearest_line_with_alt |
219 | | #define binary_find_line _bfd_nosymbols_find_line |
220 | | #define binary_find_inliner_info _bfd_nosymbols_find_inliner_info |
221 | | #define binary_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol |
222 | | #define binary_read_minisymbols _bfd_generic_read_minisymbols |
223 | | #define binary_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol |
224 | | #define binary_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false |
225 | | |
226 | | /* Set the architecture of a binary file. */ |
227 | | #define binary_set_arch_mach _bfd_generic_set_arch_mach |
228 | | |
229 | | /* Write section contents of a binary file. */ |
230 | | |
231 | | static bool |
232 | | binary_set_section_contents (bfd *abfd, |
233 | | asection *sec, |
234 | | const void * data, |
235 | | file_ptr offset, |
236 | | bfd_size_type size) |
237 | 0 | { |
238 | 0 | if (size == 0) |
239 | 0 | return true; |
240 | | |
241 | 0 | if (! abfd->output_has_begun) |
242 | 0 | { |
243 | 0 | bool found_low; |
244 | 0 | bfd_vma low; |
245 | 0 | asection *s; |
246 | | |
247 | | /* The lowest section LMA sets the virtual address of the start |
248 | | of the file. We use this to set the file position of all the |
249 | | sections. */ |
250 | 0 | found_low = false; |
251 | 0 | low = 0; |
252 | 0 | for (s = abfd->sections; s != NULL; s = s->next) |
253 | 0 | if (((s->flags |
254 | 0 | & (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_NEVER_LOAD)) |
255 | 0 | == (SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC)) |
256 | 0 | && (s->size > 0) |
257 | 0 | && (! found_low || s->lma < low)) |
258 | 0 | { |
259 | 0 | low = s->lma; |
260 | 0 | found_low = true; |
261 | 0 | } |
262 | |
|
263 | 0 | for (s = abfd->sections; s != NULL; s = s->next) |
264 | 0 | { |
265 | 0 | unsigned int opb = bfd_octets_per_byte (abfd, s); |
266 | |
|
267 | 0 | s->filepos = (s->lma - low) * opb; |
268 | | |
269 | | /* Skip following warning check for sections that will not |
270 | | occupy file space. */ |
271 | 0 | if ((s->flags |
272 | 0 | & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_NEVER_LOAD)) |
273 | 0 | != (SEC_HAS_CONTENTS | SEC_ALLOC) |
274 | 0 | || (s->size == 0)) |
275 | 0 | 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 |
281 | | have. */ |
282 | | |
283 | 0 | if (s->filepos < 0) |
284 | 0 | _bfd_error_handler |
285 | | /* xgettext:c-format */ |
286 | 0 | (_("warning: writing section `%pA' at huge (ie negative) " |
287 | 0 | "file offset"), |
288 | 0 | s); |
289 | 0 | } |
290 | |
|
291 | 0 | abfd->output_has_begun = true; |
292 | 0 | } |
293 | | |
294 | | /* We don't want to output anything for a section that is neither |
295 | | loaded nor allocated. The contents of such a section are not |
296 | | meaningful in the binary format. */ |
297 | 0 | if ((sec->flags & (SEC_LOAD | SEC_ALLOC)) == 0) |
298 | 0 | return true; |
299 | 0 | if ((sec->flags & SEC_NEVER_LOAD) != 0) |
300 | 0 | return true; |
301 | | |
302 | 0 | return _bfd_generic_set_section_contents (abfd, sec, data, offset, size); |
303 | 0 | } |
304 | | |
305 | | #define binary_sizeof_headers _bfd_nolink_sizeof_headers |
306 | | #define binary_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents |
307 | | #define binary_bfd_relax_section bfd_generic_relax_section |
308 | | #define binary_bfd_gc_sections bfd_generic_gc_sections |
309 | | #define binary_bfd_lookup_section_flags bfd_generic_lookup_section_flags |
310 | | #define binary_bfd_is_group_section bfd_generic_is_group_section |
311 | | #define binary_bfd_group_name bfd_generic_group_name |
312 | | #define binary_bfd_discard_group bfd_generic_discard_group |
313 | | #define binary_section_already_linked _bfd_generic_section_already_linked |
314 | | #define binary_bfd_define_common_symbol bfd_generic_define_common_symbol |
315 | | #define binary_bfd_link_hide_symbol _bfd_generic_link_hide_symbol |
316 | | #define binary_bfd_define_start_stop bfd_generic_define_start_stop |
317 | | #define binary_bfd_link_hash_table_create _bfd_generic_link_hash_table_create |
318 | | #define binary_bfd_link_just_syms _bfd_generic_link_just_syms |
319 | | #define binary_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type |
320 | | #define binary_bfd_link_add_symbols _bfd_generic_link_add_symbols |
321 | | #define binary_bfd_final_link _bfd_generic_final_link |
322 | | #define binary_bfd_link_split_section _bfd_generic_link_split_section |
323 | | #define binary_bfd_link_check_relocs _bfd_generic_link_check_relocs |
324 | | |
325 | | const bfd_target binary_vec = |
326 | | { |
327 | | "binary", /* name */ |
328 | | bfd_target_unknown_flavour, /* flavour */ |
329 | | BFD_ENDIAN_UNKNOWN, /* byteorder */ |
330 | | BFD_ENDIAN_UNKNOWN, /* header_byteorder */ |
331 | | EXEC_P, /* object_flags */ |
332 | | (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS |
333 | | | SEC_ALLOC | SEC_LOAD), /* section_flags */ |
334 | | 0, /* symbol_leading_char */ |
335 | | ' ', /* ar_pad_char */ |
336 | | 16, /* ar_max_namelen */ |
337 | | 255, /* match priority. */ |
338 | | TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ |
339 | | TARGET_MERGE_SECTIONS, |
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, |
348 | | binary_object_p, |
349 | | _bfd_dummy_target, |
350 | | _bfd_dummy_target, |
351 | | }, |
352 | | { /* bfd_set_format */ |
353 | | _bfd_bool_bfd_false_error, |
354 | | binary_mkobject, |
355 | | _bfd_bool_bfd_false_error, |
356 | | _bfd_bool_bfd_false_error, |
357 | | }, |
358 | | { /* bfd_write_contents */ |
359 | | _bfd_bool_bfd_false_error, |
360 | | _bfd_bool_bfd_true, |
361 | | _bfd_bool_bfd_false_error, |
362 | | _bfd_bool_bfd_false_error, |
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 (_bfd_norelocs), |
371 | | BFD_JUMP_TABLE_WRITE (binary), |
372 | | BFD_JUMP_TABLE_LINK (binary), |
373 | | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
374 | | |
375 | | NULL, |
376 | | |
377 | | NULL |
378 | | }; |