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