blob: 84ef358344dc82c8eecf5811d9d9fd5a2a3accb9 [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
Kevin Enderby6524bd82016-07-19 20:47:07 +000030static Error
31malformedError(Twine Msg) {
32 std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
33 return make_error<GenericBinaryError>(std::move(StringMsg),
34 object_error::parse_failed);
35}
36
Rafael Espindola747bc072013-07-09 03:39:35 +000037StringRef ArchiveMemberHeader::getName() const {
38 char EndCond;
39 if (Name[0] == '/' || Name[0] == '#')
40 EndCond = ' ';
41 else
42 EndCond = '/';
43 llvm::StringRef::size_type end =
44 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
45 if (end == llvm::StringRef::npos)
46 end = sizeof(Name);
47 assert(end <= sizeof(Name) && end > 0);
48 // Don't include the EndCond if there is one.
49 return llvm::StringRef(Name, end);
50}
51
Kevin Enderby6524bd82016-07-19 20:47:07 +000052Expected<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +000053 uint32_t Ret;
Kevin Enderby6524bd82016-07-19 20:47:07 +000054 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret)) {
55 std::string Buf;
56 raw_string_ostream OS(Buf);
57 OS.write_escaped(llvm::StringRef(Size, sizeof(Size)).rtrim(" "));
58 OS.flush();
59 return malformedError("characters in size field in archive header are not "
60 "all decimal numbers: '" + Buf + "'");
61 }
Rafael Espindola8e9385e2013-07-09 12:45:11 +000062 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000063}
64
Rafael Espindola8115e1d2013-07-09 12:49:24 +000065sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
66 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000067 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(' ').getAsInteger(8, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000068 llvm_unreachable("Access mode is not an octal number.");
69 return static_cast<sys::fs::perms>(Ret);
70}
71
72sys::TimeValue ArchiveMemberHeader::getLastModified() const {
73 unsigned Seconds;
Vedant Kumar98372e32016-02-16 02:06:01 +000074 if (StringRef(LastModified, sizeof(LastModified)).rtrim(' ')
Rafael Espindola8115e1d2013-07-09 12:49:24 +000075 .getAsInteger(10, Seconds))
76 llvm_unreachable("Last modified time not a decimal number.");
77
78 sys::TimeValue Ret;
79 Ret.fromEpochTime(Seconds);
80 return Ret;
81}
82
83unsigned ArchiveMemberHeader::getUID() const {
84 unsigned Ret;
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +000085 StringRef User = StringRef(UID, sizeof(UID)).rtrim(' ');
86 if (User.empty())
87 return 0;
88 if (User.getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000089 llvm_unreachable("UID time not a decimal number.");
90 return Ret;
91}
92
93unsigned ArchiveMemberHeader::getGID() const {
94 unsigned Ret;
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +000095 StringRef Group = StringRef(GID, sizeof(GID)).rtrim(' ');
96 if (Group.empty())
97 return 0;
98 if (Group.getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000099 llvm_unreachable("GID time not a decimal number.");
100 return Ret;
101}
102
Rafael Espindola43358762015-10-31 21:44:42 +0000103Archive::Child::Child(const Archive *Parent, StringRef Data,
104 uint16_t StartOfFile)
105 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
106
Kevin Enderby6524bd82016-07-19 20:47:07 +0000107Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
Rafael Espindola0f3de642013-07-09 05:26:25 +0000108 : Parent(Parent) {
109 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +0000110 return;
Lang Hames5e51a2e2016-07-22 16:11:25 +0000111 ErrorAsOutParameter ErrAsOutParam(Err);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000112
Rafael Espindola9d102062014-12-16 01:43:41 +0000113 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +0000114 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000115 if (!isThinMember()) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000116 Expected<uint64_t> MemberSize = getRawSize();
117 if (!MemberSize) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000118 if (Err)
Kevin Enderby6524bd82016-07-19 20:47:07 +0000119 *Err = MemberSize.takeError();
Kevin Enderby7a969422015-11-05 19:24:56 +0000120 return;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000121 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000122 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000123 Data = StringRef(Start, Size);
124 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000125
Rafael Espindola747bc072013-07-09 03:39:35 +0000126 // Setup StartOfFile and PaddingBytes.
127 StartOfFile = sizeof(ArchiveMemberHeader);
128 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000129 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000130 if (Name.startswith("#1/")) {
131 uint64_t NameSize;
Vedant Kumar98372e32016-02-16 02:06:01 +0000132 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000133 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000134 StartOfFile += NameSize;
135 }
136}
137
Kevin Enderby6524bd82016-07-19 20:47:07 +0000138Expected<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000139 if (Parent->IsThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000140 Expected<uint32_t> Size = getHeader()->getSize();
141 if (!Size)
142 return Size.takeError();
Kevin Enderby1c1add42015-10-13 20:48:04 +0000143 return Size.get();
144 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000145 return Data.size() - StartOfFile;
146}
147
Kevin Enderby6524bd82016-07-19 20:47:07 +0000148Expected<uint64_t> Archive::Child::getRawSize() const {
149 return getHeader()->getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000150}
151
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000152bool Archive::Child::isThinMember() const {
153 StringRef Name = getHeader()->getName();
154 return Parent->IsThin && Name != "/" && Name != "//";
155}
156
Rafael Espindola694210c2016-05-02 13:45:06 +0000157ErrorOr<std::string> Archive::Child::getFullName() const {
158 assert(isThinMember());
159 ErrorOr<StringRef> NameOrErr = getName();
160 if (std::error_code EC = NameOrErr.getError())
161 return EC;
162 StringRef Name = *NameOrErr;
163 if (sys::path::is_absolute(Name))
164 return Name;
165
166 SmallString<128> FullName = sys::path::parent_path(
167 Parent->getMemoryBufferRef().getBufferIdentifier());
168 sys::path::append(FullName, Name);
169 return StringRef(FullName);
170}
171
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000172ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000173 if (!isThinMember()) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000174 Expected<uint32_t> Size = getSize();
175 if (!Size)
176 return errorToErrorCode(Size.takeError());
Kevin Enderby7a969422015-11-05 19:24:56 +0000177 return StringRef(Data.data() + StartOfFile, Size.get());
178 }
Rafael Espindola694210c2016-05-02 13:45:06 +0000179 ErrorOr<std::string> FullNameOrEr = getFullName();
180 if (std::error_code EC = FullNameOrEr.getError())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000181 return EC;
Rafael Espindola694210c2016-05-02 13:45:06 +0000182 const std::string &FullName = *FullNameOrEr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000183 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
184 if (std::error_code EC = Buf.getError())
185 return EC;
186 Parent->ThinBuffers.push_back(std::move(*Buf));
187 return Parent->ThinBuffers.back()->getBuffer();
188}
189
Kevin Enderby6524bd82016-07-19 20:47:07 +0000190Expected<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000191 size_t SpaceToSkip = Data.size();
192 // If it's odd, add 1 to make it even.
193 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000194 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000195
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000196 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000197
Kevin Enderby7a969422015-11-05 19:24:56 +0000198 // Check to see if this is at the end of the archive.
199 if (NextLoc == Parent->Data.getBufferEnd())
200 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000201
Kevin Enderby7a969422015-11-05 19:24:56 +0000202 // Check to see if this is past the end of the archive.
203 if (NextLoc > Parent->Data.getBufferEnd())
Kevin Enderby6524bd82016-07-19 20:47:07 +0000204 return malformedError("offset to next archive member past the end of the "
205 "archive");
Kevin Enderby7a969422015-11-05 19:24:56 +0000206
Kevin Enderby6524bd82016-07-19 20:47:07 +0000207 Error Err;
208 Child Ret(Parent, NextLoc, &Err);
209 if (Err)
210 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000211 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000212}
213
Kevin Enderby13023a12015-01-15 23:19:11 +0000214uint64_t Archive::Child::getChildOffset() const {
215 const char *a = Parent->Data.getBuffer().data();
216 const char *c = Data.data();
217 uint64_t offset = c - a;
218 return offset;
219}
220
Rafael Espindolaae460022014-06-16 16:08:36 +0000221ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000222 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000223 // Check if it's a special name.
224 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000225 if (name.size() == 1) // Linker member.
226 return name;
227 if (name.size() == 2 && name[1] == '/') // String table.
228 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000229 // It's a long name.
230 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000231 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000232 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000233 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000234
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000235 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000236 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000237 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000238 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000239
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000240 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000241 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000242 StringRef::size_type End = StringRef(addr).find('\n');
243 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000244 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000245 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000246 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000247 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000248 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000249 llvm_unreachable("Long name length is not an ingeter");
Vedant Kumar98372e32016-02-16 02:06:01 +0000250 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000251 } else {
252 // It is not a long name so trim the blanks at the end of the name.
253 if (name[name.size() - 1] != '/') {
254 return name.rtrim(' ');
255 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000256 }
257 // It's a simple name.
258 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000259 return name.substr(0, name.size() - 1);
260 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000261}
262
Rafael Espindola48af1c22014-08-19 18:44:46 +0000263ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000264 ErrorOr<StringRef> NameOrErr = getName();
265 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000266 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000267 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000268 ErrorOr<StringRef> Buf = getBuffer();
269 if (std::error_code EC = Buf.getError())
270 return EC;
271 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000272}
273
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000274Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000275Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000276 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000277 if (std::error_code EC = BuffOrErr.getError())
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000278 return errorCodeToError(EC);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000279
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000280 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
281 if (BinaryOrErr)
282 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000283 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000284}
285
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000286Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
287 Error Err;
288 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
289 if (Err)
290 return std::move(Err);
Rafael Espindolaf5577132014-07-31 03:36:00 +0000291 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000292}
293
Rafael Espindola43358762015-10-31 21:44:42 +0000294void Archive::setFirstRegular(const Child &C) {
295 FirstRegularData = C.Data;
296 FirstRegularStartOfFile = C.StartOfFile;
297}
298
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000299Archive::Archive(MemoryBufferRef Source, Error &Err)
Rafael Espindola43358762015-10-31 21:44:42 +0000300 : Binary(Binary::ID_Archive, Source) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000301 ErrorAsOutParameter ErrAsOutParam(&Err);
Rafael Espindola9d102062014-12-16 01:43:41 +0000302 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000303 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000304 if (Buffer.startswith(ThinMagic)) {
305 IsThin = true;
306 } else if (Buffer.startswith(Magic)) {
307 IsThin = false;
308 } else {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000309 Err = make_error<GenericBinaryError>("File too small to be an archive",
310 object_error::invalid_file_type);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000311 return;
312 }
313
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000314 // Get the special members.
Lang Hamesfc209622016-07-14 02:24:01 +0000315 child_iterator I = child_begin(Err, false);
316 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000317 return;
318 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000319
Rafael Espindolacc371202016-06-24 13:47:29 +0000320 // This is at least a valid empty archive. Since an empty archive is the
321 // same in all formats, just claim it to be gnu to make sure Format is
322 // initialized.
323 Format = K_GNU;
324
Kevin Enderby7a969422015-11-05 19:24:56 +0000325 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000326 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000327 return;
328 }
Lang Hamesfc209622016-07-14 02:24:01 +0000329 const Child *C = &*I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000330
Kevin Enderby7a969422015-11-05 19:24:56 +0000331 auto Increment = [&]() {
332 ++I;
Lang Hamesfc209622016-07-14 02:24:01 +0000333 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000334 return true;
Lang Hamesfc209622016-07-14 02:24:01 +0000335 C = &*I;
Kevin Enderby7a969422015-11-05 19:24:56 +0000336 return false;
337 };
338
339 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000340
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000341 // Below is the pattern that is used to figure out the archive format
342 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000343 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000344 // Second member : // (may exist, if it exists, points to the string table)
345 // Note : The string table is used if the filename exceeds 15 characters
346 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000347 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
348 // There is no string table, if the filename exceeds 15 characters or has a
349 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000350 // of the filename that needs to be read after the archive header
351 // COFF archive format
352 // First member : /
353 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000354 // Third member : // (may exist, if it exists, contains the string table)
355 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
356 // even if the string table is empty. However, lib.exe does not in fact
357 // seem to create the third member if there's no member whose filename
358 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000359
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000360 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
361 if (Name == "__.SYMDEF")
362 Format = K_BSD;
363 else // Name == "__.SYMDEF_64"
364 Format = K_DARWIN64;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000365 // We know that the symbol table is not an external file, so we just assert
366 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000367 SymbolTable = *C->getBuffer();
368 if (Increment())
369 return;
370 setFirstRegular(*C);
371
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000372 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000373 return;
374 }
375
Rafael Espindola55509922013-07-10 22:07:59 +0000376 if (Name.startswith("#1/")) {
377 Format = K_BSD;
378 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000379 ErrorOr<StringRef> NameOrErr = C->getName();
Lang Hamesfc209622016-07-14 02:24:01 +0000380 if (auto ec = NameOrErr.getError()) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000381 Err = errorCodeToError(ec);
Rafael Espindola55509922013-07-10 22:07:59 +0000382 return;
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000383 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000384 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000385 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000386 // We know that the symbol table is not an external file, so we just
387 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000388 SymbolTable = *C->getBuffer();
389 if (Increment())
390 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000391 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000392 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
393 Format = K_DARWIN64;
394 // We know that the symbol table is not an external file, so we just
395 // assert there is no error.
396 SymbolTable = *C->getBuffer();
397 if (Increment())
398 return;
399 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000400 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000401 return;
402 }
403
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000404 // MIPS 64-bit ELF archives use a special format of a symbol table.
405 // This format is marked by `ar_name` field equals to "/SYM64/".
406 // For detailed description see page 96 in the following document:
407 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
408
409 bool has64SymTable = false;
410 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000411 // We know that the symbol table is not an external file, so we just assert
412 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000413 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000414 if (Name == "/SYM64/")
415 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000416
Kevin Enderby7a969422015-11-05 19:24:56 +0000417 if (Increment())
418 return;
419 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000420 Err = Error::success();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000421 return;
422 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000423 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000424 }
425
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000426 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000427 Format = has64SymTable ? K_MIPS64 : K_GNU;
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;
433 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000434 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000435 return;
436 }
437
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000438 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000439 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000440 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000441 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000442 return;
443 }
444
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000445 if (Name != "/") {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000446 Err = errorCodeToError(object_error::parse_failed);
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000447 return;
448 }
449
450 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000451 // We know that the symbol table is not an external file, so we just assert
452 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000453 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000454
Kevin Enderby7a969422015-11-05 19:24:56 +0000455 if (Increment())
456 return;
457
458 if (I == E) {
459 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000460 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000461 return;
462 }
463
Kevin Enderby7a969422015-11-05 19:24:56 +0000464 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000465
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000466 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000467 // The string table is never an external member, so we just assert on the
468 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000469 StringTable = *C->getBuffer();
470 if (Increment())
471 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000472 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000473
Kevin Enderby7a969422015-11-05 19:24:56 +0000474 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000475 Err = Error::success();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000476}
477
Lang Hamesfc209622016-07-14 02:24:01 +0000478Archive::child_iterator Archive::child_begin(Error &Err,
479 bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000480 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000481 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000482
483 if (SkipInternal)
Lang Hamesfc209622016-07-14 02:24:01 +0000484 return child_iterator(Child(this, FirstRegularData,
485 FirstRegularStartOfFile),
486 &Err);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000487
Rafael Espindola48af1c22014-08-19 18:44:46 +0000488 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby6524bd82016-07-19 20:47:07 +0000489 Child C(this, Loc, &Err);
490 if (Err)
Lang Hamesfc209622016-07-14 02:24:01 +0000491 return child_end();
Lang Hamesfc209622016-07-14 02:24:01 +0000492 return child_iterator(C, &Err);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000493}
494
Rafael Espindola23a97502014-01-21 16:09:45 +0000495Archive::child_iterator Archive::child_end() const {
Lang Hamesfc209622016-07-14 02:24:01 +0000496 return child_iterator(Child(this, nullptr, nullptr), nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000497}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000498
Rafael Espindolaae460022014-06-16 16:08:36 +0000499StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000500 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000501}
502
Rafael Espindolacc86d822015-11-03 01:20:44 +0000503ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000504 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000505 const char *Offsets = Buf;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000506 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000507 Offsets += sizeof(uint64_t);
508 else
509 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000510 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000511 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000512 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000513 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000514 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000515 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000516 // The SymbolIndex is an index into the ranlib structs that start at
517 // Offsets (the first uint32_t is the number of bytes of the ranlib
518 // structs). The ranlib structs are a pair of uint32_t's the first
519 // being a string table offset and the second being the offset into
520 // the archive of the member that defines the symbol. Which is what
521 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000522 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000523 } else if (Parent->kind() == K_DARWIN64) {
524 // The SymbolIndex is an index into the ranlib_64 structs that start at
525 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
526 // structs). The ranlib_64 structs are a pair of uint64_t's the first
527 // being a string table offset and the second being the offset into
528 // the archive of the member that defines the symbol. Which is what
529 // is needed here.
530 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000531 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000532 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000533 uint32_t MemberCount = read32le(Buf);
534 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000535
Rui Ueyama3206b792015-03-02 21:19:12 +0000536 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000537 if (SymbolIndex >= SymbolCount)
538 return object_error::parse_failed;
539
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000540 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000541 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000542
543 // Get the index of the offset in the file member offset table for this
544 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000545 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000546 // Subtract 1 since OffsetIndex is 1 based.
547 --OffsetIndex;
548
549 if (OffsetIndex >= MemberCount)
550 return object_error::parse_failed;
551
Rui Ueyama3206b792015-03-02 21:19:12 +0000552 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000553 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000554
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000555 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000556 Error Err;
557 Child C(Parent, Loc, &Err);
558 if (Err)
559 return errorToErrorCode(std::move(Err));
Kevin Enderby7a969422015-11-05 19:24:56 +0000560 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000561}
562
563Archive::Symbol Archive::Symbol::getNext() const {
564 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000565 if (Parent->kind() == K_BSD) {
566 // t.StringIndex is an offset from the start of the __.SYMDEF or
567 // "__.SYMDEF SORTED" member into the string table for the ranlib
568 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
569 // offset in the string table for t.SymbolIndex+1 we subtract the
570 // its offset from the start of the string table for t.SymbolIndex
571 // and add the offset of the string table for t.SymbolIndex+1.
572
573 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
574 // which is the number of bytes of ranlib structs that follow. The ranlib
575 // structs are a pair of uint32_t's the first being a string table offset
576 // and the second being the offset into the archive of the member that
577 // define the symbol. After that the next uint32_t is the byte count of
578 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000579 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000580 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000581 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000582 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
583 // don't change the t.StringIndex as we don't want to reference a ranlib
584 // past RanlibCount.
585 if (t.SymbolIndex + 1 < RanlibCount) {
586 const char *Ranlibs = Buf + 4;
587 uint32_t CurRanStrx = 0;
588 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000589 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
590 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000591 t.StringIndex -= CurRanStrx;
592 t.StringIndex += NextRanStrx;
593 }
594 } else {
595 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000596 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000597 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000598 ++t.SymbolIndex;
599 return t;
600}
601
Rafael Espindola23a97502014-01-21 16:09:45 +0000602Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000603 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000604 return symbol_iterator(Symbol(this, 0, 0));
605
Rafael Espindola2b054162015-07-14 01:06:16 +0000606 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000607 if (kind() == K_GNU) {
608 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000609 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000610 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000611 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000612 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000613 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000614 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000615 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
616 // which is the number of bytes of ranlib structs that follow. The ranlib
617 // structs are a pair of uint32_t's the first being a string table offset
618 // and the second being the offset into the archive of the member that
619 // define the symbol. After that the next uint32_t is the byte count of
620 // the string table followed by the string table.
621 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000622 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000623 const char *ranlibs = buf + 4;
624 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000625 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000626 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
627 // Skip the byte count of the string table.
628 buf += sizeof(uint32_t);
629 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000630 } else if (kind() == K_DARWIN64) {
631 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
632 // which is the number of bytes of ranlib_64 structs that follow. The
633 // ranlib_64 structs are a pair of uint64_t's the first being a string
634 // table offset and the second being the offset into the archive of the
635 // member that define the symbol. After that the next uint64_t is the byte
636 // count of the string table followed by the string table.
637 uint64_t ranlib_count = 0;
638 ranlib_count = read64le(buf) / 16;
639 const char *ranlibs = buf + 8;
640 uint64_t ran_strx = 0;
641 ran_strx = read64le(ranlibs);
642 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
643 // Skip the byte count of the string table.
644 buf += sizeof(uint64_t);
645 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000646 } else {
647 uint32_t member_count = 0;
648 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000649 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000650 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000651 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000652 buf += 4 + (symbol_count * 2); // Skip indices.
653 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000654 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000655 return symbol_iterator(Symbol(this, 0, string_start_offset));
656}
657
Rafael Espindola23a97502014-01-21 16:09:45 +0000658Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000659 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
660}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000661
Rui Ueyama407e0972015-05-26 16:20:40 +0000662uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000663 if (!hasSymbolTable())
664 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000665 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000666 if (kind() == K_GNU)
667 return read32be(buf);
668 if (kind() == K_MIPS64)
669 return read64be(buf);
670 if (kind() == K_BSD)
671 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000672 if (kind() == K_DARWIN64)
673 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000674 uint32_t member_count = 0;
675 member_count = read32le(buf);
676 buf += 4 + (member_count * 4); // Skip offsets.
677 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000678}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000679
Lang Hames69f49022016-07-14 20:44:27 +0000680Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000681 Archive::symbol_iterator bs = symbol_begin();
682 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000683
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000684 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000685 StringRef SymName = bs->getName();
686 if (SymName == name) {
Lang Hames69f49022016-07-14 20:44:27 +0000687 if (auto MemberOrErr = bs->getMember())
688 return Child(*MemberOrErr);
689 else
690 return errorCodeToError(MemberOrErr.getError());
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000691 }
692 }
Lang Hames69f49022016-07-14 20:44:27 +0000693 return Optional<Child>();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000694}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000695
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000696bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }