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