|
20 | 20 | #include "lld/Common/ErrorHandler.h" |
21 | 21 | #include "lld/Common/Memory.h" |
22 | 22 | #include "llvm/ADT/STLExtras.h" |
| 23 | +#include "llvm/Config/config.h" |
23 | 24 | #include "llvm/Support/EndianStream.h" |
24 | 25 | #include "llvm/Support/FileSystem.h" |
25 | 26 | #include "llvm/Support/LEB128.h" |
|
30 | 31 | #include <sys/mman.h> |
31 | 32 | #endif |
32 | 33 |
|
| 34 | +#ifdef HAVE_LIBXAR |
| 35 | +#include <fcntl.h> |
| 36 | +#include <xar/xar.h> |
| 37 | +#endif |
| 38 | + |
33 | 39 | using namespace llvm; |
34 | 40 | using namespace llvm::MachO; |
35 | 41 | using namespace llvm::support; |
@@ -1033,6 +1039,58 @@ void CodeSignatureSection::writeTo(uint8_t *buf) const { |
1033 | 1039 | memset(id + fileName.size(), 0, fileNamePad); |
1034 | 1040 | } |
1035 | 1041 |
|
| 1042 | +BitcodeBundleSection::BitcodeBundleSection() |
| 1043 | + : SyntheticSection(segment_names::llvm, section_names::bitcodeBundle) {} |
| 1044 | + |
| 1045 | +class ErrorCodeWrapper { |
| 1046 | +public: |
| 1047 | + ErrorCodeWrapper(std::error_code ec) : errorCode(ec.value()) {} |
| 1048 | + ErrorCodeWrapper(int ec) : errorCode(ec) {} |
| 1049 | + operator int() const { return errorCode; } |
| 1050 | + |
| 1051 | +private: |
| 1052 | + int errorCode; |
| 1053 | +}; |
| 1054 | + |
| 1055 | +#define CHECK_EC(exp) \ |
| 1056 | + do { \ |
| 1057 | + ErrorCodeWrapper ec(exp); \ |
| 1058 | + if (ec) \ |
| 1059 | + fatal(Twine("operation failed with error code ") + Twine(ec) + #exp); \ |
| 1060 | + } while (0); |
| 1061 | + |
| 1062 | +void BitcodeBundleSection::finalize() { |
| 1063 | +#ifdef HAVE_LIBXAR |
| 1064 | + using namespace llvm::sys::fs; |
| 1065 | + CHECK_EC(createTemporaryFile("bitcode-bundle", "xar", xarPath)); |
| 1066 | + |
| 1067 | + xar_t xar(xar_open(xarPath.data(), O_RDWR)); |
| 1068 | + if (!xar) |
| 1069 | + fatal("failed to open XAR temporary file at " + xarPath); |
| 1070 | + CHECK_EC(xar_opt_set(xar, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE)); |
| 1071 | + // FIXME: add more data to XAR |
| 1072 | + CHECK_EC(xar_close(xar)); |
| 1073 | + |
| 1074 | + file_size(xarPath, xarSize); |
| 1075 | +#endif // defined(HAVE_LIBXAR) |
| 1076 | +} |
| 1077 | + |
| 1078 | +void BitcodeBundleSection::writeTo(uint8_t *buf) const { |
| 1079 | + using namespace llvm::sys::fs; |
| 1080 | + file_t handle = |
| 1081 | + CHECK(openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None), |
| 1082 | + "failed to open XAR file"); |
| 1083 | + std::error_code ec; |
| 1084 | + mapped_file_region xarMap(handle, mapped_file_region::mapmode::readonly, |
| 1085 | + xarSize, 0, ec); |
| 1086 | + if (ec) |
| 1087 | + fatal("failed to map XAR file"); |
| 1088 | + memcpy(buf, xarMap.const_data(), xarSize); |
| 1089 | + |
| 1090 | + closeFile(handle); |
| 1091 | + remove(xarPath); |
| 1092 | +} |
| 1093 | + |
1036 | 1094 | void macho::createSyntheticSymbols() { |
1037 | 1095 | auto addHeaderSymbol = [](const char *name) { |
1038 | 1096 | symtab->addSynthetic(name, in.header->isec, 0, |
|
0 commit comments