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