OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.18.50/] [bfd/] [binary.c] - Blame information for rev 493

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.