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
34 * $DragonFly: src/gnu/usr.bin/ld/Attic/lib.c,v 1.2 2003/06/17 04:25:46 dillon Exp $ - library routines
37 #include <sys/param.h>
41 #include <sys/types.h>
58 static void linear_library __P((int, struct file_entry *));
59 static void symdef_library __P((int, struct file_entry *, int));
60 static struct file_entry *decode_library_subfile __P((int,
65 * Search the library ENTRY, already open on descriptor FD. This means
66 * deciding which library members to load, making a chain of `struct
67 * file_entry' for those members, and entering their global symbols in the
72 search_library(fd, entry)
74 struct file_entry *entry;
78 register struct file_entry *subentry;
80 if (!(link_mode & FORCEARCHIVE) && !undefined_global_sym_count)
83 /* Examine its first member, which starts SARMAG bytes in. */
84 subentry = decode_library_subfile(fd, entry, SARMAG, &member_length);
88 name = subentry->filename;
91 /* Search via __.SYMDEF if that exists, else linearly. */
93 if (!strcmp(name, "__.SYMDEF"))
94 symdef_library(fd, entry, member_length);
96 linear_library(fd, entry);
100 * Construct and return a file_entry for a library member. The library's
101 * file_entry is library_entry, and the library is open on FD.
102 * SUBFILE_OFFSET is the byte index in the library of this member's header.
103 * We store the length of the member into *LENGTH_LOC.
106 static struct file_entry *
107 decode_library_subfile(fd, library_entry, subfile_offset, length_loc)
109 struct file_entry *library_entry;
114 register int namelen;
115 int member_length, content_length;
119 register struct file_entry *subentry;
121 lseek(fd, subfile_offset, 0);
123 bytes_read = read(fd, &hdr1, sizeof hdr1);
125 return 0; /* end of archive */
127 if (sizeof hdr1 != bytes_read)
128 errx(1, "%s: malformed library archive",
129 get_file_name(library_entry));
131 if (sscanf(hdr1.ar_size, "%d", &member_length) != 1)
132 errx(1, "%s: malformatted header of archive member: %.*s",
133 get_file_name(library_entry),
134 (int)sizeof(hdr1.ar_name), hdr1.ar_name);
136 subentry = (struct file_entry *) xmalloc(sizeof(struct file_entry));
137 bzero(subentry, sizeof(struct file_entry));
140 namelen < sizeof hdr1.ar_name
141 && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
142 && hdr1.ar_name[namelen] != '/';
145 starting_offset = subfile_offset + sizeof hdr1;
146 content_length = member_length;
150 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
151 * first <namelen> bytes of the file
153 if (strncmp(hdr1.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
154 isdigit(hdr1.ar_name[sizeof(AR_EFMT1) - 1])) {
156 namelen = atoi(&hdr1.ar_name[sizeof(AR_EFMT1) - 1]);
157 name = (char *)xmalloc(namelen + 1);
158 if (read(fd, name, namelen) != namelen)
159 errx(1, "%s: malformatted archive member: %.*s",
160 get_file_name(library_entry),
161 (int)sizeof(hdr1.ar_name), hdr1.ar_name);
163 content_length -= namelen;
164 starting_offset += namelen;
169 name = (char *)xmalloc(namelen + 1);
170 strncpy(name, hdr1.ar_name, namelen);
174 subentry->filename = name;
175 subentry->local_sym_name = name;
176 subentry->starting_offset = starting_offset;
177 subentry->superfile = library_entry;
178 subentry->total_size = content_length;
180 subentry->symbols = 0;
181 subentry->strings = 0;
182 subentry->subfiles = 0;
187 (*length_loc) = member_length;
192 static int subfile_wanted_p __P((struct file_entry *));
195 * Search a library that has a __.SYMDEF member. FD is a descriptor on
196 * which the library is open. The file pointer is assumed to point at the
197 * __.SYMDEF data. ENTRY is the library's file_entry. MEMBER_LENGTH is the
198 * length of the __.SYMDEF data.
202 symdef_library(fd, entry, member_length)
204 struct file_entry *entry;
207 int *symdef_data = (int *) xmalloc(member_length);
208 register struct ranlib *symdef_base;
211 int length_of_strings;
215 struct file_entry *prev = 0;
218 bytes_read = read(fd, symdef_data, member_length);
219 if (bytes_read != member_length)
220 errx(1, "%s: malformatted __.SYMDEF",
221 get_file_name(entry));
223 nsymdefs = md_swap_long(*symdef_data) / sizeof(struct ranlib);
225 nsymdefs * sizeof(struct ranlib) + 2 * sizeof(int) > member_length)
226 errx(1, "%s: malformatted __.SYMDEF",
227 get_file_name(entry));
229 symdef_base = (struct ranlib *) (symdef_data + 1);
230 length_of_strings = md_swap_long(*(int *) (symdef_base + nsymdefs));
232 if (length_of_strings < 0
233 || nsymdefs * sizeof(struct ranlib) + length_of_strings
234 + 2 * sizeof(int) > member_length)
235 errx(1, "%s: malformatted __.SYMDEF",
236 get_file_name(entry));
238 sym_name_base = sizeof(int) + (char *) (symdef_base + nsymdefs);
240 /* Check all the string indexes for validity. */
241 md_swapin_ranlib_hdr(symdef_base, nsymdefs);
242 for (i = 0; i < nsymdefs; i++) {
243 register int index = symdef_base[i].ran_un.ran_strx;
244 if (index < 0 || index >= length_of_strings
245 || (index && *(sym_name_base + index - 1)))
246 errx(1, "%s: malformatted __.SYMDEF",
247 get_file_name(entry));
251 * Search the symdef data for members to load. Do this until one
252 * whole pass finds nothing to load.
256 while (not_finished) {
261 * Scan all the symbols mentioned in the symdef for ones that
262 * we need. Load the library members that contain such
266 for (i = 0; (i < nsymdefs &&
267 ((link_mode & FORCEARCHIVE) ||
268 undefined_global_sym_count ||
269 common_defined_global_count)); i++) {
274 register int offset = symdef_base[i].ran_off;
275 struct file_entry *subentry;
278 if (symdef_base[i].ran_un.ran_strx < 0)
281 sp = getsym_soft(sym_name_base
282 + symdef_base[i].ran_un.ran_strx);
285 * If we find a symbol that appears to be needed,
286 * think carefully about the archive member that the
291 * Per Mike Karels' recommendation, we no longer load
292 * library files if the only reference(s) that would
293 * be satisfied are 'common' references. This
294 * prevents some problems with name pollution (e.g. a
295 * global common 'utime' linked to a function).
297 * If we're not forcing the archive in then we don't
298 * need to bother if: we've never heard of the symbol,
299 * or if it is already defined. The last clause causes
300 * archive members to be searched for definitions
301 * satisfying undefined shared object symbols.
303 if (!(link_mode & FORCEARCHIVE) &&
304 (!sp || sp->defined ||
305 (!(sp->flags & GS_REFERENCED) &&
310 * Don't think carefully about any archive member
311 * more than once in a given pass.
314 if (prev_offset == offset)
316 prev_offset = offset;
319 * Read the symbol table of the archive member.
322 subentry = decode_library_subfile(fd,
323 entry, offset, &junk);
326 "invalid offset for %s in symbol table of %s",
328 + symdef_base[i].ran_un.ran_strx,
331 read_entry_symbols(fd, subentry);
332 subentry->strings = (char *)
333 alloca(subentry->string_size);
334 read_entry_strings(fd, subentry);
337 * Now scan the symbol table and decide whether to
341 if (!(link_mode & FORCEARCHIVE) &&
342 !subfile_wanted_p(subentry)) {
343 if (subentry->symbols)
344 free(subentry->symbols);
348 * This member is needed; load it. Since we
349 * are loading something on this pass, we
350 * must make another pass through the symdef
356 read_entry_relocation(fd, subentry);
357 enter_file_symbols(subentry);
360 prev->chain = subentry;
362 entry->subfiles = subentry;
366 * Clear out this member's symbols from the
367 * symdef data so that following passes won't
368 * waste time on them.
371 for (j = 0; j < nsymdefs; j++) {
372 if (symdef_base[j].ran_off == offset)
373 symdef_base[j].ran_un.ran_strx = -1;
377 * We'll read the strings again
380 subentry->strings = 0;
389 * Search a library that has no __.SYMDEF. ENTRY is the library's file_entry.
390 * FD is the descriptor it is open on.
394 linear_library(fd, entry)
396 struct file_entry *entry;
398 register struct file_entry *prev = 0;
399 register int this_subfile_offset = SARMAG;
401 while ((link_mode & FORCEARCHIVE) ||
402 undefined_global_sym_count || common_defined_global_count) {
405 register struct file_entry *subentry;
407 subentry = decode_library_subfile(fd, entry,
408 this_subfile_offset, &member_length);
413 read_entry_symbols(fd, subentry);
414 subentry->strings = (char *)alloca(subentry->string_size);
415 read_entry_strings(fd, subentry);
417 if (!(link_mode & FORCEARCHIVE) &&
418 !subfile_wanted_p(subentry)) {
419 if (subentry->symbols)
420 free(subentry->symbols);
423 read_entry_relocation(fd, subentry);
424 enter_file_symbols(subentry);
427 prev->chain = subentry;
429 entry->subfiles = subentry;
431 subentry->strings = 0; /* Since space will dissapear
435 this_subfile_offset += member_length + sizeof(struct ar_hdr);
436 if (this_subfile_offset & 1)
437 this_subfile_offset++;
442 * ENTRY is an entry for a library member. Its symbols have been read into
443 * core, but not entered. Return nonzero if we ought to load this member.
447 subfile_wanted_p(entry)
448 struct file_entry *entry;
450 struct localsymbol *lsp, *lspend;
452 register int dollar_cond = 0;
455 lspend = entry->symbols + entry->nsymbols;
457 for (lsp = entry->symbols; lsp < lspend; lsp++) {
458 register struct nlist *p = &lsp->nzlist.nlist;
459 register int type = p->n_type;
460 register char *name = p->n_un.n_strx + entry->strings;
461 register symbol *sp = getsym_soft(name);
464 * If the symbol has an interesting definition, we could
465 * potentially want it.
468 || (type == (N_UNDF | N_EXT) && p->n_value == 0
475 || SET_ELEMENT_P(type)
476 || set_element_prefixed_p(name)
483 if (name[1] == '$') {
484 sp = getsym_soft(&name[2]);
488 if (sp->flags & SP_REFERENCED) {
490 print_file_name(entry, stdout);
491 fprintf(stdout, " needed due to $-conditional %s\n", name);
500 * If this symbol has not been hashed, we can't be
508 * We don't load a file if it merely satisfies a
509 * common reference (see explanation above in
512 if ((sp->flags & GS_REFERENCED) && !sp->defined) {
514 * This is a symbol we are looking for. It
515 * is either not yet defined or defined as a
522 if (type == (N_UNDF | N_EXT)) {
524 * Symbol being defined as common.
525 * Remember this, but don't load
526 * subfile just for this.
530 * If it didn't used to be common, up
531 * the count of common symbols.
533 if (!sp->common_size)
534 common_defined_global_count++;
536 if (sp->common_size < p->n_value)
537 sp->common_size = p->n_value;
539 undefined_global_sym_count--;
543 if (sp->flags & GS_WEAK)
544 /* Weak symbols don't pull archive members */
547 print_file_name(entry, stdout);
548 fprintf(stdout, " needed due to %s\n", demangle(sp->name));
551 } else if (!sp->defined && sp->sorefs) {
553 * Check for undefined symbols or commons
556 struct localsymbol *lsp;
558 for (lsp = sp->sorefs; lsp; lsp = lsp->next) {
559 int type = lsp->nzlist.nlist.n_type;
560 if ( (type & N_EXT) &&
561 (type & N_STAB) == 0 &&
562 type != (N_UNDF | N_EXT))
563 break; /* We don't need it */
567 * We have a worthy definition in a shared
568 * object that was specified ahead of the
569 * archive we're examining now. So, punt.
574 * At this point, we have an undefined shared
575 * object reference. Again, if the archive member
576 * defines a common we just note the its size.
577 * Otherwise, the member gets included.
580 if (type == (N_UNDF|N_EXT) && p->n_value) {
582 * New symbol is common, just takes its
583 * size, but don't load.
585 sp->common_size = p->n_value;
591 * THIS STILL MISSES the case where one shared
592 * object defines a common and the next defines
593 * more strongly; fix this someday by making
594 * `struct glosym' and enter_global_ref() more
599 print_file_name(entry, stdout);
601 " needed due to shared lib ref %s (%d)\n",
603 lsp ? lsp->nzlist.nlist.n_type : -1);
613 * Read the symbols of dynamic entity ENTRY into core. Assume it is already
614 * open, on descriptor FD.
617 read_shared_object(fd, entry)
618 struct file_entry *entry;
622 struct section_dispatch_table sdt;
625 int n, i, has_nz = 0;
627 if (!(entry->flags & E_HEADER_VALID))
628 read_header(fd, entry);
630 /* Read DYNAMIC structure (first in data segment) */
631 if (lseek(fd, text_offset(entry) + entry->header.a_text, L_SET) ==
633 err(1, "%s: lseek", get_file_name(entry));
634 if (read(fd, &dyn, sizeof dyn) != sizeof dyn) {
635 errx(1, "%s: premature EOF reading _dynamic",
636 get_file_name(entry));
638 md_swapin__dynamic(&dyn);
641 switch (dyn.d_version) {
643 errx(1, "%s: unsupported _DYNAMIC version: %d",
644 get_file_name(entry), dyn.d_version);
653 /* Read Section Dispatch Table (from data segment) */
655 text_offset(entry) + (long)dyn.d_un.d_sdt -
656 (DATA_START(entry->header) - N_DATOFF(entry->header)),
658 err(1, "%s: lseek", get_file_name(entry));
659 if (read(fd, &sdt, sizeof sdt) != sizeof sdt)
660 errx(1, "%s: premature EOF reading sdt",
661 get_file_name(entry));
662 md_swapin_section_dispatch_table(&sdt);
664 /* Read symbols (text segment) */
665 n = sdt.sdt_strings - sdt.sdt_nzlist;
666 entry->nsymbols = n /
667 (has_nz ? sizeof(struct nzlist) : sizeof(struct nlist));
668 nzp = (struct nzlist *)(np = (struct nlist *)alloca (n));
669 entry->symbols = (struct localsymbol *)
670 xmalloc(entry->nsymbols * sizeof(struct localsymbol));
673 text_offset(entry) + (long)sdt.sdt_nzlist -
674 (TEXT_START(entry->header) - N_TXTOFF(entry->header)),
676 err(1, "%s: lseek", get_file_name(entry));
677 if (read(fd, (char *)nzp, n) != n)
678 errx(1, "%s: premature EOF reading symbols ",
679 get_file_name(entry));
682 md_swapin_zsymbols(nzp, entry->nsymbols);
684 md_swapin_symbols(np, entry->nsymbols);
686 /* Convert to structs localsymbol */
687 for (i = 0; i < entry->nsymbols; i++) {
689 entry->symbols[i].nzlist = *nzp++;
691 entry->symbols[i].nzlist.nlist = *np++;
692 entry->symbols[i].nzlist.nz_size = 0;
694 entry->symbols[i].symbol = NULL;
695 entry->symbols[i].next = NULL;
696 entry->symbols[i].entry = entry;
697 entry->symbols[i].gotslot_offset = -1;
698 entry->symbols[i].flags = 0;
701 /* Read strings (text segment) */
702 n = entry->string_size = sdt.sdt_str_sz;
703 entry->strings = (char *)alloca(n);
704 entry->strings_offset = text_offset(entry) + sdt.sdt_strings;
706 entry->strings_offset -
707 (TEXT_START(entry->header) - N_TXTOFF(entry->header)),
709 err(1, "%s: lseek", get_file_name(entry));
710 if (read(fd, entry->strings, n) != n)
711 errx(1, "%s: premature EOF reading strings",
712 get_file_name(entry));
713 enter_file_symbols (entry);
717 * Load any subsidiary shared objects.
722 struct file_entry *prev = NULL;
724 offset = (off_t)sdt.sdt_sods;
726 struct file_entry *subentry;
727 char *libname, name[MAXPATHLEN]; /*XXX*/
729 subentry = (struct file_entry *)
730 xmalloc(sizeof(struct file_entry));
731 bzero(subentry, sizeof(struct file_entry));
732 subentry->superfile = entry;
733 subentry->flags = E_SECONDCLASS;
736 offset - (TEXT_START(entry->header) -
737 N_TXTOFF(entry->header)),
739 err(1, "%s: lseek", get_file_name(entry));
740 if (read(fd, &sod, sizeof(sod)) != sizeof(sod))
741 errx(1, "%s: premature EOF reding sod",
742 get_file_name(entry));
743 md_swapin_sod(&sod, 1);
745 (off_t)sod.sod_name - (TEXT_START(entry->header) -
746 N_TXTOFF(entry->header)),
748 err(1, "%s: lseek", get_file_name(entry));
749 (void)read(fd, name, sizeof(name)); /*XXX*/
750 if (sod.sod_library) {
751 int sod_major = sod.sod_major;
752 int sod_minor = sod.sod_minor;
754 libname = findshlib(name,
755 &sod_major, &sod_minor, 0);
757 errx(1,"no shared -l%s.%d.%d available",
758 name, sod.sod_major, sod.sod_minor);
759 subentry->filename = libname;
760 subentry->local_sym_name = concat("-l", name, "");
762 subentry->filename = strdup(name);
763 subentry->local_sym_name = strdup(name);
765 read_file_symbols(subentry);
768 prev->chain = subentry;
770 entry->subfiles = subentry;
772 fd = file_open(entry);
773 if ((offset = (off_t)sod.sod_next) == 0)
778 if (link_mode & SILLYARCHIVE) {
782 struct file_entry *subentry;
784 sa_name = strdup(entry->filename);
787 cp = sa_name + strlen(sa_name) - 1;
788 while (cp > sa_name) {
789 if (!isdigit(*cp) && *cp != '.')
793 if (cp <= sa_name || *cp != 'o') {
794 /* Not in `libxxx.so.n.m' form */
800 if ((fd = open(sa_name, O_RDONLY, 0)) < 0)
803 /* Read archive magic */
804 bzero(armag, SARMAG);
805 (void)read(fd, armag, SARMAG);
807 if (strncmp(armag, ARMAG, SARMAG) != 0) {
808 warnx("%s: malformed silly archive",
809 get_file_name(entry));
813 subentry = (struct file_entry *)
814 xmalloc(sizeof(struct file_entry));
815 bzero(subentry, sizeof(struct file_entry));
817 entry->silly_archive = subentry;
818 subentry->superfile = entry;
819 subentry->filename = sa_name;
820 subentry->local_sym_name = sa_name;
821 subentry->flags |= E_IS_LIBRARY;
822 search_library(file_open(subentry), subentry);
834 struct file_entry *p;
838 int major = -1, minor = -1;
839 char *cp, *fname = NULL;
841 if (!(p->flags & E_SEARCH_DYNAMIC))
844 fname = findshlib(p->filename, &major, &minor, 1);
846 if (fname && (fd = open(fname, O_RDONLY, 0)) >= 0) {
848 p->lib_major = major;
849 p->lib_minor = minor;
850 p->flags &= ~E_SEARCH_DIRS;
856 p->flags &= ~E_SEARCH_DYNAMIC;
857 if ( (cp = strrchr(p->filename, '/')) ) {
859 fname = concat(concat(p->filename, "/lib", cp), ".a", "");
862 fname = concat("lib", p->filename, ".a");
864 for (i = 0; i < n_search_dirs; i++) {
866 = concat(search_dirs[i], "/", fname);
867 fd = open(path, O_RDONLY, 0);
870 p->flags &= ~E_SEARCH_DIRS;