blob: d50a69951e1236eddeeb4c38742b5ffa837d7d20 [file] [log] [blame]
Michael J. Spencerd3b7b122011-09-27 19:36:55 +00001//===- Archive.cpp - ar File Format implementation --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ArchiveObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Object/Archive.h"
15#include "llvm/ADT/APInt.h"
Rafael Espindola747bc072013-07-09 03:39:35 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Twine.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000018#include "llvm/Support/Endian.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000019#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola4b83cb52015-07-14 22:18:43 +000020#include "llvm/Support/Path.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000021
22using namespace llvm;
23using namespace object;
Rui Ueyama3206b792015-03-02 21:19:12 +000024using namespace llvm::support::endian;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000025
Craig Topperd3a34f82013-07-16 01:17:10 +000026static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000027static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000028
David Blaikiea379b1812011-12-20 02:50:00 +000029void Archive::anchor() { }
30
Rafael Espindola747bc072013-07-09 03:39:35 +000031StringRef ArchiveMemberHeader::getName() const {
32 char EndCond;
33 if (Name[0] == '/' || Name[0] == '#')
34 EndCond = ' ';
35 else
36 EndCond = '/';
37 llvm::StringRef::size_type end =
38 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
39 if (end == llvm::StringRef::npos)
40 end = sizeof(Name);
41 assert(end <= sizeof(Name) && end > 0);
42 // Don't include the EndCond if there is one.
43 return llvm::StringRef(Name, end);
44}
45
Kevin Enderby1c1add42015-10-13 20:48:04 +000046ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +000047 uint32_t Ret;
48 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
Kevin Enderbyda9dd052015-10-21 17:13:20 +000049 return object_error::parse_failed;
Rafael Espindola8e9385e2013-07-09 12:45:11 +000050 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000051}
52
Rafael Espindola8115e1d2013-07-09 12:49:24 +000053sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
54 unsigned Ret;
55 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
56 llvm_unreachable("Access mode is not an octal number.");
57 return static_cast<sys::fs::perms>(Ret);
58}
59
60sys::TimeValue ArchiveMemberHeader::getLastModified() const {
61 unsigned Seconds;
62 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
63 .getAsInteger(10, Seconds))
64 llvm_unreachable("Last modified time not a decimal number.");
65
66 sys::TimeValue Ret;
67 Ret.fromEpochTime(Seconds);
68 return Ret;
69}
70
71unsigned ArchiveMemberHeader::getUID() const {
72 unsigned Ret;
73 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
74 llvm_unreachable("UID time not a decimal number.");
75 return Ret;
76}
77
78unsigned ArchiveMemberHeader::getGID() const {
79 unsigned Ret;
80 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
81 llvm_unreachable("GID time not a decimal number.");
82 return Ret;
83}
84
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 Enderbyda9dd052015-10-21 17:13:20 +000089Archive::Child::Child(const Archive *Parent, const char *Start)
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 Enderbyda9dd052015-10-21 17:13:20 +000097 Size += getRawSize();
Rafael Espindolabe9ab262015-07-22 19:34:26 +000098 Data = StringRef(Start, Size);
99 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000100
Rafael Espindola747bc072013-07-09 03:39:35 +0000101 // Setup StartOfFile and PaddingBytes.
102 StartOfFile = sizeof(ArchiveMemberHeader);
103 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000104 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000105 if (Name.startswith("#1/")) {
106 uint64_t NameSize;
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000107 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
108 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000109 StartOfFile += NameSize;
110 }
111}
112
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000113uint64_t Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000114 if (Parent->IsThin) {
115 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000116 if (Size.getError())
117 return 0;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000118 return Size.get();
119 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000120 return Data.size() - StartOfFile;
121}
122
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000123uint64_t Archive::Child::getRawSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000124 ErrorOr<uint32_t> Size = getHeader()->getSize();
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000125 if (Size.getError())
126 return 0;
Kevin Enderby1c1add42015-10-13 20:48:04 +0000127 return Size.get();
Kevin Enderby13023a12015-01-15 23:19:11 +0000128}
129
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000130bool Archive::Child::isThinMember() const {
131 StringRef Name = getHeader()->getName();
132 return Parent->IsThin && Name != "/" && Name != "//";
133}
134
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000135ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000136 if (!isThinMember())
137 return StringRef(Data.data() + StartOfFile, getSize());
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000138 ErrorOr<StringRef> Name = getName();
139 if (std::error_code EC = Name.getError())
140 return EC;
Rafael Espindolaf662e002015-07-15 21:24:07 +0000141 SmallString<128> FullName = sys::path::parent_path(
142 Parent->getMemoryBufferRef().getBufferIdentifier());
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000143 sys::path::append(FullName, *Name);
144 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
145 if (std::error_code EC = Buf.getError())
146 return EC;
147 Parent->ThinBuffers.push_back(std::move(*Buf));
148 return Parent->ThinBuffers.back()->getBuffer();
149}
150
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000151Archive::Child Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000152 size_t SpaceToSkip = Data.size();
153 // If it's odd, add 1 to make it even.
154 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000155 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000156
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000157 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000158
159 // Check to see if this is past the end of the archive.
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000160 if (NextLoc >= Parent->Data.getBufferEnd())
161 return Child(Parent, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000162
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000163 return Child(Parent, NextLoc);
Rafael Espindola747bc072013-07-09 03:39:35 +0000164}
165
Kevin Enderby13023a12015-01-15 23:19:11 +0000166uint64_t Archive::Child::getChildOffset() const {
167 const char *a = Parent->Data.getBuffer().data();
168 const char *c = Data.data();
169 uint64_t offset = c - a;
170 return offset;
171}
172
Rafael Espindolaae460022014-06-16 16:08:36 +0000173ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000174 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000175 // Check if it's a special name.
176 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000177 if (name.size() == 1) // Linker member.
178 return name;
179 if (name.size() == 2 && name[1] == '/') // String table.
180 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000181 // It's a long name.
182 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000183 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000184 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000185 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000186
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000187 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000188 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000189 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000190 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000191
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000192 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000193 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000194 StringRef::size_type End = StringRef(addr).find('\n');
195 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000196 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000197 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000198 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000199 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000200 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000201 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindolaae460022014-06-16 16:08:36 +0000202 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000203 .rtrim(StringRef("\0", 1));
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000204 }
205 // It's a simple name.
206 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000207 return name.substr(0, name.size() - 1);
208 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000209}
210
Rafael Espindola48af1c22014-08-19 18:44:46 +0000211ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000212 ErrorOr<StringRef> NameOrErr = getName();
213 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000214 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000215 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000216 ErrorOr<StringRef> Buf = getBuffer();
217 if (std::error_code EC = Buf.getError())
218 return EC;
219 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000220}
221
222ErrorOr<std::unique_ptr<Binary>>
223Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000224 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000225 if (std::error_code EC = BuffOrErr.getError())
226 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000227
Rafael Espindola48af1c22014-08-19 18:44:46 +0000228 return createBinary(BuffOrErr.get(), Context);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000229}
230
Rafael Espindola48af1c22014-08-19 18:44:46 +0000231ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000232 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000233 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000234 if (EC)
235 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000236 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000237}
238
Rafael Espindola43358762015-10-31 21:44:42 +0000239void Archive::setFirstRegular(const Child &C) {
240 FirstRegularData = C.Data;
241 FirstRegularStartOfFile = C.StartOfFile;
242}
243
Rafael Espindola48af1c22014-08-19 18:44:46 +0000244Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindola43358762015-10-31 21:44:42 +0000245 : Binary(Binary::ID_Archive, Source) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000246 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000247 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000248 if (Buffer.startswith(ThinMagic)) {
249 IsThin = true;
250 } else if (Buffer.startswith(Magic)) {
251 IsThin = false;
252 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000253 ec = object_error::invalid_file_type;
254 return;
255 }
256
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000257 // Get the special members.
Rafael Espindola23a97502014-01-21 16:09:45 +0000258 child_iterator i = child_begin(false);
259 child_iterator e = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000260
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000261 if (i == e) {
262 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000263 return;
264 }
265
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000266 StringRef Name = i->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000267
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000268 // Below is the pattern that is used to figure out the archive format
269 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000270 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000271 // Second member : // (may exist, if it exists, points to the string table)
272 // Note : The string table is used if the filename exceeds 15 characters
273 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000274 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
275 // There is no string table, if the filename exceeds 15 characters or has a
276 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000277 // of the filename that needs to be read after the archive header
278 // COFF archive format
279 // First member : /
280 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000281 // Third member : // (may exist, if it exists, contains the string table)
282 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
283 // even if the string table is empty. However, lib.exe does not in fact
284 // seem to create the third member if there's no member whose filename
285 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000286
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000287 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000288 Format = K_BSD;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000289 // We know that the symbol table is not an external file, so we just assert
290 // there is no error.
291 SymbolTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000292 ++i;
Rafael Espindola43358762015-10-31 21:44:42 +0000293 setFirstRegular(*i);
Rui Ueyama7d099192015-06-09 15:20:42 +0000294 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000295 return;
296 }
297
Rafael Espindola55509922013-07-10 22:07:59 +0000298 if (Name.startswith("#1/")) {
299 Format = K_BSD;
300 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000301 ErrorOr<StringRef> NameOrErr = i->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000302 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000303 if (ec)
304 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000305 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000306 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000307 // We know that the symbol table is not an external file, so we just
308 // assert there is no error.
309 SymbolTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000310 ++i;
311 }
Rafael Espindola43358762015-10-31 21:44:42 +0000312 setFirstRegular(*i);
Rafael Espindola55509922013-07-10 22:07:59 +0000313 return;
314 }
315
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000316 // MIPS 64-bit ELF archives use a special format of a symbol table.
317 // This format is marked by `ar_name` field equals to "/SYM64/".
318 // For detailed description see page 96 in the following document:
319 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
320
321 bool has64SymTable = false;
322 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000323 // We know that the symbol table is not an external file, so we just assert
324 // there is no error.
325 SymbolTable = *i->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000326 if (Name == "/SYM64/")
327 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000328
329 ++i;
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000330 if (i == e) {
331 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000332 return;
333 }
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000334 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000335 }
336
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000337 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000338 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000339 // The string table is never an external member, so we just assert on the
340 // ErrorOr.
341 StringTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000342 ++i;
Rafael Espindola43358762015-10-31 21:44:42 +0000343 setFirstRegular(*i);
Rui Ueyama7d099192015-06-09 15:20:42 +0000344 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000345 return;
346 }
347
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000348 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000349 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola43358762015-10-31 21:44:42 +0000350 setFirstRegular(*i);
Rui Ueyama7d099192015-06-09 15:20:42 +0000351 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000352 return;
353 }
354
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000355 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000356 ec = object_error::parse_failed;
357 return;
358 }
359
360 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000361 // We know that the symbol table is not an external file, so we just assert
362 // there is no error.
363 SymbolTable = *i->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000364
365 ++i;
366 if (i == e) {
Rafael Espindola43358762015-10-31 21:44:42 +0000367 setFirstRegular(*i);
Rui Ueyama7d099192015-06-09 15:20:42 +0000368 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000369 return;
370 }
371
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000372 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000373
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000374 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000375 // The string table is never an external member, so we just assert on the
376 // ErrorOr.
377 StringTable = *i->getBuffer();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000378 ++i;
379 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000380
Rafael Espindola43358762015-10-31 21:44:42 +0000381 setFirstRegular(*i);
Rui Ueyama7d099192015-06-09 15:20:42 +0000382 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000383}
384
Rafael Espindola23a97502014-01-21 16:09:45 +0000385Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000386 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000387 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000388
389 if (SkipInternal)
Rafael Espindola43358762015-10-31 21:44:42 +0000390 return Child(this, FirstRegularData, FirstRegularStartOfFile);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000391
Rafael Espindola48af1c22014-08-19 18:44:46 +0000392 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000393 Child c(this, Loc);
394 return c;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000395}
396
Rafael Espindola23a97502014-01-21 16:09:45 +0000397Archive::child_iterator Archive::child_end() const {
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000398 return Child(this, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000399}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000400
Rafael Espindolaae460022014-06-16 16:08:36 +0000401StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000402 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000403}
404
Rafael Espindolaae460022014-06-16 16:08:36 +0000405ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000406 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000407 const char *Offsets = Buf;
408 if (Parent->kind() == K_MIPS64)
409 Offsets += sizeof(uint64_t);
410 else
411 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000412 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000413 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000414 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000415 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000416 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000417 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000418 // The SymbolIndex is an index into the ranlib structs that start at
419 // Offsets (the first uint32_t is the number of bytes of the ranlib
420 // structs). The ranlib structs are a pair of uint32_t's the first
421 // being a string table offset and the second being the offset into
422 // the archive of the member that defines the symbol. Which is what
423 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000424 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000425 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000426 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000427 uint32_t MemberCount = read32le(Buf);
428 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000429
Rui Ueyama3206b792015-03-02 21:19:12 +0000430 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000431 if (SymbolIndex >= SymbolCount)
432 return object_error::parse_failed;
433
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000434 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000435 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000436
437 // Get the index of the offset in the file member offset table for this
438 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000439 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000440 // Subtract 1 since OffsetIndex is 1 based.
441 --OffsetIndex;
442
443 if (OffsetIndex >= MemberCount)
444 return object_error::parse_failed;
445
Rui Ueyama3206b792015-03-02 21:19:12 +0000446 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000447 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000448
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000449 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000450 child_iterator Iter(Child(Parent, Loc));
Rafael Espindolaae460022014-06-16 16:08:36 +0000451 return Iter;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000452}
453
454Archive::Symbol Archive::Symbol::getNext() const {
455 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000456 if (Parent->kind() == K_BSD) {
457 // t.StringIndex is an offset from the start of the __.SYMDEF or
458 // "__.SYMDEF SORTED" member into the string table for the ranlib
459 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
460 // offset in the string table for t.SymbolIndex+1 we subtract the
461 // its offset from the start of the string table for t.SymbolIndex
462 // and add the offset of the string table for t.SymbolIndex+1.
463
464 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
465 // which is the number of bytes of ranlib structs that follow. The ranlib
466 // structs are a pair of uint32_t's the first being a string table offset
467 // and the second being the offset into the archive of the member that
468 // define the symbol. After that the next uint32_t is the byte count of
469 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000470 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000471 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000472 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000473 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
474 // don't change the t.StringIndex as we don't want to reference a ranlib
475 // past RanlibCount.
476 if (t.SymbolIndex + 1 < RanlibCount) {
477 const char *Ranlibs = Buf + 4;
478 uint32_t CurRanStrx = 0;
479 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000480 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
481 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000482 t.StringIndex -= CurRanStrx;
483 t.StringIndex += NextRanStrx;
484 }
485 } else {
486 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000487 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000488 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000489 ++t.SymbolIndex;
490 return t;
491}
492
Rafael Espindola23a97502014-01-21 16:09:45 +0000493Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000494 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000495 return symbol_iterator(Symbol(this, 0, 0));
496
Rafael Espindola2b054162015-07-14 01:06:16 +0000497 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000498 if (kind() == K_GNU) {
499 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000500 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000501 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000502 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000503 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000504 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000505 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000506 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
507 // which is the number of bytes of ranlib structs that follow. The ranlib
508 // structs are a pair of uint32_t's the first being a string table offset
509 // and the second being the offset into the archive of the member that
510 // define the symbol. After that the next uint32_t is the byte count of
511 // the string table followed by the string table.
512 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000513 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000514 const char *ranlibs = buf + 4;
515 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000516 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000517 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
518 // Skip the byte count of the string table.
519 buf += sizeof(uint32_t);
520 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000521 } else {
522 uint32_t member_count = 0;
523 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000524 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000525 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000526 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000527 buf += 4 + (symbol_count * 2); // Skip indices.
528 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000529 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000530 return symbol_iterator(Symbol(this, 0, string_start_offset));
531}
532
Rafael Espindola23a97502014-01-21 16:09:45 +0000533Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000534 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
535}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000536
Rui Ueyama407e0972015-05-26 16:20:40 +0000537uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000538 if (!hasSymbolTable())
539 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000540 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000541 if (kind() == K_GNU)
542 return read32be(buf);
543 if (kind() == K_MIPS64)
544 return read64be(buf);
545 if (kind() == K_BSD)
546 return read32le(buf) / 8;
547 uint32_t member_count = 0;
548 member_count = read32le(buf);
549 buf += 4 + (member_count * 4); // Skip offsets.
550 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000551}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000552
553Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000554 Archive::symbol_iterator bs = symbol_begin();
555 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000556
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000557 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000558 StringRef SymName = bs->getName();
559 if (SymName == name) {
560 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
561 // FIXME: Should we really eat the error?
562 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000563 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000564 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000565 }
566 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000567 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000568}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000569
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000570bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }