Add support for Andes NDS32:
[platform/upstream/binutils.git] / ld / emultempl / nds32elf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2012-2013 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 "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/nds32.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 /* Disable if linking a dynamically linked executable.  */
34 static int load_store_relax = 1;
35 static int target_optimize = 0; /* Switch optimization.  */
36 static int relax_status = 0;    /* Finished optimization.  */
37 static int relax_round = 0;             /* Going optimization.  */
38 static FILE *ex9_export_file = NULL;    /* --mexport-ex9=<file>  */
39 static FILE *ex9_import_file = NULL;    /* --mimport-ex9=<file>  */
40 static int update_ex9_table = 0;        /* --mupdate-ex9.  */
41 static int ex9_limit = 511;
42 static bfd_boolean ex9_loop_aware = FALSE;      /* Ignore ex9 if inside a loop.  */
43 static bfd_boolean ifc_loop_aware = FALSE;      /* Ignore ifc if inside a loop.  */
44
45 /* Save the target options into output bfd to avoid using to many global
46    variables. Do this after the output has been created, but before
47    inputs are read.  */
48 static void
49 nds32_elf_create_output_section_statements (void)
50 {
51   if (strstr (bfd_get_target (link_info.output_bfd), "nds32") == NULL)
52     {
53       /* Check the output target is nds32.  */
54       einfo ("%F%X%P: error: Cannot change output format whilst linking NDS32 binaries.\n");
55       return;
56     }
57
58   bfd_elf32_nds32_set_target_option (&link_info, relax_fp_as_gp,
59                                      eliminate_gc_relocs,
60                                      sym_ld_script,
61                                      load_store_relax,
62                                      target_optimize, relax_status, relax_round,
63                                      ex9_export_file, ex9_import_file,
64                                      update_ex9_table, ex9_limit,
65                                      ex9_loop_aware, ifc_loop_aware);
66 }
67
68 static void
69 nds32_elf_after_parse (void)
70 {
71   if (link_info.relocatable)
72     DISABLE_RELAXATION;
73
74   if (!RELAXATION_ENABLED)
75     {
76       target_optimize = target_optimize & (!NDS32_RELAX_JUMP_IFC_ON);
77       target_optimize = target_optimize & (!NDS32_RELAX_EX9_ON);
78       relax_fp_as_gp = 0;
79     }
80
81   if (ex9_import_file != NULL)
82     {
83       ex9_export_file = NULL;
84       target_optimize = target_optimize & (!NDS32_RELAX_EX9_ON);
85     }
86   else
87     update_ex9_table = 0;
88
89   if (link_info.shared)
90     {
91       target_optimize = target_optimize & (!NDS32_RELAX_JUMP_IFC_ON);
92       target_optimize = target_optimize & (!NDS32_RELAX_EX9_ON);
93     }
94
95   after_parse_default ();
96 }
97
98 static void
99 nds32_elf_after_open (void)
100 {
101   unsigned int arch_ver = (unsigned int)-1;
102   unsigned int abi_ver = (unsigned int)-1;
103   bfd *abfd;
104
105   /* For now, make sure all object files are of the same architecture.
106      We may try to merge object files with different architecture together.  */
107   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
108     {
109       if (arch_ver == (unsigned int)-1 && E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
110         arch_ver = elf_elfheader (abfd)->e_flags & EF_NDS_ARCH ;
111
112       if (abi_ver == (unsigned int)-1)
113         {
114           /* Initialize ABI version, if not ABI0.
115              (OS uses empty file to create empty ELF with ABI0).  */
116           if ((elf_elfheader (abfd)->e_flags & EF_NDS_ABI) != 0)
117             abi_ver = elf_elfheader (abfd)->e_flags & EF_NDS_ABI ;
118         }
119       else if ((elf_elfheader (abfd)->e_flags & EF_NDS_ABI) != 0
120                && abi_ver != (elf_elfheader (abfd)->e_flags & EF_NDS_ABI))
121         {
122           /* Incompatible objects.  */
123           einfo (_("%F%B: ABI version of object files mismatched\n"), abfd);
124         }
125
126       /* Append .ex9.itable section in the last input object file.  */
127       if (!link_info.relocatable && abfd->link_next == NULL)
128         {
129           asection *itable;
130           struct bfd_link_hash_entry *h;
131           itable = bfd_make_section_with_flags (abfd, ".ex9.itable",
132                                                 SEC_CODE | SEC_ALLOC | SEC_LOAD
133                                                 | SEC_HAS_CONTENTS | SEC_READONLY
134                                                 | SEC_IN_MEMORY | SEC_KEEP);
135           if (itable)
136             {
137               itable->gc_mark = 1;
138               itable->alignment_power = 2;
139               if ((target_optimize & NDS32_RELAX_EX9_ON))
140                 {
141                   itable->size = 0x1000;
142                   itable->contents = bfd_zalloc (abfd, itable->size);
143                 }
144               else
145                 {
146                   itable->size = 0x4;
147                   itable->contents = bfd_zalloc (abfd, itable->size);
148                   bfd_putb32 (INSN_BREAK_EA,itable->contents);
149                 }
150
151               /* Add a symbol in the head of ex9.itable to objdump clearly.  */
152               h = bfd_link_hash_lookup (link_info.hash, "_EX9_BASE_",
153                                         FALSE, FALSE, FALSE);
154               _bfd_generic_link_add_one_symbol
155                 (&link_info, link_info.output_bfd, "_EX9_BASE_",
156                  BSF_GLOBAL | BSF_WEAK, itable, 0, (const char *) NULL, FALSE,
157                  get_elf_backend_data (link_info.output_bfd)->collect, &h);
158             }
159         }
160     }
161
162   /* Check object files if the target is dynamic linked executable
163      or shared object.  */
164   if (elf_hash_table (&link_info)->dynamic_sections_created
165       || link_info.shared || link_info.pie)
166     {
167       for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
168         {
169           if (!(elf_elfheader (abfd)->e_flags & E_NDS32_HAS_PIC))
170             {
171               /* Non-PIC object file is used.  */
172               if (link_info.shared || link_info.pie)
173                 {
174                   /* For PIE or shared object, all input must be PIC.  */
175                   einfo (_("%B: must use -fpic to compile this file for shared object or PIE\n"), abfd);
176                 }
177               else
178                 {
179                   /* Dynamic linked executable with SDA and non-PIC.
180                      Turn off load/store relaxtion.  */
181                   load_store_relax = 0 ;
182                   relax_fp_as_gp = 0;
183                 }
184             }
185         }
186       /* Turn off relax when building shared object or PIE
187          until we can support their relaxation.  */
188     }
189
190   /* Call the standard elf routine.  */
191   gld${EMULATION_NAME}_after_open ();
192 }
193
194 static void nds32_elf_relax_stub (bfd_boolean relax)
195 {
196   /* Re-caculate memory map address.  */
197   lang_do_assignments (lang_assigning_phase_enum);
198   lang_reset_memory_regions ();
199   one_lang_size_sections_pass (&relax, FALSE);
200 }
201
202 static void
203 nds32_elf_after_allocation (void)
204 {
205   struct elf_nds32_link_hash_table *table;
206   table = nds32_elf_hash_table (&link_info);
207
208   /* Call default after allocation callback.
209      1. This is where relaxation is done.
210      2. It calls gld${EMULATION_NAME}_map_segments to build ELF segment table.
211      3. Any relaxation requires relax being done must be called after it.  */
212   gld${EMULATION_NAME}_after_allocation ();
213
214   if (!table)
215     return;
216
217   /* Use IFC */
218   if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON)
219       && !(table->relax_status & NDS32_RELAX_JUMP_IFC_DONE))
220     {
221       table->relax_round = NDS32_RELAX_JUMP_IFC_ROUND;
222       /* Traverse all sections to build j and jal list.  */
223       nds32_elf_relax_stub (TRUE);
224
225       /* Replace with ifc.  */
226       if (!nds32_elf_ifc_finish (&link_info))
227         einfo (_("%F: Please report this bug. IFC error.\n"));
228       table->relax_round = NDS32_RELAX_NONE_ROUND;
229
230       /* Adjust address after ifcall.  */
231       nds32_elf_relax_stub (FALSE);
232
233       if (!nds32_elf_ifc_reloc ())
234         einfo (_("%F: Please report this bug. IFC error.\n"));
235     }
236
237   /* EX9 Instruction Table Relaxation.  */
238   if (!link_info.relocatable && !nds32_elf_ex9_itb_base (&link_info))
239     einfo (_("%F: Please report this bug. Ex9 relocation error.\n"));
240
241
242   /* Generate ex9 table.  */
243   if ((target_optimize & NDS32_RELAX_EX9_ON)
244       && !(table->relax_status & NDS32_RELAX_EX9_DONE))
245     {
246       /* Ex9 entry point.  */
247       table->relax_round = NDS32_RELAX_EX9_BUILD_ROUND;
248
249       /* Initialize ex9 hash table.  */
250       if (!nds32_elf_ex9_init ())
251         return;
252
253       /* Build ex9 instruction table.  */
254       nds32_elf_relax_stub (TRUE);
255       nds32_elf_ex9_finish (&link_info);
256       /* Replace with ex9.it.  */
257       nds32_elf_relax_stub (TRUE);
258       table->relax_round = NDS32_RELAX_NONE_ROUND;
259
260       /* Do ifc again.  */
261       if (target_optimize & NDS32_RELAX_JUMP_IFC_ON)
262         if (!nds32_elf_ifc_finish (&link_info))
263           einfo (_("%F: Please report this bug. IFC error.\n"));
264
265       /* Re-caculate memory map address.  */
266       lang_do_assignments (lang_assigning_phase_enum);
267       /* Relocation for .ex9.itable.  */
268       nds32_elf_ex9_reloc_jmp (&link_info);
269     }
270   else if (ex9_import_file != NULL
271       && !(table->relax_status = NDS32_RELAX_EX9_DONE))
272     {
273       /* Import ex9 table.  */
274
275       if (update_ex9_table == 1)
276         {
277           /* Build ex9 table.  */
278           table->relax_round = NDS32_RELAX_EX9_BUILD_ROUND;
279           /* Initialize ex9 hash table.  */
280           if (!nds32_elf_ex9_init ())
281             return;
282           /* Build ex9 table.  */
283           nds32_elf_relax_stub (TRUE);
284
285           /* Relocation for .ex9.itable.  */
286           lang_do_assignments (lang_assigning_phase_enum);
287           nds32_elf_ex9_reloc_jmp (&link_info);
288         }
289       nds32_elf_ex9_import_table (&link_info);
290
291       /* Replace with ex9.it.  */
292       table->relax_round = NDS32_RELAX_EX9_REPLACE_ROUND;
293       table->relax_status |= NDS32_RELAX_EX9_DONE;
294       nds32_elf_relax_stub (TRUE);
295     }
296 }
297
298 EOF
299 # Define some shell vars to insert bits of code into the standard elf
300 # parse_args and list_options functions.
301 #
302 PARSE_AND_LIST_PROLOGUE='
303 #define OPTION_BASELINE                 301
304 #define OPTION_ELIM_GC_RELOCS           (OPTION_BASELINE + 1)
305 #define OPTION_FP_AS_GP                 (OPTION_BASELINE + 2)
306 #define OPTION_NO_FP_AS_GP              (OPTION_BASELINE + 3)
307 #define OPTION_REDUCE_FP_UPDATE         (OPTION_BASELINE + 4)
308 #define OPTION_NO_REDUCE_FP_UPDATE      (OPTION_BASELINE + 5)
309 #define OPTION_EXPORT_SYMBOLS           (OPTION_BASELINE + 6)
310
311 /* These are only available to ex9.  */
312 #if defined NDS32_EX9_EXT
313 #define OPTION_EX9_BASELINE             320
314 #define OPTION_EX9_TABLE                (OPTION_EX9_BASELINE + 1)
315 #define OPTION_NO_EX9_TABLE             (OPTION_EX9_BASELINE + 2)
316 #define OPTION_EXPORT_EX9               (OPTION_EX9_BASELINE + 3)
317 #define OPTION_IMPORT_EX9               (OPTION_EX9_BASELINE + 4)
318 #define OPTION_UPDATE_EX9               (OPTION_EX9_BASELINE + 5)
319 #define OPTION_EX9_LIMIT                (OPTION_EX9_BASELINE + 6)
320 #define OPTION_EX9_LOOP                 (OPTION_EX9_BASELINE + 7)
321 #endif
322
323 /* These are only available to link-time ifc.  */
324 #if defined NDS32_IFC_EXT
325 #define OPTION_IFC_BASELINE             340
326 #define OPTION_JUMP_IFC                 (OPTION_IFC_BASELINE + 1)
327 #define OPTION_NO_JUMP_IFC              (OPTION_IFC_BASELINE + 2)
328 #define OPTION_IFC_LOOP                 (OPTION_IFC_BASELINE + 3)
329 #endif
330 '
331 PARSE_AND_LIST_LONGOPTS='
332   { "mfp-as-gp", no_argument, NULL, OPTION_FP_AS_GP},
333   { "mno-fp-as-gp", no_argument, NULL, OPTION_NO_FP_AS_GP},
334   { "mgen-symbol-ld-script", required_argument, NULL, OPTION_EXPORT_SYMBOLS},
335   /* These are deprecated options.  Remove them in the future.  */
336   { "mrelax-reduce-fp-update", no_argument, NULL, OPTION_REDUCE_FP_UPDATE},
337   { "mrelax-no-reduce-fp-update", no_argument, NULL, OPTION_NO_REDUCE_FP_UPDATE},
338   { "mbaseline", required_argument, NULL, OPTION_BASELINE},
339   { "meliminate-gc-relocs", no_argument, NULL, OPTION_ELIM_GC_RELOCS},
340   { "mrelax-omit-fp", no_argument, NULL, OPTION_FP_AS_GP},
341   { "mrelax-no-omit-fp", no_argument, NULL, OPTION_NO_FP_AS_GP},
342   { "mgen-symbol-ld-script", required_argument, NULL, OPTION_EXPORT_SYMBOLS},
343   /* These are specific optioins for ex9-ext support.  */
344 #if defined NDS32_EX9_EXT
345   { "mex9", no_argument, NULL, OPTION_EX9_TABLE},
346   { "mno-ex9", no_argument, NULL, OPTION_NO_EX9_TABLE},
347   { "mexport-ex9", required_argument, NULL, OPTION_EXPORT_EX9},
348   { "mimport-ex9", required_argument, NULL, OPTION_IMPORT_EX9},
349   { "mupdate-ex9", no_argument, NULL, OPTION_UPDATE_EX9},
350   { "mex9-limit", required_argument, NULL, OPTION_EX9_LIMIT},
351   { "mex9-loop-aware", no_argument, NULL, OPTION_EX9_LOOP},
352 #endif
353   /* These are specific optioins for ifc-ext support.  */
354 #if defined NDS32_IFC_EXT
355   { "mifc", no_argument, NULL, OPTION_JUMP_IFC},
356   { "mno-ifc", no_argument, NULL, OPTION_NO_JUMP_IFC},
357   { "mifc-loop-aware", no_argument, NULL, OPTION_IFC_LOOP},
358 #endif
359 '
360 PARSE_AND_LIST_OPTIONS='
361   fprintf (file, _("\
362   --m[no-]fp-as-gp            Disable/enable fp-as-gp relaxation\n\
363   --mexport-symbols=FILE      Exporting symbols in linker script\n\
364 "));
365
366 #if defined NDS32_EX9_EXT
367   fprintf (file, _("\
368   --m[no-]ex9                 Disable/enable link-time EX9 relaxation\n\
369   --mexport-ex9=FILE          Export EX9 table after linking\n\
370   --mimport-ex9=FILE          Import Ex9 table for EX9 relaxation\n\
371   --mupdate-ex9               Update existing EX9 table\n\
372   --mex9-limit=NUM            Maximum number of entries in ex9 table\n\
373   --mex9-loop-aware           Avoid generate EX9 instruction inside loop\n\
374 "));
375 #endif
376
377 #if defined NDS32_IFC_EXT
378   fprintf (file, _("\
379   --m[no-]ifc                 Disable/enable link-time IFC optimization\n\
380   --mifc-loop-aware           Avoid generate IFC instruction inside loop\n\
381 "));
382 #endif
383 '
384 PARSE_AND_LIST_ARGS_CASES='
385   case OPTION_BASELINE:
386     einfo ("%P: --mbaseline is not used anymore.\n");
387     break;
388   case OPTION_ELIM_GC_RELOCS:
389     eliminate_gc_relocs = 1;
390     break;
391   case OPTION_FP_AS_GP:
392   case OPTION_NO_FP_AS_GP:
393     relax_fp_as_gp = (optc == OPTION_FP_AS_GP);
394     break;
395   case OPTION_REDUCE_FP_UPDATE:
396   case OPTION_NO_REDUCE_FP_UPDATE:
397     einfo ("%P: --relax-[no-]reduce-fp-updat is not used anymore.\n");
398     break;
399   case OPTION_EXPORT_SYMBOLS:
400     if (!optarg)
401       einfo (_("Missing file for --mgen-symbol-ld-script.\n"), optarg);
402
403     if(strcmp (optarg, "-") == 0)
404       sym_ld_script = stdout;
405     else
406       {
407         sym_ld_script = fopen (optarg, FOPEN_WT);
408         if(sym_ld_script == NULL)
409           einfo (_("%P%F: cannot open map file %s: %E.\n"), optarg);
410       }
411     break;
412 #if defined NDS32_EX9_EXT
413   case OPTION_EX9_TABLE:
414     target_optimize = target_optimize | NDS32_RELAX_EX9_ON;
415     break;
416   case OPTION_NO_EX9_TABLE:
417     target_optimize = target_optimize & (!NDS32_RELAX_EX9_ON);
418     break;
419   case OPTION_EXPORT_EX9:
420     if (!optarg)
421       einfo (_("Missing file for --mexport-ex9=<file>.\n"));
422
423     if(strcmp (optarg, "-") == 0)
424       ex9_export_file = stdout;
425     else
426       {
427         ex9_export_file = fopen (optarg, FOPEN_WT);
428         if(ex9_export_file == NULL)
429           einfo (_("ERROR %P%F: cannot open ex9 export file %s.\n"), optarg);
430       }
431     break;
432   case OPTION_IMPORT_EX9:
433     if (!optarg)
434       einfo (_("Missing file for --mimport-ex9=<file>.\n"));
435
436     ex9_import_file = fopen (optarg, "r+");
437     if(ex9_import_file == NULL)
438       einfo (_("ERROR %P%F: cannot open ex9 import file %s.\n"), optarg);
439     break;
440   case OPTION_UPDATE_EX9:
441     update_ex9_table = 1;
442     break;
443   case OPTION_EX9_LIMIT:
444     if (optarg)
445       {
446         ex9_limit = atoi (optarg);
447         if (ex9_limit > 511 || ex9_limit < 1)
448           {
449             einfo (_("ERROR: the range of ex9_limit must between 1 and 511\n"));
450             exit (1);
451           }
452       }
453     break;
454   case OPTION_EX9_LOOP:
455     target_optimize = target_optimize | NDS32_RELAX_EX9_ON;
456     ex9_loop_aware = 1;
457     break;
458 #endif
459 #if defined NDS32_IFC_EXT
460   case OPTION_JUMP_IFC:
461     target_optimize = target_optimize | NDS32_RELAX_JUMP_IFC_ON;
462     break;
463   case OPTION_NO_JUMP_IFC:
464     target_optimize = target_optimize & (!NDS32_RELAX_JUMP_IFC_ON);
465     break;
466   case OPTION_IFC_LOOP:
467     target_optimize = target_optimize | NDS32_RELAX_JUMP_IFC_ON;
468     ifc_loop_aware = 1;
469     break;
470 #endif
471 '
472 LDEMUL_AFTER_OPEN=nds32_elf_after_open
473 LDEMUL_AFTER_PARSE=nds32_elf_after_parse
474 LDEMUL_AFTER_ALLOCATION=nds32_elf_after_allocation
475 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=nds32_elf_create_output_section_statements