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