Commit | Line | Data |
---|---|---|
2a9fa50c ILT |
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 | /* 32 bit ELF emulation code for ${EMULATION_NAME} | |
62548927 | 7 | Copyright (C) 1991, 93, 94, 95, 1996 Free Software Foundation, Inc. |
2a9fa50c ILT |
8 | Written by Steve Chamberlain <sac@cygnus.com> |
9 | ELF support by Ian Lance Taylor <ian@cygnus.com> | |
10 | ||
11 | This file is part of GLD, the Gnu Linker. | |
12 | ||
13 | This program is free software; you can redistribute it and/or modify | |
14 | it under the terms of the GNU General Public License as published by | |
15 | the Free Software Foundation; either version 2 of the License, or | |
16 | (at your option) any later version. | |
17 | ||
18 | This program is distributed in the hope that it will be useful, | |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | GNU General Public License for more details. | |
22 | ||
23 | You should have received a copy of the GNU General Public License | |
24 | along with this program; if not, write to the Free Software | |
943fbd5b | 25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
2a9fa50c ILT |
26 | |
27 | #define TARGET_IS_${EMULATION_NAME} | |
28 | ||
29 | #include "bfd.h" | |
30 | #include "sysdep.h" | |
5efddb2e ILT |
31 | |
32 | #include <ctype.h> | |
33 | ||
2a9fa50c ILT |
34 | #include "bfdlink.h" |
35 | ||
36 | #include "ld.h" | |
2a9fa50c ILT |
37 | #include "ldmain.h" |
38 | #include "ldemul.h" | |
39 | #include "ldfile.h" | |
40 | #include "ldmisc.h" | |
41 | #include "ldexp.h" | |
42 | #include "ldlang.h" | |
5efddb2e | 43 | #include "ldgram.h" |
2a9fa50c ILT |
44 | |
45 | static void gld${EMULATION_NAME}_before_parse PARAMS ((void)); | |
5efddb2e | 46 | static boolean gld${EMULATION_NAME}_open_dynamic_archive |
ed753c20 | 47 | PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *)); |
7523be35 ILT |
48 | static void gld${EMULATION_NAME}_after_open PARAMS ((void)); |
49 | static void gld${EMULATION_NAME}_check_needed | |
50 | PARAMS ((lang_input_statement_type *)); | |
8055e7a8 ILT |
51 | static void gld${EMULATION_NAME}_stat_needed |
52 | PARAMS ((lang_input_statement_type *)); | |
7523be35 ILT |
53 | static boolean gld${EMULATION_NAME}_search_needed |
54 | PARAMS ((const char *, const char *)); | |
55 | static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *)); | |
2a9fa50c ILT |
56 | static void gld${EMULATION_NAME}_before_allocation PARAMS ((void)); |
57 | static void gld${EMULATION_NAME}_find_statement_assignment | |
58 | PARAMS ((lang_statement_union_type *)); | |
59 | static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *)); | |
5efddb2e ILT |
60 | static boolean gld${EMULATION_NAME}_place_orphan |
61 | PARAMS ((lang_input_statement_type *, asection *)); | |
62 | static void gld${EMULATION_NAME}_place_section | |
63 | PARAMS ((lang_statement_union_type *)); | |
2a9fa50c ILT |
64 | static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile)); |
65 | ||
66 | static void | |
67 | gld${EMULATION_NAME}_before_parse() | |
68 | { | |
db3bfcd6 | 69 | ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`; |
5efddb2e ILT |
70 | config.dynamic_link = ${DYNAMIC_LINK-true}; |
71 | } | |
72 | ||
73 | /* Try to open a dynamic archive. This is where we know that ELF | |
74 | dynamic libraries have an extension of .so. */ | |
75 | ||
76 | static boolean | |
ed753c20 | 77 | gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry) |
5efddb2e | 78 | const char *arch; |
ed753c20 | 79 | search_dirs_type *search; |
5efddb2e ILT |
80 | lang_input_statement_type *entry; |
81 | { | |
82 | const char *filename; | |
ed753c20 ILT |
83 | char *string; |
84 | ||
85 | if (! entry->is_archive) | |
86 | return false; | |
5efddb2e ILT |
87 | |
88 | filename = entry->filename; | |
89 | ||
ed753c20 ILT |
90 | string = (char *) xmalloc (strlen (search->name) |
91 | + strlen (filename) | |
92 | + strlen (arch) | |
93 | + sizeof "/lib.so"); | |
94 | ||
95 | sprintf (string, "%s/lib%s%s.so", search->name, filename, arch); | |
96 | ||
97 | if (! ldfile_try_open_bfd (string, entry)) | |
98 | { | |
99 | free (string); | |
100 | return false; | |
101 | } | |
102 | ||
103 | entry->filename = string; | |
5efddb2e ILT |
104 | |
105 | /* We have found a dynamic object to include in the link. The ELF | |
106 | backend linker will create a DT_NEEDED entry in the .dynamic | |
107 | section naming this file. If this file includes a DT_SONAME | |
108 | entry, it will be used. Otherwise, the ELF linker will just use | |
109 | the name of the file. For an archive found by searching, like | |
110 | this one, the DT_NEEDED entry should consist of just the name of | |
111 | the file, without the path information used to find it. Note | |
112 | that we only need to do this if we have a dynamic object; an | |
113 | archive will never be referenced by a DT_NEEDED entry. | |
114 | ||
115 | FIXME: This approach--using bfd_elf_set_dt_needed_name--is not | |
116 | very pretty. I haven't been able to think of anything that is | |
117 | pretty, though. */ | |
118 | if (bfd_check_format (entry->the_bfd, bfd_object) | |
119 | && (entry->the_bfd->flags & DYNAMIC) != 0) | |
120 | { | |
121 | char *needed_name; | |
122 | ||
123 | ASSERT (entry->is_archive && entry->search_dirs_flag); | |
124 | needed_name = (char *) xmalloc (strlen (filename) | |
125 | + strlen (arch) | |
126 | + sizeof "lib.so"); | |
127 | sprintf (needed_name, "lib%s%s.so", filename, arch); | |
128 | bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name); | |
129 | } | |
130 | ||
131 | return true; | |
2a9fa50c ILT |
132 | } |
133 | ||
7523be35 | 134 | /* These variables are required to pass information back and forth |
8055e7a8 | 135 | between after_open and check_needed and stat_needed. */ |
7523be35 | 136 | |
62548927 | 137 | static struct bfd_link_needed_list *global_needed; |
8055e7a8 | 138 | static struct stat global_stat; |
7523be35 ILT |
139 | static boolean global_found; |
140 | ||
141 | /* This is called after all the input files have been opened. */ | |
142 | ||
143 | static void | |
144 | gld${EMULATION_NAME}_after_open () | |
145 | { | |
62548927 | 146 | struct bfd_link_needed_list *needed, *l; |
7523be35 | 147 | |
776f2590 ILT |
148 | /* We only need to worry about this when doing a final link. */ |
149 | if (link_info.relocateable || link_info.shared) | |
150 | return; | |
151 | ||
7523be35 ILT |
152 | /* Get the list of files which appear in DT_NEEDED entries in |
153 | dynamic objects included in the link (often there will be none). | |
154 | For each such file, we want to track down the corresponding | |
155 | library, and include the symbol table in the link. This is what | |
156 | the runtime dynamic linker will do. Tracking the files down here | |
157 | permits one dynamic object to include another without requiring | |
158 | special action by the person doing the link. Note that the | |
159 | needed list can actually grow while we are stepping through this | |
160 | loop. */ | |
161 | needed = bfd_elf_get_needed_list (output_bfd, &link_info); | |
162 | for (l = needed; l != NULL; l = l->next) | |
163 | { | |
62548927 | 164 | struct bfd_link_needed_list *ll; |
7523be35 ILT |
165 | const char *lib_path; |
166 | size_t len; | |
167 | search_dirs_type *search; | |
168 | ||
169 | /* If we've already seen this file, skip it. */ | |
170 | for (ll = needed; ll != l; ll = ll->next) | |
171 | if (strcmp (ll->name, l->name) == 0) | |
172 | break; | |
173 | if (ll != l) | |
174 | continue; | |
175 | ||
176 | /* See if this file was included in the link explicitly. */ | |
177 | global_needed = l; | |
178 | global_found = false; | |
179 | lang_for_each_input_file (gld${EMULATION_NAME}_check_needed); | |
180 | if (global_found) | |
181 | continue; | |
182 | ||
183 | /* We need to find this file and include the symbol table. We | |
184 | want to search for the file in the same way that the dynamic | |
62548927 ILT |
185 | linker will search. That means that we want to use |
186 | rpath_link, rpath, then the environment variable | |
187 | LD_LIBRARY_PATH (native only), then the linker script | |
188 | LIB_SEARCH_DIRS. We do not search using the -L arguments. */ | |
189 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link, | |
190 | l->name)) | |
191 | continue; | |
7523be35 ILT |
192 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath, l->name)) |
193 | continue; | |
62548927 ILT |
194 | if (command_line.rpath_link == NULL |
195 | && command_line.rpath == NULL) | |
196 | { | |
197 | lib_path = (const char *) getenv ("LD_RUN_PATH"); | |
198 | if (gld${EMULATION_NAME}_search_needed (lib_path, l->name)) | |
199 | continue; | |
200 | } | |
201 | EOF | |
202 | if [ "x${host}" = "x${target}" ] ; then | |
203 | cat >>e${EMULATION_NAME}.c <<EOF | |
7523be35 ILT |
204 | lib_path = (const char *) getenv ("LD_LIBRARY_PATH"); |
205 | if (gld${EMULATION_NAME}_search_needed (lib_path, l->name)) | |
206 | continue; | |
62548927 ILT |
207 | EOF |
208 | fi | |
209 | cat >>e${EMULATION_NAME}.c <<EOF | |
7523be35 ILT |
210 | len = strlen (l->name); |
211 | for (search = search_head; search != NULL; search = search->next) | |
212 | { | |
213 | char *filename; | |
214 | ||
215 | if (search->cmdline) | |
216 | continue; | |
217 | filename = (char *) xmalloc (strlen (search->name) + len + 2); | |
218 | sprintf (filename, "%s/%s", search->name, l->name); | |
219 | if (gld${EMULATION_NAME}_try_needed (filename)) | |
220 | break; | |
221 | free (filename); | |
222 | } | |
223 | if (search != NULL) | |
224 | continue; | |
225 | ||
226 | einfo ("%P: warning: %s, needed by %B, not found\n", | |
227 | l->name, l->by); | |
228 | } | |
229 | } | |
230 | ||
231 | /* Search for a needed file in a path. */ | |
232 | ||
233 | static boolean | |
234 | gld${EMULATION_NAME}_search_needed (path, name) | |
235 | const char *path; | |
236 | const char *name; | |
237 | { | |
238 | const char *s; | |
239 | size_t len; | |
240 | ||
241 | if (path == NULL || *path == '\0') | |
242 | return false; | |
243 | len = strlen (name); | |
244 | while (1) | |
245 | { | |
246 | char *filename, *sset; | |
247 | ||
248 | s = strchr (path, ':'); | |
249 | if (s == NULL) | |
250 | s = path + strlen (path); | |
251 | ||
252 | filename = (char *) xmalloc (s - path + len + 2); | |
253 | if (s == path) | |
254 | sset = filename; | |
255 | else | |
256 | { | |
257 | memcpy (filename, path, s - path); | |
258 | filename[s - path] = '/'; | |
259 | sset = filename + (s - path) + 1; | |
260 | } | |
261 | strcpy (sset, name); | |
262 | ||
263 | if (gld${EMULATION_NAME}_try_needed (filename)) | |
264 | return true; | |
265 | ||
266 | free (filename); | |
267 | ||
268 | if (*s == '\0') | |
269 | break; | |
270 | path = s + 1; | |
271 | } | |
272 | ||
273 | return false; | |
274 | } | |
275 | ||
276 | /* This function is called for each possible name for a dynamic object | |
277 | named by a DT_NEEDED entry. */ | |
278 | ||
279 | static boolean | |
280 | gld${EMULATION_NAME}_try_needed (name) | |
281 | const char *name; | |
282 | { | |
283 | bfd *abfd; | |
284 | ||
285 | abfd = bfd_openr (name, bfd_get_target (output_bfd)); | |
286 | if (abfd == NULL) | |
287 | return false; | |
288 | if (! bfd_check_format (abfd, bfd_object)) | |
289 | { | |
290 | (void) bfd_close (abfd); | |
291 | return false; | |
292 | } | |
293 | if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0) | |
294 | { | |
295 | (void) bfd_close (abfd); | |
296 | return false; | |
297 | } | |
298 | ||
299 | /* We've found a dynamic object matching the DT_NEEDED entry. */ | |
300 | ||
8055e7a8 ILT |
301 | /* We have already checked that there is no other input file of the |
302 | same name. We must now check again that we are not including the | |
303 | same file twice. We need to do this because on many systems | |
304 | libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will | |
305 | reference libc.so.1. If we have already included libc.so, we | |
306 | don't want to include libc.so.1 if they are the same file, and we | |
307 | can only check that using stat. */ | |
308 | ||
309 | if (bfd_stat (abfd, &global_stat) != 0) | |
310 | einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd); | |
311 | global_found = false; | |
312 | lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed); | |
313 | if (global_found) | |
314 | { | |
315 | /* Return true to indicate that we found the file, even though | |
316 | we aren't going to do anything with it. */ | |
317 | return true; | |
318 | } | |
319 | ||
7523be35 ILT |
320 | /* Tell the ELF backend that don't want the output file to have a |
321 | DT_NEEDED entry for this file. */ | |
322 | bfd_elf_set_dt_needed_name (abfd, ""); | |
323 | ||
324 | /* Add this file into the symbol table. */ | |
325 | if (! bfd_link_add_symbols (abfd, &link_info)) | |
326 | einfo ("%F%B: could not read symbols: %E\n", abfd); | |
327 | ||
328 | return true; | |
329 | } | |
330 | ||
8055e7a8 | 331 | /* See if an input file matches a DT_NEEDED entry by name. */ |
7523be35 ILT |
332 | |
333 | static void | |
334 | gld${EMULATION_NAME}_check_needed (s) | |
335 | lang_input_statement_type *s; | |
336 | { | |
169df8d5 ILT |
337 | if (global_found) |
338 | return; | |
339 | ||
7523be35 ILT |
340 | if (s->filename != NULL |
341 | && strcmp (s->filename, global_needed->name) == 0) | |
169df8d5 ILT |
342 | { |
343 | global_found = true; | |
344 | return; | |
345 | } | |
346 | ||
347 | if (s->the_bfd != NULL) | |
348 | { | |
349 | const char *soname; | |
350 | ||
351 | soname = bfd_elf_get_dt_soname (s->the_bfd); | |
352 | if (soname != NULL | |
353 | && strcmp (soname, global_needed->name) == 0) | |
354 | { | |
355 | global_found = true; | |
356 | return; | |
357 | } | |
358 | } | |
359 | ||
360 | if (s->search_dirs_flag | |
361 | && s->filename != NULL | |
362 | && strchr (global_needed->name, '/') == NULL) | |
ed753c20 ILT |
363 | { |
364 | const char *f; | |
365 | ||
366 | f = strrchr (s->filename, '/'); | |
367 | if (f != NULL | |
368 | && strcmp (f + 1, global_needed->name) == 0) | |
169df8d5 ILT |
369 | { |
370 | global_found = true; | |
371 | return; | |
372 | } | |
ed753c20 | 373 | } |
7523be35 ILT |
374 | } |
375 | ||
8055e7a8 ILT |
376 | /* See if an input file matches a DT_NEEDED entry by running stat on |
377 | the file. */ | |
378 | ||
379 | static void | |
380 | gld${EMULATION_NAME}_stat_needed (s) | |
381 | lang_input_statement_type *s; | |
382 | { | |
62548927 | 383 | struct stat st; |
169df8d5 ILT |
384 | const char *suffix; |
385 | const char *soname; | |
386 | const char *f; | |
62548927 | 387 | |
8055e7a8 ILT |
388 | if (global_found) |
389 | return; | |
62548927 ILT |
390 | if (s->the_bfd == NULL) |
391 | return; | |
392 | ||
393 | if (bfd_stat (s->the_bfd, &st) != 0) | |
8055e7a8 | 394 | { |
62548927 ILT |
395 | einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd); |
396 | return; | |
397 | } | |
8055e7a8 | 398 | |
62548927 ILT |
399 | if (st.st_dev == global_stat.st_dev |
400 | && st.st_ino == global_stat.st_ino) | |
401 | { | |
402 | global_found = true; | |
403 | return; | |
404 | } | |
405 | ||
406 | /* We issue a warning if it looks like we are including two | |
407 | different versions of the same shared library. For example, | |
408 | there may be a problem if -lc picks up libc.so.6 but some other | |
169df8d5 ILT |
409 | shared library has a DT_NEEDED entry of libc.so.5. This is a |
410 | hueristic test, and it will only work if the name looks like | |
411 | NAME.so.VERSION. FIXME: Depending on file names is error-prone. | |
412 | If we really want to issue warnings about mixing version numbers | |
413 | of shared libraries, we need to find a better way. */ | |
62548927 ILT |
414 | |
415 | if (strchr (global_needed->name, '/') != NULL) | |
416 | return; | |
169df8d5 ILT |
417 | suffix = strstr (global_needed->name, ".so."); |
418 | if (suffix == NULL) | |
419 | return; | |
420 | suffix += sizeof ".so." - 1; | |
421 | ||
422 | soname = bfd_elf_get_dt_soname (s->the_bfd); | |
423 | if (soname == NULL) | |
424 | soname = s->filename; | |
62548927 | 425 | |
169df8d5 | 426 | f = strrchr (soname, '/'); |
62548927 ILT |
427 | if (f != NULL) |
428 | ++f; | |
429 | else | |
169df8d5 | 430 | f = soname; |
62548927 | 431 | |
169df8d5 | 432 | if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0) |
62548927 | 433 | einfo ("%P: warning: %s, needed by %B, may conflict with %s\n", |
169df8d5 | 434 | global_needed->name, global_needed->by, f); |
8055e7a8 ILT |
435 | } |
436 | ||
2a9fa50c ILT |
437 | /* This is called after the sections have been attached to output |
438 | sections, but before any sizes or addresses have been set. */ | |
439 | ||
440 | static void | |
441 | gld${EMULATION_NAME}_before_allocation () | |
442 | { | |
62548927 | 443 | const char *rpath; |
7fb9ca5f ILT |
444 | asection *sinterp; |
445 | ||
2a9fa50c ILT |
446 | /* If we are going to make any variable assignments, we need to let |
447 | the ELF backend know about them in case the variables are | |
448 | referred to by dynamic objects. */ | |
449 | lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment); | |
450 | ||
451 | /* Let the ELF backend work out the sizes of any sections required | |
452 | by dynamic linking. */ | |
62548927 ILT |
453 | rpath = command_line.rpath; |
454 | if (rpath == NULL) | |
455 | rpath = (const char *) getenv ("LD_RUN_PATH"); | |
5efddb2e ILT |
456 | if (! bfd_elf32_size_dynamic_sections (output_bfd, |
457 | command_line.soname, | |
62548927 | 458 | rpath, |
5efddb2e ILT |
459 | command_line.export_dynamic, |
460 | &link_info, | |
7fb9ca5f | 461 | &sinterp)) |
2a9fa50c | 462 | einfo ("%P%F: failed to set dynamic section sizes: %E\n"); |
7fb9ca5f ILT |
463 | |
464 | /* Let the user override the dynamic linker we are using. */ | |
465 | if (command_line.interpreter != NULL | |
466 | && sinterp != NULL) | |
467 | { | |
468 | sinterp->contents = (bfd_byte *) command_line.interpreter; | |
469 | sinterp->_raw_size = strlen (command_line.interpreter) + 1; | |
470 | } | |
5f3548d6 ILT |
471 | |
472 | /* Look for any sections named .gnu.warning. As a GNU extensions, | |
473 | we treat such sections as containing warning messages. We print | |
474 | out the warning message, and then zero out the section size so | |
475 | that it does not get copied into the output file. */ | |
476 | ||
477 | { | |
478 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
479 | { | |
480 | asection *s; | |
481 | bfd_size_type sz; | |
482 | char *msg; | |
483 | boolean ret; | |
484 | ||
485 | if (is->just_syms_flag) | |
486 | continue; | |
487 | ||
488 | s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning"); | |
489 | if (s == NULL) | |
490 | continue; | |
491 | ||
492 | sz = bfd_section_size (is->the_bfd, s); | |
493 | msg = xmalloc ((size_t) sz + 1); | |
494 | if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz)) | |
495 | einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n", | |
496 | is->the_bfd); | |
497 | msg[sz] = '\0'; | |
8055e7a8 ILT |
498 | ret = link_info.callbacks->warning (&link_info, msg, |
499 | (const char *) NULL, | |
500 | is->the_bfd, (asection *) NULL, | |
501 | (bfd_vma) 0); | |
5f3548d6 ILT |
502 | ASSERT (ret); |
503 | free (msg); | |
504 | ||
505 | /* Clobber the section size, so that we don't waste copying the | |
506 | warning into the output file. */ | |
507 | s->_raw_size = 0; | |
508 | } | |
509 | } | |
7523be35 ILT |
510 | |
511 | #if defined (TARGET_IS_elf32bmip) || defined (TARGET_IS_elf32lmip) | |
512 | /* For MIPS ELF the .reginfo section requires special handling. | |
513 | Each input section is 24 bytes, and the final output section must | |
514 | also be 24 bytes. We handle this by clobbering all but the first | |
515 | input section size to 0. The .reginfo section is handled | |
516 | specially by the backend code anyhow. */ | |
517 | { | |
518 | boolean found = false; | |
519 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
520 | { | |
521 | asection *s; | |
522 | ||
523 | if (is->just_syms_flag) | |
524 | continue; | |
525 | ||
526 | s = bfd_get_section_by_name (is->the_bfd, ".reginfo"); | |
527 | if (s == NULL) | |
528 | continue; | |
529 | ||
530 | if (! found) | |
531 | { | |
532 | found = true; | |
533 | continue; | |
534 | } | |
535 | ||
536 | s->_raw_size = 0; | |
537 | s->_cooked_size = 0; | |
538 | } | |
539 | } | |
540 | #endif | |
2a9fa50c ILT |
541 | } |
542 | ||
543 | /* This is called by the before_allocation routine via | |
544 | lang_for_each_statement. It locates any assignment statements, and | |
545 | tells the ELF backend about them, in case they are assignments to | |
546 | symbols which are referred to by dynamic objects. */ | |
547 | ||
548 | static void | |
549 | gld${EMULATION_NAME}_find_statement_assignment (s) | |
550 | lang_statement_union_type *s; | |
551 | { | |
552 | if (s->header.type == lang_assignment_statement_enum) | |
553 | gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp); | |
554 | } | |
555 | ||
556 | /* Look through an expression for an assignment statement. */ | |
557 | ||
558 | static void | |
559 | gld${EMULATION_NAME}_find_exp_assignment (exp) | |
560 | etree_type *exp; | |
561 | { | |
7158d2b1 ILT |
562 | struct bfd_link_hash_entry *h; |
563 | ||
2a9fa50c ILT |
564 | switch (exp->type.node_class) |
565 | { | |
7158d2b1 ILT |
566 | case etree_provide: |
567 | h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst, | |
568 | false, false, false); | |
569 | if (h == NULL) | |
570 | break; | |
571 | ||
572 | /* We call record_link_assignment even if the symbol is defined. | |
573 | This is because if it is defined by a dynamic object, we | |
574 | actually want to use the value defined by the linker script, | |
575 | not the value from the dynamic object (because we are setting | |
576 | symbols like etext). If the symbol is defined by a regular | |
577 | object, then, as it happens, calling record_link_assignment | |
578 | will do no harm. */ | |
579 | ||
580 | /* Fall through. */ | |
2a9fa50c ILT |
581 | case etree_assign: |
582 | if (strcmp (exp->assign.dst, ".") != 0) | |
583 | { | |
7158d2b1 ILT |
584 | if (! (bfd_elf32_record_link_assignment |
585 | (output_bfd, &link_info, exp->assign.dst, | |
586 | exp->type.node_class == etree_provide ? true : false))) | |
2a9fa50c ILT |
587 | einfo ("%P%F: failed to record assignment to %s: %E\n", |
588 | exp->assign.dst); | |
589 | } | |
590 | gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src); | |
591 | break; | |
592 | ||
593 | case etree_binary: | |
594 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs); | |
595 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs); | |
596 | break; | |
597 | ||
598 | case etree_trinary: | |
599 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs); | |
600 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs); | |
601 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs); | |
602 | break; | |
603 | ||
604 | case etree_unary: | |
605 | gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child); | |
606 | break; | |
607 | ||
608 | default: | |
609 | break; | |
610 | } | |
611 | } | |
612 | ||
5efddb2e ILT |
613 | /* Place an orphan section. We use this to put random SHF_ALLOC |
614 | sections in the right segment. */ | |
615 | ||
616 | static asection *hold_section; | |
617 | static lang_output_section_statement_type *hold_use; | |
618 | static lang_output_section_statement_type *hold_text; | |
619 | static lang_output_section_statement_type *hold_data; | |
620 | static lang_output_section_statement_type *hold_bss; | |
7523be35 | 621 | static lang_output_section_statement_type *hold_rel; |
5efddb2e ILT |
622 | |
623 | /*ARGSUSED*/ | |
624 | static boolean | |
625 | gld${EMULATION_NAME}_place_orphan (file, s) | |
626 | lang_input_statement_type *file; | |
627 | asection *s; | |
628 | { | |
629 | lang_output_section_statement_type *place; | |
630 | asection *snew, **pps; | |
631 | lang_statement_list_type *old; | |
632 | lang_statement_list_type add; | |
633 | etree_type *address; | |
634 | const char *secname, *ps; | |
635 | lang_output_section_statement_type *os; | |
636 | ||
637 | if ((s->flags & SEC_ALLOC) == 0) | |
638 | return false; | |
639 | ||
640 | /* Look through the script to see where to place this section. */ | |
641 | hold_section = s; | |
642 | hold_use = NULL; | |
643 | lang_for_each_statement (gld${EMULATION_NAME}_place_section); | |
644 | ||
645 | if (hold_use != NULL) | |
646 | { | |
647 | /* We have already placed a section with this name. */ | |
648 | wild_doit (&hold_use->children, s, hold_use, file); | |
649 | return true; | |
650 | } | |
651 | ||
7523be35 ILT |
652 | secname = bfd_get_section_name (s->owner, s); |
653 | ||
ed753c20 ILT |
654 | /* If this is a final link, then always put .gnu.warning.SYMBOL |
655 | sections into the .text section to get them out of the way. */ | |
656 | if (! link_info.shared | |
657 | && ! link_info.relocateable | |
658 | && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0 | |
659 | && hold_text != NULL) | |
660 | { | |
661 | wild_doit (&hold_text->children, s, hold_text, file); | |
662 | return true; | |
663 | } | |
664 | ||
5efddb2e | 665 | /* Decide which segment the section should go in based on the |
7523be35 | 666 | section name and section flags. */ |
5efddb2e ILT |
667 | place = NULL; |
668 | if ((s->flags & SEC_HAS_CONTENTS) == 0 | |
669 | && hold_bss != NULL) | |
670 | place = hold_bss; | |
671 | else if ((s->flags & SEC_READONLY) == 0 | |
672 | && hold_data != NULL) | |
673 | place = hold_data; | |
7523be35 ILT |
674 | else if (strncmp (secname, ".rel", 4) == 0 |
675 | && hold_rel != NULL) | |
676 | place = hold_rel; | |
5efddb2e ILT |
677 | else if ((s->flags & SEC_READONLY) != 0 |
678 | && hold_text != NULL) | |
679 | place = hold_text; | |
680 | if (place == NULL) | |
681 | return false; | |
682 | ||
5efddb2e | 683 | /* Create the section in the output file, and put it in the right |
7523be35 | 684 | place. This shuffling is to make the output file look neater. */ |
5efddb2e ILT |
685 | snew = bfd_make_section (output_bfd, secname); |
686 | if (snew == NULL) | |
687 | einfo ("%P%F: output format %s cannot represent section called %s\n", | |
688 | output_bfd->xvec->name, secname); | |
7523be35 ILT |
689 | if (place->bfd_section != NULL) |
690 | { | |
691 | for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next) | |
692 | ; | |
693 | *pps = snew->next; | |
694 | snew->next = place->bfd_section->next; | |
695 | place->bfd_section->next = snew; | |
696 | } | |
5efddb2e ILT |
697 | |
698 | /* Start building a list of statements for this section. */ | |
699 | old = stat_ptr; | |
700 | stat_ptr = &add; | |
701 | lang_list_init (stat_ptr); | |
702 | ||
703 | /* If the name of the section is representable in C, then create | |
704 | symbols to mark the start and the end of the section. */ | |
705 | for (ps = secname; *ps != '\0'; ps++) | |
706 | if (! isalnum (*ps) && *ps != '_') | |
707 | break; | |
708 | if (*ps == '\0' && config.build_constructors) | |
709 | { | |
710 | char *symname; | |
711 | ||
712 | symname = (char *) xmalloc (ps - secname + sizeof "__start_"); | |
713 | sprintf (symname, "__start_%s", secname); | |
714 | lang_add_assignment (exp_assop ('=', symname, | |
715 | exp_nameop (NAME, "."))); | |
716 | } | |
717 | ||
718 | if (! link_info.relocateable) | |
719 | address = NULL; | |
720 | else | |
721 | address = exp_intop ((bfd_vma) 0); | |
722 | ||
723 | lang_enter_output_section_statement (secname, address, 0, | |
724 | (bfd_vma) 0, | |
725 | (etree_type *) NULL, | |
726 | (etree_type *) NULL, | |
727 | (etree_type *) NULL); | |
728 | ||
729 | os = lang_output_section_statement_lookup (secname); | |
730 | wild_doit (&os->children, s, os, file); | |
731 | ||
732 | lang_leave_output_section_statement ((bfd_vma) 0, "*default*"); | |
733 | stat_ptr = &add; | |
734 | ||
735 | if (*ps == '\0' && config.build_constructors) | |
736 | { | |
737 | char *symname; | |
738 | ||
739 | symname = (char *) xmalloc (ps - secname + sizeof "__stop_"); | |
740 | sprintf (symname, "__stop_%s", secname); | |
741 | lang_add_assignment (exp_assop ('=', symname, | |
742 | exp_nameop (NAME, "."))); | |
743 | } | |
744 | ||
745 | /* Now stick the new statement list right after PLACE. */ | |
746 | *add.tail = place->header.next; | |
747 | place->header.next = add.head; | |
748 | ||
749 | stat_ptr = old; | |
750 | ||
751 | return true; | |
752 | } | |
753 | ||
754 | static void | |
755 | gld${EMULATION_NAME}_place_section (s) | |
756 | lang_statement_union_type *s; | |
757 | { | |
758 | lang_output_section_statement_type *os; | |
759 | ||
760 | if (s->header.type != lang_output_section_statement_enum) | |
761 | return; | |
762 | ||
763 | os = &s->output_section_statement; | |
764 | ||
765 | if (strcmp (os->name, hold_section->name) == 0) | |
766 | hold_use = os; | |
767 | ||
768 | if (strcmp (os->name, ".text") == 0) | |
769 | hold_text = os; | |
770 | else if (strcmp (os->name, ".data") == 0) | |
771 | hold_data = os; | |
772 | else if (strcmp (os->name, ".bss") == 0) | |
773 | hold_bss = os; | |
7523be35 ILT |
774 | else if (hold_rel == NULL |
775 | && strncmp (os->name, ".rel", 4) == 0) | |
776 | hold_rel = os; | |
5efddb2e ILT |
777 | } |
778 | ||
2a9fa50c ILT |
779 | static char * |
780 | gld${EMULATION_NAME}_get_script(isfile) | |
781 | int *isfile; | |
782 | EOF | |
783 | ||
784 | if test -n "$COMPILE_IN" | |
785 | then | |
786 | # Scripts compiled in. | |
787 | ||
788 | # sed commands to quote an ld script as a C string. | |
789 | sc='s/["\\]/\\&/g | |
790 | s/$/\\n\\/ | |
791 | 1s/^/"/ | |
792 | $s/$/n"/ | |
793 | ' | |
794 | ||
795 | cat >>e${EMULATION_NAME}.c <<EOF | |
796 | { | |
797 | *isfile = 0; | |
798 | ||
799 | if (link_info.relocateable == true && config.build_constructors == true) | |
800 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`; | |
801 | else if (link_info.relocateable == true) | |
802 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`; | |
803 | else if (!config.text_read_only) | |
804 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`; | |
805 | else if (!config.magic_demand_paged) | |
806 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`; | |
5efddb2e ILT |
807 | else if (link_info.shared) |
808 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`; | |
2a9fa50c ILT |
809 | else |
810 | return `sed "$sc" ldscripts/${EMULATION_NAME}.x`; | |
811 | } | |
812 | EOF | |
813 | ||
814 | else | |
815 | # Scripts read from the filesystem. | |
816 | ||
817 | cat >>e${EMULATION_NAME}.c <<EOF | |
818 | { | |
819 | *isfile = 1; | |
820 | ||
821 | if (link_info.relocateable == true && config.build_constructors == true) | |
822 | return "ldscripts/${EMULATION_NAME}.xu"; | |
823 | else if (link_info.relocateable == true) | |
824 | return "ldscripts/${EMULATION_NAME}.xr"; | |
825 | else if (!config.text_read_only) | |
826 | return "ldscripts/${EMULATION_NAME}.xbn"; | |
827 | else if (!config.magic_demand_paged) | |
828 | return "ldscripts/${EMULATION_NAME}.xn"; | |
5efddb2e ILT |
829 | else if (link_info.shared) |
830 | return "ldscripts/${EMULATION_NAME}.xs"; | |
2a9fa50c ILT |
831 | else |
832 | return "ldscripts/${EMULATION_NAME}.x"; | |
833 | } | |
834 | EOF | |
835 | ||
836 | fi | |
837 | ||
838 | cat >>e${EMULATION_NAME}.c <<EOF | |
839 | ||
840 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = | |
841 | { | |
842 | gld${EMULATION_NAME}_before_parse, | |
843 | syslib_default, | |
844 | hll_default, | |
845 | after_parse_default, | |
7523be35 | 846 | gld${EMULATION_NAME}_after_open, |
2a9fa50c ILT |
847 | after_allocation_default, |
848 | set_output_arch_default, | |
849 | ldemul_default_target, | |
850 | gld${EMULATION_NAME}_before_allocation, | |
851 | gld${EMULATION_NAME}_get_script, | |
852 | "${EMULATION_NAME}", | |
5efddb2e ILT |
853 | "${OUTPUT_FORMAT}", |
854 | NULL, | |
855 | NULL, | |
856 | gld${EMULATION_NAME}_open_dynamic_archive, | |
857 | gld${EMULATION_NAME}_place_orphan | |
2a9fa50c ILT |
858 | }; |
859 | EOF |