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