blob: 6d09bdbac06ed0d4eafdc00e27b47c1d1cd71409 [file] [log] [blame]
Michael J. Spencera51d7d92011-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 Espindola5263d0a2013-07-09 03:39:35 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Twine.h"
Michael J. Spencerc8a55a62011-11-02 19:33:12 +000018#include "llvm/Support/Endian.h"
Michael J. Spencera51d7d92011-09-27 19:36:55 +000019#include "llvm/Support/MemoryBuffer.h"
20
21using namespace llvm;
22using namespace object;
23
Craig Topper4172a8a2013-07-16 01:17:10 +000024static const char *const Magic = "!<arch>\n";
Michael J. Spencera51d7d92011-09-27 19:36:55 +000025
David Blaikie2d24e2a2011-12-20 02:50:00 +000026void Archive::anchor() { }
27
Rafael Espindola5263d0a2013-07-09 03:39:35 +000028StringRef ArchiveMemberHeader::getName() const {
29 char EndCond;
30 if (Name[0] == '/' || Name[0] == '#')
31 EndCond = ' ';
32 else
33 EndCond = '/';
34 llvm::StringRef::size_type end =
35 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
36 if (end == llvm::StringRef::npos)
37 end = sizeof(Name);
38 assert(end <= sizeof(Name) && end > 0);
39 // Don't include the EndCond if there is one.
40 return llvm::StringRef(Name, end);
41}
42
Rafael Espindola20125932013-07-09 12:45:11 +000043uint32_t ArchiveMemberHeader::getSize() const {
44 uint32_t Ret;
45 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
46 llvm_unreachable("Size is not a decimal number.");
47 return Ret;
Rafael Espindola5263d0a2013-07-09 03:39:35 +000048}
49
Rafael Espindola9941bdd2013-07-09 12:49:24 +000050sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
51 unsigned Ret;
52 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
53 llvm_unreachable("Access mode is not an octal number.");
54 return static_cast<sys::fs::perms>(Ret);
55}
56
57sys::TimeValue ArchiveMemberHeader::getLastModified() const {
58 unsigned Seconds;
59 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
60 .getAsInteger(10, Seconds))
61 llvm_unreachable("Last modified time not a decimal number.");
62
63 sys::TimeValue Ret;
64 Ret.fromEpochTime(Seconds);
65 return Ret;
66}
67
68unsigned ArchiveMemberHeader::getUID() const {
69 unsigned Ret;
70 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
71 llvm_unreachable("UID time not a decimal number.");
72 return Ret;
73}
74
75unsigned ArchiveMemberHeader::getGID() const {
76 unsigned Ret;
77 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
78 llvm_unreachable("GID time not a decimal number.");
79 return Ret;
80}
81
Rafael Espindolabe6b9102013-07-09 05:26:25 +000082Archive::Child::Child(const Archive *Parent, const char *Start)
83 : Parent(Parent) {
84 if (!Start)
Rafael Espindola5263d0a2013-07-09 03:39:35 +000085 return;
Rafael Espindolabe6b9102013-07-09 05:26:25 +000086
Rafael Espindola34ac52d2013-07-12 20:21:39 +000087 const ArchiveMemberHeader *Header =
88 reinterpret_cast<const ArchiveMemberHeader *>(Start);
Rafael Espindolabe6b9102013-07-09 05:26:25 +000089 Data = StringRef(Start, sizeof(ArchiveMemberHeader) + Header->getSize());
90
Rafael Espindola5263d0a2013-07-09 03:39:35 +000091 // Setup StartOfFile and PaddingBytes.
92 StartOfFile = sizeof(ArchiveMemberHeader);
93 // Don't include attached name.
Rafael Espindolabe6b9102013-07-09 05:26:25 +000094 StringRef Name = Header->getName();
Rafael Espindola5263d0a2013-07-09 03:39:35 +000095 if (Name.startswith("#1/")) {
96 uint64_t NameSize;
97 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
98 llvm_unreachable("Long name length is not an integer");
99 StartOfFile += NameSize;
100 }
101}
102
103Archive::Child Archive::Child::getNext() const {
104 size_t SpaceToSkip = Data.size();
105 // If it's odd, add 1 to make it even.
106 if (SpaceToSkip & 1)
107 ++SpaceToSkip;
108
109 const char *NextLoc = Data.data() + SpaceToSkip;
110
111 // Check to see if this is past the end of the archive.
112 if (NextLoc >= Parent->Data->getBufferEnd())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700113 return Child(Parent, nullptr);
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000114
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000115 return Child(Parent, NextLoc);
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000116}
117
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700118ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000119 StringRef name = getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000120 // Check if it's a special name.
121 if (name[0] == '/') {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700122 if (name.size() == 1) // Linker member.
123 return name;
124 if (name.size() == 2 && name[1] == '/') // String table.
125 return name;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000126 // It's a long name.
127 // Get the offset.
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000128 std::size_t offset;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000129 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
130 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000131 const char *addr = Parent->StringTable->Data.begin()
132 + sizeof(ArchiveMemberHeader)
Michael J. Spencere92800d2013-01-09 22:58:43 +0000133 + offset;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000134 // Verify it.
Stephen Hines36b56882014-04-23 16:57:46 -0700135 if (Parent->StringTable == Parent->child_end()
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000136 || addr < (Parent->StringTable->Data.begin()
137 + sizeof(ArchiveMemberHeader))
138 || addr > (Parent->StringTable->Data.begin()
139 + sizeof(ArchiveMemberHeader)
140 + Parent->StringTable->getSize()))
141 return object_error::parse_failed;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000142
143 // GNU long file names end with a /.
144 if (Parent->kind() == K_GNU) {
145 StringRef::size_type End = StringRef(addr).find('/');
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700146 return StringRef(addr, End);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000147 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700148 return StringRef(addr);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000149 } else if (name.startswith("#1/")) {
Michael J. Spencere92800d2013-01-09 22:58:43 +0000150 uint64_t name_size;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000151 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
152 llvm_unreachable("Long name length is not an ingeter");
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700153 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000154 .rtrim(StringRef("\0", 1));
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000155 }
156 // It's a simple name.
157 if (name[name.size() - 1] == '/')
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700158 return name.substr(0, name.size() - 1);
159 return name;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000160}
161
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700162ErrorOr<std::unique_ptr<MemoryBuffer>>
163Archive::Child::getMemoryBuffer(bool FullPath) const {
164 ErrorOr<StringRef> NameOrErr = getName();
165 if (std::error_code EC = NameOrErr.getError())
Stephen Hines36b56882014-04-23 16:57:46 -0700166 return EC;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700167 StringRef Name = NameOrErr.get();
168 SmallString<128> Path;
169 std::unique_ptr<MemoryBuffer> Ret(MemoryBuffer::getMemBuffer(
170 getBuffer(),
171 FullPath
172 ? (Twine(Parent->getFileName()) + "(" + Name + ")").toStringRef(Path)
173 : Name,
174 false));
175 return std::move(Ret);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000176}
177
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700178ErrorOr<std::unique_ptr<Binary>>
179Archive::Child::getAsBinary(LLVMContext *Context) const {
180 std::unique_ptr<Binary> ret;
181 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = getMemoryBuffer();
182 if (std::error_code EC = BuffOrErr.getError())
183 return EC;
184
185 std::unique_ptr<MemoryBuffer> Buff(BuffOrErr.get().release());
186 return createBinary(Buff, Context);
187}
188
189ErrorOr<Archive *> Archive::create(std::unique_ptr<MemoryBuffer> Source) {
190 std::error_code EC;
191 std::unique_ptr<Archive> Ret(new Archive(std::move(Source), EC));
Stephen Hines36b56882014-04-23 16:57:46 -0700192 if (EC)
193 return EC;
194 return Ret.release();
195}
196
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700197Archive::Archive(std::unique_ptr<MemoryBuffer> Source, std::error_code &ec)
198 : Binary(Binary::ID_Archive, std::move(Source)), SymbolTable(child_end()) {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000199 // Check for sufficient magic.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700200 if (Data->getBufferSize() < 8 ||
201 StringRef(Data->getBufferStart(), 8) != Magic) {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000202 ec = object_error::invalid_file_type;
203 return;
204 }
205
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000206 // Get the special members.
Stephen Hines36b56882014-04-23 16:57:46 -0700207 child_iterator i = child_begin(false);
208 child_iterator e = child_end();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000209
Rafael Espindola09a7f602013-07-03 15:57:14 +0000210 if (i == e) {
Rafael Espindola5e102c62013-07-12 13:32:28 +0000211 ec = object_error::success;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000212 return;
213 }
214
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000215 StringRef Name = i->getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000216
Shankar Easwaran206252c2012-11-13 18:38:42 +0000217 // Below is the pattern that is used to figure out the archive format
218 // GNU archive format
Rafael Espindola09a7f602013-07-03 15:57:14 +0000219 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran206252c2012-11-13 18:38:42 +0000220 // Second member : // (may exist, if it exists, points to the string table)
221 // Note : The string table is used if the filename exceeds 15 characters
222 // BSD archive format
Rafael Espindolaa7397592013-07-10 22:07:59 +0000223 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
224 // There is no string table, if the filename exceeds 15 characters or has a
225 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran206252c2012-11-13 18:38:42 +0000226 // of the filename that needs to be read after the archive header
227 // COFF archive format
228 // First member : /
229 // Second member : / (provides a directory of symbols)
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000230 // Third member : // (may exist, if it exists, contains the string table)
231 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
232 // even if the string table is empty. However, lib.exe does not in fact
233 // seem to create the third member if there's no member whose filename
234 // exceeds 15 characters. So the third member is optional.
Rafael Espindola09a7f602013-07-03 15:57:14 +0000235
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000236 if (Name == "__.SYMDEF") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000237 Format = K_BSD;
238 SymbolTable = i;
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000239 ++i;
240 FirstRegular = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000241 ec = object_error::success;
242 return;
243 }
244
Rafael Espindolaa7397592013-07-10 22:07:59 +0000245 if (Name.startswith("#1/")) {
246 Format = K_BSD;
247 // We know this is BSD, so getName will work since there is no string table.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700248 ErrorOr<StringRef> NameOrErr = i->getName();
249 ec = NameOrErr.getError();
Rafael Espindolaa7397592013-07-10 22:07:59 +0000250 if (ec)
251 return;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700252 Name = NameOrErr.get();
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000253 if (Name == "__.SYMDEF SORTED") {
Rafael Espindolaa7397592013-07-10 22:07:59 +0000254 SymbolTable = i;
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000255 ++i;
256 }
257 FirstRegular = i;
Rafael Espindolaa7397592013-07-10 22:07:59 +0000258 return;
259 }
260
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000261 if (Name == "/") {
Shankar Easwaran206252c2012-11-13 18:38:42 +0000262 SymbolTable = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000263
264 ++i;
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000265 if (i == e) {
266 ec = object_error::parse_failed;
267 return;
268 }
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000269 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000270 }
271
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000272 if (Name == "//") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000273 Format = K_GNU;
274 StringTable = i;
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000275 ++i;
276 FirstRegular = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000277 ec = object_error::success;
278 return;
279 }
280
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000281 if (Name[0] != '/') {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000282 Format = K_GNU;
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000283 FirstRegular = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000284 ec = object_error::success;
285 return;
286 }
287
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000288 if (Name != "/") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000289 ec = object_error::parse_failed;
290 return;
291 }
292
293 Format = K_COFF;
294 SymbolTable = i;
295
296 ++i;
297 if (i == e) {
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000298 FirstRegular = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000299 ec = object_error::success;
300 return;
301 }
302
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000303 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000304
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000305 if (Name == "//") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000306 StringTable = i;
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000307 ++i;
308 }
Rafael Espindola09a7f602013-07-03 15:57:14 +0000309
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000310 FirstRegular = i;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000311 ec = object_error::success;
312}
313
Stephen Hines36b56882014-04-23 16:57:46 -0700314Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola5e102c62013-07-12 13:32:28 +0000315 if (Data->getBufferSize() == 8) // empty archive.
Stephen Hines36b56882014-04-23 16:57:46 -0700316 return child_end();
Rafael Espindola34ac52d2013-07-12 20:21:39 +0000317
318 if (SkipInternal)
319 return FirstRegular;
320
Benjamin Kramere1a44272012-02-22 13:42:11 +0000321 const char *Loc = Data->getBufferStart() + strlen(Magic);
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000322 Child c(this, Loc);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000323 return c;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000324}
325
Stephen Hines36b56882014-04-23 16:57:46 -0700326Archive::child_iterator Archive::child_end() const {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700327 return Child(this, nullptr);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000328}
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000329
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700330StringRef Archive::Symbol::getName() const {
331 return Parent->SymbolTable->getBuffer().begin() + StringIndex;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000332}
333
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700334ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000335 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer768a7072012-11-14 00:04:13 +0000336 const char *Offsets = Buf + 4;
337 uint32_t Offset = 0;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000338 if (Parent->kind() == K_GNU) {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000339 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
340 + SymbolIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000341 } else if (Parent->kind() == K_BSD) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700342 // The SymbolIndex is an index into the ranlib structs that start at
343 // Offsets (the first uint32_t is the number of bytes of the ranlib
344 // structs). The ranlib structs are a pair of uint32_t's the first
345 // being a string table offset and the second being the offset into
346 // the archive of the member that defines the symbol. Which is what
347 // is needed here.
348 Offset = *(reinterpret_cast<const support::ulittle32_t *>(Offsets) +
349 (SymbolIndex * 2) + 1);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000350 } else {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000351 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
352
353 // Skip offsets.
354 Buf += sizeof(support::ulittle32_t)
355 + (MemberCount * sizeof(support::ulittle32_t));
356
357 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
358
359 if (SymbolIndex >= SymbolCount)
360 return object_error::parse_failed;
361
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000362 // Skip SymbolCount to get to the indices table.
363 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000364
365 // Get the index of the offset in the file member offset table for this
366 // symbol.
367 uint16_t OffsetIndex =
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000368 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran206252c2012-11-13 18:38:42 +0000369 + SymbolIndex);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000370 // Subtract 1 since OffsetIndex is 1 based.
371 --OffsetIndex;
372
373 if (OffsetIndex >= MemberCount)
374 return object_error::parse_failed;
375
376 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
377 + OffsetIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000378 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000379
Michael J. Spencer768a7072012-11-14 00:04:13 +0000380 const char *Loc = Parent->getData().begin() + Offset;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700381 child_iterator Iter(Child(Parent, Loc));
382 return Iter;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000383}
384
385Archive::Symbol Archive::Symbol::getNext() const {
386 Symbol t(*this);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700387 if (Parent->kind() == K_BSD) {
388 // t.StringIndex is an offset from the start of the __.SYMDEF or
389 // "__.SYMDEF SORTED" member into the string table for the ranlib
390 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
391 // offset in the string table for t.SymbolIndex+1 we subtract the
392 // its offset from the start of the string table for t.SymbolIndex
393 // and add the offset of the string table for t.SymbolIndex+1.
394
395 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
396 // which is the number of bytes of ranlib structs that follow. The ranlib
397 // structs are a pair of uint32_t's the first being a string table offset
398 // and the second being the offset into the archive of the member that
399 // define the symbol. After that the next uint32_t is the byte count of
400 // the string table followed by the string table.
401 const char *Buf = Parent->SymbolTable->getBuffer().begin();
402 uint32_t RanlibCount = 0;
403 RanlibCount = (*reinterpret_cast<const support::ulittle32_t *>(Buf)) /
404 (sizeof(uint32_t) * 2);
405 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
406 // don't change the t.StringIndex as we don't want to reference a ranlib
407 // past RanlibCount.
408 if (t.SymbolIndex + 1 < RanlibCount) {
409 const char *Ranlibs = Buf + 4;
410 uint32_t CurRanStrx = 0;
411 uint32_t NextRanStrx = 0;
412 CurRanStrx = *(reinterpret_cast<const support::ulittle32_t *>(Ranlibs) +
413 (t.SymbolIndex * 2));
414 NextRanStrx = *(reinterpret_cast<const support::ulittle32_t *>(Ranlibs) +
415 ((t.SymbolIndex + 1) * 2));
416 t.StringIndex -= CurRanStrx;
417 t.StringIndex += NextRanStrx;
418 }
419 } else {
420 // Go to one past next null.
421 t.StringIndex =
422 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
423 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000424 ++t.SymbolIndex;
425 return t;
426}
427
Stephen Hines36b56882014-04-23 16:57:46 -0700428Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolacf48cf22013-07-29 12:40:31 +0000429 if (!hasSymbolTable())
Rafael Espindola51597182013-07-10 20:14:22 +0000430 return symbol_iterator(Symbol(this, 0, 0));
431
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000432 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000433 if (kind() == K_GNU) {
434 uint32_t symbol_count = 0;
435 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
436 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
437 } else if (kind() == K_BSD) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700438 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
439 // which is the number of bytes of ranlib structs that follow. The ranlib
440 // structs are a pair of uint32_t's the first being a string table offset
441 // and the second being the offset into the archive of the member that
442 // define the symbol. After that the next uint32_t is the byte count of
443 // the string table followed by the string table.
444 uint32_t ranlib_count = 0;
445 ranlib_count = (*reinterpret_cast<const support::ulittle32_t *>(buf)) /
446 (sizeof(uint32_t) * 2);
447 const char *ranlibs = buf + 4;
448 uint32_t ran_strx = 0;
449 ran_strx = *(reinterpret_cast<const support::ulittle32_t *>(ranlibs));
450 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
451 // Skip the byte count of the string table.
452 buf += sizeof(uint32_t);
453 buf += ran_strx;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000454 } else {
455 uint32_t member_count = 0;
456 uint32_t symbol_count = 0;
457 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
458 buf += 4 + (member_count * 4); // Skip offsets.
459 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
460 buf += 4 + (symbol_count * 2); // Skip indices.
461 }
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000462 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000463 return symbol_iterator(Symbol(this, 0, string_start_offset));
464}
465
Stephen Hines36b56882014-04-23 16:57:46 -0700466Archive::symbol_iterator Archive::symbol_end() const {
Rafael Espindolacf48cf22013-07-29 12:40:31 +0000467 if (!hasSymbolTable())
Rafael Espindola51597182013-07-10 20:14:22 +0000468 return symbol_iterator(Symbol(this, 0, 0));
469
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000470 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000471 uint32_t symbol_count = 0;
472 if (kind() == K_GNU) {
473 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000474 } else if (kind() == K_BSD) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700475 symbol_count = (*reinterpret_cast<const support::ulittle32_t *>(buf)) /
476 (sizeof(uint32_t) * 2);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000477 } else {
478 uint32_t member_count = 0;
479 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
480 buf += 4 + (member_count * 4); // Skip offsets.
481 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
482 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000483 return symbol_iterator(
484 Symbol(this, symbol_count, 0));
485}
Shankar Easwaran206252c2012-11-13 18:38:42 +0000486
487Archive::child_iterator Archive::findSym(StringRef name) const {
Stephen Hines36b56882014-04-23 16:57:46 -0700488 Archive::symbol_iterator bs = symbol_begin();
489 Archive::symbol_iterator es = symbol_end();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700490
Shankar Easwaran206252c2012-11-13 18:38:42 +0000491 for (; bs != es; ++bs) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700492 StringRef SymName = bs->getName();
493 if (SymName == name) {
494 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
495 // FIXME: Should we really eat the error?
496 if (ResultOrErr.getError())
Stephen Hines36b56882014-04-23 16:57:46 -0700497 return child_end();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700498 return ResultOrErr.get();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000499 }
500 }
Stephen Hines36b56882014-04-23 16:57:46 -0700501 return child_end();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000502}
Rafael Espindolacf48cf22013-07-29 12:40:31 +0000503
504bool Archive::hasSymbolTable() const {
Stephen Hines36b56882014-04-23 16:57:46 -0700505 return SymbolTable != child_end();
Rafael Espindolacf48cf22013-07-29 12:40:31 +0000506}