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