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; |
Lang Hames | 5e51a2e | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 313 | ErrorAsOutParameter ErrAsOutParam(Err); |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 314 | |
Kevin Enderby | 95b0842 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 315 | // If there was an error in the construction of the Header and we were passed |
| 316 | // Err that is not nullptr then just return with the error now set. |
| 317 | if (Err && *Err) |
| 318 | return; |
| 319 | |
| 320 | uint64_t Size = Header.getSizeOf(); |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 321 | Data = StringRef(Start, Size); |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 322 | Expected<bool> isThinOrErr = isThinMember(); |
| 323 | if (!isThinOrErr) { |
| 324 | if (Err) |
| 325 | *Err = isThinOrErr.takeError(); |
| 326 | return; |
| 327 | } |
| 328 | bool isThin = isThinOrErr.get(); |
| 329 | if (!isThin) { |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 330 | Expected<uint64_t> MemberSize = getRawSize(); |
| 331 | if (!MemberSize) { |
Lang Hames | 5e51a2e | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 332 | if (Err) |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 333 | *Err = MemberSize.takeError(); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 334 | return; |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 335 | } |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 336 | Size += MemberSize.get(); |
Rafael Espindola | be9ab26 | 2015-07-22 19:34:26 +0000 | [diff] [blame] | 337 | Data = StringRef(Start, Size); |
| 338 | } |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 339 | |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 340 | // Setup StartOfFile and PaddingBytes. |
Kevin Enderby | 95b0842 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 341 | StartOfFile = Header.getSizeOf(); |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 342 | // Don't include attached name. |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 343 | Expected<StringRef> NameOrErr = getRawName(); |
| 344 | if (!NameOrErr){ |
| 345 | if (Err) |
| 346 | *Err = NameOrErr.takeError(); |
| 347 | return; |
| 348 | } |
| 349 | StringRef Name = NameOrErr.get(); |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 350 | if (Name.startswith("#1/")) { |
| 351 | uint64_t NameSize; |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 352 | if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) { |
| 353 | if (Err) { |
| 354 | std::string Buf; |
| 355 | raw_string_ostream OS(Buf); |
Kevin Enderby | 31b07f1 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 356 | OS.write_escaped(Name.substr(3).rtrim(' ')); |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 357 | OS.flush(); |
| 358 | uint64_t Offset = Start - Parent->getData().data(); |
| 359 | *Err = malformedError("long name length characters after the #1/ are " |
| 360 | "not all decimal numbers: '" + Buf + "' for " |
| 361 | "archive member header at offset " + |
| 362 | Twine(Offset)); |
| 363 | return; |
| 364 | } |
| 365 | } |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 366 | StartOfFile += NameSize; |
| 367 | } |
| 368 | } |
| 369 | |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 370 | Expected<uint64_t> Archive::Child::getSize() const { |
Kevin Enderby | 1c1add4 | 2015-10-13 20:48:04 +0000 | [diff] [blame] | 371 | if (Parent->IsThin) { |
Kevin Enderby | 95b0842 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 372 | Expected<uint32_t> Size = Header.getSize(); |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 373 | if (!Size) |
| 374 | return Size.takeError(); |
Kevin Enderby | 1c1add4 | 2015-10-13 20:48:04 +0000 | [diff] [blame] | 375 | return Size.get(); |
| 376 | } |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 377 | return Data.size() - StartOfFile; |
| 378 | } |
| 379 | |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 380 | Expected<uint64_t> Archive::Child::getRawSize() const { |
Kevin Enderby | 95b0842 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 381 | return Header.getSize(); |
Kevin Enderby | 13023a1 | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 384 | Expected<bool> Archive::Child::isThinMember() const { |
| 385 | Expected<StringRef> NameOrErr = Header.getRawName(); |
| 386 | if (!NameOrErr) |
| 387 | return NameOrErr.takeError(); |
| 388 | StringRef Name = NameOrErr.get(); |
Rafael Espindola | be9ab26 | 2015-07-22 19:34:26 +0000 | [diff] [blame] | 389 | return Parent->IsThin && Name != "/" && Name != "//"; |
| 390 | } |
| 391 | |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 392 | Expected<std::string> Archive::Child::getFullName() const { |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 393 | Expected<bool> isThin = isThinMember(); |
| 394 | if (!isThin) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 395 | return isThin.takeError(); |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 396 | assert(isThin.get()); |
| 397 | Expected<StringRef> NameOrErr = getName(); |
| 398 | if (!NameOrErr) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 399 | return NameOrErr.takeError(); |
Rafael Espindola | 694210c | 2016-05-02 13:45:06 +0000 | [diff] [blame] | 400 | StringRef Name = *NameOrErr; |
| 401 | if (sys::path::is_absolute(Name)) |
| 402 | return Name; |
| 403 | |
| 404 | SmallString<128> FullName = sys::path::parent_path( |
| 405 | Parent->getMemoryBufferRef().getBufferIdentifier()); |
| 406 | sys::path::append(FullName, Name); |
| 407 | return StringRef(FullName); |
| 408 | } |
| 409 | |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 410 | Expected<StringRef> Archive::Child::getBuffer() const { |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 411 | Expected<bool> isThinOrErr = isThinMember(); |
| 412 | if (!isThinOrErr) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 413 | return isThinOrErr.takeError(); |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 414 | bool isThin = isThinOrErr.get(); |
| 415 | if (!isThin) { |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 416 | Expected<uint32_t> Size = getSize(); |
| 417 | if (!Size) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 418 | return Size.takeError(); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 419 | return StringRef(Data.data() + StartOfFile, Size.get()); |
| 420 | } |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 421 | Expected<std::string> FullNameOrErr = getFullName(); |
| 422 | if (!FullNameOrErr) |
| 423 | return FullNameOrErr.takeError(); |
| 424 | const std::string &FullName = *FullNameOrErr; |
Rafael Espindola | 4b83cb5 | 2015-07-14 22:18:43 +0000 | [diff] [blame] | 425 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName); |
| 426 | if (std::error_code EC = Buf.getError()) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 427 | return errorCodeToError(EC); |
Rafael Espindola | 4b83cb5 | 2015-07-14 22:18:43 +0000 | [diff] [blame] | 428 | Parent->ThinBuffers.push_back(std::move(*Buf)); |
| 429 | return Parent->ThinBuffers.back()->getBuffer(); |
| 430 | } |
| 431 | |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 432 | Expected<Archive::Child> Archive::Child::getNext() const { |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 433 | size_t SpaceToSkip = Data.size(); |
| 434 | // If it's odd, add 1 to make it even. |
| 435 | if (SpaceToSkip & 1) |
Kevin Enderby | da9dd05 | 2015-10-21 17:13:20 +0000 | [diff] [blame] | 436 | ++SpaceToSkip; |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 437 | |
Kevin Enderby | da9dd05 | 2015-10-21 17:13:20 +0000 | [diff] [blame] | 438 | const char *NextLoc = Data.data() + SpaceToSkip; |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 439 | |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 440 | // Check to see if this is at the end of the archive. |
| 441 | if (NextLoc == Parent->Data.getBufferEnd()) |
| 442 | return Child(Parent, nullptr, nullptr); |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 443 | |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 444 | // Check to see if this is past the end of the archive. |
Kevin Enderby | 95b0842 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 445 | if (NextLoc > Parent->Data.getBufferEnd()) { |
Kevin Enderby | 31b07f1 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 446 | std::string Msg("offset to next archive member past the end of the archive " |
| 447 | "after member "); |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 448 | Expected<StringRef> NameOrErr = getName(); |
| 449 | if (!NameOrErr) { |
| 450 | consumeError(NameOrErr.takeError()); |
Kevin Enderby | 95b0842 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 451 | uint64_t Offset = Data.data() - Parent->getData().data(); |
| 452 | return malformedError(Msg + "at offset " + Twine(Offset)); |
| 453 | } else |
Kevin Enderby | 31b07f1 | 2016-07-29 22:32:02 +0000 | [diff] [blame] | 454 | return malformedError(Msg + NameOrErr.get()); |
Kevin Enderby | 95b0842 | 2016-07-25 20:36:36 +0000 | [diff] [blame] | 455 | } |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 456 | |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 457 | Error Err; |
| 458 | Child Ret(Parent, NextLoc, &Err); |
| 459 | if (Err) |
| 460 | return std::move(Err); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 461 | return Ret; |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Kevin Enderby | 13023a1 | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 464 | uint64_t Archive::Child::getChildOffset() const { |
| 465 | const char *a = Parent->Data.getBuffer().data(); |
| 466 | const char *c = Data.data(); |
| 467 | uint64_t offset = c - a; |
| 468 | return offset; |
| 469 | } |
| 470 | |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 471 | Expected<StringRef> Archive::Child::getName() const { |
| 472 | Expected<uint64_t> RawSizeOrErr = getRawSize(); |
| 473 | if (!RawSizeOrErr) |
| 474 | return RawSizeOrErr.takeError(); |
| 475 | uint64_t RawSize = RawSizeOrErr.get(); |
| 476 | Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize); |
| 477 | if (!NameOrErr) |
| 478 | return NameOrErr.takeError(); |
| 479 | StringRef Name = NameOrErr.get(); |
| 480 | return Name; |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 483 | Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const { |
| 484 | Expected<StringRef> NameOrErr = getName(); |
| 485 | if (!NameOrErr) |
| 486 | return NameOrErr.takeError(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 487 | StringRef Name = NameOrErr.get(); |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 488 | Expected<StringRef> Buf = getBuffer(); |
| 489 | if (!Buf) |
| 490 | return Buf.takeError(); |
Rafael Espindola | 4b83cb5 | 2015-07-14 22:18:43 +0000 | [diff] [blame] | 491 | return MemoryBufferRef(*Buf, Name); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 494 | Expected<std::unique_ptr<Binary>> |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 495 | Archive::Child::getAsBinary(LLVMContext *Context) const { |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 496 | Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef(); |
| 497 | if (!BuffOrErr) |
| 498 | return BuffOrErr.takeError(); |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 499 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 500 | auto BinaryOrErr = createBinary(BuffOrErr.get(), Context); |
| 501 | if (BinaryOrErr) |
| 502 | return std::move(*BinaryOrErr); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 503 | return BinaryOrErr.takeError(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 506 | Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) { |
| 507 | Error Err; |
| 508 | std::unique_ptr<Archive> Ret(new Archive(Source, Err)); |
| 509 | if (Err) |
| 510 | return std::move(Err); |
Rafael Espindola | f557713 | 2014-07-31 03:36:00 +0000 | [diff] [blame] | 511 | return std::move(Ret); |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Rafael Espindola | 4335876 | 2015-10-31 21:44:42 +0000 | [diff] [blame] | 514 | void Archive::setFirstRegular(const Child &C) { |
| 515 | FirstRegularData = C.Data; |
| 516 | FirstRegularStartOfFile = C.StartOfFile; |
| 517 | } |
| 518 | |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 519 | Archive::Archive(MemoryBufferRef Source, Error &Err) |
Rafael Espindola | 4335876 | 2015-10-31 21:44:42 +0000 | [diff] [blame] | 520 | : Binary(Binary::ID_Archive, Source) { |
Lang Hames | 5e51a2e | 2016-07-22 16:11:25 +0000 | [diff] [blame] | 521 | ErrorAsOutParameter ErrAsOutParam(&Err); |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 522 | StringRef Buffer = Data.getBuffer(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 523 | // Check for sufficient magic. |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 524 | if (Buffer.startswith(ThinMagic)) { |
| 525 | IsThin = true; |
| 526 | } else if (Buffer.startswith(Magic)) { |
| 527 | IsThin = false; |
| 528 | } else { |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 529 | Err = make_error<GenericBinaryError>("File too small to be an archive", |
| 530 | object_error::invalid_file_type); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 531 | return; |
| 532 | } |
| 533 | |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 534 | // Make sure Format is initialized before any call to |
| 535 | // ArchiveMemberHeader::getName() is made. This could be a valid empty |
| 536 | // archive which is the same in all formats. So claiming it to be gnu to is |
| 537 | // fine if not totally correct before we look for a string table or table of |
| 538 | // contents. |
| 539 | Format = K_GNU; |
| 540 | |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 541 | // Get the special members. |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 542 | child_iterator I = child_begin(Err, false); |
| 543 | if (Err) |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 544 | return; |
| 545 | child_iterator E = child_end(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 546 | |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 547 | // See if this is a valid empty archive and if so return. |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 548 | if (I == E) { |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 549 | Err = Error::success(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 550 | return; |
| 551 | } |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 552 | const Child *C = &*I; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 553 | |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 554 | auto Increment = [&]() { |
| 555 | ++I; |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 556 | if (Err) |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 557 | return true; |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 558 | C = &*I; |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 559 | return false; |
| 560 | }; |
| 561 | |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 562 | Expected<StringRef> NameOrErr = C->getRawName(); |
| 563 | if (!NameOrErr) { |
| 564 | Err = NameOrErr.takeError(); |
| 565 | return; |
| 566 | } |
| 567 | StringRef Name = NameOrErr.get(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 568 | |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 569 | // Below is the pattern that is used to figure out the archive format |
| 570 | // GNU archive format |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 571 | // First member : / (may exist, if it exists, points to the symbol table ) |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 572 | // Second member : // (may exist, if it exists, points to the string table) |
| 573 | // Note : The string table is used if the filename exceeds 15 characters |
| 574 | // BSD archive format |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 575 | // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table) |
| 576 | // There is no string table, if the filename exceeds 15 characters or has a |
| 577 | // embedded space, the filename has #1/<size>, The size represents the size |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 578 | // of the filename that needs to be read after the archive header |
| 579 | // COFF archive format |
| 580 | // First member : / |
| 581 | // Second member : / (provides a directory of symbols) |
Rui Ueyama | f4d0a8c | 2013-06-03 00:27:03 +0000 | [diff] [blame] | 582 | // Third member : // (may exist, if it exists, contains the string table) |
| 583 | // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present |
| 584 | // even if the string table is empty. However, lib.exe does not in fact |
| 585 | // seem to create the third member if there's no member whose filename |
| 586 | // exceeds 15 characters. So the third member is optional. |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 587 | |
Kevin Enderby | ae108ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 588 | if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") { |
| 589 | if (Name == "__.SYMDEF") |
| 590 | Format = K_BSD; |
| 591 | else // Name == "__.SYMDEF_64" |
| 592 | Format = K_DARWIN64; |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 593 | // We know that the symbol table is not an external file, but we still must |
| 594 | // check any Expected<> return value. |
| 595 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 596 | if (!BufOrErr) { |
| 597 | Err = BufOrErr.takeError(); |
| 598 | return; |
| 599 | } |
| 600 | SymbolTable = BufOrErr.get(); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 601 | if (Increment()) |
| 602 | return; |
| 603 | setFirstRegular(*C); |
| 604 | |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 605 | Err = Error::success(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 609 | if (Name.startswith("#1/")) { |
| 610 | Format = K_BSD; |
| 611 | // 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] | 612 | Expected<StringRef> NameOrErr = C->getName(); |
| 613 | if (!NameOrErr) { |
| 614 | Err = NameOrErr.takeError(); |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 615 | return; |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 616 | } |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 617 | Name = NameOrErr.get(); |
Nick Kledzik | f44dbda | 2014-11-12 01:37:45 +0000 | [diff] [blame] | 618 | if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") { |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 619 | // We know that the symbol table is not an external file, but we still |
| 620 | // must check any Expected<> return value. |
| 621 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 622 | if (!BufOrErr) { |
| 623 | Err = BufOrErr.takeError(); |
| 624 | return; |
| 625 | } |
| 626 | SymbolTable = BufOrErr.get(); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 627 | if (Increment()) |
| 628 | return; |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 629 | } |
Kevin Enderby | ae108ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 630 | else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") { |
| 631 | Format = K_DARWIN64; |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 632 | // We know that the symbol table is not an external file, but we still |
| 633 | // must check any Expected<> return value. |
| 634 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 635 | if (!BufOrErr) { |
| 636 | Err = BufOrErr.takeError(); |
| 637 | return; |
| 638 | } |
| 639 | SymbolTable = BufOrErr.get(); |
Kevin Enderby | ae108ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 640 | if (Increment()) |
| 641 | return; |
| 642 | } |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 643 | setFirstRegular(*C); |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 644 | return; |
| 645 | } |
| 646 | |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 647 | // MIPS 64-bit ELF archives use a special format of a symbol table. |
| 648 | // This format is marked by `ar_name` field equals to "/SYM64/". |
| 649 | // For detailed description see page 96 in the following document: |
| 650 | // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf |
| 651 | |
| 652 | bool has64SymTable = false; |
| 653 | if (Name == "/" || Name == "/SYM64/") { |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 654 | // We know that the symbol table is not an external file, but we still |
| 655 | // must check any Expected<> return value. |
| 656 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 657 | if (!BufOrErr) { |
| 658 | Err = BufOrErr.takeError(); |
| 659 | return; |
| 660 | } |
| 661 | SymbolTable = BufOrErr.get(); |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 662 | if (Name == "/SYM64/") |
| 663 | has64SymTable = true; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 664 | |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 665 | if (Increment()) |
| 666 | return; |
| 667 | if (I == E) { |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 668 | Err = Error::success(); |
Michael J. Spencer | 04614ff | 2013-01-10 00:07:38 +0000 | [diff] [blame] | 669 | return; |
| 670 | } |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 671 | Expected<StringRef> NameOrErr = C->getRawName(); |
| 672 | if (!NameOrErr) { |
| 673 | Err = NameOrErr.takeError(); |
| 674 | return; |
| 675 | } |
| 676 | Name = NameOrErr.get(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 679 | if (Name == "//") { |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 680 | Format = has64SymTable ? K_MIPS64 : K_GNU; |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 681 | // The string table is never an external member, but we still |
| 682 | // must check any Expected<> return value. |
| 683 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 684 | if (!BufOrErr) { |
| 685 | Err = BufOrErr.takeError(); |
| 686 | return; |
| 687 | } |
| 688 | StringTable = BufOrErr.get(); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 689 | if (Increment()) |
| 690 | return; |
| 691 | setFirstRegular(*C); |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 692 | Err = Error::success(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 693 | return; |
| 694 | } |
| 695 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 696 | if (Name[0] != '/') { |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 697 | Format = has64SymTable ? K_MIPS64 : K_GNU; |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 698 | setFirstRegular(*C); |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 699 | Err = Error::success(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 700 | return; |
| 701 | } |
| 702 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 703 | if (Name != "/") { |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 704 | Err = errorCodeToError(object_error::parse_failed); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | |
| 708 | Format = K_COFF; |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 709 | // We know that the symbol table is not an external file, but we still |
| 710 | // must check any Expected<> return value. |
| 711 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 712 | if (!BufOrErr) { |
| 713 | Err = BufOrErr.takeError(); |
| 714 | return; |
| 715 | } |
| 716 | SymbolTable = BufOrErr.get(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 717 | |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 718 | if (Increment()) |
| 719 | return; |
| 720 | |
| 721 | if (I == E) { |
| 722 | setFirstRegular(*C); |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 723 | Err = Error::success(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 727 | NameOrErr = C->getRawName(); |
| 728 | if (!NameOrErr) { |
| 729 | Err = NameOrErr.takeError(); |
| 730 | return; |
| 731 | } |
| 732 | Name = NameOrErr.get(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 733 | |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 734 | if (Name == "//") { |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 735 | // The string table is never an external member, but we still |
| 736 | // must check any Expected<> return value. |
| 737 | Expected<StringRef> BufOrErr = C->getBuffer(); |
| 738 | if (!BufOrErr) { |
| 739 | Err = BufOrErr.takeError(); |
| 740 | return; |
| 741 | } |
| 742 | StringTable = BufOrErr.get(); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 743 | if (Increment()) |
| 744 | return; |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 745 | } |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 746 | |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 747 | setFirstRegular(*C); |
Kevin Enderby | c60a321 | 2016-06-29 20:35:44 +0000 | [diff] [blame] | 748 | Err = Error::success(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 751 | Archive::child_iterator Archive::child_begin(Error &Err, |
| 752 | bool SkipInternal) const { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 753 | if (Data.getBufferSize() == 8) // empty archive. |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 754 | return child_end(); |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 755 | |
| 756 | if (SkipInternal) |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 757 | return child_iterator(Child(this, FirstRegularData, |
| 758 | FirstRegularStartOfFile), |
| 759 | &Err); |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 760 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 761 | const char *Loc = Data.getBufferStart() + strlen(Magic); |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 762 | Child C(this, Loc, &Err); |
| 763 | if (Err) |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 764 | return child_end(); |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 765 | return child_iterator(C, &Err); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 768 | Archive::child_iterator Archive::child_end() const { |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 769 | return child_iterator(Child(this, nullptr, nullptr), nullptr); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 770 | } |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 771 | |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 772 | StringRef Archive::Symbol::getName() const { |
Rafael Espindola | 2b05416 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 773 | return Parent->getSymbolTable().begin() + StringIndex; |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 776 | Expected<Archive::Child> Archive::Symbol::getMember() const { |
Rafael Espindola | 2b05416 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 777 | const char *Buf = Parent->getSymbolTable().begin(); |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 778 | const char *Offsets = Buf; |
Kevin Enderby | ae108ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 779 | if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64) |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 780 | Offsets += sizeof(uint64_t); |
| 781 | else |
| 782 | Offsets += sizeof(uint32_t); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 783 | uint32_t Offset = 0; |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 784 | if (Parent->kind() == K_GNU) { |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 785 | Offset = read32be(Offsets + SymbolIndex * 4); |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 786 | } else if (Parent->kind() == K_MIPS64) { |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 787 | Offset = read64be(Offsets + SymbolIndex * 8); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 788 | } else if (Parent->kind() == K_BSD) { |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 789 | // The SymbolIndex is an index into the ranlib structs that start at |
| 790 | // Offsets (the first uint32_t is the number of bytes of the ranlib |
| 791 | // structs). The ranlib structs are a pair of uint32_t's the first |
| 792 | // being a string table offset and the second being the offset into |
| 793 | // the archive of the member that defines the symbol. Which is what |
| 794 | // is needed here. |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 795 | Offset = read32le(Offsets + SymbolIndex * 8 + 4); |
Kevin Enderby | ae108ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 796 | } else if (Parent->kind() == K_DARWIN64) { |
| 797 | // The SymbolIndex is an index into the ranlib_64 structs that start at |
| 798 | // Offsets (the first uint64_t is the number of bytes of the ranlib_64 |
| 799 | // structs). The ranlib_64 structs are a pair of uint64_t's the first |
| 800 | // being a string table offset and the second being the offset into |
| 801 | // the archive of the member that defines the symbol. Which is what |
| 802 | // is needed here. |
| 803 | Offset = read64le(Offsets + SymbolIndex * 16 + 8); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 804 | } else { |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 805 | // Skip offsets. |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 806 | uint32_t MemberCount = read32le(Buf); |
| 807 | Buf += MemberCount * 4 + 4; |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 808 | |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 809 | uint32_t SymbolCount = read32le(Buf); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 810 | if (SymbolIndex >= SymbolCount) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 811 | return errorCodeToError(object_error::parse_failed); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 812 | |
Matt Beaumont-Gay | 68e0b6a | 2012-11-14 00:21:27 +0000 | [diff] [blame] | 813 | // Skip SymbolCount to get to the indices table. |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 814 | const char *Indices = Buf + 4; |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 815 | |
| 816 | // Get the index of the offset in the file member offset table for this |
| 817 | // symbol. |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 818 | uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 819 | // Subtract 1 since OffsetIndex is 1 based. |
| 820 | --OffsetIndex; |
| 821 | |
| 822 | if (OffsetIndex >= MemberCount) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 823 | return errorCodeToError(object_error::parse_failed); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 824 | |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 825 | Offset = read32le(Offsets + OffsetIndex * 4); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 826 | } |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 827 | |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 828 | const char *Loc = Parent->getData().begin() + Offset; |
Kevin Enderby | 6524bd8 | 2016-07-19 20:47:07 +0000 | [diff] [blame] | 829 | Error Err; |
| 830 | Child C(Parent, Loc, &Err); |
| 831 | if (Err) |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 832 | return std::move(Err); |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 833 | return C; |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | Archive::Symbol Archive::Symbol::getNext() const { |
| 837 | Symbol t(*this); |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 838 | if (Parent->kind() == K_BSD) { |
| 839 | // t.StringIndex is an offset from the start of the __.SYMDEF or |
| 840 | // "__.SYMDEF SORTED" member into the string table for the ranlib |
| 841 | // struct indexed by t.SymbolIndex . To change t.StringIndex to the |
| 842 | // offset in the string table for t.SymbolIndex+1 we subtract the |
| 843 | // its offset from the start of the string table for t.SymbolIndex |
| 844 | // and add the offset of the string table for t.SymbolIndex+1. |
| 845 | |
| 846 | // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t |
| 847 | // which is the number of bytes of ranlib structs that follow. The ranlib |
| 848 | // structs are a pair of uint32_t's the first being a string table offset |
| 849 | // and the second being the offset into the archive of the member that |
| 850 | // define the symbol. After that the next uint32_t is the byte count of |
| 851 | // the string table followed by the string table. |
Rafael Espindola | 2b05416 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 852 | const char *Buf = Parent->getSymbolTable().begin(); |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 853 | uint32_t RanlibCount = 0; |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 854 | RanlibCount = read32le(Buf) / 8; |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 855 | // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount) |
| 856 | // don't change the t.StringIndex as we don't want to reference a ranlib |
| 857 | // past RanlibCount. |
| 858 | if (t.SymbolIndex + 1 < RanlibCount) { |
| 859 | const char *Ranlibs = Buf + 4; |
| 860 | uint32_t CurRanStrx = 0; |
| 861 | uint32_t NextRanStrx = 0; |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 862 | CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8); |
| 863 | NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8); |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 864 | t.StringIndex -= CurRanStrx; |
| 865 | t.StringIndex += NextRanStrx; |
| 866 | } |
| 867 | } else { |
| 868 | // Go to one past next null. |
Rafael Espindola | 2b05416 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 869 | t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1; |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 870 | } |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 871 | ++t.SymbolIndex; |
| 872 | return t; |
| 873 | } |
| 874 | |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 875 | Archive::symbol_iterator Archive::symbol_begin() const { |
Rafael Espindola | b6b5f52e | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 876 | if (!hasSymbolTable()) |
Rafael Espindola | fbcafc0 | 2013-07-10 20:14:22 +0000 | [diff] [blame] | 877 | return symbol_iterator(Symbol(this, 0, 0)); |
| 878 | |
Rafael Espindola | 2b05416 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 879 | const char *buf = getSymbolTable().begin(); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 880 | if (kind() == K_GNU) { |
| 881 | uint32_t symbol_count = 0; |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 882 | symbol_count = read32be(buf); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 883 | buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 884 | } else if (kind() == K_MIPS64) { |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 885 | uint64_t symbol_count = read64be(buf); |
Simon Atanasyan | 1d902b7 | 2015-02-17 18:54:22 +0000 | [diff] [blame] | 886 | buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t))); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 887 | } else if (kind() == K_BSD) { |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 888 | // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t |
| 889 | // which is the number of bytes of ranlib structs that follow. The ranlib |
| 890 | // structs are a pair of uint32_t's the first being a string table offset |
| 891 | // and the second being the offset into the archive of the member that |
| 892 | // define the symbol. After that the next uint32_t is the byte count of |
| 893 | // the string table followed by the string table. |
| 894 | uint32_t ranlib_count = 0; |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 895 | ranlib_count = read32le(buf) / 8; |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 896 | const char *ranlibs = buf + 4; |
| 897 | uint32_t ran_strx = 0; |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 898 | ran_strx = read32le(ranlibs); |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 899 | buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t)))); |
| 900 | // Skip the byte count of the string table. |
| 901 | buf += sizeof(uint32_t); |
| 902 | buf += ran_strx; |
Kevin Enderby | ae108ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 903 | } else if (kind() == K_DARWIN64) { |
| 904 | // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t |
| 905 | // which is the number of bytes of ranlib_64 structs that follow. The |
| 906 | // ranlib_64 structs are a pair of uint64_t's the first being a string |
| 907 | // table offset and the second being the offset into the archive of the |
| 908 | // member that define the symbol. After that the next uint64_t is the byte |
| 909 | // count of the string table followed by the string table. |
| 910 | uint64_t ranlib_count = 0; |
| 911 | ranlib_count = read64le(buf) / 16; |
| 912 | const char *ranlibs = buf + 8; |
| 913 | uint64_t ran_strx = 0; |
| 914 | ran_strx = read64le(ranlibs); |
| 915 | buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t)))); |
| 916 | // Skip the byte count of the string table. |
| 917 | buf += sizeof(uint64_t); |
| 918 | buf += ran_strx; |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 919 | } else { |
| 920 | uint32_t member_count = 0; |
| 921 | uint32_t symbol_count = 0; |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 922 | member_count = read32le(buf); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 923 | buf += 4 + (member_count * 4); // Skip offsets. |
Rui Ueyama | 3206b79 | 2015-03-02 21:19:12 +0000 | [diff] [blame] | 924 | symbol_count = read32le(buf); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 925 | buf += 4 + (symbol_count * 2); // Skip indices. |
| 926 | } |
Rafael Espindola | 2b05416 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 927 | uint32_t string_start_offset = buf - getSymbolTable().begin(); |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 928 | return symbol_iterator(Symbol(this, 0, string_start_offset)); |
| 929 | } |
| 930 | |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 931 | Archive::symbol_iterator Archive::symbol_end() const { |
Rui Ueyama | 407e097 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 932 | return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0)); |
| 933 | } |
Rafael Espindola | fbcafc0 | 2013-07-10 20:14:22 +0000 | [diff] [blame] | 934 | |
Rui Ueyama | 407e097 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 935 | uint32_t Archive::getNumberOfSymbols() const { |
Rafael Espindola | 483ad20 | 2015-10-08 18:06:20 +0000 | [diff] [blame] | 936 | if (!hasSymbolTable()) |
| 937 | return 0; |
Rafael Espindola | 2b05416 | 2015-07-14 01:06:16 +0000 | [diff] [blame] | 938 | const char *buf = getSymbolTable().begin(); |
Rui Ueyama | 407e097 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 939 | if (kind() == K_GNU) |
| 940 | return read32be(buf); |
| 941 | if (kind() == K_MIPS64) |
| 942 | return read64be(buf); |
| 943 | if (kind() == K_BSD) |
| 944 | return read32le(buf) / 8; |
Kevin Enderby | ae108ff | 2016-06-17 22:16:06 +0000 | [diff] [blame] | 945 | if (kind() == K_DARWIN64) |
| 946 | return read64le(buf) / 16; |
Rui Ueyama | 407e097 | 2015-05-26 16:20:40 +0000 | [diff] [blame] | 947 | uint32_t member_count = 0; |
| 948 | member_count = read32le(buf); |
| 949 | buf += 4 + (member_count * 4); // Skip offsets. |
| 950 | return read32le(buf); |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 951 | } |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 952 | |
Lang Hames | 69f4902 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 953 | Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const { |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 954 | Archive::symbol_iterator bs = symbol_begin(); |
| 955 | Archive::symbol_iterator es = symbol_end(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 956 | |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 957 | for (; bs != es; ++bs) { |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 958 | StringRef SymName = bs->getName(); |
| 959 | if (SymName == name) { |
Lang Hames | 69f4902 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 960 | if (auto MemberOrErr = bs->getMember()) |
| 961 | return Child(*MemberOrErr); |
| 962 | else |
Kevin Enderby | 27e85bd | 2016-08-03 21:57:47 +0000 | [diff] [blame] | 963 | return MemberOrErr.takeError(); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
Lang Hames | 69f4902 | 2016-07-14 20:44:27 +0000 | [diff] [blame] | 966 | return Optional<Child>(); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 967 | } |
Rafael Espindola | b6b5f52e | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 968 | |
Rafael Espindola | 4a782fb | 2015-10-31 21:03:29 +0000 | [diff] [blame] | 969 | bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); } |