* simple.c (bfd_simple_get_relocated_section_contents): Call
[deliverable/binutils-gdb.git] / bfd / simple.c
1 /* simple.c -- BFD simple client routines
2 Copyright 2002, 2003
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 bfd_boolean simple_dummy_warning
28 PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *,
29 asection *, bfd_vma));
30
31 static bfd_boolean simple_dummy_undefined_symbol
32 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *,
33 bfd_vma, bfd_boolean));
34
35 static bfd_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 bfd_boolean simple_dummy_reloc_dangerous
40 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
41
42 static bfd_boolean simple_dummy_unattached_reloc
43 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
44
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
51 bfd_byte * bfd_simple_get_relocated_section_contents
52 PARAMS ((bfd *, asection *, bfd_byte *, asymbol **));
53
54 static bfd_boolean
55 simple_dummy_warning (link_info, warning, symbol, abfd, section, address)
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;
62 {
63 return TRUE;
64 }
65
66 static bfd_boolean
67 simple_dummy_undefined_symbol (link_info, name, abfd, section, address, fatal)
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;
73 bfd_boolean fatal ATTRIBUTE_UNUSED;
74 {
75 return TRUE;
76 }
77
78 static bfd_boolean
79 simple_dummy_reloc_overflow (link_info, name, reloc_name, addend, abfd,
80 section, address)
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;
88 {
89 return TRUE;
90 }
91
92 static bfd_boolean
93 simple_dummy_reloc_dangerous (link_info, message, abfd, section, address)
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;
99 {
100 return TRUE;
101 }
102
103 static bfd_boolean
104 simple_dummy_unattached_reloc (link_info, name, abfd, section, address)
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;
110 {
111 return TRUE;
112 }
113
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
144 /*
145 FUNCTION
146 bfd_simple_relocate_secton
147
148 SYNOPSIS
149 bfd_byte *bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec, bfd_byte *outbuf, asymbol **symbol_table);
150
151 DESCRIPTION
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}
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 *
166 bfd_simple_get_relocated_section_contents (abfd, sec, outbuf, symbol_table)
167 bfd *abfd;
168 asection *sec;
169 bfd_byte *outbuf;
170 asymbol **symbol_table;
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;
176 int storage_needed;
177 PTR saved_offsets;
178
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
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 }
224
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_generic_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;
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
264 #if 0
265 /* NOTE: cagney/2003-04-05: This free, which was introduced on
266 2003-03-31 to stop a memory leak, caused a memory corruption
267 between GDB and BFD. The problem, which is stabs specific, can
268 be identified by a bunch of failures in relocate.exp vis:
269
270 gdb.base/relocate.exp: get address of static_bar
271
272 Details of the problem can be found on the binutils@ mailing
273 list, see the discussion thread: "gdb.mi/mi-cli.exp failures". */
274 if (storage_needed != 0)
275 free (symbol_table);
276 #endif
277
278 bfd_map_over_sections (abfd, simple_restore_output_info, saved_offsets);
279 free (saved_offsets);
280
281 /* Foul hack to prevent bfd_section_size aborts. This flag only controls
282 that macro (and the related size macros), selecting between _raw_size
283 and _cooked_size. Debug sections won't change size while we're only
284 relocating. There may be trouble here someday if it tries to run
285 relaxation unexpectedly, so make sure. */
286 BFD_ASSERT (sec->_raw_size == sec->_cooked_size);
287 sec->reloc_done = 0;
288
289 bfd_link_hash_table_free (abfd, link_info.hash);
290
291 return contents;
292 }
This page took 0.042691 seconds and 5 git commands to generate.