Fix memory leaks
[deliverable/binutils-gdb.git] / bfd / simple.c
CommitLineData
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 27static 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 31static 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 35static 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 39static bfd_boolean simple_dummy_reloc_dangerous
8718ac9c 40 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
5d661875 41
b34976b6 42static bfd_boolean simple_dummy_unattached_reloc
8718ac9c 43 PARAMS ((struct bfd_link_info *, const char *, bfd *, asection *, bfd_vma));
5d661875
DA
44
45bfd_byte * bfd_simple_get_relocated_section_contents
46 PARAMS ((bfd *, asection *, bfd_byte *));
47
b34976b6 48static bfd_boolean
5d661875 49simple_dummy_warning (link_info, warning, symbol, abfd, section, address)
8718ac9c
HPN
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;
af39267e 56{
b34976b6 57 return TRUE;
af39267e
DJ
58}
59
b34976b6 60static bfd_boolean
5d661875 61simple_dummy_undefined_symbol (link_info, name, abfd, section, address, fatal)
8718ac9c
HPN
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;
b34976b6 67 bfd_boolean fatal ATTRIBUTE_UNUSED;
af39267e 68{
b34976b6 69 return TRUE;
af39267e
DJ
70}
71
b34976b6 72static bfd_boolean
5d661875
DA
73simple_dummy_reloc_overflow (link_info, name, reloc_name, addend, abfd,
74 section, address)
8718ac9c
HPN
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;
af39267e 82{
b34976b6 83 return TRUE;
af39267e
DJ
84}
85
b34976b6 86static bfd_boolean
5d661875 87simple_dummy_reloc_dangerous (link_info, message, abfd, section, address)
8718ac9c
HPN
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;
af39267e 93{
b34976b6 94 return TRUE;
af39267e
DJ
95}
96
b34976b6 97static bfd_boolean
5d661875 98simple_dummy_unattached_reloc (link_info, name, abfd, section, address)
8718ac9c
HPN
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;
af39267e 104{
b34976b6 105 return TRUE;
af39267e
DJ
106}
107
108/*
109FUNCTION
110 bfd_simple_relocate_secton
111
112SYNOPSIS
113 bfd_byte *bfd_simple_get_relocated_section_contents (bfd *abfd, asection *sec, bfd_byte *outbuf);
114
115DESCRIPTION
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}.
120
121 Generally all sections in @var{abfd} should have their
122 @code{output_section} pointing back to the original section.
123
124 Returns @code{NULL} on a fatal error; ignores errors applying
125 particular relocations.
126*/
127
128bfd_byte *
5d661875
DA
129bfd_simple_get_relocated_section_contents (abfd, sec, outbuf)
130 bfd *abfd;
131 asection *sec;
132 bfd_byte *outbuf;
af39267e
DJ
133{
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;
5ed6aba4 138 int storage_needed;
af39267e
DJ
139 asymbol **symbol_table;
140
ec4530b5
NC
141 if (! (sec->flags & SEC_RELOC))
142 {
143 bfd_size_type size = bfd_section_size (abfd, sec);
144
145 if (outbuf == NULL)
146 contents = bfd_malloc (size);
147 else
148 contents = outbuf;
149
150 if (contents)
151 bfd_get_section_contents (abfd, sec, contents, 0, size);
152
153 return contents;
154 }
155
af39267e
DJ
156 /* In order to use bfd_get_relocated_section_contents, we need
157 to forge some data structures that it expects. */
158
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;
162
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;
170
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;
177
178 data = NULL;
179 if (outbuf == NULL)
180 {
181 data = bfd_malloc (bfd_section_size (abfd, sec));
182 if (data == NULL)
183 return NULL;
184 outbuf = data;
185 }
186 bfd_link_add_symbols (abfd, &link_info);
187
188 storage_needed = bfd_get_symtab_upper_bound (abfd);
189 symbol_table = (asymbol **) bfd_malloc (storage_needed);
5ed6aba4 190 bfd_canonicalize_symtab (abfd, symbol_table);
af39267e
DJ
191
192 contents = bfd_get_relocated_section_contents (abfd,
193 &link_info,
194 &link_order,
195 outbuf,
196 0,
197 symbol_table);
198 if (contents == NULL && data != NULL)
199 free (data);
200
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;
208
209 bfd_link_hash_table_free (abfd, link_info.hash);
210
5ed6aba4 211 free (symbol_table);
af39267e
DJ
212 return contents;
213}
This page took 0.058058 seconds and 4 git commands to generate.