Convert to C90
[binutils.git] / ld / emultempl / m68kcoff.em
blobeefa32746b7515b00c34960b6fc618ef13c55228
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is is generated by a shell script.  DO NOT EDIT! */
6 /* Handle embedded relocs for m68k.
7    Copyright 2000, 2002, 2003 Free Software Foundation, Inc.
8    Written by Michael Sokolov <msokolov@ivan.Harhan.ORG>, based on generic.em
9    by Steve Chamberlain <steve@cygnus.com>, embedded relocs code based on
10    mipsecoff.em by Ian Lance Taylor <ian@cygnus.com>.
12 This file is part of GLD, the Gnu Linker.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
28 #define TARGET_IS_${EMULATION_NAME}
30 #include "bfd.h"
31 #include "sysdep.h"
32 #include "bfdlink.h"
34 #include "ld.h"
35 #include "ldmain.h"
36 #include "ldfile.h"
37 #include "ldemul.h"
38 #include "ldmisc.h"
40 static void check_sections (bfd *, asection *, void *);
42 static void
43 gld${EMULATION_NAME}_before_parse (void)
45 #ifndef TARGET_                 /* I.e., if not generic.  */
46   ldfile_set_output_arch ("`echo ${ARCH}`");
47 #endif /* not TARGET_ */
50 /* This function is run after all the input files have been opened.
51    We create a .emreloc section for each input file with a non zero
52    .data section.  The BFD backend will fill in these sections with
53    magic numbers which can be used to relocate the data section at run
54    time.  */
56 static void
57 gld${EMULATION_NAME}_after_open (void)
59   bfd *abfd;
61   if (! command_line.embedded_relocs
62       || link_info.relocatable)
63     return;
65   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
66     {
67       asection *datasec;
69       /* As first-order business, make sure that each input BFD is COFF. It
70          better be, as we are directly calling a COFF backend function.  */
71       if (bfd_get_flavour (abfd) != bfd_target_coff_flavour)
72         einfo ("%F%B: all input objects must be COFF for --embedded-relocs\n");
74       datasec = bfd_get_section_by_name (abfd, ".data");
76       /* Note that we assume that the reloc_count field has already
77          been set up.  We could call bfd_get_reloc_upper_bound, but
78          that returns the size of a memory buffer rather than a reloc
79          count.  We do not want to call bfd_canonicalize_reloc,
80          because although it would always work it would force us to
81          read in the relocs into BFD canonical form, which would waste
82          a significant amount of time and memory.  */
83       if (datasec != NULL && datasec->reloc_count > 0)
84         {
85           asection *relsec;
87           relsec = bfd_make_section (abfd, ".emreloc");
88           if (relsec == NULL
89               || ! bfd_set_section_flags (abfd, relsec,
90                                           (SEC_ALLOC
91                                            | SEC_LOAD
92                                            | SEC_HAS_CONTENTS
93                                            | SEC_IN_MEMORY))
94               || ! bfd_set_section_alignment (abfd, relsec, 2)
95               || ! bfd_set_section_size (abfd, relsec,
96                                          datasec->reloc_count * 12))
97             einfo ("%F%B: can not create .emreloc section: %E\n");
98         }
100       /* Double check that all other data sections are empty, as is
101          required for embedded PIC code.  */
102       bfd_map_over_sections (abfd, check_sections, datasec);
103     }
106 /* Check that of the data sections, only the .data section has
107    relocs.  This is called via bfd_map_over_sections.  */
109 static void
110 check_sections (bfd *abfd, asection *sec, void *datasec)
112   if ((bfd_get_section_flags (abfd, sec) & SEC_DATA)
113       && sec != datasec
114       && sec->reloc_count != 0)
115     einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
116            abfd, bfd_get_section_name (abfd, sec));
119 /* This function is called after the section sizes and offsets have
120    been set.  If we are generating embedded relocs, it calls a special
121    BFD backend routine to do the work.  */
123 static void
124 gld${EMULATION_NAME}_after_allocation (void)
126   bfd *abfd;
128   if (! command_line.embedded_relocs
129       || link_info.relocatable)
130     return;
132   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
133     {
134       asection *datasec, *relsec;
135       char *errmsg;
137       datasec = bfd_get_section_by_name (abfd, ".data");
139       if (datasec == NULL || datasec->reloc_count == 0)
140         continue;
142       relsec = bfd_get_section_by_name (abfd, ".emreloc");
143       ASSERT (relsec != NULL);
145       if (! bfd_m68k_coff_create_embedded_relocs (abfd, &link_info,
146                                                    datasec, relsec,
147                                                    &errmsg))
148         {
149           if (errmsg == NULL)
150             einfo ("%B%X: can not create runtime reloc information: %E\n",
151                    abfd);
152           else
153             einfo ("%X%B: can not create runtime reloc information: %s\n",
154                    abfd, errmsg);
155         }
156     }
159 static char *
160 gld${EMULATION_NAME}_get_script (int *isfile)
163 if test -n "$COMPILE_IN"
164 then
165 # Scripts compiled in.
167 # sed commands to quote an ld script as a C string.
168 sc="-f stringify.sed"
170 cat >>e${EMULATION_NAME}.c <<EOF
172   *isfile = 0;
174   if (link_info.relocatable && config.build_constructors)
175     return
177 sed $sc ldscripts/${EMULATION_NAME}.xu                 >> e${EMULATION_NAME}.c
178 echo '  ; else if (link_info.relocatable) return'     >> e${EMULATION_NAME}.c
179 sed $sc ldscripts/${EMULATION_NAME}.xr                 >> e${EMULATION_NAME}.c
180 echo '  ; else if (!config.text_read_only) return'     >> e${EMULATION_NAME}.c
181 sed $sc ldscripts/${EMULATION_NAME}.xbn                >> e${EMULATION_NAME}.c
182 echo '  ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
183 sed $sc ldscripts/${EMULATION_NAME}.xn                 >> e${EMULATION_NAME}.c
184 echo '  ; else return'                                 >> e${EMULATION_NAME}.c
185 sed $sc ldscripts/${EMULATION_NAME}.x                  >> e${EMULATION_NAME}.c
186 echo '; }'                                             >> e${EMULATION_NAME}.c
188 else
189 # Scripts read from the filesystem.
191 cat >>e${EMULATION_NAME}.c <<EOF
193   *isfile = 1;
195   if (link_info.relocatable && config.build_constructors)
196     return "ldscripts/${EMULATION_NAME}.xu";
197   else if (link_info.relocatable)
198     return "ldscripts/${EMULATION_NAME}.xr";
199   else if (!config.text_read_only)
200     return "ldscripts/${EMULATION_NAME}.xbn";
201   else if (!config.magic_demand_paged)
202     return "ldscripts/${EMULATION_NAME}.xn";
203   else
204     return "ldscripts/${EMULATION_NAME}.x";
210 cat >>e${EMULATION_NAME}.c <<EOF
212 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
214   gld${EMULATION_NAME}_before_parse,
215   syslib_default,
216   hll_default,
217   after_parse_default,
218   gld${EMULATION_NAME}_after_open,
219   gld${EMULATION_NAME}_after_allocation,
220   set_output_arch_default,
221   ldemul_default_target,
222   before_allocation_default,
223   gld${EMULATION_NAME}_get_script,
224   "${EMULATION_NAME}",
225   "${OUTPUT_FORMAT}",
226   NULL, /* finish */
227   NULL, /* create output section statements */
228   NULL, /* open dynamic archive */
229   NULL, /* place orphan */
230   NULL, /* set symbols */
231   NULL, /* parse args */
232   NULL, /* add_options */
233   NULL, /* handle_option */
234   NULL, /* unrecognized file */
235   NULL, /* list options */
236   NULL, /* recognized file */
237   NULL, /* find_potential_libraries */
238   NULL  /* new_vers_pattern */