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