1 /* simple.c -- BFD simple client routines
3 Free Software Foundation, Inc.
4 Contributed by MontaVista Software, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 static bfd_boolean simple_dummy_warning
28 PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *,
29 asection *, bfd_vma));
31 static bfd_boolean simple_dummy_undefined_symbol
32 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *,
33 bfd_vma, bfd_boolean));
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));
39 static bfd_boolean simple_dummy_reloc_dangerous
40 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
42 static bfd_boolean simple_dummy_unattached_reloc
43 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
45 static void simple_save_output_info
46 PARAMS ((bfd *, asection *, PTR));
48 static void simple_restore_output_info
49 PARAMS ((bfd *, asection *, PTR));
51 bfd_byte * bfd_simple_get_relocated_section_contents
52 PARAMS ((bfd *, asection *, bfd_byte *, asymbol **));
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;
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;
79 simple_dummy_reloc_overflow (link_info, name, reloc_name, addend, abfd,
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;
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;
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;
114 struct saved_output_info
121 simple_save_output_info (abfd, section, ptr)
122 bfd *abfd ATTRIBUTE_UNUSED;
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;
134 simple_restore_output_info (abfd, section, ptr)
135 bfd *abfd ATTRIBUTE_UNUSED;
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;
146 bfd_simple_relocate_secton
149 bfd_byte *bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec, bfd_byte *outbuf, asymbol **symbol_table);
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}.
158 Generally all sections in @var{abfd} should have their
159 @code{output_section} pointing back to the original section.
161 Returns @code{NULL} on a fatal error; ignores errors applying
162 particular relocations.
166 bfd_simple_get_relocated_section_contents (abfd, sec, outbuf, symbol_table)
170 asymbol **symbol_table;
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;
179 if (! (sec->flags & SEC_RELOC))
181 bfd_size_type size = bfd_section_size (abfd, sec);
184 contents = bfd_malloc (size);
189 bfd_get_section_contents (abfd, sec, contents, 0, size);
194 /* In order to use bfd_get_relocated_section_contents, we need
195 to forge some data structures that it expects. */
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;
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;
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;
219 data = bfd_malloc (bfd_section_size (abfd, sec));
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)
242 bfd_map_over_sections (abfd, simple_save_output_info, saved_offsets);
244 if (symbol_table == NULL)
246 _bfd_generic_link_add_symbols (abfd, &link_info);
248 storage_needed = bfd_get_symtab_upper_bound (abfd);
249 symbol_table = (asymbol **) bfd_malloc (storage_needed);
250 bfd_canonicalize_symtab (abfd, symbol_table);
255 contents = bfd_get_relocated_section_contents (abfd,
261 if (contents == NULL && data != NULL)
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:
270 gdb.base/relocate.exp: get address of static_bar
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)
278 bfd_map_over_sections (abfd, simple_restore_output_info, saved_offsets);
279 free (saved_offsets);
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);
289 bfd_link_hash_table_free (abfd, link_info.hash);