1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2006, 2007, 2008, 2009
3 # Free Software Foundation, Inc.
5 # This file is part of the GNU Binutils.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 # This file is sourced from elf32.em, and defines extra avr-elf specific
24 # routines. It is used to generate the trampolines for the avr6 family
25 # of devices where one needs to address the issue that it is not possible
26 # to reach the whole program memory by using 16 bit pointers.
30 #include "elf32-avr.h"
33 /* The fake file and it's corresponding section meant to hold
34 the linker stubs if needed. */
36 static lang_input_statement_type *stub_file;
37 static asection *avr_stub_section;
39 /* Variables set by the command-line parameters and transfered
40 to the bfd without use of global shared variables. */
42 static bfd_boolean avr_no_stubs = FALSE;
43 static bfd_boolean avr_debug_relax = FALSE;
44 static bfd_boolean avr_debug_stubs = FALSE;
45 static bfd_boolean avr_replace_call_ret_sequences = TRUE;
46 static bfd_vma avr_pc_wrap_around = 0x10000000;
48 /* Transfers information to the bfd frontend. */
51 avr_elf_set_global_bfd_parameters (void)
53 elf32_avr_setup_params (& link_info,
60 avr_replace_call_ret_sequences);
64 /* Makes a conservative estimate of the trampoline section size that could
65 be corrected later on. */
68 avr_elf_${EMULATION_NAME}_before_allocation (void)
72 gld${EMULATION_NAME}_before_allocation ();
74 /* We only need stubs for the avr6 family. */
75 if (strcmp ("${EMULATION_NAME}","avr6"))
78 avr_elf_set_global_bfd_parameters ();
80 /* If generating a relocatable output file, then
81 we don't have to generate the trampolines. */
82 if (link_info.relocatable)
88 ret = elf32_avr_setup_section_lists (link_info.output_bfd, &link_info);
91 einfo ("%X%P: can not setup the input section list: %E\n");
96 /* Call into the BFD backend to do the real "stub"-work. */
97 if (! elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE))
98 einfo ("%X%P: can not size stub section: %E\n");
101 /* This is called before the input files are opened. We create a new
102 fake input file to hold the stub section and generate the section itself. */
105 avr_elf_create_output_section_statements (void)
109 stub_file = lang_add_input_file ("linker stubs",
110 lang_input_file_is_fake_enum,
113 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
114 if (stub_file->the_bfd == NULL
115 || !bfd_set_arch_mach (stub_file->the_bfd,
116 bfd_get_arch (link_info.output_bfd),
117 bfd_get_mach (link_info.output_bfd)))
119 einfo ("%X%P: can not create stub BFD %E\n");
123 /* Now we add the stub section. */
125 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
126 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
127 avr_stub_section = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
130 if (avr_stub_section == NULL)
133 avr_stub_section->alignment_power = 1;
135 ldlang_add_file (stub_file);
140 einfo ("%X%P: can not make stub section: %E\n");
144 /* Re-calculates the size of the stubs so that we won't waste space. */
147 avr_elf_after_allocation (void)
149 if (!avr_no_stubs && ! RELAXATION_ENABLED)
151 /* If relaxing, elf32_avr_size_stubs will be called from
152 elf32_avr_relax_section. */
153 if (!elf32_avr_size_stubs (link_info.output_bfd, &link_info, FALSE))
154 einfo ("%X%P: can not size stub section: %E\n");
157 gld${EMULATION_NAME}_after_allocation ();
159 /* Now build the linker stubs. */
162 if (!elf32_avr_build_stubs (&link_info))
163 einfo ("%X%P: can not build stubs: %E\n");
171 PARSE_AND_LIST_PROLOGUE='
173 #define OPTION_NO_CALL_RET_REPLACEMENT 301
174 #define OPTION_PMEM_WRAP_AROUND 302
175 #define OPTION_NO_STUBS 303
176 #define OPTION_DEBUG_STUBS 304
177 #define OPTION_DEBUG_RELAX 305
180 PARSE_AND_LIST_LONGOPTS='
181 { "no-call-ret-replacement", no_argument,
182 NULL, OPTION_NO_CALL_RET_REPLACEMENT},
183 { "pmem-wrap-around", required_argument,
184 NULL, OPTION_PMEM_WRAP_AROUND},
185 { "no-stubs", no_argument,
186 NULL, OPTION_NO_STUBS},
187 { "debug-stubs", no_argument,
188 NULL, OPTION_DEBUG_STUBS},
189 { "debug-relax", no_argument,
190 NULL, OPTION_DEBUG_RELAX},
193 PARSE_AND_LIST_OPTIONS='
194 fprintf (file, _(" --pmem-wrap-around=<val> "
195 "Make the linker relaxation machine assume that a\n"
197 " program counter wrap-around occures at address\n"
199 " <val>. Supported values: 8k, 16k, 32k and 64k.\n"));
200 fprintf (file, _(" --no-call-ret-replacement "
201 "The relaxation machine normally will\n"
203 " substitute two immediately following call/ret\n"
205 " instructions by a single jump instruction.\n"
207 " This option disables this optimization.\n"));
208 fprintf (file, _(" --no-stubs "
209 "If the linker detects to attempt to access\n"
211 " an instruction beyond 128k by a reloc that\n"
213 " is limited to 128k max, it inserts a jump\n"
215 " stub. You can de-active this with this switch.\n"));
216 fprintf (file, _(" --debug-stubs "
217 "Used for debugging avr-ld.\n"));
218 fprintf (file, _(" --debug-relax "
219 "Used for debugging avr-ld.\n"));
222 PARSE_AND_LIST_ARGS_CASES='
224 case OPTION_PMEM_WRAP_AROUND:
226 /* This variable is defined in the bfd library. */
227 if ((!strcmp (optarg,"32k")) || (!strcmp (optarg,"32K")))
228 avr_pc_wrap_around = 32768;
229 else if ((!strcmp (optarg,"8k")) || (!strcmp (optarg,"8K")))
230 avr_pc_wrap_around = 8192;
231 else if ((!strcmp (optarg,"16k")) || (!strcmp (optarg,"16K")))
232 avr_pc_wrap_around = 16384;
233 else if ((!strcmp (optarg,"64k")) || (!strcmp (optarg,"64K")))
234 avr_pc_wrap_around = 0x10000;
240 case OPTION_DEBUG_STUBS:
241 avr_debug_stubs = TRUE;
244 case OPTION_DEBUG_RELAX:
245 avr_debug_relax = TRUE;
248 case OPTION_NO_STUBS:
252 case OPTION_NO_CALL_RET_REPLACEMENT:
254 /* This variable is defined in the bfd library. */
255 avr_replace_call_ret_sequences = FALSE;
261 # Put these extra avr-elf routines in ld_${EMULATION_NAME}_emulation
263 LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation
264 LDEMUL_AFTER_ALLOCATION=avr_elf_after_allocation
265 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements