blob: 47e497ac6ff6f11d159e4420b4d1253f334c15bd [file] [log] [blame]
Michael J. Spencerd3b7b122011-09-27 19:36:55 +00001//===- Archive.cpp - ar File Format implementation --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ArchiveObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Object/Archive.h"
Rafael Espindola747bc072013-07-09 03:39:35 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000017#include "llvm/Support/Endian.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000018#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola4b83cb52015-07-14 22:18:43 +000019#include "llvm/Support/Path.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000020
21using namespace llvm;
22using namespace object;
Rui Ueyama3206b792015-03-02 21:19:12 +000023using namespace llvm::support::endian;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000024
Craig Topperd3a34f82013-07-16 01:17:10 +000025static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000026static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000027
David Blaikiea379b1812011-12-20 02:50:00 +000028void Archive::anchor() { }
29
Rafael Espindola747bc072013-07-09 03:39:35 +000030StringRef ArchiveMemberHeader::getName() const {
31 char EndCond;
32 if (Name[0] == '/' || Name[0] == '#')
33 EndCond = ' ';
34 else
35 EndCond = '/';
36 llvm::StringRef::size_type end =
37 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
38 if (end == llvm::StringRef::npos)
39 end = sizeof(Name);
40 assert(end <= sizeof(Name) && end > 0);
41 // Don't include the EndCond if there is one.
42 return llvm::StringRef(Name, end);
43}
44
Kevin Enderby1c1add42015-10-13 20:48:04 +000045ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +000046 uint32_t Ret;
47 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
Kevin Enderby7a969422015-11-05 19:24:56 +000048 return object_error::parse_failed; // Size is not a decimal number.
Rafael Espindola8e9385e2013-07-09 12:45:11 +000049 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000050}
51
Rafael Espindola8115e1d2013-07-09 12:49:24 +000052sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
53 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000054 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(' ').getAsInteger(8, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000055 llvm_unreachable("Access mode is not an octal number.");
56 return static_cast<sys::fs::perms>(Ret);
57}
58
59sys::TimeValue ArchiveMemberHeader::getLastModified() const {
60 unsigned Seconds;
Vedant Kumar98372e32016-02-16 02:06:01 +000061 if (StringRef(LastModified, sizeof(LastModified)).rtrim(' ')
Rafael Espindola8115e1d2013-07-09 12:49:24 +000062 .getAsInteger(10, Seconds))
63 llvm_unreachable("Last modified time not a decimal number.");
64
65 sys::TimeValue Ret;
66 Ret.fromEpochTime(Seconds);
67 return Ret;
68}
69
70unsigned ArchiveMemberHeader::getUID() const {
71 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000072 if (StringRef(UID, sizeof(UID)).rtrim(' ').getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000073 llvm_unreachable("UID time not a decimal number.");
74 return Ret;
75}
76
77unsigned ArchiveMemberHeader::getGID() const {
78 unsigned Ret;
Vedant Kumar98372e32016-02-16 02:06:01 +000079 if (StringRef(GID, sizeof(GID)).rtrim(' ').getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +000080 llvm_unreachable("GID time not a decimal number.");
81 return Ret;
82}
83
Rafael Espindola43358762015-10-31 21:44:42 +000084Archive::Child::Child(const Archive *Parent, StringRef Data,
85 uint16_t StartOfFile)
86 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
87
Kevin Enderby7a969422015-11-05 19:24:56 +000088Archive::Child::Child(const Archive *Parent, const char *Start,
89 std::error_code *EC)
Rafael Espindola0f3de642013-07-09 05:26:25 +000090 : Parent(Parent) {
91 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000092 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000093
Rafael Espindola9d102062014-12-16 01:43:41 +000094 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +000095 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +000096 if (!isThinMember()) {
Kevin Enderby7a969422015-11-05 19:24:56 +000097 ErrorOr<uint64_t> MemberSize = getRawSize();
98 if ((*EC = MemberSize.getError()))
99 return;
100 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000101 Data = StringRef(Start, Size);
102 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000103
Rafael Espindola747bc072013-07-09 03:39:35 +0000104 // Setup StartOfFile and PaddingBytes.
105 StartOfFile = sizeof(ArchiveMemberHeader);
106 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000107 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000108 if (Name.startswith("#1/")) {
109 uint64_t NameSize;
Vedant Kumar98372e32016-02-16 02:06:01 +0000110 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000111 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000112 StartOfFile += NameSize;
113 }
114}
115
Kevin Enderby7a969422015-11-05 19:24:56 +0000116ErrorOr<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000117 if (Parent->IsThin) {
118 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000119 if (std::error_code EC = Size.getError())
120 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000121 return Size.get();
122 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000123 return Data.size() - StartOfFile;
124}
125
Kevin Enderby7a969422015-11-05 19:24:56 +0000126ErrorOr<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000127 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderby7a969422015-11-05 19:24:56 +0000128 if (std::error_code EC = Size.getError())
129 return EC;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000130 return Size.get();
Kevin Enderby13023a12015-01-15 23:19:11 +0000131}
132
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000133bool Archive::Child::isThinMember() const {
134 StringRef Name = getHeader()->getName();
135 return Parent->IsThin && Name != "/" && Name != "//";
136}
137
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000138ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000139 if (!isThinMember()) {
140 ErrorOr<uint32_t> Size = getSize();
141 if (std::error_code EC = Size.getError())
142 return EC;
143 return StringRef(Data.data() + StartOfFile, Size.get());
144 }
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000145 ErrorOr<StringRef> Name = getName();
146 if (std::error_code EC = Name.getError())
147 return EC;
Peter Collingbournef84646c2016-03-31 22:08:31 +0000148 SmallString<128> FullName;
149 if (sys::path::is_absolute(*Name))
150 FullName = *Name;
151 else {
152 FullName = sys::path::parent_path(
153 Parent->getMemoryBufferRef().getBufferIdentifier());
154 sys::path::append(FullName, *Name);
155 }
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000156 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
157 if (std::error_code EC = Buf.getError())
158 return EC;
159 Parent->ThinBuffers.push_back(std::move(*Buf));
160 return Parent->ThinBuffers.back()->getBuffer();
161}
162
Kevin Enderby7a969422015-11-05 19:24:56 +0000163ErrorOr<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000164 size_t SpaceToSkip = Data.size();
165 // If it's odd, add 1 to make it even.
166 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000167 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000168
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000169 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000170
Kevin Enderby7a969422015-11-05 19:24:56 +0000171 // Check to see if this is at the end of the archive.
172 if (NextLoc == Parent->Data.getBufferEnd())
173 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000174
Kevin Enderby7a969422015-11-05 19:24:56 +0000175 // Check to see if this is past the end of the archive.
176 if (NextLoc > Parent->Data.getBufferEnd())
177 return object_error::parse_failed;
178
179 std::error_code EC;
180 Child Ret(Parent, NextLoc, &EC);
181 if (EC)
182 return EC;
183 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000184}
185
Kevin Enderby13023a12015-01-15 23:19:11 +0000186uint64_t Archive::Child::getChildOffset() const {
187 const char *a = Parent->Data.getBuffer().data();
188 const char *c = Data.data();
189 uint64_t offset = c - a;
190 return offset;
191}
192
Rafael Espindolaae460022014-06-16 16:08:36 +0000193ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000194 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000195 // Check if it's a special name.
196 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000197 if (name.size() == 1) // Linker member.
198 return name;
199 if (name.size() == 2 && name[1] == '/') // String table.
200 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000201 // It's a long name.
202 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000203 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000204 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000205 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000206
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000207 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000208 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000209 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000210 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000211
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000212 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000213 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000214 StringRef::size_type End = StringRef(addr).find('\n');
215 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000216 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000217 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000218 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000219 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000220 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000221 llvm_unreachable("Long name length is not an ingeter");
Vedant Kumar98372e32016-02-16 02:06:01 +0000222 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000223 }
224 // It's a simple name.
225 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000226 return name.substr(0, name.size() - 1);
227 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000228}
229
Rafael Espindola48af1c22014-08-19 18:44:46 +0000230ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000231 ErrorOr<StringRef> NameOrErr = getName();
232 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000233 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000234 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000235 ErrorOr<StringRef> Buf = getBuffer();
236 if (std::error_code EC = Buf.getError())
237 return EC;
238 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000239}
240
241ErrorOr<std::unique_ptr<Binary>>
242Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000243 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000244 if (std::error_code EC = BuffOrErr.getError())
245 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000246
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000247 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
248 if (BinaryOrErr)
249 return std::move(*BinaryOrErr);
250 return errorToErrorCode(BinaryOrErr.takeError());
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000251}
252
Rafael Espindola48af1c22014-08-19 18:44:46 +0000253ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000254 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000255 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000256 if (EC)
257 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000258 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000259}
260
Rafael Espindola43358762015-10-31 21:44:42 +0000261void Archive::setFirstRegular(const Child &C) {
262 FirstRegularData = C.Data;
263 FirstRegularStartOfFile = C.StartOfFile;
264}
265
Rafael Espindola48af1c22014-08-19 18:44:46 +0000266Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindola43358762015-10-31 21:44:42 +0000267 : Binary(Binary::ID_Archive, Source) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000268 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000269 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000270 if (Buffer.startswith(ThinMagic)) {
271 IsThin = true;
272 } else if (Buffer.startswith(Magic)) {
273 IsThin = false;
274 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000275 ec = object_error::invalid_file_type;
276 return;
277 }
278
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000279 // Get the special members.
Kevin Enderby7a969422015-11-05 19:24:56 +0000280 child_iterator I = child_begin(false);
281 if ((ec = I->getError()))
282 return;
283 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000284
Kevin Enderby7a969422015-11-05 19:24:56 +0000285 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000286 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000287 return;
288 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000289 const Child *C = &**I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000290
Kevin Enderby7a969422015-11-05 19:24:56 +0000291 auto Increment = [&]() {
292 ++I;
293 if ((ec = I->getError()))
294 return true;
295 C = &**I;
296 return false;
297 };
298
299 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000300
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000301 // Below is the pattern that is used to figure out the archive format
302 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000303 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000304 // Second member : // (may exist, if it exists, points to the string table)
305 // Note : The string table is used if the filename exceeds 15 characters
306 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000307 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
308 // There is no string table, if the filename exceeds 15 characters or has a
309 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000310 // of the filename that needs to be read after the archive header
311 // COFF archive format
312 // First member : /
313 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000314 // Third member : // (may exist, if it exists, contains the string table)
315 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
316 // even if the string table is empty. However, lib.exe does not in fact
317 // seem to create the third member if there's no member whose filename
318 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000319
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000320 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000321 Format = K_BSD;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000322 // We know that the symbol table is not an external file, so we just assert
323 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000324 SymbolTable = *C->getBuffer();
325 if (Increment())
326 return;
327 setFirstRegular(*C);
328
Rui Ueyama7d099192015-06-09 15:20:42 +0000329 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000330 return;
331 }
332
Rafael Espindola55509922013-07-10 22:07:59 +0000333 if (Name.startswith("#1/")) {
334 Format = K_BSD;
335 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000336 ErrorOr<StringRef> NameOrErr = C->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000337 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000338 if (ec)
339 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000340 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000341 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000342 // We know that the symbol table is not an external file, so we just
343 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000344 SymbolTable = *C->getBuffer();
345 if (Increment())
346 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000347 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000348 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000349 return;
350 }
351
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000352 // MIPS 64-bit ELF archives use a special format of a symbol table.
353 // This format is marked by `ar_name` field equals to "/SYM64/".
354 // For detailed description see page 96 in the following document:
355 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
356
357 bool has64SymTable = false;
358 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000359 // We know that the symbol table is not an external file, so we just assert
360 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000361 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000362 if (Name == "/SYM64/")
363 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000364
Kevin Enderby7a969422015-11-05 19:24:56 +0000365 if (Increment())
366 return;
367 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000368 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000369 return;
370 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000371 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000372 }
373
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000374 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000375 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000376 // The string table is never an external member, so we just assert on the
377 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000378 StringTable = *C->getBuffer();
379 if (Increment())
380 return;
381 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000382 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000383 return;
384 }
385
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000386 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000387 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000388 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000389 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000390 return;
391 }
392
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000393 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000394 ec = object_error::parse_failed;
395 return;
396 }
397
398 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000399 // We know that the symbol table is not an external file, so we just assert
400 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000401 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000402
Kevin Enderby7a969422015-11-05 19:24:56 +0000403 if (Increment())
404 return;
405
406 if (I == E) {
407 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000408 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000409 return;
410 }
411
Kevin Enderby7a969422015-11-05 19:24:56 +0000412 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000413
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000414 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000415 // The string table is never an external member, so we just assert on the
416 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000417 StringTable = *C->getBuffer();
418 if (Increment())
419 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000420 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000421
Kevin Enderby7a969422015-11-05 19:24:56 +0000422 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000423 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000424}
425
Rafael Espindola23a97502014-01-21 16:09:45 +0000426Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000427 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000428 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000429
430 if (SkipInternal)
Rafael Espindola43358762015-10-31 21:44:42 +0000431 return Child(this, FirstRegularData, FirstRegularStartOfFile);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000432
Rafael Espindola48af1c22014-08-19 18:44:46 +0000433 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby7a969422015-11-05 19:24:56 +0000434 std::error_code EC;
435 Child c(this, Loc, &EC);
436 if (EC)
437 return child_iterator(EC);
438 return child_iterator(c);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000439}
440
Rafael Espindola23a97502014-01-21 16:09:45 +0000441Archive::child_iterator Archive::child_end() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000442 return Child(this, nullptr, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000443}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000444
Rafael Espindolaae460022014-06-16 16:08:36 +0000445StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000446 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000447}
448
Rafael Espindolacc86d822015-11-03 01:20:44 +0000449ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000450 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000451 const char *Offsets = Buf;
452 if (Parent->kind() == K_MIPS64)
453 Offsets += sizeof(uint64_t);
454 else
455 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000456 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000457 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000458 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000459 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000460 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000461 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000462 // The SymbolIndex is an index into the ranlib structs that start at
463 // Offsets (the first uint32_t is the number of bytes of the ranlib
464 // structs). The ranlib structs are a pair of uint32_t's the first
465 // being a string table offset and the second being the offset into
466 // the archive of the member that defines the symbol. Which is what
467 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000468 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000469 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000470 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000471 uint32_t MemberCount = read32le(Buf);
472 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000473
Rui Ueyama3206b792015-03-02 21:19:12 +0000474 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000475 if (SymbolIndex >= SymbolCount)
476 return object_error::parse_failed;
477
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000478 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000479 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000480
481 // Get the index of the offset in the file member offset table for this
482 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000483 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000484 // Subtract 1 since OffsetIndex is 1 based.
485 --OffsetIndex;
486
487 if (OffsetIndex >= MemberCount)
488 return object_error::parse_failed;
489
Rui Ueyama3206b792015-03-02 21:19:12 +0000490 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000491 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000492
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000493 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby7a969422015-11-05 19:24:56 +0000494 std::error_code EC;
495 Child C(Parent, Loc, &EC);
496 if (EC)
497 return EC;
498 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000499}
500
501Archive::Symbol Archive::Symbol::getNext() const {
502 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000503 if (Parent->kind() == K_BSD) {
504 // t.StringIndex is an offset from the start of the __.SYMDEF or
505 // "__.SYMDEF SORTED" member into the string table for the ranlib
506 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
507 // offset in the string table for t.SymbolIndex+1 we subtract the
508 // its offset from the start of the string table for t.SymbolIndex
509 // and add the offset of the string table for t.SymbolIndex+1.
510
511 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
512 // which is the number of bytes of ranlib structs that follow. The ranlib
513 // structs are a pair of uint32_t's the first being a string table offset
514 // and the second being the offset into the archive of the member that
515 // define the symbol. After that the next uint32_t is the byte count of
516 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000517 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000518 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000519 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000520 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
521 // don't change the t.StringIndex as we don't want to reference a ranlib
522 // past RanlibCount.
523 if (t.SymbolIndex + 1 < RanlibCount) {
524 const char *Ranlibs = Buf + 4;
525 uint32_t CurRanStrx = 0;
526 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000527 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
528 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000529 t.StringIndex -= CurRanStrx;
530 t.StringIndex += NextRanStrx;
531 }
532 } else {
533 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000534 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000535 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000536 ++t.SymbolIndex;
537 return t;
538}
539
Rafael Espindola23a97502014-01-21 16:09:45 +0000540Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000541 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000542 return symbol_iterator(Symbol(this, 0, 0));
543
Rafael Espindola2b054162015-07-14 01:06:16 +0000544 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000545 if (kind() == K_GNU) {
546 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000547 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000548 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000549 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000550 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000551 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000552 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000553 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
554 // which is the number of bytes of ranlib structs that follow. The ranlib
555 // structs are a pair of uint32_t's the first being a string table offset
556 // and the second being the offset into the archive of the member that
557 // define the symbol. After that the next uint32_t is the byte count of
558 // the string table followed by the string table.
559 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000560 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000561 const char *ranlibs = buf + 4;
562 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000563 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000564 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
565 // Skip the byte count of the string table.
566 buf += sizeof(uint32_t);
567 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000568 } else {
569 uint32_t member_count = 0;
570 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000571 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000572 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000573 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000574 buf += 4 + (symbol_count * 2); // Skip indices.
575 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000576 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000577 return symbol_iterator(Symbol(this, 0, string_start_offset));
578}
579
Rafael Espindola23a97502014-01-21 16:09:45 +0000580Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000581 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
582}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000583
Rui Ueyama407e0972015-05-26 16:20:40 +0000584uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000585 if (!hasSymbolTable())
586 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000587 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000588 if (kind() == K_GNU)
589 return read32be(buf);
590 if (kind() == K_MIPS64)
591 return read64be(buf);
592 if (kind() == K_BSD)
593 return read32le(buf) / 8;
594 uint32_t member_count = 0;
595 member_count = read32le(buf);
596 buf += 4 + (member_count * 4); // Skip offsets.
597 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000598}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000599
600Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000601 Archive::symbol_iterator bs = symbol_begin();
602 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000603
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000604 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000605 StringRef SymName = bs->getName();
606 if (SymName == name) {
607 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
608 // FIXME: Should we really eat the error?
609 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000610 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000611 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000612 }
613 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000614 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000615}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000616
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000617bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }