Commit | Line | Data |
---|---|---|
af39267e | 1 | /* simple.c -- BFD simple client routines |
5ed6aba4 | 2 | Copyright 2002, 2003 |
af39267e DJ |
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 | ||
b34976b6 | 27 | static bfd_boolean simple_dummy_warning |
8718ac9c HPN |
28 | PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *, |
29 | asection *, bfd_vma)); | |
5d661875 | 30 | |
b34976b6 | 31 | static bfd_boolean simple_dummy_undefined_symbol |
8718ac9c | 32 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, |
b34976b6 | 33 | bfd_vma, bfd_boolean)); |
5d661875 | 34 | |
b34976b6 | 35 | static bfd_boolean simple_dummy_reloc_overflow |
8718ac9c HPN |
36 | PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma, |
37 | bfd *, asection *, bfd_vma)); | |
5d661875 | 38 | |
b34976b6 | 39 | static bfd_boolean simple_dummy_reloc_dangerous |
8718ac9c | 40 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); |
5d661875 | 41 | |
b34976b6 | 42 | static bfd_boolean simple_dummy_unattached_reloc |
8718ac9c | 43 | PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma)); |
5d661875 | 44 | |
6e84a906 DJ |
45 | static void simple_save_output_info |
46 | PARAMS ((bfd *, asection *, PTR)); | |
47 | ||
48 | static void simple_restore_output_info | |
49 | PARAMS ((bfd *, asection *, PTR)); | |
50 | ||
5d661875 | 51 | bfd_byte * bfd_simple_get_relocated_section_contents |
6e84a906 | 52 | PARAMS ((bfd *, asection *, bfd_byte *, asymbol **)); |
5d661875 | 53 | |
b34976b6 | 54 | static bfd_boolean |
5d661875 | 55 | simple_dummy_warning (link_info, warning, symbol, abfd, section, address) |
8718ac9c HPN |
56 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED; |
57 | const char *warning ATTRIBUTE_UNUSED; | |
58 | const char *symbol ATTRIBUTE_UNUSED; | |
59 | bfd *abfd ATTRIBUTE_UNUSED; | |
60 | asection *section ATTRIBUTE_UNUSED; | |
61 | bfd_vma address ATTRIBUTE_UNUSED; | |
af39267e | 62 | { |
b34976b6 | 63 | return TRUE; |
af39267e DJ |
64 | } |
65 | ||
b34976b6 | 66 | static bfd_boolean |
5d661875 | 67 | simple_dummy_undefined_symbol (link_info, name, abfd, section, address, fatal) |
8718ac9c HPN |
68 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED; |
69 | const char *name ATTRIBUTE_UNUSED; | |
70 | bfd *abfd ATTRIBUTE_UNUSED; | |
71 | asection *section ATTRIBUTE_UNUSED; | |
72 | bfd_vma address ATTRIBUTE_UNUSED; | |
b34976b6 | 73 | bfd_boolean fatal ATTRIBUTE_UNUSED; |
af39267e | 74 | { |
b34976b6 | 75 | return TRUE; |
af39267e DJ |
76 | } |
77 | ||
b34976b6 | 78 | static bfd_boolean |
5d661875 DA |
79 | simple_dummy_reloc_overflow (link_info, name, reloc_name, addend, abfd, |
80 | section, address) | |
8718ac9c HPN |
81 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED; |
82 | const char *name ATTRIBUTE_UNUSED; | |
83 | const char *reloc_name ATTRIBUTE_UNUSED; | |
84 | bfd_vma addend ATTRIBUTE_UNUSED; | |
85 | bfd *abfd ATTRIBUTE_UNUSED; | |
86 | asection *section ATTRIBUTE_UNUSED; | |
87 | bfd_vma address ATTRIBUTE_UNUSED; | |
af39267e | 88 | { |
b34976b6 | 89 | return TRUE; |
af39267e DJ |
90 | } |
91 | ||
b34976b6 | 92 | static bfd_boolean |
5d661875 | 93 | simple_dummy_reloc_dangerous (link_info, message, abfd, section, address) |
8718ac9c HPN |
94 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED; |
95 | const char *message ATTRIBUTE_UNUSED; | |
96 | bfd *abfd ATTRIBUTE_UNUSED; | |
97 | asection *section ATTRIBUTE_UNUSED; | |
98 | bfd_vma address ATTRIBUTE_UNUSED; | |
af39267e | 99 | { |
b34976b6 | 100 | return TRUE; |
af39267e DJ |
101 | } |
102 | ||
b34976b6 | 103 | static bfd_boolean |
5d661875 | 104 | simple_dummy_unattached_reloc (link_info, name, abfd, section, address) |
8718ac9c HPN |
105 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED; |
106 | const char *name ATTRIBUTE_UNUSED; | |
107 | bfd *abfd ATTRIBUTE_UNUSED; | |
108 | asection *section ATTRIBUTE_UNUSED; | |
109 | bfd_vma address ATTRIBUTE_UNUSED; | |
af39267e | 110 | { |
b34976b6 | 111 | return TRUE; |
af39267e DJ |
112 | } |
113 | ||
6e84a906 DJ |
114 | struct saved_output_info |
115 | { | |
116 | bfd_vma offset; | |
117 | asection *section; | |
118 | }; | |
119 | ||
120 | static void | |
121 | simple_save_output_info (abfd, section, ptr) | |
122 | bfd *abfd ATTRIBUTE_UNUSED; | |
123 | asection *section; | |
124 | PTR ptr; | |
125 | { | |
126 | struct saved_output_info *output_info = (struct saved_output_info *) ptr; | |
127 | output_info[section->index].offset = section->output_offset; | |
128 | output_info[section->index].section = section->output_section; | |
129 | section->output_offset = 0; | |
130 | section->output_section = section; | |
131 | } | |
132 | ||
133 | static void | |
134 | simple_restore_output_info (abfd, section, ptr) | |
135 | bfd *abfd ATTRIBUTE_UNUSED; | |
136 | asection *section; | |
137 | PTR ptr; | |
138 | { | |
139 | struct saved_output_info *output_info = (struct saved_output_info *) ptr; | |
140 | section->output_offset = output_info[section->index].offset; | |
141 | section->output_section = output_info[section->index].section; | |
142 | } | |
143 | ||
af39267e DJ |
144 | /* |
145 | FUNCTION | |
146 | bfd_simple_relocate_secton | |
147 | ||
148 | SYNOPSIS | |
6e84a906 | 149 | bfd_byte *bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec, bfd_byte *outbuf, asymbol **symbol_table); |
af39267e DJ |
150 | |
151 | DESCRIPTION | |
6e84a906 DJ |
152 | Returns the relocated contents of section @var{sec}. The symbols in |
153 | @var{symbol_table} will be used, or the symbols from @var{abfd} if | |
154 | @var{symbol_table} is NULL. The output offsets for all sections will | |
155 | be temporarily reset to 0. The result will be stored at @var{outbuf} | |
af39267e DJ |
156 | or allocated with @code{bfd_malloc} if @var{outbuf} is @code{NULL}. |
157 | ||
158 | Generally all sections in @var{abfd} should have their | |
159 | @code{output_section} pointing back to the original section. | |
160 | ||
161 | Returns @code{NULL} on a fatal error; ignores errors applying | |
162 | particular relocations. | |
163 | */ | |
164 | ||
165 | bfd_byte * | |
6e84a906 | 166 | bfd_simple_get_relocated_section_contents (abfd, sec, outbuf, symbol_table) |
5d661875 DA |
167 | bfd *abfd; |
168 | asection *sec; | |
169 | bfd_byte *outbuf; | |
6e84a906 | 170 | asymbol **symbol_table; |
af39267e DJ |
171 | { |
172 | struct bfd_link_info link_info; | |
173 | struct bfd_link_order link_order; | |
174 | struct bfd_link_callbacks callbacks; | |
175 | bfd_byte *contents, *data; | |
5ed6aba4 | 176 | int storage_needed; |
6e84a906 | 177 | PTR saved_offsets; |
af39267e | 178 | |
ec4530b5 NC |
179 | if (! (sec->flags & SEC_RELOC)) |
180 | { | |
181 | bfd_size_type size = bfd_section_size (abfd, sec); | |
182 | ||
183 | if (outbuf == NULL) | |
184 | contents = bfd_malloc (size); | |
185 | else | |
186 | contents = outbuf; | |
187 | ||
188 | if (contents) | |
189 | bfd_get_section_contents (abfd, sec, contents, 0, size); | |
190 | ||
191 | return contents; | |
192 | } | |
193 | ||
af39267e DJ |
194 | /* In order to use bfd_get_relocated_section_contents, we need |
195 | to forge some data structures that it expects. */ | |
196 | ||
197 | /* Fill in the bare minimum number of fields for our purposes. */ | |
198 | memset (&link_info, 0, sizeof (link_info)); | |
199 | link_info.input_bfds = abfd; | |
200 | ||
201 | link_info.hash = bfd_link_hash_table_create (abfd); | |
202 | link_info.callbacks = &callbacks; | |
203 | callbacks.warning = simple_dummy_warning; | |
204 | callbacks.undefined_symbol = simple_dummy_undefined_symbol; | |
205 | callbacks.reloc_overflow = simple_dummy_reloc_overflow; | |
206 | callbacks.reloc_dangerous = simple_dummy_reloc_dangerous; | |
207 | callbacks.unattached_reloc = simple_dummy_unattached_reloc; | |
208 | ||
209 | memset (&link_order, 0, sizeof (link_order)); | |
210 | link_order.next = NULL; | |
211 | link_order.type = bfd_indirect_link_order; | |
212 | link_order.offset = 0; | |
213 | link_order.size = bfd_section_size (abfd, sec); | |
214 | link_order.u.indirect.section = sec; | |
215 | ||
216 | data = NULL; | |
217 | if (outbuf == NULL) | |
218 | { | |
219 | data = bfd_malloc (bfd_section_size (abfd, sec)); | |
220 | if (data == NULL) | |
221 | return NULL; | |
222 | outbuf = data; | |
223 | } | |
af39267e | 224 | |
6e84a906 DJ |
225 | /* The sections in ABFD may already have output sections and offsets set. |
226 | Because this function is primarily for debug sections, and GCC uses the | |
227 | knowledge that debug sections will generally have VMA 0 when emiting | |
228 | relocations between DWARF-2 sections (which are supposed to be | |
229 | section-relative offsets anyway), we need to reset the output offsets | |
230 | to zero. We also need to arrange for section->output_section->vma plus | |
231 | section->output_offset to equal section->vma, which we do by setting | |
232 | section->output_section to point back to section. Save the original | |
233 | output offset and output section to restore later. */ | |
234 | saved_offsets = malloc (sizeof (struct saved_output_info) | |
235 | * abfd->section_count); | |
236 | if (saved_offsets == NULL) | |
237 | { | |
238 | if (data) | |
239 | free (data); | |
240 | return NULL; | |
241 | } | |
242 | bfd_map_over_sections (abfd, simple_save_output_info, saved_offsets); | |
243 | ||
244 | if (symbol_table == NULL) | |
245 | { | |
246 | bfd_link_add_symbols (abfd, &link_info); | |
247 | ||
248 | storage_needed = bfd_get_symtab_upper_bound (abfd); | |
249 | symbol_table = (asymbol **) bfd_malloc (storage_needed); | |
250 | bfd_canonicalize_symtab (abfd, symbol_table); | |
251 | } | |
252 | else | |
253 | storage_needed = 0; | |
af39267e DJ |
254 | |
255 | contents = bfd_get_relocated_section_contents (abfd, | |
256 | &link_info, | |
257 | &link_order, | |
258 | outbuf, | |
259 | 0, | |
260 | symbol_table); | |
261 | if (contents == NULL && data != NULL) | |
262 | free (data); | |
263 | ||
6e84a906 DJ |
264 | if (storage_needed != 0) |
265 | free (symbol_table); | |
266 | ||
267 | bfd_map_over_sections (abfd, simple_restore_output_info, saved_offsets); | |
268 | free (saved_offsets); | |
269 | ||
af39267e DJ |
270 | /* Foul hack to prevent bfd_section_size aborts. This flag only controls |
271 | that macro (and the related size macros), selecting between _raw_size | |
272 | and _cooked_size. Debug sections won't change size while we're only | |
273 | relocating. There may be trouble here someday if it tries to run | |
274 | relaxation unexpectedly, so make sure. */ | |
275 | BFD_ASSERT (sec->_raw_size == sec->_cooked_size); | |
276 | sec->reloc_done = 0; | |
277 | ||
278 | bfd_link_hash_table_free (abfd, link_info.hash); | |
279 | ||
280 | return contents; | |
281 | } |