blob: 99b0650c8b7e26efc0d7bd832f1cdc4f42cb0250 [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;
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
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;
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000111 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
112 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;
Rafael Espindolaf662e002015-07-15 21:24:07 +0000149 SmallString<128> FullName = sys::path::parent_path(
150 Parent->getMemoryBufferRef().getBufferIdentifier());
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000151 sys::path::append(FullName, *Name);
152 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
153 if (std::error_code EC = Buf.getError())
154 return EC;
155 Parent->ThinBuffers.push_back(std::move(*Buf));
156 return Parent->ThinBuffers.back()->getBuffer();
157}
158
Kevin Enderby7a969422015-11-05 19:24:56 +0000159ErrorOr<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000160 size_t SpaceToSkip = Data.size();
161 // If it's odd, add 1 to make it even.
162 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000163 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000164
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000165 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000166
Kevin Enderby7a969422015-11-05 19:24:56 +0000167 // Check to see if this is at the end of the archive.
168 if (NextLoc == Parent->Data.getBufferEnd())
169 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000170
Kevin Enderby7a969422015-11-05 19:24:56 +0000171 // Check to see if this is past the end of the archive.
172 if (NextLoc > Parent->Data.getBufferEnd())
173 return object_error::parse_failed;
174
175 std::error_code EC;
176 Child Ret(Parent, NextLoc, &EC);
177 if (EC)
178 return EC;
179 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000180}
181
Kevin Enderby13023a12015-01-15 23:19:11 +0000182uint64_t Archive::Child::getChildOffset() const {
183 const char *a = Parent->Data.getBuffer().data();
184 const char *c = Data.data();
185 uint64_t offset = c - a;
186 return offset;
187}
188
Rafael Espindolaae460022014-06-16 16:08:36 +0000189ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000190 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000191 // Check if it's a special name.
192 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000193 if (name.size() == 1) // Linker member.
194 return name;
195 if (name.size() == 2 && name[1] == '/') // String table.
196 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000197 // It's a long name.
198 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000199 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000200 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000201 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000202
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000203 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000204 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000205 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000206 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000207
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000208 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000209 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000210 StringRef::size_type End = StringRef(addr).find('\n');
211 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000212 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000213 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000214 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000215 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000216 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000217 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindolaae460022014-06-16 16:08:36 +0000218 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000219 .rtrim(StringRef("\0", 1));
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000220 }
221 // It's a simple name.
222 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000223 return name.substr(0, name.size() - 1);
224 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000225}
226
Rafael Espindola48af1c22014-08-19 18:44:46 +0000227ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000228 ErrorOr<StringRef> NameOrErr = getName();
229 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000230 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000231 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000232 ErrorOr<StringRef> Buf = getBuffer();
233 if (std::error_code EC = Buf.getError())
234 return EC;
235 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000236}
237
238ErrorOr<std::unique_ptr<Binary>>
239Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000240 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000241 if (std::error_code EC = BuffOrErr.getError())
242 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000243
Rafael Espindola48af1c22014-08-19 18:44:46 +0000244 return createBinary(BuffOrErr.get(), Context);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000245}
246
Rafael Espindola48af1c22014-08-19 18:44:46 +0000247ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000248 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000249 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000250 if (EC)
251 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000252 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000253}
254
Rafael Espindola43358762015-10-31 21:44:42 +0000255void Archive::setFirstRegular(const Child &C) {
256 FirstRegularData = C.Data;
257 FirstRegularStartOfFile = C.StartOfFile;
258}
259
Rafael Espindola48af1c22014-08-19 18:44:46 +0000260Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindola43358762015-10-31 21:44:42 +0000261 : Binary(Binary::ID_Archive, Source) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000262 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000263 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000264 if (Buffer.startswith(ThinMagic)) {
265 IsThin = true;
266 } else if (Buffer.startswith(Magic)) {
267 IsThin = false;
268 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000269 ec = object_error::invalid_file_type;
270 return;
271 }
272
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000273 // Get the special members.
Kevin Enderby7a969422015-11-05 19:24:56 +0000274 child_iterator I = child_begin(false);
275 if ((ec = I->getError()))
276 return;
277 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000278
Kevin Enderby7a969422015-11-05 19:24:56 +0000279 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000280 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000281 return;
282 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000283 const Child *C = &**I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000284
Kevin Enderby7a969422015-11-05 19:24:56 +0000285 auto Increment = [&]() {
286 ++I;
287 if ((ec = I->getError()))
288 return true;
289 C = &**I;
290 return false;
291 };
292
293 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000294
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000295 // Below is the pattern that is used to figure out the archive format
296 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000297 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000298 // Second member : // (may exist, if it exists, points to the string table)
299 // Note : The string table is used if the filename exceeds 15 characters
300 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000301 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
302 // There is no string table, if the filename exceeds 15 characters or has a
303 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000304 // of the filename that needs to be read after the archive header
305 // COFF archive format
306 // First member : /
307 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000308 // Third member : // (may exist, if it exists, contains the string table)
309 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
310 // even if the string table is empty. However, lib.exe does not in fact
311 // seem to create the third member if there's no member whose filename
312 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000313
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000314 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000315 Format = K_BSD;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000316 // We know that the symbol table is not an external file, so we just assert
317 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000318 SymbolTable = *C->getBuffer();
319 if (Increment())
320 return;
321 setFirstRegular(*C);
322
Rui Ueyama7d099192015-06-09 15:20:42 +0000323 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000324 return;
325 }
326
Rafael Espindola55509922013-07-10 22:07:59 +0000327 if (Name.startswith("#1/")) {
328 Format = K_BSD;
329 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000330 ErrorOr<StringRef> NameOrErr = C->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000331 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000332 if (ec)
333 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000334 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000335 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000336 // We know that the symbol table is not an external file, so we just
337 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000338 SymbolTable = *C->getBuffer();
339 if (Increment())
340 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000341 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000342 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000343 return;
344 }
345
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000346 // MIPS 64-bit ELF archives use a special format of a symbol table.
347 // This format is marked by `ar_name` field equals to "/SYM64/".
348 // For detailed description see page 96 in the following document:
349 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
350
351 bool has64SymTable = false;
352 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000353 // We know that the symbol table is not an external file, so we just assert
354 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000355 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000356 if (Name == "/SYM64/")
357 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000358
Kevin Enderby7a969422015-11-05 19:24:56 +0000359 if (Increment())
360 return;
361 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000362 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000363 return;
364 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000365 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000366 }
367
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000368 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000369 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000370 // The string table is never an external member, so we just assert on the
371 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000372 StringTable = *C->getBuffer();
373 if (Increment())
374 return;
375 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000376 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000377 return;
378 }
379
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000380 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000381 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000382 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 != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000388 ec = object_error::parse_failed;
389 return;
390 }
391
392 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000393 // We know that the symbol table is not an external file, so we just assert
394 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000395 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000396
Kevin Enderby7a969422015-11-05 19:24:56 +0000397 if (Increment())
398 return;
399
400 if (I == E) {
401 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000402 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000403 return;
404 }
405
Kevin Enderby7a969422015-11-05 19:24:56 +0000406 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000407
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000408 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000409 // The string table is never an external member, so we just assert on the
410 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000411 StringTable = *C->getBuffer();
412 if (Increment())
413 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000414 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000415
Kevin Enderby7a969422015-11-05 19:24:56 +0000416 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000417 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000418}
419
Rafael Espindola23a97502014-01-21 16:09:45 +0000420Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000421 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000422 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000423
424 if (SkipInternal)
Rafael Espindola43358762015-10-31 21:44:42 +0000425 return Child(this, FirstRegularData, FirstRegularStartOfFile);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000426
Rafael Espindola48af1c22014-08-19 18:44:46 +0000427 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby7a969422015-11-05 19:24:56 +0000428 std::error_code EC;
429 Child c(this, Loc, &EC);
430 if (EC)
431 return child_iterator(EC);
432 return child_iterator(c);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000433}
434
Rafael Espindola23a97502014-01-21 16:09:45 +0000435Archive::child_iterator Archive::child_end() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000436 return Child(this, nullptr, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000437}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000438
Rafael Espindolaae460022014-06-16 16:08:36 +0000439StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000440 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000441}
442
Rafael Espindolacc86d822015-11-03 01:20:44 +0000443ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000444 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000445 const char *Offsets = Buf;
446 if (Parent->kind() == K_MIPS64)
447 Offsets += sizeof(uint64_t);
448 else
449 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000450 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000451 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000452 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000453 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000454 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000455 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000456 // The SymbolIndex is an index into the ranlib structs that start at
457 // Offsets (the first uint32_t is the number of bytes of the ranlib
458 // structs). The ranlib structs are a pair of uint32_t's the first
459 // being a string table offset and the second being the offset into
460 // the archive of the member that defines the symbol. Which is what
461 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000462 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000463 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000464 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000465 uint32_t MemberCount = read32le(Buf);
466 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000467
Rui Ueyama3206b792015-03-02 21:19:12 +0000468 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000469 if (SymbolIndex >= SymbolCount)
470 return object_error::parse_failed;
471
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000472 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000473 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000474
475 // Get the index of the offset in the file member offset table for this
476 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000477 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000478 // Subtract 1 since OffsetIndex is 1 based.
479 --OffsetIndex;
480
481 if (OffsetIndex >= MemberCount)
482 return object_error::parse_failed;
483
Rui Ueyama3206b792015-03-02 21:19:12 +0000484 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000485 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000486
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000487 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby7a969422015-11-05 19:24:56 +0000488 std::error_code EC;
489 Child C(Parent, Loc, &EC);
490 if (EC)
491 return EC;
492 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000493}
494
495Archive::Symbol Archive::Symbol::getNext() const {
496 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000497 if (Parent->kind() == K_BSD) {
498 // t.StringIndex is an offset from the start of the __.SYMDEF or
499 // "__.SYMDEF SORTED" member into the string table for the ranlib
500 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
501 // offset in the string table for t.SymbolIndex+1 we subtract the
502 // its offset from the start of the string table for t.SymbolIndex
503 // and add the offset of the string table for t.SymbolIndex+1.
504
505 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
506 // which is the number of bytes of ranlib structs that follow. The ranlib
507 // structs are a pair of uint32_t's the first being a string table offset
508 // and the second being the offset into the archive of the member that
509 // define the symbol. After that the next uint32_t is the byte count of
510 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000511 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000512 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000513 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000514 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
515 // don't change the t.StringIndex as we don't want to reference a ranlib
516 // past RanlibCount.
517 if (t.SymbolIndex + 1 < RanlibCount) {
518 const char *Ranlibs = Buf + 4;
519 uint32_t CurRanStrx = 0;
520 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000521 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
522 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000523 t.StringIndex -= CurRanStrx;
524 t.StringIndex += NextRanStrx;
525 }
526 } else {
527 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000528 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000529 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000530 ++t.SymbolIndex;
531 return t;
532}
533
Rafael Espindola23a97502014-01-21 16:09:45 +0000534Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000535 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000536 return symbol_iterator(Symbol(this, 0, 0));
537
Rafael Espindola2b054162015-07-14 01:06:16 +0000538 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000539 if (kind() == K_GNU) {
540 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000541 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000542 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000543 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000544 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000545 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000546 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000547 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
548 // which is the number of bytes of ranlib structs that follow. The ranlib
549 // structs are a pair of uint32_t's the first being a string table offset
550 // and the second being the offset into the archive of the member that
551 // define the symbol. After that the next uint32_t is the byte count of
552 // the string table followed by the string table.
553 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000554 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000555 const char *ranlibs = buf + 4;
556 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000557 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000558 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
559 // Skip the byte count of the string table.
560 buf += sizeof(uint32_t);
561 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000562 } else {
563 uint32_t member_count = 0;
564 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000565 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000566 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000567 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000568 buf += 4 + (symbol_count * 2); // Skip indices.
569 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000570 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000571 return symbol_iterator(Symbol(this, 0, string_start_offset));
572}
573
Rafael Espindola23a97502014-01-21 16:09:45 +0000574Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000575 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
576}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000577
Rui Ueyama407e0972015-05-26 16:20:40 +0000578uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000579 if (!hasSymbolTable())
580 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000581 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000582 if (kind() == K_GNU)
583 return read32be(buf);
584 if (kind() == K_MIPS64)
585 return read64be(buf);
586 if (kind() == K_BSD)
587 return read32le(buf) / 8;
588 uint32_t member_count = 0;
589 member_count = read32le(buf);
590 buf += 4 + (member_count * 4); // Skip offsets.
591 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000592}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000593
594Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000595 Archive::symbol_iterator bs = symbol_begin();
596 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000597
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000598 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000599 StringRef SymName = bs->getName();
600 if (SymName == name) {
601 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
602 // FIXME: Should we really eat the error?
603 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000604 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000605 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000606 }
607 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000608 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000609}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000610
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000611bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }