RISC-V: Fix lui relaxation issue with code at address 0.
[external/binutils.git] / ld / emultempl / nds32elf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2012-2019 Free Software Foundation, Inc.
3 # Contributed by Andes Technology Corporation.
4 #
5 # This file is part of the GNU Binutils.
6 #
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.
11 #
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.
16 #
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,
20 # MA 02110-1301, USA.
21 #
22
23 fragment <<EOF
24
25 #include "elf-bfd.h"
26 #include "elf/nds32.h"
27 #include "bfd_stdint.h"
28 #include "elf32-nds32.h"
29
30 static int relax_fp_as_gp = 1;          /* --mrelax-omit-fp  */
31 static int eliminate_gc_relocs = 0;     /* --meliminate-gc-relocs  */
32 static FILE *sym_ld_script = NULL;      /* --mgen-symbol-ld-script=<file>  */
33 static int hyper_relax = 1;             /* --mhyper-relax  */
34 static int tls_desc_trampoline = 0;     /* --m[no]tlsdesc-trampoline.  */
35 /* Disable if linking a dynamically linked executable.  */
36 static int load_store_relax = 1;
37
38 /* Save the target options into output bfd to avoid using to many global
39    variables. Do this after the output has been created, but before
40    inputs are read.  */
41 static void
42 nds32_elf_create_output_section_statements (void)
43 {
44   if (strstr (bfd_get_target (link_info.output_bfd), "nds32") == NULL)
45     {
46       /* Check the output target is nds32.  */
47       einfo (_("%F%P: error: cannot change output format whilst "
48                "linking %s binaries\n"), "NDS32");
49       return;
50     }
51
52   bfd_elf32_nds32_set_target_option (&link_info,
53                                      relax_fp_as_gp,
54                                      eliminate_gc_relocs,
55                                      sym_ld_script,
56                                      hyper_relax,
57                                      tls_desc_trampoline,
58                                      load_store_relax);
59 }
60
61 static void
62 nds32_elf_after_parse (void)
63 {
64   if (bfd_link_relocatable (&link_info)
65       || bfd_link_pic (&link_info))
66     DISABLE_RELAXATION;
67
68   if (!RELAXATION_ENABLED)
69     relax_fp_as_gp = 0;
70
71   gld${EMULATION_NAME}_after_parse ();
72 }
73
74 static void
75 nds32_elf_after_open (void)
76 {
77   unsigned int arch_ver = (unsigned int)-1;
78   unsigned int abi_ver = (unsigned int)-1;
79   bfd *abfd;
80
81   /* For now, make sure all object files are of the same architecture.
82      We may try to merge object files with different architecture together.  */
83   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
84     {
85       if (arch_ver == (unsigned int)-1 && E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
86         arch_ver = elf_elfheader (abfd)->e_flags & EF_NDS_ARCH ;
87
88       if (abi_ver == (unsigned int)-1)
89         {
90           /* Initialize ABI version, if not ABI0.
91              (OS uses empty file to create empty ELF with ABI0).  */
92           if ((elf_elfheader (abfd)->e_flags & EF_NDS_ABI) != 0)
93             abi_ver = elf_elfheader (abfd)->e_flags & EF_NDS_ABI ;
94         }
95       else if ((elf_elfheader (abfd)->e_flags & EF_NDS_ABI) != 0
96                && abi_ver != (elf_elfheader (abfd)->e_flags & EF_NDS_ABI))
97         {
98           /* Incompatible objects.  */
99           einfo (_("%F%P: %pB: ABI version of object files mismatched\n"),
100                  abfd);
101         }
102     }
103
104   /* Check object files if the target is dynamic linked executable
105      or shared object.  */
106   if (elf_hash_table (&link_info)->dynamic_sections_created
107       || bfd_link_pic (&link_info)
108       || bfd_link_pie (&link_info))
109     {
110       /* Dynamic linked executable with SDA and non-PIC.
111          Turn off load/store relaxtion.  */
112       /* This may support in the future.  */
113       load_store_relax = 0 ;
114       relax_fp_as_gp = 0;
115     }
116
117   /* Call the standard elf routine.  */
118   gld${EMULATION_NAME}_after_open ();
119 }
120
121 static void
122 nds32_elf_after_allocation (void)
123 {
124   /* Call default after allocation callback.
125      1. This is where relaxation is done.
126      2. It calls gld${EMULATION_NAME}_map_segments to build ELF segment table.
127      3. Any relaxation requires relax being done must be called after it.  */
128   gld${EMULATION_NAME}_after_allocation ();
129 }
130
131 EOF
132 # Define some shell vars to insert bits of code into the standard elf
133 # parse_args and list_options functions.
134 #
135 PARSE_AND_LIST_PROLOGUE='
136 #define OPTION_BASELINE                 301
137 #define OPTION_ELIM_GC_RELOCS           (OPTION_BASELINE + 1)
138 #define OPTION_FP_AS_GP                 (OPTION_BASELINE + 2)
139 #define OPTION_NO_FP_AS_GP              (OPTION_BASELINE + 3)
140 #define OPTION_REDUCE_FP_UPDATE         (OPTION_BASELINE + 4)
141 #define OPTION_NO_REDUCE_FP_UPDATE      (OPTION_BASELINE + 5)
142 #define OPTION_EXPORT_SYMBOLS           (OPTION_BASELINE + 6)
143 #define OPTION_HYPER_RELAX              (OPTION_BASELINE + 7)
144 #define OPTION_TLSDESC_TRAMPOLINE       (OPTION_BASELINE + 8)
145 #define OPTION_NO_TLSDESC_TRAMPOLINE    (OPTION_BASELINE + 9)
146 '
147 PARSE_AND_LIST_LONGOPTS='
148   { "mfp-as-gp", no_argument, NULL, OPTION_FP_AS_GP},
149   { "mno-fp-as-gp", no_argument, NULL, OPTION_NO_FP_AS_GP},
150   { "mexport-symbols", required_argument, NULL, OPTION_EXPORT_SYMBOLS},
151   { "mhyper-relax", required_argument, NULL, OPTION_HYPER_RELAX},
152   { "mtlsdesc-trampoline", no_argument, NULL, OPTION_TLSDESC_TRAMPOLINE},
153   { "mno-tlsdesc-trampoline", no_argument, NULL, OPTION_NO_TLSDESC_TRAMPOLINE},
154   /* These are deprecated options.  Remove them in the future.  */
155   { "mrelax-reduce-fp-update", no_argument, NULL, OPTION_REDUCE_FP_UPDATE},
156   { "mrelax-no-reduce-fp-update", no_argument, NULL, OPTION_NO_REDUCE_FP_UPDATE},
157   { "mbaseline", required_argument, NULL, OPTION_BASELINE},
158   { "meliminate-gc-relocs", no_argument, NULL, OPTION_ELIM_GC_RELOCS},
159   { "mrelax-omit-fp", no_argument, NULL, OPTION_FP_AS_GP},
160   { "mrelax-no-omit-fp", no_argument, NULL, OPTION_NO_FP_AS_GP},
161   { "mgen-symbol-ld-script", required_argument, NULL, OPTION_EXPORT_SYMBOLS},
162 '
163 PARSE_AND_LIST_OPTIONS='
164   fprintf (file, _("\
165   --m[no-]fp-as-gp            Disable/enable fp-as-gp relaxation\n"));
166   fprintf (file, _("\
167   --mexport-symbols=FILE      Exporting symbols in linker script\n"));
168   fprintf (file, _("\
169   --mhyper-relax=level        Adjust relax level (low|medium|high). default: medium\n"));
170   fprintf (file, _("\
171   --m[no-]tlsdesc-trampoline  Disable/enable TLS DESC trampoline\n"));
172 '
173 PARSE_AND_LIST_ARGS_CASES='
174   case OPTION_BASELINE:
175     einfo (_("%P: --mbaseline is not used anymore\n"));
176     break;
177   case OPTION_ELIM_GC_RELOCS:
178     eliminate_gc_relocs = 1;
179     break;
180   case OPTION_FP_AS_GP:
181   case OPTION_NO_FP_AS_GP:
182     relax_fp_as_gp = (optc == OPTION_FP_AS_GP);
183     break;
184   case OPTION_REDUCE_FP_UPDATE:
185   case OPTION_NO_REDUCE_FP_UPDATE:
186     einfo (_("%P: --relax-[no-]reduce-fp-updat is not used anymore\n"));
187     break;
188   case OPTION_EXPORT_SYMBOLS:
189     if (!optarg)
190       einfo (_("%P: missing file for --mexport-symbols\n"), optarg);
191
192     if(strcmp (optarg, "-") == 0)
193       sym_ld_script = stdout;
194     else
195       {
196         sym_ld_script = fopen (optarg, FOPEN_WT);
197         if(sym_ld_script == NULL)
198           einfo (_("%F%P: cannot open map file %s: %E\n"), optarg);
199       }
200     break;
201   case OPTION_HYPER_RELAX:
202     if (!optarg)
203       einfo (_("%P: valid arguments to --mhyper-relax=(low|medium|high)\n"));
204
205     if (strcmp (optarg, "low") == 0)
206       hyper_relax = 0;
207     else if (strcmp (optarg, "medium") == 0)
208       hyper_relax = 1;
209     else if (strcmp (optarg, "high") == 0)
210       hyper_relax = 2;
211     else
212       einfo (_("%P: valid arguments to --mhyper-relax=(low|medium|high)\n"));
213
214     break;
215   case OPTION_TLSDESC_TRAMPOLINE:
216     tls_desc_trampoline = 1;
217     break;
218   case OPTION_NO_TLSDESC_TRAMPOLINE:
219     tls_desc_trampoline = 0;
220      break;
221 '
222 LDEMUL_AFTER_OPEN=nds32_elf_after_open
223 LDEMUL_AFTER_PARSE=nds32_elf_after_parse
224 LDEMUL_AFTER_ALLOCATION=nds32_elf_after_allocation
225 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=nds32_elf_create_output_section_statements