PR ld/6766
[external/binutils.git] / ld / emultempl / cr16elf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
3 # Contributed by M R Swami Reddy <MR.Swami.Reddy@nsc.com>
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 # This file is sourced from elf32.em, and defines extra cr16-elf
24 # specific routines.
25 #
26 fragment <<EOF
27
28 #include "ldctor.h"
29
30 /* Flag for the emulation-specific "--no-relax" option.  */
31 static bfd_boolean disable_relaxation = FALSE;
32
33 static void check_sections (bfd *, asection *, void *);
34
35
36 /* This function is run after all the input files have been opened.  */
37
38 static void
39 cr16_elf_after_open (void)
40 {
41   /* Call the standard elf routine.  */
42   gld${EMULATION_NAME}_after_open ();
43
44    if (command_line.embedded_relocs
45        && (! link_info.relocatable))
46      {
47        bfd *abfd;
48
49       /* In the embedded relocs mode we create a .emreloc section for each
50          input file with a nonzero .data section.  The BFD backend will fill in
51          these sections with magic numbers which can be used to relocate the
52          data section at run time.  */
53       for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
54         {
55           asection *datasec;
56
57           /* As first-order business, make sure that each input BFD is either
58              COFF or ELF.  We need to call a special BFD backend function to
59              generate the embedded relocs, and we have such functions only for
60              COFF and ELF.  */
61           if (bfd_get_flavour (abfd) != bfd_target_coff_flavour
62               && bfd_get_flavour (abfd) != bfd_target_elf_flavour)
63             einfo ("%F%B: all input objects must be COFF or ELF for --embedded-relocs\n");
64
65           datasec = bfd_get_section_by_name (abfd, ".data.rel");
66
67           /* Note that we assume that the reloc_count field has already
68              been set up.  We could call bfd_get_reloc_upper_bound, but
69              that returns the size of a memory buffer rather than a reloc
70              count.  We do not want to call bfd_canonicalize_reloc,
71              because although it would always work it would force us to
72              read in the relocs into BFD canonical form, which would waste
73              a significant amount of time and memory.  */
74           if (datasec != NULL && datasec->reloc_count > 0)
75             {
76               asection *relsec;
77
78               relsec = bfd_make_section (abfd, ".emreloc");
79               if (relsec == NULL
80                   || ! bfd_set_section_flags (abfd, relsec,
81                                               (SEC_ALLOC
82                                                | SEC_LOAD
83                                                | SEC_HAS_CONTENTS
84                                                | SEC_IN_MEMORY))
85                   || ! bfd_set_section_alignment (abfd, relsec, 2)
86                   || ! bfd_set_section_size (abfd, relsec,
87                                              datasec->reloc_count * 8))
88                 einfo ("%F%B: can not create .emreloc section: %E\n");
89             }
90
91           /* Double check that all other data sections are empty, as is
92              required for embedded PIC code.  */
93           bfd_map_over_sections (abfd, check_sections, datasec); 
94         }
95     }
96 }
97
98 /* Check that of the data sections, only the .data section has
99    relocs.  This is called via bfd_map_over_sections.  */
100
101 static void
102 check_sections (bfd *abfd, asection *sec, void *datasec)
103 {
104   if ((strncmp (bfd_get_section_name (abfd, sec), ".data.rel", 9) == 0)
105      && sec != datasec
106      && sec->reloc_count == 0 )
107     einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
108            abfd, bfd_get_section_name (abfd, sec));
109 }
110
111 static void
112 cr16elf_after_parse (void)
113 {
114   /* Always behave as if called with --sort-common command line
115      option.
116      This is to emulate the CRTools' method of keeping variables
117      of different alignment in separate sections.  */
118   config.sort_common = TRUE;
119
120   /* Don't create a demand-paged executable, since this feature isn't
121      meaninful in CR16 embedded systems. Moreover, when magic_demand_paged
122      is true the link sometimes fails.  */
123   config.magic_demand_paged = FALSE;
124
125   after_parse_default ();
126 }
127
128 /* This is called after the sections have been attached to output
129    sections, but before any sizes or addresses have been set.  */
130
131 static void
132 cr16elf_before_allocation (void)
133 {
134   /* Call the default first.  */
135   gld${EMULATION_NAME}_before_allocation ();
136
137    if (command_line.embedded_relocs
138        && (! link_info.relocatable))
139      {
140
141    bfd *abfd;
142
143    /* If we are generating embedded relocs, call a special BFD backend
144          routine to do the work.  */
145    for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
146       {
147           asection *datasec, *relsec;
148           char *errmsg;
149
150           datasec = bfd_get_section_by_name (abfd, ".data.rel");
151
152           if (datasec == NULL || datasec->reloc_count == 0)
153             continue;
154
155           relsec = bfd_get_section_by_name (abfd, ".emreloc");
156           ASSERT (relsec != NULL);
157
158           if (! bfd_cr16_elf32_create_embedded_relocs (abfd, &link_info,
159                                                            datasec, relsec,
160                                                            &errmsg))
161                 {
162                   if (errmsg == NULL)
163                     einfo ("%B%X: can not create runtime reloc information: %E\n",
164                            abfd);
165                   else
166                     einfo ("%X%B: can not create runtime reloc information: %s\n",
167                            abfd, errmsg);
168                 }
169        }
170      }
171
172   /* Enable relaxation by default if the "--no-relax" option was not
173      specified.  This is done here instead of in the before_parse hook
174      because there is a check in main() to prohibit use of --relax and
175      -r together.  */
176
177   if (!disable_relaxation)
178     command_line.relax = TRUE;
179 }
180
181 EOF
182
183 # Define some shell vars to insert bits of code into the standard elf
184 # parse_args and list_options functions.
185 #
186 PARSE_AND_LIST_PROLOGUE='
187 #define OPTION_NO_RELAX                 301
188 '
189
190 PARSE_AND_LIST_LONGOPTS='
191   { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
192 '
193
194 PARSE_AND_LIST_OPTIONS='
195   fprintf (file, _("  --no-relax                  Do not relax branches\n"));
196 '
197
198 PARSE_AND_LIST_ARGS_CASES='
199     case OPTION_NO_RELAX:
200       disable_relaxation = TRUE;
201       break;
202 '
203
204 # Put these extra cr16-elf routines in ld_${EMULATION_NAME}_emulation
205 #
206 LDEMUL_AFTER_OPEN=cr16_elf_after_open
207 LDEMUL_AFTER_PARSE=cr16elf_after_parse
208 LDEMUL_BEFORE_ALLOCATION=cr16elf_before_allocation