* reloc.c: Add BFD_RELOC_XSTORMY16_12.
[binutils.git] / bfd / simple.c
blob88f13370551edae5cabbdc9d877a8a1a57e124fa
1 /* simple.c -- BFD simple client routines
2 Copyright 2002
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. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
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 bfd_byte * bfd_simple_get_relocated_section_contents
46 PARAMS ((bfd *, asection *, bfd_byte *));
48 static bfd_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;
57 return TRUE;
60 static bfd_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 bfd_boolean fatal ATTRIBUTE_UNUSED;
69 return TRUE;
72 static bfd_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;
83 return TRUE;
86 static bfd_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;
94 return TRUE;
97 static bfd_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;
105 return TRUE;
109 FUNCTION
110 bfd_simple_relocate_secton
112 SYNOPSIS
113 bfd_byte *bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec, bfd_byte *outbuf);
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}.
121 Generally all sections in @var{abfd} should have their
122 @code{output_section} pointing back to the original section.
124 Returns @code{NULL} on a fatal error; ignores errors applying
125 particular relocations.
128 bfd_byte *
129 bfd_simple_get_relocated_section_contents (abfd, sec, outbuf)
130 bfd *abfd;
131 asection *sec;
132 bfd_byte *outbuf;
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;
141 if (! (sec->flags & SEC_RELOC))
143 bfd_size_type size = bfd_section_size (abfd, sec);
145 if (outbuf == NULL)
146 contents = bfd_malloc (size);
147 else
148 contents = outbuf;
150 if (contents)
151 bfd_get_section_contents (abfd, sec, contents, 0, size);
153 return contents;
156 /* In order to use bfd_get_relocated_section_contents, we need
157 to forge some data structures that it expects. */
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;
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;
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;
178 data = NULL;
179 if (outbuf == NULL)
181 data = bfd_malloc (bfd_section_size (abfd, sec));
182 if (data == NULL)
183 return NULL;
184 outbuf = data;
186 bfd_link_add_symbols (abfd, &link_info);
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);
192 contents = bfd_get_relocated_section_contents (abfd,
193 &link_info,
194 &link_order,
195 outbuf,
197 symbol_table);
198 if (contents == NULL && data != NULL)
199 free (data);
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;
209 bfd_link_hash_table_free (abfd, link_info.hash);
211 return contents;