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