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