1*a9fa9459Szrj /* simple.c -- BFD simple client routines
2*a9fa9459Szrj Copyright (C) 2002-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj Contributed by MontaVista Software, Inc.
4*a9fa9459Szrj
5*a9fa9459Szrj This file is part of BFD, the Binary File Descriptor library.
6*a9fa9459Szrj
7*a9fa9459Szrj This program is free software; you can redistribute it and/or modify
8*a9fa9459Szrj it under the terms of the GNU General Public License as published by
9*a9fa9459Szrj the Free Software Foundation; either version 3 of the License, or
10*a9fa9459Szrj (at your option) any later version.
11*a9fa9459Szrj
12*a9fa9459Szrj This program is distributed in the hope that it will be useful,
13*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
14*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*a9fa9459Szrj GNU General Public License for more details.
16*a9fa9459Szrj
17*a9fa9459Szrj You should have received a copy of the GNU General Public License
18*a9fa9459Szrj along with this program; if not, write to the Free Software
19*a9fa9459Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20*a9fa9459Szrj MA 02110-1301, USA. */
21*a9fa9459Szrj
22*a9fa9459Szrj #include "sysdep.h"
23*a9fa9459Szrj #include "bfd.h"
24*a9fa9459Szrj #include "libbfd.h"
25*a9fa9459Szrj #include "bfdlink.h"
26*a9fa9459Szrj
27*a9fa9459Szrj static void
simple_dummy_warning(struct bfd_link_info * link_info ATTRIBUTE_UNUSED,const char * warning ATTRIBUTE_UNUSED,const char * symbol ATTRIBUTE_UNUSED,bfd * abfd ATTRIBUTE_UNUSED,asection * section ATTRIBUTE_UNUSED,bfd_vma address ATTRIBUTE_UNUSED)28*a9fa9459Szrj simple_dummy_warning (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
29*a9fa9459Szrj const char *warning ATTRIBUTE_UNUSED,
30*a9fa9459Szrj const char *symbol ATTRIBUTE_UNUSED,
31*a9fa9459Szrj bfd *abfd ATTRIBUTE_UNUSED,
32*a9fa9459Szrj asection *section ATTRIBUTE_UNUSED,
33*a9fa9459Szrj bfd_vma address ATTRIBUTE_UNUSED)
34*a9fa9459Szrj {
35*a9fa9459Szrj }
36*a9fa9459Szrj
37*a9fa9459Szrj static void
simple_dummy_undefined_symbol(struct bfd_link_info * link_info ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,bfd * abfd ATTRIBUTE_UNUSED,asection * section ATTRIBUTE_UNUSED,bfd_vma address ATTRIBUTE_UNUSED,bfd_boolean fatal ATTRIBUTE_UNUSED)38*a9fa9459Szrj simple_dummy_undefined_symbol (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
39*a9fa9459Szrj const char *name ATTRIBUTE_UNUSED,
40*a9fa9459Szrj bfd *abfd ATTRIBUTE_UNUSED,
41*a9fa9459Szrj asection *section ATTRIBUTE_UNUSED,
42*a9fa9459Szrj bfd_vma address ATTRIBUTE_UNUSED,
43*a9fa9459Szrj bfd_boolean fatal ATTRIBUTE_UNUSED)
44*a9fa9459Szrj {
45*a9fa9459Szrj }
46*a9fa9459Szrj
47*a9fa9459Szrj static void
simple_dummy_reloc_overflow(struct bfd_link_info * link_info ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * entry ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,const char * reloc_name ATTRIBUTE_UNUSED,bfd_vma addend ATTRIBUTE_UNUSED,bfd * abfd ATTRIBUTE_UNUSED,asection * section ATTRIBUTE_UNUSED,bfd_vma address ATTRIBUTE_UNUSED)48*a9fa9459Szrj simple_dummy_reloc_overflow (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
49*a9fa9459Szrj struct bfd_link_hash_entry *entry ATTRIBUTE_UNUSED,
50*a9fa9459Szrj const char *name ATTRIBUTE_UNUSED,
51*a9fa9459Szrj const char *reloc_name ATTRIBUTE_UNUSED,
52*a9fa9459Szrj bfd_vma addend ATTRIBUTE_UNUSED,
53*a9fa9459Szrj bfd *abfd ATTRIBUTE_UNUSED,
54*a9fa9459Szrj asection *section ATTRIBUTE_UNUSED,
55*a9fa9459Szrj bfd_vma address ATTRIBUTE_UNUSED)
56*a9fa9459Szrj {
57*a9fa9459Szrj }
58*a9fa9459Szrj
59*a9fa9459Szrj static void
simple_dummy_reloc_dangerous(struct bfd_link_info * link_info ATTRIBUTE_UNUSED,const char * message ATTRIBUTE_UNUSED,bfd * abfd ATTRIBUTE_UNUSED,asection * section ATTRIBUTE_UNUSED,bfd_vma address ATTRIBUTE_UNUSED)60*a9fa9459Szrj simple_dummy_reloc_dangerous (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
61*a9fa9459Szrj const char *message ATTRIBUTE_UNUSED,
62*a9fa9459Szrj bfd *abfd ATTRIBUTE_UNUSED,
63*a9fa9459Szrj asection *section ATTRIBUTE_UNUSED,
64*a9fa9459Szrj bfd_vma address ATTRIBUTE_UNUSED)
65*a9fa9459Szrj {
66*a9fa9459Szrj }
67*a9fa9459Szrj
68*a9fa9459Szrj static void
simple_dummy_unattached_reloc(struct bfd_link_info * link_info ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,bfd * abfd ATTRIBUTE_UNUSED,asection * section ATTRIBUTE_UNUSED,bfd_vma address ATTRIBUTE_UNUSED)69*a9fa9459Szrj simple_dummy_unattached_reloc (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
70*a9fa9459Szrj const char *name ATTRIBUTE_UNUSED,
71*a9fa9459Szrj bfd *abfd ATTRIBUTE_UNUSED,
72*a9fa9459Szrj asection *section ATTRIBUTE_UNUSED,
73*a9fa9459Szrj bfd_vma address ATTRIBUTE_UNUSED)
74*a9fa9459Szrj {
75*a9fa9459Szrj }
76*a9fa9459Szrj
77*a9fa9459Szrj static void
simple_dummy_multiple_definition(struct bfd_link_info * link_info ATTRIBUTE_UNUSED,struct bfd_link_hash_entry * h ATTRIBUTE_UNUSED,bfd * nbfd ATTRIBUTE_UNUSED,asection * nsec ATTRIBUTE_UNUSED,bfd_vma nval ATTRIBUTE_UNUSED)78*a9fa9459Szrj simple_dummy_multiple_definition (struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
79*a9fa9459Szrj struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
80*a9fa9459Szrj bfd *nbfd ATTRIBUTE_UNUSED,
81*a9fa9459Szrj asection *nsec ATTRIBUTE_UNUSED,
82*a9fa9459Szrj bfd_vma nval ATTRIBUTE_UNUSED)
83*a9fa9459Szrj {
84*a9fa9459Szrj }
85*a9fa9459Szrj
86*a9fa9459Szrj static void
simple_dummy_einfo(const char * fmt ATTRIBUTE_UNUSED,...)87*a9fa9459Szrj simple_dummy_einfo (const char *fmt ATTRIBUTE_UNUSED, ...)
88*a9fa9459Szrj {
89*a9fa9459Szrj }
90*a9fa9459Szrj
91*a9fa9459Szrj struct saved_output_info
92*a9fa9459Szrj {
93*a9fa9459Szrj bfd_vma offset;
94*a9fa9459Szrj asection *section;
95*a9fa9459Szrj };
96*a9fa9459Szrj
97*a9fa9459Szrj struct saved_offsets
98*a9fa9459Szrj {
99*a9fa9459Szrj unsigned int section_count;
100*a9fa9459Szrj struct saved_output_info *sections;
101*a9fa9459Szrj };
102*a9fa9459Szrj
103*a9fa9459Szrj /* The sections in ABFD may already have output sections and offsets
104*a9fa9459Szrj set if we are here during linking.
105*a9fa9459Szrj
106*a9fa9459Szrj DWARF-2 specifies offsets into debug sections in many cases and
107*a9fa9459Szrj bfd_simple_get_relocated_section_contents is called to relocate
108*a9fa9459Szrj debug info for a single relocatable object file. So we want
109*a9fa9459Szrj offsets relative to that object file's sections, not offsets in the
110*a9fa9459Szrj output file. For that reason, reset a debug section->output_offset
111*a9fa9459Szrj to zero.
112*a9fa9459Szrj
113*a9fa9459Szrj If not called during linking then set section->output_section to
114*a9fa9459Szrj point back to the input section, because output_section must not be
115*a9fa9459Szrj NULL when calling the relocation routines.
116*a9fa9459Szrj
117*a9fa9459Szrj Save the original output offset and section to restore later. */
118*a9fa9459Szrj
119*a9fa9459Szrj static void
simple_save_output_info(bfd * abfd ATTRIBUTE_UNUSED,asection * section,void * ptr)120*a9fa9459Szrj simple_save_output_info (bfd *abfd ATTRIBUTE_UNUSED,
121*a9fa9459Szrj asection *section,
122*a9fa9459Szrj void *ptr)
123*a9fa9459Szrj {
124*a9fa9459Szrj struct saved_offsets *saved_offsets = (struct saved_offsets *) ptr;
125*a9fa9459Szrj struct saved_output_info *output_info;
126*a9fa9459Szrj
127*a9fa9459Szrj output_info = &saved_offsets->sections[section->index];
128*a9fa9459Szrj output_info->offset = section->output_offset;
129*a9fa9459Szrj output_info->section = section->output_section;
130*a9fa9459Szrj if ((section->flags & SEC_DEBUGGING) != 0
131*a9fa9459Szrj || section->output_section == NULL)
132*a9fa9459Szrj {
133*a9fa9459Szrj section->output_offset = 0;
134*a9fa9459Szrj section->output_section = section;
135*a9fa9459Szrj }
136*a9fa9459Szrj }
137*a9fa9459Szrj
138*a9fa9459Szrj static void
simple_restore_output_info(bfd * abfd ATTRIBUTE_UNUSED,asection * section,void * ptr)139*a9fa9459Szrj simple_restore_output_info (bfd *abfd ATTRIBUTE_UNUSED,
140*a9fa9459Szrj asection *section,
141*a9fa9459Szrj void *ptr)
142*a9fa9459Szrj {
143*a9fa9459Szrj struct saved_offsets *saved_offsets = (struct saved_offsets *) ptr;
144*a9fa9459Szrj struct saved_output_info *output_info;
145*a9fa9459Szrj
146*a9fa9459Szrj if (section->index >= saved_offsets->section_count)
147*a9fa9459Szrj return;
148*a9fa9459Szrj
149*a9fa9459Szrj output_info = &saved_offsets->sections[section->index];
150*a9fa9459Szrj section->output_offset = output_info->offset;
151*a9fa9459Szrj section->output_section = output_info->section;
152*a9fa9459Szrj }
153*a9fa9459Szrj
154*a9fa9459Szrj /*
155*a9fa9459Szrj FUNCTION
156*a9fa9459Szrj bfd_simple_relocate_secton
157*a9fa9459Szrj
158*a9fa9459Szrj SYNOPSIS
159*a9fa9459Szrj bfd_byte *bfd_simple_get_relocated_section_contents
160*a9fa9459Szrj (bfd *abfd, asection *sec, bfd_byte *outbuf, asymbol **symbol_table);
161*a9fa9459Szrj
162*a9fa9459Szrj DESCRIPTION
163*a9fa9459Szrj Returns the relocated contents of section @var{sec}. The symbols in
164*a9fa9459Szrj @var{symbol_table} will be used, or the symbols from @var{abfd} if
165*a9fa9459Szrj @var{symbol_table} is NULL. The output offsets for debug sections will
166*a9fa9459Szrj be temporarily reset to 0. The result will be stored at @var{outbuf}
167*a9fa9459Szrj or allocated with @code{bfd_malloc} if @var{outbuf} is @code{NULL}.
168*a9fa9459Szrj
169*a9fa9459Szrj Returns @code{NULL} on a fatal error; ignores errors applying
170*a9fa9459Szrj particular relocations.
171*a9fa9459Szrj */
172*a9fa9459Szrj
173*a9fa9459Szrj bfd_byte *
bfd_simple_get_relocated_section_contents(bfd * abfd,asection * sec,bfd_byte * outbuf,asymbol ** symbol_table)174*a9fa9459Szrj bfd_simple_get_relocated_section_contents (bfd *abfd,
175*a9fa9459Szrj asection *sec,
176*a9fa9459Szrj bfd_byte *outbuf,
177*a9fa9459Szrj asymbol **symbol_table)
178*a9fa9459Szrj {
179*a9fa9459Szrj struct bfd_link_info link_info;
180*a9fa9459Szrj struct bfd_link_order link_order;
181*a9fa9459Szrj struct bfd_link_callbacks callbacks;
182*a9fa9459Szrj bfd_byte *contents, *data;
183*a9fa9459Szrj int storage_needed;
184*a9fa9459Szrj struct saved_offsets saved_offsets;
185*a9fa9459Szrj bfd *link_next;
186*a9fa9459Szrj
187*a9fa9459Szrj /* Don't apply relocation on executable and shared library. See
188*a9fa9459Szrj PR 4756. */
189*a9fa9459Szrj if ((abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC)) != HAS_RELOC
190*a9fa9459Szrj || ! (sec->flags & SEC_RELOC))
191*a9fa9459Szrj {
192*a9fa9459Szrj contents = outbuf;
193*a9fa9459Szrj if (!bfd_get_full_section_contents (abfd, sec, &contents))
194*a9fa9459Szrj return NULL;
195*a9fa9459Szrj return contents;
196*a9fa9459Szrj }
197*a9fa9459Szrj
198*a9fa9459Szrj /* In order to use bfd_get_relocated_section_contents, we need
199*a9fa9459Szrj to forge some data structures that it expects. */
200*a9fa9459Szrj
201*a9fa9459Szrj /* Fill in the bare minimum number of fields for our purposes. */
202*a9fa9459Szrj memset (&link_info, 0, sizeof (link_info));
203*a9fa9459Szrj link_info.output_bfd = abfd;
204*a9fa9459Szrj link_info.input_bfds = abfd;
205*a9fa9459Szrj link_info.input_bfds_tail = &abfd->link.next;
206*a9fa9459Szrj
207*a9fa9459Szrj link_next = abfd->link.next;
208*a9fa9459Szrj abfd->link.next = NULL;
209*a9fa9459Szrj link_info.hash = _bfd_generic_link_hash_table_create (abfd);
210*a9fa9459Szrj link_info.callbacks = &callbacks;
211*a9fa9459Szrj callbacks.warning = simple_dummy_warning;
212*a9fa9459Szrj callbacks.undefined_symbol = simple_dummy_undefined_symbol;
213*a9fa9459Szrj callbacks.reloc_overflow = simple_dummy_reloc_overflow;
214*a9fa9459Szrj callbacks.reloc_dangerous = simple_dummy_reloc_dangerous;
215*a9fa9459Szrj callbacks.unattached_reloc = simple_dummy_unattached_reloc;
216*a9fa9459Szrj callbacks.multiple_definition = simple_dummy_multiple_definition;
217*a9fa9459Szrj callbacks.einfo = simple_dummy_einfo;
218*a9fa9459Szrj
219*a9fa9459Szrj memset (&link_order, 0, sizeof (link_order));
220*a9fa9459Szrj link_order.next = NULL;
221*a9fa9459Szrj link_order.type = bfd_indirect_link_order;
222*a9fa9459Szrj link_order.offset = 0;
223*a9fa9459Szrj link_order.size = sec->size;
224*a9fa9459Szrj link_order.u.indirect.section = sec;
225*a9fa9459Szrj
226*a9fa9459Szrj data = NULL;
227*a9fa9459Szrj if (outbuf == NULL)
228*a9fa9459Szrj {
229*a9fa9459Szrj bfd_size_type amt = sec->rawsize > sec->size ? sec->rawsize : sec->size;
230*a9fa9459Szrj data = (bfd_byte *) bfd_malloc (amt);
231*a9fa9459Szrj if (data == NULL)
232*a9fa9459Szrj {
233*a9fa9459Szrj _bfd_generic_link_hash_table_free (abfd);
234*a9fa9459Szrj abfd->link.next = link_next;
235*a9fa9459Szrj return NULL;
236*a9fa9459Szrj }
237*a9fa9459Szrj outbuf = data;
238*a9fa9459Szrj }
239*a9fa9459Szrj
240*a9fa9459Szrj saved_offsets.section_count = abfd->section_count;
241*a9fa9459Szrj saved_offsets.sections = malloc (sizeof (*saved_offsets.sections)
242*a9fa9459Szrj * saved_offsets.section_count);
243*a9fa9459Szrj if (saved_offsets.sections == NULL)
244*a9fa9459Szrj {
245*a9fa9459Szrj if (data)
246*a9fa9459Szrj free (data);
247*a9fa9459Szrj _bfd_generic_link_hash_table_free (abfd);
248*a9fa9459Szrj abfd->link.next = link_next;
249*a9fa9459Szrj return NULL;
250*a9fa9459Szrj }
251*a9fa9459Szrj bfd_map_over_sections (abfd, simple_save_output_info, &saved_offsets);
252*a9fa9459Szrj
253*a9fa9459Szrj if (symbol_table == NULL)
254*a9fa9459Szrj {
255*a9fa9459Szrj _bfd_generic_link_add_symbols (abfd, &link_info);
256*a9fa9459Szrj
257*a9fa9459Szrj storage_needed = bfd_get_symtab_upper_bound (abfd);
258*a9fa9459Szrj symbol_table = (asymbol **) bfd_malloc (storage_needed);
259*a9fa9459Szrj bfd_canonicalize_symtab (abfd, symbol_table);
260*a9fa9459Szrj }
261*a9fa9459Szrj else
262*a9fa9459Szrj storage_needed = 0;
263*a9fa9459Szrj
264*a9fa9459Szrj contents = bfd_get_relocated_section_contents (abfd,
265*a9fa9459Szrj &link_info,
266*a9fa9459Szrj &link_order,
267*a9fa9459Szrj outbuf,
268*a9fa9459Szrj 0,
269*a9fa9459Szrj symbol_table);
270*a9fa9459Szrj if (contents == NULL && data != NULL)
271*a9fa9459Szrj free (data);
272*a9fa9459Szrj
273*a9fa9459Szrj bfd_map_over_sections (abfd, simple_restore_output_info, &saved_offsets);
274*a9fa9459Szrj free (saved_offsets.sections);
275*a9fa9459Szrj
276*a9fa9459Szrj _bfd_generic_link_hash_table_free (abfd);
277*a9fa9459Szrj abfd->link.next = link_next;
278*a9fa9459Szrj return contents;
279*a9fa9459Szrj }
280