Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 1 | //===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the writeArchive function. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Object/ArchiveWriter.h" |
| 15 | #include "llvm/ADT/ArrayRef.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/IR/LLVMContext.h" |
| 18 | #include "llvm/Object/Archive.h" |
| 19 | #include "llvm/Object/ObjectFile.h" |
| 20 | #include "llvm/Object/SymbolicFile.h" |
Benjamin Kramer | cd278b7 | 2015-06-17 16:02:56 +0000 | [diff] [blame] | 21 | #include "llvm/Support/EndianStream.h" |
Rafael Espindola | 74f2932 | 2015-06-13 17:23:04 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Errc.h" |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/Format.h" |
| 25 | #include "llvm/Support/Path.h" |
| 26 | #include "llvm/Support/ToolOutputFile.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | |
Peter Collingbourne | 7ab1a3b | 2015-06-08 02:43:32 +0000 | [diff] [blame] | 29 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 30 | #include <unistd.h> |
Peter Collingbourne | 7ab1a3b | 2015-06-08 02:43:32 +0000 | [diff] [blame] | 31 | #else |
| 32 | #include <io.h> |
| 33 | #endif |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace llvm; |
| 36 | |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 37 | NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef) |
| 38 | : Buf(MemoryBuffer::getMemBuffer(BufRef, false)) {} |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 39 | |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 40 | Expected<NewArchiveMember> |
| 41 | NewArchiveMember::getOldMember(const object::Archive::Child &OldMember, |
| 42 | bool Deterministic) { |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 43 | Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef(); |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 44 | if (!BufOrErr) |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 45 | return BufOrErr.takeError(); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 46 | |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 47 | NewArchiveMember M; |
David Callahan | 5cb34077e8 | 2016-11-30 22:32:58 +0000 | [diff] [blame] | 48 | assert(M.IsNew == false); |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 49 | M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false); |
| 50 | if (!Deterministic) { |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 51 | auto ModTimeOrErr = OldMember.getLastModified(); |
Vedant Kumar | 4031d9f | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 52 | if (!ModTimeOrErr) |
| 53 | return ModTimeOrErr.takeError(); |
| 54 | M.ModTime = ModTimeOrErr.get(); |
| 55 | Expected<unsigned> UIDOrErr = OldMember.getUID(); |
| 56 | if (!UIDOrErr) |
| 57 | return UIDOrErr.takeError(); |
| 58 | M.UID = UIDOrErr.get(); |
| 59 | Expected<unsigned> GIDOrErr = OldMember.getGID(); |
| 60 | if (!GIDOrErr) |
| 61 | return GIDOrErr.takeError(); |
| 62 | M.GID = GIDOrErr.get(); |
| 63 | Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode(); |
| 64 | if (!AccessModeOrErr) |
| 65 | return AccessModeOrErr.takeError(); |
| 66 | M.Perms = AccessModeOrErr.get(); |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 67 | } |
| 68 | return std::move(M); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 71 | Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName, |
| 72 | bool Deterministic) { |
| 73 | sys::fs::file_status Status; |
| 74 | int FD; |
| 75 | if (auto EC = sys::fs::openFileForRead(FileName, FD)) |
| 76 | return errorCodeToError(EC); |
| 77 | assert(FD != -1); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 78 | |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 79 | if (auto EC = sys::fs::status(FD, Status)) |
| 80 | return errorCodeToError(EC); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 81 | |
| 82 | // Opening a directory doesn't make sense. Let it fail. |
| 83 | // Linux cannot open directories with open(2), although |
| 84 | // cygwin and *bsd can. |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 85 | if (Status.type() == sys::fs::file_type::directory_file) |
| 86 | return errorCodeToError(make_error_code(errc::is_a_directory)); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 87 | |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 88 | ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr = |
| 89 | MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false); |
| 90 | if (!MemberBufferOrErr) |
| 91 | return errorCodeToError(MemberBufferOrErr.getError()); |
| 92 | |
| 93 | if (close(FD) != 0) |
| 94 | return errorCodeToError(std::error_code(errno, std::generic_category())); |
| 95 | |
| 96 | NewArchiveMember M; |
David Callahan | 5cb34077e8 | 2016-11-30 22:32:58 +0000 | [diff] [blame] | 97 | M.IsNew = true; |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 98 | M.Buf = std::move(*MemberBufferOrErr); |
| 99 | if (!Deterministic) { |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 100 | M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>( |
| 101 | Status.getLastModificationTime()); |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 102 | M.UID = Status.getUser(); |
| 103 | M.GID = Status.getGroup(); |
| 104 | M.Perms = Status.permissions(); |
| 105 | } |
| 106 | return std::move(M); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | template <typename T> |
| 110 | static void printWithSpacePadding(raw_fd_ostream &OS, T Data, unsigned Size, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 111 | bool MayTruncate = false) { |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 112 | uint64_t OldPos = OS.tell(); |
| 113 | OS << Data; |
| 114 | unsigned SizeSoFar = OS.tell() - OldPos; |
| 115 | if (Size > SizeSoFar) { |
Benjamin Kramer | cd278b7 | 2015-06-17 16:02:56 +0000 | [diff] [blame] | 116 | OS.indent(Size - SizeSoFar); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 117 | } else if (Size < SizeSoFar) { |
| 118 | assert(MayTruncate && "Data doesn't fit in Size"); |
| 119 | // Some of the data this is used for (like UID) can be larger than the |
| 120 | // space available in the archive format. Truncate in that case. |
| 121 | OS.seek(OldPos + Size); |
| 122 | } |
| 123 | } |
| 124 | |
Rafael Espindola | b870e9c | 2015-07-09 15:13:41 +0000 | [diff] [blame] | 125 | static void print32(raw_ostream &Out, object::Archive::Kind Kind, |
| 126 | uint32_t Val) { |
| 127 | if (Kind == object::Archive::K_GNU) |
| 128 | support::endian::Writer<support::big>(Out).write(Val); |
| 129 | else |
| 130 | support::endian::Writer<support::little>(Out).write(Val); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 133 | static void printRestOfMemberHeader( |
| 134 | raw_fd_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime, |
| 135 | unsigned UID, unsigned GID, unsigned Perms, unsigned Size) { |
| 136 | printWithSpacePadding(Out, sys::toTimeT(ModTime), 12); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 137 | printWithSpacePadding(Out, UID, 6, true); |
| 138 | printWithSpacePadding(Out, GID, 6, true); |
| 139 | printWithSpacePadding(Out, format("%o", Perms), 8); |
| 140 | printWithSpacePadding(Out, Size, 10); |
| 141 | Out << "`\n"; |
| 142 | } |
| 143 | |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 144 | static void |
| 145 | printGNUSmallMemberHeader(raw_fd_ostream &Out, StringRef Name, |
| 146 | const sys::TimePoint<std::chrono::seconds> &ModTime, |
| 147 | unsigned UID, unsigned GID, unsigned Perms, |
| 148 | unsigned Size) { |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 149 | printWithSpacePadding(Out, Twine(Name) + "/", 16); |
| 150 | printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size); |
| 151 | } |
| 152 | |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 153 | static void |
| 154 | printBSDMemberHeader(raw_fd_ostream &Out, StringRef Name, |
| 155 | const sys::TimePoint<std::chrono::seconds> &ModTime, |
| 156 | unsigned UID, unsigned GID, unsigned Perms, |
| 157 | unsigned Size) { |
Rafael Espindola | 8cde5c0 | 2015-07-09 14:54:12 +0000 | [diff] [blame] | 158 | uint64_t PosAfterHeader = Out.tell() + 60 + Name.size(); |
| 159 | // Pad so that even 64 bit object files are aligned. |
| 160 | unsigned Pad = OffsetToAlignment(PosAfterHeader, 8); |
| 161 | unsigned NameWithPadding = Name.size() + Pad; |
| 162 | printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16); |
| 163 | printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, |
| 164 | NameWithPadding + Size); |
| 165 | Out << Name; |
| 166 | assert(PosAfterHeader == Out.tell()); |
| 167 | while (Pad--) |
| 168 | Out.write(uint8_t(0)); |
| 169 | } |
| 170 | |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 171 | static bool useStringTable(bool Thin, StringRef Name) { |
| 172 | return Thin || Name.size() >= 16; |
| 173 | } |
| 174 | |
Rafael Espindola | c291a4b2 | 2015-07-08 17:08:26 +0000 | [diff] [blame] | 175 | static void |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 176 | printMemberHeader(raw_fd_ostream &Out, object::Archive::Kind Kind, bool Thin, |
Rafael Espindola | a2ed0b0 | 2015-07-08 20:47:32 +0000 | [diff] [blame] | 177 | StringRef Name, |
Rafael Espindola | c291a4b2 | 2015-07-08 17:08:26 +0000 | [diff] [blame] | 178 | std::vector<unsigned>::iterator &StringMapIndexIter, |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 179 | const sys::TimePoint<std::chrono::seconds> &ModTime, |
| 180 | unsigned UID, unsigned GID, unsigned Perms, unsigned Size) { |
Rafael Espindola | 8cde5c0 | 2015-07-09 14:54:12 +0000 | [diff] [blame] | 181 | if (Kind == object::Archive::K_BSD) |
| 182 | return printBSDMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size); |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 183 | if (!useStringTable(Thin, Name)) |
Rafael Espindola | 8cde5c0 | 2015-07-09 14:54:12 +0000 | [diff] [blame] | 184 | return printGNUSmallMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size); |
Rafael Espindola | 65a9953 | 2015-07-08 17:26:24 +0000 | [diff] [blame] | 185 | Out << '/'; |
| 186 | printWithSpacePadding(Out, *StringMapIndexIter++, 15); |
| 187 | printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size); |
Rafael Espindola | c291a4b2 | 2015-07-08 17:08:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Rafael Espindola | 06d6d19 | 2015-07-16 00:14:49 +0000 | [diff] [blame] | 190 | // Compute the relative path from From to To. |
| 191 | static std::string computeRelativePath(StringRef From, StringRef To) { |
| 192 | if (sys::path::is_absolute(From) || sys::path::is_absolute(To)) |
| 193 | return To; |
| 194 | |
| 195 | StringRef DirFrom = sys::path::parent_path(From); |
| 196 | auto FromI = sys::path::begin(DirFrom); |
| 197 | auto ToI = sys::path::begin(To); |
| 198 | while (*FromI == *ToI) { |
| 199 | ++FromI; |
| 200 | ++ToI; |
| 201 | } |
| 202 | |
| 203 | SmallString<128> Relative; |
| 204 | for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI) |
| 205 | sys::path::append(Relative, ".."); |
| 206 | |
| 207 | for (auto ToE = sys::path::end(To); ToI != ToE; ++ToI) |
| 208 | sys::path::append(Relative, *ToI); |
| 209 | |
Peter Collingbourne | bc9a574 | 2016-11-15 21:36:35 +0000 | [diff] [blame] | 210 | #ifdef LLVM_ON_WIN32 |
| 211 | // Replace backslashes with slashes so that the path is portable between *nix |
| 212 | // and Windows. |
| 213 | std::replace(Relative.begin(), Relative.end(), '\\', '/'); |
| 214 | #endif |
| 215 | |
Rafael Espindola | 06d6d19 | 2015-07-16 00:14:49 +0000 | [diff] [blame] | 216 | return Relative.str(); |
| 217 | } |
| 218 | |
| 219 | static void writeStringTable(raw_fd_ostream &Out, StringRef ArcName, |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 220 | ArrayRef<NewArchiveMember> Members, |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 221 | std::vector<unsigned> &StringMapIndexes, |
| 222 | bool Thin) { |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 223 | unsigned StartOffset = 0; |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 224 | for (const NewArchiveMember &M : Members) { |
| 225 | StringRef Path = M.Buf->getBufferIdentifier(); |
| 226 | StringRef Name = sys::path::filename(Path); |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 227 | if (!useStringTable(Thin, Name)) |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 228 | continue; |
| 229 | if (StartOffset == 0) { |
| 230 | printWithSpacePadding(Out, "//", 58); |
| 231 | Out << "`\n"; |
| 232 | StartOffset = Out.tell(); |
| 233 | } |
| 234 | StringMapIndexes.push_back(Out.tell() - StartOffset); |
Rafael Espindola | 06d6d19 | 2015-07-16 00:14:49 +0000 | [diff] [blame] | 235 | |
David Callahan | 5cb34077e8 | 2016-11-30 22:32:58 +0000 | [diff] [blame] | 236 | if (Thin) { |
| 237 | if (M.IsNew) |
| 238 | Out << computeRelativePath(ArcName, Path); |
| 239 | else |
| 240 | Out << M.Buf->getBufferIdentifier(); |
| 241 | } else |
Rafael Espindola | 06d6d19 | 2015-07-16 00:14:49 +0000 | [diff] [blame] | 242 | Out << Name; |
| 243 | |
| 244 | Out << "/\n"; |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 245 | } |
| 246 | if (StartOffset == 0) |
| 247 | return; |
| 248 | if (Out.tell() % 2) |
| 249 | Out << '\n'; |
| 250 | int Pos = Out.tell(); |
| 251 | Out.seek(StartOffset - 12); |
| 252 | printWithSpacePadding(Out, Pos - StartOffset, 10); |
| 253 | Out.seek(Pos); |
| 254 | } |
| 255 | |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 256 | static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) { |
| 257 | using namespace std::chrono; |
| 258 | |
Rafael Espindola | 6a8e86f | 2015-07-13 20:38:09 +0000 | [diff] [blame] | 259 | if (!Deterministic) |
Pavel Labath | bff47b5 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 260 | return time_point_cast<seconds>(system_clock::now()); |
| 261 | return sys::TimePoint<seconds>(); |
Rafael Espindola | 6a8e86f | 2015-07-13 20:38:09 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 264 | // Returns the offset of the first reference to a member offset. |
| 265 | static ErrorOr<unsigned> |
Rafael Espindola | a2ed0b0 | 2015-07-08 20:47:32 +0000 | [diff] [blame] | 266 | writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind, |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 267 | ArrayRef<NewArchiveMember> Members, |
Rafael Espindola | 6a8e86f | 2015-07-13 20:38:09 +0000 | [diff] [blame] | 268 | std::vector<unsigned> &MemberOffsetRefs, bool Deterministic) { |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 269 | unsigned HeaderStartOffset = 0; |
| 270 | unsigned BodyStartOffset = 0; |
Rafael Espindola | 80c662d | 2015-07-08 21:07:18 +0000 | [diff] [blame] | 271 | SmallString<128> NameBuf; |
| 272 | raw_svector_ostream NameOS(NameBuf); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 273 | LLVMContext Context; |
Rafael Espindola | 80c662d | 2015-07-08 21:07:18 +0000 | [diff] [blame] | 274 | for (unsigned MemberNum = 0, N = Members.size(); MemberNum < N; ++MemberNum) { |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 275 | MemoryBufferRef MemberBuffer = Members[MemberNum].Buf->getMemBufferRef(); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 276 | Expected<std::unique_ptr<object::SymbolicFile>> ObjOrErr = |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 277 | object::SymbolicFile::createSymbolicFile( |
| 278 | MemberBuffer, sys::fs::file_magic::unknown, &Context); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 279 | if (!ObjOrErr) { |
| 280 | // FIXME: check only for "not an object file" errors. |
| 281 | consumeError(ObjOrErr.takeError()); |
| 282 | continue; |
| 283 | } |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 284 | object::SymbolicFile &Obj = *ObjOrErr.get(); |
| 285 | |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 286 | if (!HeaderStartOffset) { |
| 287 | HeaderStartOffset = Out.tell(); |
| 288 | if (Kind == object::Archive::K_GNU) |
Rafael Espindola | 6a8e86f | 2015-07-13 20:38:09 +0000 | [diff] [blame] | 289 | printGNUSmallMemberHeader(Out, "", now(Deterministic), 0, 0, 0, 0); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 290 | else |
Rafael Espindola | 6a8e86f | 2015-07-13 20:38:09 +0000 | [diff] [blame] | 291 | printBSDMemberHeader(Out, "__.SYMDEF", now(Deterministic), 0, 0, 0, 0); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 292 | BodyStartOffset = Out.tell(); |
| 293 | print32(Out, Kind, 0); // number of entries or bytes |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | for (const object::BasicSymbolRef &S : Obj.symbols()) { |
| 297 | uint32_t Symflags = S.getFlags(); |
| 298 | if (Symflags & object::SymbolRef::SF_FormatSpecific) |
| 299 | continue; |
| 300 | if (!(Symflags & object::SymbolRef::SF_Global)) |
| 301 | continue; |
| 302 | if (Symflags & object::SymbolRef::SF_Undefined) |
| 303 | continue; |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 304 | |
| 305 | unsigned NameOffset = NameOS.tell(); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 306 | if (auto EC = S.printName(NameOS)) |
| 307 | return EC; |
| 308 | NameOS << '\0'; |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 309 | MemberOffsetRefs.push_back(MemberNum); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 310 | if (Kind == object::Archive::K_BSD) |
| 311 | print32(Out, Kind, NameOffset); |
| 312 | print32(Out, Kind, 0); // member offset |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 315 | |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 316 | if (HeaderStartOffset == 0) |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 319 | StringRef StringTable = NameOS.str(); |
| 320 | if (Kind == object::Archive::K_BSD) |
| 321 | print32(Out, Kind, StringTable.size()); // byte count of the string table |
| 322 | Out << StringTable; |
| 323 | |
Rafael Espindola | 594e676 | 2015-07-09 19:48:06 +0000 | [diff] [blame] | 324 | // ld64 requires the next member header to start at an offset that is |
| 325 | // 4 bytes aligned. |
| 326 | unsigned Pad = OffsetToAlignment(Out.tell(), 4); |
| 327 | while (Pad--) |
| 328 | Out.write(uint8_t(0)); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 329 | |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 330 | // Patch up the size of the symbol table now that we know how big it is. |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 331 | unsigned Pos = Out.tell(); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 332 | const unsigned MemberHeaderSize = 60; |
| 333 | Out.seek(HeaderStartOffset + 48); // offset of the size field. |
| 334 | printWithSpacePadding(Out, Pos - MemberHeaderSize - HeaderStartOffset, 10); |
| 335 | |
| 336 | // Patch up the number of symbols. |
| 337 | Out.seek(BodyStartOffset); |
Rafael Espindola | 2ba806c | 2015-07-09 15:24:39 +0000 | [diff] [blame] | 338 | unsigned NumSyms = MemberOffsetRefs.size(); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 339 | if (Kind == object::Archive::K_GNU) |
| 340 | print32(Out, Kind, NumSyms); |
| 341 | else |
| 342 | print32(Out, Kind, NumSyms * 8); |
| 343 | |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 344 | Out.seek(Pos); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 345 | return BodyStartOffset + 4; |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 348 | std::pair<StringRef, std::error_code> |
| 349 | llvm::writeArchive(StringRef ArcName, |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 350 | std::vector<NewArchiveMember> &NewMembers, |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 351 | bool WriteSymtab, object::Archive::Kind Kind, |
Rafael Espindola | 484983f | 2016-05-09 13:31:11 +0000 | [diff] [blame] | 352 | bool Deterministic, bool Thin, |
| 353 | std::unique_ptr<MemoryBuffer> OldArchiveBuf) { |
Davide Italiano | 8a25933 | 2016-05-03 07:30:56 +0000 | [diff] [blame] | 354 | assert((!Thin || Kind == object::Archive::K_GNU) && |
| 355 | "Only the gnu format has a thin mode"); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 356 | SmallString<128> TmpArchive; |
| 357 | int TmpArchiveFD; |
| 358 | if (auto EC = sys::fs::createUniqueFile(ArcName + ".temp-archive-%%%%%%%.a", |
| 359 | TmpArchiveFD, TmpArchive)) |
| 360 | return std::make_pair(ArcName, EC); |
| 361 | |
| 362 | tool_output_file Output(TmpArchive, TmpArchiveFD); |
| 363 | raw_fd_ostream &Out = Output.os(); |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 364 | if (Thin) |
| 365 | Out << "!<thin>\n"; |
| 366 | else |
| 367 | Out << "!<arch>\n"; |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 368 | |
| 369 | std::vector<unsigned> MemberOffsetRefs; |
| 370 | |
| 371 | std::vector<std::unique_ptr<MemoryBuffer>> Buffers; |
| 372 | std::vector<MemoryBufferRef> Members; |
| 373 | std::vector<sys::fs::file_status> NewMemberStatus; |
| 374 | |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 375 | unsigned MemberReferenceOffset = 0; |
| 376 | if (WriteSymtab) { |
Rafael Espindola | 6a8e86f | 2015-07-13 20:38:09 +0000 | [diff] [blame] | 377 | ErrorOr<unsigned> MemberReferenceOffsetOrErr = writeSymbolTable( |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 378 | Out, Kind, NewMembers, MemberOffsetRefs, Deterministic); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 379 | if (auto EC = MemberReferenceOffsetOrErr.getError()) |
| 380 | return std::make_pair(ArcName, EC); |
| 381 | MemberReferenceOffset = MemberReferenceOffsetOrErr.get(); |
| 382 | } |
| 383 | |
| 384 | std::vector<unsigned> StringMapIndexes; |
Rafael Espindola | a2ed0b0 | 2015-07-08 20:47:32 +0000 | [diff] [blame] | 385 | if (Kind != object::Archive::K_BSD) |
Rafael Espindola | 06d6d19 | 2015-07-16 00:14:49 +0000 | [diff] [blame] | 386 | writeStringTable(Out, ArcName, NewMembers, StringMapIndexes, Thin); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 387 | |
Rafael Espindola | c291a4b2 | 2015-07-08 17:08:26 +0000 | [diff] [blame] | 388 | std::vector<unsigned>::iterator StringMapIndexIter = StringMapIndexes.begin(); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 389 | std::vector<unsigned> MemberOffset; |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 390 | for (const NewArchiveMember &M : NewMembers) { |
| 391 | MemoryBufferRef File = M.Buf->getMemBufferRef(); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 392 | |
| 393 | unsigned Pos = Out.tell(); |
| 394 | MemberOffset.push_back(Pos); |
| 395 | |
Peter Collingbourne | 8ec68fa | 2016-06-29 22:27:42 +0000 | [diff] [blame] | 396 | printMemberHeader(Out, Kind, Thin, |
| 397 | sys::path::filename(M.Buf->getBufferIdentifier()), |
| 398 | StringMapIndexIter, M.ModTime, M.UID, M.GID, M.Perms, |
| 399 | M.Buf->getBufferSize()); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 400 | |
Rafael Espindola | e649258 | 2015-07-15 05:47:46 +0000 | [diff] [blame] | 401 | if (!Thin) |
| 402 | Out << File.getBuffer(); |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 403 | |
| 404 | if (Out.tell() % 2) |
| 405 | Out << '\n'; |
| 406 | } |
| 407 | |
| 408 | if (MemberReferenceOffset) { |
| 409 | Out.seek(MemberReferenceOffset); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 410 | for (unsigned MemberNum : MemberOffsetRefs) { |
| 411 | if (Kind == object::Archive::K_BSD) |
| 412 | Out.seek(Out.tell() + 4); // skip over the string offset |
Rafael Espindola | b870e9c | 2015-07-09 15:13:41 +0000 | [diff] [blame] | 413 | print32(Out, Kind, MemberOffset[MemberNum]); |
Rafael Espindola | c79bff6 | 2015-07-09 15:56:23 +0000 | [diff] [blame] | 414 | } |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | Output.keep(); |
| 418 | Out.close(); |
Rafael Espindola | 484983f | 2016-05-09 13:31:11 +0000 | [diff] [blame] | 419 | |
| 420 | // At this point, we no longer need whatever backing memory |
| 421 | // was used to generate the NewMembers. On Windows, this buffer |
| 422 | // could be a mapped view of the file we want to replace (if |
| 423 | // we're updating an existing archive, say). In that case, the |
| 424 | // rename would still succeed, but it would leave behind a |
| 425 | // temporary file (actually the original file renamed) because |
| 426 | // a file cannot be deleted while there's a handle open on it, |
| 427 | // only renamed. So by freeing this buffer, this ensures that |
| 428 | // the last open handle on the destination file, if any, is |
| 429 | // closed before we attempt to rename. |
| 430 | OldArchiveBuf.reset(); |
| 431 | |
Peter Collingbourne | fd66a48 | 2015-06-08 02:32:01 +0000 | [diff] [blame] | 432 | sys::fs::rename(TmpArchive, ArcName); |
| 433 | return std::make_pair("", std::error_code()); |
| 434 | } |