blob: 8a3b34b015890d46a63b1f56ea4e42fe1a667708 [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"
15#include "llvm/ADT/APInt.h"
Rafael Espindola747bc072013-07-09 03:39:35 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Twine.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000018#include "llvm/Support/Endian.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000019#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola4b83cb52015-07-14 22:18:43 +000020#include "llvm/Support/Path.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000021
22using namespace llvm;
23using namespace object;
Rui Ueyama3206b792015-03-02 21:19:12 +000024using namespace llvm::support::endian;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000025
Craig Topperd3a34f82013-07-16 01:17:10 +000026static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000027static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000028
David Blaikiea379b1812011-12-20 02:50:00 +000029void Archive::anchor() { }
30
Rafael Espindola747bc072013-07-09 03:39:35 +000031StringRef ArchiveMemberHeader::getName() const {
32 char EndCond;
33 if (Name[0] == '/' || Name[0] == '#')
34 EndCond = ' ';
35 else
36 EndCond = '/';
37 llvm::StringRef::size_type end =
38 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
39 if (end == llvm::StringRef::npos)
40 end = sizeof(Name);
41 assert(end <= sizeof(Name) && end > 0);
42 // Don't include the EndCond if there is one.
43 return llvm::StringRef(Name, end);
44}
45
Kevin Enderby1c1add42015-10-13 20:48:04 +000046ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +000047 uint32_t Ret;
48 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
Kevin Enderby7a969422015-11-05 19:24:56 +000049 return object_error::parse_failed; // Size is not a decimal number.
Rafael Espindola8e9385e2013-07-09 12:45:11 +000050 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000051}
52
Rafael Espindola8115e1d2013-07-09 12:49:24 +000053sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
54 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000055 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(' ').getAsInteger(8, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000056 llvm_unreachable("Access mode is not an octal number.");
57 return static_cast<sys::fs::perms>(Ret);
58}
59
60sys::TimeValue ArchiveMemberHeader::getLastModified() const {
61 unsigned Seconds;
Vedant Kumar98372e32016-02-16 02:06:01 +000062 if (StringRef(LastModified, sizeof(LastModified)).rtrim(' ')
Rafael Espindola8115e1d2013-07-09 12:49:24 +000063 .getAsInteger(10, Seconds))
64 llvm_unreachable("Last modified time not a decimal number.");
65
66 sys::TimeValue Ret;
67 Ret.fromEpochTime(Seconds);
68 return Ret;
69}
70
71unsigned ArchiveMemberHeader::getUID() const {
72 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000073 if (StringRef(UID, sizeof(UID)).rtrim(' ').getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000074 llvm_unreachable("UID time not a decimal number.");
75 return Ret;
76}
77
78unsigned ArchiveMemberHeader::getGID() const {
79 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000080 if (StringRef(GID, sizeof(GID)).rtrim(' ').getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000081 llvm_unreachable("GID time not a decimal number.");
82 return Ret;
83}
84
Rafael Espindola43358762015-10-31 21:44:42 +000085Archive::Child::Child(const Archive *Parent, StringRef Data,
86 uint16_t StartOfFile)
87 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
88
Kevin Enderby7a969422015-11-05 19:24:56 +000089Archive::Child::Child(const Archive *Parent, const char *Start,
90 std::error_code *EC)
Rafael Espindola0f3de642013-07-09 05:26:25 +000091 : Parent(Parent) {
92 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000093 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000094
Rafael Espindola9d102062014-12-16 01:43:41 +000095 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +000096 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +000097 if (!isThinMember()) {
Kevin Enderby7a969422015-11-05 19:24:56 +000098 ErrorOr<uint64_t> MemberSize = getRawSize();
99 if ((*EC = MemberSize.getError()))
100 return;
101 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000102 Data = StringRef(Start, Size);
103 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000104
Rafael Espindola747bc072013-07-09 03:39:35 +0000105 // Setup StartOfFile and PaddingBytes.
106 StartOfFile = sizeof(ArchiveMemberHeader);
107 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000108 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000109 if (Name.startswith("#1/")) {
110 uint64_t NameSize;
Vedant Kumar98372e32016-02-16 02:06:01 +0000111 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000112 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000113 StartOfFile += NameSize;
114 }
115}
116
Kevin Enderby7a969422015-11-05 19:24:56 +0000117ErrorOr<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000118 if (Parent->IsThin) {
119 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000120 if (std::error_code EC = Size.getError())
121 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000122 return Size.get();
123 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000124 return Data.size() - StartOfFile;
125}
126
Kevin Enderby7a969422015-11-05 19:24:56 +0000127ErrorOr<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000128 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000129 if (std::error_code EC = Size.getError())
130 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000131 return Size.get();
Kevin Enderby13023a12015-01-15 23:19:11 +0000132}
133
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000134bool Archive::Child::isThinMember() const {
135 StringRef Name = getHeader()->getName();
136 return Parent->IsThin && Name != "/" && Name != "//";
137}
138
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000139ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000140 if (!isThinMember()) {
141 ErrorOr<uint32_t> Size = getSize();
142 if (std::error_code EC = Size.getError())
143 return EC;
144 return StringRef(Data.data() + StartOfFile, Size.get());
145 }
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000146 ErrorOr<StringRef> Name = getName();
147 if (std::error_code EC = Name.getError())
148 return EC;
Peter Collingbournef84646c2016-03-31 22:08:31 +0000149 SmallString<128> FullName;
150 if (sys::path::is_absolute(*Name))
151 FullName = *Name;
152 else {
153 FullName = sys::path::parent_path(
154 Parent->getMemoryBufferRef().getBufferIdentifier());
155 sys::path::append(FullName, *Name);
156 }
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000157 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
158 if (std::error_code EC = Buf.getError())
159 return EC;
160 Parent->ThinBuffers.push_back(std::move(*Buf));
161 return Parent->ThinBuffers.back()->getBuffer();
162}
163
Kevin Enderby7a969422015-11-05 19:24:56 +0000164ErrorOr<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000165 size_t SpaceToSkip = Data.size();
166 // If it's odd, add 1 to make it even.
167 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000168 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000169
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000170 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000171
Kevin Enderby7a969422015-11-05 19:24:56 +0000172 // Check to see if this is at the end of the archive.
173 if (NextLoc == Parent->Data.getBufferEnd())
174 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000175
Kevin Enderby7a969422015-11-05 19:24:56 +0000176 // Check to see if this is past the end of the archive.
177 if (NextLoc > Parent->Data.getBufferEnd())
178 return object_error::parse_failed;
179
180 std::error_code EC;
181 Child Ret(Parent, NextLoc, &EC);
182 if (EC)
183 return EC;
184 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000185}
186
Kevin Enderby13023a12015-01-15 23:19:11 +0000187uint64_t Archive::Child::getChildOffset() const {
188 const char *a = Parent->Data.getBuffer().data();
189 const char *c = Data.data();
190 uint64_t offset = c - a;
191 return offset;
192}
193
Rafael Espindolaae460022014-06-16 16:08:36 +0000194ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000195 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000196 // Check if it's a special name.
197 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000198 if (name.size() == 1) // Linker member.
199 return name;
200 if (name.size() == 2 && name[1] == '/') // String table.
201 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000202 // It's a long name.
203 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000204 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000205 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000206 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000207
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000208 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000209 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000210 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000211 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000212
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000213 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000214 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000215 StringRef::size_type End = StringRef(addr).find('\n');
216 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000217 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000218 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000219 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000220 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000221 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000222 llvm_unreachable("Long name length is not an ingeter");
Vedant Kumar98372e32016-02-16 02:06:01 +0000223 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000224 }
225 // It's a simple name.
226 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000227 return name.substr(0, name.size() - 1);
228 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000229}
230
Rafael Espindola48af1c22014-08-19 18:44:46 +0000231ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000232 ErrorOr<StringRef> NameOrErr = getName();
233 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000234 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000235 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000236 ErrorOr<StringRef> Buf = getBuffer();
237 if (std::error_code EC = Buf.getError())
238 return EC;
239 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000240}
241
242ErrorOr<std::unique_ptr<Binary>>
243Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000244 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000245 if (std::error_code EC = BuffOrErr.getError())
246 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000247
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000248 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
249 if (BinaryOrErr)
250 return std::move(*BinaryOrErr);
251 return errorToErrorCode(BinaryOrErr.takeError());
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000252}
253
Rafael Espindola48af1c22014-08-19 18:44:46 +0000254ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000255 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000256 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000257 if (EC)
258 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000259 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000260}
261
Rafael Espindola43358762015-10-31 21:44:42 +0000262void Archive::setFirstRegular(const Child &C) {
263 FirstRegularData = C.Data;
264 FirstRegularStartOfFile = C.StartOfFile;
265}
266
Rafael Espindola48af1c22014-08-19 18:44:46 +0000267Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindola43358762015-10-31 21:44:42 +0000268 : Binary(Binary::ID_Archive, Source) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000269 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000270 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000271 if (Buffer.startswith(ThinMagic)) {
272 IsThin = true;
273 } else if (Buffer.startswith(Magic)) {
274 IsThin = false;
275 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000276 ec = object_error::invalid_file_type;
277 return;
278 }
279
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000280 // Get the special members.
Kevin Enderby7a969422015-11-05 19:24:56 +0000281 child_iterator I = child_begin(false);
282 if ((ec = I->getError()))
283 return;
284 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000285
Kevin Enderby7a969422015-11-05 19:24:56 +0000286 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000287 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000288 return;
289 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000290 const Child *C = &**I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000291
Kevin Enderby7a969422015-11-05 19:24:56 +0000292 auto Increment = [&]() {
293 ++I;
294 if ((ec = I->getError()))
295 return true;
296 C = &**I;
297 return false;
298 };
299
300 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000301
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000302 // Below is the pattern that is used to figure out the archive format
303 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000304 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000305 // Second member : // (may exist, if it exists, points to the string table)
306 // Note : The string table is used if the filename exceeds 15 characters
307 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000308 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
309 // There is no string table, if the filename exceeds 15 characters or has a
310 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000311 // of the filename that needs to be read after the archive header
312 // COFF archive format
313 // First member : /
314 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000315 // Third member : // (may exist, if it exists, contains the string table)
316 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
317 // even if the string table is empty. However, lib.exe does not in fact
318 // seem to create the third member if there's no member whose filename
319 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000320
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000321 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000322 Format = K_BSD;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000323 // We know that the symbol table is not an external file, so we just assert
324 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000325 SymbolTable = *C->getBuffer();
326 if (Increment())
327 return;
328 setFirstRegular(*C);
329
Rui Ueyama7d099192015-06-09 15:20:42 +0000330 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000331 return;
332 }
333
Rafael Espindola55509922013-07-10 22:07:59 +0000334 if (Name.startswith("#1/")) {
335 Format = K_BSD;
336 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000337 ErrorOr<StringRef> NameOrErr = C->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000338 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000339 if (ec)
340 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000341 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000342 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000343 // We know that the symbol table is not an external file, so we just
344 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000345 SymbolTable = *C->getBuffer();
346 if (Increment())
347 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000348 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000349 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000350 return;
351 }
352
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000353 // MIPS 64-bit ELF archives use a special format of a symbol table.
354 // This format is marked by `ar_name` field equals to "/SYM64/".
355 // For detailed description see page 96 in the following document:
356 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
357
358 bool has64SymTable = false;
359 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000360 // We know that the symbol table is not an external file, so we just assert
361 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000362 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000363 if (Name == "/SYM64/")
364 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000365
Kevin Enderby7a969422015-11-05 19:24:56 +0000366 if (Increment())
367 return;
368 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000369 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000370 return;
371 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000372 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000373 }
374
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000375 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000376 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000377 // The string table is never an external member, so we just assert on the
378 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000379 StringTable = *C->getBuffer();
380 if (Increment())
381 return;
382 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000383 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000384 return;
385 }
386
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000387 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000388 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000389 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000390 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000391 return;
392 }
393
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000394 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000395 ec = object_error::parse_failed;
396 return;
397 }
398
399 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000400 // We know that the symbol table is not an external file, so we just assert
401 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000402 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000403
Kevin Enderby7a969422015-11-05 19:24:56 +0000404 if (Increment())
405 return;
406
407 if (I == E) {
408 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000409 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000410 return;
411 }
412
Kevin Enderby7a969422015-11-05 19:24:56 +0000413 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000414
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000415 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000416 // The string table is never an external member, so we just assert on the
417 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000418 StringTable = *C->getBuffer();
419 if (Increment())
420 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000421 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000422
Kevin Enderby7a969422015-11-05 19:24:56 +0000423 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000424 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000425}
426
Rafael Espindola23a97502014-01-21 16:09:45 +0000427Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000428 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000429 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000430
431 if (SkipInternal)
Rafael Espindola43358762015-10-31 21:44:42 +0000432 return Child(this, FirstRegularData, FirstRegularStartOfFile);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000433
Rafael Espindola48af1c22014-08-19 18:44:46 +0000434 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby7a969422015-11-05 19:24:56 +0000435 std::error_code EC;
436 Child c(this, Loc, &EC);
437 if (EC)
438 return child_iterator(EC);
439 return child_iterator(c);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000440}
441
Rafael Espindola23a97502014-01-21 16:09:45 +0000442Archive::child_iterator Archive::child_end() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000443 return Child(this, nullptr, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000444}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000445
Rafael Espindolaae460022014-06-16 16:08:36 +0000446StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000447 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000448}
449
Rafael Espindolacc86d822015-11-03 01:20:44 +0000450ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000451 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000452 const char *Offsets = Buf;
453 if (Parent->kind() == K_MIPS64)
454 Offsets += sizeof(uint64_t);
455 else
456 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000457 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000458 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000459 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000460 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000461 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000462 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000463 // The SymbolIndex is an index into the ranlib structs that start at
464 // Offsets (the first uint32_t is the number of bytes of the ranlib
465 // structs). The ranlib structs are a pair of uint32_t's the first
466 // being a string table offset and the second being the offset into
467 // the archive of the member that defines the symbol. Which is what
468 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000469 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000470 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000471 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000472 uint32_t MemberCount = read32le(Buf);
473 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000474
Rui Ueyama3206b792015-03-02 21:19:12 +0000475 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000476 if (SymbolIndex >= SymbolCount)
477 return object_error::parse_failed;
478
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000479 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000480 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000481
482 // Get the index of the offset in the file member offset table for this
483 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000484 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000485 // Subtract 1 since OffsetIndex is 1 based.
486 --OffsetIndex;
487
488 if (OffsetIndex >= MemberCount)
489 return object_error::parse_failed;
490
Rui Ueyama3206b792015-03-02 21:19:12 +0000491 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000492 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000493
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000494 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby7a969422015-11-05 19:24:56 +0000495 std::error_code EC;
496 Child C(Parent, Loc, &EC);
497 if (EC)
498 return EC;
499 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000500}
501
502Archive::Symbol Archive::Symbol::getNext() const {
503 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000504 if (Parent->kind() == K_BSD) {
505 // t.StringIndex is an offset from the start of the __.SYMDEF or
506 // "__.SYMDEF SORTED" member into the string table for the ranlib
507 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
508 // offset in the string table for t.SymbolIndex+1 we subtract the
509 // its offset from the start of the string table for t.SymbolIndex
510 // and add the offset of the string table for t.SymbolIndex+1.
511
512 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
513 // which is the number of bytes of ranlib structs that follow. The ranlib
514 // structs are a pair of uint32_t's the first being a string table offset
515 // and the second being the offset into the archive of the member that
516 // define the symbol. After that the next uint32_t is the byte count of
517 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000518 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000519 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000520 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000521 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
522 // don't change the t.StringIndex as we don't want to reference a ranlib
523 // past RanlibCount.
524 if (t.SymbolIndex + 1 < RanlibCount) {
525 const char *Ranlibs = Buf + 4;
526 uint32_t CurRanStrx = 0;
527 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000528 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
529 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000530 t.StringIndex -= CurRanStrx;
531 t.StringIndex += NextRanStrx;
532 }
533 } else {
534 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000535 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000536 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000537 ++t.SymbolIndex;
538 return t;
539}
540
Rafael Espindola23a97502014-01-21 16:09:45 +0000541Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000542 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000543 return symbol_iterator(Symbol(this, 0, 0));
544
Rafael Espindola2b054162015-07-14 01:06:16 +0000545 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000546 if (kind() == K_GNU) {
547 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000548 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000549 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000550 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000551 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000552 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000553 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000554 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
555 // which is the number of bytes of ranlib structs that follow. The ranlib
556 // structs are a pair of uint32_t's the first being a string table offset
557 // and the second being the offset into the archive of the member that
558 // define the symbol. After that the next uint32_t is the byte count of
559 // the string table followed by the string table.
560 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000561 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000562 const char *ranlibs = buf + 4;
563 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000564 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000565 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
566 // Skip the byte count of the string table.
567 buf += sizeof(uint32_t);
568 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000569 } else {
570 uint32_t member_count = 0;
571 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000572 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000573 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000574 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000575 buf += 4 + (symbol_count * 2); // Skip indices.
576 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000577 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000578 return symbol_iterator(Symbol(this, 0, string_start_offset));
579}
580
Rafael Espindola23a97502014-01-21 16:09:45 +0000581Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000582 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
583}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000584
Rui Ueyama407e0972015-05-26 16:20:40 +0000585uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000586 if (!hasSymbolTable())
587 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000588 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000589 if (kind() == K_GNU)
590 return read32be(buf);
591 if (kind() == K_MIPS64)
592 return read64be(buf);
593 if (kind() == K_BSD)
594 return read32le(buf) / 8;
595 uint32_t member_count = 0;
596 member_count = read32le(buf);
597 buf += 4 + (member_count * 4); // Skip offsets.
598 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000599}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000600
601Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000602 Archive::symbol_iterator bs = symbol_begin();
603 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000604
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000605 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000606 StringRef SymName = bs->getName();
607 if (SymName == name) {
608 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
609 // FIXME: Should we really eat the error?
610 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000611 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000612 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000613 }
614 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000615 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000616}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000617
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000618bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }