xref: /netbsd/usr.bin/make/arch.c (revision 299b777e)
1 /*	$NetBSD: arch.c,v 1.213 2023/02/14 21:08: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 /*
72  * Manipulate libraries, archives and their members.
73  *
74  * The first time an archive is referenced, all of its members' headers are
75  * read and cached and the archive closed again.  All cached archives are kept
76  * on a list which is searched each time an archive member is referenced.
77  *
78  * The interface to this module is:
79  *
80  *	Arch_Init	Initialize this module.
81  *
82  *	Arch_End	Clean up this module.
83  *
84  *	Arch_ParseArchive
85  *			Parse an archive specification such as
86  *			"archive.a(member1 member2)".
87  *
88  *	Arch_Touch	Alter the modification time of the archive
89  *			member described by the given node to be
90  *			the time when make was started.
91  *
92  *	Arch_TouchLib	Update the modification time of the library
93  *			described by the given node. This is special
94  *			because it also updates the modification time
95  *			of the library's table of contents.
96  *
97  *	Arch_UpdateMTime
98  *			Find the modification time of a member of
99  *			an archive *in the archive* and place it in the
100  *			member's GNode.
101  *
102  *	Arch_UpdateMemberMTime
103  *			Find the modification time of a member of
104  *			an archive. Called when the member doesn't
105  *			already exist. Looks in the archive for the
106  *			modification time. Returns the modification
107  *			time.
108  *
109  *	Arch_FindLib	Search for a library along a path. The
110  *			library name in the GNode should be in
111  *			-l<name> format.
112  *
113  *	Arch_LibOODate	Decide if a library node is out-of-date.
114  */
115 
116 #include <sys/types.h>
117 #include <sys/stat.h>
118 #include <sys/time.h>
119 #include <sys/param.h>
120 
121 #include <ar.h>
122 #include <utime.h>
123 
124 #include "make.h"
125 #include "dir.h"
126 #include "config.h"
127 
128 /*	"@(#)arch.c	8.2 (Berkeley) 1/2/94"	*/
129 MAKE_RCSID("$NetBSD: arch.c,v 1.213 2023/02/14 21:08:00 rillig Exp $");
130 
131 typedef struct List ArchList;
132 typedef struct ListNode ArchListNode;
133 
134 static ArchList archives;	/* The archives we've already examined */
135 
136 typedef struct Arch {
137 	char *name;		/* Name of archive */
138 	HashTable members;	/* All the members of the archive described
139 				 * by <name, struct ar_hdr *> key/value pairs */
140 	char *fnametab;		/* Extended name table strings */
141 	size_t fnamesize;	/* Size of the string table */
142 } Arch;
143 
144 static FILE *ArchFindMember(const char *, const char *,
145 			    struct ar_hdr *, const char *);
146 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
147 #define SVR4ARCHIVES
148 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
149 #endif
150 
151 
152 #ifdef CLEANUP
153 static void
ArchFree(void * ap)154 ArchFree(void *ap)
155 {
156 	Arch *a = ap;
157 	HashIter hi;
158 
159 	/* Free memory from hash entries */
160 	HashIter_Init(&hi, &a->members);
161 	while (HashIter_Next(&hi) != NULL)
162 		free(hi.entry->value);
163 
164 	free(a->name);
165 	free(a->fnametab);
166 	HashTable_Done(&a->members);
167 	free(a);
168 }
169 #endif
170 
171 /* Return "archive(member)". */
172 static char *
FullName(const char * archive,const char * member)173 FullName(const char *archive, const char *member)
174 {
175 	size_t len1 = strlen(archive);
176 	size_t len3 = strlen(member);
177 	char *result = bmake_malloc(len1 + 1 + len3 + 1 + 1);
178 	memcpy(result, archive, len1);
179 	memcpy(result + len1, "(", 1);
180 	memcpy(result + len1 + 1, member, len3);
181 	memcpy(result + len1 + 1 + len3, ")", 1 + 1);
182 	return result;
183 }
184 
185 /*
186  * Parse an archive specification such as "archive.a(member1 member2.${EXT})",
187  * adding nodes for the expanded members to gns.  Nodes are created as
188  * necessary.
189  *
190  * Input:
191  *	pp		The start of the specification.
192  *	gns		The list on which to place the nodes.
193  *	scope		The scope in which to expand variables.
194  *
195  * Output:
196  *	return		True if it was a valid specification.
197  *	*pp		Points to the first non-space after the archive spec.
198  */
199 bool
Arch_ParseArchive(char ** pp,GNodeList * gns,GNode * scope)200 Arch_ParseArchive(char **pp, GNodeList *gns, GNode *scope)
201 {
202 	char *spec;		/* For modifying some bytes of *pp */
203 	const char *cp;		/* Pointer into line */
204 	GNode *gn;		/* New node */
205 	FStr lib;		/* Library-part of specification */
206 	FStr mem;		/* Member-part of specification */
207 	char saveChar;		/* Ending delimiter of member-name */
208 	bool expandLib;		/* Whether the parsed lib contains variable
209 				 * expressions that need to be expanded */
210 
211 	spec = *pp;
212 	lib = FStr_InitRefer(spec);
213 	expandLib = false;
214 
215 	for (cp = lib.str; *cp != '(' && *cp != '\0';) {
216 		if (*cp == '$') {
217 			/* Expand nested variable expressions. */
218 			/* XXX: This code can probably be shortened. */
219 			const char *nested_p = cp;
220 			FStr result;
221 			bool isError;
222 
223 			/* XXX: is expanded twice: once here and once below */
224 			result = Var_Parse(&nested_p, scope, VARE_UNDEFERR);
225 			/* TODO: handle errors */
226 			isError = result.str == var_Error;
227 			FStr_Done(&result);
228 			if (isError)
229 				return false;
230 
231 			expandLib = true;
232 			cp += nested_p - cp;
233 		} else
234 			cp++;
235 	}
236 
237 	spec[cp++ - spec] = '\0';
238 	if (expandLib)
239 		Var_Expand(&lib, scope, VARE_UNDEFERR);
240 
241 	for (;;) {
242 		/*
243 		 * First skip to the start of the member's name, mark that
244 		 * place and skip to the end of it (either white-space or
245 		 * a close paren).
246 		 */
247 		bool doSubst = false;
248 
249 		cpp_skip_whitespace(&cp);
250 
251 		mem = FStr_InitRefer(cp);
252 		while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) {
253 			if (*cp == '$') {
254 				/* Expand nested variable expressions. */
255 				/*
256 				 * XXX: This code can probably be shortened.
257 				 */
258 				FStr result;
259 				bool isError;
260 				const char *nested_p = cp;
261 
262 				result = Var_Parse(&nested_p, scope,
263 				    VARE_UNDEFERR);
264 				/* TODO: handle errors */
265 				isError = result.str == var_Error;
266 				FStr_Done(&result);
267 
268 				if (isError)
269 					return false;
270 
271 				doSubst = true;
272 				cp += nested_p - cp;
273 			} else {
274 				cp++;
275 			}
276 		}
277 
278 		/*
279 		 * If the specification ends without a closing parenthesis,
280 		 * chances are there's something wrong (like a missing
281 		 * backslash), so it's better to return failure than allow
282 		 * such things to happen
283 		 */
284 		if (*cp == '\0') {
285 			Parse_Error(PARSE_FATAL,
286 			    "No closing parenthesis "
287 			    "in archive specification");
288 			return false;
289 		}
290 
291 		/*
292 		 * If we didn't move anywhere, we must be done
293 		 */
294 		if (cp == mem.str)
295 			break;
296 
297 		saveChar = *cp;
298 		spec[cp - spec] = '\0';
299 
300 		/*
301 		 * XXX: This should be taken care of intelligently by
302 		 * SuffExpandChildren, both for the archive and the member
303 		 * portions.
304 		 */
305 		/*
306 		 * If member contains variables, try and substitute for them.
307 		 * This slows down archive specs with dynamic sources, since
308 		 * they are (non-)substituted three times, but we need to do
309 		 * this since SuffExpandChildren calls us, otherwise we could
310 		 * assume the substitutions would be taken care of later.
311 		 */
312 		if (doSubst) {
313 			char *fullName;
314 			char *p;
315 			const char *unexpandedMem = mem.str;
316 
317 			Var_Expand(&mem, scope, VARE_UNDEFERR);
318 
319 			/*
320 			 * Now form an archive spec and recurse to deal with
321 			 * nested variables and multi-word variable values.
322 			 */
323 			fullName = FullName(lib.str, mem.str);
324 			p = fullName;
325 
326 			if (strcmp(mem.str, unexpandedMem) == 0) {
327 				/*
328 				 * Must contain dynamic sources, so we can't
329 				 * deal with it now. Just create an ARCHV node
330 				 * for the thing and let SuffExpandChildren
331 				 * handle it.
332 				 */
333 				gn = Targ_GetNode(fullName);
334 				gn->type |= OP_ARCHV;
335 				Lst_Append(gns, gn);
336 
337 			} else if (!Arch_ParseArchive(&p, gns, scope)) {
338 				/* Error in nested call. */
339 				free(fullName);
340 				/* XXX: does unexpandedMemName leak? */
341 				return false;
342 			}
343 			free(fullName);
344 			/* XXX: does unexpandedMemName leak? */
345 
346 		} else if (Dir_HasWildcards(mem.str)) {
347 			StringList members = LST_INIT;
348 			SearchPath_Expand(&dirSearchPath, mem.str, &members);
349 
350 			while (!Lst_IsEmpty(&members)) {
351 				char *member = Lst_Dequeue(&members);
352 				char *fullname = FullName(lib.str, member);
353 				free(member);
354 
355 				gn = Targ_GetNode(fullname);
356 				free(fullname);
357 
358 				gn->type |= OP_ARCHV;
359 				Lst_Append(gns, gn);
360 			}
361 			Lst_Done(&members);
362 
363 		} else {
364 			char *fullname = FullName(lib.str, mem.str);
365 			gn = Targ_GetNode(fullname);
366 			free(fullname);
367 
368 			/*
369 			 * We've found the node, but have to make sure the
370 			 * rest of the world knows it's an archive member,
371 			 * without having to constantly check for parentheses,
372 			 * so we type the thing with the OP_ARCHV bit before
373 			 * we place it on the end of the provided list.
374 			 */
375 			gn->type |= OP_ARCHV;
376 			Lst_Append(gns, gn);
377 		}
378 		FStr_Done(&mem);
379 
380 		spec[cp - spec] = saveChar;
381 	}
382 
383 	FStr_Done(&lib);
384 
385 	cp++;			/* skip the ')' */
386 	/* We promised that pp would be set up at the next non-space. */
387 	cpp_skip_whitespace(&cp);
388 	*pp += cp - *pp;
389 	return true;
390 }
391 
392 /*
393  * Locate a member of an archive, given the path of the archive and the path
394  * of the desired member.
395  *
396  * Input:
397  *	archive		Path to the archive
398  *	member		Name of member; only its basename is used.
399  *	addToCache	True if archive should be cached if not already so.
400  *
401  * Results:
402  *	The ar_hdr for the member, or NULL.
403  *
404  * See ArchFindMember for an almost identical copy of this code.
405  */
406 static struct ar_hdr *
ArchStatMember(const char * archive,const char * member,bool addToCache)407 ArchStatMember(const char *archive, const char *member, bool addToCache)
408 {
409 #define AR_MAX_NAME_LEN (sizeof arh.ar_name - 1)
410 	FILE *arch;
411 	size_t size;		/* Size of archive member */
412 	char magic[SARMAG];
413 	ArchListNode *ln;
414 	Arch *ar;		/* Archive descriptor */
415 	struct ar_hdr arh;	/* archive-member header for reading archive */
416 	char memName[MAXPATHLEN + 1];
417 	/* Current member name while hashing. */
418 
419 	/*
420 	 * Because of space constraints and similar things, files are archived
421 	 * using their basename, not the entire path.
422 	 */
423 	member = str_basename(member);
424 
425 	for (ln = archives.first; ln != NULL; ln = ln->next) {
426 		const Arch *a = ln->datum;
427 		if (strcmp(a->name, archive) == 0)
428 			break;
429 	}
430 
431 	if (ln != NULL) {
432 		struct ar_hdr *hdr;
433 
434 		ar = ln->datum;
435 		hdr = HashTable_FindValue(&ar->members, member);
436 		if (hdr != NULL)
437 			return hdr;
438 
439 		{
440 			/* Try truncated name */
441 			char copy[AR_MAX_NAME_LEN + 1];
442 			size_t len = strlen(member);
443 
444 			if (len > AR_MAX_NAME_LEN) {
445 				snprintf(copy, sizeof copy, "%s", member);
446 				hdr = HashTable_FindValue(&ar->members, copy);
447 			}
448 			return hdr;
449 		}
450 	}
451 
452 	if (!addToCache) {
453 		/*
454 		 * Caller doesn't want the thing cached, just use
455 		 * ArchFindMember to read the header for the member out and
456 		 * close down the stream again. Since the archive is not to be
457 		 * cached, we assume there's no need to allocate extra room
458 		 * for the header we're returning, so just declare it static.
459 		 */
460 		static struct ar_hdr sarh;
461 
462 		arch = ArchFindMember(archive, member, &sarh, "r");
463 		if (arch == NULL)
464 			return NULL;
465 
466 		fclose(arch);
467 		return &sarh;
468 	}
469 
470 	/*
471 	 * We don't have this archive on the list yet, so we want to find out
472 	 * everything that's in it and cache it so we can get at it quickly.
473 	 */
474 	arch = fopen(archive, "r");
475 	if (arch == NULL)
476 		return NULL;
477 
478 	/*
479 	 * We use the ARMAG string to make sure this is an archive we
480 	 * can handle...
481 	 */
482 	if (fread(magic, SARMAG, 1, arch) != 1 ||
483 	    strncmp(magic, ARMAG, SARMAG) != 0) {
484 		(void)fclose(arch);
485 		return NULL;
486 	}
487 
488 	ar = bmake_malloc(sizeof *ar);
489 	ar->name = bmake_strdup(archive);
490 	ar->fnametab = NULL;
491 	ar->fnamesize = 0;
492 	HashTable_Init(&ar->members);
493 	memName[AR_MAX_NAME_LEN] = '\0';
494 
495 	while (fread(&arh, sizeof arh, 1, arch) == 1) {
496 		char *nameend;
497 
498 		/* If the header is bogus, there's no way we can recover. */
499 		if (strncmp(arh.ar_fmag, ARFMAG, sizeof arh.ar_fmag) != 0)
500 			goto badarch;
501 
502 		/*
503 		 * We need to advance the stream's pointer to the start of the
504 		 * next header. Files are padded with newlines to an even-byte
505 		 * boundary, so we need to extract the size of the file from
506 		 * the 'size' field of the header and round it up during the
507 		 * seek.
508 		 */
509 		arh.ar_size[sizeof arh.ar_size - 1] = '\0';
510 		size = (size_t)strtol(arh.ar_size, NULL, 10);
511 
512 		memcpy(memName, arh.ar_name, sizeof arh.ar_name);
513 		nameend = memName + AR_MAX_NAME_LEN;
514 		while (nameend > memName && *nameend == ' ')
515 			nameend--;
516 		nameend[1] = '\0';
517 
518 #ifdef SVR4ARCHIVES
519 		/*
520 		 * svr4 names are slash-terminated.
521 		 * Also svr4 extended the AR format.
522 		 */
523 		if (memName[0] == '/') {
524 			/* svr4 magic mode; handle it */
525 			switch (ArchSVR4Entry(ar, memName, size, arch)) {
526 			case -1:	/* Invalid data */
527 				goto badarch;
528 			case 0:		/* List of files entry */
529 				continue;
530 			default:	/* Got the entry */
531 				break;
532 			}
533 		} else {
534 			if (nameend[0] == '/')
535 				nameend[0] = '\0';
536 		}
537 #endif
538 
539 #ifdef AR_EFMT1
540 		/*
541 		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
542 		 * first <namelen> bytes of the file
543 		 */
544 		if (strncmp(memName, AR_EFMT1, sizeof AR_EFMT1 - 1) == 0 &&
545 		    ch_isdigit(memName[sizeof AR_EFMT1 - 1])) {
546 
547 			size_t elen = (size_t)atoi(
548 			    memName + sizeof AR_EFMT1 - 1);
549 
550 			if (elen > MAXPATHLEN)
551 				goto badarch;
552 			if (fread(memName, elen, 1, arch) != 1)
553 				goto badarch;
554 			memName[elen] = '\0';
555 			if (fseek(arch, -(long)elen, SEEK_CUR) != 0)
556 				goto badarch;
557 			if (DEBUG(ARCH) || DEBUG(MAKE))
558 				debug_printf(
559 				    "ArchStatMember: "
560 				    "Extended format entry for %s\n",
561 				    memName);
562 		}
563 #endif
564 
565 		{
566 			struct ar_hdr *cached_hdr = bmake_malloc(
567 			    sizeof *cached_hdr);
568 			memcpy(cached_hdr, &arh, sizeof arh);
569 			HashTable_Set(&ar->members, memName, cached_hdr);
570 		}
571 
572 		if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
573 			goto badarch;
574 	}
575 
576 	fclose(arch);
577 
578 	Lst_Append(&archives, ar);
579 
580 	/*
581 	 * Now that the archive has been read and cached, we can look into
582 	 * the addToCache table to find the desired member's header.
583 	 */
584 	return HashTable_FindValue(&ar->members, member);
585 
586 badarch:
587 	fclose(arch);
588 	HashTable_Done(&ar->members);
589 	free(ar->fnametab);
590 	free(ar);
591 	return NULL;
592 }
593 
594 #ifdef SVR4ARCHIVES
595 /*
596  * Parse an SVR4 style entry that begins with a slash.
597  * If it is "//", then load the table of filenames.
598  * If it is "/<offset>", then try to substitute the long file name
599  * from offset of a table previously read.
600  * If a table is read, the file pointer is moved to the next archive member.
601  *
602  * Results:
603  *	-1: Bad data in archive
604  *	 0: A table was loaded from the file
605  *	 1: Name was successfully substituted from table
606  *	 2: Name was not successfully substituted from table
607  */
608 static int
ArchSVR4Entry(Arch * ar,char * inout_name,size_t size,FILE * arch)609 ArchSVR4Entry(Arch *ar, char *inout_name, size_t size, FILE *arch)
610 {
611 #define ARLONGNAMES1 "//"
612 #define ARLONGNAMES2 "/ARFILENAMES"
613 	size_t entry;
614 	char *ptr, *eptr;
615 
616 	if (strncmp(inout_name, ARLONGNAMES1, sizeof ARLONGNAMES1 - 1) == 0 ||
617 	    strncmp(inout_name, ARLONGNAMES2, sizeof ARLONGNAMES2 - 1) == 0) {
618 
619 		if (ar->fnametab != NULL) {
620 			DEBUG0(ARCH,
621 			    "Attempted to redefine an SVR4 name table\n");
622 			return -1;
623 		}
624 
625 		/*
626 		 * This is a table of archive names, so we build one for
627 		 * ourselves
628 		 */
629 		ar->fnametab = bmake_malloc(size);
630 		ar->fnamesize = size;
631 
632 		if (fread(ar->fnametab, size, 1, arch) != 1) {
633 			DEBUG0(ARCH, "Reading an SVR4 name table failed\n");
634 			return -1;
635 		}
636 		eptr = ar->fnametab + size;
637 		for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
638 			if (*ptr == '/') {
639 				entry++;
640 				*ptr = '\0';
641 			}
642 		DEBUG1(ARCH,
643 		    "Found svr4 archive name table with %lu entries\n",
644 		    (unsigned long)entry);
645 		return 0;
646 	}
647 
648 	if (inout_name[1] == ' ' || inout_name[1] == '\0')
649 		return 2;
650 
651 	entry = (size_t)strtol(&inout_name[1], &eptr, 0);
652 	if ((*eptr != ' ' && *eptr != '\0') || eptr == &inout_name[1]) {
653 		DEBUG1(ARCH, "Could not parse SVR4 name %s\n", inout_name);
654 		return 2;
655 	}
656 	if (entry >= ar->fnamesize) {
657 		DEBUG2(ARCH, "SVR4 entry offset %s is greater than %lu\n",
658 		    inout_name, (unsigned long)ar->fnamesize);
659 		return 2;
660 	}
661 
662 	DEBUG2(ARCH, "Replaced %s with %s\n", inout_name, &ar->fnametab[entry]);
663 
664 	snprintf(inout_name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
665 	return 1;
666 }
667 #endif
668 
669 
670 static bool
ArchiveMember_HasName(const struct ar_hdr * hdr,const char * name,size_t namelen)671 ArchiveMember_HasName(const struct ar_hdr *hdr,
672 		      const char *name, size_t namelen)
673 {
674 	const size_t ar_name_len = sizeof hdr->ar_name;
675 	const char *ar_name = hdr->ar_name;
676 
677 	if (strncmp(ar_name, name, namelen) != 0)
678 		return false;
679 
680 	if (namelen >= ar_name_len)
681 		return namelen == ar_name_len;
682 
683 	/* hdr->ar_name is space-padded to the right. */
684 	if (ar_name[namelen] == ' ')
685 		return true;
686 
687 	/*
688 	 * In archives created by GNU binutils 2.27, the member names end
689 	 * with a slash.
690 	 */
691 	if (ar_name[namelen] == '/' &&
692 	    (namelen == ar_name_len || ar_name[namelen + 1] == ' '))
693 		return true;
694 
695 	return false;
696 }
697 
698 /*
699  * Locate a member of an archive, given the path of the archive and the path
700  * of the desired member.
701  *
702  * Input:
703  *	archive		Path to the archive
704  *	member		Name of member. If it is a path, only the last
705  *			component is used.
706  *	out_arh		Archive header to be filled in
707  *	mode		"r" for read-only access, "r+" for read-write access
708  *
709  * Output:
710  *	return		The archive file, positioned at the start of the
711  *			member's struct ar_hdr, or NULL if the member doesn't
712  *			exist.
713  *	*out_arh	The current struct ar_hdr for member.
714  *
715  * See ArchStatMember for an almost identical copy of this code.
716  */
717 static FILE *
ArchFindMember(const char * archive,const char * member,struct ar_hdr * out_arh,const char * mode)718 ArchFindMember(const char *archive, const char *member, struct ar_hdr *out_arh,
719 	       const char *mode)
720 {
721 	FILE *arch;		/* Stream to archive */
722 	int size;		/* Size of archive member */
723 	char magic[SARMAG];
724 	size_t len;
725 
726 	arch = fopen(archive, mode);
727 	if (arch == NULL)
728 		return NULL;
729 
730 	/*
731 	 * We use the ARMAG string to make sure this is an archive we
732 	 * can handle...
733 	 */
734 	if (fread(magic, SARMAG, 1, arch) != 1 ||
735 	    strncmp(magic, ARMAG, SARMAG) != 0) {
736 		fclose(arch);
737 		return NULL;
738 	}
739 
740 	/*
741 	 * Because of space constraints and similar things, files are archived
742 	 * using their basename, not the entire path.
743 	 */
744 	member = str_basename(member);
745 
746 	len = strlen(member);
747 
748 	while (fread(out_arh, sizeof *out_arh, 1, arch) == 1) {
749 
750 		if (strncmp(out_arh->ar_fmag, ARFMAG,
751 			    sizeof out_arh->ar_fmag) != 0) {
752 			/*
753 			 * The header is bogus, so the archive is bad
754 			 * and there's no way we can recover...
755 			 */
756 			fclose(arch);
757 			return NULL;
758 		}
759 
760 		DEBUG5(ARCH, "Reading archive %s member %.*s mtime %.*s\n",
761 		    archive,
762 		    (int)sizeof out_arh->ar_name, out_arh->ar_name,
763 		    (int)sizeof out_arh->ar_date, out_arh->ar_date);
764 
765 		if (ArchiveMember_HasName(out_arh, member, len)) {
766 			/*
767 			 * To make life easier for callers that want to update
768 			 * the archive, we reposition the file at the start of
769 			 * the header we just read before we return the
770 			 * stream. In a more general situation, it might be
771 			 * better to leave the file at the actual member,
772 			 * rather than its header, but not here.
773 			 */
774 			if (fseek(arch, -(long)sizeof *out_arh, SEEK_CUR) !=
775 			    0) {
776 				fclose(arch);
777 				return NULL;
778 			}
779 			return arch;
780 		}
781 
782 #ifdef AR_EFMT1
783 		/*
784 		 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
785 		 * first <namelen> bytes of the file
786 		 */
787 		if (strncmp(out_arh->ar_name, AR_EFMT1, sizeof AR_EFMT1 - 1) ==
788 		    0 &&
789 		    (ch_isdigit(out_arh->ar_name[sizeof AR_EFMT1 - 1]))) {
790 			size_t elen = (size_t)atoi(
791 			    &out_arh->ar_name[sizeof AR_EFMT1 - 1]);
792 			char ename[MAXPATHLEN + 1];
793 
794 			if (elen > MAXPATHLEN) {
795 				fclose(arch);
796 				return NULL;
797 			}
798 			if (fread(ename, elen, 1, arch) != 1) {
799 				fclose(arch);
800 				return NULL;
801 			}
802 			ename[elen] = '\0';
803 			if (DEBUG(ARCH) || DEBUG(MAKE))
804 				debug_printf(
805 				    "ArchFindMember: "
806 				    "Extended format entry for %s\n",
807 				    ename);
808 			if (strncmp(ename, member, len) == 0) {
809 				/* Found as extended name */
810 				if (fseek(arch,
811 				    -(long)(sizeof(struct ar_hdr) - elen),
812 				    SEEK_CUR) != 0) {
813 					fclose(arch);
814 					return NULL;
815 				}
816 				return arch;
817 			}
818 			if (fseek(arch, -(long)elen, SEEK_CUR) != 0) {
819 				fclose(arch);
820 				return NULL;
821 			}
822 		}
823 #endif
824 
825 		/*
826 		 * This isn't the member we're after, so we need to advance the
827 		 * stream's pointer to the start of the next header. Files are
828 		 * padded with newlines to an even-byte boundary, so we need to
829 		 * extract the size of the file from the 'size' field of the
830 		 * header and round it up during the seek.
831 		 */
832 		out_arh->ar_size[sizeof out_arh->ar_size - 1] = '\0';
833 		size = (int)strtol(out_arh->ar_size, NULL, 10);
834 		if (fseek(arch, (size + 1) & ~1L, SEEK_CUR) != 0) {
835 			fclose(arch);
836 			return NULL;
837 		}
838 	}
839 
840 	fclose(arch);
841 	return NULL;
842 }
843 
844 /*
845  * Touch a member of an archive, on disk.
846  * The GNode's modification time is left as-is.
847  *
848  * The st_mtime of the entire archive is also changed.
849  * For a library, it may be required to run ranlib after this.
850  *
851  * Input:
852  *	gn		Node of member to touch
853  *
854  * Results:
855  *	The 'time' field of the member's header is updated.
856  */
857 void
Arch_Touch(GNode * gn)858 Arch_Touch(GNode *gn)
859 {
860 	FILE *f;
861 	struct ar_hdr arh;
862 
863 	f = ArchFindMember(GNode_VarArchive(gn), GNode_VarMember(gn), &arh,
864 	    "r+");
865 	if (f == NULL)
866 		return;
867 
868 	snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
869 	(void)fwrite(&arh, sizeof arh, 1, f);
870 	fclose(f);		/* TODO: handle errors */
871 }
872 
873 /*
874  * Given a node which represents a library, touch the thing, making sure that
875  * the table of contents is also touched.
876  *
877  * Both the modification time of the library and of the RANLIBMAG member are
878  * set to 'now'.
879  */
880 /*ARGSUSED*/
881 void
Arch_TouchLib(GNode * gn MAKE_ATTR_UNUSED)882 Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
883 {
884 #ifdef RANLIBMAG
885 	FILE *f;
886 	struct ar_hdr arh;	/* Header describing table of contents */
887 	struct utimbuf times;
888 
889 	f = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
890 	if (f == NULL)
891 		return;
892 
893 	snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
894 	(void)fwrite(&arh, sizeof arh, 1, f);
895 	fclose(f);		/* TODO: handle errors */
896 
897 	times.actime = times.modtime = now;
898 	utime(gn->path, &times);	/* TODO: handle errors */
899 #endif
900 }
901 
902 /*
903  * Update the mtime of the GNode with the mtime from the archive member on
904  * disk (or in the cache).
905  */
906 void
Arch_UpdateMTime(GNode * gn)907 Arch_UpdateMTime(GNode *gn)
908 {
909 	struct ar_hdr *arh;
910 
911 	arh = ArchStatMember(GNode_VarArchive(gn), GNode_VarMember(gn), true);
912 	if (arh != NULL)
913 		gn->mtime = (time_t)strtol(arh->ar_date, NULL, 10);
914 	else
915 		gn->mtime = 0;
916 }
917 
918 /*
919  * Given a nonexistent archive member's node, update gn->mtime from its
920  * archived form, if it exists.
921  */
922 void
Arch_UpdateMemberMTime(GNode * gn)923 Arch_UpdateMemberMTime(GNode *gn)
924 {
925 	GNodeListNode *ln;
926 
927 	for (ln = gn->parents.first; ln != NULL; ln = ln->next) {
928 		GNode *pgn = ln->datum;
929 
930 		if (pgn->type & OP_ARCHV) {
931 			/*
932 			 * If the parent is an archive specification and is
933 			 * being made and its member's name matches the name
934 			 * of the node we were given, record the modification
935 			 * time of the parent in the child. We keep searching
936 			 * its parents in case some other parent requires this
937 			 * child to exist.
938 			 */
939 			const char *nameStart = strchr(pgn->name, '(') + 1;
940 			const char *nameEnd = strchr(nameStart, ')');
941 			size_t nameLen = (size_t)(nameEnd - nameStart);
942 
943 			if (pgn->flags.remake &&
944 			    strncmp(nameStart, gn->name, nameLen) == 0) {
945 				Arch_UpdateMTime(pgn);
946 				gn->mtime = pgn->mtime;
947 			}
948 		} else if (pgn->flags.remake) {
949 			/*
950 			 * Something which isn't a library depends on the
951 			 * existence of this target, so it needs to exist.
952 			 */
953 			gn->mtime = 0;
954 			break;
955 		}
956 	}
957 }
958 
959 /*
960  * Search for a library along the given search path.
961  *
962  * The node's 'path' field is set to the found path (including the
963  * actual file name, not -l...). If the system can handle the -L
964  * flag when linking (or we cannot find the library), we assume that
965  * the user has placed the .LIBS variable in the final linking
966  * command (or the linker will know where to find it) and set the
967  * TARGET variable for this node to be the node's name. Otherwise,
968  * we set the TARGET variable to be the full path of the library,
969  * as returned by Dir_FindFile.
970  *
971  * Input:
972  *	gn		Node of library to find
973  */
974 void
Arch_FindLib(GNode * gn,SearchPath * path)975 Arch_FindLib(GNode *gn, SearchPath *path)
976 {
977 	char *libName = str_concat3("lib", gn->name + 2, ".a");
978 	gn->path = Dir_FindFile(libName, path);
979 	free(libName);
980 
981 #ifdef LIBRARIES
982 	Var_Set(gn, TARGET, gn->name);
983 #else
984 	Var_Set(gn, TARGET, GNode_Path(gn));
985 #endif
986 }
987 
988 /* ARGSUSED */
989 static bool
RanlibOODate(const GNode * gn MAKE_ATTR_UNUSED)990 RanlibOODate(const GNode *gn MAKE_ATTR_UNUSED)
991 {
992 #ifdef RANLIBMAG
993 	struct ar_hdr *arh;	/* Header for __.SYMDEF */
994 	int tocModTime;		/* The table-of-contents' mod time */
995 
996 	arh = ArchStatMember(gn->path, RANLIBMAG, false);
997 
998 	if (arh == NULL) {
999 		/* A library without a table of contents is out-of-date. */
1000 		if (DEBUG(ARCH) || DEBUG(MAKE))
1001 			debug_printf("no toc...");
1002 		return true;
1003 	}
1004 
1005 	tocModTime = (int)strtol(arh->ar_date, NULL, 10);
1006 
1007 	if (DEBUG(ARCH) || DEBUG(MAKE))
1008 		debug_printf("%s modified %s...",
1009 		    RANLIBMAG, Targ_FmtTime(tocModTime));
1010 	return gn->youngestChild == NULL ||
1011 	       gn->youngestChild->mtime > tocModTime;
1012 #else
1013 	return false;
1014 #endif
1015 }
1016 
1017 /*
1018  * Decide if a node with the OP_LIB attribute is out-of-date. Called from
1019  * GNode_IsOODate to make its life easier.
1020  * The library is cached if it hasn't been already.
1021  *
1022  * There are several ways for a library to be out-of-date that are
1023  * not available to ordinary files. In addition, there are ways
1024  * that are open to regular files that are not available to
1025  * libraries.
1026  *
1027  * A library that is only used as a source is never
1028  * considered out-of-date by itself. This does not preclude the
1029  * library's modification time from making its parent be out-of-date.
1030  * A library will be considered out-of-date for any of these reasons,
1031  * given that it is a target on a dependency line somewhere:
1032  *
1033  *	Its modification time is less than that of one of its sources
1034  *	(gn->mtime < gn->youngestChild->mtime).
1035  *
1036  *	Its modification time is greater than the time at which the make
1037  *	began (i.e. it's been modified in the course of the make, probably
1038  *	by archiving).
1039  *
1040  *	The modification time of one of its sources is greater than the one
1041  *	of its RANLIBMAG member (i.e. its table of contents is out-of-date).
1042  *	We don't compare the archive time vs. TOC time because they can be
1043  *	too close. In my opinion we should not bother with the TOC at all
1044  *	since this is used by 'ar' rules that affect the data contents of the
1045  *	archive, not by ranlib rules, which affect the TOC.
1046  */
1047 bool
Arch_LibOODate(GNode * gn)1048 Arch_LibOODate(GNode *gn)
1049 {
1050 
1051 	if (gn->type & OP_PHONY) {
1052 		return true;
1053 	} else if (!GNode_IsTarget(gn) && Lst_IsEmpty(&gn->children)) {
1054 		return false;
1055 	} else if ((!Lst_IsEmpty(&gn->children) && gn->youngestChild == NULL) ||
1056 		   (gn->mtime > now) ||
1057 		   (gn->youngestChild != NULL &&
1058 		    gn->mtime < gn->youngestChild->mtime)) {
1059 		return true;
1060 	} else {
1061 		return RanlibOODate(gn);
1062 	}
1063 }
1064 
1065 /* Initialize the archives module. */
1066 void
Arch_Init(void)1067 Arch_Init(void)
1068 {
1069 	Lst_Init(&archives);
1070 }
1071 
1072 /* Clean up the archives module. */
1073 void
Arch_End(void)1074 Arch_End(void)
1075 {
1076 #ifdef CLEANUP
1077 	Lst_DoneCall(&archives, ArchFree);
1078 #endif
1079 }
1080 
1081 bool
Arch_IsLib(GNode * gn)1082 Arch_IsLib(GNode *gn)
1083 {
1084 	static const char armag[] = "!<arch>\n";
1085 	char buf[sizeof armag - 1];
1086 	int fd;
1087 
1088 	if ((fd = open(gn->path, O_RDONLY)) == -1)
1089 		return false;
1090 
1091 	if (read(fd, buf, sizeof buf) != sizeof buf) {
1092 		(void)close(fd);
1093 		return false;
1094 	}
1095 
1096 	(void)close(fd);
1097 
1098 	return memcmp(buf, armag, sizeof buf) == 0;
1099 }
1100