1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2012-2019 Free Software Foundation, Inc.
3 # Contributed by Andes Technology Corporation.
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,
26 #include "elf/nds32.h"
27 #include "bfd_stdint.h"
28 #include "elf32-nds32.h"
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;
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
42 nds32_elf_create_output_section_statements (void)
44 if (strstr (bfd_get_target (link_info.output_bfd), "nds32") == NULL)
46 /* Check the output target is nds32. */
47 einfo (_("%F%P: error: cannot change output format whilst "
48 "linking %s binaries\n"), "NDS32");
52 bfd_elf32_nds32_set_target_option (&link_info,
62 nds32_elf_after_parse (void)
64 if (bfd_link_relocatable (&link_info)
65 || bfd_link_pic (&link_info))
68 if (!RELAXATION_ENABLED)
71 gld${EMULATION_NAME}_after_parse ();
75 nds32_elf_after_open (void)
77 unsigned int arch_ver = (unsigned int)-1;
78 unsigned int abi_ver = (unsigned int)-1;
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)
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 ;
88 if (abi_ver == (unsigned int)-1)
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 ;
95 else if ((elf_elfheader (abfd)->e_flags & EF_NDS_ABI) != 0
96 && abi_ver != (elf_elfheader (abfd)->e_flags & EF_NDS_ABI))
98 /* Incompatible objects. */
99 einfo (_("%F%P: %pB: ABI version of object files mismatched\n"),
104 /* Check object files if the target is dynamic linked executable
106 if (elf_hash_table (&link_info)->dynamic_sections_created
107 || bfd_link_pic (&link_info)
108 || bfd_link_pie (&link_info))
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 ;
117 /* Call the standard elf routine. */
118 gld${EMULATION_NAME}_after_open ();
122 nds32_elf_after_allocation (void)
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 ();
132 # Define some shell vars to insert bits of code into the standard elf
133 # parse_args and list_options functions.
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)
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},
163 PARSE_AND_LIST_OPTIONS='
165 --m[no-]fp-as-gp Disable/enable fp-as-gp relaxation\n"));
167 --mexport-symbols=FILE Exporting symbols in linker script\n"));
169 --mhyper-relax=level Adjust relax level (low|medium|high). default: medium\n"));
171 --m[no-]tlsdesc-trampoline Disable/enable TLS DESC trampoline\n"));
173 PARSE_AND_LIST_ARGS_CASES='
174 case OPTION_BASELINE:
175 einfo (_("%P: --mbaseline is not used anymore\n"));
177 case OPTION_ELIM_GC_RELOCS:
178 eliminate_gc_relocs = 1;
180 case OPTION_FP_AS_GP:
181 case OPTION_NO_FP_AS_GP:
182 relax_fp_as_gp = (optc == OPTION_FP_AS_GP);
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"));
188 case OPTION_EXPORT_SYMBOLS:
190 einfo (_("%P: missing file for --mexport-symbols\n"), optarg);
192 if(strcmp (optarg, "-") == 0)
193 sym_ld_script = stdout;
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);
201 case OPTION_HYPER_RELAX:
203 einfo (_("%P: valid arguments to --mhyper-relax=(low|medium|high)\n"));
205 if (strcmp (optarg, "low") == 0)
207 else if (strcmp (optarg, "medium") == 0)
209 else if (strcmp (optarg, "high") == 0)
212 einfo (_("%P: valid arguments to --mhyper-relax=(low|medium|high)\n"));
215 case OPTION_TLSDESC_TRAMPOLINE:
216 tls_desc_trampoline = 1;
218 case OPTION_NO_TLSDESC_TRAMPOLINE:
219 tls_desc_trampoline = 0;
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