blob: 4bb50a0f7570c0d2e01f8aa83fd028620e746116 [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 Enderbyda9dd052015-10-21 17:13:20 +000049 return object_error::parse_failed;
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;
55 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
56 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;
62 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
63 .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;
73 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
74 llvm_unreachable("UID time not a decimal number.");
75 return Ret;
76}
77
78unsigned ArchiveMemberHeader::getGID() const {
79 unsigned Ret;
80 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
81 llvm_unreachable("GID time not a decimal number.");
82 return Ret;
83}
84
Kevin Enderbyda9dd052015-10-21 17:13:20 +000085Archive::Child::Child(const Archive *Parent, const char *Start)
Rafael Espindola0f3de642013-07-09 05:26:25 +000086 : Parent(Parent) {
87 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000088 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000089
Rafael Espindola9d102062014-12-16 01:43:41 +000090 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +000091 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +000092 if (!isThinMember()) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +000093 Size += getRawSize();
Rafael Espindolabe9ab262015-07-22 19:34:26 +000094 Data = StringRef(Start, Size);
95 }
Rafael Espindola0f3de642013-07-09 05:26:25 +000096
Rafael Espindola747bc072013-07-09 03:39:35 +000097 // Setup StartOfFile and PaddingBytes.
98 StartOfFile = sizeof(ArchiveMemberHeader);
99 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000100 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000101 if (Name.startswith("#1/")) {
102 uint64_t NameSize;
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000103 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
104 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000105 StartOfFile += NameSize;
106 }
107}
108
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000109uint64_t Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000110 if (Parent->IsThin) {
111 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000112 if (Size.getError())
113 return 0;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000114 return Size.get();
115 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000116 return Data.size() - StartOfFile;
117}
118
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000119uint64_t Archive::Child::getRawSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000120 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000121 if (Size.getError())
122 return 0;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000123 return Size.get();
Kevin Enderby13023a12015-01-15 23:19:11 +0000124}
125
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000126bool Archive::Child::isThinMember() const {
127 StringRef Name = getHeader()->getName();
128 return Parent->IsThin && Name != "/" && Name != "//";
129}
130
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000131ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000132 if (!isThinMember())
133 return StringRef(Data.data() + StartOfFile, getSize());
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000134 ErrorOr<StringRef> Name = getName();
135 if (std::error_code EC = Name.getError())
136 return EC;
Rafael Espindolaf662e002015-07-15 21:24:07 +0000137 SmallString<128> FullName = sys::path::parent_path(
138 Parent->getMemoryBufferRef().getBufferIdentifier());
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000139 sys::path::append(FullName, *Name);
140 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
141 if (std::error_code EC = Buf.getError())
142 return EC;
143 Parent->ThinBuffers.push_back(std::move(*Buf));
144 return Parent->ThinBuffers.back()->getBuffer();
145}
146
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000147Archive::Child Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000148 size_t SpaceToSkip = Data.size();
149 // If it's odd, add 1 to make it even.
150 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000151 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000152
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000153 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000154
155 // Check to see if this is past the end of the archive.
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000156 if (NextLoc >= Parent->Data.getBufferEnd())
157 return Child(Parent, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000158
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000159 return Child(Parent, NextLoc);
Rafael Espindola747bc072013-07-09 03:39:35 +0000160}
161
Kevin Enderby13023a12015-01-15 23:19:11 +0000162uint64_t Archive::Child::getChildOffset() const {
163 const char *a = Parent->Data.getBuffer().data();
164 const char *c = Data.data();
165 uint64_t offset = c - a;
166 return offset;
167}
168
Rafael Espindolaae460022014-06-16 16:08:36 +0000169ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000170 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000171 // Check if it's a special name.
172 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000173 if (name.size() == 1) // Linker member.
174 return name;
175 if (name.size() == 2 && name[1] == '/') // String table.
176 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000177 // It's a long name.
178 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000179 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000180 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000181 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000182
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000183 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000184 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000185 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000186 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000187
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000188 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000189 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000190 StringRef::size_type End = StringRef(addr).find('\n');
191 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000192 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000193 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000194 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000195 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000196 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000197 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindolaae460022014-06-16 16:08:36 +0000198 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000199 .rtrim(StringRef("\0", 1));
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000200 }
201 // It's a simple name.
202 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000203 return name.substr(0, name.size() - 1);
204 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000205}
206
Rafael Espindola48af1c22014-08-19 18:44:46 +0000207ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000208 ErrorOr<StringRef> NameOrErr = getName();
209 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000210 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000211 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000212 ErrorOr<StringRef> Buf = getBuffer();
213 if (std::error_code EC = Buf.getError())
214 return EC;
215 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000216}
217
218ErrorOr<std::unique_ptr<Binary>>
219Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000220 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000221 if (std::error_code EC = BuffOrErr.getError())
222 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000223
Rafael Espindola48af1c22014-08-19 18:44:46 +0000224 return createBinary(BuffOrErr.get(), Context);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000225}
226
Rafael Espindola48af1c22014-08-19 18:44:46 +0000227ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000228 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000229 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000230 if (EC)
231 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000232 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000233}
234
Rafael Espindola48af1c22014-08-19 18:44:46 +0000235Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000236 : Binary(Binary::ID_Archive, Source), FirstRegular(child_end()) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000237 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000238 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000239 if (Buffer.startswith(ThinMagic)) {
240 IsThin = true;
241 } else if (Buffer.startswith(Magic)) {
242 IsThin = false;
243 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000244 ec = object_error::invalid_file_type;
245 return;
246 }
247
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000248 // Get the special members.
Rafael Espindola23a97502014-01-21 16:09:45 +0000249 child_iterator i = child_begin(false);
250 child_iterator e = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000251
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000252 if (i == e) {
253 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000254 return;
255 }
256
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000257 StringRef Name = i->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000258
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000259 // Below is the pattern that is used to figure out the archive format
260 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000261 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000262 // Second member : // (may exist, if it exists, points to the string table)
263 // Note : The string table is used if the filename exceeds 15 characters
264 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000265 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
266 // There is no string table, if the filename exceeds 15 characters or has a
267 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000268 // of the filename that needs to be read after the archive header
269 // COFF archive format
270 // First member : /
271 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000272 // Third member : // (may exist, if it exists, contains the string table)
273 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
274 // even if the string table is empty. However, lib.exe does not in fact
275 // seem to create the third member if there's no member whose filename
276 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000277
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000278 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000279 Format = K_BSD;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000280 // We know that the symbol table is not an external file, so we just assert
281 // there is no error.
282 SymbolTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000283 ++i;
284 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000285 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000286 return;
287 }
288
Rafael Espindola55509922013-07-10 22:07:59 +0000289 if (Name.startswith("#1/")) {
290 Format = K_BSD;
291 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000292 ErrorOr<StringRef> NameOrErr = i->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000293 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000294 if (ec)
295 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000296 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000297 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000298 // We know that the symbol table is not an external file, so we just
299 // assert there is no error.
300 SymbolTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000301 ++i;
302 }
303 FirstRegular = i;
Rafael Espindola55509922013-07-10 22:07:59 +0000304 return;
305 }
306
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000307 // MIPS 64-bit ELF archives use a special format of a symbol table.
308 // This format is marked by `ar_name` field equals to "/SYM64/".
309 // For detailed description see page 96 in the following document:
310 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
311
312 bool has64SymTable = false;
313 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000314 // We know that the symbol table is not an external file, so we just assert
315 // there is no error.
316 SymbolTable = *i->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000317 if (Name == "/SYM64/")
318 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000319
320 ++i;
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000321 if (i == e) {
322 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000323 return;
324 }
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000325 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000326 }
327
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000328 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000329 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000330 // The string table is never an external member, so we just assert on the
331 // ErrorOr.
332 StringTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000333 ++i;
334 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000335 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000336 return;
337 }
338
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000339 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000340 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000341 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000342 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000343 return;
344 }
345
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000346 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000347 ec = object_error::parse_failed;
348 return;
349 }
350
351 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000352 // We know that the symbol table is not an external file, so we just assert
353 // there is no error.
354 SymbolTable = *i->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000355
356 ++i;
357 if (i == e) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000358 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000359 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000360 return;
361 }
362
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000363 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000364
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000365 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000366 // The string table is never an external member, so we just assert on the
367 // ErrorOr.
368 StringTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000369 ++i;
370 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000371
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000372 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000373 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000374}
375
Rafael Espindola23a97502014-01-21 16:09:45 +0000376Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000377 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000378 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000379
380 if (SkipInternal)
381 return FirstRegular;
382
Rafael Espindola48af1c22014-08-19 18:44:46 +0000383 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000384 Child c(this, Loc);
385 return c;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000386}
387
Rafael Espindola23a97502014-01-21 16:09:45 +0000388Archive::child_iterator Archive::child_end() const {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000389 return Child(this, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000390}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000391
Rafael Espindolaae460022014-06-16 16:08:36 +0000392StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000393 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000394}
395
Rafael Espindolaae460022014-06-16 16:08:36 +0000396ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000397 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000398 const char *Offsets = Buf;
399 if (Parent->kind() == K_MIPS64)
400 Offsets += sizeof(uint64_t);
401 else
402 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000403 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000404 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000405 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000406 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000407 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000408 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000409 // The SymbolIndex is an index into the ranlib structs that start at
410 // Offsets (the first uint32_t is the number of bytes of the ranlib
411 // structs). The ranlib structs are a pair of uint32_t's the first
412 // being a string table offset and the second being the offset into
413 // the archive of the member that defines the symbol. Which is what
414 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000415 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000416 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000417 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000418 uint32_t MemberCount = read32le(Buf);
419 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000420
Rui Ueyama3206b792015-03-02 21:19:12 +0000421 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000422 if (SymbolIndex >= SymbolCount)
423 return object_error::parse_failed;
424
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000425 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000426 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000427
428 // Get the index of the offset in the file member offset table for this
429 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000430 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000431 // Subtract 1 since OffsetIndex is 1 based.
432 --OffsetIndex;
433
434 if (OffsetIndex >= MemberCount)
435 return object_error::parse_failed;
436
Rui Ueyama3206b792015-03-02 21:19:12 +0000437 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000438 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000439
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000440 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000441 child_iterator Iter(Child(Parent, Loc));
Rafael Espindolaae460022014-06-16 16:08:36 +0000442 return Iter;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000443}
444
445Archive::Symbol Archive::Symbol::getNext() const {
446 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000447 if (Parent->kind() == K_BSD) {
448 // t.StringIndex is an offset from the start of the __.SYMDEF or
449 // "__.SYMDEF SORTED" member into the string table for the ranlib
450 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
451 // offset in the string table for t.SymbolIndex+1 we subtract the
452 // its offset from the start of the string table for t.SymbolIndex
453 // and add the offset of the string table for t.SymbolIndex+1.
454
455 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
456 // which is the number of bytes of ranlib structs that follow. The ranlib
457 // structs are a pair of uint32_t's the first being a string table offset
458 // and the second being the offset into the archive of the member that
459 // define the symbol. After that the next uint32_t is the byte count of
460 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000461 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000462 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000463 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000464 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
465 // don't change the t.StringIndex as we don't want to reference a ranlib
466 // past RanlibCount.
467 if (t.SymbolIndex + 1 < RanlibCount) {
468 const char *Ranlibs = Buf + 4;
469 uint32_t CurRanStrx = 0;
470 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000471 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
472 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000473 t.StringIndex -= CurRanStrx;
474 t.StringIndex += NextRanStrx;
475 }
476 } else {
477 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000478 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000479 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000480 ++t.SymbolIndex;
481 return t;
482}
483
Rafael Espindola23a97502014-01-21 16:09:45 +0000484Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000485 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000486 return symbol_iterator(Symbol(this, 0, 0));
487
Rafael Espindola2b054162015-07-14 01:06:16 +0000488 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000489 if (kind() == K_GNU) {
490 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000491 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000492 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000493 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000494 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000495 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000496 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000497 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
498 // which is the number of bytes of ranlib structs that follow. The ranlib
499 // structs are a pair of uint32_t's the first being a string table offset
500 // and the second being the offset into the archive of the member that
501 // define the symbol. After that the next uint32_t is the byte count of
502 // the string table followed by the string table.
503 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000504 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000505 const char *ranlibs = buf + 4;
506 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000507 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000508 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
509 // Skip the byte count of the string table.
510 buf += sizeof(uint32_t);
511 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000512 } else {
513 uint32_t member_count = 0;
514 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000515 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000516 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000517 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000518 buf += 4 + (symbol_count * 2); // Skip indices.
519 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000520 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000521 return symbol_iterator(Symbol(this, 0, string_start_offset));
522}
523
Rafael Espindola23a97502014-01-21 16:09:45 +0000524Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000525 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
526}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000527
Rui Ueyama407e0972015-05-26 16:20:40 +0000528uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000529 if (!hasSymbolTable())
530 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000531 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000532 if (kind() == K_GNU)
533 return read32be(buf);
534 if (kind() == K_MIPS64)
535 return read64be(buf);
536 if (kind() == K_BSD)
537 return read32le(buf) / 8;
538 uint32_t member_count = 0;
539 member_count = read32le(buf);
540 buf += 4 + (member_count * 4); // Skip offsets.
541 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000542}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000543
544Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000545 Archive::symbol_iterator bs = symbol_begin();
546 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000547
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000548 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000549 StringRef SymName = bs->getName();
550 if (SymName == name) {
551 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
552 // FIXME: Should we really eat the error?
553 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000554 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000555 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000556 }
557 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000558 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000559}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000560
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000561bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }