Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / ld / lib.c
1 /*-
2  * This code is derived from software copyrighted by the Free Software
3  * Foundation.
4  *
5  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6  *
7  * Modified 1993 by Paul Kranenburg, Erasmus University
8  */
9
10 /* Derived from ld.c: "@(#)ld.c 6.10 (Berkeley) 5/22/91"; */
11
12 /* Linker `ld' for GNU
13    Copyright (C) 1988 Free Software Foundation, Inc.
14
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)
18    any later version.
19
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.
24
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.  */
28
29 /* Written by Richard Stallman with some help from Eric Albert.
30    Set, indirect, and warning symbol features added by Randy Smith. */
31
32 /*
33  * $FreeBSD: src/gnu/usr.bin/ld/lib.c,v 1.23 1999/08/27 23:36:01 peter Exp $    - library routines
34  */
35
36 #include <sys/param.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/file.h>
43 #include <sys/time.h>
44 #include <err.h>
45 #include <fcntl.h>
46 #include <ar.h>
47 #include <ranlib.h>
48 #include <a.out.h>
49 #include <stab.h>
50 #include <string.h>
51 #include <dirent.h>
52 #include <ctype.h>
53
54 #include "ld.h"
55 #include "dynamic.h"
56
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,
60                                                         struct file_entry *,
61                                                         int, int *));
62
63 /*
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
67  * hash table.
68  */
69
70 void
71 search_library(fd, entry)
72         int             fd;
73         struct file_entry *entry;
74 {
75         int             member_length;
76         register char  *name;
77         register struct file_entry *subentry;
78
79         if (!(link_mode & FORCEARCHIVE) && !undefined_global_sym_count)
80                 return;
81
82         /* Examine its first member, which starts SARMAG bytes in.  */
83         subentry = decode_library_subfile(fd, entry, SARMAG, &member_length);
84         if (!subentry)
85                 return;
86
87         name = subentry->filename;
88         free(subentry);
89
90         /* Search via __.SYMDEF if that exists, else linearly.  */
91
92         if (!strcmp(name, "__.SYMDEF"))
93                 symdef_library(fd, entry, member_length);
94         else
95                 linear_library(fd, entry);
96 }
97
98 /*
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.
103  */
104
105 static struct file_entry *
106 decode_library_subfile(fd, library_entry, subfile_offset, length_loc)
107         int             fd;
108         struct file_entry *library_entry;
109         int             subfile_offset;
110         int            *length_loc;
111 {
112         int             bytes_read;
113         register int    namelen;
114         int             member_length, content_length;
115         int             starting_offset;
116         register char  *name;
117         struct ar_hdr   hdr1;
118         register struct file_entry *subentry;
119
120         lseek(fd, subfile_offset, 0);
121
122         bytes_read = read(fd, &hdr1, sizeof hdr1);
123         if (!bytes_read)
124                 return 0;       /* end of archive */
125
126         if (sizeof hdr1 != bytes_read)
127                 errx(1, "%s: malformed library archive",
128                         get_file_name(library_entry));
129
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);
134
135         subentry = (struct file_entry *) xmalloc(sizeof(struct file_entry));
136         bzero(subentry, sizeof(struct file_entry));
137
138         for (namelen = 0;
139              namelen < sizeof hdr1.ar_name
140              && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
141              && hdr1.ar_name[namelen] != '/';
142              namelen++);
143
144         starting_offset = subfile_offset + sizeof hdr1;
145         content_length = member_length;
146
147 #ifdef AR_EFMT1
148         /*
149          * BSD 4.4 extended AR format: #1/<namelen>, with name as the
150          * first <namelen> bytes of the file
151          */
152         if (strncmp(hdr1.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
153                         isdigit(hdr1.ar_name[sizeof(AR_EFMT1) - 1])) {
154
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);
161                 name[namelen] = 0;
162                 content_length -= namelen;
163                 starting_offset += namelen;
164         } else
165
166 #endif
167         {
168                 name = (char *)xmalloc(namelen + 1);
169                 strncpy(name, hdr1.ar_name, namelen);
170                 name[namelen] = 0;
171         }
172
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;
178 #if 0
179         subentry->symbols = 0;
180         subentry->strings = 0;
181         subentry->subfiles = 0;
182         subentry->chain = 0;
183         subentry->flags = 0;
184 #endif
185
186         (*length_loc) = member_length;
187
188         return subentry;
189 }
190
191 static int      subfile_wanted_p __P((struct file_entry *));
192
193 /*
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.
198  */
199
200 static void
201 symdef_library(fd, entry, member_length)
202         int             fd;
203         struct file_entry *entry;
204         int             member_length;
205 {
206         int            *symdef_data = (int *) xmalloc(member_length);
207         register struct ranlib *symdef_base;
208         char           *sym_name_base;
209         int             nsymdefs;
210         int             length_of_strings;
211         int             not_finished;
212         int             bytes_read;
213         register int    i;
214         struct file_entry *prev = 0;
215         int             prev_offset = 0;
216
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));
221
222         nsymdefs = md_swap_long(*symdef_data) / sizeof(struct ranlib);
223         if (nsymdefs < 0 ||
224             nsymdefs * sizeof(struct ranlib) + 2 * sizeof(int) > member_length)
225                 errx(1, "%s: malformatted __.SYMDEF",
226                         get_file_name(entry));
227
228         symdef_base = (struct ranlib *) (symdef_data + 1);
229         length_of_strings = md_swap_long(*(int *) (symdef_base + nsymdefs));
230
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));
236
237         sym_name_base = sizeof(int) + (char *) (symdef_base + nsymdefs);
238
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));
247         }
248
249         /*
250          * Search the symdef data for members to load. Do this until one
251          * whole pass finds nothing to load.
252          */
253
254         not_finished = 1;
255         while (not_finished) {
256
257                 not_finished = 0;
258
259                 /*
260                  * Scan all the symbols mentioned in the symdef for ones that
261                  * we need. Load the library members that contain such
262                  * symbols.
263                  */
264
265                 for (i = 0; (i < nsymdefs &&
266                                         ((link_mode & FORCEARCHIVE) ||
267                                         undefined_global_sym_count ||
268                                         common_defined_global_count)); i++) {
269
270                         register symbol *sp;
271                         int             junk;
272                         register int    j;
273                         register int    offset = symdef_base[i].ran_off;
274                         struct file_entry *subentry;
275
276
277                         if (symdef_base[i].ran_un.ran_strx < 0)
278                                 continue;
279
280                         sp = getsym_soft(sym_name_base
281                                          + symdef_base[i].ran_un.ran_strx);
282
283                         /*
284                          * If we find a symbol that appears to be needed,
285                          * think carefully about the archive member that the
286                          * symbol is in.
287                          */
288
289                         /*
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).
295                          *
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.
301                          */
302                         if (!(link_mode & FORCEARCHIVE) &&
303                                 (!sp || sp->defined ||
304                                         (!(sp->flags & GS_REFERENCED) &&
305                                                 !sp->sorefs)))
306                                 continue;
307
308                         /*
309                          * Don't think carefully about any archive member
310                          * more than once in a given pass.
311                          */
312
313                         if (prev_offset == offset)
314                                 continue;
315                         prev_offset = offset;
316
317                         /*
318                          * Read the symbol table of the archive member.
319                          */
320
321                         subentry = decode_library_subfile(fd,
322                                                       entry, offset, &junk);
323                         if (subentry == 0)
324                                 errx(1,
325                                 "invalid offset for %s in symbol table of %s",
326                                       sym_name_base
327                                               + symdef_base[i].ran_un.ran_strx,
328                                       entry->filename);
329
330                         read_entry_symbols(fd, subentry);
331                         subentry->strings = (char *)
332                                 alloca(subentry->string_size);
333                         read_entry_strings(fd, subentry);
334
335                         /*
336                          * Now scan the symbol table and decide whether to
337                          * load.
338                          */
339
340                         if (!(link_mode & FORCEARCHIVE) &&
341                                         !subfile_wanted_p(subentry)) {
342                                 if (subentry->symbols)
343                                         free(subentry->symbols);
344                                 free(subentry);
345                         } else {
346                                 /*
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
350                                  * data.
351                                  */
352
353                                 not_finished = 1;
354
355                                 read_entry_relocation(fd, subentry);
356                                 enter_file_symbols(subentry);
357
358                                 if (prev)
359                                         prev->chain = subentry;
360                                 else
361                                         entry->subfiles = subentry;
362                                 prev = subentry;
363
364                                 /*
365                                  * Clear out this member's symbols from the
366                                  * symdef data so that following passes won't
367                                  * waste time on them.
368                                  */
369
370                                 for (j = 0; j < nsymdefs; j++) {
371                                         if (symdef_base[j].ran_off == offset)
372                                                 symdef_base[j].ran_un.ran_strx = -1;
373                                 }
374
375                                 /*
376                                  * We'll read the strings again
377                                  * if we need them.
378                                  */
379                                 subentry->strings = 0;
380                         }
381                 }
382         }
383
384         free(symdef_data);
385 }
386
387 /*
388  * Search a library that has no __.SYMDEF. ENTRY is the library's file_entry.
389  * FD is the descriptor it is open on.
390  */
391
392 static void
393 linear_library(fd, entry)
394         int             fd;
395         struct file_entry *entry;
396 {
397         register struct file_entry *prev = 0;
398         register int    this_subfile_offset = SARMAG;
399
400         while ((link_mode & FORCEARCHIVE) ||
401                 undefined_global_sym_count || common_defined_global_count) {
402
403                 int                             member_length;
404                 register struct file_entry      *subentry;
405
406                 subentry = decode_library_subfile(fd, entry,
407                                         this_subfile_offset, &member_length);
408
409                 if (!subentry)
410                         return;
411
412                 read_entry_symbols(fd, subentry);
413                 subentry->strings = (char *)alloca(subentry->string_size);
414                 read_entry_strings(fd, subentry);
415
416                 if (!(link_mode & FORCEARCHIVE) &&
417                                         !subfile_wanted_p(subentry)) {
418                         if (subentry->symbols)
419                                 free(subentry->symbols);
420                         free(subentry);
421                 } else {
422                         read_entry_relocation(fd, subentry);
423                         enter_file_symbols(subentry);
424
425                         if (prev)
426                                 prev->chain = subentry;
427                         else
428                                 entry->subfiles = subentry;
429                         prev = subentry;
430                         subentry->strings = 0;  /* Since space will dissapear
431                                                  * on return */
432                 }
433
434                 this_subfile_offset += member_length + sizeof(struct ar_hdr);
435                 if (this_subfile_offset & 1)
436                         this_subfile_offset++;
437         }
438 }
439
440 /*
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.
443  */
444
445 static int
446 subfile_wanted_p(entry)
447         struct file_entry *entry;
448 {
449         struct localsymbol      *lsp, *lspend;
450 #ifdef DOLLAR_KLUDGE
451         register int    dollar_cond = 0;
452 #endif
453
454         lspend = entry->symbols + entry->nsymbols;
455
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);
461
462                 /*
463                  * If the symbol has an interesting definition, we could
464                  * potentially want it.
465                  */
466                 if (! (type & N_EXT)
467                     || (type == (N_UNDF | N_EXT) && p->n_value == 0
468
469 #ifdef DOLLAR_KLUDGE
470                         && name[1] != '$'
471 #endif
472                         )
473 #ifdef SET_ELEMENT_P
474                     || SET_ELEMENT_P(type)
475                     || set_element_prefixed_p(name)
476 #endif
477                 )
478                         continue;
479
480
481 #ifdef DOLLAR_KLUDGE
482                 if (name[1] == '$') {
483                         sp = getsym_soft(&name[2]);
484                         dollar_cond = 1;
485                         if (!sp)
486                                 continue;
487                         if (sp->flags & SP_REFERENCED) {
488                                 if (write_map) {
489                                         print_file_name(entry, stdout);
490                                         fprintf(stdout, " needed due to $-conditional %s\n", name);
491                                 }
492                                 return 1;
493                         }
494                         continue;
495                 }
496 #endif
497
498                 /*
499                  * If this symbol has not been hashed, we can't be
500                  * looking for it.
501                  */
502
503                 if (!sp)
504                         continue;
505
506                 /*
507                  * We don't load a file if it merely satisfies a
508                  * common reference (see explanation above in
509                  * symdef_library()).
510                  */
511                 if ((sp->flags & GS_REFERENCED) && !sp->defined) {
512                         /*
513                          * This is a symbol we are looking for.  It
514                          * is either not yet defined or defined as a
515                          * common.
516                          */
517 #ifdef DOLLAR_KLUDGE
518                         if (dollar_cond)
519                                 continue;
520 #endif
521                         if (type == (N_UNDF | N_EXT)) {
522                                 /*
523                                  * Symbol being defined as common.
524                                  * Remember this, but don't load
525                                  * subfile just for this.
526                                  */
527
528                                 /*
529                                  * If it didn't used to be common, up
530                                  * the count of common symbols.
531                                  */
532                                 if (!sp->common_size)
533                                         common_defined_global_count++;
534
535                                 if (sp->common_size < p->n_value)
536                                         sp->common_size = p->n_value;
537                                 if (!sp->defined)
538                                         undefined_global_sym_count--;
539                                 sp->defined = type;
540                                 continue;
541                         }
542                         if (sp->flags & GS_WEAK)
543                                 /* Weak symbols don't pull archive members */
544                                 continue;
545                         if (write_map) {
546                                 print_file_name(entry, stdout);
547                                 fprintf(stdout, " needed due to %s\n", demangle(sp->name));
548                         }
549                         return 1;
550                 } else  if (!sp->defined && sp->sorefs) {
551                         /*
552                          * Check for undefined symbols or commons
553                          * in shared objects.
554                          */
555                         struct localsymbol *lsp;
556
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 */
563                         }
564                         if (lsp != NULL)
565                                 /*
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.
569                                  */
570                                 continue;
571
572                         /*
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.
577                          */
578
579                         if (type == (N_UNDF|N_EXT) && p->n_value) {
580                                 /*
581                                  * New symbol is common, just takes its
582                                  * size, but don't load.
583                                  */
584                                 sp->common_size = p->n_value;
585                                 sp->defined = type;
586                                 continue;
587                         }
588
589                         /*
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
594                          * symmetric.
595                          */
596
597                         if (write_map) {
598                                 print_file_name(entry, stdout);
599                                 fprintf(stdout,
600                                         " needed due to shared lib ref %s (%d)\n",
601                                         demangle(sp->name),
602                                         lsp ? lsp->nzlist.nlist.n_type : -1);
603                         }
604                         return 1;
605                 }
606         }
607
608         return 0;
609 }
610
611 /*
612  * Read the symbols of dynamic entity ENTRY into core. Assume it is already
613  * open, on descriptor FD.
614  */
615 void
616 read_shared_object(fd, entry)
617         struct file_entry *entry;
618         int fd;
619 {
620         struct _dynamic                 dyn;
621         struct section_dispatch_table   sdt;
622         struct nlist                    *np;
623         struct nzlist                   *nzp;
624         int                             n, i, has_nz = 0;
625
626         if (!(entry->flags & E_HEADER_VALID))
627                 read_header(fd, entry);
628
629         /* Read DYNAMIC structure (first in data segment) */
630         if (lseek(fd, text_offset(entry) + entry->header.a_text, L_SET) ==
631             (off_t)-1)
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));
636         }
637         md_swapin__dynamic(&dyn);
638
639         /* Check version */
640         switch (dyn.d_version) {
641         default:
642                 errx(1, "%s: unsupported _DYNAMIC version: %d",
643                         get_file_name(entry), dyn.d_version);
644                 break;
645         case LD_VERSION_SUN:
646                 break;
647         case LD_VERSION_BSD:
648                 has_nz = 1;
649                 break;
650         }
651
652         /* Read Section Dispatch Table (from data segment) */
653         if (lseek(fd,
654             text_offset(entry) + (long)dyn.d_un.d_sdt -
655                 (DATA_START(entry->header) - N_DATOFF(entry->header)),
656             L_SET) == (off_t)-1)
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);
662
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));
670
671         if (lseek(fd,
672             text_offset(entry) + (long)sdt.sdt_nzlist -
673                 (TEXT_START(entry->header) - N_TXTOFF(entry->header)),
674             L_SET) == (off_t)-1)
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));
679
680         if (has_nz)
681                 md_swapin_zsymbols(nzp, entry->nsymbols);
682         else
683                 md_swapin_symbols(np, entry->nsymbols);
684
685         /* Convert to structs localsymbol */
686         for (i = 0; i < entry->nsymbols; i++) {
687                 if (has_nz) {
688                         entry->symbols[i].nzlist = *nzp++;
689                 } else {
690                         entry->symbols[i].nzlist.nlist = *np++;
691                         entry->symbols[i].nzlist.nz_size = 0;
692                 }
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;
698         }
699
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;
704         if (lseek(fd,
705             entry->strings_offset -
706                 (TEXT_START(entry->header) - N_TXTOFF(entry->header)),
707             L_SET) == (off_t)-1)
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);
713         entry->strings = 0;
714
715         /*
716          * Load any subsidiary shared objects.
717          */
718         if (sdt.sdt_sods) {
719                 struct sod              sod;
720                 off_t                   offset;
721                 struct file_entry       *prev = NULL;
722
723                 offset = (off_t)sdt.sdt_sods;
724                 while (1) {
725                         struct file_entry *subentry;
726                         char *libname, name[MAXPATHLEN]; /*XXX*/
727
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;
733
734                         if (lseek(fd,
735                             offset - (TEXT_START(entry->header) -
736                                       N_TXTOFF(entry->header)),
737                             L_SET) == (off_t)-1)
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);
743                         if (lseek(fd,
744                             (off_t)sod.sod_name - (TEXT_START(entry->header) -
745                                                    N_TXTOFF(entry->header)),
746                             L_SET) == (off_t)-1)
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;
752
753                                 libname = findshlib(name,
754                                                 &sod_major, &sod_minor, 0);
755                                 if (libname == NULL)
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, "");
760                         } else {
761                                 subentry->filename = strdup(name);
762                                 subentry->local_sym_name = strdup(name);
763                         }
764                         read_file_symbols(subentry);
765
766                         if (prev)
767                                 prev->chain = subentry;
768                         else
769                                 entry->subfiles = subentry;
770                         prev = subentry;
771                         fd = file_open(entry);
772                         if ((offset = (off_t)sod.sod_next) == 0)
773                                 break;
774                 }
775         }
776 #ifdef SUN_COMPAT
777         if (link_mode & SILLYARCHIVE) {
778                 char                    *cp, *sa_name;
779                 char                    armag[SARMAG];
780                 int                     fd;
781                 struct file_entry       *subentry;
782
783                 sa_name = strdup(entry->filename);
784                 if (sa_name == NULL)
785                         goto out;
786                 cp = sa_name + strlen(sa_name) - 1;
787                 while (cp > sa_name) {
788                         if (!isdigit(*cp) && *cp != '.')
789                                 break;
790                         --cp;
791                 }
792                 if (cp <= sa_name || *cp != 'o') {
793                         /* Not in `libxxx.so.n.m' form */
794                         free(sa_name);
795                         goto out;
796                 }
797
798                 *cp = 'a';
799                 if ((fd = open(sa_name, O_RDONLY, 0)) < 0)
800                         goto out;
801
802                 /* Read archive magic */
803                 bzero(armag, SARMAG);
804                 (void)read(fd, armag, SARMAG);
805                 (void)close(fd);
806                 if (strncmp(armag, ARMAG, SARMAG) != 0) {
807                         warnx("%s: malformed silly archive",
808                                         get_file_name(entry));
809                         goto out;
810                 }
811
812                 subentry = (struct file_entry *)
813                                 xmalloc(sizeof(struct file_entry));
814                 bzero(subentry, sizeof(struct file_entry));
815
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);
822 out:
823                 ;
824         }
825 #endif
826 }
827
828 #undef major
829 #undef minor
830
831 int
832 findlib(p)
833 struct file_entry       *p;
834 {
835         int             i;
836         int             fd = -1;
837         int             major = -1, minor = -1;
838         char            *cp, *fname = NULL;
839
840         if (!(p->flags & E_SEARCH_DYNAMIC))
841                 goto dot_a;
842
843         fname = findshlib(p->filename, &major, &minor, 1);
844
845         if (fname && (fd = open(fname, O_RDONLY, 0)) >= 0) {
846                 p->filename = fname;
847                 p->lib_major = major;
848                 p->lib_minor = minor;
849                 p->flags &= ~E_SEARCH_DIRS;
850                 return fd;
851         }
852         (void)free(fname);
853
854 dot_a:
855         p->flags &= ~E_SEARCH_DYNAMIC;
856         if ( (cp = strrchr(p->filename, '/')) ) {
857                 *cp++ = '\0';
858                 fname = concat(concat(p->filename, "/lib", cp), ".a", "");
859                 *(--cp) = '/';
860         } else
861                 fname = concat("lib", p->filename, ".a");
862
863         for (i = 0; i < n_search_dirs; i++) {
864                 register char *path
865                         = concat(search_dirs[i], "/", fname);
866                 fd = open(path, O_RDONLY, 0);
867                 if (fd >= 0) {
868                         p->filename = path;
869                         p->flags &= ~E_SEARCH_DIRS;
870                         break;
871                 }
872                 (void)free(path);
873         }
874         (void)free(fname);
875         return fd;
876 }