Commit | Line | Data |
---|---|---|
252b5132 RH |
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 | /* SunOS emulation code for ${EMULATION_NAME} | |
7 | Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 1999 | |
8 | Free Software Foundation, Inc. | |
9 | Written by Steve Chamberlain <sac@cygnus.com> | |
10 | SunOS shared library support 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 <ctype.h> | |
31 | ||
32 | #include "bfd.h" | |
33 | #include "sysdep.h" | |
34 | #include "bfdlink.h" | |
35 | #include "libiberty.h" | |
36 | ||
37 | #include "ld.h" | |
38 | #include "ldmain.h" | |
39 | #include "ldemul.h" | |
40 | #include "ldfile.h" | |
41 | #include "ldmisc.h" | |
42 | #include "ldexp.h" | |
43 | #include "ldlang.h" | |
44 | ||
45 | #ifdef HAVE_DIRENT_H | |
46 | # include <dirent.h> | |
47 | #else | |
48 | # define dirent direct | |
49 | # ifdef HAVE_SYS_NDIR_H | |
50 | # include <sys/ndir.h> | |
51 | # endif | |
52 | # ifdef HAVE_SYS_DIR_H | |
53 | # include <sys/dir.h> | |
54 | # endif | |
55 | # ifdef HAVE_NDIR_H | |
56 | # include <ndir.h> | |
57 | # endif | |
58 | #endif | |
59 | ||
60 | static void gld${EMULATION_NAME}_before_parse PARAMS ((void)); | |
61 | static void gld${EMULATION_NAME}_set_symbols PARAMS ((void)); | |
62 | static void gld${EMULATION_NAME}_create_output_section_statements | |
63 | PARAMS ((void)); | |
64 | static void gld${EMULATION_NAME}_find_so | |
65 | PARAMS ((lang_input_statement_type *)); | |
66 | static char *gld${EMULATION_NAME}_search_dir | |
67 | PARAMS ((const char *, const char *, boolean *)); | |
68 | static void gld${EMULATION_NAME}_after_open PARAMS ((void)); | |
69 | static void gld${EMULATION_NAME}_check_needed | |
70 | PARAMS ((lang_input_statement_type *)); | |
71 | static boolean gld${EMULATION_NAME}_search_needed | |
72 | PARAMS ((const char *, const char *)); | |
73 | static boolean gld${EMULATION_NAME}_try_needed | |
74 | PARAMS ((const char *, const char *)); | |
75 | static void gld${EMULATION_NAME}_before_allocation PARAMS ((void)); | |
76 | static void gld${EMULATION_NAME}_find_assignment | |
77 | PARAMS ((lang_statement_union_type *)); | |
78 | static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *)); | |
79 | static void gld${EMULATION_NAME}_count_need | |
80 | PARAMS ((lang_input_statement_type *)); | |
81 | static void gld${EMULATION_NAME}_set_need | |
82 | PARAMS ((lang_input_statement_type *)); | |
83 | static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile)); | |
84 | ||
85 | static void | |
86 | gld${EMULATION_NAME}_before_parse() | |
87 | { | |
88 | ldfile_output_architecture = bfd_arch_${ARCH}; | |
89 | config.dynamic_link = true; | |
90 | config.has_shared = true; | |
91 | } | |
92 | ||
93 | /* This is called after the command line arguments have been parsed, | |
94 | but before the linker script has been read. If this is a native | |
95 | linker, we add the directories in LD_LIBRARY_PATH to the search | |
96 | list. */ | |
97 | ||
98 | static void | |
99 | gld${EMULATION_NAME}_set_symbols () | |
100 | { | |
101 | EOF | |
102 | if [ "x${host}" = "x${target}" ] ; then | |
103 | if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then | |
104 | cat >>e${EMULATION_NAME}.c <<EOF | |
105 | const char *env; | |
106 | ||
107 | env = (const char *) getenv ("LD_LIBRARY_PATH"); | |
108 | if (env != NULL) | |
109 | { | |
110 | char *l; | |
111 | ||
112 | l = xstrdup (env); | |
113 | while (1) | |
114 | { | |
115 | char *c; | |
116 | ||
117 | c = strchr (l, ':'); | |
118 | if (c != NULL) | |
119 | *c++ = '\0'; | |
120 | if (*l != '\0') | |
121 | ldfile_add_library_path (l, false); | |
122 | if (c == NULL) | |
123 | break; | |
124 | l = c; | |
125 | } | |
126 | } | |
127 | EOF | |
128 | fi | |
129 | fi | |
130 | cat >>e${EMULATION_NAME}.c <<EOF | |
131 | } | |
132 | ||
133 | /* Despite the name, we use this routine to search for dynamic | |
134 | libraries. On SunOS this requires a directory search. We need to | |
135 | find the .so file with the highest version number. The user may | |
136 | restrict the major version by saying, e.g., -lc.1. Also, if we | |
137 | find a .so file, we need to look for a the same file after | |
138 | replacing .so with .sa; if it exists, it will be an archive which | |
139 | provide some initializations for data symbols, and we need to | |
140 | search it after including the .so file. */ | |
141 | ||
142 | static void | |
143 | gld${EMULATION_NAME}_create_output_section_statements () | |
144 | { | |
145 | lang_for_each_input_file (gld${EMULATION_NAME}_find_so); | |
146 | } | |
147 | ||
148 | /* Search the directory for a .so file for each library search. */ | |
149 | ||
150 | static void | |
151 | gld${EMULATION_NAME}_find_so (inp) | |
152 | lang_input_statement_type *inp; | |
153 | { | |
154 | search_dirs_type *search; | |
155 | char *found = NULL; | |
156 | char *alc; | |
157 | struct stat st; | |
158 | ||
159 | if (! inp->search_dirs_flag | |
160 | || ! inp->is_archive | |
161 | || ! inp->dynamic) | |
162 | return; | |
163 | ||
164 | ASSERT (strncmp (inp->local_sym_name, "-l", 2) == 0); | |
165 | ||
166 | for (search = search_head; search != NULL; search = search->next) | |
167 | { | |
168 | boolean found_static; | |
169 | ||
170 | found = gld${EMULATION_NAME}_search_dir (search->name, inp->filename, | |
171 | &found_static); | |
172 | if (found != NULL || found_static) | |
173 | break; | |
174 | } | |
175 | ||
176 | if (found == NULL) | |
177 | { | |
178 | /* We did not find a matching .so file. This isn't an error, | |
179 | since there might still be a matching .a file, which will be | |
180 | found by the usual search. */ | |
181 | return; | |
182 | } | |
183 | ||
184 | /* Replace the filename with the one we have found. */ | |
185 | alc = (char *) xmalloc (strlen (search->name) + strlen (found) + 2); | |
186 | sprintf (alc, "%s/%s", search->name, found); | |
187 | inp->filename = alc; | |
188 | ||
189 | /* Turn off the search_dirs_flag to prevent ldfile_open_file from | |
190 | searching for this file again. */ | |
191 | inp->search_dirs_flag = false; | |
192 | ||
193 | free (found); | |
194 | ||
195 | /* Now look for the same file name, but with .sa instead of .so. If | |
196 | found, add it to the list of input files. */ | |
197 | alc = (char *) xmalloc (strlen (inp->filename) + 1); | |
198 | strcpy (alc, inp->filename); | |
199 | strstr (alc + strlen (search->name), ".so")[2] = 'a'; | |
200 | if (stat (alc, &st) != 0) | |
201 | free (alc); | |
202 | else | |
203 | { | |
204 | lang_input_statement_type *sa; | |
205 | ||
206 | /* Add the .sa file to the statement list just before the .so | |
207 | file. This is really a hack. */ | |
208 | sa = ((lang_input_statement_type *) | |
209 | xmalloc (sizeof (lang_input_statement_type))); | |
210 | *sa = *inp; | |
211 | ||
212 | inp->filename = alc; | |
213 | inp->local_sym_name = alc; | |
214 | ||
215 | inp->header.next = (lang_statement_union_type *) sa; | |
216 | inp->next_real_file = (lang_statement_union_type *) sa; | |
217 | } | |
218 | } | |
219 | ||
220 | /* Search a directory for a .so file. */ | |
221 | ||
222 | static char * | |
223 | gld${EMULATION_NAME}_search_dir (dirname, filename, found_static) | |
224 | const char *dirname; | |
225 | const char *filename; | |
226 | boolean *found_static; | |
227 | { | |
228 | int force_maj, force_min; | |
229 | const char *dot; | |
230 | unsigned int len; | |
231 | char *alc; | |
232 | char *found; | |
233 | int max_maj, max_min; | |
234 | DIR *dir; | |
235 | struct dirent *entry; | |
236 | unsigned int dirnamelen; | |
237 | char *full_path; | |
238 | int statval; | |
239 | struct stat st; | |
240 | ||
241 | *found_static = false; | |
242 | ||
243 | force_maj = -1; | |
244 | force_min = -1; | |
245 | dot = strchr (filename, '.'); | |
246 | if (dot == NULL) | |
247 | { | |
248 | len = strlen (filename); | |
249 | alc = NULL; | |
250 | } | |
251 | else | |
252 | { | |
253 | force_maj = atoi (dot + 1); | |
254 | ||
255 | len = dot - filename; | |
256 | alc = (char *) xmalloc (len + 1); | |
257 | strncpy (alc, filename, len); | |
258 | alc[len] = '\0'; | |
259 | filename = alc; | |
260 | ||
261 | dot = strchr (dot + 1, '.'); | |
262 | if (dot != NULL) | |
263 | force_min = atoi (dot + 1); | |
264 | } | |
265 | ||
266 | found = NULL; | |
267 | max_maj = max_min = 0; | |
268 | ||
269 | dir = opendir (dirname); | |
270 | if (dir == NULL) | |
271 | return NULL; | |
272 | dirnamelen = strlen (dirname); | |
273 | ||
274 | while ((entry = readdir (dir)) != NULL) | |
275 | { | |
276 | const char *s; | |
277 | int found_maj, found_min; | |
278 | ||
279 | if (strncmp (entry->d_name, "lib", 3) != 0 | |
280 | || strncmp (entry->d_name + 3, filename, len) != 0) | |
281 | continue; | |
282 | ||
283 | if (dot == NULL | |
284 | && strcmp (entry->d_name + 3 + len, ".a") == 0) | |
285 | { | |
286 | *found_static = true; | |
287 | continue; | |
288 | } | |
289 | ||
290 | /* We accept libfoo.so without a version number, even though the | |
291 | native linker does not. This is more convenient for packages | |
292 | which just generate .so files for shared libraries, as on ELF | |
293 | systems. */ | |
294 | if (strncmp (entry->d_name + 3 + len, ".so", 3) != 0) | |
295 | continue; | |
296 | if (entry->d_name[6 + len] == '\0') | |
297 | ; | |
298 | else if (entry->d_name[6 + len] == '.' | |
299 | && isdigit ((unsigned char) entry->d_name[7 + len])) | |
300 | ; | |
301 | else | |
302 | continue; | |
303 | ||
304 | for (s = entry->d_name + 6 + len; *s != '\0'; s++) | |
305 | if (*s != '.' && ! isdigit ((unsigned char) *s)) | |
306 | break; | |
307 | if (*s != '\0') | |
308 | continue; | |
309 | ||
310 | /* We've found a .so file. Work out the major and minor | |
311 | version numbers. */ | |
312 | found_maj = 0; | |
313 | found_min = 0; | |
314 | sscanf (entry->d_name + 3 + len, ".so.%d.%d", | |
315 | &found_maj, &found_min); | |
316 | ||
317 | if ((force_maj != -1 && force_maj != found_maj) | |
318 | || (force_min != -1 && force_min != found_min)) | |
319 | continue; | |
320 | ||
321 | /* Make sure the file really exists (ignore broken symlinks). */ | |
322 | full_path = xmalloc (dirnamelen + 1 + strlen (entry->d_name) + 1); | |
323 | sprintf (full_path, "%s/%s", dirname, entry->d_name); | |
324 | statval = stat (full_path, &st); | |
325 | free (full_path); | |
326 | if (statval != 0) | |
327 | continue; | |
328 | ||
329 | /* We've found a match for the name we are searching for. See | |
330 | if this is the version we should use. If the major and minor | |
331 | versions match, we use the last entry in alphabetical order; | |
332 | I don't know if this is how SunOS distinguishes libc.so.1.8 | |
333 | from libc.so.1.8.1, but it ought to suffice. */ | |
334 | if (found == NULL | |
335 | || (found_maj > max_maj) | |
336 | || (found_maj == max_maj | |
337 | && (found_min > max_min | |
338 | || (found_min == max_min | |
339 | && strcmp (entry->d_name, found) > 0)))) | |
340 | { | |
341 | if (found != NULL) | |
342 | free (found); | |
343 | found = (char *) xmalloc (strlen (entry->d_name) + 1); | |
344 | strcpy (found, entry->d_name); | |
345 | max_maj = found_maj; | |
346 | max_min = found_min; | |
347 | } | |
348 | } | |
349 | ||
350 | closedir (dir); | |
351 | ||
352 | if (alc != NULL) | |
353 | free (alc); | |
354 | ||
355 | return found; | |
356 | } | |
357 | ||
358 | /* These variables are required to pass information back and forth | |
359 | between after_open and check_needed. */ | |
360 | ||
361 | static struct bfd_link_needed_list *global_needed; | |
362 | static boolean global_found; | |
363 | ||
364 | /* This is called after all the input files have been opened. */ | |
365 | ||
366 | static void | |
367 | gld${EMULATION_NAME}_after_open () | |
368 | { | |
369 | struct bfd_link_needed_list *needed, *l; | |
370 | ||
371 | /* We only need to worry about this when doing a final link. */ | |
372 | if (link_info.relocateable || link_info.shared) | |
373 | return; | |
374 | ||
375 | /* Get the list of files which appear in ld_need entries in dynamic | |
376 | objects included in the link. For each such file, we want to | |
377 | track down the corresponding library, and include the symbol | |
378 | table in the link. This is what the runtime dynamic linker will | |
379 | do. Tracking the files down here permits one dynamic object to | |
380 | include another without requiring special action by the person | |
381 | doing the link. Note that the needed list can actually grow | |
382 | while we are stepping through this loop. */ | |
383 | needed = bfd_sunos_get_needed_list (output_bfd, &link_info); | |
384 | for (l = needed; l != NULL; l = l->next) | |
385 | { | |
386 | struct bfd_link_needed_list *ll; | |
387 | const char *lname; | |
388 | search_dirs_type *search; | |
389 | ||
390 | lname = l->name; | |
391 | ||
392 | /* If we've already seen this file, skip it. */ | |
393 | for (ll = needed; ll != l; ll = ll->next) | |
394 | if (strcmp (ll->name, lname) == 0) | |
395 | break; | |
396 | if (ll != l) | |
397 | continue; | |
398 | ||
399 | /* See if this file was included in the link explicitly. */ | |
400 | global_needed = l; | |
401 | global_found = false; | |
402 | lang_for_each_input_file (gld${EMULATION_NAME}_check_needed); | |
403 | if (global_found) | |
404 | continue; | |
405 | ||
406 | if (strncmp (lname, "-l", 2) != 0) | |
407 | { | |
408 | bfd *abfd; | |
409 | ||
410 | abfd = bfd_openr (lname, bfd_get_target (output_bfd)); | |
411 | if (abfd != NULL) | |
412 | { | |
413 | if (! bfd_check_format (abfd, bfd_object)) | |
414 | { | |
415 | (void) bfd_close (abfd); | |
416 | abfd = NULL; | |
417 | } | |
418 | } | |
419 | if (abfd != NULL) | |
420 | { | |
421 | if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0) | |
422 | { | |
423 | (void) bfd_close (abfd); | |
424 | abfd = NULL; | |
425 | } | |
426 | } | |
427 | if (abfd != NULL) | |
428 | { | |
429 | /* We've found the needed dynamic object. */ | |
430 | if (! bfd_link_add_symbols (abfd, &link_info)) | |
431 | einfo ("%F%B: could not read symbols: %E\n", abfd); | |
432 | } | |
433 | else | |
434 | { | |
435 | einfo ("%P: warning: %s, needed by %B, not found\n", | |
436 | lname, l->by); | |
437 | } | |
438 | ||
439 | continue; | |
440 | } | |
441 | ||
442 | lname += 2; | |
443 | ||
444 | /* We want to search for the file in the same way that the | |
445 | dynamic linker will search. That means that we want to use | |
446 | rpath_link, rpath or -L, then the environment variable | |
447 | LD_LIBRARY_PATH (native only), then (if rpath was used) the | |
448 | linker script LIB_SEARCH_DIRS. */ | |
449 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link, | |
450 | lname)) | |
451 | continue; | |
452 | if (command_line.rpath != NULL) | |
453 | { | |
454 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath, lname)) | |
455 | continue; | |
456 | } | |
457 | else | |
458 | { | |
459 | for (search = search_head; search != NULL; search = search->next) | |
460 | if (gld${EMULATION_NAME}_try_needed (search->name, lname)) | |
461 | break; | |
462 | if (search != NULL) | |
463 | continue; | |
464 | } | |
465 | EOF | |
466 | if [ "x${host}" = "x${target}" ] ; then | |
467 | if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then | |
468 | cat >>e${EMULATION_NAME}.c <<EOF | |
469 | { | |
470 | const char *lib_path; | |
471 | ||
472 | lib_path = (const char *) getenv ("LD_LIBRARY_PATH"); | |
473 | if (gld${EMULATION_NAME}_search_needed (lib_path, lname)) | |
474 | continue; | |
475 | } | |
476 | EOF | |
477 | fi | |
478 | fi | |
479 | cat >>e${EMULATION_NAME}.c <<EOF | |
480 | if (command_line.rpath != NULL) | |
481 | { | |
482 | for (search = search_head; search != NULL; search = search->next) | |
483 | { | |
484 | if (search->cmdline) | |
485 | continue; | |
486 | if (gld${EMULATION_NAME}_try_needed (search->name, lname)) | |
487 | break; | |
488 | } | |
489 | if (search != NULL) | |
490 | continue; | |
491 | } | |
492 | ||
493 | einfo ("%P: warning: %s, needed by %B, not found\n", | |
494 | l->name, l->by); | |
495 | } | |
496 | } | |
497 | ||
498 | /* Search for a needed file in a path. */ | |
499 | ||
500 | static boolean | |
501 | gld${EMULATION_NAME}_search_needed (path, name) | |
502 | const char *path; | |
503 | const char *name; | |
504 | { | |
505 | const char *s; | |
506 | ||
507 | if (path == NULL || *path == '\0') | |
508 | return false; | |
509 | while (1) | |
510 | { | |
511 | const char *dir; | |
512 | char *dircopy; | |
513 | ||
514 | s = strchr (path, ':'); | |
515 | if (s == NULL) | |
516 | { | |
517 | dircopy = NULL; | |
518 | dir = path; | |
519 | } | |
520 | else | |
521 | { | |
522 | dircopy = (char *) xmalloc (s - path + 1); | |
523 | memcpy (dircopy, path, s - path); | |
524 | dircopy[s - path] = '\0'; | |
525 | dir = dircopy; | |
526 | } | |
527 | ||
528 | if (gld${EMULATION_NAME}_try_needed (dir, name)) | |
529 | return true; | |
530 | ||
531 | if (dircopy != NULL) | |
532 | free (dircopy); | |
533 | ||
534 | if (s == NULL) | |
535 | break; | |
536 | path = s + 1; | |
537 | } | |
538 | ||
539 | return false; | |
540 | } | |
541 | ||
542 | /* This function is called for each possible directory for a needed | |
543 | dynamic object. */ | |
544 | ||
545 | static boolean | |
546 | gld${EMULATION_NAME}_try_needed (dir, name) | |
547 | const char *dir; | |
548 | const char *name; | |
549 | { | |
550 | char *file; | |
551 | char *alc; | |
552 | boolean ignore; | |
553 | bfd *abfd; | |
554 | ||
555 | file = gld${EMULATION_NAME}_search_dir (dir, name, &ignore); | |
556 | if (file == NULL) | |
557 | return false; | |
558 | ||
559 | alc = (char *) xmalloc (strlen (dir) + strlen (file) + 2); | |
560 | sprintf (alc, "%s/%s", dir, file); | |
561 | free (file); | |
562 | abfd = bfd_openr (alc, bfd_get_target (output_bfd)); | |
563 | if (abfd == NULL) | |
564 | return false; | |
565 | if (! bfd_check_format (abfd, bfd_object)) | |
566 | { | |
567 | (void) bfd_close (abfd); | |
568 | return false; | |
569 | } | |
570 | if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0) | |
571 | { | |
572 | (void) bfd_close (abfd); | |
573 | return false; | |
574 | } | |
575 | ||
576 | /* We've found the needed dynamic object. */ | |
577 | ||
578 | /* Add this file into the symbol table. */ | |
579 | if (! bfd_link_add_symbols (abfd, &link_info)) | |
580 | einfo ("%F%B: could not read symbols: %E\n", abfd); | |
581 | ||
582 | return true; | |
583 | } | |
584 | ||
585 | /* See if we have already included a needed object in the link. This | |
586 | does not have to be precise, as it does no harm to include a | |
587 | dynamic object more than once. */ | |
588 | ||
589 | static void | |
590 | gld${EMULATION_NAME}_check_needed (s) | |
591 | lang_input_statement_type *s; | |
592 | { | |
593 | if (s->filename == NULL) | |
594 | return; | |
595 | if (strncmp (global_needed->name, "-l", 2) != 0) | |
596 | { | |
597 | if (strcmp (s->filename, global_needed->name) == 0) | |
598 | global_found = true; | |
599 | } | |
600 | else | |
601 | { | |
602 | const char *sname, *lname; | |
603 | const char *sdot, *ldot; | |
604 | int lmaj, lmin, smaj, smin; | |
605 | ||
606 | lname = global_needed->name + 2; | |
607 | ||
608 | sname = strrchr (s->filename, '/'); | |
609 | if (sname == NULL) | |
610 | sname = s->filename; | |
611 | else | |
612 | ++sname; | |
613 | ||
614 | if (strncmp (sname, "lib", 3) != 0) | |
615 | return; | |
616 | sname += 3; | |
617 | ||
618 | ldot = strchr (lname, '.'); | |
619 | if (ldot == NULL) | |
620 | ldot = lname + strlen (lname); | |
621 | ||
622 | sdot = strstr (sname, ".so."); | |
623 | if (sdot == NULL) | |
624 | return; | |
625 | ||
626 | if (sdot - sname != ldot - lname | |
627 | || strncmp (lname, sname, sdot - sname) != 0) | |
628 | return; | |
629 | ||
630 | lmaj = lmin = -1; | |
631 | sscanf (ldot, ".%d.%d", &lmaj, &lmin); | |
632 | smaj = smin = -1; | |
633 | sscanf (sdot, ".so.%d.%d", &smaj, &smin); | |
634 | if ((smaj != lmaj && smaj != -1 && lmaj != -1) | |
635 | || (smin != lmin && smin != -1 && lmin != -1)) | |
636 | return; | |
637 | ||
638 | global_found = true; | |
639 | } | |
640 | } | |
641 | ||
642 | /* We need to use static variables to pass information around the call | |
643 | to lang_for_each_statement. Ick. */ | |
644 | ||
645 | static const char *find_assign; | |
646 | static boolean found_assign; | |
647 | ||
648 | /* We need to use static variables to pass information around the call | |
649 | to lang_for_each_input_file. Ick. */ | |
650 | ||
651 | static bfd_size_type need_size; | |
652 | static bfd_size_type need_entries; | |
653 | static bfd_byte *need_contents; | |
654 | static bfd_byte *need_pinfo; | |
655 | static bfd_byte *need_pnames; | |
656 | ||
657 | /* The size of one entry in the .need section, not including the file | |
658 | name. */ | |
659 | ||
660 | #define NEED_ENTRY_SIZE (16) | |
661 | ||
662 | /* This is called after the sections have been attached to output | |
663 | sections, but before any sizes or addresses have been set. */ | |
664 | ||
665 | static void | |
666 | gld${EMULATION_NAME}_before_allocation () | |
667 | { | |
668 | struct bfd_link_hash_entry *hdyn = NULL; | |
669 | asection *sneed; | |
670 | asection *srules; | |
671 | asection *sdyn; | |
672 | ||
673 | /* The SunOS native linker creates a shared library whenever there | |
674 | are any undefined symbols in a link, unless -e is used. This is | |
675 | pretty weird, but we are compatible. */ | |
676 | if (! link_info.shared && ! link_info.relocateable && ! entry_from_cmdline) | |
677 | { | |
678 | struct bfd_link_hash_entry *h; | |
679 | ||
680 | for (h = link_info.hash->undefs; h != NULL; h = h->next) | |
681 | { | |
682 | if (h->type == bfd_link_hash_undefined | |
683 | && h->u.undef.abfd != NULL | |
684 | && (h->u.undef.abfd->flags & DYNAMIC) == 0 | |
685 | && strcmp (h->root.string, "__DYNAMIC") != 0 | |
686 | && strcmp (h->root.string, "__GLOBAL_OFFSET_TABLE_") != 0) | |
687 | { | |
688 | find_assign = h->root.string; | |
689 | found_assign = false; | |
690 | lang_for_each_statement (gld${EMULATION_NAME}_find_assignment); | |
691 | if (! found_assign) | |
692 | { | |
693 | link_info.shared = true; | |
694 | break; | |
695 | } | |
696 | } | |
697 | } | |
698 | } | |
699 | ||
700 | if (link_info.shared) | |
701 | { | |
702 | lang_output_section_statement_type *os; | |
703 | ||
704 | /* Set the .text section to start at 0x20, not 0x2020. FIXME: | |
705 | This is too magical. */ | |
706 | os = lang_output_section_statement_lookup (".text"); | |
707 | if (os->addr_tree == NULL) | |
708 | os->addr_tree = exp_intop (0x20); | |
709 | } | |
710 | ||
711 | /* We need to create a __DYNAMIC symbol. We don't do this in the | |
712 | linker script because we want to set the value to the start of | |
713 | the dynamic section if there is one, or to zero if there isn't | |
714 | one. We need to create the symbol before calling | |
715 | size_dynamic_sections, although we can't set the value until | |
716 | afterward. */ | |
717 | if (! link_info.relocateable) | |
718 | { | |
719 | hdyn = bfd_link_hash_lookup (link_info.hash, "__DYNAMIC", true, false, | |
720 | false); | |
721 | if (hdyn == NULL) | |
722 | einfo ("%P%F: bfd_link_hash_lookup: %E\n"); | |
723 | if (! bfd_sunos_record_link_assignment (output_bfd, &link_info, | |
724 | "__DYNAMIC")) | |
725 | einfo ("%P%F: failed to record assignment to __DYNAMIC: %E\n"); | |
726 | } | |
727 | ||
728 | /* If we are going to make any variable assignments, we need to let | |
729 | the backend linker know about them in case the variables are | |
730 | referred to by dynamic objects. */ | |
731 | lang_for_each_statement (gld${EMULATION_NAME}_find_assignment); | |
732 | ||
733 | /* Let the backend linker work out the sizes of any sections | |
734 | required by dynamic linking. */ | |
735 | if (! bfd_sunos_size_dynamic_sections (output_bfd, &link_info, &sdyn, | |
736 | &sneed, &srules)) | |
737 | einfo ("%P%F: failed to set dynamic section sizes: %E\n"); | |
738 | ||
739 | if (sneed != NULL) | |
740 | { | |
741 | /* Set up the .need section. See the description of the ld_need | |
742 | field in include/aout/sun4.h. */ | |
743 | ||
744 | need_entries = 0; | |
745 | need_size = 0; | |
746 | ||
747 | lang_for_each_input_file (gld${EMULATION_NAME}_count_need); | |
748 | ||
749 | /* We should only have a .need section if we have at least one | |
750 | dynamic object. */ | |
751 | ASSERT (need_entries != 0); | |
752 | ||
753 | sneed->_raw_size = need_size; | |
754 | sneed->contents = (bfd_byte *) xmalloc (need_size); | |
755 | ||
756 | need_contents = sneed->contents; | |
757 | need_pinfo = sneed->contents; | |
758 | need_pnames = sneed->contents + need_entries * 16; | |
759 | ||
760 | lang_for_each_input_file (gld${EMULATION_NAME}_set_need); | |
761 | ||
762 | ASSERT ((bfd_size_type) (need_pnames - sneed->contents) == need_size); | |
763 | } | |
764 | ||
765 | if (srules != NULL) | |
766 | { | |
767 | /* Set up the .rules section. This is just a PATH like string | |
768 | of the -L arguments given on the command line. We permit the | |
769 | user to specify the directories using the -rpath command line | |
770 | option. */ | |
771 | if (command_line.rpath) | |
772 | { | |
773 | srules->_raw_size = strlen (command_line.rpath); | |
774 | srules->contents = (bfd_byte *) command_line.rpath; | |
775 | } | |
776 | else | |
777 | { | |
778 | unsigned int size; | |
779 | search_dirs_type *search; | |
780 | ||
781 | size = 0; | |
782 | for (search = search_head; search != NULL; search = search->next) | |
783 | if (search->cmdline) | |
784 | size += strlen (search->name) + 1; | |
785 | srules->_raw_size = size; | |
786 | if (size > 0) | |
787 | { | |
788 | char *p; | |
789 | ||
790 | srules->contents = (bfd_byte *) xmalloc (size); | |
791 | p = (char *) srules->contents; | |
792 | *p = '\0'; | |
793 | for (search = search_head; search != NULL; search = search->next) | |
794 | { | |
795 | if (search->cmdline) | |
796 | { | |
797 | if (p != (char *) srules->contents) | |
798 | *p++ = ':'; | |
799 | strcpy (p, search->name); | |
800 | p += strlen (p); | |
801 | } | |
802 | } | |
803 | } | |
804 | } | |
805 | } | |
806 | ||
807 | /* We must assign a value to __DYNAMIC. It should be zero if we are | |
808 | not doing a dynamic link, or the start of the .dynamic section if | |
809 | we are doing one. */ | |
810 | if (! link_info.relocateable) | |
811 | { | |
812 | hdyn->type = bfd_link_hash_defined; | |
813 | hdyn->u.def.value = 0; | |
814 | if (sdyn != NULL) | |
815 | hdyn->u.def.section = sdyn; | |
816 | else | |
817 | hdyn->u.def.section = bfd_abs_section_ptr; | |
818 | } | |
819 | } | |
820 | ||
821 | /* This is called by the before_allocation routine via | |
822 | lang_for_each_statement. It does one of two things: if the | |
823 | variable find_assign is set, it sets found_assign if it finds an | |
824 | assignment to that variable; otherwise it tells the backend linker | |
825 | about all assignment statements, in case they are assignments to | |
826 | symbols which are referred to by dynamic objects. */ | |
827 | ||
828 | static void | |
829 | gld${EMULATION_NAME}_find_assignment (s) | |
830 | lang_statement_union_type *s; | |
831 | { | |
832 | if (s->header.type == lang_assignment_statement_enum | |
833 | && (find_assign == NULL || ! found_assign)) | |
834 | gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp); | |
835 | } | |
836 | ||
837 | /* Look through an expression for an assignment statement. */ | |
838 | ||
839 | static void | |
840 | gld${EMULATION_NAME}_find_exp_assignment (exp) | |
841 | etree_type *exp; | |
842 | { | |
843 | switch (exp->type.node_class) | |
844 | { | |
845 | case etree_assign: | |
846 | if (find_assign != NULL) | |
847 | { | |
848 | if (strcmp (find_assign, exp->assign.dst) == 0) | |
849 | found_assign = true; | |
850 | return; | |
851 | } | |
852 | ||
853 | if (strcmp (exp->assign.dst, ".") != 0) | |
854 | { | |
855 | if (! bfd_sunos_record_link_assignment (output_bfd, &link_info, | |
856 | exp->assign.dst)) | |
857 | einfo ("%P%F: failed to record assignment to %s: %E\n", | |
858 | exp->assign.dst); | |
859 | } | |
860 | gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src); | |
861 | break; | |
862 | ||
863 | case etree_binary: | |
864 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs); | |
865 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs); | |
866 | break; | |
867 | ||
868 | case etree_trinary: | |
869 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond); | |
870 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs); | |
871 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs); | |
872 | break; | |
873 | ||
874 | case etree_unary: | |
875 | gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child); | |
876 | break; | |
877 | ||
878 | default: | |
879 | break; | |
880 | } | |
881 | } | |
882 | ||
883 | /* Work out the size of the .need section, and the number of entries. | |
884 | The backend will set the ld_need field of the dynamic linking | |
885 | information to point to the .need section. See include/aout/sun4.h | |
886 | for more information. */ | |
887 | ||
888 | static void | |
889 | gld${EMULATION_NAME}_count_need (inp) | |
890 | lang_input_statement_type *inp; | |
891 | { | |
892 | if (inp->the_bfd != NULL | |
893 | && (inp->the_bfd->flags & DYNAMIC) != 0) | |
894 | { | |
895 | ++need_entries; | |
896 | need_size += NEED_ENTRY_SIZE; | |
897 | if (! inp->is_archive) | |
898 | need_size += strlen (inp->filename) + 1; | |
899 | else | |
900 | { | |
901 | ASSERT (inp->local_sym_name[0] == '-' | |
902 | && inp->local_sym_name[1] == 'l'); | |
903 | need_size += strlen (inp->local_sym_name + 2) + 1; | |
904 | } | |
905 | } | |
906 | } | |
907 | ||
908 | /* Fill in the contents of the .need section. */ | |
909 | ||
910 | static void | |
911 | gld${EMULATION_NAME}_set_need (inp) | |
912 | lang_input_statement_type *inp; | |
913 | { | |
914 | if (inp->the_bfd != NULL | |
915 | && (inp->the_bfd->flags & DYNAMIC) != 0) | |
916 | { | |
917 | bfd_size_type c; | |
918 | ||
919 | /* To really fill in the .need section contents, we need to know | |
920 | the final file position of the section, but we don't. | |
921 | Instead, we use offsets, and rely on the BFD backend to | |
922 | finish the section up correctly. FIXME: Talk about lack of | |
923 | referential locality. */ | |
924 | bfd_put_32 (output_bfd, need_pnames - need_contents, need_pinfo); | |
925 | if (! inp->is_archive) | |
926 | { | |
927 | bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 4); | |
928 | bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 8); | |
929 | bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 10); | |
930 | strcpy (need_pnames, inp->filename); | |
931 | } | |
932 | else | |
933 | { | |
934 | char *verstr; | |
935 | int maj, min; | |
936 | ||
937 | bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, need_pinfo + 4); | |
938 | maj = 0; | |
939 | min = 0; | |
940 | verstr = strstr (inp->filename, ".so."); | |
941 | if (verstr != NULL) | |
942 | sscanf (verstr, ".so.%d.%d", &maj, &min); | |
943 | bfd_put_16 (output_bfd, (bfd_vma) maj, need_pinfo + 8); | |
944 | bfd_put_16 (output_bfd, (bfd_vma) min, need_pinfo + 10); | |
945 | strcpy (need_pnames, inp->local_sym_name + 2); | |
946 | } | |
947 | ||
948 | c = (need_pinfo - need_contents) / NEED_ENTRY_SIZE; | |
949 | if (c + 1 >= need_entries) | |
950 | bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 12); | |
951 | else | |
952 | bfd_put_32 (output_bfd, (bfd_vma) (c + 1) * NEED_ENTRY_SIZE, | |
953 | need_pinfo + 12); | |
954 | ||
955 | need_pinfo += NEED_ENTRY_SIZE; | |
956 | need_pnames += strlen (need_pnames) + 1; | |
957 | } | |
958 | } | |
959 | ||
960 | static char * | |
961 | gld${EMULATION_NAME}_get_script(isfile) | |
962 | int *isfile; | |
963 | EOF | |
964 | ||
965 | if test -n "$COMPILE_IN" | |
966 | then | |
967 | # Scripts compiled in. | |
968 | ||
969 | # sed commands to quote an ld script as a C string. | |
970 | sc='s/["\\]/\\&/g | |
971 | s/$/\\n\\/ | |
972 | 1s/^/"/ | |
973 | $s/$/n"/ | |
974 | ' | |
975 | ||
976 | cat >>e${EMULATION_NAME}.c <<EOF | |
977 | { | |
978 | *isfile = 0; | |
979 | ||
980 | if (link_info.relocateable == true && config.build_constructors == true) | |
981 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`; | |
982 | else if (link_info.relocateable == true) | |
983 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`; | |
984 | else if (!config.text_read_only) | |
985 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`; | |
986 | else if (!config.magic_demand_paged) | |
987 | return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`; | |
988 | else | |
989 | return `sed "$sc" ldscripts/${EMULATION_NAME}.x`; | |
990 | } | |
991 | EOF | |
992 | ||
993 | else | |
994 | # Scripts read from the filesystem. | |
995 | ||
996 | cat >>e${EMULATION_NAME}.c <<EOF | |
997 | { | |
998 | *isfile = 1; | |
999 | ||
1000 | if (link_info.relocateable == true && config.build_constructors == true) | |
1001 | return "ldscripts/${EMULATION_NAME}.xu"; | |
1002 | else if (link_info.relocateable == true) | |
1003 | return "ldscripts/${EMULATION_NAME}.xr"; | |
1004 | else if (!config.text_read_only) | |
1005 | return "ldscripts/${EMULATION_NAME}.xbn"; | |
1006 | else if (!config.magic_demand_paged) | |
1007 | return "ldscripts/${EMULATION_NAME}.xn"; | |
1008 | else | |
1009 | return "ldscripts/${EMULATION_NAME}.x"; | |
1010 | } | |
1011 | EOF | |
1012 | ||
1013 | fi | |
1014 | ||
1015 | cat >>e${EMULATION_NAME}.c <<EOF | |
1016 | ||
1017 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = | |
1018 | { | |
1019 | gld${EMULATION_NAME}_before_parse, | |
1020 | syslib_default, | |
1021 | hll_default, | |
1022 | after_parse_default, | |
1023 | gld${EMULATION_NAME}_after_open, | |
1024 | after_allocation_default, | |
1025 | set_output_arch_default, | |
1026 | ldemul_default_target, | |
1027 | gld${EMULATION_NAME}_before_allocation, | |
1028 | gld${EMULATION_NAME}_get_script, | |
1029 | "${EMULATION_NAME}", | |
1030 | "${OUTPUT_FORMAT}", | |
1031 | NULL, /* finish */ | |
1032 | gld${EMULATION_NAME}_create_output_section_statements, | |
1033 | NULL, /* open_dynamic_library */ | |
1034 | NULL, /* place_orphan */ | |
1035 | gld${EMULATION_NAME}_set_symbols | |
1036 | }; | |
1037 | EOF |