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