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