blob: 15db95123ac3dc7f6d1e24dc708c8ec4a09558b8 [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 Espindola694210c2016-05-02 13:45:06 +0000138ErrorOr<std::string> Archive::Child::getFullName() const {
139 assert(isThinMember());
140 ErrorOr<StringRef> NameOrErr = getName();
141 if (std::error_code EC = NameOrErr.getError())
142 return EC;
143 StringRef Name = *NameOrErr;
144 if (sys::path::is_absolute(Name))
145 return Name;
146
147 SmallString<128> FullName = sys::path::parent_path(
148 Parent->getMemoryBufferRef().getBufferIdentifier());
149 sys::path::append(FullName, Name);
150 return StringRef(FullName);
151}
152
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000153ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000154 if (!isThinMember()) {
155 ErrorOr<uint32_t> Size = getSize();
156 if (std::error_code EC = Size.getError())
157 return EC;
158 return StringRef(Data.data() + StartOfFile, Size.get());
159 }
Rafael Espindola694210c2016-05-02 13:45:06 +0000160 ErrorOr<std::string> FullNameOrEr = getFullName();
161 if (std::error_code EC = FullNameOrEr.getError())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000162 return EC;
Rafael Espindola694210c2016-05-02 13:45:06 +0000163 const std::string &FullName = *FullNameOrEr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000164 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
165 if (std::error_code EC = Buf.getError())
166 return EC;
167 Parent->ThinBuffers.push_back(std::move(*Buf));
168 return Parent->ThinBuffers.back()->getBuffer();
169}
170
Kevin Enderby7a969422015-11-05 19:24:56 +0000171ErrorOr<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000172 size_t SpaceToSkip = Data.size();
173 // If it's odd, add 1 to make it even.
174 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000175 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000176
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000177 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000178
Kevin Enderby7a969422015-11-05 19:24:56 +0000179 // Check to see if this is at the end of the archive.
180 if (NextLoc == Parent->Data.getBufferEnd())
181 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000182
Kevin Enderby7a969422015-11-05 19:24:56 +0000183 // Check to see if this is past the end of the archive.
184 if (NextLoc > Parent->Data.getBufferEnd())
185 return object_error::parse_failed;
186
187 std::error_code EC;
188 Child Ret(Parent, NextLoc, &EC);
189 if (EC)
190 return EC;
191 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000192}
193
Kevin Enderby13023a12015-01-15 23:19:11 +0000194uint64_t Archive::Child::getChildOffset() const {
195 const char *a = Parent->Data.getBuffer().data();
196 const char *c = Data.data();
197 uint64_t offset = c - a;
198 return offset;
199}
200
Rafael Espindolaae460022014-06-16 16:08:36 +0000201ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000202 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000203 // Check if it's a special name.
204 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000205 if (name.size() == 1) // Linker member.
206 return name;
207 if (name.size() == 2 && name[1] == '/') // String table.
208 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000209 // It's a long name.
210 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000211 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000212 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000213 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000214
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000215 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000216 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000217 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000218 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000219
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000220 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000221 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000222 StringRef::size_type End = StringRef(addr).find('\n');
223 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000224 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000225 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000226 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000227 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000228 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000229 llvm_unreachable("Long name length is not an ingeter");
Vedant Kumar98372e32016-02-16 02:06:01 +0000230 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000231 } else {
232 // It is not a long name so trim the blanks at the end of the name.
233 if (name[name.size() - 1] != '/') {
234 return name.rtrim(' ');
235 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000236 }
237 // It's a simple name.
238 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000239 return name.substr(0, name.size() - 1);
240 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000241}
242
Rafael Espindola48af1c22014-08-19 18:44:46 +0000243ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000244 ErrorOr<StringRef> NameOrErr = getName();
245 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000246 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000247 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000248 ErrorOr<StringRef> Buf = getBuffer();
249 if (std::error_code EC = Buf.getError())
250 return EC;
251 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000252}
253
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000254Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000255Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000256 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000257 if (std::error_code EC = BuffOrErr.getError())
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000258 return errorCodeToError(EC);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000259
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000260 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
261 if (BinaryOrErr)
262 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000263 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000264}
265
Rafael Espindola48af1c22014-08-19 18:44:46 +0000266ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000267 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000268 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000269 if (EC)
270 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000271 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000272}
273
Rafael Espindola43358762015-10-31 21:44:42 +0000274void Archive::setFirstRegular(const Child &C) {
275 FirstRegularData = C.Data;
276 FirstRegularStartOfFile = C.StartOfFile;
277}
278
Rafael Espindola48af1c22014-08-19 18:44:46 +0000279Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindola43358762015-10-31 21:44:42 +0000280 : Binary(Binary::ID_Archive, Source) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000281 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000282 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000283 if (Buffer.startswith(ThinMagic)) {
284 IsThin = true;
285 } else if (Buffer.startswith(Magic)) {
286 IsThin = false;
287 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000288 ec = object_error::invalid_file_type;
289 return;
290 }
291
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000292 // Get the special members.
Kevin Enderby7a969422015-11-05 19:24:56 +0000293 child_iterator I = child_begin(false);
294 if ((ec = I->getError()))
295 return;
296 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000297
Kevin Enderby7a969422015-11-05 19:24:56 +0000298 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000299 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000300 return;
301 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000302 const Child *C = &**I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000303
Kevin Enderby7a969422015-11-05 19:24:56 +0000304 auto Increment = [&]() {
305 ++I;
306 if ((ec = I->getError()))
307 return true;
308 C = &**I;
309 return false;
310 };
311
312 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000313
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000314 // Below is the pattern that is used to figure out the archive format
315 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000316 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000317 // Second member : // (may exist, if it exists, points to the string table)
318 // Note : The string table is used if the filename exceeds 15 characters
319 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000320 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
321 // There is no string table, if the filename exceeds 15 characters or has a
322 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000323 // of the filename that needs to be read after the archive header
324 // COFF archive format
325 // First member : /
326 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000327 // Third member : // (may exist, if it exists, contains the string table)
328 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
329 // even if the string table is empty. However, lib.exe does not in fact
330 // seem to create the third member if there's no member whose filename
331 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000332
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000333 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
334 if (Name == "__.SYMDEF")
335 Format = K_BSD;
336 else // Name == "__.SYMDEF_64"
337 Format = K_DARWIN64;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000338 // We know that the symbol table is not an external file, so we just assert
339 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000340 SymbolTable = *C->getBuffer();
341 if (Increment())
342 return;
343 setFirstRegular(*C);
344
Rui Ueyama7d099192015-06-09 15:20:42 +0000345 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000346 return;
347 }
348
Rafael Espindola55509922013-07-10 22:07:59 +0000349 if (Name.startswith("#1/")) {
350 Format = K_BSD;
351 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000352 ErrorOr<StringRef> NameOrErr = C->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000353 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000354 if (ec)
355 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000356 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000357 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000358 // We know that the symbol table is not an external file, so we just
359 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000360 SymbolTable = *C->getBuffer();
361 if (Increment())
362 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000363 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000364 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
365 Format = K_DARWIN64;
366 // We know that the symbol table is not an external file, so we just
367 // assert there is no error.
368 SymbolTable = *C->getBuffer();
369 if (Increment())
370 return;
371 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000372 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000373 return;
374 }
375
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000376 // MIPS 64-bit ELF archives use a special format of a symbol table.
377 // This format is marked by `ar_name` field equals to "/SYM64/".
378 // For detailed description see page 96 in the following document:
379 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
380
381 bool has64SymTable = false;
382 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000383 // We know that the symbol table is not an external file, so we just assert
384 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000385 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000386 if (Name == "/SYM64/")
387 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000388
Kevin Enderby7a969422015-11-05 19:24:56 +0000389 if (Increment())
390 return;
391 if (I == E) {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000392 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000393 return;
394 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000395 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000396 }
397
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000398 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000399 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000400 // The string table is never an external member, so we just assert on the
401 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000402 StringTable = *C->getBuffer();
403 if (Increment())
404 return;
405 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000406 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000407 return;
408 }
409
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000410 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000411 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000412 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000413 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000414 return;
415 }
416
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000417 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000418 ec = object_error::parse_failed;
419 return;
420 }
421
422 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000423 // We know that the symbol table is not an external file, so we just assert
424 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000425 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000426
Kevin Enderby7a969422015-11-05 19:24:56 +0000427 if (Increment())
428 return;
429
430 if (I == E) {
431 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000432 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000433 return;
434 }
435
Kevin Enderby7a969422015-11-05 19:24:56 +0000436 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000437
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000438 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000439 // The string table is never an external member, so we just assert on the
440 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000441 StringTable = *C->getBuffer();
442 if (Increment())
443 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000444 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000445
Kevin Enderby7a969422015-11-05 19:24:56 +0000446 setFirstRegular(*C);
Rui Ueyama7d099192015-06-09 15:20:42 +0000447 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000448}
449
Rafael Espindola23a97502014-01-21 16:09:45 +0000450Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000451 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000452 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000453
454 if (SkipInternal)
Rafael Espindola43358762015-10-31 21:44:42 +0000455 return Child(this, FirstRegularData, FirstRegularStartOfFile);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000456
Rafael Espindola48af1c22014-08-19 18:44:46 +0000457 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby7a969422015-11-05 19:24:56 +0000458 std::error_code EC;
459 Child c(this, Loc, &EC);
460 if (EC)
461 return child_iterator(EC);
462 return child_iterator(c);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000463}
464
Rafael Espindola23a97502014-01-21 16:09:45 +0000465Archive::child_iterator Archive::child_end() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000466 return Child(this, nullptr, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000467}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000468
Rafael Espindolaae460022014-06-16 16:08:36 +0000469StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000470 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000471}
472
Rafael Espindolacc86d822015-11-03 01:20:44 +0000473ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000474 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000475 const char *Offsets = Buf;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000476 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000477 Offsets += sizeof(uint64_t);
478 else
479 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000480 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000481 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000482 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000483 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000484 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000485 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000486 // The SymbolIndex is an index into the ranlib structs that start at
487 // Offsets (the first uint32_t is the number of bytes of the ranlib
488 // structs). The ranlib structs are a pair of uint32_t's the first
489 // being a string table offset and the second being the offset into
490 // the archive of the member that defines the symbol. Which is what
491 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000492 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000493 } else if (Parent->kind() == K_DARWIN64) {
494 // The SymbolIndex is an index into the ranlib_64 structs that start at
495 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
496 // structs). The ranlib_64 structs are a pair of uint64_t's the first
497 // being a string table offset and the second being the offset into
498 // the archive of the member that defines the symbol. Which is what
499 // is needed here.
500 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000501 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000502 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000503 uint32_t MemberCount = read32le(Buf);
504 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000505
Rui Ueyama3206b792015-03-02 21:19:12 +0000506 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000507 if (SymbolIndex >= SymbolCount)
508 return object_error::parse_failed;
509
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000510 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000511 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000512
513 // Get the index of the offset in the file member offset table for this
514 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000515 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000516 // Subtract 1 since OffsetIndex is 1 based.
517 --OffsetIndex;
518
519 if (OffsetIndex >= MemberCount)
520 return object_error::parse_failed;
521
Rui Ueyama3206b792015-03-02 21:19:12 +0000522 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000523 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000524
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000525 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby7a969422015-11-05 19:24:56 +0000526 std::error_code EC;
527 Child C(Parent, Loc, &EC);
528 if (EC)
529 return EC;
530 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000531}
532
533Archive::Symbol Archive::Symbol::getNext() const {
534 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000535 if (Parent->kind() == K_BSD) {
536 // t.StringIndex is an offset from the start of the __.SYMDEF or
537 // "__.SYMDEF SORTED" member into the string table for the ranlib
538 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
539 // offset in the string table for t.SymbolIndex+1 we subtract the
540 // its offset from the start of the string table for t.SymbolIndex
541 // and add the offset of the string table for t.SymbolIndex+1.
542
543 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
544 // which is the number of bytes of ranlib structs that follow. The ranlib
545 // structs are a pair of uint32_t's the first being a string table offset
546 // and the second being the offset into the archive of the member that
547 // define the symbol. After that the next uint32_t is the byte count of
548 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000549 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000550 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000551 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000552 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
553 // don't change the t.StringIndex as we don't want to reference a ranlib
554 // past RanlibCount.
555 if (t.SymbolIndex + 1 < RanlibCount) {
556 const char *Ranlibs = Buf + 4;
557 uint32_t CurRanStrx = 0;
558 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000559 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
560 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000561 t.StringIndex -= CurRanStrx;
562 t.StringIndex += NextRanStrx;
563 }
564 } else {
565 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000566 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000567 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000568 ++t.SymbolIndex;
569 return t;
570}
571
Rafael Espindola23a97502014-01-21 16:09:45 +0000572Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000573 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000574 return symbol_iterator(Symbol(this, 0, 0));
575
Rafael Espindola2b054162015-07-14 01:06:16 +0000576 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000577 if (kind() == K_GNU) {
578 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000579 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000580 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000581 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000582 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000583 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000584 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000585 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
586 // which is the number of bytes of ranlib structs that follow. The ranlib
587 // structs are a pair of uint32_t's the first being a string table offset
588 // and the second being the offset into the archive of the member that
589 // define the symbol. After that the next uint32_t is the byte count of
590 // the string table followed by the string table.
591 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000592 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000593 const char *ranlibs = buf + 4;
594 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000595 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000596 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
597 // Skip the byte count of the string table.
598 buf += sizeof(uint32_t);
599 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000600 } else if (kind() == K_DARWIN64) {
601 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
602 // which is the number of bytes of ranlib_64 structs that follow. The
603 // ranlib_64 structs are a pair of uint64_t's the first being a string
604 // table offset and the second being the offset into the archive of the
605 // member that define the symbol. After that the next uint64_t is the byte
606 // count of the string table followed by the string table.
607 uint64_t ranlib_count = 0;
608 ranlib_count = read64le(buf) / 16;
609 const char *ranlibs = buf + 8;
610 uint64_t ran_strx = 0;
611 ran_strx = read64le(ranlibs);
612 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
613 // Skip the byte count of the string table.
614 buf += sizeof(uint64_t);
615 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000616 } else {
617 uint32_t member_count = 0;
618 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000619 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000620 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000621 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000622 buf += 4 + (symbol_count * 2); // Skip indices.
623 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000624 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000625 return symbol_iterator(Symbol(this, 0, string_start_offset));
626}
627
Rafael Espindola23a97502014-01-21 16:09:45 +0000628Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000629 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
630}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000631
Rui Ueyama407e0972015-05-26 16:20:40 +0000632uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000633 if (!hasSymbolTable())
634 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000635 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000636 if (kind() == K_GNU)
637 return read32be(buf);
638 if (kind() == K_MIPS64)
639 return read64be(buf);
640 if (kind() == K_BSD)
641 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000642 if (kind() == K_DARWIN64)
643 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000644 uint32_t member_count = 0;
645 member_count = read32le(buf);
646 buf += 4 + (member_count * 4); // Skip offsets.
647 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000648}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000649
650Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000651 Archive::symbol_iterator bs = symbol_begin();
652 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000653
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000654 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000655 StringRef SymName = bs->getName();
656 if (SymName == name) {
657 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
658 // FIXME: Should we really eat the error?
659 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000660 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000661 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000662 }
663 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000664 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000665}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000666
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000667bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }