OSDN Git Service

Use bfd_simple_get_relocated_section_contents() instead of bfd_get_section_contents().
[pf3gnuchains/pf3gnuchains3x.git] / bfd / simple.c
1 /* simple.c -- BFD simple client routines
2    Copyright 2002
3    Free Software Foundation, Inc.
4    Contributed by MontaVista Software, Inc.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26
27 static boolean simple_dummy_warning
28   PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *,
29            asection *, bfd_vma));
30
31 static boolean simple_dummy_undefined_symbol
32   PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *,
33            bfd_vma, boolean));
34
35 static boolean simple_dummy_reloc_overflow 
36   PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma,
37            bfd *, asection *, bfd_vma));
38
39 static boolean simple_dummy_reloc_dangerous
40   PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
41
42 static boolean simple_dummy_unattached_reloc
43   PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
44
45 bfd_byte * bfd_simple_get_relocated_section_contents
46   PARAMS ((bfd *, asection *, bfd_byte *));
47
48 static boolean
49 simple_dummy_warning (link_info, warning, symbol, abfd, section, address)
50      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
51      const char *warning ATTRIBUTE_UNUSED;
52      const char *symbol ATTRIBUTE_UNUSED;
53      bfd *abfd ATTRIBUTE_UNUSED;
54      asection *section ATTRIBUTE_UNUSED;
55      bfd_vma address ATTRIBUTE_UNUSED;
56 {
57   return true;
58 }
59
60 static boolean
61 simple_dummy_undefined_symbol (link_info, name, abfd, section, address, fatal)
62      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
63      const char *name ATTRIBUTE_UNUSED;
64      bfd *abfd ATTRIBUTE_UNUSED;
65      asection *section ATTRIBUTE_UNUSED;
66      bfd_vma address ATTRIBUTE_UNUSED;
67      boolean fatal ATTRIBUTE_UNUSED;
68 {
69   return true;
70 }
71
72 static boolean
73 simple_dummy_reloc_overflow (link_info, name, reloc_name, addend, abfd,
74                              section, address)
75      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
76      const char *name ATTRIBUTE_UNUSED;
77      const char *reloc_name ATTRIBUTE_UNUSED;
78      bfd_vma addend ATTRIBUTE_UNUSED;
79      bfd *abfd ATTRIBUTE_UNUSED;
80      asection *section ATTRIBUTE_UNUSED;
81      bfd_vma address ATTRIBUTE_UNUSED;
82 {
83   return true;
84 }
85
86 static boolean
87 simple_dummy_reloc_dangerous (link_info, message, abfd, section, address)
88      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
89      const char *message ATTRIBUTE_UNUSED;
90      bfd *abfd ATTRIBUTE_UNUSED;
91      asection *section ATTRIBUTE_UNUSED;
92      bfd_vma address ATTRIBUTE_UNUSED;
93 {
94   return true;
95 }
96
97 static boolean
98 simple_dummy_unattached_reloc (link_info, name, abfd, section, address)
99      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
100      const char *name ATTRIBUTE_UNUSED;
101      bfd *abfd ATTRIBUTE_UNUSED;
102      asection *section ATTRIBUTE_UNUSED;
103      bfd_vma address ATTRIBUTE_UNUSED;
104 {
105   return true;
106 }
107
108 /*
109 FUNCTION
110         bfd_simple_relocate_secton
111
112 SYNOPSIS
113         bfd_byte *bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec, bfd_byte *outbuf);
114
115 DESCRIPTION
116         Returns the relocated contents of section @var{sec}.  Only symbols
117         from @var{abfd} and the output offsets assigned to sections in
118         @var{abfd} are used.  The result will be stored at @var{outbuf}
119         or allocated with @code{bfd_malloc} if @var{outbuf} is @code{NULL}.
120
121         Generally all sections in @var{abfd} should have their
122         @code{output_section} pointing back to the original section.
123
124         Returns @code{NULL} on a fatal error; ignores errors applying
125         particular relocations.
126 */
127
128 bfd_byte *
129 bfd_simple_get_relocated_section_contents (abfd, sec, outbuf)
130      bfd *abfd;
131      asection *sec;
132      bfd_byte *outbuf;
133 {
134   struct bfd_link_info link_info;
135   struct bfd_link_order link_order;
136   struct bfd_link_callbacks callbacks;
137   bfd_byte *contents, *data;
138   int storage_needed, number_of_symbols;
139   asymbol **symbol_table;
140
141   if (! (sec->flags & SEC_RELOC))
142     {
143       bfd_size_type size = bfd_section_size (abfd, sec);
144
145       if (outbuf == NULL)
146         contents = bfd_malloc (size);
147       else
148         contents = outbuf;
149
150       if (contents)
151         bfd_get_section_contents (abfd, sec, contents, 0, size);
152
153       return contents;
154     }
155
156   /* In order to use bfd_get_relocated_section_contents, we need
157      to forge some data structures that it expects.  */
158
159   /* Fill in the bare minimum number of fields for our purposes.  */
160   memset (&link_info, 0, sizeof (link_info));
161   link_info.input_bfds = abfd;
162
163   link_info.hash = bfd_link_hash_table_create (abfd);
164   link_info.callbacks = &callbacks;
165   callbacks.warning = simple_dummy_warning;
166   callbacks.undefined_symbol = simple_dummy_undefined_symbol;
167   callbacks.reloc_overflow = simple_dummy_reloc_overflow;
168   callbacks.reloc_dangerous = simple_dummy_reloc_dangerous;
169   callbacks.unattached_reloc = simple_dummy_unattached_reloc;
170
171   memset (&link_order, 0, sizeof (link_order));
172   link_order.next = NULL;
173   link_order.type = bfd_indirect_link_order;
174   link_order.offset = 0;
175   link_order.size = bfd_section_size (abfd, sec);
176   link_order.u.indirect.section = sec;
177
178   data = NULL;
179   if (outbuf == NULL)
180     {
181       data = bfd_malloc (bfd_section_size (abfd, sec));
182       if (data == NULL)
183         return NULL;
184       outbuf = data;
185     }
186   bfd_link_add_symbols (abfd, &link_info);
187
188   storage_needed = bfd_get_symtab_upper_bound (abfd);
189   symbol_table = (asymbol **) bfd_malloc (storage_needed);
190   number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
191
192   contents = bfd_get_relocated_section_contents (abfd,
193                                                  &link_info,
194                                                  &link_order,
195                                                  outbuf,
196                                                  0,
197                                                  symbol_table);
198   if (contents == NULL && data != NULL)
199     free (data);
200
201   /* Foul hack to prevent bfd_section_size aborts.  This flag only controls
202      that macro (and the related size macros), selecting between _raw_size
203      and _cooked_size.  Debug sections won't change size while we're only
204      relocating.  There may be trouble here someday if it tries to run
205      relaxation unexpectedly, so make sure.  */
206   BFD_ASSERT (sec->_raw_size == sec->_cooked_size);
207   sec->reloc_done = 0;
208
209   bfd_link_hash_table_free (abfd, link_info.hash);
210
211   return contents;
212 }