blob: 667732baa279ebcfcfe668a54c33a6969496f5fa [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 Enderby1c1add42015-10-13 20:48:04 +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 Espindola0f3de642013-07-09 05:26:25 +000085Archive::Child::Child(const Archive *Parent, const char *Start)
86 : Parent(Parent) {
87 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000088 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000089
Rafael Espindola9d102062014-12-16 01:43:41 +000090 uint64_t Size = sizeof(ArchiveMemberHeader);
Rafael Espindola9d102062014-12-16 01:43:41 +000091 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +000092 if (!isThinMember()) {
93 Size += getRawSize();
94 Data = StringRef(Start, Size);
95 }
Rafael Espindola0f3de642013-07-09 05:26:25 +000096
Rafael Espindola747bc072013-07-09 03:39:35 +000097 // Setup StartOfFile and PaddingBytes.
98 StartOfFile = sizeof(ArchiveMemberHeader);
99 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000100 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000101 if (Name.startswith("#1/")) {
102 uint64_t NameSize;
103 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
104 llvm_unreachable("Long name length is not an integer");
105 StartOfFile += NameSize;
106 }
107}
108
Rafael Espindola9d102062014-12-16 01:43:41 +0000109uint64_t Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000110 if (Parent->IsThin) {
111 ErrorOr<uint32_t> Size = getHeader()->getSize();
112 if (Size.getError())
113 return 0;
114 return Size.get();
115 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000116 return Data.size() - StartOfFile;
117}
118
Kevin Enderby13023a12015-01-15 23:19:11 +0000119uint64_t Archive::Child::getRawSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000120 ErrorOr<uint32_t> Size = getHeader()->getSize();
121 if (Size.getError())
122 return 0;
123 return Size.get();
Kevin Enderby13023a12015-01-15 23:19:11 +0000124}
125
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000126bool Archive::Child::isThinMember() const {
127 StringRef Name = getHeader()->getName();
128 return Parent->IsThin && Name != "/" && Name != "//";
129}
130
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000131ErrorOr<StringRef> Archive::Child::getBuffer() const {
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000132 if (!isThinMember())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000133 return StringRef(Data.data() + StartOfFile, getSize());
134 ErrorOr<StringRef> Name = getName();
135 if (std::error_code EC = Name.getError())
136 return EC;
Rafael Espindolaf662e002015-07-15 21:24:07 +0000137 SmallString<128> FullName = sys::path::parent_path(
138 Parent->getMemoryBufferRef().getBufferIdentifier());
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000139 sys::path::append(FullName, *Name);
140 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
141 if (std::error_code EC = Buf.getError())
142 return EC;
143 Parent->ThinBuffers.push_back(std::move(*Buf));
144 return Parent->ThinBuffers.back()->getBuffer();
145}
146
Rafael Espindola747bc072013-07-09 03:39:35 +0000147Archive::Child Archive::Child::getNext() const {
148 size_t SpaceToSkip = Data.size();
149 // If it's odd, add 1 to make it even.
150 if (SpaceToSkip & 1)
151 ++SpaceToSkip;
152
153 const char *NextLoc = Data.data() + SpaceToSkip;
154
155 // Check to see if this is past the end of the archive.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000156 if (NextLoc >= Parent->Data.getBufferEnd())
Craig Topper2617dcc2014-04-15 06:32:26 +0000157 return Child(Parent, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000158
Rafael Espindola0f3de642013-07-09 05:26:25 +0000159 return Child(Parent, NextLoc);
Rafael Espindola747bc072013-07-09 03:39:35 +0000160}
161
Kevin Enderby13023a12015-01-15 23:19:11 +0000162uint64_t Archive::Child::getChildOffset() const {
163 const char *a = Parent->Data.getBuffer().data();
164 const char *c = Data.data();
165 uint64_t offset = c - a;
166 return offset;
167}
168
Rafael Espindolaae460022014-06-16 16:08:36 +0000169ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000170 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000171 // Check if it's a special name.
172 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000173 if (name.size() == 1) // Linker member.
174 return name;
175 if (name.size() == 2 && name[1] == '/') // String table.
176 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000177 // It's a long name.
178 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000179 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000180 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
181 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000182 const char *addr = Parent->StringTable->Data.begin()
183 + sizeof(ArchiveMemberHeader)
Michael J. Spencer751fd882013-01-09 22:58:43 +0000184 + offset;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000185 // Verify it.
Rafael Espindola23a97502014-01-21 16:09:45 +0000186 if (Parent->StringTable == Parent->child_end()
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000187 || addr < (Parent->StringTable->Data.begin()
188 + sizeof(ArchiveMemberHeader))
189 || addr > (Parent->StringTable->Data.begin()
190 + sizeof(ArchiveMemberHeader)
191 + Parent->StringTable->getSize()))
192 return object_error::parse_failed;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000193
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000194 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000195 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000196 StringRef::size_type End = StringRef(addr).find('\n');
197 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000198 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000199 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000200 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000201 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000202 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
203 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindolaae460022014-06-16 16:08:36 +0000204 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000205 .rtrim(StringRef("\0", 1));
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000206 }
207 // It's a simple name.
208 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000209 return name.substr(0, name.size() - 1);
210 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000211}
212
Rafael Espindola48af1c22014-08-19 18:44:46 +0000213ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000214 ErrorOr<StringRef> NameOrErr = getName();
215 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000216 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000217 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000218 ErrorOr<StringRef> Buf = getBuffer();
219 if (std::error_code EC = Buf.getError())
220 return EC;
221 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000222}
223
224ErrorOr<std::unique_ptr<Binary>>
225Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000226 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000227 if (std::error_code EC = BuffOrErr.getError())
228 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000229
Rafael Espindola48af1c22014-08-19 18:44:46 +0000230 return createBinary(BuffOrErr.get(), Context);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000231}
232
Rafael Espindola48af1c22014-08-19 18:44:46 +0000233ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000234 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000235 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000236 if (EC)
237 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000238 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000239}
240
Rafael Espindola48af1c22014-08-19 18:44:46 +0000241Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindolac91177e2015-07-08 22:15:07 +0000242 : Binary(Binary::ID_Archive, Source), SymbolTable(child_end()),
243 StringTable(child_end()), FirstRegular(child_end()) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000244 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000245 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000246 if (Buffer.startswith(ThinMagic)) {
247 IsThin = true;
248 } else if (Buffer.startswith(Magic)) {
249 IsThin = false;
250 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000251 ec = object_error::invalid_file_type;
252 return;
253 }
254
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000255 // Get the special members.
Rafael Espindola23a97502014-01-21 16:09:45 +0000256 child_iterator i = child_begin(false);
257 child_iterator e = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000258
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000259 if (i == e) {
Rui Ueyama7d099192015-06-09 15:20:42 +0000260 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000261 return;
262 }
263
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000264 StringRef Name = i->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000265
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000266 // Below is the pattern that is used to figure out the archive format
267 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000268 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000269 // Second member : // (may exist, if it exists, points to the string table)
270 // Note : The string table is used if the filename exceeds 15 characters
271 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000272 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
273 // There is no string table, if the filename exceeds 15 characters or has a
274 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000275 // of the filename that needs to be read after the archive header
276 // COFF archive format
277 // First member : /
278 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000279 // Third member : // (may exist, if it exists, contains the string table)
280 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
281 // even if the string table is empty. However, lib.exe does not in fact
282 // seem to create the third member if there's no member whose filename
283 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000284
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000285 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000286 Format = K_BSD;
287 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000288 ++i;
289 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000290 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000291 return;
292 }
293
Rafael Espindola55509922013-07-10 22:07:59 +0000294 if (Name.startswith("#1/")) {
295 Format = K_BSD;
296 // We know this is BSD, so getName will work since there is no string table.
Rafael Espindolaae460022014-06-16 16:08:36 +0000297 ErrorOr<StringRef> NameOrErr = i->getName();
298 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000299 if (ec)
300 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000301 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000302 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola55509922013-07-10 22:07:59 +0000303 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000304 ++i;
305 }
306 FirstRegular = i;
Rafael Espindola55509922013-07-10 22:07:59 +0000307 return;
308 }
309
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000310 // MIPS 64-bit ELF archives use a special format of a symbol table.
311 // This format is marked by `ar_name` field equals to "/SYM64/".
312 // For detailed description see page 96 in the following document:
313 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
314
315 bool has64SymTable = false;
316 if (Name == "/" || Name == "/SYM64/") {
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000317 SymbolTable = i;
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000318 if (Name == "/SYM64/")
319 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000320
321 ++i;
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000322 if (i == e) {
Rafael Espindola4104fe82015-07-08 22:27:54 +0000323 ec = std::error_code();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000324 return;
325 }
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000326 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000327 }
328
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000329 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000330 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000331 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000332 ++i;
333 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000334 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000335 return;
336 }
337
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000338 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000339 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000340 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000341 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000342 return;
343 }
344
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000345 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000346 ec = object_error::parse_failed;
347 return;
348 }
349
350 Format = K_COFF;
351 SymbolTable = i;
352
353 ++i;
354 if (i == e) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000355 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000356 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000357 return;
358 }
359
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000360 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000361
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000362 if (Name == "//") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000363 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000364 ++i;
365 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000366
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000367 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000368 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000369}
370
Rafael Espindola23a97502014-01-21 16:09:45 +0000371Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000372 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000373 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000374
375 if (SkipInternal)
376 return FirstRegular;
377
Rafael Espindola48af1c22014-08-19 18:44:46 +0000378 const char *Loc = Data.getBufferStart() + strlen(Magic);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000379 Child c(this, Loc);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000380 return c;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000381}
382
Rafael Espindola23a97502014-01-21 16:09:45 +0000383Archive::child_iterator Archive::child_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000384 return Child(this, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000385}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000386
Rafael Espindolaae460022014-06-16 16:08:36 +0000387StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000388 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000389}
390
Rafael Espindolaae460022014-06-16 16:08:36 +0000391ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000392 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000393 const char *Offsets = Buf;
394 if (Parent->kind() == K_MIPS64)
395 Offsets += sizeof(uint64_t);
396 else
397 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000398 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000399 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000400 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000401 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000402 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000403 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000404 // The SymbolIndex is an index into the ranlib structs that start at
405 // Offsets (the first uint32_t is the number of bytes of the ranlib
406 // structs). The ranlib structs are a pair of uint32_t's the first
407 // being a string table offset and the second being the offset into
408 // the archive of the member that defines the symbol. Which is what
409 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000410 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000411 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000412 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000413 uint32_t MemberCount = read32le(Buf);
414 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000415
Rui Ueyama3206b792015-03-02 21:19:12 +0000416 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000417 if (SymbolIndex >= SymbolCount)
418 return object_error::parse_failed;
419
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000420 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000421 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000422
423 // Get the index of the offset in the file member offset table for this
424 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000425 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000426 // Subtract 1 since OffsetIndex is 1 based.
427 --OffsetIndex;
428
429 if (OffsetIndex >= MemberCount)
430 return object_error::parse_failed;
431
Rui Ueyama3206b792015-03-02 21:19:12 +0000432 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000433 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000434
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000435 const char *Loc = Parent->getData().begin() + Offset;
Rafael Espindolaae460022014-06-16 16:08:36 +0000436 child_iterator Iter(Child(Parent, Loc));
437 return Iter;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000438}
439
440Archive::Symbol Archive::Symbol::getNext() const {
441 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000442 if (Parent->kind() == K_BSD) {
443 // t.StringIndex is an offset from the start of the __.SYMDEF or
444 // "__.SYMDEF SORTED" member into the string table for the ranlib
445 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
446 // offset in the string table for t.SymbolIndex+1 we subtract the
447 // its offset from the start of the string table for t.SymbolIndex
448 // and add the offset of the string table for t.SymbolIndex+1.
449
450 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
451 // which is the number of bytes of ranlib structs that follow. The ranlib
452 // structs are a pair of uint32_t's the first being a string table offset
453 // and the second being the offset into the archive of the member that
454 // define the symbol. After that the next uint32_t is the byte count of
455 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000456 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000457 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000458 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000459 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
460 // don't change the t.StringIndex as we don't want to reference a ranlib
461 // past RanlibCount.
462 if (t.SymbolIndex + 1 < RanlibCount) {
463 const char *Ranlibs = Buf + 4;
464 uint32_t CurRanStrx = 0;
465 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000466 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
467 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000468 t.StringIndex -= CurRanStrx;
469 t.StringIndex += NextRanStrx;
470 }
471 } else {
472 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000473 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000474 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000475 ++t.SymbolIndex;
476 return t;
477}
478
Rafael Espindola23a97502014-01-21 16:09:45 +0000479Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000480 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000481 return symbol_iterator(Symbol(this, 0, 0));
482
Rafael Espindola2b054162015-07-14 01:06:16 +0000483 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000484 if (kind() == K_GNU) {
485 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000486 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000487 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000488 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000489 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000490 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000491 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000492 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
493 // which is the number of bytes of ranlib structs that follow. The ranlib
494 // structs are a pair of uint32_t's the first being a string table offset
495 // and the second being the offset into the archive of the member that
496 // define the symbol. After that the next uint32_t is the byte count of
497 // the string table followed by the string table.
498 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000499 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000500 const char *ranlibs = buf + 4;
501 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000502 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000503 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
504 // Skip the byte count of the string table.
505 buf += sizeof(uint32_t);
506 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000507 } else {
508 uint32_t member_count = 0;
509 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000510 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000511 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000512 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000513 buf += 4 + (symbol_count * 2); // Skip indices.
514 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000515 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000516 return symbol_iterator(Symbol(this, 0, string_start_offset));
517}
518
Rafael Espindola23a97502014-01-21 16:09:45 +0000519Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000520 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
521}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000522
Rui Ueyama407e0972015-05-26 16:20:40 +0000523uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000524 if (!hasSymbolTable())
525 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000526 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000527 if (kind() == K_GNU)
528 return read32be(buf);
529 if (kind() == K_MIPS64)
530 return read64be(buf);
531 if (kind() == K_BSD)
532 return read32le(buf) / 8;
533 uint32_t member_count = 0;
534 member_count = read32le(buf);
535 buf += 4 + (member_count * 4); // Skip offsets.
536 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000537}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000538
539Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000540 Archive::symbol_iterator bs = symbol_begin();
541 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000542
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000543 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000544 StringRef SymName = bs->getName();
545 if (SymName == name) {
546 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
547 // FIXME: Should we really eat the error?
548 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000549 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000550 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000551 }
552 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000553 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000554}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000555
556bool Archive::hasSymbolTable() const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000557 return SymbolTable != child_end();
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000558}