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