1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2013 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 metagelf
23 # specific routines. Taken from hppaelf.em.
28 #include "elf32-metag.h"
31 /* Fake input file for stubs. */
32 static lang_input_statement_type *stub_file;
34 /* Whether we need to call metag_layout_sections_again. */
35 static int need_laying_out = 0;
37 /* Maximum size of a group of input sections that can be handled by
38 one stub section. A value of +/-1 indicates the bfd back-end
39 should use a suitable default size. */
40 static bfd_signed_vma group_size = 1;
42 /* This is called before the input files are opened. We create a new
43 fake input file to hold the stub sections. */
46 metagelf_create_output_section_statements (void)
48 extern const bfd_target bfd_elf32_metag_vec;
50 if (link_info.output_bfd->xvec != &bfd_elf32_metag_vec)
53 stub_file = lang_add_input_file ("linker stubs",
54 lang_input_file_is_fake_enum,
56 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
57 if (stub_file->the_bfd == NULL
58 || ! bfd_set_arch_mach (stub_file->the_bfd,
59 bfd_get_arch (link_info.output_bfd),
60 bfd_get_mach (link_info.output_bfd)))
62 einfo ("%X%P: can not create BFD %E\n");
66 stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
67 ldlang_add_file (stub_file);
73 lang_statement_list_type add;
74 asection *input_section;
77 /* Traverse the linker tree to find the spot where the stub goes. */
80 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
82 lang_statement_union_type *l;
85 for (; (l = *lp) != NULL; lp = &l->header.next)
87 switch (l->header.type)
89 case lang_constructors_statement_enum:
90 ret = hook_in_stub (info, &constructor_list.head);
95 case lang_output_section_statement_enum:
96 ret = hook_in_stub (info,
97 &l->output_section_statement.children.head);
102 case lang_wild_statement_enum:
103 ret = hook_in_stub (info, &l->wild_statement.children.head);
108 case lang_group_statement_enum:
109 ret = hook_in_stub (info, &l->group_statement.children.head);
114 case lang_input_section_enum:
115 if (l->input_section.section == info->input_section)
117 /* We've found our section. Insert the stub immediately
118 before its associated input section. */
119 *lp = info->add.head;
120 *(info->add.tail) = l;
125 case lang_data_statement_enum:
126 case lang_reloc_statement_enum:
127 case lang_object_symbols_statement_enum:
128 case lang_output_statement_enum:
129 case lang_target_statement_enum:
130 case lang_input_statement_enum:
131 case lang_assignment_statement_enum:
132 case lang_padding_statement_enum:
133 case lang_address_statement_enum:
134 case lang_fill_statement_enum:
146 /* Call-back for elf_metag_size_stubs. */
148 /* Create a new stub section, and arrange for it to be linked
149 immediately before INPUT_SECTION. */
152 metagelf_add_stub_section (const char *stub_sec_name, asection *input_section)
156 asection *output_section;
158 lang_output_section_statement_type *os;
159 struct hook_stub_info info;
161 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
162 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
163 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
164 stub_sec_name, flags);
165 if (stub_sec == NULL)
168 output_section = input_section->output_section;
169 secname = bfd_get_section_name (output_section->owner, output_section);
170 os = lang_output_section_find (secname);
172 info.input_section = input_section;
173 lang_list_init (&info.add);
174 lang_add_section (&info.add, stub_sec, NULL, os);
176 if (info.add.head == NULL)
179 if (hook_in_stub (&info, &os->children.head))
183 einfo ("%X%P: can not make stub section: %E\n");
188 /* Another call-back for elf_metag_size_stubs. */
191 metagelf_layout_sections_again (void)
193 /* If we have changed sizes of the stub sections, then we need
194 to recalculate all the section offsets. This may mean we need to
195 add even more stubs. */
196 gld${EMULATION_NAME}_map_segments (TRUE);
197 need_laying_out = -1;
202 build_section_lists (lang_statement_union_type *statement)
204 if (statement->header.type == lang_input_section_enum)
206 asection *i = statement->input_section.section;
208 if (!((lang_input_statement_type *) i->owner->usrdata)->flags.just_syms
209 && (i->flags & SEC_EXCLUDE) == 0
210 && i->output_section != NULL
211 && i->output_section->owner == link_info.output_bfd)
213 elf_metag_next_input_section (&link_info, i);
219 /* For Meta we use this opportunity to build linker stubs. */
222 gld${EMULATION_NAME}_after_allocation (void)
224 /* bfd_elf_discard_info just plays with data and debugging sections,
225 ie. doesn't affect code size, so we can delay resizing the
226 sections. It's likely we'll resize everything in the process of
228 if (bfd_elf_discard_info (link_info.output_bfd, &link_info))
231 /* If generating a relocatable output file, then we don't
232 have to examine the relocs. */
233 if (stub_file != NULL && !link_info.relocatable)
235 int ret = elf_metag_setup_section_lists (link_info.output_bfd,
242 einfo ("%X%P: can not size stub section: %E\n");
246 lang_for_each_statement (build_section_lists);
248 /* Call into the BFD backend to do the real work. */
249 if (! elf_metag_size_stubs (link_info.output_bfd,
253 &metagelf_add_stub_section,
254 &metagelf_layout_sections_again))
256 einfo ("%X%P: can not size stub section: %E\n");
262 if (need_laying_out != -1)
263 gld${EMULATION_NAME}_map_segments (need_laying_out);
265 if (! link_info.relocatable)
267 /* Now build the linker stubs. */
268 if (stub_file != NULL && stub_file->the_bfd->sections != NULL)
270 if (! elf_metag_build_stubs (&link_info))
271 einfo ("%X%P: can not build stubs: %E\n");
277 /* Avoid processing the fake stub_file in vercheck, stat_needed and
278 check_needed routines. */
280 static void (*real_func) (lang_input_statement_type *);
282 static void metag_for_each_input_file_wrapper (lang_input_statement_type *l)
289 metag_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
292 lang_for_each_input_file (&metag_for_each_input_file_wrapper);
295 #define lang_for_each_input_file metag_lang_for_each_input_file
299 # Define some shell vars to insert bits of code into the standard elf
300 # parse_args and list_options functions.
302 PARSE_AND_LIST_PROLOGUE='
303 #define OPTION_STUBGROUP_SIZE 301
306 PARSE_AND_LIST_LONGOPTS='
307 { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
310 PARSE_AND_LIST_OPTIONS='
312 --stub-group-size=N Maximum size of a group of input sections that\n\
313 can be handled by one stub section. A negative\n\
314 value locates all stubs before their branches\n\
315 (with a group size of -N), while a positive\n\
316 value allows two groups of input sections, one\n\
317 before, and one after each stub section.\n\
318 Values of +/-1 indicate the linker should\n\
319 choose suitable defaults.\n"
323 PARSE_AND_LIST_ARGS_CASES='
324 case OPTION_STUBGROUP_SIZE:
327 group_size = bfd_scan_vma (optarg, &end, 0);
329 einfo (_("%P%F: invalid number `%s'\''\n"), optarg);
334 # Put these extra metagelf routines in ld_${EMULATION_NAME}_emulation
336 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
337 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=metagelf_create_output_section_statements