arch.c revision 1.87 1 /* $NetBSD: arch.c,v 1.87 2020/08/22 11:35:00 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #ifndef MAKE_NATIVE
72 static char rcsid[] = "$NetBSD: arch.c,v 1.87 2020/08/22 11:35:00 rillig Exp $";
73 #else
74 #include <sys/cdefs.h>
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "@(#)arch.c 8.2 (Berkeley) 1/2/94";
78 #else
79 __RCSID("$NetBSD: arch.c,v 1.87 2020/08/22 11:35:00 rillig Exp $");
80 #endif
81 #endif /* not lint */
82 #endif
83
84 /*-
85 * arch.c --
86 * Functions to manipulate libraries, archives and their members.
87 *
88 * Once again, cacheing/hashing comes into play in the manipulation
89 * of archives. The first time an archive is referenced, all of its members'
90 * headers are read and hashed and the archive closed again. All hashed
91 * archives are kept on a list which is searched each time an archive member
92 * is referenced.
93 *
94 * The interface to this module is:
95 * Arch_ParseArchive Given an archive specification, return a list
96 * of GNode's, one for each member in the spec.
97 * FAILURE is returned if the specification is
98 * invalid for some reason.
99 *
100 * Arch_Touch Alter the modification time of the archive
101 * member described by the given node to be
102 * the current time.
103 *
104 * Arch_TouchLib Update the modification time of the library
105 * described by the given node. This is special
106 * because it also updates the modification time
107 * of the library's table of contents.
108 *
109 * Arch_MTime Find the modification time of a member of
110 * an archive *in the archive*. The time is also
111 * placed in the member's GNode. Returns the
112 * modification time.
113 *
114 * Arch_MemTime Find the modification time of a member of
115 * an archive. Called when the member doesn't
116 * already exist. Looks in the archive for the
117 * modification time. Returns the modification
118 * time.
119 *
120 * Arch_FindLib Search for a library along a path. The
121 * library name in the GNode should be in
122 * -l<name> format.
123 *
124 * Arch_LibOODate Special function to decide if a library node
125 * is out-of-date.
126 *
127 * Arch_Init Initialize this module.
128 *
129 * Arch_End Cleanup this module.
130 */
131
132 #include <sys/types.h>
133 #include <sys/stat.h>
134 #include <sys/time.h>
135 #include <sys/param.h>
136
137 #include <ar.h>
138 #include <ctype.h>
139 #include <stdio.h>
140 #include <stdlib.h>
141 #include <utime.h>
142
143 #include "make.h"
144 #include "hash.h"
145 #include "dir.h"
146 #include "config.h"
147
148 #ifdef TARGET_MACHINE
149 #undef MAKE_MACHINE
150 #define MAKE_MACHINE TARGET_MACHINE
151 #endif
152 #ifdef TARGET_MACHINE_ARCH
153 #undef MAKE_MACHINE_ARCH
154 #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
155 #endif
156
157 static Lst archives; /* Lst of archives we've already examined */
158
159 typedef struct Arch {
160 char *name; /* Name of archive */
161 Hash_Table members; /* All the members of the archive described
162 * by <name, struct ar_hdr *> key/value pairs */
163 char *fnametab; /* Extended name table strings */
164 size_t fnamesize; /* Size of the string table */
165 } Arch;
166
167 static int ArchFindArchive(const void *, const void *);
168 #ifdef CLEANUP
169 static void ArchFree(void *);
170 #endif
171 static struct ar_hdr *ArchStatMember(const char *, const char *, Boolean);
172 static FILE *ArchFindMember(const char *, const char *,
173 struct ar_hdr *, const char *);
174 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
175 #define SVR4ARCHIVES
176 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
177 #endif
178
179 #ifdef CLEANUP
180 /*-
181 *-----------------------------------------------------------------------
182 * ArchFree --
183 * Free memory used by an archive
184 *
185 * Results:
186 * None.
187 *
188 * Side Effects:
189 * None.
190 *
191 *-----------------------------------------------------------------------
192 */
193 static void
194 ArchFree(void *ap)
195 {
196 Arch *a = (Arch *)ap;
197 Hash_Search search;
198 Hash_Entry *entry;
199
200 /* Free memory from hash entries */
201 for (entry = Hash_EnumFirst(&a->members, &search);
202 entry != NULL;
203 entry = Hash_EnumNext(&search))
204 free(Hash_GetValue(entry));
205
206 free(a->name);
207 free(a->fnametab);
208 Hash_DeleteTable(&a->members);
209 free(a);
210 }
211 #endif
212
213
214
215 /*-
216 *-----------------------------------------------------------------------
217 * Arch_ParseArchive --
218 * Parse the archive specification in the given line and find/create
219 * the nodes for the specified archive members, placing their nodes
220 * on the given list.
221 *
222 * Input:
223 * linePtr Pointer to start of specification
224 * nodeLst Lst on which to place the nodes
225 * ctxt Context in which to expand variables
226 *
227 * Results:
228 * SUCCESS if it was a valid specification. The linePtr is updated
229 * to point to the first non-space after the archive spec. The
230 * nodes for the members are placed on the given list.
231 *
232 * Side Effects:
233 * Some nodes may be created. The given list is extended.
234 *
235 *-----------------------------------------------------------------------
236 */
237 ReturnStatus
238 Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
239 {
240 char *cp; /* Pointer into line */
241 GNode *gn; /* New node */
242 char *libName; /* Library-part of specification */
243 char *memName; /* Member-part of specification */
244 char *nameBuf; /* temporary place for node name */
245 char saveChar; /* Ending delimiter of member-name */
246 Boolean subLibName; /* TRUE if libName should have/had
247 * variable substitution performed on it */
248
249 libName = *linePtr;
250
251 subLibName = FALSE;
252
253 for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
254 if (*cp == '$') {
255 /*
256 * Variable spec, so call the Var module to parse the puppy
257 * so we can safely advance beyond it...
258 */
259 int length;
260 void *freeIt;
261 const char *result;
262
263 result = Var_Parse(cp, ctxt, VARE_UNDEFERR|VARE_WANTRES,
264 &length, &freeIt);
265 free(freeIt);
266
267 if (result == var_Error) {
268 return FAILURE;
269 } else {
270 subLibName = TRUE;
271 }
272
273 cp += length-1;
274 }
275 }
276
277 *cp++ = '\0';
278 if (subLibName) {
279 libName = Var_Subst(libName, ctxt, VARE_UNDEFERR|VARE_WANTRES);
280 }
281
282
283 for (;;) {
284 /*
285 * First skip to the start of the member's name, mark that
286 * place and skip to the end of it (either white-space or
287 * a close paren).
288 */
289 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
290
291 while (*cp != '\0' && *cp != ')' && isspace ((unsigned char)*cp)) {
292 cp++;
293 }
294 memName = cp;
295 while (*cp != '\0' && *cp != ')' && !isspace ((unsigned char)*cp)) {
296 if (*cp == '$') {
297 /*
298 * Variable spec, so call the Var module to parse the puppy
299 * so we can safely advance beyond it...
300 */
301 int length;
302 void *freeIt;
303 const char *result;
304
305 result = Var_Parse(cp, ctxt, VARE_UNDEFERR|VARE_WANTRES,
306 &length, &freeIt);
307 free(freeIt);
308
309 if (result == var_Error) {
310 return FAILURE;
311 } else {
312 doSubst = TRUE;
313 }
314
315 cp += length;
316 } else {
317 cp++;
318 }
319 }
320
321 /*
322 * If the specification ends without a closing parenthesis,
323 * chances are there's something wrong (like a missing backslash),
324 * so it's better to return failure than allow such things to happen
325 */
326 if (*cp == '\0') {
327 printf("No closing parenthesis in archive specification\n");
328 return FAILURE;
329 }
330
331 /*
332 * If we didn't move anywhere, we must be done
333 */
334 if (cp == memName) {
335 break;
336 }
337
338 saveChar = *cp;
339 *cp = '\0';
340
341 /*
342 * XXX: This should be taken care of intelligently by
343 * SuffExpandChildren, both for the archive and the member portions.
344 */
345 /*
346 * If member contains variables, try and substitute for them.
347 * This will slow down archive specs with dynamic sources, of course,
348 * since we'll be (non-)substituting them three times, but them's
349 * the breaks -- we need to do this since SuffExpandChildren calls
350 * us, otherwise we could assume the thing would be taken care of
351 * later.
352 */
353 if (doSubst) {
354 char *buf;
355 char *sacrifice;
356 char *oldMemName = memName;
357
358 memName = Var_Subst(memName, ctxt, VARE_UNDEFERR | VARE_WANTRES);
359
360 /*
361 * Now form an archive spec and recurse to deal with nested
362 * variables and multi-word variable values.... The results
363 * are just placed at the end of the nodeLst we're returning.
364 */
365 buf = sacrifice = str_concat4(libName, "(", memName, ")");
366
367 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
368 /*
369 * Must contain dynamic sources, so we can't deal with it now.
370 * Just create an ARCHV node for the thing and let
371 * SuffExpandChildren handle it...
372 */
373 gn = Targ_FindNode(buf, TARG_CREATE);
374
375 if (gn == NULL) {
376 free(buf);
377 return FAILURE;
378 } else {
379 gn->type |= OP_ARCHV;
380 Lst_AppendS(nodeLst, gn);
381 }
382 } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt)!=SUCCESS) {
383 /*
384 * Error in nested call -- free buffer and return FAILURE
385 * ourselves.
386 */
387 free(buf);
388 return FAILURE;
389 }
390 /*
391 * Free buffer and continue with our work.
392 */
393 free(buf);
394 } else if (Dir_HasWildcards(memName)) {
395 Lst members = Lst_Init();
396 char *member;
397 size_t sz = MAXPATHLEN, nsz;
398 nameBuf = bmake_malloc(sz);
399
400 Dir_Expand(memName, dirSearchPath, members);
401 while (!Lst_IsEmpty(members)) {
402 member = (char *)Lst_DeQueue(members);
403 nsz = strlen(libName) + strlen(member) + 3;
404 if (sz > nsz)
405 nameBuf = bmake_realloc(nameBuf, sz = nsz * 2);
406
407 snprintf(nameBuf, sz, "%s(%s)", libName, member);
408 free(member);
409 gn = Targ_FindNode(nameBuf, TARG_CREATE);
410 if (gn == NULL) {
411 free(nameBuf);
412 return FAILURE;
413 } else {
414 /*
415 * We've found the node, but have to make sure the rest of
416 * the world knows it's an archive member, without having
417 * to constantly check for parentheses, so we type the
418 * thing with the OP_ARCHV bit before we place it on the
419 * end of the provided list.
420 */
421 gn->type |= OP_ARCHV;
422 Lst_AppendS(nodeLst, gn);
423 }
424 }
425 Lst_Destroy(members, NULL);
426 free(nameBuf);
427 } else {
428 size_t sz = strlen(libName) + strlen(memName) + 3;
429 nameBuf = bmake_malloc(sz);
430 snprintf(nameBuf, sz, "%s(%s)", libName, memName);
431 gn = Targ_FindNode(nameBuf, TARG_CREATE);
432 free(nameBuf);
433 if (gn == NULL) {
434 return FAILURE;
435 } else {
436 /*
437 * We've found the node, but have to make sure the rest of the
438 * world knows it's an archive member, without having to
439 * constantly check for parentheses, so we type the thing with
440 * the OP_ARCHV bit before we place it on the end of the
441 * provided list.
442 */
443 gn->type |= OP_ARCHV;
444 Lst_AppendS(nodeLst, gn);
445 }
446 }
447 if (doSubst) {
448 free(memName);
449 }
450
451 *cp = saveChar;
452 }
453
454 /*
455 * If substituted libName, free it now, since we need it no longer.
456 */
457 if (subLibName) {
458 free(libName);
459 }
460
461 /*
462 * We promised the pointer would be set up at the next non-space, so
463 * we must advance cp there before setting *linePtr... (note that on
464 * entrance to the loop, cp is guaranteed to point at a ')')
465 */
466 do {
467 cp++;
468 } while (*cp != '\0' && isspace ((unsigned char)*cp));
469
470 *linePtr = cp;
471 return SUCCESS;
472 }
473
474 /*-
475 *-----------------------------------------------------------------------
476 * ArchFindArchive --
477 * See if the given archive is the one we are looking for. Called
478 * From ArchStatMember and ArchFindMember via Lst_Find.
479 *
480 * Input:
481 * ar Current list element
482 * archName Name we want
483 *
484 * Results:
485 * 0 if it is, non-zero if it isn't.
486 *
487 * Side Effects:
488 * None.
489 *
490 *-----------------------------------------------------------------------
491 */
492 static int
493 ArchFindArchive(const void *ar, const void *archName)
494 {
495 return strcmp(archName, ((const Arch *)ar)->name);
496 }
497
498 /*-
499 *-----------------------------------------------------------------------
500 * ArchStatMember --
501 * Locate a member of an archive, given the path of the archive and
502 * the path of the desired member.
503 *
504 * Input:
505 * archive Path to the archive
506 * member Name of member. If it is a path, only the last
507 * component is used.
508 * hash TRUE if archive should be hashed if not already so.
509 *
510 * Results:
511 * A pointer to the current struct ar_hdr structure for the member. Note
512 * That no position is returned, so this is not useful for touching
513 * archive members. This is mostly because we have no assurances that
514 * The archive will remain constant after we read all the headers, so
515 * there's not much point in remembering the position...
516 *
517 * Side Effects:
518 *
519 *-----------------------------------------------------------------------
520 */
521 static struct ar_hdr *
522 ArchStatMember(const char *archive, const char *member, Boolean hash)
523 {
524 #define AR_MAX_NAME_LEN (sizeof(arh.ar_name)-1)
525 FILE * arch; /* Stream to archive */
526 int size; /* Size of archive member */
527 char magic[SARMAG];
528 LstNode ln; /* Lst member containing archive descriptor */
529 Arch *ar; /* Archive descriptor */
530 Hash_Entry *he; /* Entry containing member's description */
531 struct ar_hdr arh; /* archive-member header for reading archive */
532 char memName[MAXPATHLEN+1];
533 /* Current member name while hashing. */
534
535 /*
536 * Because of space constraints and similar things, files are archived
537 * using their final path components, not the entire thing, so we need
538 * to point 'member' to the final component, if there is one, to make
539 * the comparisons easier...
540 */
541 const char *base = strrchr(member, '/');
542 if (base != NULL) {
543 member = base + 1;
544 }
545
546 ln = Lst_Find(archives, archive, ArchFindArchive);
547 if (ln != NULL) {
548 ar = (Arch *)Lst_Datum(ln);
549
550 he = Hash_FindEntry(&ar->members, member);
551
552 if (he != NULL) {
553 return (struct ar_hdr *)Hash_GetValue(he);
554 } else {
555 /* Try truncated name */
556 char copy[AR_MAX_NAME_LEN+1];
557 size_t len = strlen(member);
558
559 if (len > AR_MAX_NAME_LEN) {
560 len = AR_MAX_NAME_LEN;
561 snprintf(copy, sizeof copy, "%s", member);
562 }
563 if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
564 return (struct ar_hdr *)Hash_GetValue(he);
565 return NULL;
566 }
567 }
568
569 if (!hash) {
570 /*
571 * Caller doesn't want the thing hashed, just use ArchFindMember
572 * to read the header for the member out and close down the stream
573 * again. Since the archive is not to be hashed, we assume there's
574 * no need to allocate extra room for the header we're returning,
575 * so just declare it static.
576 */
577 static struct ar_hdr sarh;
578
579 arch = ArchFindMember(archive, member, &sarh, "r");
580
581 if (arch == NULL) {
582 return NULL;
583 } else {
584 fclose(arch);
585 return &sarh;
586 }
587 }
588
589 /*
590 * We don't have this archive on the list yet, so we want to find out
591 * everything that's in it and cache it so we can get at it quickly.
592 */
593 arch = fopen(archive, "r");
594 if (arch == NULL) {
595 return NULL;
596 }
597
598 /*
599 * We use the ARMAG string to make sure this is an archive we
600 * can handle...
601 */
602 if ((fread(magic, SARMAG, 1, arch) != 1) ||
603 (strncmp(magic, ARMAG, SARMAG) != 0)) {
604 fclose(arch);
605 return NULL;
606 }
607
608 ar = bmake_malloc(sizeof(Arch));
609 ar->name = bmake_strdup(archive);
610 ar->fnametab = NULL;
611 ar->fnamesize = 0;
612 Hash_InitTable(&ar->members, -1);
613 memName[AR_MAX_NAME_LEN] = '\0';
614
615 while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
616 if (strncmp( arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
617 /*
618 * The header is bogus, so the archive is bad
619 * and there's no way we can recover...
620 */
621 goto badarch;
622 } else {
623 char *nameend;
624
625 /*
626 * We need to advance the stream's pointer to the start of the
627 * next header. Files are padded with newlines to an even-byte
628 * boundary, so we need to extract the size of the file from the
629 * 'size' field of the header and round it up during the seek.
630 */
631 arh.ar_size[sizeof(arh.ar_size)-1] = '\0';
632 size = (int)strtol(arh.ar_size, NULL, 10);
633
634 memcpy(memName, arh.ar_name, sizeof(arh.ar_name));
635 nameend = memName + AR_MAX_NAME_LEN;
636 while (*nameend == ' ') {
637 nameend--;
638 }
639 nameend[1] = '\0';
640
641 #ifdef SVR4ARCHIVES
642 /*
643 * svr4 names are slash terminated. Also svr4 extended AR format.
644 */
645 if (memName[0] == '/') {
646 /*
647 * svr4 magic mode; handle it
648 */
649 switch (ArchSVR4Entry(ar, memName, size, arch)) {
650 case -1: /* Invalid data */
651 goto badarch;
652 case 0: /* List of files entry */
653 continue;
654 default: /* Got the entry */
655 break;
656 }
657 }
658 else {
659 if (nameend[0] == '/')
660 nameend[0] = '\0';
661 }
662 #endif
663
664 #ifdef AR_EFMT1
665 /*
666 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
667 * first <namelen> bytes of the file
668 */
669 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
670 isdigit((unsigned char)memName[sizeof(AR_EFMT1) - 1])) {
671
672 unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
673
674 if (elen > MAXPATHLEN)
675 goto badarch;
676 if (fread(memName, elen, 1, arch) != 1)
677 goto badarch;
678 memName[elen] = '\0';
679 if (fseek(arch, -elen, SEEK_CUR) != 0)
680 goto badarch;
681 if (DEBUG(ARCH) || DEBUG(MAKE)) {
682 fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName);
683 }
684 }
685 #endif
686
687 he = Hash_CreateEntry(&ar->members, memName, NULL);
688 Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr)));
689 memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
690 }
691 if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
692 goto badarch;
693 }
694
695 fclose(arch);
696
697 Lst_AppendS(archives, ar);
698
699 /*
700 * Now that the archive has been read and cached, we can look into
701 * the hash table to find the desired member's header.
702 */
703 he = Hash_FindEntry(&ar->members, member);
704
705 if (he != NULL) {
706 return (struct ar_hdr *)Hash_GetValue(he);
707 } else {
708 return NULL;
709 }
710
711 badarch:
712 fclose(arch);
713 Hash_DeleteTable(&ar->members);
714 free(ar->fnametab);
715 free(ar);
716 return NULL;
717 }
718
719 #ifdef SVR4ARCHIVES
720 /*-
721 *-----------------------------------------------------------------------
722 * ArchSVR4Entry --
723 * Parse an SVR4 style entry that begins with a slash.
724 * If it is "//", then load the table of filenames
725 * If it is "/<offset>", then try to substitute the long file name
726 * from offset of a table previously read.
727 *
728 * Results:
729 * -1: Bad data in archive
730 * 0: A table was loaded from the file
731 * 1: Name was successfully substituted from table
732 * 2: Name was not successfully substituted from table
733 *
734 * Side Effects:
735 * If a table is read, the file pointer is moved to the next archive
736 * member
737 *
738 *-----------------------------------------------------------------------
739 */
740 static int
741 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
742 {
743 #define ARLONGNAMES1 "//"
744 #define ARLONGNAMES2 "/ARFILENAMES"
745 size_t entry;
746 char *ptr, *eptr;
747
748 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
749 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
750
751 if (ar->fnametab != NULL) {
752 if (DEBUG(ARCH)) {
753 fprintf(debug_file, "Attempted to redefine an SVR4 name table\n");
754 }
755 return -1;
756 }
757
758 /*
759 * This is a table of archive names, so we build one for
760 * ourselves
761 */
762 ar->fnametab = bmake_malloc(size);
763 ar->fnamesize = size;
764
765 if (fread(ar->fnametab, size, 1, arch) != 1) {
766 if (DEBUG(ARCH)) {
767 fprintf(debug_file, "Reading an SVR4 name table failed\n");
768 }
769 return -1;
770 }
771 eptr = ar->fnametab + size;
772 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
773 switch (*ptr) {
774 case '/':
775 entry++;
776 *ptr = '\0';
777 break;
778
779 case '\n':
780 break;
781
782 default:
783 break;
784 }
785 if (DEBUG(ARCH)) {
786 fprintf(debug_file, "Found svr4 archive name table with %lu entries\n",
787 (unsigned long)entry);
788 }
789 return 0;
790 }
791
792 if (name[1] == ' ' || name[1] == '\0')
793 return 2;
794
795 entry = (size_t)strtol(&name[1], &eptr, 0);
796 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
797 if (DEBUG(ARCH)) {
798 fprintf(debug_file, "Could not parse SVR4 name %s\n", name);
799 }
800 return 2;
801 }
802 if (entry >= ar->fnamesize) {
803 if (DEBUG(ARCH)) {
804 fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n",
805 name, (unsigned long)ar->fnamesize);
806 }
807 return 2;
808 }
809
810 if (DEBUG(ARCH)) {
811 fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
812 }
813
814 snprintf(name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
815 return 1;
816 }
817 #endif
818
819
820 /*-
821 *-----------------------------------------------------------------------
822 * ArchFindMember --
823 * Locate a member of an archive, given the path of the archive and
824 * the path of the desired member. If the archive is to be modified,
825 * the mode should be "r+", if not, it should be "r".
826 *
827 * Input:
828 * archive Path to the archive
829 * member Name of member. If it is a path, only the last
830 * component is used.
831 * arhPtr Pointer to header structure to be filled in
832 * mode The mode for opening the stream
833 *
834 * Results:
835 * An FILE *, opened for reading and writing, positioned at the
836 * start of the member's struct ar_hdr, or NULL if the member was
837 * nonexistent. The current struct ar_hdr for member.
838 *
839 * Side Effects:
840 * The passed struct ar_hdr structure is filled in.
841 *
842 *-----------------------------------------------------------------------
843 */
844 static FILE *
845 ArchFindMember(const char *archive, const char *member, struct ar_hdr *arhPtr,
846 const char *mode)
847 {
848 FILE * arch; /* Stream to archive */
849 int size; /* Size of archive member */
850 char magic[SARMAG];
851 size_t len, tlen;
852 const char * base;
853
854 arch = fopen(archive, mode);
855 if (arch == NULL) {
856 return NULL;
857 }
858
859 /*
860 * We use the ARMAG string to make sure this is an archive we
861 * can handle...
862 */
863 if ((fread(magic, SARMAG, 1, arch) != 1) ||
864 (strncmp(magic, ARMAG, SARMAG) != 0)) {
865 fclose(arch);
866 return NULL;
867 }
868
869 /*
870 * Because of space constraints and similar things, files are archived
871 * using their final path components, not the entire thing, so we need
872 * to point 'member' to the final component, if there is one, to make
873 * the comparisons easier...
874 */
875 base = strrchr(member, '/');
876 if (base != NULL) {
877 member = base + 1;
878 }
879 len = tlen = strlen(member);
880 if (len > sizeof(arhPtr->ar_name)) {
881 tlen = sizeof(arhPtr->ar_name);
882 }
883
884 while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
885 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
886 /*
887 * The header is bogus, so the archive is bad
888 * and there's no way we can recover...
889 */
890 fclose(arch);
891 return NULL;
892 } else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
893 /*
894 * If the member's name doesn't take up the entire 'name' field,
895 * we have to be careful of matching prefixes. Names are space-
896 * padded to the right, so if the character in 'name' at the end
897 * of the matched string is anything but a space, this isn't the
898 * member we sought.
899 */
900 if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
901 goto skip;
902 } else {
903 /*
904 * To make life easier, we reposition the file at the start
905 * of the header we just read before we return the stream.
906 * In a more general situation, it might be better to leave
907 * the file at the actual member, rather than its header, but
908 * not here...
909 */
910 if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) != 0) {
911 fclose(arch);
912 return NULL;
913 }
914 return arch;
915 }
916 } else
917 #ifdef AR_EFMT1
918 /*
919 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
920 * first <namelen> bytes of the file
921 */
922 if (strncmp(arhPtr->ar_name, AR_EFMT1,
923 sizeof(AR_EFMT1) - 1) == 0 &&
924 isdigit((unsigned char)arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
925
926 unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
927 char ename[MAXPATHLEN + 1];
928
929 if (elen > MAXPATHLEN) {
930 fclose(arch);
931 return NULL;
932 }
933 if (fread(ename, elen, 1, arch) != 1) {
934 fclose(arch);
935 return NULL;
936 }
937 ename[elen] = '\0';
938 if (DEBUG(ARCH) || DEBUG(MAKE)) {
939 fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename);
940 }
941 if (strncmp(ename, member, len) == 0) {
942 /* Found as extended name */
943 if (fseek(arch, -sizeof(struct ar_hdr) - elen,
944 SEEK_CUR) != 0) {
945 fclose(arch);
946 return NULL;
947 }
948 return arch;
949 }
950 if (fseek(arch, -elen, SEEK_CUR) != 0) {
951 fclose(arch);
952 return NULL;
953 }
954 goto skip;
955 } else
956 #endif
957 {
958 skip:
959 /*
960 * This isn't the member we're after, so we need to advance the
961 * stream's pointer to the start of the next header. Files are
962 * padded with newlines to an even-byte boundary, so we need to
963 * extract the size of the file from the 'size' field of the
964 * header and round it up during the seek.
965 */
966 arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
967 size = (int)strtol(arhPtr->ar_size, NULL, 10);
968 if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
969 fclose(arch);
970 return NULL;
971 }
972 }
973 }
974
975 /*
976 * We've looked everywhere, but the member is not to be found. Close the
977 * archive and return NULL -- an error.
978 */
979 fclose(arch);
980 return NULL;
981 }
982
983 /*-
984 *-----------------------------------------------------------------------
985 * Arch_Touch --
986 * Touch a member of an archive.
987 *
988 * Input:
989 * gn Node of member to touch
990 *
991 * Results:
992 * The 'time' field of the member's header is updated.
993 *
994 * Side Effects:
995 * The modification time of the entire archive is also changed.
996 * For a library, this could necessitate the re-ranlib'ing of the
997 * whole thing.
998 *
999 *-----------------------------------------------------------------------
1000 */
1001 void
1002 Arch_Touch(GNode *gn)
1003 {
1004 FILE * arch; /* Stream open to archive, positioned properly */
1005 struct ar_hdr arh; /* Current header describing member */
1006 char *p1, *p2;
1007
1008 arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
1009 Var_Value(MEMBER, gn, &p2),
1010 &arh, "r+");
1011
1012 bmake_free(p1);
1013 bmake_free(p2);
1014
1015 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
1016
1017 if (arch != NULL) {
1018 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
1019 fclose(arch);
1020 }
1021 }
1022
1023 /*-
1024 *-----------------------------------------------------------------------
1025 * Arch_TouchLib --
1026 * Given a node which represents a library, touch the thing, making
1027 * sure that the table of contents also is touched.
1028 *
1029 * Input:
1030 * gn The node of the library to touch
1031 *
1032 * Results:
1033 * None.
1034 *
1035 * Side Effects:
1036 * Both the modification time of the library and of the RANLIBMAG
1037 * member are set to 'now'.
1038 *
1039 *-----------------------------------------------------------------------
1040 */
1041 void
1042 #if !defined(RANLIBMAG)
1043 Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
1044 #else
1045 Arch_TouchLib(GNode *gn)
1046 #endif
1047 {
1048 #ifdef RANLIBMAG
1049 FILE * arch; /* Stream open to archive */
1050 struct ar_hdr arh; /* Header describing table of contents */
1051 struct utimbuf times; /* Times for utime() call */
1052
1053 arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
1054 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
1055
1056 if (arch != NULL) {
1057 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
1058 fclose(arch);
1059
1060 times.actime = times.modtime = now;
1061 utime(gn->path, ×);
1062 }
1063 #endif
1064 }
1065
1066 /*-
1067 *-----------------------------------------------------------------------
1068 * Arch_MTime --
1069 * Return the modification time of a member of an archive.
1070 *
1071 * Input:
1072 * gn Node describing archive member
1073 *
1074 * Results:
1075 * The modification time(seconds).
1076 *
1077 * Side Effects:
1078 * The mtime field of the given node is filled in with the value
1079 * returned by the function.
1080 *
1081 *-----------------------------------------------------------------------
1082 */
1083 time_t
1084 Arch_MTime(GNode *gn)
1085 {
1086 struct ar_hdr *arhPtr; /* Header of desired member */
1087 time_t modTime; /* Modification time as an integer */
1088 char *p1, *p2;
1089
1090 arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
1091 Var_Value(MEMBER, gn, &p2),
1092 TRUE);
1093
1094 bmake_free(p1);
1095 bmake_free(p2);
1096
1097 if (arhPtr != NULL) {
1098 modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
1099 } else {
1100 modTime = 0;
1101 }
1102
1103 gn->mtime = modTime;
1104 return modTime;
1105 }
1106
1107 /*-
1108 *-----------------------------------------------------------------------
1109 * Arch_MemMTime --
1110 * Given a non-existent archive member's node, get its modification
1111 * time from its archived form, if it exists.
1112 *
1113 * Results:
1114 * The modification time.
1115 *
1116 * Side Effects:
1117 * The mtime field is filled in.
1118 *
1119 *-----------------------------------------------------------------------
1120 */
1121 time_t
1122 Arch_MemMTime(GNode *gn)
1123 {
1124 LstNode ln;
1125 GNode *pgn;
1126 char *nameStart,
1127 *nameEnd;
1128
1129 if (Lst_Open(gn->parents) != SUCCESS) {
1130 gn->mtime = 0;
1131 return 0;
1132 }
1133 while ((ln = Lst_NextS(gn->parents)) != NULL) {
1134 pgn = (GNode *)Lst_Datum(ln);
1135
1136 if (pgn->type & OP_ARCHV) {
1137 /*
1138 * If the parent is an archive specification and is being made
1139 * and its member's name matches the name of the node we were
1140 * given, record the modification time of the parent in the
1141 * child. We keep searching its parents in case some other
1142 * parent requires this child to exist...
1143 */
1144 nameStart = strchr(pgn->name, '(') + 1;
1145 nameEnd = strchr(nameStart, ')');
1146
1147 if ((pgn->flags & REMAKE) &&
1148 strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
1149 gn->mtime = Arch_MTime(pgn);
1150 }
1151 } else if (pgn->flags & REMAKE) {
1152 /*
1153 * Something which isn't a library depends on the existence of
1154 * this target, so it needs to exist.
1155 */
1156 gn->mtime = 0;
1157 break;
1158 }
1159 }
1160
1161 Lst_CloseS(gn->parents);
1162
1163 return gn->mtime;
1164 }
1165
1166 /*-
1167 *-----------------------------------------------------------------------
1168 * Arch_FindLib --
1169 * Search for a library along the given search path.
1170 *
1171 * Input:
1172 * gn Node of library to find
1173 * path Search path
1174 *
1175 * Results:
1176 * None.
1177 *
1178 * Side Effects:
1179 * The node's 'path' field is set to the found path (including the
1180 * actual file name, not -l...). If the system can handle the -L
1181 * flag when linking (or we cannot find the library), we assume that
1182 * the user has placed the .LIBRARIES variable in the final linking
1183 * command (or the linker will know where to find it) and set the
1184 * TARGET variable for this node to be the node's name. Otherwise,
1185 * we set the TARGET variable to be the full path of the library,
1186 * as returned by Dir_FindFile.
1187 *
1188 *-----------------------------------------------------------------------
1189 */
1190 void
1191 Arch_FindLib(GNode *gn, Lst path)
1192 {
1193 char *libName; /* file name for archive */
1194 size_t sz = strlen(gn->name) + 6 - 2;
1195
1196 libName = bmake_malloc(sz);
1197 snprintf(libName, sz, "lib%s.a", &gn->name[2]);
1198
1199 gn->path = Dir_FindFile(libName, path);
1200
1201 free(libName);
1202
1203 #ifdef LIBRARIES
1204 Var_Set(TARGET, gn->name, gn);
1205 #else
1206 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
1207 #endif /* LIBRARIES */
1208 }
1209
1210 /*-
1211 *-----------------------------------------------------------------------
1212 * Arch_LibOODate --
1213 * Decide if a node with the OP_LIB attribute is out-of-date. Called
1214 * from Make_OODate to make its life easier.
1215 *
1216 * There are several ways for a library to be out-of-date that are
1217 * not available to ordinary files. In addition, there are ways
1218 * that are open to regular files that are not available to
1219 * libraries. A library that is only used as a source is never
1220 * considered out-of-date by itself. This does not preclude the
1221 * library's modification time from making its parent be out-of-date.
1222 * A library will be considered out-of-date for any of these reasons,
1223 * given that it is a target on a dependency line somewhere:
1224 * Its modification time is less than that of one of its
1225 * sources (gn->mtime < gn->cmgn->mtime).
1226 * Its modification time is greater than the time at which the
1227 * make began (i.e. it's been modified in the course
1228 * of the make, probably by archiving).
1229 * The modification time of one of its sources is greater than
1230 * the one of its RANLIBMAG member (i.e. its table of contents
1231 * is out-of-date). We don't compare of the archive time
1232 * vs. TOC time because they can be too close. In my
1233 * opinion we should not bother with the TOC at all since
1234 * this is used by 'ar' rules that affect the data contents
1235 * of the archive, not by ranlib rules, which affect the
1236 * TOC.
1237 *
1238 * Input:
1239 * gn The library's graph node
1240 *
1241 * Results:
1242 * TRUE if the library is out-of-date. FALSE otherwise.
1243 *
1244 * Side Effects:
1245 * The library will be hashed if it hasn't been already.
1246 *
1247 *-----------------------------------------------------------------------
1248 */
1249 Boolean
1250 Arch_LibOODate(GNode *gn)
1251 {
1252 Boolean oodate;
1253
1254 if (gn->type & OP_PHONY) {
1255 oodate = TRUE;
1256 } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
1257 oodate = FALSE;
1258 } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) ||
1259 (gn->mtime > now) ||
1260 (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) {
1261 oodate = TRUE;
1262 } else {
1263 #ifdef RANLIBMAG
1264 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */
1265 int modTimeTOC; /* The table-of-contents's mod time */
1266
1267 arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
1268
1269 if (arhPtr != NULL) {
1270 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
1271
1272 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1273 fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1274 }
1275 oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC);
1276 } else {
1277 /*
1278 * A library w/o a table of contents is out-of-date
1279 */
1280 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1281 fprintf(debug_file, "No t.o.c....");
1282 }
1283 oodate = TRUE;
1284 }
1285 #else
1286 oodate = FALSE;
1287 #endif
1288 }
1289 return oodate;
1290 }
1291
1292 /*-
1293 *-----------------------------------------------------------------------
1294 * Arch_Init --
1295 * Initialize things for this module.
1296 *
1297 * Results:
1298 * None.
1299 *
1300 * Side Effects:
1301 * The 'archives' list is initialized.
1302 *
1303 *-----------------------------------------------------------------------
1304 */
1305 void
1306 Arch_Init(void)
1307 {
1308 archives = Lst_Init();
1309 }
1310
1311
1312
1313 /*-
1314 *-----------------------------------------------------------------------
1315 * Arch_End --
1316 * Cleanup things for this module.
1317 *
1318 * Results:
1319 * None.
1320 *
1321 * Side Effects:
1322 * The 'archives' list is freed
1323 *
1324 *-----------------------------------------------------------------------
1325 */
1326 void
1327 Arch_End(void)
1328 {
1329 #ifdef CLEANUP
1330 Lst_Destroy(archives, ArchFree);
1331 #endif
1332 }
1333
1334 /*-
1335 *-----------------------------------------------------------------------
1336 * Arch_IsLib --
1337 * Check if the node is a library
1338 *
1339 * Results:
1340 * True or False.
1341 *
1342 * Side Effects:
1343 * None.
1344 *
1345 *-----------------------------------------------------------------------
1346 */
1347 int
1348 Arch_IsLib(GNode *gn)
1349 {
1350 static const char armag[] = "!<arch>\n";
1351 char buf[sizeof(armag)-1];
1352 int fd;
1353
1354 if ((fd = open(gn->path, O_RDONLY)) == -1)
1355 return FALSE;
1356
1357 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
1358 (void)close(fd);
1359 return FALSE;
1360 }
1361
1362 (void)close(fd);
1363
1364 return memcmp(buf, armag, sizeof(buf)) == 0;
1365 }
1366