daily update
[binutils.git] / bfd / simple.c
blob10178a40ad4cd62bead3ad5ed450673d594ee1d5
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 boolean simple_dummy_warning
28 PARAMS ((struct bfd_link_info *, const char *, const char *, bfd *,
29 asection *, bfd_vma));
31 static boolean simple_dummy_undefined_symbol
32 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *,
33 bfd_vma, boolean));
35 static boolean simple_dummy_reloc_overflow
36 PARAMS ((struct bfd_link_info *, const char *, const char *, bfd_vma,
37 bfd *, asection *, bfd_vma));
39 static boolean simple_dummy_reloc_dangerous
40 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
42 static 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 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 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;
69 return true;
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;
83 return true;
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;
94 return true;
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;
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 /* In order to use bfd_get_relocated_section_contents, we need
142 to forge some data structures that it expects. */
144 /* Fill in the bare minimum number of fields for our purposes. */
145 memset (&link_info, 0, sizeof (link_info));
146 link_info.input_bfds = abfd;
148 link_info.hash = bfd_link_hash_table_create (abfd);
149 link_info.callbacks = &callbacks;
150 callbacks.warning = simple_dummy_warning;
151 callbacks.undefined_symbol = simple_dummy_undefined_symbol;
152 callbacks.reloc_overflow = simple_dummy_reloc_overflow;
153 callbacks.reloc_dangerous = simple_dummy_reloc_dangerous;
154 callbacks.unattached_reloc = simple_dummy_unattached_reloc;
156 memset (&link_order, 0, sizeof (link_order));
157 link_order.next = NULL;
158 link_order.type = bfd_indirect_link_order;
159 link_order.offset = 0;
160 link_order.size = bfd_section_size (abfd, sec);
161 link_order.u.indirect.section = sec;
163 data = NULL;
164 if (outbuf == NULL)
166 data = bfd_malloc (bfd_section_size (abfd, sec));
167 if (data == NULL)
168 return NULL;
169 outbuf = data;
171 bfd_link_add_symbols (abfd, &link_info);
173 storage_needed = bfd_get_symtab_upper_bound (abfd);
174 symbol_table = (asymbol **) bfd_malloc (storage_needed);
175 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
177 contents = bfd_get_relocated_section_contents (abfd,
178 &link_info,
179 &link_order,
180 outbuf,
182 symbol_table);
183 if (contents == NULL && data != NULL)
184 free (data);
186 /* Foul hack to prevent bfd_section_size aborts. This flag only controls
187 that macro (and the related size macros), selecting between _raw_size
188 and _cooked_size. Debug sections won't change size while we're only
189 relocating. There may be trouble here someday if it tries to run
190 relaxation unexpectedly, so make sure. */
191 BFD_ASSERT (sec->_raw_size == sec->_cooked_size);
192 sec->reloc_done = 0;
194 bfd_link_hash_table_free (abfd, link_info.hash);
196 return contents;