Fix: Segmentation fault caused by npd in objdump
[binutils-gdb.git] / ld / emultempl / avrelf.em
blob1949a4dc9307c1232e475a3f92cc8fab96065ea1
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright (C) 2006-2023 Free Software Foundation, Inc.
4 # This file is part of the 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,
19 # MA 02110-1301, USA.
22 # This file is sourced from elf.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
25 # to reach the whole program memory by using 16 bit pointers.
27 fragment <<EOF
29 #include "elf32-avr.h"
30 #include "ldctor.h"
31 #include "elf/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 transferred
40    to the bfd without use of global shared variables.  */
42 static bool avr_no_stubs = false;
43 static bool avr_debug_relax = false;
44 static bool avr_debug_stubs = false;
45 static bool avr_replace_call_ret_sequences = true;
46 static bfd_vma avr_pc_wrap_around = 0x10000000;
48 /* Transfers information to the bfd frontend.  */
50 static void
51 avr_elf_set_global_bfd_parameters (void)
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);
64 /* Makes a conservative estimate of the trampoline section size that could
65    be corrected later on.  */
67 static void
68 avr_elf_${EMULATION_NAME}_before_allocation (void)
70   int ret;
72   gld${EMULATION_NAME}_before_allocation ();
74   if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
75     {
76       avr_no_stubs = true;
77       return;
78     }
80   /* We only need stubs for avr6, avrxmega6, and avrxmega7.  */
81   if (strcmp ("${EMULATION_NAME}", "avr6") != 0
82       && strcmp ("${EMULATION_NAME}", "avrxmega6") != 0
83       && strcmp ("${EMULATION_NAME}", "avrxmega7") != 0)
84     avr_no_stubs = true;
86   avr_elf_set_global_bfd_parameters ();
88   /* If generating a relocatable output file, then
89      we don't  have to generate the trampolines.  */
90   if (bfd_link_relocatable (&link_info))
91     avr_no_stubs = true;
93   if (avr_no_stubs)
94     return;
96   ret = elf32_avr_setup_section_lists (link_info.output_bfd, &link_info);
98   if (ret < 0)
99     einfo (_("%X%P: can not setup the input section list: %E\n"));
101   if (ret <= 0)
102     return;
104   /* Call into the BFD backend to do the real "stub"-work.  */
105   if (! elf32_avr_size_stubs (link_info.output_bfd, &link_info, true))
106     einfo (_("%X%P: can not size stub section: %E\n"));
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.  */
112 static void
113 avr_elf_create_output_section_statements (void)
115   flagword flags;
117   if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
118     {
119       einfo (_("%F%P: error: cannot change output format "
120                "whilst linking %s binaries\n"), "AVR");
121       return;
122     }
124   stub_file = lang_add_input_file ("linker stubs",
125                                    lang_input_file_is_fake_enum,
126                                    NULL);
128   stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
129   if (stub_file->the_bfd == NULL
130       || !bfd_set_arch_mach (stub_file->the_bfd,
131                              bfd_get_arch (link_info.output_bfd),
132                              bfd_get_mach (link_info.output_bfd)))
133     {
134       einfo (_("%X%P: can not create stub BFD: %E\n"));
135       return;
136     }
137   stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
139   /* Now we add the stub section.  */
141   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
142            | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
143   avr_stub_section = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
144                                                          ".trampolines",
145                                                          flags);
146   if (avr_stub_section == NULL)
147     goto err_ret;
149   avr_stub_section->alignment_power = 1;
151   ldlang_add_file (stub_file);
153   return;
155  err_ret:
156   einfo (_("%X%P: can not make stub section: %E\n"));
157   return;
160 /* Re-calculates the size of the stubs so that we won't waste space.  */
162 static void
163 avr_elf_after_allocation (void)
165   if (!avr_no_stubs && ! RELAXATION_ENABLED)
166     {
167       /* If relaxing, elf32_avr_size_stubs will be called from
168          elf32_avr_relax_section.  */
169       if (!elf32_avr_size_stubs (link_info.output_bfd, &link_info, true))
170         einfo (_("%X%P: can not size stub section: %E\n"));
171     }
173   gld${EMULATION_NAME}_after_allocation ();
175   /* Now build the linker stubs.  */
176   if (!avr_no_stubs)
177     {
178       if (!elf32_avr_build_stubs (&link_info))
179         einfo (_("%X%P: can not build stubs: %E\n"));
180     }
183 static void
184 avr_elf_before_parse (void)
186   /* Don't create a demand-paged executable, since this feature isn't
187      meaningful in AVR. */
188   config.magic_demand_paged = false;
190   gld${EMULATION_NAME}_before_parse ();
193 static void
194 avr_finish (void)
196   bfd *abfd;
197   bool avr_link_relax;
199   if (bfd_link_relocatable (&link_info))
200     {
201       avr_link_relax = true;
202       for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
203         {
204           /* Don't let the linker stubs prevent the final object being
205              marked as link-relax ready.  */
206           if ((elf_elfheader (abfd)->e_flags
207                & EF_AVR_LINKRELAX_PREPARED) == 0
208               && abfd != stub_file->the_bfd)
209             {
210               avr_link_relax = false;
211               break;
212             }
213         }
214     }
215   else
216     {
217       avr_link_relax = RELAXATION_ENABLED;
218     }
220   abfd = link_info.output_bfd;
222   if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
223     {
224       if (avr_link_relax)
225         elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
226       else
227         elf_elfheader (abfd)->e_flags &= ~EF_AVR_LINKRELAX_PREPARED;
228     }
230   finish_default ();
235 PARSE_AND_LIST_PROLOGUE='
237 #define OPTION_NO_CALL_RET_REPLACEMENT 301
238 #define OPTION_PMEM_WRAP_AROUND        302
239 #define OPTION_NO_STUBS                303
240 #define OPTION_DEBUG_STUBS             304
241 #define OPTION_DEBUG_RELAX             305
244 PARSE_AND_LIST_LONGOPTS='
245   { "no-call-ret-replacement", no_argument,
246     NULL, OPTION_NO_CALL_RET_REPLACEMENT},
247   { "pmem-wrap-around", required_argument,
248     NULL, OPTION_PMEM_WRAP_AROUND},
249   { "no-stubs", no_argument,
250     NULL, OPTION_NO_STUBS},
251   { "debug-stubs", no_argument,
252     NULL, OPTION_DEBUG_STUBS},
253   { "debug-relax", no_argument,
254     NULL, OPTION_DEBUG_RELAX},
257 PARSE_AND_LIST_OPTIONS='
258   fprintf (file, _("  --pmem-wrap-around=<val>    "
259                    "Make the linker relaxation machine assume that a\n"
260                    "                              "
261                    "  program counter wrap-around occurs at address\n"
262                    "                              "
263                    "  <val>.  Supported values: 8k, 16k, 32k and 64k.\n"));
264   fprintf (file, _("  --no-call-ret-replacement   "
265                    "The relaxation machine normally will\n"
266                    "                              "
267                    "  substitute two immediately following call/ret\n"
268                    "                              "
269                    "  instructions by a single jump instruction.\n"
270                    "                              "
271                    "  This option disables this optimization.\n"));
272   fprintf (file, _("  --no-stubs                  "
273                    "If the linker detects to attempt to access\n"
274                    "                              "
275                    "  an instruction beyond 128k by a reloc that\n"
276                    "                              "
277                    "  is limited to 128k max, it inserts a jump\n"
278                    "                              "
279                    "  stub. You can de-active this with this switch.\n"));
280   fprintf (file, _("  --debug-stubs               "
281                    "Used for debugging avr-ld.\n"));
282   fprintf (file, _("  --debug-relax               "
283                    "Used for debugging avr-ld.\n"));
286 PARSE_AND_LIST_ARGS_CASES='
288     case OPTION_PMEM_WRAP_AROUND:
289       {
290         /* This variable is defined in the bfd library.  */
291         if ((!strcmp (optarg,"32k"))      || (!strcmp (optarg,"32K")))
292           avr_pc_wrap_around = 32768;
293         else if ((!strcmp (optarg,"8k"))  || (!strcmp (optarg,"8K")))
294           avr_pc_wrap_around = 8192;
295         else if ((!strcmp (optarg,"16k")) || (!strcmp (optarg,"16K")))
296           avr_pc_wrap_around = 16384;
297         else if ((!strcmp (optarg,"64k")) || (!strcmp (optarg,"64K")))
298           avr_pc_wrap_around = 0x10000;
299         else
300           return false;
301       }
302       break;
304     case OPTION_DEBUG_STUBS:
305       avr_debug_stubs = true;
306       break;
308     case OPTION_DEBUG_RELAX:
309       avr_debug_relax = true;
310       break;
312     case OPTION_NO_STUBS:
313       avr_no_stubs = true;
314       break;
316     case OPTION_NO_CALL_RET_REPLACEMENT:
317       {
318         /* This variable is defined in the bfd library.  */
319         avr_replace_call_ret_sequences = false;
320       }
321       break;
325 # Put these extra avr-elf routines in ld_${EMULATION_NAME}_emulation
327 LDEMUL_BEFORE_PARSE=avr_elf_before_parse
328 LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation
329 LDEMUL_AFTER_ALLOCATION=avr_elf_after_allocation
330 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements
331 LDEMUL_FINISH=avr_finish