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