Commit | Line | Data |
---|---|---|
20bccb34 NC |
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! */ | |
5 | ||
6 | /* Handle embedded relocs for m68k. | |
a2b64bed | 7 | Copyright 2000 Free Software Foundation, Inc. |
20bccb34 NC |
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>. | |
11 | ||
12 | This file is part of GLD, the Gnu Linker. | |
13 | ||
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. | |
18 | ||
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. | |
23 | ||
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. */ | |
27 | ||
28 | #define TARGET_IS_${EMULATION_NAME} | |
29 | ||
30 | #include "bfd.h" | |
31 | #include "sysdep.h" | |
32 | #include "bfdlink.h" | |
33 | ||
34 | #include "ld.h" | |
35 | #include "ldmain.h" | |
20bccb34 | 36 | #include "ldfile.h" |
41392f03 | 37 | #include "ldemul.h" |
20bccb34 NC |
38 | #include "ldmisc.h" |
39 | ||
40 | static void gld${EMULATION_NAME}_before_parse PARAMS ((void)); | |
41 | static void gld${EMULATION_NAME}_after_open PARAMS ((void)); | |
42 | static void check_sections PARAMS ((bfd *, asection *, PTR)); | |
43 | static void gld${EMULATION_NAME}_after_allocation PARAMS ((void)); | |
44 | static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile)); | |
45 | ||
46 | static void | |
47 | gld${EMULATION_NAME}_before_parse () | |
48 | { | |
49 | #ifndef TARGET_ /* I.e., if not generic. */ | |
50 | ldfile_set_output_arch ("`echo ${ARCH}`"); | |
51 | #endif /* not TARGET_ */ | |
52 | } | |
53 | ||
54 | /* This function is run after all the input files have been opened. | |
55 | We create a .emreloc section for each input file with a non zero | |
56 | .data section. The BFD backend will fill in these sections with | |
57 | magic numbers which can be used to relocate the data section at run | |
58 | time. */ | |
59 | ||
60 | static void | |
61 | gld${EMULATION_NAME}_after_open () | |
62 | { | |
63 | bfd *abfd; | |
64 | ||
65 | if (! command_line.embedded_relocs | |
66 | || link_info.relocateable) | |
67 | return; | |
68 | ||
69 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next) | |
70 | { | |
71 | asection *datasec; | |
72 | ||
73 | /* As first-order business, make sure that each input BFD is COFF. It | |
74 | better be, as we are directly calling a COFF backend function. */ | |
75 | if (bfd_get_flavour (abfd) != bfd_target_coff_flavour) | |
76 | einfo ("%F%B: all input objects must be COFF for --embedded-relocs\n"); | |
77 | ||
78 | datasec = bfd_get_section_by_name (abfd, ".data"); | |
79 | ||
80 | /* Note that we assume that the reloc_count field has already | |
81 | been set up. We could call bfd_get_reloc_upper_bound, but | |
82 | that returns the size of a memory buffer rather than a reloc | |
83 | count. We do not want to call bfd_canonicalize_reloc, | |
84 | because although it would always work it would force us to | |
85 | read in the relocs into BFD canonical form, which would waste | |
86 | a significant amount of time and memory. */ | |
87 | if (datasec != NULL && datasec->reloc_count > 0) | |
88 | { | |
89 | asection *relsec; | |
90 | ||
91 | relsec = bfd_make_section (abfd, ".emreloc"); | |
92 | if (relsec == NULL | |
93 | || ! bfd_set_section_flags (abfd, relsec, | |
94 | (SEC_ALLOC | |
95 | | SEC_LOAD | |
96 | | SEC_HAS_CONTENTS | |
97 | | SEC_IN_MEMORY)) | |
98 | || ! bfd_set_section_alignment (abfd, relsec, 2) | |
99 | || ! bfd_set_section_size (abfd, relsec, | |
100 | datasec->reloc_count * 12)) | |
101 | einfo ("%F%B: can not create .emreloc section: %E\n"); | |
102 | } | |
103 | ||
104 | /* Double check that all other data sections are empty, as is | |
105 | required for embedded PIC code. */ | |
106 | bfd_map_over_sections (abfd, check_sections, (PTR) datasec); | |
107 | } | |
108 | } | |
109 | ||
110 | /* Check that of the data sections, only the .data section has | |
111 | relocs. This is called via bfd_map_over_sections. */ | |
112 | ||
113 | static void | |
114 | check_sections (abfd, sec, datasec) | |
115 | bfd *abfd; | |
116 | asection *sec; | |
117 | PTR datasec; | |
118 | { | |
119 | if ((bfd_get_section_flags (abfd, sec) & SEC_DATA) | |
120 | && sec != (asection *) datasec | |
121 | && sec->reloc_count != 0) | |
122 | einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n", | |
123 | abfd, bfd_get_section_name (abfd, sec)); | |
124 | } | |
125 | ||
126 | /* This function is called after the section sizes and offsets have | |
127 | been set. If we are generating embedded relocs, it calls a special | |
128 | BFD backend routine to do the work. */ | |
129 | ||
130 | static void | |
131 | gld${EMULATION_NAME}_after_allocation () | |
132 | { | |
133 | bfd *abfd; | |
134 | ||
135 | if (! command_line.embedded_relocs | |
136 | || link_info.relocateable) | |
137 | return; | |
138 | ||
139 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next) | |
140 | { | |
141 | asection *datasec, *relsec; | |
142 | char *errmsg; | |
143 | ||
144 | datasec = bfd_get_section_by_name (abfd, ".data"); | |
145 | ||
146 | if (datasec == NULL || datasec->reloc_count == 0) | |
147 | continue; | |
148 | ||
149 | relsec = bfd_get_section_by_name (abfd, ".emreloc"); | |
150 | ASSERT (relsec != NULL); | |
151 | ||
152 | if (! bfd_m68k_coff_create_embedded_relocs (abfd, &link_info, | |
153 | datasec, relsec, | |
154 | &errmsg)) | |
155 | { | |
156 | if (errmsg == NULL) | |
157 | einfo ("%B%X: can not create runtime reloc information: %E\n", | |
158 | abfd); | |
159 | else | |
160 | einfo ("%X%B: can not create runtime reloc information: %s\n", | |
161 | abfd, errmsg); | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | static char * | |
167 | gld${EMULATION_NAME}_get_script(isfile) | |
168 | int *isfile; | |
169 | EOF | |
170 | ||
171 | if test -n "$COMPILE_IN" | |
172 | then | |
173 | # Scripts compiled in. | |
174 | ||
175 | # sed commands to quote an ld script as a C string. | |
176 | sc="-f stringify.sed" | |
177 | ||
178 | cat >>e${EMULATION_NAME}.c <<EOF | |
179 | { | |
180 | *isfile = 0; | |
181 | ||
182 | if (link_info.relocateable == true && config.build_constructors == true) | |
183 | return | |
184 | EOF | |
185 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c | |
186 | echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c | |
187 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c | |
188 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
189 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
190 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c | |
191 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
192 | echo ' ; else return' >> e${EMULATION_NAME}.c | |
193 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
194 | echo '; }' >> e${EMULATION_NAME}.c | |
195 | ||
196 | else | |
197 | # Scripts read from the filesystem. | |
198 | ||
199 | cat >>e${EMULATION_NAME}.c <<EOF | |
200 | { | |
201 | *isfile = 1; | |
202 | ||
203 | if (link_info.relocateable == true && config.build_constructors == true) | |
204 | return "ldscripts/${EMULATION_NAME}.xu"; | |
205 | else if (link_info.relocateable == true) | |
206 | return "ldscripts/${EMULATION_NAME}.xr"; | |
207 | else if (!config.text_read_only) | |
208 | return "ldscripts/${EMULATION_NAME}.xbn"; | |
209 | else if (!config.magic_demand_paged) | |
210 | return "ldscripts/${EMULATION_NAME}.xn"; | |
211 | else | |
212 | return "ldscripts/${EMULATION_NAME}.x"; | |
213 | } | |
214 | EOF | |
215 | ||
216 | fi | |
217 | ||
218 | cat >>e${EMULATION_NAME}.c <<EOF | |
219 | ||
220 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = | |
221 | { | |
222 | gld${EMULATION_NAME}_before_parse, | |
223 | syslib_default, | |
224 | hll_default, | |
225 | after_parse_default, | |
226 | gld${EMULATION_NAME}_after_open, | |
227 | gld${EMULATION_NAME}_after_allocation, | |
228 | set_output_arch_default, | |
229 | ldemul_default_target, | |
230 | before_allocation_default, | |
231 | gld${EMULATION_NAME}_get_script, | |
232 | "${EMULATION_NAME}", | |
233 | "${OUTPUT_FORMAT}", | |
234 | NULL, /* finish */ | |
235 | NULL, /* create output section statements */ | |
236 | NULL, /* open dynamic archive */ | |
237 | NULL, /* place orphan */ | |
238 | NULL, /* set symbols */ | |
239 | NULL, /* parse args */ | |
240 | NULL, /* unrecognized file */ | |
241 | NULL, /* list options */ | |
242 | NULL, /* recognized file */ | |
243 | NULL /* find_potential_libraries */ | |
244 | }; | |
245 | EOF |