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" |
| 15 | #include "llvm/ADT/APInt.h" |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
| 17 | #include "llvm/ADT/Twine.h" |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Endian.h" |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace object; |
| 23 | |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 24 | static const char *const Magic = "!<arch>\n"; |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 25 | static const char *const ThinMagic = "!<thin>\n"; |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 26 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 27 | void Archive::anchor() { } |
| 28 | |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 29 | StringRef ArchiveMemberHeader::getName() const { |
| 30 | char EndCond; |
| 31 | if (Name[0] == '/' || Name[0] == '#') |
| 32 | EndCond = ' '; |
| 33 | else |
| 34 | EndCond = '/'; |
| 35 | llvm::StringRef::size_type end = |
| 36 | llvm::StringRef(Name, sizeof(Name)).find(EndCond); |
| 37 | if (end == llvm::StringRef::npos) |
| 38 | end = sizeof(Name); |
| 39 | assert(end <= sizeof(Name) && end > 0); |
| 40 | // Don't include the EndCond if there is one. |
| 41 | return llvm::StringRef(Name, end); |
| 42 | } |
| 43 | |
Rafael Espindola | 8e9385e | 2013-07-09 12:45:11 +0000 | [diff] [blame] | 44 | uint32_t ArchiveMemberHeader::getSize() const { |
| 45 | uint32_t Ret; |
| 46 | if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret)) |
| 47 | llvm_unreachable("Size is not a decimal number."); |
| 48 | return Ret; |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Rafael Espindola | 8115e1d | 2013-07-09 12:49:24 +0000 | [diff] [blame] | 51 | sys::fs::perms ArchiveMemberHeader::getAccessMode() const { |
| 52 | unsigned Ret; |
| 53 | if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret)) |
| 54 | llvm_unreachable("Access mode is not an octal number."); |
| 55 | return static_cast<sys::fs::perms>(Ret); |
| 56 | } |
| 57 | |
| 58 | sys::TimeValue ArchiveMemberHeader::getLastModified() const { |
| 59 | unsigned Seconds; |
| 60 | if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ") |
| 61 | .getAsInteger(10, Seconds)) |
| 62 | llvm_unreachable("Last modified time not a decimal number."); |
| 63 | |
| 64 | sys::TimeValue Ret; |
| 65 | Ret.fromEpochTime(Seconds); |
| 66 | return Ret; |
| 67 | } |
| 68 | |
| 69 | unsigned ArchiveMemberHeader::getUID() const { |
| 70 | unsigned Ret; |
| 71 | if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret)) |
| 72 | llvm_unreachable("UID time not a decimal number."); |
| 73 | return Ret; |
| 74 | } |
| 75 | |
| 76 | unsigned ArchiveMemberHeader::getGID() const { |
| 77 | unsigned Ret; |
| 78 | if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret)) |
| 79 | llvm_unreachable("GID time not a decimal number."); |
| 80 | return Ret; |
| 81 | } |
| 82 | |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 83 | Archive::Child::Child(const Archive *Parent, const char *Start) |
| 84 | : Parent(Parent) { |
| 85 | if (!Start) |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 86 | return; |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 87 | |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 88 | const ArchiveMemberHeader *Header = |
| 89 | reinterpret_cast<const ArchiveMemberHeader *>(Start); |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 90 | uint64_t Size = sizeof(ArchiveMemberHeader); |
| 91 | if (!Parent->IsThin || Header->getName() == "/" || Header->getName() == "//") |
| 92 | Size += Header->getSize(); |
| 93 | Data = StringRef(Start, Size); |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 94 | |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 95 | // Setup StartOfFile and PaddingBytes. |
| 96 | StartOfFile = sizeof(ArchiveMemberHeader); |
| 97 | // Don't include attached name. |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 98 | StringRef Name = Header->getName(); |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 99 | if (Name.startswith("#1/")) { |
| 100 | uint64_t NameSize; |
| 101 | if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize)) |
| 102 | llvm_unreachable("Long name length is not an integer"); |
| 103 | StartOfFile += NameSize; |
| 104 | } |
| 105 | } |
| 106 | |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 107 | uint64_t Archive::Child::getSize() const { |
| 108 | if (Parent->IsThin) |
| 109 | return getHeader()->getSize(); |
| 110 | return Data.size() - StartOfFile; |
| 111 | } |
| 112 | |
Kevin Enderby | 13023a1 | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 113 | uint64_t Archive::Child::getRawSize() const { |
Kevin Enderby | c1271893 | 2015-01-16 22:10:36 +0000 | [diff] [blame] | 114 | return getHeader()->getSize(); |
Kevin Enderby | 13023a1 | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 117 | Archive::Child Archive::Child::getNext() const { |
| 118 | size_t SpaceToSkip = Data.size(); |
| 119 | // If it's odd, add 1 to make it even. |
| 120 | if (SpaceToSkip & 1) |
| 121 | ++SpaceToSkip; |
| 122 | |
| 123 | const char *NextLoc = Data.data() + SpaceToSkip; |
| 124 | |
| 125 | // Check to see if this is past the end of the archive. |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 126 | if (NextLoc >= Parent->Data.getBufferEnd()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 127 | return Child(Parent, nullptr); |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 128 | |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 129 | return Child(Parent, NextLoc); |
Rafael Espindola | 747bc07 | 2013-07-09 03:39:35 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Kevin Enderby | 13023a1 | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 132 | uint64_t Archive::Child::getChildOffset() const { |
| 133 | const char *a = Parent->Data.getBuffer().data(); |
| 134 | const char *c = Data.data(); |
| 135 | uint64_t offset = c - a; |
| 136 | return offset; |
| 137 | } |
| 138 | |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 139 | ErrorOr<StringRef> Archive::Child::getName() const { |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 140 | StringRef name = getRawName(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 141 | // Check if it's a special name. |
| 142 | if (name[0] == '/') { |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 143 | if (name.size() == 1) // Linker member. |
| 144 | return name; |
| 145 | if (name.size() == 2 && name[1] == '/') // String table. |
| 146 | return name; |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 147 | // It's a long name. |
| 148 | // Get the offset. |
Michael J. Spencer | 04614ff | 2013-01-10 00:07:38 +0000 | [diff] [blame] | 149 | std::size_t offset; |
Michael J. Spencer | 8c71e92 | 2013-01-10 01:05:34 +0000 | [diff] [blame] | 150 | if (name.substr(1).rtrim(" ").getAsInteger(10, offset)) |
| 151 | llvm_unreachable("Long name offset is not an integer"); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 152 | const char *addr = Parent->StringTable->Data.begin() |
| 153 | + sizeof(ArchiveMemberHeader) |
Michael J. Spencer | 751fd88 | 2013-01-09 22:58:43 +0000 | [diff] [blame] | 154 | + offset; |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 155 | // Verify it. |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 156 | if (Parent->StringTable == Parent->child_end() |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 157 | || addr < (Parent->StringTable->Data.begin() |
| 158 | + sizeof(ArchiveMemberHeader)) |
| 159 | || addr > (Parent->StringTable->Data.begin() |
| 160 | + sizeof(ArchiveMemberHeader) |
| 161 | + Parent->StringTable->getSize())) |
| 162 | return object_error::parse_failed; |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 163 | |
| 164 | // GNU long file names end with a /. |
| 165 | if (Parent->kind() == K_GNU) { |
| 166 | StringRef::size_type End = StringRef(addr).find('/'); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 167 | return StringRef(addr, End); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 168 | } |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 169 | return StringRef(addr); |
Michael J. Spencer | 9aaa852 | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 170 | } else if (name.startswith("#1/")) { |
Michael J. Spencer | 751fd88 | 2013-01-09 22:58:43 +0000 | [diff] [blame] | 171 | uint64_t name_size; |
Michael J. Spencer | 8c71e92 | 2013-01-10 01:05:34 +0000 | [diff] [blame] | 172 | if (name.substr(3).rtrim(" ").getAsInteger(10, name_size)) |
| 173 | llvm_unreachable("Long name length is not an ingeter"); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 174 | return Data.substr(sizeof(ArchiveMemberHeader), name_size) |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 175 | .rtrim(StringRef("\0", 1)); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 176 | } |
| 177 | // It's a simple name. |
| 178 | if (name[name.size() - 1] == '/') |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 179 | return name.substr(0, name.size() - 1); |
| 180 | return name; |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 183 | ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const { |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 184 | ErrorOr<StringRef> NameOrErr = getName(); |
| 185 | if (std::error_code EC = NameOrErr.getError()) |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 186 | return EC; |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 187 | StringRef Name = NameOrErr.get(); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 188 | return MemoryBufferRef(getBuffer(), Name); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | ErrorOr<std::unique_ptr<Binary>> |
| 192 | Archive::Child::getAsBinary(LLVMContext *Context) const { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 193 | ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 194 | if (std::error_code EC = BuffOrErr.getError()) |
| 195 | return EC; |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 196 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 197 | return createBinary(BuffOrErr.get(), Context); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 200 | ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 201 | std::error_code EC; |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 202 | std::unique_ptr<Archive> Ret(new Archive(Source, EC)); |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 203 | if (EC) |
| 204 | return EC; |
Rafael Espindola | f557713 | 2014-07-31 03:36:00 +0000 | [diff] [blame] | 205 | return std::move(Ret); |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 208 | Archive::Archive(MemoryBufferRef Source, std::error_code &ec) |
| 209 | : Binary(Binary::ID_Archive, Source), SymbolTable(child_end()) { |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 210 | StringRef Buffer = Data.getBuffer(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 211 | // Check for sufficient magic. |
Rafael Espindola | 9d10206 | 2014-12-16 01:43:41 +0000 | [diff] [blame] | 212 | if (Buffer.startswith(ThinMagic)) { |
| 213 | IsThin = true; |
| 214 | } else if (Buffer.startswith(Magic)) { |
| 215 | IsThin = false; |
| 216 | } else { |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 217 | ec = object_error::invalid_file_type; |
| 218 | return; |
| 219 | } |
| 220 | |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 221 | // Get the special members. |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 222 | child_iterator i = child_begin(false); |
| 223 | child_iterator e = child_end(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 224 | |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 225 | if (i == e) { |
Rafael Espindola | f0c6172 | 2013-07-12 13:32:28 +0000 | [diff] [blame] | 226 | ec = object_error::success; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 230 | StringRef Name = i->getRawName(); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 231 | |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 232 | // Below is the pattern that is used to figure out the archive format |
| 233 | // GNU archive format |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 234 | // First member : / (may exist, if it exists, points to the symbol table ) |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 235 | // Second member : // (may exist, if it exists, points to the string table) |
| 236 | // Note : The string table is used if the filename exceeds 15 characters |
| 237 | // BSD archive format |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 238 | // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table) |
| 239 | // There is no string table, if the filename exceeds 15 characters or has a |
| 240 | // embedded space, the filename has #1/<size>, The size represents the size |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 241 | // of the filename that needs to be read after the archive header |
| 242 | // COFF archive format |
| 243 | // First member : / |
| 244 | // Second member : / (provides a directory of symbols) |
Rui Ueyama | f4d0a8c | 2013-06-03 00:27:03 +0000 | [diff] [blame] | 245 | // Third member : // (may exist, if it exists, contains the string table) |
| 246 | // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present |
| 247 | // even if the string table is empty. However, lib.exe does not in fact |
| 248 | // seem to create the third member if there's no member whose filename |
| 249 | // exceeds 15 characters. So the third member is optional. |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 250 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 251 | if (Name == "__.SYMDEF") { |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 252 | Format = K_BSD; |
| 253 | SymbolTable = i; |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 254 | ++i; |
| 255 | FirstRegular = i; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 256 | ec = object_error::success; |
| 257 | return; |
| 258 | } |
| 259 | |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 260 | if (Name.startswith("#1/")) { |
| 261 | Format = K_BSD; |
| 262 | // We know this is BSD, so getName will work since there is no string table. |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 263 | ErrorOr<StringRef> NameOrErr = i->getName(); |
| 264 | ec = NameOrErr.getError(); |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 265 | if (ec) |
| 266 | return; |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 267 | Name = NameOrErr.get(); |
Nick Kledzik | f44dbda | 2014-11-12 01:37:45 +0000 | [diff] [blame] | 268 | if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") { |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 269 | SymbolTable = i; |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 270 | ++i; |
| 271 | } |
| 272 | FirstRegular = i; |
Rafael Espindola | 5550992 | 2013-07-10 22:07:59 +0000 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 276 | if (Name == "/") { |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 277 | SymbolTable = i; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 278 | |
| 279 | ++i; |
Michael J. Spencer | 04614ff | 2013-01-10 00:07:38 +0000 | [diff] [blame] | 280 | if (i == e) { |
| 281 | ec = object_error::parse_failed; |
| 282 | return; |
| 283 | } |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 284 | Name = i->getRawName(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 287 | if (Name == "//") { |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 288 | Format = K_GNU; |
| 289 | StringTable = i; |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 290 | ++i; |
| 291 | FirstRegular = i; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 292 | ec = object_error::success; |
| 293 | return; |
| 294 | } |
| 295 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 296 | if (Name[0] != '/') { |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 297 | Format = K_GNU; |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 298 | FirstRegular = i; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 299 | ec = object_error::success; |
| 300 | return; |
| 301 | } |
| 302 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 303 | if (Name != "/") { |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 304 | ec = object_error::parse_failed; |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | Format = K_COFF; |
| 309 | SymbolTable = i; |
| 310 | |
| 311 | ++i; |
| 312 | if (i == e) { |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 313 | FirstRegular = i; |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 314 | ec = object_error::success; |
| 315 | return; |
| 316 | } |
| 317 | |
Rafael Espindola | 6cc2dc7 | 2013-07-05 03:35:15 +0000 | [diff] [blame] | 318 | Name = i->getRawName(); |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 319 | |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 320 | if (Name == "//") { |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 321 | StringTable = i; |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 322 | ++i; |
| 323 | } |
Rafael Espindola | 88ae7dd | 2013-07-03 15:57:14 +0000 | [diff] [blame] | 324 | |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 325 | FirstRegular = i; |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 326 | ec = object_error::success; |
| 327 | } |
| 328 | |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 329 | Archive::child_iterator Archive::child_begin(bool SkipInternal) const { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 330 | if (Data.getBufferSize() == 8) // empty archive. |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 331 | return child_end(); |
Rafael Espindola | 3e2b21c | 2013-07-12 20:21:39 +0000 | [diff] [blame] | 332 | |
| 333 | if (SkipInternal) |
| 334 | return FirstRegular; |
| 335 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 336 | const char *Loc = Data.getBufferStart() + strlen(Magic); |
Rafael Espindola | 0f3de64 | 2013-07-09 05:26:25 +0000 | [diff] [blame] | 337 | Child c(this, Loc); |
Michael J. Spencer | 9aaa852 | 2011-10-25 22:30:42 +0000 | [diff] [blame] | 338 | return c; |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 341 | Archive::child_iterator Archive::child_end() const { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 342 | return Child(this, nullptr); |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 343 | } |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 344 | |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 345 | StringRef Archive::Symbol::getName() const { |
| 346 | return Parent->SymbolTable->getBuffer().begin() + StringIndex; |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 349 | ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const { |
Michael J. Spencer | 9718f45 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 350 | const char *Buf = Parent->SymbolTable->getBuffer().begin(); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 351 | const char *Offsets = Buf + 4; |
| 352 | uint32_t Offset = 0; |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 353 | if (Parent->kind() == K_GNU) { |
Simon Atanasyan | 0ca5989 | 2015-02-10 21:38:25 +0000 | [diff] [blame] | 354 | Offset = |
| 355 | *(reinterpret_cast<const support::ubig32_t *>(Offsets) + SymbolIndex); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 356 | } else if (Parent->kind() == K_BSD) { |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 357 | // The SymbolIndex is an index into the ranlib structs that start at |
| 358 | // Offsets (the first uint32_t is the number of bytes of the ranlib |
| 359 | // structs). The ranlib structs are a pair of uint32_t's the first |
| 360 | // being a string table offset and the second being the offset into |
| 361 | // the archive of the member that defines the symbol. Which is what |
| 362 | // is needed here. |
| 363 | Offset = *(reinterpret_cast<const support::ulittle32_t *>(Offsets) + |
| 364 | (SymbolIndex * 2) + 1); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 365 | } else { |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 366 | uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf); |
| 367 | |
| 368 | // Skip offsets. |
Simon Atanasyan | 0ca5989 | 2015-02-10 21:38:25 +0000 | [diff] [blame] | 369 | Buf += sizeof(support::ulittle32_t) + |
| 370 | (MemberCount * sizeof(support::ulittle32_t)); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 371 | |
| 372 | uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf); |
| 373 | |
| 374 | if (SymbolIndex >= SymbolCount) |
| 375 | return object_error::parse_failed; |
| 376 | |
Matt Beaumont-Gay | 68e0b6a | 2012-11-14 00:21:27 +0000 | [diff] [blame] | 377 | // Skip SymbolCount to get to the indices table. |
| 378 | const char *Indices = Buf + sizeof(support::ulittle32_t); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 379 | |
| 380 | // Get the index of the offset in the file member offset table for this |
| 381 | // symbol. |
| 382 | uint16_t OffsetIndex = |
Matt Beaumont-Gay | 68e0b6a | 2012-11-14 00:21:27 +0000 | [diff] [blame] | 383 | *(reinterpret_cast<const support::ulittle16_t*>(Indices) |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 384 | + SymbolIndex); |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 385 | // Subtract 1 since OffsetIndex is 1 based. |
| 386 | --OffsetIndex; |
| 387 | |
| 388 | if (OffsetIndex >= MemberCount) |
| 389 | return object_error::parse_failed; |
| 390 | |
| 391 | Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets) |
| 392 | + OffsetIndex); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 393 | } |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 394 | |
Michael J. Spencer | 4e92d5b | 2012-11-14 00:04:13 +0000 | [diff] [blame] | 395 | const char *Loc = Parent->getData().begin() + Offset; |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 396 | child_iterator Iter(Child(Parent, Loc)); |
| 397 | return Iter; |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | Archive::Symbol Archive::Symbol::getNext() const { |
| 401 | Symbol t(*this); |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 402 | if (Parent->kind() == K_BSD) { |
| 403 | // t.StringIndex is an offset from the start of the __.SYMDEF or |
| 404 | // "__.SYMDEF SORTED" member into the string table for the ranlib |
| 405 | // struct indexed by t.SymbolIndex . To change t.StringIndex to the |
| 406 | // offset in the string table for t.SymbolIndex+1 we subtract the |
| 407 | // its offset from the start of the string table for t.SymbolIndex |
| 408 | // and add the offset of the string table for t.SymbolIndex+1. |
| 409 | |
| 410 | // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t |
| 411 | // which is the number of bytes of ranlib structs that follow. The ranlib |
| 412 | // structs are a pair of uint32_t's the first being a string table offset |
| 413 | // and the second being the offset into the archive of the member that |
| 414 | // define the symbol. After that the next uint32_t is the byte count of |
| 415 | // the string table followed by the string table. |
| 416 | const char *Buf = Parent->SymbolTable->getBuffer().begin(); |
| 417 | uint32_t RanlibCount = 0; |
| 418 | RanlibCount = (*reinterpret_cast<const support::ulittle32_t *>(Buf)) / |
| 419 | (sizeof(uint32_t) * 2); |
| 420 | // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount) |
| 421 | // don't change the t.StringIndex as we don't want to reference a ranlib |
| 422 | // past RanlibCount. |
| 423 | if (t.SymbolIndex + 1 < RanlibCount) { |
| 424 | const char *Ranlibs = Buf + 4; |
| 425 | uint32_t CurRanStrx = 0; |
| 426 | uint32_t NextRanStrx = 0; |
| 427 | CurRanStrx = *(reinterpret_cast<const support::ulittle32_t *>(Ranlibs) + |
| 428 | (t.SymbolIndex * 2)); |
| 429 | NextRanStrx = *(reinterpret_cast<const support::ulittle32_t *>(Ranlibs) + |
| 430 | ((t.SymbolIndex + 1) * 2)); |
| 431 | t.StringIndex -= CurRanStrx; |
| 432 | t.StringIndex += NextRanStrx; |
| 433 | } |
| 434 | } else { |
| 435 | // Go to one past next null. |
| 436 | t.StringIndex = |
| 437 | Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1; |
| 438 | } |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 439 | ++t.SymbolIndex; |
| 440 | return t; |
| 441 | } |
| 442 | |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 443 | Archive::symbol_iterator Archive::symbol_begin() const { |
Rafael Espindola | b6b5f52e | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 444 | if (!hasSymbolTable()) |
Rafael Espindola | fbcafc0 | 2013-07-10 20:14:22 +0000 | [diff] [blame] | 445 | return symbol_iterator(Symbol(this, 0, 0)); |
| 446 | |
Michael J. Spencer | 9718f45 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 447 | const char *buf = SymbolTable->getBuffer().begin(); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 448 | if (kind() == K_GNU) { |
| 449 | uint32_t symbol_count = 0; |
| 450 | symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf); |
| 451 | buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); |
| 452 | } else if (kind() == K_BSD) { |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 453 | // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t |
| 454 | // which is the number of bytes of ranlib structs that follow. The ranlib |
| 455 | // structs are a pair of uint32_t's the first being a string table offset |
| 456 | // and the second being the offset into the archive of the member that |
| 457 | // define the symbol. After that the next uint32_t is the byte count of |
| 458 | // the string table followed by the string table. |
| 459 | uint32_t ranlib_count = 0; |
| 460 | ranlib_count = (*reinterpret_cast<const support::ulittle32_t *>(buf)) / |
| 461 | (sizeof(uint32_t) * 2); |
| 462 | const char *ranlibs = buf + 4; |
| 463 | uint32_t ran_strx = 0; |
| 464 | ran_strx = *(reinterpret_cast<const support::ulittle32_t *>(ranlibs)); |
| 465 | buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t)))); |
| 466 | // Skip the byte count of the string table. |
| 467 | buf += sizeof(uint32_t); |
| 468 | buf += ran_strx; |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 469 | } else { |
| 470 | uint32_t member_count = 0; |
| 471 | uint32_t symbol_count = 0; |
| 472 | member_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 473 | buf += 4 + (member_count * 4); // Skip offsets. |
| 474 | symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 475 | buf += 4 + (symbol_count * 2); // Skip indices. |
| 476 | } |
Michael J. Spencer | 9718f45 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 477 | uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin(); |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 478 | return symbol_iterator(Symbol(this, 0, string_start_offset)); |
| 479 | } |
| 480 | |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 481 | Archive::symbol_iterator Archive::symbol_end() const { |
Rafael Espindola | b6b5f52e | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 482 | if (!hasSymbolTable()) |
Rafael Espindola | fbcafc0 | 2013-07-10 20:14:22 +0000 | [diff] [blame] | 483 | return symbol_iterator(Symbol(this, 0, 0)); |
| 484 | |
Michael J. Spencer | 9718f45 | 2013-02-03 10:48:50 +0000 | [diff] [blame] | 485 | const char *buf = SymbolTable->getBuffer().begin(); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 486 | uint32_t symbol_count = 0; |
| 487 | if (kind() == K_GNU) { |
| 488 | symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 489 | } else if (kind() == K_BSD) { |
Kevin Enderby | 8c50dbb | 2014-07-08 22:10:02 +0000 | [diff] [blame] | 490 | symbol_count = (*reinterpret_cast<const support::ulittle32_t *>(buf)) / |
| 491 | (sizeof(uint32_t) * 2); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 492 | } else { |
| 493 | uint32_t member_count = 0; |
| 494 | member_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 495 | buf += 4 + (member_count * 4); // Skip offsets. |
| 496 | symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf); |
| 497 | } |
Simon Atanasyan | 0ca5989 | 2015-02-10 21:38:25 +0000 | [diff] [blame] | 498 | return symbol_iterator(Symbol(this, symbol_count, 0)); |
Michael J. Spencer | e03ea9c | 2011-11-02 19:33:12 +0000 | [diff] [blame] | 499 | } |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 500 | |
| 501 | Archive::child_iterator Archive::findSym(StringRef name) const { |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 502 | Archive::symbol_iterator bs = symbol_begin(); |
| 503 | Archive::symbol_iterator es = symbol_end(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 504 | |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 505 | for (; bs != es; ++bs) { |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 506 | StringRef SymName = bs->getName(); |
| 507 | if (SymName == name) { |
| 508 | ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember(); |
| 509 | // FIXME: Should we really eat the error? |
| 510 | if (ResultOrErr.getError()) |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 511 | return child_end(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 512 | return ResultOrErr.get(); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 515 | return child_end(); |
Shankar Easwaran | 15b28be | 2012-11-13 18:38:42 +0000 | [diff] [blame] | 516 | } |
Rafael Espindola | b6b5f52e | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 517 | |
| 518 | bool Archive::hasSymbolTable() const { |
Rafael Espindola | 23a9750 | 2014-01-21 16:09:45 +0000 | [diff] [blame] | 519 | return SymbolTable != child_end(); |
Rafael Espindola | b6b5f52e | 2013-07-29 12:40:31 +0000 | [diff] [blame] | 520 | } |