blob: 6d0330d52ec4a8dd75d68d360bd87d199e328692 [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;
Rafael Espindola0f3de642013-07-09 05:26:25 +0000111
Rafael Espindola9d102062014-12-16 01:43:41 +0000112 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +0000113 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000114 if (!isThinMember()) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000115 Expected<uint64_t> MemberSize = getRawSize();
116 if (!MemberSize) {
117 if (Err) {
118 ErrorAsOutParameter ErrAsOutParam(*Err);
119 *Err = MemberSize.takeError();
120 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000121 return;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000122 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000123 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000124 Data = StringRef(Start, Size);
125 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000126
Rafael Espindola747bc072013-07-09 03:39:35 +0000127 // Setup StartOfFile and PaddingBytes.
128 StartOfFile = sizeof(ArchiveMemberHeader);
129 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000130 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000131 if (Name.startswith("#1/")) {
132 uint64_t NameSize;
Vedant Kumar98372e32016-02-16 02:06:01 +0000133 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000134 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000135 StartOfFile += NameSize;
136 }
137}
138
Kevin Enderby6524bd82016-07-19 20:47:07 +0000139Expected<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000140 if (Parent->IsThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000141 Expected<uint32_t> Size = getHeader()->getSize();
142 if (!Size)
143 return Size.takeError();
Kevin Enderby1c1add42015-10-13 20:48:04 +0000144 return Size.get();
145 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000146 return Data.size() - StartOfFile;
147}
148
Kevin Enderby6524bd82016-07-19 20:47:07 +0000149Expected<uint64_t> Archive::Child::getRawSize() const {
150 return getHeader()->getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000151}
152
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000153bool Archive::Child::isThinMember() const {
154 StringRef Name = getHeader()->getName();
155 return Parent->IsThin && Name != "/" && Name != "//";
156}
157
Rafael Espindola694210c2016-05-02 13:45:06 +0000158ErrorOr<std::string> Archive::Child::getFullName() const {
159 assert(isThinMember());
160 ErrorOr<StringRef> NameOrErr = getName();
161 if (std::error_code EC = NameOrErr.getError())
162 return EC;
163 StringRef Name = *NameOrErr;
164 if (sys::path::is_absolute(Name))
165 return Name;
166
167 SmallString<128> FullName = sys::path::parent_path(
168 Parent->getMemoryBufferRef().getBufferIdentifier());
169 sys::path::append(FullName, Name);
170 return StringRef(FullName);
171}
172
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000173ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000174 if (!isThinMember()) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000175 Expected<uint32_t> Size = getSize();
176 if (!Size)
177 return errorToErrorCode(Size.takeError());
Kevin Enderby7a969422015-11-05 19:24:56 +0000178 return StringRef(Data.data() + StartOfFile, Size.get());
179 }
Rafael Espindola694210c2016-05-02 13:45:06 +0000180 ErrorOr<std::string> FullNameOrEr = getFullName();
181 if (std::error_code EC = FullNameOrEr.getError())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000182 return EC;
Rafael Espindola694210c2016-05-02 13:45:06 +0000183 const std::string &FullName = *FullNameOrEr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000184 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
185 if (std::error_code EC = Buf.getError())
186 return EC;
187 Parent->ThinBuffers.push_back(std::move(*Buf));
188 return Parent->ThinBuffers.back()->getBuffer();
189}
190
Kevin Enderby6524bd82016-07-19 20:47:07 +0000191Expected<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000192 size_t SpaceToSkip = Data.size();
193 // If it's odd, add 1 to make it even.
194 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000195 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000196
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000197 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000198
Kevin Enderby7a969422015-11-05 19:24:56 +0000199 // Check to see if this is at the end of the archive.
200 if (NextLoc == Parent->Data.getBufferEnd())
201 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000202
Kevin Enderby7a969422015-11-05 19:24:56 +0000203 // Check to see if this is past the end of the archive.
204 if (NextLoc > Parent->Data.getBufferEnd())
Kevin Enderby6524bd82016-07-19 20:47:07 +0000205 return malformedError("offset to next archive member past the end of the "
206 "archive");
Kevin Enderby7a969422015-11-05 19:24:56 +0000207
Kevin Enderby6524bd82016-07-19 20:47:07 +0000208 Error Err;
209 Child Ret(Parent, NextLoc, &Err);
210 if (Err)
211 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000212 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000213}
214
Kevin Enderby13023a12015-01-15 23:19:11 +0000215uint64_t Archive::Child::getChildOffset() const {
216 const char *a = Parent->Data.getBuffer().data();
217 const char *c = Data.data();
218 uint64_t offset = c - a;
219 return offset;
220}
221
Rafael Espindolaae460022014-06-16 16:08:36 +0000222ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000223 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000224 // Check if it's a special name.
225 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000226 if (name.size() == 1) // Linker member.
227 return name;
228 if (name.size() == 2 && name[1] == '/') // String table.
229 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000230 // It's a long name.
231 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000232 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000233 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000234 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000235
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000236 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000237 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000238 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000239 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000240
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000241 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000242 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000243 StringRef::size_type End = StringRef(addr).find('\n');
244 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000245 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000246 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000247 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000248 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000249 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000250 llvm_unreachable("Long name length is not an ingeter");
Vedant Kumar98372e32016-02-16 02:06:01 +0000251 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000252 } else {
253 // It is not a long name so trim the blanks at the end of the name.
254 if (name[name.size() - 1] != '/') {
255 return name.rtrim(' ');
256 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000257 }
258 // It's a simple name.
259 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000260 return name.substr(0, name.size() - 1);
261 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000262}
263
Rafael Espindola48af1c22014-08-19 18:44:46 +0000264ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000265 ErrorOr<StringRef> NameOrErr = getName();
266 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000267 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000268 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000269 ErrorOr<StringRef> Buf = getBuffer();
270 if (std::error_code EC = Buf.getError())
271 return EC;
272 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000273}
274
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000275Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000276Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000277 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000278 if (std::error_code EC = BuffOrErr.getError())
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000279 return errorCodeToError(EC);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000280
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000281 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
282 if (BinaryOrErr)
283 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000284 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000285}
286
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000287Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
288 Error Err;
289 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
290 if (Err)
291 return std::move(Err);
Rafael Espindolaf5577132014-07-31 03:36:00 +0000292 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000293}
294
Rafael Espindola43358762015-10-31 21:44:42 +0000295void Archive::setFirstRegular(const Child &C) {
296 FirstRegularData = C.Data;
297 FirstRegularStartOfFile = C.StartOfFile;
298}
299
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000300Archive::Archive(MemoryBufferRef Source, Error &Err)
Rafael Espindola43358762015-10-31 21:44:42 +0000301 : Binary(Binary::ID_Archive, Source) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000302 ErrorAsOutParameter ErrAsOutParam(Err);
Rafael Espindola9d102062014-12-16 01:43:41 +0000303 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000304 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000305 if (Buffer.startswith(ThinMagic)) {
306 IsThin = true;
307 } else if (Buffer.startswith(Magic)) {
308 IsThin = false;
309 } else {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000310 Err = make_error<GenericBinaryError>("File too small to be an archive",
311 object_error::invalid_file_type);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000312 return;
313 }
314
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000315 // Get the special members.
Lang Hamesfc209622016-07-14 02:24:01 +0000316 child_iterator I = child_begin(Err, false);
317 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000318 return;
319 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000320
Rafael Espindolacc371202016-06-24 13:47:29 +0000321 // This is at least a valid empty archive. Since an empty archive is the
322 // same in all formats, just claim it to be gnu to make sure Format is
323 // initialized.
324 Format = K_GNU;
325
Kevin Enderby7a969422015-11-05 19:24:56 +0000326 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000327 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000328 return;
329 }
Lang Hamesfc209622016-07-14 02:24:01 +0000330 const Child *C = &*I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000331
Kevin Enderby7a969422015-11-05 19:24:56 +0000332 auto Increment = [&]() {
333 ++I;
Lang Hamesfc209622016-07-14 02:24:01 +0000334 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000335 return true;
Lang Hamesfc209622016-07-14 02:24:01 +0000336 C = &*I;
Kevin Enderby7a969422015-11-05 19:24:56 +0000337 return false;
338 };
339
340 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000341
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000342 // Below is the pattern that is used to figure out the archive format
343 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000344 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000345 // Second member : // (may exist, if it exists, points to the string table)
346 // Note : The string table is used if the filename exceeds 15 characters
347 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000348 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
349 // There is no string table, if the filename exceeds 15 characters or has a
350 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000351 // of the filename that needs to be read after the archive header
352 // COFF archive format
353 // First member : /
354 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000355 // Third member : // (may exist, if it exists, contains the string table)
356 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
357 // even if the string table is empty. However, lib.exe does not in fact
358 // seem to create the third member if there's no member whose filename
359 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000360
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000361 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
362 if (Name == "__.SYMDEF")
363 Format = K_BSD;
364 else // Name == "__.SYMDEF_64"
365 Format = K_DARWIN64;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000366 // We know that the symbol table is not an external file, so we just assert
367 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000368 SymbolTable = *C->getBuffer();
369 if (Increment())
370 return;
371 setFirstRegular(*C);
372
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000373 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000374 return;
375 }
376
Rafael Espindola55509922013-07-10 22:07:59 +0000377 if (Name.startswith("#1/")) {
378 Format = K_BSD;
379 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000380 ErrorOr<StringRef> NameOrErr = C->getName();
Lang Hamesfc209622016-07-14 02:24:01 +0000381 if (auto ec = NameOrErr.getError()) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000382 Err = errorCodeToError(ec);
Rafael Espindola55509922013-07-10 22:07:59 +0000383 return;
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000384 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000385 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000386 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000387 // We know that the symbol table is not an external file, so we just
388 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000389 SymbolTable = *C->getBuffer();
390 if (Increment())
391 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000392 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000393 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
394 Format = K_DARWIN64;
395 // We know that the symbol table is not an external file, so we just
396 // assert there is no error.
397 SymbolTable = *C->getBuffer();
398 if (Increment())
399 return;
400 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000401 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000402 return;
403 }
404
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000405 // MIPS 64-bit ELF archives use a special format of a symbol table.
406 // This format is marked by `ar_name` field equals to "/SYM64/".
407 // For detailed description see page 96 in the following document:
408 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
409
410 bool has64SymTable = false;
411 if (Name == "/" || Name == "/SYM64/") {
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();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000415 if (Name == "/SYM64/")
416 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000417
Kevin Enderby7a969422015-11-05 19:24:56 +0000418 if (Increment())
419 return;
420 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000421 Err = Error::success();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000422 return;
423 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000424 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000425 }
426
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000427 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000428 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000429 // The string table is never an external member, so we just assert on the
430 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000431 StringTable = *C->getBuffer();
432 if (Increment())
433 return;
434 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000435 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000436 return;
437 }
438
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000439 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000440 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000441 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000442 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000443 return;
444 }
445
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000446 if (Name != "/") {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000447 Err = errorCodeToError(object_error::parse_failed);
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000448 return;
449 }
450
451 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000452 // We know that the symbol table is not an external file, so we just assert
453 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000454 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000455
Kevin Enderby7a969422015-11-05 19:24:56 +0000456 if (Increment())
457 return;
458
459 if (I == E) {
460 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000461 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000462 return;
463 }
464
Kevin Enderby7a969422015-11-05 19:24:56 +0000465 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000466
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000467 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000468 // The string table is never an external member, so we just assert on the
469 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000470 StringTable = *C->getBuffer();
471 if (Increment())
472 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000473 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000474
Kevin Enderby7a969422015-11-05 19:24:56 +0000475 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000476 Err = Error::success();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000477}
478
Lang Hamesfc209622016-07-14 02:24:01 +0000479Archive::child_iterator Archive::child_begin(Error &Err,
480 bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000481 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000482 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000483
484 if (SkipInternal)
Lang Hamesfc209622016-07-14 02:24:01 +0000485 return child_iterator(Child(this, FirstRegularData,
486 FirstRegularStartOfFile),
487 &Err);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000488
Rafael Espindola48af1c22014-08-19 18:44:46 +0000489 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby6524bd82016-07-19 20:47:07 +0000490 Child C(this, Loc, &Err);
491 if (Err)
Lang Hamesfc209622016-07-14 02:24:01 +0000492 return child_end();
Lang Hamesfc209622016-07-14 02:24:01 +0000493 return child_iterator(C, &Err);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000494}
495
Rafael Espindola23a97502014-01-21 16:09:45 +0000496Archive::child_iterator Archive::child_end() const {
Lang Hamesfc209622016-07-14 02:24:01 +0000497 return child_iterator(Child(this, nullptr, nullptr), nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000498}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000499
Rafael Espindolaae460022014-06-16 16:08:36 +0000500StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000501 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000502}
503
Rafael Espindolacc86d822015-11-03 01:20:44 +0000504ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000505 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000506 const char *Offsets = Buf;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000507 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000508 Offsets += sizeof(uint64_t);
509 else
510 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000511 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000512 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000513 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000514 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000515 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000516 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000517 // The SymbolIndex is an index into the ranlib structs that start at
518 // Offsets (the first uint32_t is the number of bytes of the ranlib
519 // structs). The ranlib structs are a pair of uint32_t's the first
520 // being a string table offset and the second being the offset into
521 // the archive of the member that defines the symbol. Which is what
522 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000523 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000524 } else if (Parent->kind() == K_DARWIN64) {
525 // The SymbolIndex is an index into the ranlib_64 structs that start at
526 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
527 // structs). The ranlib_64 structs are a pair of uint64_t's the first
528 // being a string table offset and the second being the offset into
529 // the archive of the member that defines the symbol. Which is what
530 // is needed here.
531 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000532 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000533 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000534 uint32_t MemberCount = read32le(Buf);
535 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000536
Rui Ueyama3206b792015-03-02 21:19:12 +0000537 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000538 if (SymbolIndex >= SymbolCount)
539 return object_error::parse_failed;
540
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000541 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000542 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000543
544 // Get the index of the offset in the file member offset table for this
545 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000546 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000547 // Subtract 1 since OffsetIndex is 1 based.
548 --OffsetIndex;
549
550 if (OffsetIndex >= MemberCount)
551 return object_error::parse_failed;
552
Rui Ueyama3206b792015-03-02 21:19:12 +0000553 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000554 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000555
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000556 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000557 Error Err;
558 Child C(Parent, Loc, &Err);
559 if (Err)
560 return errorToErrorCode(std::move(Err));
Kevin Enderby7a969422015-11-05 19:24:56 +0000561 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000562}
563
564Archive::Symbol Archive::Symbol::getNext() const {
565 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000566 if (Parent->kind() == K_BSD) {
567 // t.StringIndex is an offset from the start of the __.SYMDEF or
568 // "__.SYMDEF SORTED" member into the string table for the ranlib
569 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
570 // offset in the string table for t.SymbolIndex+1 we subtract the
571 // its offset from the start of the string table for t.SymbolIndex
572 // and add the offset of the string table for t.SymbolIndex+1.
573
574 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
575 // which is the number of bytes of ranlib structs that follow. The ranlib
576 // structs are a pair of uint32_t's the first being a string table offset
577 // and the second being the offset into the archive of the member that
578 // define the symbol. After that the next uint32_t is the byte count of
579 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000580 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000581 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000582 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000583 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
584 // don't change the t.StringIndex as we don't want to reference a ranlib
585 // past RanlibCount.
586 if (t.SymbolIndex + 1 < RanlibCount) {
587 const char *Ranlibs = Buf + 4;
588 uint32_t CurRanStrx = 0;
589 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000590 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
591 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000592 t.StringIndex -= CurRanStrx;
593 t.StringIndex += NextRanStrx;
594 }
595 } else {
596 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000597 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000598 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000599 ++t.SymbolIndex;
600 return t;
601}
602
Rafael Espindola23a97502014-01-21 16:09:45 +0000603Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000604 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000605 return symbol_iterator(Symbol(this, 0, 0));
606
Rafael Espindola2b054162015-07-14 01:06:16 +0000607 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000608 if (kind() == K_GNU) {
609 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000610 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000611 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000612 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000613 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000614 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000615 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000616 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
617 // which is the number of bytes of ranlib structs that follow. The ranlib
618 // structs are a pair of uint32_t's the first being a string table offset
619 // and the second being the offset into the archive of the member that
620 // define the symbol. After that the next uint32_t is the byte count of
621 // the string table followed by the string table.
622 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000623 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000624 const char *ranlibs = buf + 4;
625 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000626 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000627 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
628 // Skip the byte count of the string table.
629 buf += sizeof(uint32_t);
630 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000631 } else if (kind() == K_DARWIN64) {
632 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
633 // which is the number of bytes of ranlib_64 structs that follow. The
634 // ranlib_64 structs are a pair of uint64_t's the first being a string
635 // table offset and the second being the offset into the archive of the
636 // member that define the symbol. After that the next uint64_t is the byte
637 // count of the string table followed by the string table.
638 uint64_t ranlib_count = 0;
639 ranlib_count = read64le(buf) / 16;
640 const char *ranlibs = buf + 8;
641 uint64_t ran_strx = 0;
642 ran_strx = read64le(ranlibs);
643 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
644 // Skip the byte count of the string table.
645 buf += sizeof(uint64_t);
646 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000647 } else {
648 uint32_t member_count = 0;
649 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000650 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000651 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000652 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000653 buf += 4 + (symbol_count * 2); // Skip indices.
654 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000655 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000656 return symbol_iterator(Symbol(this, 0, string_start_offset));
657}
658
Rafael Espindola23a97502014-01-21 16:09:45 +0000659Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000660 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
661}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000662
Rui Ueyama407e0972015-05-26 16:20:40 +0000663uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000664 if (!hasSymbolTable())
665 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000666 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000667 if (kind() == K_GNU)
668 return read32be(buf);
669 if (kind() == K_MIPS64)
670 return read64be(buf);
671 if (kind() == K_BSD)
672 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000673 if (kind() == K_DARWIN64)
674 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000675 uint32_t member_count = 0;
676 member_count = read32le(buf);
677 buf += 4 + (member_count * 4); // Skip offsets.
678 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000679}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000680
Lang Hames69f49022016-07-14 20:44:27 +0000681Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000682 Archive::symbol_iterator bs = symbol_begin();
683 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000684
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000685 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000686 StringRef SymName = bs->getName();
687 if (SymName == name) {
Lang Hames69f49022016-07-14 20:44:27 +0000688 if (auto MemberOrErr = bs->getMember())
689 return Child(*MemberOrErr);
690 else
691 return errorCodeToError(MemberOrErr.getError());
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000692 }
693 }
Lang Hames69f49022016-07-14 20:44:27 +0000694 return Optional<Child>();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000695}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000696
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000697bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }