Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 1 | //===- Archive.cpp - ar File Format implementation ------------------------===// |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 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 ArchiveObjectFile class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/Object/Archive.h" |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Optional.h" |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 19 | #include "llvm/Object/Binary.h" |
| 20 | #include "llvm/Object/Error.h" |
| 21 | #include "llvm/Support/Chrono.h" |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Error.h" |
| 24 | #include "llvm/Support/ErrorOr.h" |
| 25 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | 7a6e343 | 2015-07-14 22:18:43 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <cstddef> |
| 32 | #include <cstdint> |
| 33 | #include <cstring> |
| 34 | #include <memory> |
| 35 | #include <string> |
| 36 | #include <system_error> |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| 39 | using namespace object; |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 40 | using namespace llvm::support::endian; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 41 | |
Craig Topper | 4172a8a | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 42 | static const char *const Magic = "!<arch>\n"; |
Rafael Espindola | 96b7967 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 43 | static const char *const ThinMagic = "!<thin>\n"; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 44 | |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 45 | void Archive::anchor() {} |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 46 | |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 47 | static Error |
| 48 | malformedError(Twine Msg) { |
| 49 | std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")"; |
| 50 | return make_error<GenericBinaryError>(std::move(StringMsg), |
| 51 | object_error::parse_failed); |
| 52 | } |
| 53 | |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 54 | ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent, |
| 55 | const char *RawHeaderPtr, |
| 56 | uint64_t Size, Error *Err) |
| 57 | : Parent(Parent), |
| 58 | ArMemHdr(reinterpret_cast<const ArMemHdrType *>(RawHeaderPtr)) { |
| 59 | if (RawHeaderPtr == nullptr) |
| 60 | return; |
| 61 | ErrorAsOutParameter ErrAsOutParam(Err); |
| 62 | |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 63 | if (Size < sizeof(ArMemHdrType)) { |
| 64 | if (Err) { |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 65 | std::string Msg("remaining size of archive too small for next archive " |
| 66 | "member header "); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 67 | Expected<StringRef> NameOrErr = getName(Size); |
| 68 | if (!NameOrErr) { |
| 69 | consumeError(NameOrErr.takeError()); |
| 70 | uint64_t Offset = RawHeaderPtr - Parent->getData().data(); |
| 71 | *Err = malformedError(Msg + "at offset " + Twine(Offset)); |
| 72 | } else |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 73 | *Err = malformedError(Msg + "for " + NameOrErr.get()); |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 74 | } |
| 75 | return; |
| 76 | } |
| 77 | if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') { |
| 78 | if (Err) { |
| 79 | std::string Buf; |
| 80 | raw_string_ostream OS(Buf); |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 81 | OS.write_escaped(StringRef(ArMemHdr->Terminator, |
| 82 | sizeof(ArMemHdr->Terminator))); |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 83 | OS.flush(); |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 84 | std::string Msg("terminator characters in archive member \"" + Buf + |
| 85 | "\" not the correct \"`\\n\" values for the archive " |
| 86 | "member header "); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 87 | Expected<StringRef> NameOrErr = getName(Size); |
| 88 | if (!NameOrErr) { |
| 89 | consumeError(NameOrErr.takeError()); |
| 90 | uint64_t Offset = RawHeaderPtr - Parent->getData().data(); |
| 91 | *Err = malformedError(Msg + "at offset " + Twine(Offset)); |
| 92 | } else |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 93 | *Err = malformedError(Msg + "for " + NameOrErr.get()); |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 94 | } |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 99 | // This gets the raw name from the ArMemHdr->Name field and checks that it is |
| 100 | // valid for the kind of archive. If it is not valid it returns an Error. |
| 101 | Expected<StringRef> ArchiveMemberHeader::getRawName() const { |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 102 | char EndCond; |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 103 | auto Kind = Parent->kind(); |
| 104 | if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) { |
| 105 | if (ArMemHdr->Name[0] == ' ') { |
| 106 | uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - |
| 107 | Parent->getData().data(); |
| 108 | return malformedError("name contains a leading space for archive member " |
| 109 | "header at offset " + Twine(Offset)); |
| 110 | } |
| 111 | EndCond = ' '; |
| 112 | } |
| 113 | else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#') |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 114 | EndCond = ' '; |
| 115 | else |
| 116 | EndCond = '/'; |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 117 | StringRef::size_type end = |
| 118 | StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond); |
| 119 | if (end == StringRef::npos) |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 120 | end = sizeof(ArMemHdr->Name); |
| 121 | assert(end <= sizeof(ArMemHdr->Name) && end > 0); |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 122 | // Don't include the EndCond if there is one. |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 123 | return StringRef(ArMemHdr->Name, end); |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 126 | // This gets the name looking up long names. Size is the size of the archive |
| 127 | // member including the header, so the size of any name following the header |
| 128 | // is checked to make sure it does not overflow. |
| 129 | Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const { |
| 130 | |
| 131 | // This can be called from the ArchiveMemberHeader constructor when the |
| 132 | // archive header is truncated to produce an error message with the name. |
| 133 | // Make sure the name field is not truncated. |
| 134 | if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) { |
| 135 | uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - |
| 136 | Parent->getData().data(); |
| 137 | return malformedError("archive header truncated before the name field " |
| 138 | "for archive member header at offset " + |
| 139 | Twine(ArchiveOffset)); |
| 140 | } |
| 141 | |
| 142 | // The raw name itself can be invalid. |
| 143 | Expected<StringRef> NameOrErr = getRawName(); |
| 144 | if (!NameOrErr) |
| 145 | return NameOrErr.takeError(); |
| 146 | StringRef Name = NameOrErr.get(); |
| 147 | |
| 148 | // Check if it's a special name. |
| 149 | if (Name[0] == '/') { |
| 150 | if (Name.size() == 1) // Linker member. |
| 151 | return Name; |
| 152 | if (Name.size() == 2 && Name[1] == '/') // String table. |
| 153 | return Name; |
| 154 | // It's a long name. |
| 155 | // Get the string table offset. |
| 156 | std::size_t StringOffset; |
| 157 | if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) { |
| 158 | std::string Buf; |
| 159 | raw_string_ostream OS(Buf); |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 160 | OS.write_escaped(Name.substr(1).rtrim(' ')); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 161 | OS.flush(); |
| 162 | uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - |
| 163 | Parent->getData().data(); |
| 164 | return malformedError("long name offset characters after the '/' are " |
| 165 | "not all decimal numbers: '" + Buf + "' for " |
| 166 | "archive member header at offset " + |
| 167 | Twine(ArchiveOffset)); |
| 168 | } |
| 169 | |
| 170 | // Verify it. |
| 171 | if (StringOffset >= Parent->getStringTable().size()) { |
| 172 | uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - |
| 173 | Parent->getData().data(); |
| 174 | return malformedError("long name offset " + Twine(StringOffset) + " past " |
| 175 | "the end of the string table for archive member " |
| 176 | "header at offset " + Twine(ArchiveOffset)); |
| 177 | } |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 178 | |
| 179 | // GNU long file names end with a "/\n". |
| 180 | if (Parent->kind() == Archive::K_GNU || |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 181 | Parent->kind() == Archive::K_GNU64) { |
Hans Wennborg | aa4eef7 | 2018-05-08 08:22:58 +0000 | [diff] [blame] | 182 | size_t End = Parent->getStringTable().find('\n', /*From=*/StringOffset); |
| 183 | if (End == StringRef::npos || End < 1 || |
| 184 | Parent->getStringTable()[End - 1] != '/') { |
| 185 | return malformedError("string table at long name offset " + |
| 186 | Twine(StringOffset) + "not terminated"); |
| 187 | } |
| 188 | return Parent->getStringTable().slice(StringOffset, End - 1); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 189 | } |
Hans Wennborg | aa4eef7 | 2018-05-08 08:22:58 +0000 | [diff] [blame] | 190 | return Parent->getStringTable().begin() + StringOffset; |
David Blaikie | 4c02879 | 2016-08-01 21:50:43 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | if (Name.startswith("#1/")) { |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 194 | uint64_t NameLength; |
| 195 | if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) { |
| 196 | std::string Buf; |
| 197 | raw_string_ostream OS(Buf); |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 198 | OS.write_escaped(Name.substr(3).rtrim(' ')); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 199 | OS.flush(); |
| 200 | uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - |
| 201 | Parent->getData().data(); |
| 202 | return malformedError("long name length characters after the #1/ are " |
| 203 | "not all decimal numbers: '" + Buf + "' for " |
| 204 | "archive member header at offset " + |
| 205 | Twine(ArchiveOffset)); |
| 206 | } |
| 207 | if (getSizeOf() + NameLength > Size) { |
| 208 | uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) - |
| 209 | Parent->getData().data(); |
| 210 | return malformedError("long name length: " + Twine(NameLength) + |
| 211 | " extends past the end of the member or archive " |
| 212 | "for archive member header at offset " + |
| 213 | Twine(ArchiveOffset)); |
| 214 | } |
| 215 | return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(), |
| 216 | NameLength).rtrim('\0'); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 217 | } |
David Blaikie | 4c02879 | 2016-08-01 21:50:43 +0000 | [diff] [blame] | 218 | |
| 219 | // It is not a long name so trim the blanks at the end of the name. |
| 220 | if (Name[Name.size() - 1] != '/') |
| 221 | return Name.rtrim(' '); |
| 222 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 223 | // It's a simple name. |
David Blaikie | 4c02879 | 2016-08-01 21:50:43 +0000 | [diff] [blame] | 224 | return Name.drop_back(1); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 227 | Expected<uint32_t> ArchiveMemberHeader::getSize() const { |
Rafael Espindola | 2012593 | 2013-07-09 12:45:11 +0000 | [diff] [blame] | 228 | uint32_t Ret; |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 229 | if (StringRef(ArMemHdr->Size, |
| 230 | sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) { |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 231 | std::string Buf; |
| 232 | raw_string_ostream OS(Buf); |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 233 | OS.write_escaped(StringRef(ArMemHdr->Size, |
| 234 | sizeof(ArMemHdr->Size)).rtrim(" ")); |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 235 | OS.flush(); |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 236 | uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - |
| 237 | Parent->getData().data(); |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 238 | return malformedError("characters in size field in archive header are not " |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 239 | "all decimal numbers: '" + Buf + "' for archive " |
| 240 | "member header at offset " + Twine(Offset)); |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 241 | } |
Rafael Espindola | 2012593 | 2013-07-09 12:45:11 +0000 | [diff] [blame] | 242 | return Ret; |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 245 | Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const { |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 246 | unsigned Ret; |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 247 | if (StringRef(ArMemHdr->AccessMode, |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 248 | sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) { |
| 249 | std::string Buf; |
| 250 | raw_string_ostream OS(Buf); |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 251 | OS.write_escaped(StringRef(ArMemHdr->AccessMode, |
| 252 | sizeof(ArMemHdr->AccessMode)).rtrim(" ")); |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 253 | OS.flush(); |
| 254 | uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - |
| 255 | Parent->getData().data(); |
| 256 | return malformedError("characters in AccessMode field in archive header " |
| 257 | "are not all decimal numbers: '" + Buf + "' for the " |
| 258 | "archive member header at offset " + Twine(Offset)); |
| 259 | } |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 260 | return static_cast<sys::fs::perms>(Ret); |
| 261 | } |
| 262 | |
Pavel Labath | 6df9562 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 263 | Expected<sys::TimePoint<std::chrono::seconds>> |
| 264 | ArchiveMemberHeader::getLastModified() const { |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 265 | unsigned Seconds; |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 266 | if (StringRef(ArMemHdr->LastModified, |
| 267 | sizeof(ArMemHdr->LastModified)).rtrim(' ') |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 268 | .getAsInteger(10, Seconds)) { |
| 269 | std::string Buf; |
| 270 | raw_string_ostream OS(Buf); |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 271 | OS.write_escaped(StringRef(ArMemHdr->LastModified, |
| 272 | sizeof(ArMemHdr->LastModified)).rtrim(" ")); |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 273 | OS.flush(); |
| 274 | uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - |
| 275 | Parent->getData().data(); |
| 276 | return malformedError("characters in LastModified field in archive header " |
| 277 | "are not all decimal numbers: '" + Buf + "' for the " |
| 278 | "archive member header at offset " + Twine(Offset)); |
| 279 | } |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 280 | |
Pavel Labath | 6df9562 | 2016-10-24 13:38:27 +0000 | [diff] [blame] | 281 | return sys::toTimePoint(Seconds); |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 284 | Expected<unsigned> ArchiveMemberHeader::getUID() const { |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 285 | unsigned Ret; |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 286 | StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' '); |
Saleem Abdulrasool | e88f1b3 | 2016-07-05 00:23:05 +0000 | [diff] [blame] | 287 | if (User.empty()) |
| 288 | return 0; |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 289 | if (User.getAsInteger(10, Ret)) { |
| 290 | std::string Buf; |
| 291 | raw_string_ostream OS(Buf); |
| 292 | OS.write_escaped(User); |
| 293 | OS.flush(); |
| 294 | uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - |
| 295 | Parent->getData().data(); |
| 296 | return malformedError("characters in UID field in archive header " |
| 297 | "are not all decimal numbers: '" + Buf + "' for the " |
| 298 | "archive member header at offset " + Twine(Offset)); |
| 299 | } |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 300 | return Ret; |
| 301 | } |
| 302 | |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 303 | Expected<unsigned> ArchiveMemberHeader::getGID() const { |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 304 | unsigned Ret; |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 305 | StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' '); |
Saleem Abdulrasool | e88f1b3 | 2016-07-05 00:23:05 +0000 | [diff] [blame] | 306 | if (Group.empty()) |
| 307 | return 0; |
Vedant Kumar | e5d1578 | 2016-08-03 19:02:50 +0000 | [diff] [blame] | 308 | if (Group.getAsInteger(10, Ret)) { |
| 309 | std::string Buf; |
| 310 | raw_string_ostream OS(Buf); |
| 311 | OS.write_escaped(Group); |
| 312 | OS.flush(); |
| 313 | uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) - |
| 314 | Parent->getData().data(); |
| 315 | return malformedError("characters in GID field in archive header " |
| 316 | "are not all decimal numbers: '" + Buf + "' for the " |
| 317 | "archive member header at offset " + Twine(Offset)); |
| 318 | } |
Rafael Espindola | 9941bdd | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 319 | return Ret; |
| 320 | } |
| 321 | |
Rafael Espindola | d3ca239 | 2015-10-31 21:44:42 +0000 | [diff] [blame] | 322 | Archive::Child::Child(const Archive *Parent, StringRef Data, |
| 323 | uint16_t StartOfFile) |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 324 | : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr), |
| 325 | Data(Data), StartOfFile(StartOfFile) { |
| 326 | } |
Rafael Espindola | d3ca239 | 2015-10-31 21:44:42 +0000 | [diff] [blame] | 327 | |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 328 | Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err) |
Lang Hames | 1911873 | 2016-10-05 21:20:00 +0000 | [diff] [blame] | 329 | : Parent(Parent), |
| 330 | Header(Parent, Start, |
| 331 | Parent |
| 332 | ? Parent->getData().size() - (Start - Parent->getData().data()) |
| 333 | : 0, Err) { |
Rafael Espindola | be6b910 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 334 | if (!Start) |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 335 | return; |
Kevin Enderby | 74bccc3 | 2016-08-04 21:54:19 +0000 | [diff] [blame] | 336 | |
| 337 | // If we are pointed to real data, Start is not a nullptr, then there must be |
| 338 | // a non-null Err pointer available to report malformed data on. Only in |
| 339 | // the case sentinel value is being constructed is Err is permitted to be a |
| 340 | // nullptr. |
| 341 | assert(Err && "Err can't be nullptr if Start is not a nullptr"); |
| 342 | |
Lang Hames | 7809543 | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 343 | ErrorAsOutParameter ErrAsOutParam(Err); |
Rafael Espindola | be6b910 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 344 | |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 345 | // If there was an error in the construction of the Header |
Kevin Enderby | 74bccc3 | 2016-08-04 21:54:19 +0000 | [diff] [blame] | 346 | // then just return with the error now set. |
| 347 | if (*Err) |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 348 | return; |
| 349 | |
| 350 | uint64_t Size = Header.getSizeOf(); |
Rafael Espindola | 96b7967 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 351 | Data = StringRef(Start, Size); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 352 | Expected<bool> isThinOrErr = isThinMember(); |
| 353 | if (!isThinOrErr) { |
Kevin Enderby | 74bccc3 | 2016-08-04 21:54:19 +0000 | [diff] [blame] | 354 | *Err = isThinOrErr.takeError(); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | bool isThin = isThinOrErr.get(); |
| 358 | if (!isThin) { |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 359 | Expected<uint64_t> MemberSize = getRawSize(); |
| 360 | if (!MemberSize) { |
Kevin Enderby | 74bccc3 | 2016-08-04 21:54:19 +0000 | [diff] [blame] | 361 | *Err = MemberSize.takeError(); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 362 | return; |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 363 | } |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 364 | Size += MemberSize.get(); |
Rafael Espindola | 9db135a | 2015-07-22 19:34:26 +0000 | [diff] [blame] | 365 | Data = StringRef(Start, Size); |
| 366 | } |
Rafael Espindola | be6b910 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 367 | |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 368 | // Setup StartOfFile and PaddingBytes. |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 369 | StartOfFile = Header.getSizeOf(); |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 370 | // Don't include attached name. |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 371 | Expected<StringRef> NameOrErr = getRawName(); |
| 372 | if (!NameOrErr){ |
Kevin Enderby | 74bccc3 | 2016-08-04 21:54:19 +0000 | [diff] [blame] | 373 | *Err = NameOrErr.takeError(); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | StringRef Name = NameOrErr.get(); |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 377 | if (Name.startswith("#1/")) { |
| 378 | uint64_t NameSize; |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 379 | if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) { |
Kevin Enderby | 74bccc3 | 2016-08-04 21:54:19 +0000 | [diff] [blame] | 380 | std::string Buf; |
| 381 | raw_string_ostream OS(Buf); |
| 382 | OS.write_escaped(Name.substr(3).rtrim(' ')); |
| 383 | OS.flush(); |
| 384 | uint64_t Offset = Start - Parent->getData().data(); |
| 385 | *Err = malformedError("long name length characters after the #1/ are " |
| 386 | "not all decimal numbers: '" + Buf + "' for " |
| 387 | "archive member header at offset " + |
| 388 | Twine(Offset)); |
| 389 | return; |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 390 | } |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 391 | StartOfFile += NameSize; |
| 392 | } |
| 393 | } |
| 394 | |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 395 | Expected<uint64_t> Archive::Child::getSize() const { |
Kevin Enderby | 1cd4c56 | 2015-10-13 20:48:04 +0000 | [diff] [blame] | 396 | if (Parent->IsThin) { |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 397 | Expected<uint32_t> Size = Header.getSize(); |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 398 | if (!Size) |
| 399 | return Size.takeError(); |
Kevin Enderby | 1cd4c56 | 2015-10-13 20:48:04 +0000 | [diff] [blame] | 400 | return Size.get(); |
| 401 | } |
Rafael Espindola | 96b7967 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 402 | return Data.size() - StartOfFile; |
| 403 | } |
| 404 | |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 405 | Expected<uint64_t> Archive::Child::getRawSize() const { |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 406 | return Header.getSize(); |
Kevin Enderby | cdfe54f | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 409 | Expected<bool> Archive::Child::isThinMember() const { |
| 410 | Expected<StringRef> NameOrErr = Header.getRawName(); |
| 411 | if (!NameOrErr) |
| 412 | return NameOrErr.takeError(); |
| 413 | StringRef Name = NameOrErr.get(); |
Rafael Espindola | 9db135a | 2015-07-22 19:34:26 +0000 | [diff] [blame] | 414 | return Parent->IsThin && Name != "/" && Name != "//"; |
| 415 | } |
| 416 | |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 417 | Expected<std::string> Archive::Child::getFullName() const { |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 418 | Expected<bool> isThin = isThinMember(); |
| 419 | if (!isThin) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 420 | return isThin.takeError(); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 421 | assert(isThin.get()); |
| 422 | Expected<StringRef> NameOrErr = getName(); |
| 423 | if (!NameOrErr) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 424 | return NameOrErr.takeError(); |
Rafael Espindola | 4c7d166 | 2016-05-02 13:45:06 +0000 | [diff] [blame] | 425 | StringRef Name = *NameOrErr; |
| 426 | if (sys::path::is_absolute(Name)) |
| 427 | return Name; |
| 428 | |
| 429 | SmallString<128> FullName = sys::path::parent_path( |
| 430 | Parent->getMemoryBufferRef().getBufferIdentifier()); |
| 431 | sys::path::append(FullName, Name); |
| 432 | return StringRef(FullName); |
| 433 | } |
| 434 | |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 435 | Expected<StringRef> Archive::Child::getBuffer() const { |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 436 | Expected<bool> isThinOrErr = isThinMember(); |
| 437 | if (!isThinOrErr) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 438 | return isThinOrErr.takeError(); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 439 | bool isThin = isThinOrErr.get(); |
| 440 | if (!isThin) { |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 441 | Expected<uint32_t> Size = getSize(); |
| 442 | if (!Size) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 443 | return Size.takeError(); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 444 | return StringRef(Data.data() + StartOfFile, Size.get()); |
| 445 | } |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 446 | Expected<std::string> FullNameOrErr = getFullName(); |
| 447 | if (!FullNameOrErr) |
| 448 | return FullNameOrErr.takeError(); |
| 449 | const std::string &FullName = *FullNameOrErr; |
Rafael Espindola | 7a6e343 | 2015-07-14 22:18:43 +0000 | [diff] [blame] | 450 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName); |
| 451 | if (std::error_code EC = Buf.getError()) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 452 | return errorCodeToError(EC); |
Rafael Espindola | 7a6e343 | 2015-07-14 22:18:43 +0000 | [diff] [blame] | 453 | Parent->ThinBuffers.push_back(std::move(*Buf)); |
| 454 | return Parent->ThinBuffers.back()->getBuffer(); |
| 455 | } |
| 456 | |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 457 | Expected<Archive::Child> Archive::Child::getNext() const { |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 458 | size_t SpaceToSkip = Data.size(); |
| 459 | // If it's odd, add 1 to make it even. |
| 460 | if (SpaceToSkip & 1) |
Kevin Enderby | da78537 | 2015-10-21 17:13:20 +0000 | [diff] [blame] | 461 | ++SpaceToSkip; |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 462 | |
Kevin Enderby | da78537 | 2015-10-21 17:13:20 +0000 | [diff] [blame] | 463 | const char *NextLoc = Data.data() + SpaceToSkip; |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 464 | |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 465 | // Check to see if this is at the end of the archive. |
| 466 | if (NextLoc == Parent->Data.getBufferEnd()) |
Lang Hames | 1911873 | 2016-10-05 21:20:00 +0000 | [diff] [blame] | 467 | return Child(nullptr, nullptr, nullptr); |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 468 | |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 469 | // Check to see if this is past the end of the archive. |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 470 | if (NextLoc > Parent->Data.getBufferEnd()) { |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 471 | std::string Msg("offset to next archive member past the end of the archive " |
| 472 | "after member "); |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 473 | Expected<StringRef> NameOrErr = getName(); |
| 474 | if (!NameOrErr) { |
| 475 | consumeError(NameOrErr.takeError()); |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 476 | uint64_t Offset = Data.data() - Parent->getData().data(); |
| 477 | return malformedError(Msg + "at offset " + Twine(Offset)); |
| 478 | } else |
Kevin Enderby | ebd8336 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 479 | return malformedError(Msg + NameOrErr.get()); |
Kevin Enderby | dc02554 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 480 | } |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 481 | |
Mehdi Amini | df0b8bc | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 482 | Error Err = Error::success(); |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 483 | Child Ret(Parent, NextLoc, &Err); |
| 484 | if (Err) |
| 485 | return std::move(Err); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 486 | return Ret; |
Rafael Espindola | 5263d0a | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Kevin Enderby | cdfe54f | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 489 | uint64_t Archive::Child::getChildOffset() const { |
| 490 | const char *a = Parent->Data.getBuffer().data(); |
| 491 | const char *c = Data.data(); |
| 492 | uint64_t offset = c - a; |
| 493 | return offset; |
| 494 | } |
| 495 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 496 | Expected<StringRef> Archive::Child::getName() const { |
| 497 | Expected<uint64_t> RawSizeOrErr = getRawSize(); |
| 498 | if (!RawSizeOrErr) |
| 499 | return RawSizeOrErr.takeError(); |
| 500 | uint64_t RawSize = RawSizeOrErr.get(); |
| 501 | Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize); |
| 502 | if (!NameOrErr) |
| 503 | return NameOrErr.takeError(); |
| 504 | StringRef Name = NameOrErr.get(); |
| 505 | return Name; |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 508 | Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const { |
| 509 | Expected<StringRef> NameOrErr = getName(); |
| 510 | if (!NameOrErr) |
| 511 | return NameOrErr.takeError(); |
Rafael Espindola | 0659928 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 512 | StringRef Name = NameOrErr.get(); |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 513 | Expected<StringRef> Buf = getBuffer(); |
| 514 | if (!Buf) |
| 515 | return Buf.takeError(); |
Rafael Espindola | 7a6e343 | 2015-07-14 22:18:43 +0000 | [diff] [blame] | 516 | return MemoryBufferRef(*Buf, Name); |
Rafael Espindola | 0659928 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Kevin Enderby | 77be094 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 519 | Expected<std::unique_ptr<Binary>> |
Rafael Espindola | 0659928 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 520 | Archive::Child::getAsBinary(LLVMContext *Context) const { |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 521 | Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef(); |
| 522 | if (!BuffOrErr) |
| 523 | return BuffOrErr.takeError(); |
Rafael Espindola | 1f65932 | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 524 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 525 | auto BinaryOrErr = createBinary(BuffOrErr.get(), Context); |
| 526 | if (BinaryOrErr) |
| 527 | return std::move(*BinaryOrErr); |
Kevin Enderby | 77be094 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 528 | return BinaryOrErr.takeError(); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 531 | Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) { |
Mehdi Amini | df0b8bc | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 532 | Error Err = Error::success(); |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 533 | std::unique_ptr<Archive> Ret(new Archive(Source, Err)); |
| 534 | if (Err) |
| 535 | return std::move(Err); |
Rafael Espindola | 1c9b982 | 2014-07-31 03:36:00 +0000 | [diff] [blame] | 536 | return std::move(Ret); |
Rafael Espindola | 3d21815 | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Rafael Espindola | d3ca239 | 2015-10-31 21:44:42 +0000 | [diff] [blame] | 539 | void Archive::setFirstRegular(const Child &C) { |
| 540 | FirstRegularData = C.Data; |
| 541 | FirstRegularStartOfFile = C.StartOfFile; |
| 542 | } |
| 543 | |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 544 | Archive::Archive(MemoryBufferRef Source, Error &Err) |
Rafael Espindola | d3ca239 | 2015-10-31 21:44:42 +0000 | [diff] [blame] | 545 | : Binary(Binary::ID_Archive, Source) { |
Lang Hames | 7809543 | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 546 | ErrorAsOutParameter ErrAsOutParam(&Err); |
Rafael Espindola | 96b7967 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 547 | StringRef Buffer = Data.getBuffer(); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 548 | // Check for sufficient magic. |
Rafael Espindola | 96b7967 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 549 | if (Buffer.startswith(ThinMagic)) { |
| 550 | IsThin = true; |
| 551 | } else if (Buffer.startswith(Magic)) { |
| 552 | IsThin = false; |
| 553 | } else { |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 554 | Err = make_error<GenericBinaryError>("File too small to be an archive", |
| 555 | object_error::invalid_file_type); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 559 | // Make sure Format is initialized before any call to |
| 560 | // ArchiveMemberHeader::getName() is made. This could be a valid empty |
| 561 | // archive which is the same in all formats. So claiming it to be gnu to is |
| 562 | // fine if not totally correct before we look for a string table or table of |
| 563 | // contents. |
| 564 | Format = K_GNU; |
| 565 | |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 566 | // Get the special members. |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 567 | child_iterator I = child_begin(Err, false); |
| 568 | if (Err) |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 569 | return; |
| 570 | child_iterator E = child_end(); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 571 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 572 | // See if this is a valid empty archive and if so return. |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 573 | if (I == E) { |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 574 | Err = Error::success(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 575 | return; |
| 576 | } |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 577 | const Child *C = &*I; |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 578 | |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 579 | auto Increment = [&]() { |
| 580 | ++I; |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 581 | if (Err) |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 582 | return true; |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 583 | C = &*I; |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 584 | return false; |
| 585 | }; |
| 586 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 587 | Expected<StringRef> NameOrErr = C->getRawName(); |
| 588 | if (!NameOrErr) { |
| 589 | Err = NameOrErr.takeError(); |
| 590 | return; |
| 591 | } |
| 592 | StringRef Name = NameOrErr.get(); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 593 | |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 594 | // Below is the pattern that is used to figure out the archive format |
| 595 | // GNU archive format |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 596 | // First member : / (may exist, if it exists, points to the symbol table ) |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 597 | // Second member : // (may exist, if it exists, points to the string table) |
| 598 | // Note : The string table is used if the filename exceeds 15 characters |
| 599 | // BSD archive format |
Rafael Espindola | a739759 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 600 | // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table) |
| 601 | // There is no string table, if the filename exceeds 15 characters or has a |
| 602 | // embedded space, the filename has #1/<size>, The size represents the size |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 603 | // of the filename that needs to be read after the archive header |
| 604 | // COFF archive format |
| 605 | // First member : / |
| 606 | // Second member : / (provides a directory of symbols) |
Rui Ueyama | 891c0cd | 2013-06-03 00:27:03 +0000 | [diff] [blame] | 607 | // Third member : // (may exist, if it exists, contains the string table) |
| 608 | // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present |
| 609 | // even if the string table is empty. However, lib.exe does not in fact |
| 610 | // seem to create the third member if there's no member whose filename |
| 611 | // exceeds 15 characters. So the third member is optional. |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 612 | |
Kevin Enderby | 68806ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 613 | if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") { |
| 614 | if (Name == "__.SYMDEF") |
| 615 | Format = K_BSD; |
| 616 | else // Name == "__.SYMDEF_64" |
| 617 | Format = K_DARWIN64; |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 618 | // We know that the symbol table is not an external file, but we still must |
| 619 | // check any Expected<> return value. |
| 620 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 621 | if (!BufOrErr) { |
| 622 | Err = BufOrErr.takeError(); |
| 623 | return; |
| 624 | } |
| 625 | SymbolTable = BufOrErr.get(); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 626 | if (Increment()) |
| 627 | return; |
| 628 | setFirstRegular(*C); |
| 629 | |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 630 | Err = Error::success(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 631 | return; |
| 632 | } |
| 633 | |
Rafael Espindola | a739759 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 634 | if (Name.startswith("#1/")) { |
| 635 | Format = K_BSD; |
| 636 | // We know this is BSD, so getName will work since there is no string table. |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 637 | Expected<StringRef> NameOrErr = C->getName(); |
| 638 | if (!NameOrErr) { |
| 639 | Err = NameOrErr.takeError(); |
Rafael Espindola | a739759 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 640 | return; |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 641 | } |
Rafael Espindola | 0659928 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 642 | Name = NameOrErr.get(); |
Nick Kledzik | 9abbcb7 | 2014-11-12 01:37:45 +0000 | [diff] [blame] | 643 | if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") { |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 644 | // We know that the symbol table is not an external file, but we still |
| 645 | // must check any Expected<> return value. |
| 646 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 647 | if (!BufOrErr) { |
| 648 | Err = BufOrErr.takeError(); |
| 649 | return; |
| 650 | } |
| 651 | SymbolTable = BufOrErr.get(); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 652 | if (Increment()) |
| 653 | return; |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 654 | } |
Kevin Enderby | 68806ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 655 | else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") { |
| 656 | Format = K_DARWIN64; |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 657 | // We know that the symbol table is not an external file, but we still |
| 658 | // must check any Expected<> return value. |
| 659 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 660 | if (!BufOrErr) { |
| 661 | Err = BufOrErr.takeError(); |
| 662 | return; |
| 663 | } |
| 664 | SymbolTable = BufOrErr.get(); |
Kevin Enderby | 68806ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 665 | if (Increment()) |
| 666 | return; |
| 667 | } |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 668 | setFirstRegular(*C); |
Rafael Espindola | a739759 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | |
Simon Atanasyan | 602706e | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 672 | // MIPS 64-bit ELF archives use a special format of a symbol table. |
| 673 | // This format is marked by `ar_name` field equals to "/SYM64/". |
| 674 | // For detailed description see page 96 in the following document: |
| 675 | // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf |
| 676 | |
| 677 | bool has64SymTable = false; |
| 678 | if (Name == "/" || Name == "/SYM64/") { |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 679 | // We know that the symbol table is not an external file, but we still |
| 680 | // must check any Expected<> return value. |
| 681 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 682 | if (!BufOrErr) { |
| 683 | Err = BufOrErr.takeError(); |
| 684 | return; |
| 685 | } |
| 686 | SymbolTable = BufOrErr.get(); |
Simon Atanasyan | 602706e | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 687 | if (Name == "/SYM64/") |
| 688 | has64SymTable = true; |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 689 | |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 690 | if (Increment()) |
| 691 | return; |
| 692 | if (I == E) { |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 693 | Err = Error::success(); |
Michael J. Spencer | bf82b07 | 2013-01-10 00:07:38 +0000 | [diff] [blame] | 694 | return; |
| 695 | } |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 696 | Expected<StringRef> NameOrErr = C->getRawName(); |
| 697 | if (!NameOrErr) { |
| 698 | Err = NameOrErr.takeError(); |
| 699 | return; |
| 700 | } |
| 701 | Name = NameOrErr.get(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Rafael Espindola | 4a0bf54 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 704 | if (Name == "//") { |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 705 | Format = has64SymTable ? K_GNU64 : K_GNU; |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 706 | // The string table is never an external member, but we still |
| 707 | // must check any Expected<> return value. |
| 708 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 709 | if (!BufOrErr) { |
| 710 | Err = BufOrErr.takeError(); |
| 711 | return; |
| 712 | } |
| 713 | StringTable = BufOrErr.get(); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 714 | if (Increment()) |
| 715 | return; |
| 716 | setFirstRegular(*C); |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 717 | Err = Error::success(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 718 | return; |
| 719 | } |
| 720 | |
Rafael Espindola | 4a0bf54 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 721 | if (Name[0] != '/') { |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 722 | Format = has64SymTable ? K_GNU64 : K_GNU; |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 723 | setFirstRegular(*C); |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 724 | Err = Error::success(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 725 | return; |
| 726 | } |
| 727 | |
Rafael Espindola | 4a0bf54 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 728 | if (Name != "/") { |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 729 | Err = errorCodeToError(object_error::parse_failed); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 730 | return; |
| 731 | } |
| 732 | |
| 733 | Format = K_COFF; |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 734 | // We know that the symbol table is not an external file, but we still |
| 735 | // must check any Expected<> return value. |
| 736 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 737 | if (!BufOrErr) { |
| 738 | Err = BufOrErr.takeError(); |
| 739 | return; |
| 740 | } |
| 741 | SymbolTable = BufOrErr.get(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 742 | |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 743 | if (Increment()) |
| 744 | return; |
| 745 | |
| 746 | if (I == E) { |
| 747 | setFirstRegular(*C); |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 748 | Err = Error::success(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 749 | return; |
| 750 | } |
| 751 | |
Kevin Enderby | 2a71517 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 752 | NameOrErr = C->getRawName(); |
| 753 | if (!NameOrErr) { |
| 754 | Err = NameOrErr.takeError(); |
| 755 | return; |
| 756 | } |
| 757 | Name = NameOrErr.get(); |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 758 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 759 | if (Name == "//") { |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 760 | // The string table is never an external member, but we still |
| 761 | // must check any Expected<> return value. |
| 762 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 763 | if (!BufOrErr) { |
| 764 | Err = BufOrErr.takeError(); |
| 765 | return; |
| 766 | } |
| 767 | StringTable = BufOrErr.get(); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 768 | if (Increment()) |
| 769 | return; |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 770 | } |
Rafael Espindola | 09a7f60 | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 771 | |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 772 | setFirstRegular(*C); |
Kevin Enderby | 0b21d88 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 773 | Err = Error::success(); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 776 | Archive::child_iterator Archive::child_begin(Error &Err, |
| 777 | bool SkipInternal) const { |
Rui Ueyama | eabc8ab | 2016-09-30 17:54:31 +0000 | [diff] [blame] | 778 | if (isEmpty()) |
Rafael Espindola | 94ad5a1 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 779 | return child_end(); |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 780 | |
| 781 | if (SkipInternal) |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 782 | return child_iterator(Child(this, FirstRegularData, |
| 783 | FirstRegularStartOfFile), |
| 784 | &Err); |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 785 | |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 786 | const char *Loc = Data.getBufferStart() + strlen(Magic); |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 787 | Child C(this, Loc, &Err); |
| 788 | if (Err) |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 789 | return child_end(); |
Lang Hames | aacf2fb | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 790 | return child_iterator(C, &Err); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Rafael Espindola | 94ad5a1 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 793 | Archive::child_iterator Archive::child_end() const { |
Lang Hames | 1911873 | 2016-10-05 21:20:00 +0000 | [diff] [blame] | 794 | return child_iterator(Child(nullptr, nullptr, nullptr), nullptr); |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 795 | } |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 796 | |
Rafael Espindola | 0659928 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 797 | StringRef Archive::Symbol::getName() const { |
Rafael Espindola | ec6cc05 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 798 | return Parent->getSymbolTable().begin() + StringIndex; |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 801 | Expected<Archive::Child> Archive::Symbol::getMember() const { |
Rafael Espindola | ec6cc05 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 802 | const char *Buf = Parent->getSymbolTable().begin(); |
Simon Atanasyan | 602706e | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 803 | const char *Offsets = Buf; |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 804 | if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64) |
Simon Atanasyan | 602706e | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 805 | Offsets += sizeof(uint64_t); |
| 806 | else |
| 807 | Offsets += sizeof(uint32_t); |
Jake Ehrlich | 9a8a815 | 2017-10-27 21:47:38 +0000 | [diff] [blame] | 808 | uint64_t Offset = 0; |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 809 | if (Parent->kind() == K_GNU) { |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 810 | Offset = read32be(Offsets + SymbolIndex * 4); |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 811 | } else if (Parent->kind() == K_GNU64) { |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 812 | Offset = read64be(Offsets + SymbolIndex * 8); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 813 | } else if (Parent->kind() == K_BSD) { |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 814 | // The SymbolIndex is an index into the ranlib structs that start at |
| 815 | // Offsets (the first uint32_t is the number of bytes of the ranlib |
| 816 | // structs). The ranlib structs are a pair of uint32_t's the first |
| 817 | // being a string table offset and the second being the offset into |
| 818 | // the archive of the member that defines the symbol. Which is what |
| 819 | // is needed here. |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 820 | Offset = read32le(Offsets + SymbolIndex * 8 + 4); |
Kevin Enderby | 68806ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 821 | } else if (Parent->kind() == K_DARWIN64) { |
| 822 | // The SymbolIndex is an index into the ranlib_64 structs that start at |
| 823 | // Offsets (the first uint64_t is the number of bytes of the ranlib_64 |
| 824 | // structs). The ranlib_64 structs are a pair of uint64_t's the first |
| 825 | // being a string table offset and the second being the offset into |
| 826 | // the archive of the member that defines the symbol. Which is what |
| 827 | // is needed here. |
| 828 | Offset = read64le(Offsets + SymbolIndex * 16 + 8); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 829 | } else { |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 830 | // Skip offsets. |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 831 | uint32_t MemberCount = read32le(Buf); |
| 832 | Buf += MemberCount * 4 + 4; |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 833 | |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 834 | uint32_t SymbolCount = read32le(Buf); |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 835 | if (SymbolIndex >= SymbolCount) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 836 | return errorCodeToError(object_error::parse_failed); |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 837 | |
Matt Beaumont-Gay | f1c2a6b | 2012-11-14 00:21:27 +0000 | [diff] [blame] | 838 | // Skip SymbolCount to get to the indices table. |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 839 | const char *Indices = Buf + 4; |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 840 | |
| 841 | // Get the index of the offset in the file member offset table for this |
| 842 | // symbol. |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 843 | uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2); |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 844 | // Subtract 1 since OffsetIndex is 1 based. |
| 845 | --OffsetIndex; |
| 846 | |
| 847 | if (OffsetIndex >= MemberCount) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 848 | return errorCodeToError(object_error::parse_failed); |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 849 | |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 850 | Offset = read32le(Offsets + OffsetIndex * 4); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 851 | } |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 852 | |
Michael J. Spencer | 768a707 | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 853 | const char *Loc = Parent->getData().begin() + Offset; |
Mehdi Amini | df0b8bc | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 854 | Error Err = Error::success(); |
Kevin Enderby | fa90761 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 855 | Child C(Parent, Loc, &Err); |
| 856 | if (Err) |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 857 | return std::move(Err); |
Kevin Enderby | 268709a | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 858 | return C; |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | Archive::Symbol Archive::Symbol::getNext() const { |
| 862 | Symbol t(*this); |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 863 | if (Parent->kind() == K_BSD) { |
| 864 | // t.StringIndex is an offset from the start of the __.SYMDEF or |
| 865 | // "__.SYMDEF SORTED" member into the string table for the ranlib |
| 866 | // struct indexed by t.SymbolIndex . To change t.StringIndex to the |
| 867 | // offset in the string table for t.SymbolIndex+1 we subtract the |
| 868 | // its offset from the start of the string table for t.SymbolIndex |
| 869 | // and add the offset of the string table for t.SymbolIndex+1. |
| 870 | |
| 871 | // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t |
| 872 | // which is the number of bytes of ranlib structs that follow. The ranlib |
| 873 | // structs are a pair of uint32_t's the first being a string table offset |
| 874 | // and the second being the offset into the archive of the member that |
| 875 | // define the symbol. After that the next uint32_t is the byte count of |
| 876 | // the string table followed by the string table. |
Rafael Espindola | ec6cc05 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 877 | const char *Buf = Parent->getSymbolTable().begin(); |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 878 | uint32_t RanlibCount = 0; |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 879 | RanlibCount = read32le(Buf) / 8; |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 880 | // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount) |
| 881 | // don't change the t.StringIndex as we don't want to reference a ranlib |
| 882 | // past RanlibCount. |
| 883 | if (t.SymbolIndex + 1 < RanlibCount) { |
| 884 | const char *Ranlibs = Buf + 4; |
| 885 | uint32_t CurRanStrx = 0; |
| 886 | uint32_t NextRanStrx = 0; |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 887 | CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8); |
| 888 | NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8); |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 889 | t.StringIndex -= CurRanStrx; |
| 890 | t.StringIndex += NextRanStrx; |
| 891 | } |
| 892 | } else { |
| 893 | // Go to one past next null. |
Rafael Espindola | ec6cc05 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 894 | t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1; |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 895 | } |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 896 | ++t.SymbolIndex; |
| 897 | return t; |
| 898 | } |
| 899 | |
Rafael Espindola | 94ad5a1 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 900 | Archive::symbol_iterator Archive::symbol_begin() const { |
Rafael Espindola | cf48cf2 | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 901 | if (!hasSymbolTable()) |
Rafael Espindola | 5159718 | 2013-07-10 20:14:22 +0000 | [diff] [blame] | 902 | return symbol_iterator(Symbol(this, 0, 0)); |
| 903 | |
Rafael Espindola | ec6cc05 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 904 | const char *buf = getSymbolTable().begin(); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 905 | if (kind() == K_GNU) { |
| 906 | uint32_t symbol_count = 0; |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 907 | symbol_count = read32be(buf); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 908 | buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 909 | } else if (kind() == K_GNU64) { |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 910 | uint64_t symbol_count = read64be(buf); |
Simon Atanasyan | 602706e | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 911 | buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t))); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 912 | } else if (kind() == K_BSD) { |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 913 | // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t |
| 914 | // which is the number of bytes of ranlib structs that follow. The ranlib |
| 915 | // structs are a pair of uint32_t's the first being a string table offset |
| 916 | // and the second being the offset into the archive of the member that |
| 917 | // define the symbol. After that the next uint32_t is the byte count of |
| 918 | // the string table followed by the string table. |
| 919 | uint32_t ranlib_count = 0; |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 920 | ranlib_count = read32le(buf) / 8; |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 921 | const char *ranlibs = buf + 4; |
| 922 | uint32_t ran_strx = 0; |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 923 | ran_strx = read32le(ranlibs); |
Kevin Enderby | d2ae115 | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 924 | buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t)))); |
| 925 | // Skip the byte count of the string table. |
| 926 | buf += sizeof(uint32_t); |
| 927 | buf += ran_strx; |
Kevin Enderby | 68806ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 928 | } else if (kind() == K_DARWIN64) { |
| 929 | // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t |
| 930 | // which is the number of bytes of ranlib_64 structs that follow. The |
| 931 | // ranlib_64 structs are a pair of uint64_t's the first being a string |
| 932 | // table offset and the second being the offset into the archive of the |
| 933 | // member that define the symbol. After that the next uint64_t is the byte |
| 934 | // count of the string table followed by the string table. |
| 935 | uint64_t ranlib_count = 0; |
| 936 | ranlib_count = read64le(buf) / 16; |
| 937 | const char *ranlibs = buf + 8; |
| 938 | uint64_t ran_strx = 0; |
| 939 | ran_strx = read64le(ranlibs); |
| 940 | buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t)))); |
| 941 | // Skip the byte count of the string table. |
| 942 | buf += sizeof(uint64_t); |
| 943 | buf += ran_strx; |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 944 | } else { |
| 945 | uint32_t member_count = 0; |
| 946 | uint32_t symbol_count = 0; |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 947 | member_count = read32le(buf); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 948 | buf += 4 + (member_count * 4); // Skip offsets. |
Rui Ueyama | 4d1d4ba | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 949 | symbol_count = read32le(buf); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 950 | buf += 4 + (symbol_count * 2); // Skip indices. |
| 951 | } |
Rafael Espindola | ec6cc05 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 952 | uint32_t string_start_offset = buf - getSymbolTable().begin(); |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 953 | return symbol_iterator(Symbol(this, 0, string_start_offset)); |
| 954 | } |
| 955 | |
Rafael Espindola | 94ad5a1 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 956 | Archive::symbol_iterator Archive::symbol_end() const { |
Rui Ueyama | 5a22e84 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 957 | return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0)); |
| 958 | } |
Rafael Espindola | 5159718 | 2013-07-10 20:14:22 +0000 | [diff] [blame] | 959 | |
Rui Ueyama | 5a22e84 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 960 | uint32_t Archive::getNumberOfSymbols() const { |
Rafael Espindola | 8a8ae26 | 2015-10-08 18:06:20 +0000 | [diff] [blame] | 961 | if (!hasSymbolTable()) |
| 962 | return 0; |
Rafael Espindola | ec6cc05 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 963 | const char *buf = getSymbolTable().begin(); |
Rui Ueyama | 5a22e84 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 964 | if (kind() == K_GNU) |
| 965 | return read32be(buf); |
Jake Ehrlich | f45adc2 | 2017-09-20 18:23:01 +0000 | [diff] [blame] | 966 | if (kind() == K_GNU64) |
Rui Ueyama | 5a22e84 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 967 | return read64be(buf); |
| 968 | if (kind() == K_BSD) |
| 969 | return read32le(buf) / 8; |
Kevin Enderby | 68806ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 970 | if (kind() == K_DARWIN64) |
| 971 | return read64le(buf) / 16; |
Rui Ueyama | 5a22e84 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 972 | uint32_t member_count = 0; |
| 973 | member_count = read32le(buf); |
| 974 | buf += 4 + (member_count * 4); // Skip offsets. |
| 975 | return read32le(buf); |
Michael J. Spencer | c8a55a6 | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 976 | } |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 977 | |
Lang Hames | 36105c0 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 978 | Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const { |
Rafael Espindola | 94ad5a1 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 979 | Archive::symbol_iterator bs = symbol_begin(); |
| 980 | Archive::symbol_iterator es = symbol_end(); |
Rafael Espindola | 0659928 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 981 | |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 982 | for (; bs != es; ++bs) { |
Rafael Espindola | 0659928 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 983 | StringRef SymName = bs->getName(); |
| 984 | if (SymName == name) { |
Lang Hames | 36105c0 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 985 | if (auto MemberOrErr = bs->getMember()) |
| 986 | return Child(*MemberOrErr); |
| 987 | else |
Kevin Enderby | b48816b | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 988 | return MemberOrErr.takeError(); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
Lang Hames | 36105c0 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 991 | return Optional<Child>(); |
Shankar Easwaran | 206252c | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 992 | } |
Rafael Espindola | cf48cf2 | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 993 | |
Rui Ueyama | eabc8ab | 2016-09-30 17:54:31 +0000 | [diff] [blame] | 994 | // Returns true if archive file contains no member file. |
| 995 | bool Archive::isEmpty() const { return Data.getBufferSize() == 8; } |
| 996 | |
Rafael Espindola | 2825a23 | 2015-10-31 21:03:29 +0000 | [diff] [blame] | 997 | bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); } |