blob: 43b0771a4a443bd5811e5073bdae3844e9b691e5 [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"
20
21using namespace llvm;
22using namespace object;
23
Craig Topperd3a34f82013-07-16 01:17:10 +000024static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000025static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000026
David Blaikiea379b1812011-12-20 02:50:00 +000027void Archive::anchor() { }
28
Rafael Espindola747bc072013-07-09 03:39:35 +000029StringRef ArchiveMemberHeader::getName() const {
30 char EndCond;
31 if (Name[0] == '/' || Name[0] == '#')
32 EndCond = ' ';
33 else
34 EndCond = '/';
35 llvm::StringRef::size_type end =
36 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
37 if (end == llvm::StringRef::npos)
38 end = sizeof(Name);
39 assert(end <= sizeof(Name) && end > 0);
40 // Don't include the EndCond if there is one.
41 return llvm::StringRef(Name, end);
42}
43
Rafael Espindola8e9385e2013-07-09 12:45:11 +000044uint32_t ArchiveMemberHeader::getSize() const {
45 uint32_t Ret;
46 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
47 llvm_unreachable("Size is not a decimal number.");
48 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000049}
50
Rafael Espindola8115e1d2013-07-09 12:49:24 +000051sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
52 unsigned Ret;
53 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
54 llvm_unreachable("Access mode is not an octal number.");
55 return static_cast<sys::fs::perms>(Ret);
56}
57
58sys::TimeValue ArchiveMemberHeader::getLastModified() const {
59 unsigned Seconds;
60 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
61 .getAsInteger(10, Seconds))
62 llvm_unreachable("Last modified time not a decimal number.");
63
64 sys::TimeValue Ret;
65 Ret.fromEpochTime(Seconds);
66 return Ret;
67}
68
69unsigned ArchiveMemberHeader::getUID() const {
70 unsigned Ret;
71 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
72 llvm_unreachable("UID time not a decimal number.");
73 return Ret;
74}
75
76unsigned ArchiveMemberHeader::getGID() const {
77 unsigned Ret;
78 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
79 llvm_unreachable("GID time not a decimal number.");
80 return Ret;
81}
82
Rafael Espindola0f3de642013-07-09 05:26:25 +000083Archive::Child::Child(const Archive *Parent, const char *Start)
84 : Parent(Parent) {
85 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000086 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000087
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000088 const ArchiveMemberHeader *Header =
89 reinterpret_cast<const ArchiveMemberHeader *>(Start);
Rafael Espindola9d102062014-12-16 01:43:41 +000090 uint64_t Size = sizeof(ArchiveMemberHeader);
91 if (!Parent->IsThin || Header->getName() == "/" || Header->getName() == "//")
92 Size += Header->getSize();
93 Data = StringRef(Start, Size);
Rafael Espindola0f3de642013-07-09 05:26:25 +000094
Rafael Espindola747bc072013-07-09 03:39:35 +000095 // Setup StartOfFile and PaddingBytes.
96 StartOfFile = sizeof(ArchiveMemberHeader);
97 // Don't include attached name.
Rafael Espindola0f3de642013-07-09 05:26:25 +000098 StringRef Name = Header->getName();
Rafael Espindola747bc072013-07-09 03:39:35 +000099 if (Name.startswith("#1/")) {
100 uint64_t NameSize;
101 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
102 llvm_unreachable("Long name length is not an integer");
103 StartOfFile += NameSize;
104 }
105}
106
Rafael Espindola9d102062014-12-16 01:43:41 +0000107uint64_t Archive::Child::getSize() const {
108 if (Parent->IsThin)
109 return getHeader()->getSize();
110 return Data.size() - StartOfFile;
111}
112
Kevin Enderby13023a12015-01-15 23:19:11 +0000113uint64_t Archive::Child::getRawSize() const {
Kevin Enderbyc12718932015-01-16 22:10:36 +0000114 return getHeader()->getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000115}
116
Rafael Espindola747bc072013-07-09 03:39:35 +0000117Archive::Child Archive::Child::getNext() const {
118 size_t SpaceToSkip = Data.size();
119 // If it's odd, add 1 to make it even.
120 if (SpaceToSkip & 1)
121 ++SpaceToSkip;
122
123 const char *NextLoc = Data.data() + SpaceToSkip;
124
125 // Check to see if this is past the end of the archive.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000126 if (NextLoc >= Parent->Data.getBufferEnd())
Craig Topper2617dcc2014-04-15 06:32:26 +0000127 return Child(Parent, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000128
Rafael Espindola0f3de642013-07-09 05:26:25 +0000129 return Child(Parent, NextLoc);
Rafael Espindola747bc072013-07-09 03:39:35 +0000130}
131
Kevin Enderby13023a12015-01-15 23:19:11 +0000132uint64_t Archive::Child::getChildOffset() const {
133 const char *a = Parent->Data.getBuffer().data();
134 const char *c = Data.data();
135 uint64_t offset = c - a;
136 return offset;
137}
138
Rafael Espindolaae460022014-06-16 16:08:36 +0000139ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000140 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000141 // Check if it's a special name.
142 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000143 if (name.size() == 1) // Linker member.
144 return name;
145 if (name.size() == 2 && name[1] == '/') // String table.
146 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000147 // It's a long name.
148 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000149 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000150 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
151 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000152 const char *addr = Parent->StringTable->Data.begin()
153 + sizeof(ArchiveMemberHeader)
Michael J. Spencer751fd882013-01-09 22:58:43 +0000154 + offset;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000155 // Verify it.
Rafael Espindola23a97502014-01-21 16:09:45 +0000156 if (Parent->StringTable == Parent->child_end()
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000157 || addr < (Parent->StringTable->Data.begin()
158 + sizeof(ArchiveMemberHeader))
159 || addr > (Parent->StringTable->Data.begin()
160 + sizeof(ArchiveMemberHeader)
161 + Parent->StringTable->getSize()))
162 return object_error::parse_failed;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000163
164 // GNU long file names end with a /.
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000165 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000166 StringRef::size_type End = StringRef(addr).find('/');
Rafael Espindolaae460022014-06-16 16:08:36 +0000167 return StringRef(addr, End);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000168 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000169 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000170 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000171 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000172 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
173 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindolaae460022014-06-16 16:08:36 +0000174 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000175 .rtrim(StringRef("\0", 1));
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000176 }
177 // It's a simple name.
178 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000179 return name.substr(0, name.size() - 1);
180 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000181}
182
Rafael Espindola48af1c22014-08-19 18:44:46 +0000183ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000184 ErrorOr<StringRef> NameOrErr = getName();
185 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000186 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000187 StringRef Name = NameOrErr.get();
Rafael Espindola48af1c22014-08-19 18:44:46 +0000188 return MemoryBufferRef(getBuffer(), Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000189}
190
191ErrorOr<std::unique_ptr<Binary>>
192Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000193 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000194 if (std::error_code EC = BuffOrErr.getError())
195 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000196
Rafael Espindola48af1c22014-08-19 18:44:46 +0000197 return createBinary(BuffOrErr.get(), Context);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000198}
199
Rafael Espindola48af1c22014-08-19 18:44:46 +0000200ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000201 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000202 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000203 if (EC)
204 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000205 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000206}
207
Rafael Espindola48af1c22014-08-19 18:44:46 +0000208Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
209 : Binary(Binary::ID_Archive, Source), SymbolTable(child_end()) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000210 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000211 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000212 if (Buffer.startswith(ThinMagic)) {
213 IsThin = true;
214 } else if (Buffer.startswith(Magic)) {
215 IsThin = false;
216 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000217 ec = object_error::invalid_file_type;
218 return;
219 }
220
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000221 // Get the special members.
Rafael Espindola23a97502014-01-21 16:09:45 +0000222 child_iterator i = child_begin(false);
223 child_iterator e = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000224
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000225 if (i == e) {
Rafael Espindolaf0c61722013-07-12 13:32:28 +0000226 ec = object_error::success;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000227 return;
228 }
229
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000230 StringRef Name = i->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000231
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000232 // Below is the pattern that is used to figure out the archive format
233 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000234 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000235 // Second member : // (may exist, if it exists, points to the string table)
236 // Note : The string table is used if the filename exceeds 15 characters
237 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000238 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
239 // There is no string table, if the filename exceeds 15 characters or has a
240 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000241 // of the filename that needs to be read after the archive header
242 // COFF archive format
243 // First member : /
244 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000245 // Third member : // (may exist, if it exists, contains the string table)
246 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
247 // even if the string table is empty. However, lib.exe does not in fact
248 // seem to create the third member if there's no member whose filename
249 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000250
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000251 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000252 Format = K_BSD;
253 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000254 ++i;
255 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000256 ec = object_error::success;
257 return;
258 }
259
Rafael Espindola55509922013-07-10 22:07:59 +0000260 if (Name.startswith("#1/")) {
261 Format = K_BSD;
262 // We know this is BSD, so getName will work since there is no string table.
Rafael Espindolaae460022014-06-16 16:08:36 +0000263 ErrorOr<StringRef> NameOrErr = i->getName();
264 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000265 if (ec)
266 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000267 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000268 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola55509922013-07-10 22:07:59 +0000269 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000270 ++i;
271 }
272 FirstRegular = i;
Rafael Espindola55509922013-07-10 22:07:59 +0000273 return;
274 }
275
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000276 // MIPS 64-bit ELF archives use a special format of a symbol table.
277 // This format is marked by `ar_name` field equals to "/SYM64/".
278 // For detailed description see page 96 in the following document:
279 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
280
281 bool has64SymTable = false;
282 if (Name == "/" || Name == "/SYM64/") {
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000283 SymbolTable = i;
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000284 if (Name == "/SYM64/")
285 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000286
287 ++i;
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000288 if (i == e) {
289 ec = object_error::parse_failed;
290 return;
291 }
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000292 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000293 }
294
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000295 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000296 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000297 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000298 ++i;
299 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000300 ec = object_error::success;
301 return;
302 }
303
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000304 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000305 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000306 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000307 ec = object_error::success;
308 return;
309 }
310
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000311 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000312 ec = object_error::parse_failed;
313 return;
314 }
315
316 Format = K_COFF;
317 SymbolTable = i;
318
319 ++i;
320 if (i == e) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000321 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000322 ec = object_error::success;
323 return;
324 }
325
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000326 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000327
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000328 if (Name == "//") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000329 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000330 ++i;
331 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000332
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000333 FirstRegular = i;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000334 ec = object_error::success;
335}
336
Rafael Espindola23a97502014-01-21 16:09:45 +0000337Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000338 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000339 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000340
341 if (SkipInternal)
342 return FirstRegular;
343
Rafael Espindola48af1c22014-08-19 18:44:46 +0000344 const char *Loc = Data.getBufferStart() + strlen(Magic);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000345 Child c(this, Loc);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000346 return c;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000347}
348
Rafael Espindola23a97502014-01-21 16:09:45 +0000349Archive::child_iterator Archive::child_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000350 return Child(this, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000351}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000352
Rafael Espindolaae460022014-06-16 16:08:36 +0000353StringRef Archive::Symbol::getName() const {
354 return Parent->SymbolTable->getBuffer().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000355}
356
Rafael Espindolaae460022014-06-16 16:08:36 +0000357ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
Michael J. Spencer9718f452013-02-03 10:48:50 +0000358 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000359 const char *Offsets = Buf;
360 if (Parent->kind() == K_MIPS64)
361 Offsets += sizeof(uint64_t);
362 else
363 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000364 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000365 if (Parent->kind() == K_GNU) {
Simon Atanasyan0ca59892015-02-10 21:38:25 +0000366 Offset =
367 *(reinterpret_cast<const support::ubig32_t *>(Offsets) + SymbolIndex);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000368 } else if (Parent->kind() == K_MIPS64) {
369 Offset =
370 *(reinterpret_cast<const support::ubig64_t *>(Offsets) + SymbolIndex);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000371 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000372 // The SymbolIndex is an index into the ranlib structs that start at
373 // Offsets (the first uint32_t is the number of bytes of the ranlib
374 // structs). The ranlib structs are a pair of uint32_t's the first
375 // being a string table offset and the second being the offset into
376 // the archive of the member that defines the symbol. Which is what
377 // is needed here.
378 Offset = *(reinterpret_cast<const support::ulittle32_t *>(Offsets) +
379 (SymbolIndex * 2) + 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000380 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000381 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
382
383 // Skip offsets.
Simon Atanasyan0ca59892015-02-10 21:38:25 +0000384 Buf += sizeof(support::ulittle32_t) +
385 (MemberCount * sizeof(support::ulittle32_t));
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000386
387 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
388
389 if (SymbolIndex >= SymbolCount)
390 return object_error::parse_failed;
391
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000392 // Skip SymbolCount to get to the indices table.
393 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000394
395 // Get the index of the offset in the file member offset table for this
396 // symbol.
397 uint16_t OffsetIndex =
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000398 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000399 + SymbolIndex);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000400 // Subtract 1 since OffsetIndex is 1 based.
401 --OffsetIndex;
402
403 if (OffsetIndex >= MemberCount)
404 return object_error::parse_failed;
405
406 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
407 + OffsetIndex);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000408 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000409
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000410 const char *Loc = Parent->getData().begin() + Offset;
Rafael Espindolaae460022014-06-16 16:08:36 +0000411 child_iterator Iter(Child(Parent, Loc));
412 return Iter;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000413}
414
415Archive::Symbol Archive::Symbol::getNext() const {
416 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000417 if (Parent->kind() == K_BSD) {
418 // t.StringIndex is an offset from the start of the __.SYMDEF or
419 // "__.SYMDEF SORTED" member into the string table for the ranlib
420 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
421 // offset in the string table for t.SymbolIndex+1 we subtract the
422 // its offset from the start of the string table for t.SymbolIndex
423 // and add the offset of the string table for t.SymbolIndex+1.
424
425 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
426 // which is the number of bytes of ranlib structs that follow. The ranlib
427 // structs are a pair of uint32_t's the first being a string table offset
428 // and the second being the offset into the archive of the member that
429 // define the symbol. After that the next uint32_t is the byte count of
430 // the string table followed by the string table.
431 const char *Buf = Parent->SymbolTable->getBuffer().begin();
432 uint32_t RanlibCount = 0;
433 RanlibCount = (*reinterpret_cast<const support::ulittle32_t *>(Buf)) /
434 (sizeof(uint32_t) * 2);
435 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
436 // don't change the t.StringIndex as we don't want to reference a ranlib
437 // past RanlibCount.
438 if (t.SymbolIndex + 1 < RanlibCount) {
439 const char *Ranlibs = Buf + 4;
440 uint32_t CurRanStrx = 0;
441 uint32_t NextRanStrx = 0;
442 CurRanStrx = *(reinterpret_cast<const support::ulittle32_t *>(Ranlibs) +
443 (t.SymbolIndex * 2));
444 NextRanStrx = *(reinterpret_cast<const support::ulittle32_t *>(Ranlibs) +
445 ((t.SymbolIndex + 1) * 2));
446 t.StringIndex -= CurRanStrx;
447 t.StringIndex += NextRanStrx;
448 }
449 } else {
450 // Go to one past next null.
451 t.StringIndex =
452 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
453 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000454 ++t.SymbolIndex;
455 return t;
456}
457
Rafael Espindola23a97502014-01-21 16:09:45 +0000458Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000459 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000460 return symbol_iterator(Symbol(this, 0, 0));
461
Michael J. Spencer9718f452013-02-03 10:48:50 +0000462 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000463 if (kind() == K_GNU) {
464 uint32_t symbol_count = 0;
465 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
466 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000467 } else if (kind() == K_MIPS64) {
468 uint64_t symbol_count = *reinterpret_cast<const support::ubig64_t *>(buf);
469 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000470 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000471 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
472 // which is the number of bytes of ranlib structs that follow. The ranlib
473 // structs are a pair of uint32_t's the first being a string table offset
474 // and the second being the offset into the archive of the member that
475 // define the symbol. After that the next uint32_t is the byte count of
476 // the string table followed by the string table.
477 uint32_t ranlib_count = 0;
478 ranlib_count = (*reinterpret_cast<const support::ulittle32_t *>(buf)) /
479 (sizeof(uint32_t) * 2);
480 const char *ranlibs = buf + 4;
481 uint32_t ran_strx = 0;
482 ran_strx = *(reinterpret_cast<const support::ulittle32_t *>(ranlibs));
483 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
484 // Skip the byte count of the string table.
485 buf += sizeof(uint32_t);
486 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000487 } else {
488 uint32_t member_count = 0;
489 uint32_t symbol_count = 0;
490 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
491 buf += 4 + (member_count * 4); // Skip offsets.
492 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
493 buf += 4 + (symbol_count * 2); // Skip indices.
494 }
Michael J. Spencer9718f452013-02-03 10:48:50 +0000495 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000496 return symbol_iterator(Symbol(this, 0, string_start_offset));
497}
498
Rafael Espindola23a97502014-01-21 16:09:45 +0000499Archive::symbol_iterator Archive::symbol_end() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000500 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000501 return symbol_iterator(Symbol(this, 0, 0));
502
Michael J. Spencer9718f452013-02-03 10:48:50 +0000503 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000504 uint32_t symbol_count = 0;
505 if (kind() == K_GNU) {
506 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000507 } else if (kind() == K_MIPS64) {
508 symbol_count = *reinterpret_cast<const support::ubig64_t*>(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000509 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000510 symbol_count = (*reinterpret_cast<const support::ulittle32_t *>(buf)) /
511 (sizeof(uint32_t) * 2);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000512 } else {
513 uint32_t member_count = 0;
514 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
515 buf += 4 + (member_count * 4); // Skip offsets.
516 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
517 }
Simon Atanasyan0ca59892015-02-10 21:38:25 +0000518 return symbol_iterator(Symbol(this, symbol_count, 0));
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000519}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000520
521Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000522 Archive::symbol_iterator bs = symbol_begin();
523 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000524
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000525 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000526 StringRef SymName = bs->getName();
527 if (SymName == name) {
528 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
529 // FIXME: Should we really eat the error?
530 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000531 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000532 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000533 }
534 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000535 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000536}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000537
538bool Archive::hasSymbolTable() const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000539 return SymbolTable != child_end();
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000540}