Automatic date update in version.in
[deliverable/binutils-gdb.git] / ld / emultempl / avrelf.em
CommitLineData
28c9d252 1# This shell script emits a C file. -*- C -*-
2571583a 2# Copyright (C) 2006-2017 Free Software Foundation, Inc.
28c9d252 3#
f96b4a7b 4# This file is part of the GNU Binutils.
28c9d252
NC
5#
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
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
28c9d252
NC
9# (at your option) any later version.
10#
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.
15#
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
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
28c9d252 20
f96b4a7b
NC
21
22# This file is sourced from elf32.em, and defines extra avr-elf specific
23# routines. It is used to generate the trampolines for the avr6 family
24# of devices where one needs to address the issue that it is not possible
28c9d252
NC
25# to reach the whole program memory by using 16 bit pointers.
26
92b93329 27fragment <<EOF
28c9d252
NC
28
29#include "elf32-avr.h"
30#include "ldctor.h"
9d7b48dc 31#include "elf/avr.h"
28c9d252 32
92b93329 33/* The fake file and it's corresponding section meant to hold
28c9d252
NC
34 the linker stubs if needed. */
35
36static lang_input_statement_type *stub_file;
37static asection *avr_stub_section;
38
839b0b3f 39/* Variables set by the command-line parameters and transferred
28c9d252
NC
40 to the bfd without use of global shared variables. */
41
42static bfd_boolean avr_no_stubs = FALSE;
43static bfd_boolean avr_debug_relax = FALSE;
44static bfd_boolean avr_debug_stubs = FALSE;
45static bfd_boolean avr_replace_call_ret_sequences = TRUE;
46static bfd_vma avr_pc_wrap_around = 0x10000000;
47
48/* Transfers information to the bfd frontend. */
49
50static void
51avr_elf_set_global_bfd_parameters (void)
52{
53 elf32_avr_setup_params (& link_info,
54 stub_file->the_bfd,
55 avr_stub_section,
56 avr_no_stubs,
57 avr_debug_stubs,
58 avr_debug_relax,
59 avr_pc_wrap_around,
60 avr_replace_call_ret_sequences);
61}
62
63
64/* Makes a conservative estimate of the trampoline section size that could
65 be corrected later on. */
66
67static void
68avr_elf_${EMULATION_NAME}_before_allocation (void)
69{
70 int ret;
71
72 gld${EMULATION_NAME}_before_allocation ();
73
fd9770d8
NC
74 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
75 {
76 avr_no_stubs = TRUE;
77 return;
78 }
79
8cc66334 80 /* We only need stubs for avr6, avrxmega6, and avrxmega7. */
e4492aa0 81 if (strcmp ("${EMULATION_NAME}","avr6")
8cc66334
EW
82 && strcmp ("${EMULATION_NAME}","avrxmega6")
83 && strcmp ("${EMULATION_NAME}","avrxmega7") )
28c9d252
NC
84 avr_no_stubs = TRUE;
85
86 avr_elf_set_global_bfd_parameters ();
87
88 /* If generating a relocatable output file, then
89 we don't have to generate the trampolines. */
0e1862bb 90 if (bfd_link_relocatable (&link_info))
28c9d252
NC
91 avr_no_stubs = TRUE;
92
93 if (avr_no_stubs)
94 return;
95
f13a99db 96 ret = elf32_avr_setup_section_lists (link_info.output_bfd, &link_info);
28c9d252
NC
97
98 if (ret < 0)
99 einfo ("%X%P: can not setup the input section list: %E\n");
100
101 if (ret <= 0)
102 return;
103
104 /* Call into the BFD backend to do the real "stub"-work. */
f13a99db 105 if (! elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE))
28c9d252
NC
106 einfo ("%X%P: can not size stub section: %E\n");
107}
108
109/* This is called before the input files are opened. We create a new
110 fake input file to hold the stub section and generate the section itself. */
111
112static void
113avr_elf_create_output_section_statements (void)
114{
115 flagword flags;
116
fd9770d8
NC
117 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
118 {
119 einfo ("%X%P: changing output format whilst linking is not supported\n");
120 return;
121 }
122
28c9d252
NC
123 stub_file = lang_add_input_file ("linker stubs",
124 lang_input_file_is_fake_enum,
125 NULL);
126
f13a99db 127 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
28c9d252
NC
128 if (stub_file->the_bfd == NULL
129 || !bfd_set_arch_mach (stub_file->the_bfd,
f13a99db
AM
130 bfd_get_arch (link_info.output_bfd),
131 bfd_get_mach (link_info.output_bfd)))
28c9d252
NC
132 {
133 einfo ("%X%P: can not create stub BFD %E\n");
134 return;
135 }
136
137 /* Now we add the stub section. */
138
28c9d252
NC
139 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
140 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
9795b468
AM
141 avr_stub_section = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
142 ".trampolines",
143 flags);
144 if (avr_stub_section == NULL)
28c9d252
NC
145 goto err_ret;
146
147 avr_stub_section->alignment_power = 1;
92b93329 148
28c9d252
NC
149 ldlang_add_file (stub_file);
150
151 return;
152
153 err_ret:
154 einfo ("%X%P: can not make stub section: %E\n");
155 return;
156}
157
158/* Re-calculates the size of the stubs so that we won't waste space. */
159
160static void
eaeb0a9d 161avr_elf_after_allocation (void)
92b93329 162{
28d5f677 163 if (!avr_no_stubs && ! RELAXATION_ENABLED)
28c9d252 164 {
eaeb0a9d
AM
165 /* If relaxing, elf32_avr_size_stubs will be called from
166 elf32_avr_relax_section. */
43315eb7 167 if (!elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE))
eaeb0a9d 168 einfo ("%X%P: can not size stub section: %E\n");
28c9d252
NC
169 }
170
eaeb0a9d
AM
171 gld${EMULATION_NAME}_after_allocation ();
172
173 /* Now build the linker stubs. */
174 if (!avr_no_stubs)
175 {
176 if (!elf32_avr_build_stubs (&link_info))
177 einfo ("%X%P: can not build stubs: %E\n");
178 }
28c9d252
NC
179}
180
4d4ef6fd
AM
181static void
182avr_elf_before_parse (void)
183{
184 /* Don't create a demand-paged executable, since this feature isn't
185 meaningful in AVR. */
186 config.magic_demand_paged = FALSE;
187
188 gld${EMULATION_NAME}_before_parse ();
189}
28c9d252 190
9d7b48dc
AB
191static void
192avr_finish (void)
193{
194 bfd *abfd;
195 bfd_boolean avr_link_relax;
196
0e1862bb 197 if (bfd_link_relocatable (&link_info))
9d7b48dc
AB
198 {
199 avr_link_relax = TRUE;
200 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
201 {
202 /* Don't let the linker stubs prevent the final object being
203 marked as link-relax ready. */
204 if ((elf_elfheader (abfd)->e_flags
205 & EF_AVR_LINKRELAX_PREPARED) == 0
206 && abfd != stub_file->the_bfd)
207 {
208 avr_link_relax = FALSE;
209 break;
210 }
211 }
212 }
213 else
214 {
215 avr_link_relax = RELAXATION_ENABLED;
216 }
217
218 abfd = link_info.output_bfd;
fd9770d8
NC
219
220 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
221 {
222 if (avr_link_relax)
223 elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
224 else
225 elf_elfheader (abfd)->e_flags &= ~EF_AVR_LINKRELAX_PREPARED;
226 }
9d7b48dc
AB
227
228 finish_default ();
229}
28c9d252
NC
230EOF
231
232
233PARSE_AND_LIST_PROLOGUE='
234
235#define OPTION_NO_CALL_RET_REPLACEMENT 301
236#define OPTION_PMEM_WRAP_AROUND 302
237#define OPTION_NO_STUBS 303
238#define OPTION_DEBUG_STUBS 304
239#define OPTION_DEBUG_RELAX 305
240'
241
242PARSE_AND_LIST_LONGOPTS='
92b93329 243 { "no-call-ret-replacement", no_argument,
28c9d252 244 NULL, OPTION_NO_CALL_RET_REPLACEMENT},
92b93329 245 { "pmem-wrap-around", required_argument,
28c9d252 246 NULL, OPTION_PMEM_WRAP_AROUND},
92b93329 247 { "no-stubs", no_argument,
28c9d252 248 NULL, OPTION_NO_STUBS},
92b93329 249 { "debug-stubs", no_argument,
28c9d252 250 NULL, OPTION_DEBUG_STUBS},
92b93329 251 { "debug-relax", no_argument,
28c9d252
NC
252 NULL, OPTION_DEBUG_RELAX},
253'
254
255PARSE_AND_LIST_OPTIONS='
442996ee
AM
256 fprintf (file, _(" --pmem-wrap-around=<val> "
257 "Make the linker relaxation machine assume that a\n"
258 " "
259 " program counter wrap-around occures at address\n"
260 " "
261 " <val>. Supported values: 8k, 16k, 32k and 64k.\n"));
262 fprintf (file, _(" --no-call-ret-replacement "
263 "The relaxation machine normally will\n"
264 " "
265 " substitute two immediately following call/ret\n"
266 " "
267 " instructions by a single jump instruction.\n"
268 " "
269 " This option disables this optimization.\n"));
270 fprintf (file, _(" --no-stubs "
271 "If the linker detects to attempt to access\n"
272 " "
273 " an instruction beyond 128k by a reloc that\n"
274 " "
275 " is limited to 128k max, it inserts a jump\n"
276 " "
277 " stub. You can de-active this with this switch.\n"));
278 fprintf (file, _(" --debug-stubs "
279 "Used for debugging avr-ld.\n"));
280 fprintf (file, _(" --debug-relax "
281 "Used for debugging avr-ld.\n"));
28c9d252
NC
282'
283
284PARSE_AND_LIST_ARGS_CASES='
285
286 case OPTION_PMEM_WRAP_AROUND:
92b93329 287 {
28c9d252
NC
288 /* This variable is defined in the bfd library. */
289 if ((!strcmp (optarg,"32k")) || (!strcmp (optarg,"32K")))
290 avr_pc_wrap_around = 32768;
2a69000b
DC
291 else if ((!strcmp (optarg,"8k")) || (!strcmp (optarg,"8K")))
292 avr_pc_wrap_around = 8192;
28c9d252
NC
293 else if ((!strcmp (optarg,"16k")) || (!strcmp (optarg,"16K")))
294 avr_pc_wrap_around = 16384;
295 else if ((!strcmp (optarg,"64k")) || (!strcmp (optarg,"64K")))
296 avr_pc_wrap_around = 0x10000;
297 else
298 return FALSE;
299 }
300 break;
301
302 case OPTION_DEBUG_STUBS:
303 avr_debug_stubs = TRUE;
304 break;
305
306 case OPTION_DEBUG_RELAX:
307 avr_debug_relax = TRUE;
308 break;
309
310 case OPTION_NO_STUBS:
311 avr_no_stubs = TRUE;
312 break;
313
314 case OPTION_NO_CALL_RET_REPLACEMENT:
315 {
316 /* This variable is defined in the bfd library. */
317 avr_replace_call_ret_sequences = FALSE;
318 }
319 break;
320'
321
322#
323# Put these extra avr-elf routines in ld_${EMULATION_NAME}_emulation
324#
4d4ef6fd 325LDEMUL_BEFORE_PARSE=avr_elf_before_parse
28c9d252 326LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation
eaeb0a9d 327LDEMUL_AFTER_ALLOCATION=avr_elf_after_allocation
28c9d252 328LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements
9d7b48dc 329LDEMUL_FINISH=avr_finish
This page took 0.475769 seconds and 4 git commands to generate.