blob: daf301e2e7e45ba763061c513ff60792ab76dcc2 [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;
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +000072 StringRef User = StringRef(UID, sizeof(UID)).rtrim(' ');
73 if (User.empty())
74 return 0;
75 if (User.getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000076 llvm_unreachable("UID time not a decimal number.");
77 return Ret;
78}
79
80unsigned ArchiveMemberHeader::getGID() const {
81 unsigned Ret;
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +000082 StringRef Group = StringRef(GID, sizeof(GID)).rtrim(' ');
83 if (Group.empty())
84 return 0;
85 if (Group.getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000086 llvm_unreachable("GID time not a decimal number.");
87 return Ret;
88}
89
Rafael Espindola43358762015-10-31 21:44:42 +000090Archive::Child::Child(const Archive *Parent, StringRef Data,
91 uint16_t StartOfFile)
92 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
93
Kevin Enderby7a969422015-11-05 19:24:56 +000094Archive::Child::Child(const Archive *Parent, const char *Start,
95 std::error_code *EC)
Rafael Espindola0f3de642013-07-09 05:26:25 +000096 : Parent(Parent) {
97 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000098 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000099
Rafael Espindola9d102062014-12-16 01:43:41 +0000100 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +0000101 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000102 if (!isThinMember()) {
Kevin Enderby7a969422015-11-05 19:24:56 +0000103 ErrorOr<uint64_t> MemberSize = getRawSize();
104 if ((*EC = MemberSize.getError()))
105 return;
106 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000107 Data = StringRef(Start, Size);
108 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000109
Rafael Espindola747bc072013-07-09 03:39:35 +0000110 // Setup StartOfFile and PaddingBytes.
111 StartOfFile = sizeof(ArchiveMemberHeader);
112 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000113 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000114 if (Name.startswith("#1/")) {
115 uint64_t NameSize;
Vedant Kumar98372e32016-02-16 02:06:01 +0000116 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000117 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000118 StartOfFile += NameSize;
119 }
120}
121
Kevin Enderby7a969422015-11-05 19:24:56 +0000122ErrorOr<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000123 if (Parent->IsThin) {
124 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000125 if (std::error_code EC = Size.getError())
126 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000127 return Size.get();
128 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000129 return Data.size() - StartOfFile;
130}
131
Kevin Enderby7a969422015-11-05 19:24:56 +0000132ErrorOr<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000133 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000134 if (std::error_code EC = Size.getError())
135 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000136 return Size.get();
Kevin Enderby13023a12015-01-15 23:19:11 +0000137}
138
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000139bool Archive::Child::isThinMember() const {
140 StringRef Name = getHeader()->getName();
141 return Parent->IsThin && Name != "/" && Name != "//";
142}
143
Rafael Espindola694210c2016-05-02 13:45:06 +0000144ErrorOr<std::string> Archive::Child::getFullName() const {
145 assert(isThinMember());
146 ErrorOr<StringRef> NameOrErr = getName();
147 if (std::error_code EC = NameOrErr.getError())
148 return EC;
149 StringRef Name = *NameOrErr;
150 if (sys::path::is_absolute(Name))
151 return Name;
152
153 SmallString<128> FullName = sys::path::parent_path(
154 Parent->getMemoryBufferRef().getBufferIdentifier());
155 sys::path::append(FullName, Name);
156 return StringRef(FullName);
157}
158
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000159ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000160 if (!isThinMember()) {
161 ErrorOr<uint32_t> Size = getSize();
162 if (std::error_code EC = Size.getError())
163 return EC;
164 return StringRef(Data.data() + StartOfFile, Size.get());
165 }
Rafael Espindola694210c2016-05-02 13:45:06 +0000166 ErrorOr<std::string> FullNameOrEr = getFullName();
167 if (std::error_code EC = FullNameOrEr.getError())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000168 return EC;
Rafael Espindola694210c2016-05-02 13:45:06 +0000169 const std::string &FullName = *FullNameOrEr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000170 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
171 if (std::error_code EC = Buf.getError())
172 return EC;
173 Parent->ThinBuffers.push_back(std::move(*Buf));
174 return Parent->ThinBuffers.back()->getBuffer();
175}
176
Kevin Enderby7a969422015-11-05 19:24:56 +0000177ErrorOr<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000178 size_t SpaceToSkip = Data.size();
179 // If it's odd, add 1 to make it even.
180 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000181 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000182
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000183 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000184
Kevin Enderby7a969422015-11-05 19:24:56 +0000185 // Check to see if this is at the end of the archive.
186 if (NextLoc == Parent->Data.getBufferEnd())
187 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000188
Kevin Enderby7a969422015-11-05 19:24:56 +0000189 // Check to see if this is past the end of the archive.
190 if (NextLoc > Parent->Data.getBufferEnd())
191 return object_error::parse_failed;
192
193 std::error_code EC;
194 Child Ret(Parent, NextLoc, &EC);
195 if (EC)
196 return EC;
197 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000198}
199
Kevin Enderby13023a12015-01-15 23:19:11 +0000200uint64_t Archive::Child::getChildOffset() const {
201 const char *a = Parent->Data.getBuffer().data();
202 const char *c = Data.data();
203 uint64_t offset = c - a;
204 return offset;
205}
206
Rafael Espindolaae460022014-06-16 16:08:36 +0000207ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000208 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000209 // Check if it's a special name.
210 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000211 if (name.size() == 1) // Linker member.
212 return name;
213 if (name.size() == 2 && name[1] == '/') // String table.
214 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000215 // It's a long name.
216 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000217 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000218 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000219 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000220
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000221 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000222 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000223 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000224 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000225
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000226 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000227 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000228 StringRef::size_type End = StringRef(addr).find('\n');
229 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000230 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000231 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000232 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000233 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000234 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000235 llvm_unreachable("Long name length is not an ingeter");
Vedant Kumar98372e32016-02-16 02:06:01 +0000236 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000237 } else {
238 // It is not a long name so trim the blanks at the end of the name.
239 if (name[name.size() - 1] != '/') {
240 return name.rtrim(' ');
241 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000242 }
243 // It's a simple name.
244 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000245 return name.substr(0, name.size() - 1);
246 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000247}
248
Rafael Espindola48af1c22014-08-19 18:44:46 +0000249ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000250 ErrorOr<StringRef> NameOrErr = getName();
251 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000252 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000253 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000254 ErrorOr<StringRef> Buf = getBuffer();
255 if (std::error_code EC = Buf.getError())
256 return EC;
257 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000258}
259
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000260Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000261Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000262 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000263 if (std::error_code EC = BuffOrErr.getError())
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000264 return errorCodeToError(EC);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000265
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000266 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
267 if (BinaryOrErr)
268 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000269 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000270}
271
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000272Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
273 Error Err;
274 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
275 if (Err)
276 return std::move(Err);
Rafael Espindolaf5577132014-07-31 03:36:00 +0000277 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000278}
279
Rafael Espindola43358762015-10-31 21:44:42 +0000280void Archive::setFirstRegular(const Child &C) {
281 FirstRegularData = C.Data;
282 FirstRegularStartOfFile = C.StartOfFile;
283}
284
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000285Archive::Archive(MemoryBufferRef Source, Error &Err)
Rafael Espindola43358762015-10-31 21:44:42 +0000286 : Binary(Binary::ID_Archive, Source) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000287 ErrorAsOutParameter ErrAsOutParam(Err);
Rafael Espindola9d102062014-12-16 01:43:41 +0000288 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000289 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000290 if (Buffer.startswith(ThinMagic)) {
291 IsThin = true;
292 } else if (Buffer.startswith(Magic)) {
293 IsThin = false;
294 } else {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000295 Err = make_error<GenericBinaryError>("File too small to be an archive",
296 object_error::invalid_file_type);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000297 return;
298 }
299
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000300 // Get the special members.
Lang Hamesfc209622016-07-14 02:24:01 +0000301 child_iterator I = child_begin(Err, false);
302 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000303 return;
304 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000305
Rafael Espindolacc371202016-06-24 13:47:29 +0000306 // This is at least a valid empty archive. Since an empty archive is the
307 // same in all formats, just claim it to be gnu to make sure Format is
308 // initialized.
309 Format = K_GNU;
310
Kevin Enderby7a969422015-11-05 19:24:56 +0000311 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000312 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000313 return;
314 }
Lang Hamesfc209622016-07-14 02:24:01 +0000315 const Child *C = &*I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000316
Kevin Enderby7a969422015-11-05 19:24:56 +0000317 auto Increment = [&]() {
318 ++I;
Lang Hamesfc209622016-07-14 02:24:01 +0000319 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000320 return true;
Lang Hamesfc209622016-07-14 02:24:01 +0000321 C = &*I;
Kevin Enderby7a969422015-11-05 19:24:56 +0000322 return false;
323 };
324
325 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000326
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000327 // Below is the pattern that is used to figure out the archive format
328 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000329 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000330 // Second member : // (may exist, if it exists, points to the string table)
331 // Note : The string table is used if the filename exceeds 15 characters
332 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000333 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
334 // There is no string table, if the filename exceeds 15 characters or has a
335 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000336 // of the filename that needs to be read after the archive header
337 // COFF archive format
338 // First member : /
339 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000340 // Third member : // (may exist, if it exists, contains the string table)
341 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
342 // even if the string table is empty. However, lib.exe does not in fact
343 // seem to create the third member if there's no member whose filename
344 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000345
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000346 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
347 if (Name == "__.SYMDEF")
348 Format = K_BSD;
349 else // Name == "__.SYMDEF_64"
350 Format = K_DARWIN64;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000351 // We know that the symbol table is not an external file, so we just assert
352 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000353 SymbolTable = *C->getBuffer();
354 if (Increment())
355 return;
356 setFirstRegular(*C);
357
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000358 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000359 return;
360 }
361
Rafael Espindola55509922013-07-10 22:07:59 +0000362 if (Name.startswith("#1/")) {
363 Format = K_BSD;
364 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000365 ErrorOr<StringRef> NameOrErr = C->getName();
Lang Hamesfc209622016-07-14 02:24:01 +0000366 if (auto ec = NameOrErr.getError()) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000367 Err = errorCodeToError(ec);
Rafael Espindola55509922013-07-10 22:07:59 +0000368 return;
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000369 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000370 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000371 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000372 // We know that the symbol table is not an external file, so we just
373 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000374 SymbolTable = *C->getBuffer();
375 if (Increment())
376 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000377 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000378 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
379 Format = K_DARWIN64;
380 // We know that the symbol table is not an external file, so we just
381 // assert there is no error.
382 SymbolTable = *C->getBuffer();
383 if (Increment())
384 return;
385 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000386 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000387 return;
388 }
389
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000390 // MIPS 64-bit ELF archives use a special format of a symbol table.
391 // This format is marked by `ar_name` field equals to "/SYM64/".
392 // For detailed description see page 96 in the following document:
393 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
394
395 bool has64SymTable = false;
396 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000397 // We know that the symbol table is not an external file, so we just assert
398 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000399 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000400 if (Name == "/SYM64/")
401 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000402
Kevin Enderby7a969422015-11-05 19:24:56 +0000403 if (Increment())
404 return;
405 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000406 Err = Error::success();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000407 return;
408 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000409 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000410 }
411
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000412 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000413 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000414 // The string table is never an external member, so we just assert on the
415 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000416 StringTable = *C->getBuffer();
417 if (Increment())
418 return;
419 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000420 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000421 return;
422 }
423
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000424 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000425 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000426 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000427 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000428 return;
429 }
430
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000431 if (Name != "/") {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000432 Err = errorCodeToError(object_error::parse_failed);
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000433 return;
434 }
435
436 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000437 // We know that the symbol table is not an external file, so we just assert
438 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000439 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000440
Kevin Enderby7a969422015-11-05 19:24:56 +0000441 if (Increment())
442 return;
443
444 if (I == E) {
445 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000446 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000447 return;
448 }
449
Kevin Enderby7a969422015-11-05 19:24:56 +0000450 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000451
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000452 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000453 // The string table is never an external member, so we just assert on the
454 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000455 StringTable = *C->getBuffer();
456 if (Increment())
457 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000458 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000459
Kevin Enderby7a969422015-11-05 19:24:56 +0000460 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000461 Err = Error::success();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000462}
463
Lang Hamesfc209622016-07-14 02:24:01 +0000464Archive::child_iterator Archive::child_begin(Error &Err,
465 bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000466 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000467 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000468
469 if (SkipInternal)
Lang Hamesfc209622016-07-14 02:24:01 +0000470 return child_iterator(Child(this, FirstRegularData,
471 FirstRegularStartOfFile),
472 &Err);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000473
Rafael Espindola48af1c22014-08-19 18:44:46 +0000474 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby7a969422015-11-05 19:24:56 +0000475 std::error_code EC;
Lang Hamesfc209622016-07-14 02:24:01 +0000476 Child C(this, Loc, &EC);
477 if (EC) {
478 ErrorAsOutParameter ErrAsOutParam(Err);
479 Err = errorCodeToError(EC);
480 return child_end();
481 }
482 return child_iterator(C, &Err);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000483}
484
Rafael Espindola23a97502014-01-21 16:09:45 +0000485Archive::child_iterator Archive::child_end() const {
Lang Hamesfc209622016-07-14 02:24:01 +0000486 return child_iterator(Child(this, nullptr, nullptr), nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000487}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000488
Rafael Espindolaae460022014-06-16 16:08:36 +0000489StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000490 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000491}
492
Rafael Espindolacc86d822015-11-03 01:20:44 +0000493ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000494 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000495 const char *Offsets = Buf;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000496 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000497 Offsets += sizeof(uint64_t);
498 else
499 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000500 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000501 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000502 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000503 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000504 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000505 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000506 // The SymbolIndex is an index into the ranlib structs that start at
507 // Offsets (the first uint32_t is the number of bytes of the ranlib
508 // structs). The ranlib structs are a pair of uint32_t's the first
509 // being a string table offset and the second being the offset into
510 // the archive of the member that defines the symbol. Which is what
511 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000512 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000513 } else if (Parent->kind() == K_DARWIN64) {
514 // The SymbolIndex is an index into the ranlib_64 structs that start at
515 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
516 // structs). The ranlib_64 structs are a pair of uint64_t's the first
517 // being a string table offset and the second being the offset into
518 // the archive of the member that defines the symbol. Which is what
519 // is needed here.
520 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000521 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000522 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000523 uint32_t MemberCount = read32le(Buf);
524 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000525
Rui Ueyama3206b792015-03-02 21:19:12 +0000526 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000527 if (SymbolIndex >= SymbolCount)
528 return object_error::parse_failed;
529
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000530 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000531 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000532
533 // Get the index of the offset in the file member offset table for this
534 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000535 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000536 // Subtract 1 since OffsetIndex is 1 based.
537 --OffsetIndex;
538
539 if (OffsetIndex >= MemberCount)
540 return object_error::parse_failed;
541
Rui Ueyama3206b792015-03-02 21:19:12 +0000542 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000543 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000544
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000545 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby7a969422015-11-05 19:24:56 +0000546 std::error_code EC;
547 Child C(Parent, Loc, &EC);
548 if (EC)
549 return EC;
550 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000551}
552
553Archive::Symbol Archive::Symbol::getNext() const {
554 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000555 if (Parent->kind() == K_BSD) {
556 // t.StringIndex is an offset from the start of the __.SYMDEF or
557 // "__.SYMDEF SORTED" member into the string table for the ranlib
558 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
559 // offset in the string table for t.SymbolIndex+1 we subtract the
560 // its offset from the start of the string table for t.SymbolIndex
561 // and add the offset of the string table for t.SymbolIndex+1.
562
563 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
564 // which is the number of bytes of ranlib structs that follow. The ranlib
565 // structs are a pair of uint32_t's the first being a string table offset
566 // and the second being the offset into the archive of the member that
567 // define the symbol. After that the next uint32_t is the byte count of
568 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000569 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000570 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000571 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000572 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
573 // don't change the t.StringIndex as we don't want to reference a ranlib
574 // past RanlibCount.
575 if (t.SymbolIndex + 1 < RanlibCount) {
576 const char *Ranlibs = Buf + 4;
577 uint32_t CurRanStrx = 0;
578 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000579 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
580 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000581 t.StringIndex -= CurRanStrx;
582 t.StringIndex += NextRanStrx;
583 }
584 } else {
585 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000586 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000587 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000588 ++t.SymbolIndex;
589 return t;
590}
591
Rafael Espindola23a97502014-01-21 16:09:45 +0000592Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000593 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000594 return symbol_iterator(Symbol(this, 0, 0));
595
Rafael Espindola2b054162015-07-14 01:06:16 +0000596 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000597 if (kind() == K_GNU) {
598 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000599 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000600 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000601 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000602 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000603 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000604 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000605 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
606 // which is the number of bytes of ranlib structs that follow. The ranlib
607 // structs are a pair of uint32_t's the first being a string table offset
608 // and the second being the offset into the archive of the member that
609 // define the symbol. After that the next uint32_t is the byte count of
610 // the string table followed by the string table.
611 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000612 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000613 const char *ranlibs = buf + 4;
614 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000615 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000616 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
617 // Skip the byte count of the string table.
618 buf += sizeof(uint32_t);
619 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000620 } else if (kind() == K_DARWIN64) {
621 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
622 // which is the number of bytes of ranlib_64 structs that follow. The
623 // ranlib_64 structs are a pair of uint64_t's the first being a string
624 // table offset and the second being the offset into the archive of the
625 // member that define the symbol. After that the next uint64_t is the byte
626 // count of the string table followed by the string table.
627 uint64_t ranlib_count = 0;
628 ranlib_count = read64le(buf) / 16;
629 const char *ranlibs = buf + 8;
630 uint64_t ran_strx = 0;
631 ran_strx = read64le(ranlibs);
632 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
633 // Skip the byte count of the string table.
634 buf += sizeof(uint64_t);
635 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000636 } else {
637 uint32_t member_count = 0;
638 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000639 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000640 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000641 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000642 buf += 4 + (symbol_count * 2); // Skip indices.
643 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000644 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000645 return symbol_iterator(Symbol(this, 0, string_start_offset));
646}
647
Rafael Espindola23a97502014-01-21 16:09:45 +0000648Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000649 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
650}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000651
Rui Ueyama407e0972015-05-26 16:20:40 +0000652uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000653 if (!hasSymbolTable())
654 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000655 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000656 if (kind() == K_GNU)
657 return read32be(buf);
658 if (kind() == K_MIPS64)
659 return read64be(buf);
660 if (kind() == K_BSD)
661 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000662 if (kind() == K_DARWIN64)
663 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000664 uint32_t member_count = 0;
665 member_count = read32le(buf);
666 buf += 4 + (member_count * 4); // Skip offsets.
667 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000668}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000669
Lang Hames69f49022016-07-14 20:44:27 +0000670Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000671 Archive::symbol_iterator bs = symbol_begin();
672 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000673
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000674 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000675 StringRef SymName = bs->getName();
676 if (SymName == name) {
Lang Hames69f49022016-07-14 20:44:27 +0000677 if (auto MemberOrErr = bs->getMember())
678 return Child(*MemberOrErr);
679 else
680 return errorCodeToError(MemberOrErr.getError());
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000681 }
682 }
Lang Hames69f49022016-07-14 20:44:27 +0000683 return Optional<Child>();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000684}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000685
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000686bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }