| ... | ... | @@ -18,10 +18,15 @@ |
| 18 | 18 | #include "llvm/IR/LLVMContext.h" |
| 19 | 19 | #include "llvm/Object/Archive.h" |
| 20 | 20 | #include "llvm/Object/ArchiveWriter.h" |
| 21 | #include "llvm/Object/COFFImportFile.h" |
| 22 | #include "llvm/Object/ELFObjectFile.h" |
| 21 | 23 | #include "llvm/Object/IRObjectFile.h" |
| 22 | 24 | #include "llvm/Object/MachO.h" |
| 23 | 25 | #include "llvm/Object/ObjectFile.h" |
| 24 | 26 | #include "llvm/Object/SymbolicFile.h" |
| 27 | #include "llvm/Object/TapiFile.h" |
| 28 | #include "llvm/Object/Wasm.h" |
| 29 | #include "llvm/Object/XCOFFObjectFile.h" |
| 25 | 30 | #include "llvm/Support/Chrono.h" |
| 26 | 31 | #include "llvm/Support/CommandLine.h" |
| 27 | 32 | #include "llvm/Support/ConvertUTF.h" |
| ... | ... | @@ -54,6 +59,7 @@ |
| 54 | 59 | #endif |
| 55 | 60 | |
| 56 | 61 | using namespace llvm; |
| 62 | using namespace llvm::object; |
| 57 | 63 | |
| 58 | 64 | // The name this program was invoked as. |
| 59 | 65 | static StringRef ToolName; |
| ... | ... | @@ -61,37 +67,36 @@ static StringRef ToolName; |
| 61 | 67 | // The basename of this program. |
| 62 | 68 | static StringRef Stem; |
| 63 | 69 | |
| 64 | | const char RanlibHelp[] = R"(OVERVIEW: LLVM Ranlib (llvm-ranlib) |
| 65 | | |
| 66 | | This program generates an index to speed access to archives |
| 67 | | |
| 68 | | USAGE: llvm-ranlib <archive-file> |
| 69 | | |
| 70 | | OPTIONS: |
| 71 | | -h --help - Display available options |
| 72 | | -v --version - Display the version of this program |
| 73 | | -D - Use zero for timestamps and uids/gids (default) |
| 74 | | -U - Use actual timestamps and uids/gids |
| 75 | | )"; |
| 76 | | |
| 77 | | const char ArHelp[] = R"(OVERVIEW: LLVM Archiver |
| 78 | | |
| 79 | | USAGE: llvm-ar [options] [-]<operation>[modifiers] [relpos] [count] <archive> [files] |
| 80 | | llvm-ar -M [<mri-script] |
| 70 | static void printRanLibHelp(StringRef ToolName) { |
| 71 | outs() << "OVERVIEW: LLVM Ranlib\n\n" |
| 72 | << "This program generates an index to speed access to archives\n\n" |
| 73 | << "USAGE: " + ToolName + " <archive-file>\n\n" |
| 74 | << "OPTIONS:\n" |
| 75 | << " -h --help - Display available options\n" |
| 76 | << " -v --version - Display the version of this program\n" |
| 77 | << " -D - Use zero for timestamps and uids/gids " |
| 78 | "(default)\n" |
| 79 | << " -U - Use actual timestamps and uids/gids\n"; |
| 80 | } |
| 81 | 81 | |
| 82 | | OPTIONS: |
| 82 | static void printArHelp(StringRef ToolName) { |
| 83 | const char ArOptions[] = |
| 84 | R"(OPTIONS: |
| 83 | 85 | --format - archive format to create |
| 84 | 86 | =default - default |
| 85 | 87 | =gnu - gnu |
| 86 | 88 | =darwin - darwin |
| 87 | 89 | =bsd - bsd |
| 90 | =bigarchive - big archive (AIX OS) |
| 88 | 91 | --plugin=<string> - ignored for compatibility |
| 89 | 92 | -h --help - display this help and exit |
| 93 | --output - the directory to extract archive members to |
| 90 | 94 | --rsp-quoting - quoting style for response files |
| 91 | 95 | =posix - posix |
| 92 | 96 | =windows - windows |
| 93 | 97 | --thin - create a thin archive |
| 94 | 98 | --version - print the version and exit |
| 99 | -X{32|64|32_64|any} - object mode (only for AIX OS) |
| 95 | 100 | @<file> - read options from <file> |
| 96 | 101 | |
| 97 | 102 | OPERATIONS: |
| ... | ... | @@ -126,11 +131,20 @@ MODIFIERS: |
| 126 | 131 | [V] - display the version and exit |
| 127 | 132 | )"; |
| 128 | 133 | |
| 134 | outs() << "OVERVIEW: LLVM Archiver\n\n" |
| 135 | << "USAGE: " + ToolName + |
| 136 | " [options] [-]<operation>[modifiers] [relpos] " |
| 137 | "[count] <archive> [files]\n" |
| 138 | << " " + ToolName + " -M [<mri-script]\n\n"; |
| 139 | |
| 140 | outs() << ArOptions; |
| 141 | } |
| 142 | |
| 129 | 143 | static void printHelpMessage() { |
| 130 | 144 | if (Stem.contains_insensitive("ranlib")) |
| 131 | | outs() << RanlibHelp; |
| 145 | printRanLibHelp(Stem); |
| 132 | 146 | else if (Stem.contains_insensitive("ar")) |
| 133 | | outs() << ArHelp; |
| 147 | printArHelp(Stem); |
| 134 | 148 | } |
| 135 | 149 | |
| 136 | 150 | static unsigned MRILineNumber; |
| ... | ... | @@ -176,12 +190,16 @@ static void failIfError(Error E, Twine Context = "") { |
| 176 | 190 | }); |
| 177 | 191 | } |
| 178 | 192 | |
| 193 | static void warn(Twine Message) { |
| 194 | WithColor::warning(errs(), ToolName) << Message << "\n"; |
| 195 | } |
| 196 | |
| 179 | 197 | static SmallVector<const char *, 256> PositionalArgs; |
| 180 | 198 | |
| 181 | 199 | static bool MRI; |
| 182 | 200 | |
| 183 | 201 | namespace { |
| 184 | | enum Format { Default, GNU, BSD, DARWIN, Unknown }; |
| 202 | enum Format { Default, GNU, BSD, DARWIN, BIGARCHIVE, Unknown }; |
| 185 | 203 | } |
| 186 | 204 | |
| 187 | 205 | static Format FormatType = Default; |
| ... | ... | @@ -201,6 +219,10 @@ enum ArchiveOperation { |
| 201 | 219 | CreateSymTab ///< Create a symbol table in an existing archive |
| 202 | 220 | }; |
| 203 | 221 | |
| 222 | enum class BitModeTy { Bit32, Bit64, Bit32_64, Any, Unknown }; |
| 223 | |
| 224 | static BitModeTy BitMode = BitModeTy::Bit32; |
| 225 | |
| 204 | 226 | // Modifiers to follow operation to vary behavior |
| 205 | 227 | static bool AddAfter = false; ///< 'a' modifier |
| 206 | 228 | static bool AddBefore = false; ///< 'b' modifier |
| ... | ... | @@ -230,6 +252,9 @@ static int CountParam = 0; |
| 230 | 252 | // command line. |
| 231 | 253 | static std::string ArchiveName; |
| 232 | 254 | |
| 255 | // Output directory specified by --output. |
| 256 | static std::string OutputDir; |
| 257 | |
| 233 | 258 | static std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers; |
| 234 | 259 | static std::vector<std::unique_ptr<object::Archive>> Archives; |
| 235 | 260 | |
| ... | ... | @@ -447,6 +472,19 @@ static ArchiveOperation parseCommandLine() { |
| 447 | 472 | if (AddLibrary && Operation != QuickAppend) |
| 448 | 473 | badUsage("the 'L' modifier is only applicable to the 'q' operation"); |
| 449 | 474 | |
| 475 | if (!OutputDir.empty()) { |
| 476 | if (Operation != Extract) |
| 477 | badUsage("--output is only applicable to the 'x' operation"); |
| 478 | bool IsDir = false; |
| 479 | // If OutputDir is not a directory, create_directories may still succeed if |
| 480 | // all components of the path prefix are directories. Test is_directory as |
| 481 | // well. |
| 482 | if (!sys::fs::create_directories(OutputDir)) |
| 483 | sys::fs::is_directory(OutputDir, IsDir); |
| 484 | if (!IsDir) |
| 485 | fail("'" + OutputDir + "' is not a directory"); |
| 486 | } |
| 487 | |
| 450 | 488 | // Return the parsed operation to the caller |
| 451 | 489 | return Operation; |
| 452 | 490 | } |
| ... | ... | @@ -547,7 +585,15 @@ static void doExtract(StringRef Name, const object::Archive::Child &C) { |
| 547 | 585 | failIfError(ModeOrErr.takeError()); |
| 548 | 586 | sys::fs::perms Mode = ModeOrErr.get(); |
| 549 | 587 | |
| 550 | | llvm::StringRef outputFilePath = sys::path::filename(Name); |
| 588 | StringRef outputFilePath; |
| 589 | SmallString<128> path; |
| 590 | if (OutputDir.empty()) { |
| 591 | outputFilePath = sys::path::filename(Name); |
| 592 | } else { |
| 593 | sys::path::append(path, OutputDir, sys::path::filename(Name)); |
| 594 | outputFilePath = path.str(); |
| 595 | } |
| 596 | |
| 551 | 597 | if (Verbose) |
| 552 | 598 | outs() << "x - " << outputFilePath << '\n'; |
| 553 | 599 | |
| ... | ... | @@ -600,6 +646,71 @@ static bool shouldCreateArchive(ArchiveOperation Op) { |
| 600 | 646 | llvm_unreachable("Missing entry in covered switch."); |
| 601 | 647 | } |
| 602 | 648 | |
| 649 | static bool is64BitSymbolicFile(SymbolicFile &Obj) { |
| 650 | if (auto *IRObj = dyn_cast<IRObjectFile>(&Obj)) |
| 651 | return Triple(IRObj->getTargetTriple()).isArch64Bit(); |
| 652 | if (isa<COFFObjectFile>(Obj) || isa<COFFImportFile>(Obj)) |
| 653 | return false; |
| 654 | if (XCOFFObjectFile *XCOFFObj = dyn_cast<XCOFFObjectFile>(&Obj)) |
| 655 | return XCOFFObj->is64Bit(); |
| 656 | if (isa<WasmObjectFile>(Obj)) |
| 657 | return false; |
| 658 | if (TapiFile *Tapi = dyn_cast<TapiFile>(&Obj)) |
| 659 | return Tapi->is64Bit(); |
| 660 | if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj)) |
| 661 | return MachO->is64Bit(); |
| 662 | if (ELFObjectFileBase *ElfO = dyn_cast<ELFObjectFileBase>(&Obj)) |
| 663 | return ElfO->getBytesInAddress() == 8; |
| 664 | |
| 665 | fail("unsupported file format"); |
| 666 | } |
| 667 | |
| 668 | static bool isValidInBitMode(Binary &Bin) { |
| 669 | if (BitMode == BitModeTy::Bit32_64 || BitMode == BitModeTy::Any) |
| 670 | return true; |
| 671 | |
| 672 | if (SymbolicFile *SymFile = dyn_cast<SymbolicFile>(&Bin)) { |
| 673 | bool Is64Bit = is64BitSymbolicFile(*SymFile); |
| 674 | if ((Is64Bit && (BitMode == BitModeTy::Bit32)) || |
| 675 | (!Is64Bit && (BitMode == BitModeTy::Bit64))) |
| 676 | return false; |
| 677 | } |
| 678 | // In AIX "ar", non-object files are always considered to have a valid bit |
| 679 | // mode. |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | Expected<std::unique_ptr<Binary>> getAsBinary(const NewArchiveMember &NM, |
| 684 | LLVMContext *Context) { |
| 685 | auto BinaryOrErr = createBinary(NM.Buf->getMemBufferRef(), Context); |
| 686 | if (BinaryOrErr) |
| 687 | return std::move(*BinaryOrErr); |
| 688 | return BinaryOrErr.takeError(); |
| 689 | } |
| 690 | |
| 691 | Expected<std::unique_ptr<Binary>> getAsBinary(const Archive::Child &C, |
| 692 | LLVMContext *Context) { |
| 693 | return C.getAsBinary(Context); |
| 694 | } |
| 695 | |
| 696 | template <class A> static bool isValidInBitMode(const A &Member) { |
| 697 | if (object::Archive::getDefaultKindForHost() != object::Archive::K_AIXBIG) |
| 698 | return true; |
| 699 | LLVMContext Context; |
| 700 | Expected<std::unique_ptr<Binary>> BinOrErr = getAsBinary(Member, &Context); |
| 701 | // In AIX "ar", if there is a non-object file member, it is never ignored due |
| 702 | // to the bit mode setting. |
| 703 | if (!BinOrErr) { |
| 704 | consumeError(BinOrErr.takeError()); |
| 705 | return true; |
| 706 | } |
| 707 | return isValidInBitMode(*BinOrErr.get()); |
| 708 | } |
| 709 | |
| 710 | static void warnInvalidObjectForFileMode(Twine Name) { |
| 711 | warn("'" + Name + "' is not valid with the current object file mode"); |
| 712 | } |
| 713 | |
| 603 | 714 | static void performReadOperation(ArchiveOperation Operation, |
| 604 | 715 | object::Archive *OldArchive) { |
| 605 | 716 | if (Operation == Extract && OldArchive->isThin()) |
| ... | ... | @@ -614,6 +725,10 @@ static void performReadOperation(ArchiveOperation Operation, |
| 614 | 725 | failIfError(NameOrErr.takeError()); |
| 615 | 726 | StringRef Name = NameOrErr.get(); |
| 616 | 727 | |
| 728 | // Check whether to ignore this object due to its bitness. |
| 729 | if (!isValidInBitMode(C)) |
| 730 | continue; |
| 731 | |
| 617 | 732 | if (Filter) { |
| 618 | 733 | auto I = find_if(Members, [Name](StringRef Path) { |
| 619 | 734 | return comparePaths(Name, Path); |
| ... | ... | @@ -652,8 +767,6 @@ static void performReadOperation(ArchiveOperation Operation, |
| 652 | 767 | static void addChildMember(std::vector<NewArchiveMember> &Members, |
| 653 | 768 | const object::Archive::Child &M, |
| 654 | 769 | bool FlattenArchive = false) { |
| 655 | | if (Thin && !M.getParent()->isThin()) |
| 656 | | fail("cannot convert a regular archive to a thin one"); |
| 657 | 770 | Expected<NewArchiveMember> NMOrErr = |
| 658 | 771 | NewArchiveMember::getOldMember(M, Deterministic); |
| 659 | 772 | failIfError(NMOrErr.takeError()); |
| ... | ... | @@ -692,8 +805,7 @@ static void addChildMember(std::vector<NewArchiveMember> &Members, |
| 692 | 805 | Members.push_back(std::move(*NMOrErr)); |
| 693 | 806 | } |
| 694 | 807 | |
| 695 | | static void addMember(std::vector<NewArchiveMember> &Members, |
| 696 | | StringRef FileName, bool FlattenArchive = false) { |
| 808 | static NewArchiveMember getArchiveMember(StringRef FileName) { |
| 697 | 809 | Expected<NewArchiveMember> NMOrErr = |
| 698 | 810 | NewArchiveMember::getFile(FileName, Deterministic); |
| 699 | 811 | failIfError(NMOrErr.takeError(), FileName); |
| ... | ... | @@ -713,9 +825,24 @@ static void addMember(std::vector<NewArchiveMember> &Members, |
| 713 | 825 | PathOrErr ? *PathOrErr : sys::path::convert_to_slash(FileName)); |
| 714 | 826 | } |
| 715 | 827 | } |
| 828 | return std::move(*NMOrErr); |
| 829 | } |
| 830 | |
| 831 | static void addMember(std::vector<NewArchiveMember> &Members, |
| 832 | NewArchiveMember &NM) { |
| 833 | Members.push_back(std::move(NM)); |
| 834 | } |
| 835 | |
| 836 | static void addMember(std::vector<NewArchiveMember> &Members, |
| 837 | StringRef FileName, bool FlattenArchive = false) { |
| 838 | NewArchiveMember NM = getArchiveMember(FileName); |
| 839 | if (!isValidInBitMode(NM)) { |
| 840 | warnInvalidObjectForFileMode(FileName); |
| 841 | return; |
| 842 | } |
| 716 | 843 | |
| 717 | 844 | if (FlattenArchive && |
| 718 | | identify_magic(NMOrErr->Buf->getBuffer()) == file_magic::archive) { |
| 845 | identify_magic(NM.Buf->getBuffer()) == file_magic::archive) { |
| 719 | 846 | object::Archive &Lib = readLibrary(FileName); |
| 720 | 847 | // When creating thin archives, only flatten if the member is also thin. |
| 721 | 848 | if (!Thin || Lib.isThin()) { |
| ... | ... | @@ -727,7 +854,7 @@ static void addMember(std::vector<NewArchiveMember> &Members, |
| 727 | 854 | return; |
| 728 | 855 | } |
| 729 | 856 | } |
| 730 | | Members.push_back(std::move(*NMOrErr)); |
| 857 | Members.push_back(std::move(NM)); |
| 731 | 858 | } |
| 732 | 859 | |
| 733 | 860 | enum InsertAction { |
| ... | ... | @@ -743,6 +870,9 @@ static InsertAction computeInsertAction(ArchiveOperation Operation, |
| 743 | 870 | StringRef Name, |
| 744 | 871 | std::vector<StringRef>::iterator &Pos, |
| 745 | 872 | StringMap<int> &MemberCount) { |
| 873 | if (!isValidInBitMode(Member)) |
| 874 | return IA_AddOldMember; |
| 875 | |
| 746 | 876 | if (Operation == QuickAppend || Members.empty()) |
| 747 | 877 | return IA_AddOldMember; |
| 748 | 878 | auto MI = find_if( |
| ... | ... | @@ -804,7 +934,7 @@ computeNewArchiveMembers(ArchiveOperation Operation, |
| 804 | 934 | Expected<StringRef> NameOrErr = Child.getName(); |
| 805 | 935 | failIfError(NameOrErr.takeError()); |
| 806 | 936 | std::string Name = std::string(NameOrErr.get()); |
| 807 | | if (comparePaths(Name, RelPos)) { |
| 937 | if (comparePaths(Name, RelPos) && isValidInBitMode(Child)) { |
| 808 | 938 | assert(AddAfter || AddBefore); |
| 809 | 939 | if (AddBefore) |
| 810 | 940 | InsertPos = Pos; |
| ... | ... | @@ -815,12 +945,25 @@ computeNewArchiveMembers(ArchiveOperation Operation, |
| 815 | 945 | std::vector<StringRef>::iterator MemberI = Members.end(); |
| 816 | 946 | InsertAction Action = |
| 817 | 947 | computeInsertAction(Operation, Child, Name, MemberI, MemberCount); |
| 948 | |
| 949 | auto HandleNewMember = [](auto Member, auto &Members, auto &Child) { |
| 950 | NewArchiveMember NM = getArchiveMember(*Member); |
| 951 | if (isValidInBitMode(NM)) |
| 952 | addMember(Members, NM); |
| 953 | else { |
| 954 | // If a new member is not a valid object for the bit mode, add |
| 955 | // the old member back. |
| 956 | warnInvalidObjectForFileMode(*Member); |
| 957 | addChildMember(Members, Child, /*FlattenArchive=*/Thin); |
| 958 | } |
| 959 | }; |
| 960 | |
| 818 | 961 | switch (Action) { |
| 819 | 962 | case IA_AddOldMember: |
| 820 | 963 | addChildMember(Ret, Child, /*FlattenArchive=*/Thin); |
| 821 | 964 | break; |
| 822 | 965 | case IA_AddNewMember: |
| 823 | | addMember(Ret, *MemberI); |
| 966 | HandleNewMember(MemberI, Ret, Child); |
| 824 | 967 | break; |
| 825 | 968 | case IA_Delete: |
| 826 | 969 | break; |
| ... | ... | @@ -828,7 +971,7 @@ computeNewArchiveMembers(ArchiveOperation Operation, |
| 828 | 971 | addChildMember(Moved, Child, /*FlattenArchive=*/Thin); |
| 829 | 972 | break; |
| 830 | 973 | case IA_MoveNewMember: |
| 831 | | addMember(Moved, *MemberI); |
| 974 | HandleNewMember(MemberI, Moved, Child); |
| 832 | 975 | break; |
| 833 | 976 | } |
| 834 | 977 | // When processing elements with the count param, we need to preserve the |
| ... | ... | @@ -875,48 +1018,18 @@ computeNewArchiveMembers(ArchiveOperation Operation, |
| 875 | 1018 | return Ret; |
| 876 | 1019 | } |
| 877 | 1020 | |
| 878 | | static object::Archive::Kind getDefaultForHost() { |
| 879 | | return Triple(sys::getProcessTriple()).isOSDarwin() |
| 880 | | ? object::Archive::K_DARWIN |
| 881 | | : object::Archive::K_GNU; |
| 882 | | } |
| 883 | | |
| 884 | | static object::Archive::Kind getKindFromMember(const NewArchiveMember &Member) { |
| 885 | | auto MemBufferRef = Member.Buf->getMemBufferRef(); |
| 886 | | Expected<std::unique_ptr<object::ObjectFile>> OptionalObject = |
| 887 | | object::ObjectFile::createObjectFile(MemBufferRef); |
| 888 | | |
| 889 | | if (OptionalObject) |
| 890 | | return isa<object::MachOObjectFile>(**OptionalObject) |
| 891 | | ? object::Archive::K_DARWIN |
| 892 | | : object::Archive::K_GNU; |
| 893 | | |
| 894 | | // squelch the error in case we had a non-object file |
| 895 | | consumeError(OptionalObject.takeError()); |
| 896 | | |
| 897 | | // If we're adding a bitcode file to the archive, detect the Archive kind |
| 898 | | // based on the target triple. |
| 899 | | LLVMContext Context; |
| 900 | | if (identify_magic(MemBufferRef.getBuffer()) == file_magic::bitcode) { |
| 901 | | if (auto ObjOrErr = object::SymbolicFile::createSymbolicFile( |
| 902 | | MemBufferRef, file_magic::bitcode, &Context)) { |
| 903 | | auto &IRObject = cast<object::IRObjectFile>(**ObjOrErr); |
| 904 | | return Triple(IRObject.getTargetTriple()).isOSDarwin() |
| 905 | | ? object::Archive::K_DARWIN |
| 906 | | : object::Archive::K_GNU; |
| 907 | | } else { |
| 908 | | // Squelch the error in case this was not a SymbolicFile. |
| 909 | | consumeError(ObjOrErr.takeError()); |
| 910 | | } |
| 911 | | } |
| 912 | | |
| 913 | | return getDefaultForHost(); |
| 914 | | } |
| 915 | | |
| 916 | 1021 | static void performWriteOperation(ArchiveOperation Operation, |
| 917 | 1022 | object::Archive *OldArchive, |
| 918 | 1023 | std::unique_ptr<MemoryBuffer> OldArchiveBuf, |
| 919 | 1024 | std::vector<NewArchiveMember> *NewMembersP) { |
| 1025 | if (OldArchive) { |
| 1026 | if (Thin && !OldArchive->isThin()) |
| 1027 | fail("cannot convert a regular archive to a thin one"); |
| 1028 | |
| 1029 | if (OldArchive->isThin()) |
| 1030 | Thin = true; |
| 1031 | } |
| 1032 | |
| 920 | 1033 | std::vector<NewArchiveMember> NewMembers; |
| 921 | 1034 | if (!NewMembersP) |
| 922 | 1035 | NewMembers = computeNewArchiveMembers(Operation, OldArchive); |
| ... | ... | @@ -926,14 +1039,23 @@ static void performWriteOperation(ArchiveOperation Operation, |
| 926 | 1039 | case Default: |
| 927 | 1040 | if (Thin) |
| 928 | 1041 | Kind = object::Archive::K_GNU; |
| 929 | | else if (OldArchive) |
| 1042 | else if (OldArchive) { |
| 930 | 1043 | Kind = OldArchive->kind(); |
| 931 | | else if (NewMembersP) |
| 932 | | Kind = !NewMembersP->empty() ? getKindFromMember(NewMembersP->front()) |
| 933 | | : getDefaultForHost(); |
| 1044 | if (Kind == object::Archive::K_BSD) { |
| 1045 | auto InferredKind = object::Archive::K_BSD; |
| 1046 | if (NewMembersP && !NewMembersP->empty()) |
| 1047 | InferredKind = NewMembersP->front().detectKindFromObject(); |
| 1048 | else if (!NewMembers.empty()) |
| 1049 | InferredKind = NewMembers.front().detectKindFromObject(); |
| 1050 | if (InferredKind == object::Archive::K_DARWIN) |
| 1051 | Kind = object::Archive::K_DARWIN; |
| 1052 | } |
| 1053 | } else if (NewMembersP) |
| 1054 | Kind = !NewMembersP->empty() ? NewMembersP->front().detectKindFromObject() |
| 1055 | : object::Archive::getDefaultKindForHost(); |
| 934 | 1056 | else |
| 935 | | Kind = !NewMembers.empty() ? getKindFromMember(NewMembers.front()) |
| 936 | | : getDefaultForHost(); |
| 1057 | Kind = !NewMembers.empty() ? NewMembers.front().detectKindFromObject() |
| 1058 | : object::Archive::getDefaultKindForHost(); |
| 937 | 1059 | break; |
| 938 | 1060 | case GNU: |
| 939 | 1061 | Kind = object::Archive::K_GNU; |
| ... | ... | @@ -948,6 +1070,11 @@ static void performWriteOperation(ArchiveOperation Operation, |
| 948 | 1070 | fail("only the gnu format has a thin mode"); |
| 949 | 1071 | Kind = object::Archive::K_DARWIN; |
| 950 | 1072 | break; |
| 1073 | case BIGARCHIVE: |
| 1074 | if (Thin) |
| 1075 | fail("only the gnu format has a thin mode"); |
| 1076 | Kind = object::Archive::K_AIXBIG; |
| 1077 | break; |
| 951 | 1078 | case Unknown: |
| 952 | 1079 | llvm_unreachable(""); |
| 953 | 1080 | } |
| ... | ... | @@ -1029,8 +1156,7 @@ static int performOperation(ArchiveOperation Operation, |
| 1029 | 1156 | } else { |
| 1030 | 1157 | if (!Create) { |
| 1031 | 1158 | // Produce a warning if we should and we're creating the archive |
| 1032 | | WithColor::warning(errs(), ToolName) |
| 1033 | | << "creating " << ArchiveName << "\n"; |
| 1159 | warn("creating " + ArchiveName); |
| 1034 | 1160 | } |
| 1035 | 1161 | } |
| 1036 | 1162 | |
| ... | ... | @@ -1073,8 +1199,12 @@ static void runMRIScript() { |
| 1073 | 1199 | |
| 1074 | 1200 | switch (Command) { |
| 1075 | 1201 | case MRICommand::AddLib: { |
| 1202 | if (!Create) |
| 1203 | fail("no output archive has been opened"); |
| 1076 | 1204 | object::Archive &Lib = readLibrary(Rest); |
| 1077 | 1205 | { |
| 1206 | if (Thin && !Lib.isThin()) |
| 1207 | fail("cannot add a regular archive's contents to a thin archive"); |
| 1078 | 1208 | Error Err = Error::success(); |
| 1079 | 1209 | for (auto &Member : Lib.children(Err)) |
| 1080 | 1210 | addChildMember(NewMembers, Member, /*FlattenArchive=*/Thin); |
| ... | ... | @@ -1083,6 +1213,8 @@ static void runMRIScript() { |
| 1083 | 1213 | break; |
| 1084 | 1214 | } |
| 1085 | 1215 | case MRICommand::AddMod: |
| 1216 | if (!Create) |
| 1217 | fail("no output archive has been opened"); |
| 1086 | 1218 | addMember(NewMembers, Rest); |
| 1087 | 1219 | break; |
| 1088 | 1220 | case MRICommand::CreateThin: |
| ... | ... | @@ -1095,6 +1227,8 @@ static void runMRIScript() { |
| 1095 | 1227 | if (Saved) |
| 1096 | 1228 | fail("file already saved"); |
| 1097 | 1229 | ArchiveName = std::string(Rest); |
| 1230 | if (ArchiveName.empty()) |
| 1231 | fail("missing archive name"); |
| 1098 | 1232 | break; |
| 1099 | 1233 | case MRICommand::Delete: { |
| 1100 | 1234 | llvm::erase_if(NewMembers, [=](NewArchiveMember &M) { |
| ... | ... | @@ -1116,7 +1250,8 @@ static void runMRIScript() { |
| 1116 | 1250 | |
| 1117 | 1251 | // Nothing to do if not saved. |
| 1118 | 1252 | if (Saved) |
| 1119 | | performOperation(ReplaceOrInsert, &NewMembers); |
| 1253 | performOperation(ReplaceOrInsert, /*OldArchive=*/nullptr, |
| 1254 | /*OldArchiveBuf=*/nullptr, &NewMembers); |
| 1120 | 1255 | exit(0); |
| 1121 | 1256 | } |
| 1122 | 1257 | |
| ... | ... | @@ -1132,6 +1267,15 @@ static bool handleGenericOption(StringRef arg) { |
| 1132 | 1267 | return false; |
| 1133 | 1268 | } |
| 1134 | 1269 | |
| 1270 | static BitModeTy getBitMode(const char *RawBitMode) { |
| 1271 | return StringSwitch<BitModeTy>(RawBitMode) |
| 1272 | .Case("32", BitModeTy::Bit32) |
| 1273 | .Case("64", BitModeTy::Bit64) |
| 1274 | .Case("32_64", BitModeTy::Bit32_64) |
| 1275 | .Case("any", BitModeTy::Any) |
| 1276 | .Default(BitModeTy::Unknown); |
| 1277 | } |
| 1278 | |
| 1135 | 1279 | static const char *matchFlagWithArg(StringRef Expected, |
| 1136 | 1280 | ArrayRef<const char *>::iterator &ArgIt, |
| 1137 | 1281 | ArrayRef<const char *> Args) { |
| ... | ... | @@ -1181,6 +1325,14 @@ static int ar_main(int argc, char **argv) { |
| 1181 | 1325 | |
| 1182 | 1326 | cl::ExpandResponseFiles(Saver, getRspQuoting(makeArrayRef(argv, argc)), Argv); |
| 1183 | 1327 | |
| 1328 | // Get BitMode from enviorment variable "OBJECT_MODE" for AIX OS, if |
| 1329 | // specified. |
| 1330 | if (object::Archive::getDefaultKindForHost() == object::Archive::K_AIXBIG) { |
| 1331 | BitMode = getBitMode(getenv("OBJECT_MODE")); |
| 1332 | if (BitMode == BitModeTy::Unknown) |
| 1333 | BitMode = BitModeTy::Bit32; |
| 1334 | } |
| 1335 | |
| 1184 | 1336 | for (ArrayRef<const char *>::iterator ArgIt = Argv.begin(); |
| 1185 | 1337 | ArgIt != Argv.end(); ++ArgIt) { |
| 1186 | 1338 | const char *Match = nullptr; |
| ... | ... | @@ -1219,16 +1371,35 @@ static int ar_main(int argc, char **argv) { |
| 1219 | 1371 | .Case("gnu", GNU) |
| 1220 | 1372 | .Case("darwin", DARWIN) |
| 1221 | 1373 | .Case("bsd", BSD) |
| 1374 | .Case("bigarchive", BIGARCHIVE) |
| 1222 | 1375 | .Default(Unknown); |
| 1223 | 1376 | if (FormatType == Unknown) |
| 1224 | 1377 | fail(std::string("Invalid format ") + Match); |
| 1225 | 1378 | continue; |
| 1226 | 1379 | } |
| 1227 | 1380 | |
| 1381 | if ((Match = matchFlagWithArg("output", ArgIt, Argv))) { |
| 1382 | OutputDir = Match; |
| 1383 | continue; |
| 1384 | } |
| 1385 | |
| 1228 | 1386 | if (matchFlagWithArg("plugin", ArgIt, Argv) || |
| 1229 | 1387 | matchFlagWithArg("rsp-quoting", ArgIt, Argv)) |
| 1230 | 1388 | continue; |
| 1231 | 1389 | |
| 1390 | if (strncmp(*ArgIt, "-X", 2) == 0) { |
| 1391 | if (object::Archive::getDefaultKindForHost() == |
| 1392 | object::Archive::K_AIXBIG) { |
| 1393 | Match = *(*ArgIt + 2) != '\0' ? *ArgIt + 2 : *(++ArgIt); |
| 1394 | BitMode = getBitMode(Match); |
| 1395 | if (BitMode == BitModeTy::Unknown) |
| 1396 | fail(Twine("invalid bit mode: ") + Match); |
| 1397 | continue; |
| 1398 | } else { |
| 1399 | fail(Twine(*ArgIt) + " option not supported on non AIX OS"); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1232 | 1403 | Options += *ArgIt + 1; |
| 1233 | 1404 | } |
| 1234 | 1405 | |