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