1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2013-2016 Free Software Foundation, Inc.
4 # This file is part of GNU Binutils.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 # This file is sourced from elf32.em, and defines extra Nios II ELF
23 # specific routines. Taken from metagelf.em.
28 #include "elf32-nios2.h"
31 /* Fake input file for stubs. */
32 static lang_input_statement_type *stub_file;
34 /* Whether we need to call nios2_layout_sections_again. */
35 static int need_laying_out = 0;
38 /* This is called before the input files are opened. We create a new
39 fake input file to hold the stub sections. */
42 nios2elf_create_output_section_statements (void)
44 extern const bfd_target nios2_elf32_le_vec, nios2_elf32_be_vec;
46 if (link_info.output_bfd->xvec != &nios2_elf32_le_vec
47 && link_info.output_bfd->xvec != &nios2_elf32_be_vec)
50 /* If --no-relax was not explicitly specified by the user, enable
51 relaxation. If it's not enabled (either explicitly or by default),
52 we're done, as we won't need to create any stubs. */
53 if (!bfd_link_relocatable (&link_info) && RELAXATION_DISABLED_BY_DEFAULT)
55 if (!RELAXATION_ENABLED)
58 stub_file = lang_add_input_file ("linker stubs",
59 lang_input_file_is_fake_enum,
61 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
62 if (stub_file->the_bfd == NULL
63 || ! bfd_set_arch_mach (stub_file->the_bfd,
64 bfd_get_arch (link_info.output_bfd),
65 bfd_get_mach (link_info.output_bfd)))
67 einfo ("%X%P: can not create BFD %E\n");
71 stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
72 ldlang_add_file (stub_file);
78 lang_statement_list_type add;
79 asection *input_section;
82 /* Traverse the linker tree to find the spot where the stub goes. */
85 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp,
88 lang_statement_union_type *l;
91 for (; (l = *lp) != NULL; lp = &l->header.next)
93 switch (l->header.type)
95 case lang_constructors_statement_enum:
96 ret = hook_in_stub (info, &constructor_list.head, afterp);
101 case lang_output_section_statement_enum:
102 ret = hook_in_stub (info,
103 &l->output_section_statement.children.head,
109 case lang_wild_statement_enum:
110 ret = hook_in_stub (info, &l->wild_statement.children.head, afterp);
115 case lang_group_statement_enum:
116 ret = hook_in_stub (info, &l->group_statement.children.head, afterp);
121 case lang_input_section_enum:
122 if (l->input_section.section == info->input_section)
124 /* We've found our section. Insert the stub immediately
125 before or after its associated input section. */
128 *(info->add.tail) = l->header.next;
129 l->header.next = info->add.head;
133 *lp = info->add.head;
134 *(info->add.tail) = l;
140 case lang_data_statement_enum:
141 case lang_reloc_statement_enum:
142 case lang_object_symbols_statement_enum:
143 case lang_output_statement_enum:
144 case lang_target_statement_enum:
145 case lang_input_statement_enum:
146 case lang_assignment_statement_enum:
147 case lang_padding_statement_enum:
148 case lang_address_statement_enum:
149 case lang_fill_statement_enum:
160 /* Call-back for elf32_nios2_size_stubs. */
162 /* Create a new stub section, and arrange for it to be linked
163 immediately before or after INPUT_SECTION, according to AFTERP. */
166 nios2elf_add_stub_section (const char *stub_sec_name, asection *input_section,
171 asection *output_section;
173 lang_output_section_statement_type *os;
174 struct hook_stub_info info;
176 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
177 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
178 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
179 stub_sec_name, flags);
180 if (stub_sec == NULL)
183 output_section = input_section->output_section;
184 secname = bfd_get_section_name (output_section->owner, output_section);
185 os = lang_output_section_find (secname);
187 info.input_section = input_section;
188 lang_list_init (&info.add);
189 lang_add_section (&info.add, stub_sec, NULL, os);
191 if (info.add.head == NULL)
194 if (hook_in_stub (&info, &os->children.head, afterp))
198 einfo ("%X%P: can not make stub section: %E\n");
203 /* Another call-back for elf32_nios2_size_stubs. */
206 nios2elf_layout_sections_again (void)
208 /* If we have changed sizes of the stub sections, then we need
209 to recalculate all the section offsets. This may mean we need to
210 add even more stubs. */
211 gld${EMULATION_NAME}_map_segments (TRUE);
212 need_laying_out = -1;
217 build_section_lists (lang_statement_union_type *statement)
219 if (statement->header.type == lang_input_section_enum)
221 asection *i = statement->input_section.section;
223 if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
224 && (i->flags & SEC_EXCLUDE) == 0
225 && i->output_section != NULL
226 && i->output_section->owner == link_info.output_bfd)
228 nios2_elf32_next_input_section (&link_info, i);
234 /* For Nios II we use this opportunity to build linker stubs. */
237 gld${EMULATION_NAME}_after_allocation (void)
241 /* bfd_elf_discard_info just plays with data and debugging sections,
242 ie. doesn't affect code size, so we can delay resizing the
243 sections. It's likely we'll resize everything in the process of
245 ret = bfd_elf_discard_info (link_info.output_bfd, &link_info);
248 einfo ("%X%P: .eh_frame/.stab edit: %E\n");
254 /* If generating a relocatable output file, then we don't
255 have to examine the relocs. */
256 if (stub_file != NULL
257 && !bfd_link_relocatable (&link_info)
258 && RELAXATION_ENABLED)
260 ret = nios2_elf32_setup_section_lists (link_info.output_bfd, &link_info);
265 einfo ("%X%P: can not size stub section: %E\n");
269 lang_for_each_statement (build_section_lists);
271 /* Call into the BFD backend to do the real work. */
272 if (! nios2_elf32_size_stubs (link_info.output_bfd,
275 &nios2elf_add_stub_section,
276 &nios2elf_layout_sections_again))
278 einfo ("%X%P: can not size stub section: %E\n");
284 if (need_laying_out != -1)
285 gld${EMULATION_NAME}_map_segments (need_laying_out);
287 if (!bfd_link_relocatable (&link_info) && RELAXATION_ENABLED)
289 /* Now build the linker stubs. */
290 if (stub_file != NULL && stub_file->the_bfd->sections != NULL)
292 if (! nios2_elf32_build_stubs (&link_info))
293 einfo ("%X%P: can not build stubs: %E\n");
299 /* Avoid processing the fake stub_file in vercheck, stat_needed and
300 check_needed routines. */
302 static void (*real_func) (lang_input_statement_type *);
304 static void nios2_for_each_input_file_wrapper (lang_input_statement_type *l)
311 nios2_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
314 lang_for_each_input_file (&nios2_for_each_input_file_wrapper);
317 #define lang_for_each_input_file nios2_lang_for_each_input_file
322 # Put these extra nios2elf routines in ld_${EMULATION_NAME}_emulation
324 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
325 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=nios2elf_create_output_section_statements