PR binutils/16595
[external/binutils.git] / bfd / simple.c
1 /* simple.c -- BFD simple client routines
2    Copyright 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27
28 static bfd_boolean
29 simple_dummy_warning (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
30                       const char *warning ATTRIBUTE_UNUSED,
31                       const char *symbol ATTRIBUTE_UNUSED,
32                       bfd *abfd ATTRIBUTE_UNUSED,
33                       asection *section ATTRIBUTE_UNUSED,
34                       bfd_vma address ATTRIBUTE_UNUSED)
35 {
36   return TRUE;
37 }
38
39 static bfd_boolean
40 simple_dummy_undefined_symbol (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
41                                const char *name ATTRIBUTE_UNUSED,
42                                bfd *abfd ATTRIBUTE_UNUSED,
43                                asection *section ATTRIBUTE_UNUSED,
44                                bfd_vma address ATTRIBUTE_UNUSED,
45                                bfd_boolean fatal ATTRIBUTE_UNUSED)
46 {
47   return TRUE;
48 }
49
50 static bfd_boolean
51 simple_dummy_reloc_overflow (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
52                              struct bfd_link_hash_entry *entry ATTRIBUTE_UNUSED,
53                              const char *name ATTRIBUTE_UNUSED,
54                              const char *reloc_name ATTRIBUTE_UNUSED,
55                              bfd_vma addend ATTRIBUTE_UNUSED,
56                              bfd *abfd ATTRIBUTE_UNUSED,
57                              asection *section ATTRIBUTE_UNUSED,
58                              bfd_vma address ATTRIBUTE_UNUSED)
59 {
60   return TRUE;
61 }
62
63 static bfd_boolean
64 simple_dummy_reloc_dangerous (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
65                               const char *message ATTRIBUTE_UNUSED,
66                               bfd *abfd ATTRIBUTE_UNUSED,
67                               asection *section ATTRIBUTE_UNUSED,
68                               bfd_vma address ATTRIBUTE_UNUSED)
69 {
70   return TRUE;
71 }
72
73 static bfd_boolean
74 simple_dummy_unattached_reloc (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
75                                const char *name ATTRIBUTE_UNUSED,
76                                bfd *abfd ATTRIBUTE_UNUSED,
77                                asection *section ATTRIBUTE_UNUSED,
78                                bfd_vma address ATTRIBUTE_UNUSED)
79 {
80   return TRUE;
81 }
82
83 static bfd_boolean
84 simple_dummy_multiple_definition (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
85                                   struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
86                                   bfd *nbfd ATTRIBUTE_UNUSED,
87                                   asection *nsec ATTRIBUTE_UNUSED,
88                                   bfd_vma nval ATTRIBUTE_UNUSED)
89 {
90   return TRUE;
91 }
92
93 static void
94 simple_dummy_einfo (const char *fmt ATTRIBUTE_UNUSED, ...)
95 {
96 }
97
98 struct saved_output_info
99 {
100   bfd_vma offset;
101   asection *section;
102 };
103
104 struct saved_offsets
105 {
106   int section_count;
107   struct saved_output_info *sections;
108 };
109
110 static void
111 simple_save_output_info (bfd *abfd ATTRIBUTE_UNUSED,
112                          asection *section,
113                          void *ptr)
114 {
115   struct saved_offsets *saved_offsets = (struct saved_offsets *) ptr;
116   struct saved_output_info *output_info;
117
118   output_info = &saved_offsets->sections[section->index];
119   output_info->offset = section->output_offset;
120   output_info->section = section->output_section;
121   if ((section->flags & SEC_DEBUGGING) != 0
122       || section->output_section == NULL)
123     {
124       section->output_offset = 0;
125       section->output_section = section;
126     }
127 }
128
129 static void
130 simple_restore_output_info (bfd *abfd ATTRIBUTE_UNUSED,
131                             asection *section,
132                             void *ptr)
133 {
134   struct saved_offsets *saved_offsets = (struct saved_offsets *) ptr;
135   struct saved_output_info *output_info;
136
137   if (section->index >= saved_offsets->section_count)
138     return;
139
140   output_info = &saved_offsets->sections[section->index];
141   section->output_offset = output_info->offset;
142   section->output_section = output_info->section;
143 }
144
145 /*
146 FUNCTION
147         bfd_simple_relocate_secton
148
149 SYNOPSIS
150         bfd_byte *bfd_simple_get_relocated_section_contents
151           (bfd *abfd, asection *sec, bfd_byte *outbuf, asymbol **symbol_table);
152
153 DESCRIPTION
154         Returns the relocated contents of section @var{sec}.  The symbols in
155         @var{symbol_table} will be used, or the symbols from @var{abfd} if
156         @var{symbol_table} is NULL.  The output offsets for debug sections will
157         be temporarily reset to 0.  The result will be stored at @var{outbuf}
158         or allocated with @code{bfd_malloc} if @var{outbuf} is @code{NULL}.
159
160         Returns @code{NULL} on a fatal error; ignores errors applying
161         particular relocations.
162 */
163
164 bfd_byte *
165 bfd_simple_get_relocated_section_contents (bfd *abfd,
166                                            asection *sec,
167                                            bfd_byte *outbuf,
168                                            asymbol **symbol_table)
169 {
170   struct bfd_link_info link_info;
171   struct bfd_link_order link_order;
172   struct bfd_link_callbacks callbacks;
173   bfd_byte *contents, *data;
174   int storage_needed;
175   struct saved_offsets saved_offsets;
176
177   /* Don't apply relocation on executable and shared library.  See
178      PR 4756.  */
179   if ((abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC)) != HAS_RELOC
180       || ! (sec->flags & SEC_RELOC))
181     {
182       contents = outbuf;
183       if (!bfd_get_full_section_contents (abfd, sec, &contents))
184         return NULL;
185       return contents;
186     }
187
188   /* In order to use bfd_get_relocated_section_contents, we need
189      to forge some data structures that it expects.  */
190
191   /* Fill in the bare minimum number of fields for our purposes.  */
192   memset (&link_info, 0, sizeof (link_info));
193   link_info.output_bfd = abfd;
194   link_info.input_bfds = abfd;
195   link_info.input_bfds_tail = &abfd->link_next;
196
197   link_info.hash = _bfd_generic_link_hash_table_create (abfd);
198   link_info.callbacks = &callbacks;
199   callbacks.warning = simple_dummy_warning;
200   callbacks.undefined_symbol = simple_dummy_undefined_symbol;
201   callbacks.reloc_overflow = simple_dummy_reloc_overflow;
202   callbacks.reloc_dangerous = simple_dummy_reloc_dangerous;
203   callbacks.unattached_reloc = simple_dummy_unattached_reloc;
204   callbacks.multiple_definition = simple_dummy_multiple_definition;
205   callbacks.einfo = simple_dummy_einfo;
206
207   memset (&link_order, 0, sizeof (link_order));
208   link_order.next = NULL;
209   link_order.type = bfd_indirect_link_order;
210   link_order.offset = 0;
211   link_order.size = sec->size;
212   link_order.u.indirect.section = sec;
213
214   data = NULL;
215   if (outbuf == NULL)
216     {
217       bfd_size_type amt = sec->rawsize > sec->size ? sec->rawsize : sec->size;
218       data = (bfd_byte *) bfd_malloc (amt);
219       if (data == NULL)
220         return NULL;
221       outbuf = data;
222     }
223
224   /* The sections in ABFD may already have output sections and offsets set.
225      Because this function is primarily for debug sections, and GCC uses the
226      knowledge that debug sections will generally have VMA 0 when emitting
227      relocations between DWARF-2 sections (which are supposed to be
228      section-relative offsets anyway), we need to reset the output offsets
229      to zero.  We also need to arrange for section->output_section->vma plus
230      section->output_offset to equal section->vma, which we do by setting
231      section->output_section to point back to section.  Save the original
232      output offset and output section to restore later.  */
233   saved_offsets.section_count = abfd->section_count;
234   saved_offsets.sections = malloc (sizeof (*saved_offsets.sections)
235                                    * saved_offsets.section_count);
236   if (saved_offsets.sections == 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   bfd_map_over_sections (abfd, simple_restore_output_info, &saved_offsets);
265   free (saved_offsets.sections);
266
267   _bfd_generic_link_hash_table_free (link_info.hash);
268   return contents;
269 }