blob: 21621c189cf931dfdd9edc1c2d513dbd29907404 [file] [log] [blame]
Michael J. Spencerd3b7b122011-09-27 19:36:55 +00001//===- 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 Espindola747bc072013-07-09 03:39:35 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000017#include "llvm/Support/Endian.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000018#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola4b83cb52015-07-14 22:18:43 +000019#include "llvm/Support/Path.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000020
21using namespace llvm;
22using namespace object;
Rui Ueyama3206b792015-03-02 21:19:12 +000023using namespace llvm::support::endian;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000024
Craig Topperd3a34f82013-07-16 01:17:10 +000025static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000026static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000027
David Blaikiea379b1812011-12-20 02:50:00 +000028void Archive::anchor() { }
29
Rafael Espindola747bc072013-07-09 03:39:35 +000030StringRef ArchiveMemberHeader::getName() const {
31 char EndCond;
32 if (Name[0] == '/' || Name[0] == '#')
33 EndCond = ' ';
34 else
35 EndCond = '/';
36 llvm::StringRef::size_type end =
37 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
38 if (end == llvm::StringRef::npos)
39 end = sizeof(Name);
40 assert(end <= sizeof(Name) && end > 0);
41 // Don't include the EndCond if there is one.
42 return llvm::StringRef(Name, end);
43}
44
Kevin Enderby1c1add42015-10-13 20:48:04 +000045ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +000046 uint32_t Ret;
47 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
Kevin Enderby7a969422015-11-05 19:24:56 +000048 return object_error::parse_failed; // Size is not a decimal number.
Rafael Espindola8e9385e2013-07-09 12:45:11 +000049 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000050}
51
Rafael Espindola8115e1d2013-07-09 12:49:24 +000052sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
53 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000054 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(' ').getAsInteger(8, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000055 llvm_unreachable("Access mode is not an octal number.");
56 return static_cast<sys::fs::perms>(Ret);
57}
58
59sys::TimeValue ArchiveMemberHeader::getLastModified() const {
60 unsigned Seconds;
Vedant Kumar98372e32016-02-16 02:06:01 +000061 if (StringRef(LastModified, sizeof(LastModified)).rtrim(' ')
Rafael Espindola8115e1d2013-07-09 12:49:24 +000062 .getAsInteger(10, Seconds))
63 llvm_unreachable("Last modified time not a decimal number.");
64
65 sys::TimeValue Ret;
66 Ret.fromEpochTime(Seconds);
67 return Ret;
68}
69
70unsigned ArchiveMemberHeader::getUID() const {
71 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000072 if (StringRef(UID, sizeof(UID)).rtrim(' ').getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000073 llvm_unreachable("UID time not a decimal number.");
74 return Ret;
75}
76
77unsigned ArchiveMemberHeader::getGID() const {
78 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000079 if (StringRef(GID, sizeof(GID)).rtrim(' ').getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000080 llvm_unreachable("GID time not a decimal number.");
81 return Ret;
82}
83
Rafael Espindola43358762015-10-31 21:44:42 +000084Archive::Child::Child(const Archive *Parent, StringRef Data,
85 uint16_t StartOfFile)
86 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
87
Kevin Enderby7a969422015-11-05 19:24:56 +000088Archive::Child::Child(const Archive *Parent, const char *Start,
89 std::error_code *EC)
Rafael Espindola0f3de642013-07-09 05:26:25 +000090 : Parent(Parent) {
91 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000092 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000093
Rafael Espindola9d102062014-12-16 01:43:41 +000094 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +000095 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +000096 if (!isThinMember()) {
Kevin Enderby7a969422015-11-05 19:24:56 +000097 ErrorOr<uint64_t> MemberSize = getRawSize();
98 if ((*EC = MemberSize.getError()))
99 return;
100 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000101 Data = StringRef(Start, Size);
102 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000103
Rafael Espindola747bc072013-07-09 03:39:35 +0000104 // Setup StartOfFile and PaddingBytes.
105 StartOfFile = sizeof(ArchiveMemberHeader);
106 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000107 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000108 if (Name.startswith("#1/")) {
109 uint64_t NameSize;
Vedant Kumar98372e32016-02-16 02:06:01 +0000110 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000111 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000112 StartOfFile += NameSize;
113 }
114}
115
Kevin Enderby7a969422015-11-05 19:24:56 +0000116ErrorOr<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000117 if (Parent->IsThin) {
118 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000119 if (std::error_code EC = Size.getError())
120 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000121 return Size.get();
122 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000123 return Data.size() - StartOfFile;
124}
125
Kevin Enderby7a969422015-11-05 19:24:56 +0000126ErrorOr<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000127 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000128 if (std::error_code EC = Size.getError())
129 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000130 return Size.get();
Kevin Enderby13023a12015-01-15 23:19:11 +0000131}
132
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000133bool Archive::Child::isThinMember() const {
134 StringRef Name = getHeader()->getName();
135 return Parent->IsThin && Name != "/" && Name != "//";
136}
137
Rafael Espindola694210c2016-05-02 13:45:06 +0000138ErrorOr<std::string> Archive::Child::getFullName() const {
139 assert(isThinMember());
140 ErrorOr<StringRef> NameOrErr = getName();
141 if (std::error_code EC = NameOrErr.getError())
142 return EC;
143 StringRef Name = *NameOrErr;
144 if (sys::path::is_absolute(Name))
145 return Name;
146
147 SmallString<128> FullName = sys::path::parent_path(
148 Parent->getMemoryBufferRef().getBufferIdentifier());
149 sys::path::append(FullName, Name);
150 return StringRef(FullName);
151}
152
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000153ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000154 if (!isThinMember()) {
155 ErrorOr<uint32_t> Size = getSize();
156 if (std::error_code EC = Size.getError())
157 return EC;
158 return StringRef(Data.data() + StartOfFile, Size.get());
159 }
Rafael Espindola694210c2016-05-02 13:45:06 +0000160 ErrorOr<std::string> FullNameOrEr = getFullName();
161 if (std::error_code EC = FullNameOrEr.getError())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000162 return EC;
Rafael Espindola694210c2016-05-02 13:45:06 +0000163 const std::string &FullName = *FullNameOrEr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000164 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
165 if (std::error_code EC = Buf.getError())
166 return EC;
167 Parent->ThinBuffers.push_back(std::move(*Buf));
168 return Parent->ThinBuffers.back()->getBuffer();
169}
170
Kevin Enderby7a969422015-11-05 19:24:56 +0000171ErrorOr<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000172 size_t SpaceToSkip = Data.size();
173 // If it's odd, add 1 to make it even.
174 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000175 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000176
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000177 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000178
Kevin Enderby7a969422015-11-05 19:24:56 +0000179 // Check to see if this is at the end of the archive.
180 if (NextLoc == Parent->Data.getBufferEnd())
181 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000182
Kevin Enderby7a969422015-11-05 19:24:56 +0000183 // Check to see if this is past the end of the archive.
184 if (NextLoc > Parent->Data.getBufferEnd())
185 return object_error::parse_failed;
186
187 std::error_code EC;
188 Child Ret(Parent, NextLoc, &EC);
189 if (EC)
190 return EC;
191 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000192}
193
Kevin Enderby13023a12015-01-15 23:19:11 +0000194uint64_t Archive::Child::getChildOffset() const {
195 const char *a = Parent->Data.getBuffer().data();
196 const char *c = Data.data();
197 uint64_t offset = c - a;
198 return offset;
199}
200
Rafael Espindolaae460022014-06-16 16:08:36 +0000201ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000202 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000203 // Check if it's a special name.
204 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000205 if (name.size() == 1) // Linker member.
206 return name;
207 if (name.size() == 2 && name[1] == '/') // String table.
208 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000209 // It's a long name.
210 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000211 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000212 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000213 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000214
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000215 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000216 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000217 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000218 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000219
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000220 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000221 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000222 StringRef::size_type End = StringRef(addr).find('\n');
223 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000224 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000225 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000226 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000227 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000228 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000229 llvm_unreachable("Long name length is not an ingeter");
Vedant Kumar98372e32016-02-16 02:06:01 +0000230 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000231 } else {
232 // It is not a long name so trim the blanks at the end of the name.
233 if (name[name.size() - 1] != '/') {
234 return name.rtrim(' ');
235 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000236 }
237 // It's a simple name.
238 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000239 return name.substr(0, name.size() - 1);
240 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000241}
242
Rafael Espindola48af1c22014-08-19 18:44:46 +0000243ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000244 ErrorOr<StringRef> NameOrErr = getName();
245 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000246 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000247 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000248 ErrorOr<StringRef> Buf = getBuffer();
249 if (std::error_code EC = Buf.getError())
250 return EC;
251 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000252}
253
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000254Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000255Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000256 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000257 if (std::error_code EC = BuffOrErr.getError())
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000258 return errorCodeToError(EC);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000259
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000260 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
261 if (BinaryOrErr)
262 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000263 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000264}
265
Rafael Espindola48af1c22014-08-19 18:44:46 +0000266ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000267 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000268 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000269 if (EC)
270 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000271 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000272}
273
Rafael Espindola43358762015-10-31 21:44:42 +0000274void Archive::setFirstRegular(const Child &C) {
275 FirstRegularData = C.Data;
276 FirstRegularStartOfFile = C.StartOfFile;
277}
278
Rafael Espindola48af1c22014-08-19 18:44:46 +0000279Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindola43358762015-10-31 21:44:42 +0000280 : Binary(Binary::ID_Archive, Source) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000281 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000282 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000283 if (Buffer.startswith(ThinMagic)) {
284 IsThin = true;
285 } else if (Buffer.startswith(Magic)) {
286 IsThin = false;
287 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000288 ec = object_error::invalid_file_type;
289 return;
290 }
291
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000292 // Get the special members.
Kevin Enderby7a969422015-11-05 19:24:56 +0000293 child_iterator I = child_begin(false);
294 if ((ec = I->getError()))
295 return;
296 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000297
Kevin Enderby7a969422015-11-05 19:24:56 +0000298 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000299 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000300 return;
301 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000302 const Child *C = &**I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000303
Kevin Enderby7a969422015-11-05 19:24:56 +0000304 auto Increment = [&]() {
305 ++I;
306 if ((ec = I->getError()))
307 return true;
308 C = &**I;
309 return false;
310 };
311
312 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000313
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000314 // Below is the pattern that is used to figure out the archive format
315 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000316 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000317 // Second member : // (may exist, if it exists, points to the string table)
318 // Note : The string table is used if the filename exceeds 15 characters
319 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000320 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
321 // There is no string table, if the filename exceeds 15 characters or has a
322 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000323 // of the filename that needs to be read after the archive header
324 // COFF archive format
325 // First member : /
326 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000327 // Third member : // (may exist, if it exists, contains the string table)
328 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
329 // even if the string table is empty. However, lib.exe does not in fact
330 // seem to create the third member if there's no member whose filename
331 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000332
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000333 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000334 Format = K_BSD;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000335 // We know that the symbol table is not an external file, so we just assert
336 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000337 SymbolTable = *C->getBuffer();
338 if (Increment())
339 return;
340 setFirstRegular(*C);
341
Rui Ueyama7d099192015-06-09 15:20:42 +0000342 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000343 return;
344 }
345
Rafael Espindola55509922013-07-10 22:07:59 +0000346 if (Name.startswith("#1/")) {
347 Format = K_BSD;
348 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000349 ErrorOr<StringRef> NameOrErr = C->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000350 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000351 if (ec)
352 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000353 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000354 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000355 // We know that the symbol table is not an external file, so we just
356 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000357 SymbolTable = *C->getBuffer();
358 if (Increment())
359 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000360 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000361 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000362 return;
363 }
364
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000365 // MIPS 64-bit ELF archives use a special format of a symbol table.
366 // This format is marked by `ar_name` field equals to "/SYM64/".
367 // For detailed description see page 96 in the following document:
368 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
369
370 bool has64SymTable = false;
371 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000372 // We know that the symbol table is not an external file, so we just assert
373 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000374 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000375 if (Name == "/SYM64/")
376 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000377
Kevin Enderby7a969422015-11-05 19:24:56 +0000378 if (Increment())
379 return;
380 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000381 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000382 return;
383 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000384 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000385 }
386
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000387 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000388 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000389 // The string table is never an external member, so we just assert on the
390 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000391 StringTable = *C->getBuffer();
392 if (Increment())
393 return;
394 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000395 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000396 return;
397 }
398
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000399 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000400 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000401 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000402 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000403 return;
404 }
405
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000406 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000407 ec = object_error::parse_failed;
408 return;
409 }
410
411 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000412 // We know that the symbol table is not an external file, so we just assert
413 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000414 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000415
Kevin Enderby7a969422015-11-05 19:24:56 +0000416 if (Increment())
417 return;
418
419 if (I == E) {
420 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000421 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000422 return;
423 }
424
Kevin Enderby7a969422015-11-05 19:24:56 +0000425 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000426
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000427 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000428 // The string table is never an external member, so we just assert on the
429 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000430 StringTable = *C->getBuffer();
431 if (Increment())
432 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000433 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000434
Kevin Enderby7a969422015-11-05 19:24:56 +0000435 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000436 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000437}
438
Rafael Espindola23a97502014-01-21 16:09:45 +0000439Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000440 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000441 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000442
443 if (SkipInternal)
Rafael Espindola43358762015-10-31 21:44:42 +0000444 return Child(this, FirstRegularData, FirstRegularStartOfFile);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000445
Rafael Espindola48af1c22014-08-19 18:44:46 +0000446 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby7a969422015-11-05 19:24:56 +0000447 std::error_code EC;
448 Child c(this, Loc, &EC);
449 if (EC)
450 return child_iterator(EC);
451 return child_iterator(c);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000452}
453
Rafael Espindola23a97502014-01-21 16:09:45 +0000454Archive::child_iterator Archive::child_end() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000455 return Child(this, nullptr, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000456}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000457
Rafael Espindolaae460022014-06-16 16:08:36 +0000458StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000459 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000460}
461
Rafael Espindolacc86d822015-11-03 01:20:44 +0000462ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000463 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000464 const char *Offsets = Buf;
465 if (Parent->kind() == K_MIPS64)
466 Offsets += sizeof(uint64_t);
467 else
468 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000469 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000470 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000471 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000472 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000473 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000474 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000475 // The SymbolIndex is an index into the ranlib structs that start at
476 // Offsets (the first uint32_t is the number of bytes of the ranlib
477 // structs). The ranlib structs are a pair of uint32_t's the first
478 // being a string table offset and the second being the offset into
479 // the archive of the member that defines the symbol. Which is what
480 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000481 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000482 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000483 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000484 uint32_t MemberCount = read32le(Buf);
485 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000486
Rui Ueyama3206b792015-03-02 21:19:12 +0000487 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000488 if (SymbolIndex >= SymbolCount)
489 return object_error::parse_failed;
490
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000491 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000492 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000493
494 // Get the index of the offset in the file member offset table for this
495 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000496 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000497 // Subtract 1 since OffsetIndex is 1 based.
498 --OffsetIndex;
499
500 if (OffsetIndex >= MemberCount)
501 return object_error::parse_failed;
502
Rui Ueyama3206b792015-03-02 21:19:12 +0000503 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000504 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000505
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000506 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby7a969422015-11-05 19:24:56 +0000507 std::error_code EC;
508 Child C(Parent, Loc, &EC);
509 if (EC)
510 return EC;
511 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000512}
513
514Archive::Symbol Archive::Symbol::getNext() const {
515 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000516 if (Parent->kind() == K_BSD) {
517 // t.StringIndex is an offset from the start of the __.SYMDEF or
518 // "__.SYMDEF SORTED" member into the string table for the ranlib
519 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
520 // offset in the string table for t.SymbolIndex+1 we subtract the
521 // its offset from the start of the string table for t.SymbolIndex
522 // and add the offset of the string table for t.SymbolIndex+1.
523
524 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
525 // which is the number of bytes of ranlib structs that follow. The ranlib
526 // structs are a pair of uint32_t's the first being a string table offset
527 // and the second being the offset into the archive of the member that
528 // define the symbol. After that the next uint32_t is the byte count of
529 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000530 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000531 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000532 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000533 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
534 // don't change the t.StringIndex as we don't want to reference a ranlib
535 // past RanlibCount.
536 if (t.SymbolIndex + 1 < RanlibCount) {
537 const char *Ranlibs = Buf + 4;
538 uint32_t CurRanStrx = 0;
539 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000540 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
541 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000542 t.StringIndex -= CurRanStrx;
543 t.StringIndex += NextRanStrx;
544 }
545 } else {
546 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000547 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000548 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000549 ++t.SymbolIndex;
550 return t;
551}
552
Rafael Espindola23a97502014-01-21 16:09:45 +0000553Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000554 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000555 return symbol_iterator(Symbol(this, 0, 0));
556
Rafael Espindola2b054162015-07-14 01:06:16 +0000557 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000558 if (kind() == K_GNU) {
559 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000560 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000561 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000562 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000563 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000564 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000565 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000566 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
567 // which is the number of bytes of ranlib structs that follow. The ranlib
568 // structs are a pair of uint32_t's the first being a string table offset
569 // and the second being the offset into the archive of the member that
570 // define the symbol. After that the next uint32_t is the byte count of
571 // the string table followed by the string table.
572 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000573 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000574 const char *ranlibs = buf + 4;
575 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000576 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000577 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
578 // Skip the byte count of the string table.
579 buf += sizeof(uint32_t);
580 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000581 } else {
582 uint32_t member_count = 0;
583 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000584 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000585 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000586 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000587 buf += 4 + (symbol_count * 2); // Skip indices.
588 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000589 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000590 return symbol_iterator(Symbol(this, 0, string_start_offset));
591}
592
Rafael Espindola23a97502014-01-21 16:09:45 +0000593Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000594 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
595}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000596
Rui Ueyama407e0972015-05-26 16:20:40 +0000597uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000598 if (!hasSymbolTable())
599 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000600 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000601 if (kind() == K_GNU)
602 return read32be(buf);
603 if (kind() == K_MIPS64)
604 return read64be(buf);
605 if (kind() == K_BSD)
606 return read32le(buf) / 8;
607 uint32_t member_count = 0;
608 member_count = read32le(buf);
609 buf += 4 + (member_count * 4); // Skip offsets.
610 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000611}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000612
613Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000614 Archive::symbol_iterator bs = symbol_begin();
615 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000616
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000617 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000618 StringRef SymName = bs->getName();
619 if (SymName == name) {
620 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
621 // FIXME: Should we really eat the error?
622 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000623 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000624 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000625 }
626 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000627 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000628}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000629
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000630bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }