Commit | Line | Data |
---|---|---|
28c9d252 | 1 | # This shell script emits a C file. -*- C -*- |
4b95cf5c | 2 | # Copyright (C) 2006-2014 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 | 27 | fragment <<EOF |
28c9d252 NC |
28 | |
29 | #include "elf32-avr.h" | |
30 | #include "ldctor.h" | |
31 | ||
92b93329 | 32 | /* The fake file and it's corresponding section meant to hold |
28c9d252 NC |
33 | the linker stubs if needed. */ |
34 | ||
35 | static lang_input_statement_type *stub_file; | |
36 | static asection *avr_stub_section; | |
37 | ||
38 | /* Variables set by the command-line parameters and transfered | |
39 | to the bfd without use of global shared variables. */ | |
40 | ||
41 | static bfd_boolean avr_no_stubs = FALSE; | |
42 | static bfd_boolean avr_debug_relax = FALSE; | |
43 | static bfd_boolean avr_debug_stubs = FALSE; | |
44 | static bfd_boolean avr_replace_call_ret_sequences = TRUE; | |
45 | static bfd_vma avr_pc_wrap_around = 0x10000000; | |
46 | ||
47 | /* Transfers information to the bfd frontend. */ | |
48 | ||
49 | static void | |
50 | avr_elf_set_global_bfd_parameters (void) | |
51 | { | |
52 | elf32_avr_setup_params (& link_info, | |
53 | stub_file->the_bfd, | |
54 | avr_stub_section, | |
55 | avr_no_stubs, | |
56 | avr_debug_stubs, | |
57 | avr_debug_relax, | |
58 | avr_pc_wrap_around, | |
59 | avr_replace_call_ret_sequences); | |
60 | } | |
61 | ||
62 | ||
63 | /* Makes a conservative estimate of the trampoline section size that could | |
64 | be corrected later on. */ | |
65 | ||
66 | static void | |
67 | avr_elf_${EMULATION_NAME}_before_allocation (void) | |
68 | { | |
69 | int ret; | |
70 | ||
71 | gld${EMULATION_NAME}_before_allocation (); | |
72 | ||
8cc66334 | 73 | /* We only need stubs for avr6, avrxmega6, and avrxmega7. */ |
e4492aa0 | 74 | if (strcmp ("${EMULATION_NAME}","avr6") |
8cc66334 EW |
75 | && strcmp ("${EMULATION_NAME}","avrxmega6") |
76 | && strcmp ("${EMULATION_NAME}","avrxmega7") ) | |
28c9d252 NC |
77 | avr_no_stubs = TRUE; |
78 | ||
79 | avr_elf_set_global_bfd_parameters (); | |
80 | ||
81 | /* If generating a relocatable output file, then | |
82 | we don't have to generate the trampolines. */ | |
83 | if (link_info.relocatable) | |
84 | avr_no_stubs = TRUE; | |
85 | ||
86 | if (avr_no_stubs) | |
87 | return; | |
88 | ||
f13a99db | 89 | ret = elf32_avr_setup_section_lists (link_info.output_bfd, &link_info); |
28c9d252 NC |
90 | |
91 | if (ret < 0) | |
92 | einfo ("%X%P: can not setup the input section list: %E\n"); | |
93 | ||
94 | if (ret <= 0) | |
95 | return; | |
96 | ||
97 | /* Call into the BFD backend to do the real "stub"-work. */ | |
f13a99db | 98 | if (! elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE)) |
28c9d252 NC |
99 | einfo ("%X%P: can not size stub section: %E\n"); |
100 | } | |
101 | ||
102 | /* This is called before the input files are opened. We create a new | |
103 | fake input file to hold the stub section and generate the section itself. */ | |
104 | ||
105 | static void | |
106 | avr_elf_create_output_section_statements (void) | |
107 | { | |
108 | flagword flags; | |
109 | ||
110 | stub_file = lang_add_input_file ("linker stubs", | |
111 | lang_input_file_is_fake_enum, | |
112 | NULL); | |
113 | ||
f13a99db | 114 | stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd); |
28c9d252 NC |
115 | if (stub_file->the_bfd == NULL |
116 | || !bfd_set_arch_mach (stub_file->the_bfd, | |
f13a99db AM |
117 | bfd_get_arch (link_info.output_bfd), |
118 | bfd_get_mach (link_info.output_bfd))) | |
28c9d252 NC |
119 | { |
120 | einfo ("%X%P: can not create stub BFD %E\n"); | |
121 | return; | |
122 | } | |
123 | ||
124 | /* Now we add the stub section. */ | |
125 | ||
28c9d252 NC |
126 | flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE |
127 | | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP); | |
9795b468 AM |
128 | avr_stub_section = bfd_make_section_anyway_with_flags (stub_file->the_bfd, |
129 | ".trampolines", | |
130 | flags); | |
131 | if (avr_stub_section == NULL) | |
28c9d252 NC |
132 | goto err_ret; |
133 | ||
134 | avr_stub_section->alignment_power = 1; | |
92b93329 | 135 | |
28c9d252 NC |
136 | ldlang_add_file (stub_file); |
137 | ||
138 | return; | |
139 | ||
140 | err_ret: | |
141 | einfo ("%X%P: can not make stub section: %E\n"); | |
142 | return; | |
143 | } | |
144 | ||
145 | /* Re-calculates the size of the stubs so that we won't waste space. */ | |
146 | ||
147 | static void | |
eaeb0a9d | 148 | avr_elf_after_allocation (void) |
92b93329 | 149 | { |
28d5f677 | 150 | if (!avr_no_stubs && ! RELAXATION_ENABLED) |
28c9d252 | 151 | { |
eaeb0a9d AM |
152 | /* If relaxing, elf32_avr_size_stubs will be called from |
153 | elf32_avr_relax_section. */ | |
43315eb7 | 154 | if (!elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE)) |
eaeb0a9d | 155 | einfo ("%X%P: can not size stub section: %E\n"); |
28c9d252 NC |
156 | } |
157 | ||
eaeb0a9d AM |
158 | gld${EMULATION_NAME}_after_allocation (); |
159 | ||
160 | /* Now build the linker stubs. */ | |
161 | if (!avr_no_stubs) | |
162 | { | |
163 | if (!elf32_avr_build_stubs (&link_info)) | |
164 | einfo ("%X%P: can not build stubs: %E\n"); | |
165 | } | |
28c9d252 NC |
166 | } |
167 | ||
4d4ef6fd AM |
168 | static void |
169 | avr_elf_before_parse (void) | |
170 | { | |
171 | /* Don't create a demand-paged executable, since this feature isn't | |
172 | meaningful in AVR. */ | |
173 | config.magic_demand_paged = FALSE; | |
174 | ||
175 | gld${EMULATION_NAME}_before_parse (); | |
176 | } | |
28c9d252 NC |
177 | |
178 | EOF | |
179 | ||
180 | ||
181 | PARSE_AND_LIST_PROLOGUE=' | |
182 | ||
183 | #define OPTION_NO_CALL_RET_REPLACEMENT 301 | |
184 | #define OPTION_PMEM_WRAP_AROUND 302 | |
185 | #define OPTION_NO_STUBS 303 | |
186 | #define OPTION_DEBUG_STUBS 304 | |
187 | #define OPTION_DEBUG_RELAX 305 | |
188 | ' | |
189 | ||
190 | PARSE_AND_LIST_LONGOPTS=' | |
92b93329 | 191 | { "no-call-ret-replacement", no_argument, |
28c9d252 | 192 | NULL, OPTION_NO_CALL_RET_REPLACEMENT}, |
92b93329 | 193 | { "pmem-wrap-around", required_argument, |
28c9d252 | 194 | NULL, OPTION_PMEM_WRAP_AROUND}, |
92b93329 | 195 | { "no-stubs", no_argument, |
28c9d252 | 196 | NULL, OPTION_NO_STUBS}, |
92b93329 | 197 | { "debug-stubs", no_argument, |
28c9d252 | 198 | NULL, OPTION_DEBUG_STUBS}, |
92b93329 | 199 | { "debug-relax", no_argument, |
28c9d252 NC |
200 | NULL, OPTION_DEBUG_RELAX}, |
201 | ' | |
202 | ||
203 | PARSE_AND_LIST_OPTIONS=' | |
442996ee AM |
204 | fprintf (file, _(" --pmem-wrap-around=<val> " |
205 | "Make the linker relaxation machine assume that a\n" | |
206 | " " | |
207 | " program counter wrap-around occures at address\n" | |
208 | " " | |
209 | " <val>. Supported values: 8k, 16k, 32k and 64k.\n")); | |
210 | fprintf (file, _(" --no-call-ret-replacement " | |
211 | "The relaxation machine normally will\n" | |
212 | " " | |
213 | " substitute two immediately following call/ret\n" | |
214 | " " | |
215 | " instructions by a single jump instruction.\n" | |
216 | " " | |
217 | " This option disables this optimization.\n")); | |
218 | fprintf (file, _(" --no-stubs " | |
219 | "If the linker detects to attempt to access\n" | |
220 | " " | |
221 | " an instruction beyond 128k by a reloc that\n" | |
222 | " " | |
223 | " is limited to 128k max, it inserts a jump\n" | |
224 | " " | |
225 | " stub. You can de-active this with this switch.\n")); | |
226 | fprintf (file, _(" --debug-stubs " | |
227 | "Used for debugging avr-ld.\n")); | |
228 | fprintf (file, _(" --debug-relax " | |
229 | "Used for debugging avr-ld.\n")); | |
28c9d252 NC |
230 | ' |
231 | ||
232 | PARSE_AND_LIST_ARGS_CASES=' | |
233 | ||
234 | case OPTION_PMEM_WRAP_AROUND: | |
92b93329 | 235 | { |
28c9d252 NC |
236 | /* This variable is defined in the bfd library. */ |
237 | if ((!strcmp (optarg,"32k")) || (!strcmp (optarg,"32K"))) | |
238 | avr_pc_wrap_around = 32768; | |
2a69000b DC |
239 | else if ((!strcmp (optarg,"8k")) || (!strcmp (optarg,"8K"))) |
240 | avr_pc_wrap_around = 8192; | |
28c9d252 NC |
241 | else if ((!strcmp (optarg,"16k")) || (!strcmp (optarg,"16K"))) |
242 | avr_pc_wrap_around = 16384; | |
243 | else if ((!strcmp (optarg,"64k")) || (!strcmp (optarg,"64K"))) | |
244 | avr_pc_wrap_around = 0x10000; | |
245 | else | |
246 | return FALSE; | |
247 | } | |
248 | break; | |
249 | ||
250 | case OPTION_DEBUG_STUBS: | |
251 | avr_debug_stubs = TRUE; | |
252 | break; | |
253 | ||
254 | case OPTION_DEBUG_RELAX: | |
255 | avr_debug_relax = TRUE; | |
256 | break; | |
257 | ||
258 | case OPTION_NO_STUBS: | |
259 | avr_no_stubs = TRUE; | |
260 | break; | |
261 | ||
262 | case OPTION_NO_CALL_RET_REPLACEMENT: | |
263 | { | |
264 | /* This variable is defined in the bfd library. */ | |
265 | avr_replace_call_ret_sequences = FALSE; | |
266 | } | |
267 | break; | |
268 | ' | |
269 | ||
270 | # | |
271 | # Put these extra avr-elf routines in ld_${EMULATION_NAME}_emulation | |
272 | # | |
4d4ef6fd | 273 | LDEMUL_BEFORE_PARSE=avr_elf_before_parse |
28c9d252 | 274 | LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation |
eaeb0a9d | 275 | LDEMUL_AFTER_ALLOCATION=avr_elf_after_allocation |
28c9d252 | 276 | LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements |