2 * This code is derived from software copyrighted by the Free Software
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
7 * Modified 1993 by Paul Kranenburg, Erasmus University
10 /* Derived from ld.c: "@(#)ld.c 6.10 (Berkeley) 5/22/91"; */
12 /* Linker `ld' for GNU
13 Copyright (C) 1988 Free Software Foundation, Inc.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 1, or (at your option)
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* Written by Richard Stallman with some help from Eric Albert.
30 Set, indirect, and warning symbol features added by Randy Smith. */
33 * $FreeBSD: src/gnu/usr.bin/ld/lib.c,v 1.23 1999/08/27 23:36:01 peter Exp $ - library routines
36 #include <sys/param.h>
40 #include <sys/types.h>
57 static void linear_library __P((int, struct file_entry *));
58 static void symdef_library __P((int, struct file_entry *, int));
59 static struct file_entry *decode_library_subfile __P((int,
64 * Search the library ENTRY, already open on descriptor FD. This means
65 * deciding which library members to load, making a chain of `struct
66 * file_entry' for those members, and entering their global symbols in the
71 search_library(fd, entry)
73 struct file_entry *entry;
77 register struct file_entry *subentry;
79 if (!(link_mode & FORCEARCHIVE) && !undefined_global_sym_count)
82 /* Examine its first member, which starts SARMAG bytes in. */
83 subentry = decode_library_subfile(fd, entry, SARMAG, &member_length);
87 name = subentry->filename;
90 /* Search via __.SYMDEF if that exists, else linearly. */
92 if (!strcmp(name, "__.SYMDEF"))
93 symdef_library(fd, entry, member_length);
95 linear_library(fd, entry);
99 * Construct and return a file_entry for a library member. The library's
100 * file_entry is library_entry, and the library is open on FD.
101 * SUBFILE_OFFSET is the byte index in the library of this member's header.
102 * We store the length of the member into *LENGTH_LOC.
105 static struct file_entry *
106 decode_library_subfile(fd, library_entry, subfile_offset, length_loc)
108 struct file_entry *library_entry;
113 register int namelen;
114 int member_length, content_length;
118 register struct file_entry *subentry;
120 lseek(fd, subfile_offset, 0);
122 bytes_read = read(fd, &hdr1, sizeof hdr1);
124 return 0; /* end of archive */
126 if (sizeof hdr1 != bytes_read)
127 errx(1, "%s: malformed library archive",
128 get_file_name(library_entry));
130 if (sscanf(hdr1.ar_size, "%d", &member_length) != 1)
131 errx(1, "%s: malformatted header of archive member: %.*s",
132 get_file_name(library_entry),
133 (int)sizeof(hdr1.ar_name), hdr1.ar_name);
135 subentry = (struct file_entry *) xmalloc(sizeof(struct file_entry));
136 bzero(subentry, sizeof(struct file_entry));
139 namelen < sizeof hdr1.ar_name
140 && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
141 && hdr1.ar_name[namelen] != '/';
144 starting_offset = subfile_offset + sizeof hdr1;
145 content_length = member_length;
149 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
150 * first <namelen> bytes of the file
152 if (strncmp(hdr1.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
153 isdigit(hdr1.ar_name[sizeof(AR_EFMT1) - 1])) {
155 namelen = atoi(&hdr1.ar_name[sizeof(AR_EFMT1) - 1]);
156 name = (char *)xmalloc(namelen + 1);
157 if (read(fd, name, namelen) != namelen)
158 errx(1, "%s: malformatted archive member: %.*s",
159 get_file_name(library_entry),
160 (int)sizeof(hdr1.ar_name), hdr1.ar_name);
162 content_length -= namelen;
163 starting_offset += namelen;
168 name = (char *)xmalloc(namelen + 1);
169 strncpy(name, hdr1.ar_name, namelen);
173 subentry->filename = name;
174 subentry->local_sym_name = name;
175 subentry->starting_offset = starting_offset;
176 subentry->superfile = library_entry;
177 subentry->total_size = content_length;
179 subentry->symbols = 0;
180 subentry->strings = 0;
181 subentry->subfiles = 0;
186 (*length_loc) = member_length;
191 static int subfile_wanted_p __P((struct file_entry *));
194 * Search a library that has a __.SYMDEF member. FD is a descriptor on
195 * which the library is open. The file pointer is assumed to point at the
196 * __.SYMDEF data. ENTRY is the library's file_entry. MEMBER_LENGTH is the
197 * length of the __.SYMDEF data.
201 symdef_library(fd, entry, member_length)
203 struct file_entry *entry;
206 int *symdef_data = (int *) xmalloc(member_length);
207 register struct ranlib *symdef_base;
210 int length_of_strings;
214 struct file_entry *prev = 0;
217 bytes_read = read(fd, symdef_data, member_length);
218 if (bytes_read != member_length)
219 errx(1, "%s: malformatted __.SYMDEF",
220 get_file_name(entry));
222 nsymdefs = md_swap_long(*symdef_data) / sizeof(struct ranlib);
224 nsymdefs * sizeof(struct ranlib) + 2 * sizeof(int) > member_length)
225 errx(1, "%s: malformatted __.SYMDEF",
226 get_file_name(entry));
228 symdef_base = (struct ranlib *) (symdef_data + 1);
229 length_of_strings = md_swap_long(*(int *) (symdef_base + nsymdefs));
231 if (length_of_strings < 0
232 || nsymdefs * sizeof(struct ranlib) + length_of_strings
233 + 2 * sizeof(int) > member_length)
234 errx(1, "%s: malformatted __.SYMDEF",
235 get_file_name(entry));
237 sym_name_base = sizeof(int) + (char *) (symdef_base + nsymdefs);
239 /* Check all the string indexes for validity. */
240 md_swapin_ranlib_hdr(symdef_base, nsymdefs);
241 for (i = 0; i < nsymdefs; i++) {
242 register int index = symdef_base[i].ran_un.ran_strx;
243 if (index < 0 || index >= length_of_strings
244 || (index && *(sym_name_base + index - 1)))
245 errx(1, "%s: malformatted __.SYMDEF",
246 get_file_name(entry));
250 * Search the symdef data for members to load. Do this until one
251 * whole pass finds nothing to load.
255 while (not_finished) {
260 * Scan all the symbols mentioned in the symdef for ones that
261 * we need. Load the library members that contain such
265 for (i = 0; (i < nsymdefs &&
266 ((link_mode & FORCEARCHIVE) ||
267 undefined_global_sym_count ||
268 common_defined_global_count)); i++) {
273 register int offset = symdef_base[i].ran_off;
274 struct file_entry *subentry;
277 if (symdef_base[i].ran_un.ran_strx < 0)
280 sp = getsym_soft(sym_name_base
281 + symdef_base[i].ran_un.ran_strx);
284 * If we find a symbol that appears to be needed,
285 * think carefully about the archive member that the
290 * Per Mike Karels' recommendation, we no longer load
291 * library files if the only reference(s) that would
292 * be satisfied are 'common' references. This
293 * prevents some problems with name pollution (e.g. a
294 * global common 'utime' linked to a function).
296 * If we're not forcing the archive in then we don't
297 * need to bother if: we've never heard of the symbol,
298 * or if it is already defined. The last clause causes
299 * archive members to be searched for definitions
300 * satisfying undefined shared object symbols.
302 if (!(link_mode & FORCEARCHIVE) &&
303 (!sp || sp->defined ||
304 (!(sp->flags & GS_REFERENCED) &&
309 * Don't think carefully about any archive member
310 * more than once in a given pass.
313 if (prev_offset == offset)
315 prev_offset = offset;
318 * Read the symbol table of the archive member.
321 subentry = decode_library_subfile(fd,
322 entry, offset, &junk);
325 "invalid offset for %s in symbol table of %s",
327 + symdef_base[i].ran_un.ran_strx,
330 read_entry_symbols(fd, subentry);
331 subentry->strings = (char *)
332 alloca(subentry->string_size);
333 read_entry_strings(fd, subentry);
336 * Now scan the symbol table and decide whether to
340 if (!(link_mode & FORCEARCHIVE) &&
341 !subfile_wanted_p(subentry)) {
342 if (subentry->symbols)
343 free(subentry->symbols);
347 * This member is needed; load it. Since we
348 * are loading something on this pass, we
349 * must make another pass through the symdef
355 read_entry_relocation(fd, subentry);
356 enter_file_symbols(subentry);
359 prev->chain = subentry;
361 entry->subfiles = subentry;
365 * Clear out this member's symbols from the
366 * symdef data so that following passes won't
367 * waste time on them.
370 for (j = 0; j < nsymdefs; j++) {
371 if (symdef_base[j].ran_off == offset)
372 symdef_base[j].ran_un.ran_strx = -1;
376 * We'll read the strings again
379 subentry->strings = 0;
388 * Search a library that has no __.SYMDEF. ENTRY is the library's file_entry.
389 * FD is the descriptor it is open on.
393 linear_library(fd, entry)
395 struct file_entry *entry;
397 register struct file_entry *prev = 0;
398 register int this_subfile_offset = SARMAG;
400 while ((link_mode & FORCEARCHIVE) ||
401 undefined_global_sym_count || common_defined_global_count) {
404 register struct file_entry *subentry;
406 subentry = decode_library_subfile(fd, entry,
407 this_subfile_offset, &member_length);
412 read_entry_symbols(fd, subentry);
413 subentry->strings = (char *)alloca(subentry->string_size);
414 read_entry_strings(fd, subentry);
416 if (!(link_mode & FORCEARCHIVE) &&
417 !subfile_wanted_p(subentry)) {
418 if (subentry->symbols)
419 free(subentry->symbols);
422 read_entry_relocation(fd, subentry);
423 enter_file_symbols(subentry);
426 prev->chain = subentry;
428 entry->subfiles = subentry;
430 subentry->strings = 0; /* Since space will dissapear
434 this_subfile_offset += member_length + sizeof(struct ar_hdr);
435 if (this_subfile_offset & 1)
436 this_subfile_offset++;
441 * ENTRY is an entry for a library member. Its symbols have been read into
442 * core, but not entered. Return nonzero if we ought to load this member.
446 subfile_wanted_p(entry)
447 struct file_entry *entry;
449 struct localsymbol *lsp, *lspend;
451 register int dollar_cond = 0;
454 lspend = entry->symbols + entry->nsymbols;
456 for (lsp = entry->symbols; lsp < lspend; lsp++) {
457 register struct nlist *p = &lsp->nzlist.nlist;
458 register int type = p->n_type;
459 register char *name = p->n_un.n_strx + entry->strings;
460 register symbol *sp = getsym_soft(name);
463 * If the symbol has an interesting definition, we could
464 * potentially want it.
467 || (type == (N_UNDF | N_EXT) && p->n_value == 0
474 || SET_ELEMENT_P(type)
475 || set_element_prefixed_p(name)
482 if (name[1] == '$') {
483 sp = getsym_soft(&name[2]);
487 if (sp->flags & SP_REFERENCED) {
489 print_file_name(entry, stdout);
490 fprintf(stdout, " needed due to $-conditional %s\n", name);
499 * If this symbol has not been hashed, we can't be
507 * We don't load a file if it merely satisfies a
508 * common reference (see explanation above in
511 if ((sp->flags & GS_REFERENCED) && !sp->defined) {
513 * This is a symbol we are looking for. It
514 * is either not yet defined or defined as a
521 if (type == (N_UNDF | N_EXT)) {
523 * Symbol being defined as common.
524 * Remember this, but don't load
525 * subfile just for this.
529 * If it didn't used to be common, up
530 * the count of common symbols.
532 if (!sp->common_size)
533 common_defined_global_count++;
535 if (sp->common_size < p->n_value)
536 sp->common_size = p->n_value;
538 undefined_global_sym_count--;
542 if (sp->flags & GS_WEAK)
543 /* Weak symbols don't pull archive members */
546 print_file_name(entry, stdout);
547 fprintf(stdout, " needed due to %s\n", demangle(sp->name));
550 } else if (!sp->defined && sp->sorefs) {
552 * Check for undefined symbols or commons
555 struct localsymbol *lsp;
557 for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
558 int type = lsp->nzlist.nlist.n_type;
559 if ( (type & N_EXT) &&
560 (type & N_STAB) == 0 &&
561 type != (N_UNDF | N_EXT))
562 break; /* We don't need it */
566 * We have a worthy definition in a shared
567 * object that was specified ahead of the
568 * archive we're examining now. So, punt.
573 * At this point, we have an undefined shared
574 * object reference. Again, if the archive member
575 * defines a common we just note the its size.
576 * Otherwise, the member gets included.
579 if (type == (N_UNDF|N_EXT) && p->n_value) {
581 * New symbol is common, just takes its
582 * size, but don't load.
584 sp->common_size = p->n_value;
590 * THIS STILL MISSES the case where one shared
591 * object defines a common and the next defines
592 * more strongly; fix this someday by making
593 * `struct glosym' and enter_global_ref() more
598 print_file_name(entry, stdout);
600 " needed due to shared lib ref %s (%d)\n",
602 lsp ? lsp->nzlist.nlist.n_type : -1);
612 * Read the symbols of dynamic entity ENTRY into core. Assume it is already
613 * open, on descriptor FD.
616 read_shared_object(fd, entry)
617 struct file_entry *entry;
621 struct section_dispatch_table sdt;
624 int n, i, has_nz = 0;
626 if (!(entry->flags & E_HEADER_VALID))
627 read_header(fd, entry);
629 /* Read DYNAMIC structure (first in data segment) */
630 if (lseek(fd, text_offset(entry) + entry->header.a_text, L_SET) ==
632 err(1, "%s: lseek", get_file_name(entry));
633 if (read(fd, &dyn, sizeof dyn) != sizeof dyn) {
634 errx(1, "%s: premature EOF reading _dynamic",
635 get_file_name(entry));
637 md_swapin__dynamic(&dyn);
640 switch (dyn.d_version) {
642 errx(1, "%s: unsupported _DYNAMIC version: %d",
643 get_file_name(entry), dyn.d_version);
652 /* Read Section Dispatch Table (from data segment) */
654 text_offset(entry) + (long)dyn.d_un.d_sdt -
655 (DATA_START(entry->header) - N_DATOFF(entry->header)),
657 err(1, "%s: lseek", get_file_name(entry));
658 if (read(fd, &sdt, sizeof sdt) != sizeof sdt)
659 errx(1, "%s: premature EOF reading sdt",
660 get_file_name(entry));
661 md_swapin_section_dispatch_table(&sdt);
663 /* Read symbols (text segment) */
664 n = sdt.sdt_strings - sdt.sdt_nzlist;
665 entry->nsymbols = n /
666 (has_nz ? sizeof(struct nzlist) : sizeof(struct nlist));
667 nzp = (struct nzlist *)(np = (struct nlist *)alloca (n));
668 entry->symbols = (struct localsymbol *)
669 xmalloc(entry->nsymbols * sizeof(struct localsymbol));
672 text_offset(entry) + (long)sdt.sdt_nzlist -
673 (TEXT_START(entry->header) - N_TXTOFF(entry->header)),
675 err(1, "%s: lseek", get_file_name(entry));
676 if (read(fd, (char *)nzp, n) != n)
677 errx(1, "%s: premature EOF reading symbols ",
678 get_file_name(entry));
681 md_swapin_zsymbols(nzp, entry->nsymbols);
683 md_swapin_symbols(np, entry->nsymbols);
685 /* Convert to structs localsymbol */
686 for (i = 0; i < entry->nsymbols; i++) {
688 entry->symbols[i].nzlist = *nzp++;
690 entry->symbols[i].nzlist.nlist = *np++;
691 entry->symbols[i].nzlist.nz_size = 0;
693 entry->symbols[i].symbol = NULL;
694 entry->symbols[i].next = NULL;
695 entry->symbols[i].entry = entry;
696 entry->symbols[i].gotslot_offset = -1;
697 entry->symbols[i].flags = 0;
700 /* Read strings (text segment) */
701 n = entry->string_size = sdt.sdt_str_sz;
702 entry->strings = (char *)alloca(n);
703 entry->strings_offset = text_offset(entry) + sdt.sdt_strings;
705 entry->strings_offset -
706 (TEXT_START(entry->header) - N_TXTOFF(entry->header)),
708 err(1, "%s: lseek", get_file_name(entry));
709 if (read(fd, entry->strings, n) != n)
710 errx(1, "%s: premature EOF reading strings",
711 get_file_name(entry));
712 enter_file_symbols (entry);
716 * Load any subsidiary shared objects.
721 struct file_entry *prev = NULL;
723 offset = (off_t)sdt.sdt_sods;
725 struct file_entry *subentry;
726 char *libname, name[MAXPATHLEN]; /*XXX*/
728 subentry = (struct file_entry *)
729 xmalloc(sizeof(struct file_entry));
730 bzero(subentry, sizeof(struct file_entry));
731 subentry->superfile = entry;
732 subentry->flags = E_SECONDCLASS;
735 offset - (TEXT_START(entry->header) -
736 N_TXTOFF(entry->header)),
738 err(1, "%s: lseek", get_file_name(entry));
739 if (read(fd, &sod, sizeof(sod)) != sizeof(sod))
740 errx(1, "%s: premature EOF reding sod",
741 get_file_name(entry));
742 md_swapin_sod(&sod, 1);
744 (off_t)sod.sod_name - (TEXT_START(entry->header) -
745 N_TXTOFF(entry->header)),
747 err(1, "%s: lseek", get_file_name(entry));
748 (void)read(fd, name, sizeof(name)); /*XXX*/
749 if (sod.sod_library) {
750 int sod_major = sod.sod_major;
751 int sod_minor = sod.sod_minor;
753 libname = findshlib(name,
754 &sod_major, &sod_minor, 0);
756 errx(1,"no shared -l%s.%d.%d available",
757 name, sod.sod_major, sod.sod_minor);
758 subentry->filename = libname;
759 subentry->local_sym_name = concat("-l", name, "");
761 subentry->filename = strdup(name);
762 subentry->local_sym_name = strdup(name);
764 read_file_symbols(subentry);
767 prev->chain = subentry;
769 entry->subfiles = subentry;
771 fd = file_open(entry);
772 if ((offset = (off_t)sod.sod_next) == 0)
777 if (link_mode & SILLYARCHIVE) {
781 struct file_entry *subentry;
783 sa_name = strdup(entry->filename);
786 cp = sa_name + strlen(sa_name) - 1;
787 while (cp > sa_name) {
788 if (!isdigit(*cp) && *cp != '.')
792 if (cp <= sa_name || *cp != 'o') {
793 /* Not in `libxxx.so.n.m' form */
799 if ((fd = open(sa_name, O_RDONLY, 0)) < 0)
802 /* Read archive magic */
803 bzero(armag, SARMAG);
804 (void)read(fd, armag, SARMAG);
806 if (strncmp(armag, ARMAG, SARMAG) != 0) {
807 warnx("%s: malformed silly archive",
808 get_file_name(entry));
812 subentry = (struct file_entry *)
813 xmalloc(sizeof(struct file_entry));
814 bzero(subentry, sizeof(struct file_entry));
816 entry->silly_archive = subentry;
817 subentry->superfile = entry;
818 subentry->filename = sa_name;
819 subentry->local_sym_name = sa_name;
820 subentry->flags |= E_IS_LIBRARY;
821 search_library(file_open(subentry), subentry);
833 struct file_entry *p;
837 int major = -1, minor = -1;
838 char *cp, *fname = NULL;
840 if (!(p->flags & E_SEARCH_DYNAMIC))
843 fname = findshlib(p->filename, &major, &minor, 1);
845 if (fname && (fd = open(fname, O_RDONLY, 0)) >= 0) {
847 p->lib_major = major;
848 p->lib_minor = minor;
849 p->flags &= ~E_SEARCH_DIRS;
855 p->flags &= ~E_SEARCH_DYNAMIC;
856 if ( (cp = strrchr(p->filename, '/')) ) {
858 fname = concat(concat(p->filename, "/lib", cp), ".a", "");
861 fname = concat("lib", p->filename, ".a");
863 for (i = 0; i < n_search_dirs; i++) {
865 = concat(search_dirs[i], "/", fname);
866 fd = open(path, O_RDONLY, 0);
869 p->flags &= ~E_SEARCH_DIRS;