blob: d03c328ae3b17ba1797f12f0f6a7d6222627c223 [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;
Rui Ueyama3206b792015-03-02 21:19:12 +000023using namespace llvm::support::endian;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000024
Craig Topperd3a34f82013-07-16 01:17:10 +000025static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000026static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000027
David Blaikiea379b1812011-12-20 02:50:00 +000028void Archive::anchor() { }
29
Rafael Espindola747bc072013-07-09 03:39:35 +000030StringRef ArchiveMemberHeader::getName() const {
31 char EndCond;
32 if (Name[0] == '/' || Name[0] == '#')
33 EndCond = ' ';
34 else
35 EndCond = '/';
36 llvm::StringRef::size_type end =
37 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
38 if (end == llvm::StringRef::npos)
39 end = sizeof(Name);
40 assert(end <= sizeof(Name) && end > 0);
41 // Don't include the EndCond if there is one.
42 return llvm::StringRef(Name, end);
43}
44
Rafael Espindola8e9385e2013-07-09 12:45:11 +000045uint32_t ArchiveMemberHeader::getSize() const {
46 uint32_t Ret;
47 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
48 llvm_unreachable("Size is not a decimal number.");
49 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000050}
51
Rafael Espindola8115e1d2013-07-09 12:49:24 +000052sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
53 unsigned Ret;
54 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
55 llvm_unreachable("Access mode is not an octal number.");
56 return static_cast<sys::fs::perms>(Ret);
57}
58
59sys::TimeValue ArchiveMemberHeader::getLastModified() const {
60 unsigned Seconds;
61 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
62 .getAsInteger(10, Seconds))
63 llvm_unreachable("Last modified time not a decimal number.");
64
65 sys::TimeValue Ret;
66 Ret.fromEpochTime(Seconds);
67 return Ret;
68}
69
70unsigned ArchiveMemberHeader::getUID() const {
71 unsigned Ret;
72 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
73 llvm_unreachable("UID time not a decimal number.");
74 return Ret;
75}
76
77unsigned ArchiveMemberHeader::getGID() const {
78 unsigned Ret;
79 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
80 llvm_unreachable("GID time not a decimal number.");
81 return Ret;
82}
83
Rafael Espindola0f3de642013-07-09 05:26:25 +000084Archive::Child::Child(const Archive *Parent, const char *Start)
85 : Parent(Parent) {
86 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000087 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000088
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000089 const ArchiveMemberHeader *Header =
90 reinterpret_cast<const ArchiveMemberHeader *>(Start);
Rafael Espindola9d102062014-12-16 01:43:41 +000091 uint64_t Size = sizeof(ArchiveMemberHeader);
92 if (!Parent->IsThin || Header->getName() == "/" || Header->getName() == "//")
93 Size += Header->getSize();
94 Data = StringRef(Start, Size);
Rafael Espindola0f3de642013-07-09 05:26:25 +000095
Rafael Espindola747bc072013-07-09 03:39:35 +000096 // Setup StartOfFile and PaddingBytes.
97 StartOfFile = sizeof(ArchiveMemberHeader);
98 // Don't include attached name.
Rafael Espindola0f3de642013-07-09 05:26:25 +000099 StringRef Name = Header->getName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000100 if (Name.startswith("#1/")) {
101 uint64_t NameSize;
102 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
103 llvm_unreachable("Long name length is not an integer");
104 StartOfFile += NameSize;
105 }
106}
107
Rafael Espindola9d102062014-12-16 01:43:41 +0000108uint64_t Archive::Child::getSize() const {
109 if (Parent->IsThin)
110 return getHeader()->getSize();
111 return Data.size() - StartOfFile;
112}
113
Kevin Enderby13023a12015-01-15 23:19:11 +0000114uint64_t Archive::Child::getRawSize() const {
Kevin Enderbyc12718932015-01-16 22:10:36 +0000115 return getHeader()->getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000116}
117
Rafael Espindola747bc072013-07-09 03:39:35 +0000118Archive::Child Archive::Child::getNext() const {
119 size_t SpaceToSkip = Data.size();
120 // If it's odd, add 1 to make it even.
121 if (SpaceToSkip & 1)
122 ++SpaceToSkip;
123
124 const char *NextLoc = Data.data() + SpaceToSkip;
125
126 // Check to see if this is past the end of the archive.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000127 if (NextLoc >= Parent->Data.getBufferEnd())
Craig Topper2617dcc2014-04-15 06:32:26 +0000128 return Child(Parent, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000129
Rafael Espindola0f3de642013-07-09 05:26:25 +0000130 return Child(Parent, NextLoc);
Rafael Espindola747bc072013-07-09 03:39:35 +0000131}
132
Kevin Enderby13023a12015-01-15 23:19:11 +0000133uint64_t Archive::Child::getChildOffset() const {
134 const char *a = Parent->Data.getBuffer().data();
135 const char *c = Data.data();
136 uint64_t offset = c - a;
137 return offset;
138}
139
Rafael Espindolaae460022014-06-16 16:08:36 +0000140ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000141 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000142 // Check if it's a special name.
143 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000144 if (name.size() == 1) // Linker member.
145 return name;
146 if (name.size() == 2 && name[1] == '/') // String table.
147 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000148 // It's a long name.
149 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000150 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000151 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
152 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000153 const char *addr = Parent->StringTable->Data.begin()
154 + sizeof(ArchiveMemberHeader)
Michael J. Spencer751fd882013-01-09 22:58:43 +0000155 + offset;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000156 // Verify it.
Rafael Espindola23a97502014-01-21 16:09:45 +0000157 if (Parent->StringTable == Parent->child_end()
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000158 || addr < (Parent->StringTable->Data.begin()
159 + sizeof(ArchiveMemberHeader))
160 || addr > (Parent->StringTable->Data.begin()
161 + sizeof(ArchiveMemberHeader)
162 + Parent->StringTable->getSize()))
163 return object_error::parse_failed;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000164
165 // GNU long file names end with a /.
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000166 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000167 StringRef::size_type End = StringRef(addr).find('/');
Rafael Espindolaae460022014-06-16 16:08:36 +0000168 return StringRef(addr, End);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000169 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000170 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000171 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000172 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000173 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
174 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindolaae460022014-06-16 16:08:36 +0000175 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000176 .rtrim(StringRef("\0", 1));
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000177 }
178 // It's a simple name.
179 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000180 return name.substr(0, name.size() - 1);
181 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000182}
183
Rafael Espindola48af1c22014-08-19 18:44:46 +0000184ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000185 ErrorOr<StringRef> NameOrErr = getName();
186 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000187 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000188 StringRef Name = NameOrErr.get();
Rafael Espindola48af1c22014-08-19 18:44:46 +0000189 return MemoryBufferRef(getBuffer(), Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000190}
191
192ErrorOr<std::unique_ptr<Binary>>
193Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000194 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000195 if (std::error_code EC = BuffOrErr.getError())
196 return EC;
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000197
Rafael Espindola48af1c22014-08-19 18:44:46 +0000198 return createBinary(BuffOrErr.get(), Context);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000199}
200
Rafael Espindola48af1c22014-08-19 18:44:46 +0000201ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000202 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000203 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000204 if (EC)
205 return EC;
Rafael Espindolaf5577132014-07-31 03:36:00 +0000206 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000207}
208
Rafael Espindola48af1c22014-08-19 18:44:46 +0000209Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
Rafael Espindolac91177e2015-07-08 22:15:07 +0000210 : Binary(Binary::ID_Archive, Source), SymbolTable(child_end()),
211 StringTable(child_end()), FirstRegular(child_end()) {
Rafael Espindola9d102062014-12-16 01:43:41 +0000212 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000213 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000214 if (Buffer.startswith(ThinMagic)) {
215 IsThin = true;
216 } else if (Buffer.startswith(Magic)) {
217 IsThin = false;
218 } else {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000219 ec = object_error::invalid_file_type;
220 return;
221 }
222
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000223 // Get the special members.
Rafael Espindola23a97502014-01-21 16:09:45 +0000224 child_iterator i = child_begin(false);
225 child_iterator e = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000226
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000227 if (i == e) {
Rui Ueyama7d099192015-06-09 15:20:42 +0000228 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000229 return;
230 }
231
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000232 StringRef Name = i->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000233
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000234 // Below is the pattern that is used to figure out the archive format
235 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000236 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000237 // Second member : // (may exist, if it exists, points to the string table)
238 // Note : The string table is used if the filename exceeds 15 characters
239 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000240 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
241 // There is no string table, if the filename exceeds 15 characters or has a
242 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000243 // of the filename that needs to be read after the archive header
244 // COFF archive format
245 // First member : /
246 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000247 // Third member : // (may exist, if it exists, contains the string table)
248 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
249 // even if the string table is empty. However, lib.exe does not in fact
250 // seem to create the third member if there's no member whose filename
251 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000252
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000253 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000254 Format = K_BSD;
255 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000256 ++i;
257 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000258 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000259 return;
260 }
261
Rafael Espindola55509922013-07-10 22:07:59 +0000262 if (Name.startswith("#1/")) {
263 Format = K_BSD;
264 // We know this is BSD, so getName will work since there is no string table.
Rafael Espindolaae460022014-06-16 16:08:36 +0000265 ErrorOr<StringRef> NameOrErr = i->getName();
266 ec = NameOrErr.getError();
Rafael Espindola55509922013-07-10 22:07:59 +0000267 if (ec)
268 return;
Rafael Espindolaae460022014-06-16 16:08:36 +0000269 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000270 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola55509922013-07-10 22:07:59 +0000271 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000272 ++i;
273 }
274 FirstRegular = i;
Rafael Espindola55509922013-07-10 22:07:59 +0000275 return;
276 }
277
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000278 // MIPS 64-bit ELF archives use a special format of a symbol table.
279 // This format is marked by `ar_name` field equals to "/SYM64/".
280 // For detailed description see page 96 in the following document:
281 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
282
283 bool has64SymTable = false;
284 if (Name == "/" || Name == "/SYM64/") {
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000285 SymbolTable = i;
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000286 if (Name == "/SYM64/")
287 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000288
289 ++i;
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000290 if (i == e) {
291 ec = object_error::parse_failed;
292 return;
293 }
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000294 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000295 }
296
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000297 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000298 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000299 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000300 ++i;
301 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000302 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000303 return;
304 }
305
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000306 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000307 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000308 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000309 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000310 return;
311 }
312
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000313 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000314 ec = object_error::parse_failed;
315 return;
316 }
317
318 Format = K_COFF;
319 SymbolTable = i;
320
321 ++i;
322 if (i == e) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000323 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000324 ec = std::error_code();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000325 return;
326 }
327
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000328 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000329
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000330 if (Name == "//") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000331 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000332 ++i;
333 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000334
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000335 FirstRegular = i;
Rui Ueyama7d099192015-06-09 15:20:42 +0000336 ec = std::error_code();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000337}
338
Rafael Espindola23a97502014-01-21 16:09:45 +0000339Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000340 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000341 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000342
343 if (SkipInternal)
344 return FirstRegular;
345
Rafael Espindola48af1c22014-08-19 18:44:46 +0000346 const char *Loc = Data.getBufferStart() + strlen(Magic);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000347 Child c(this, Loc);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000348 return c;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000349}
350
Rafael Espindola23a97502014-01-21 16:09:45 +0000351Archive::child_iterator Archive::child_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000352 return Child(this, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000353}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000354
Rafael Espindolaae460022014-06-16 16:08:36 +0000355StringRef Archive::Symbol::getName() const {
356 return Parent->SymbolTable->getBuffer().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000357}
358
Rafael Espindolaae460022014-06-16 16:08:36 +0000359ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
Michael J. Spencer9718f452013-02-03 10:48:50 +0000360 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000361 const char *Offsets = Buf;
362 if (Parent->kind() == K_MIPS64)
363 Offsets += sizeof(uint64_t);
364 else
365 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000366 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000367 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000368 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000369 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000370 Offset = read64be(Offsets + SymbolIndex * 8);
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.
Rui Ueyama3206b792015-03-02 21:19:12 +0000378 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000379 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000380 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000381 uint32_t MemberCount = read32le(Buf);
382 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000383
Rui Ueyama3206b792015-03-02 21:19:12 +0000384 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000385 if (SymbolIndex >= SymbolCount)
386 return object_error::parse_failed;
387
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000388 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000389 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000390
391 // Get the index of the offset in the file member offset table for this
392 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000393 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000394 // Subtract 1 since OffsetIndex is 1 based.
395 --OffsetIndex;
396
397 if (OffsetIndex >= MemberCount)
398 return object_error::parse_failed;
399
Rui Ueyama3206b792015-03-02 21:19:12 +0000400 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000401 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000402
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000403 const char *Loc = Parent->getData().begin() + Offset;
Rafael Espindolaae460022014-06-16 16:08:36 +0000404 child_iterator Iter(Child(Parent, Loc));
405 return Iter;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000406}
407
408Archive::Symbol Archive::Symbol::getNext() const {
409 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000410 if (Parent->kind() == K_BSD) {
411 // t.StringIndex is an offset from the start of the __.SYMDEF or
412 // "__.SYMDEF SORTED" member into the string table for the ranlib
413 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
414 // offset in the string table for t.SymbolIndex+1 we subtract the
415 // its offset from the start of the string table for t.SymbolIndex
416 // and add the offset of the string table for t.SymbolIndex+1.
417
418 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
419 // which is the number of bytes of ranlib structs that follow. The ranlib
420 // structs are a pair of uint32_t's the first being a string table offset
421 // and the second being the offset into the archive of the member that
422 // define the symbol. After that the next uint32_t is the byte count of
423 // the string table followed by the string table.
424 const char *Buf = Parent->SymbolTable->getBuffer().begin();
425 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000426 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000427 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
428 // don't change the t.StringIndex as we don't want to reference a ranlib
429 // past RanlibCount.
430 if (t.SymbolIndex + 1 < RanlibCount) {
431 const char *Ranlibs = Buf + 4;
432 uint32_t CurRanStrx = 0;
433 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000434 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
435 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000436 t.StringIndex -= CurRanStrx;
437 t.StringIndex += NextRanStrx;
438 }
439 } else {
440 // Go to one past next null.
441 t.StringIndex =
442 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
443 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000444 ++t.SymbolIndex;
445 return t;
446}
447
Rafael Espindola23a97502014-01-21 16:09:45 +0000448Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000449 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000450 return symbol_iterator(Symbol(this, 0, 0));
451
Michael J. Spencer9718f452013-02-03 10:48:50 +0000452 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000453 if (kind() == K_GNU) {
454 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000455 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000456 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000457 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000458 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000459 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000460 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000461 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
462 // which is the number of bytes of ranlib structs that follow. The ranlib
463 // structs are a pair of uint32_t's the first being a string table offset
464 // and the second being the offset into the archive of the member that
465 // define the symbol. After that the next uint32_t is the byte count of
466 // the string table followed by the string table.
467 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000468 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000469 const char *ranlibs = buf + 4;
470 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000471 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000472 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
473 // Skip the byte count of the string table.
474 buf += sizeof(uint32_t);
475 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000476 } else {
477 uint32_t member_count = 0;
478 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000479 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000480 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000481 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000482 buf += 4 + (symbol_count * 2); // Skip indices.
483 }
Michael J. Spencer9718f452013-02-03 10:48:50 +0000484 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000485 return symbol_iterator(Symbol(this, 0, string_start_offset));
486}
487
Rafael Espindola23a97502014-01-21 16:09:45 +0000488Archive::symbol_iterator Archive::symbol_end() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000489 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000490 return symbol_iterator(Symbol(this, 0, 0));
Rui Ueyama407e0972015-05-26 16:20:40 +0000491 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
492}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000493
Rui Ueyama407e0972015-05-26 16:20:40 +0000494uint32_t Archive::getNumberOfSymbols() const {
Michael J. Spencer9718f452013-02-03 10:48:50 +0000495 const char *buf = SymbolTable->getBuffer().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000496 if (kind() == K_GNU)
497 return read32be(buf);
498 if (kind() == K_MIPS64)
499 return read64be(buf);
500 if (kind() == K_BSD)
501 return read32le(buf) / 8;
502 uint32_t member_count = 0;
503 member_count = read32le(buf);
504 buf += 4 + (member_count * 4); // Skip offsets.
505 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000506}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000507
508Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000509 Archive::symbol_iterator bs = symbol_begin();
510 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000511
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000512 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000513 StringRef SymName = bs->getName();
514 if (SymName == name) {
515 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
516 // FIXME: Should we really eat the error?
517 if (ResultOrErr.getError())
Rafael Espindola23a97502014-01-21 16:09:45 +0000518 return child_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000519 return ResultOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000520 }
521 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000522 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000523}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000524
525bool Archive::hasSymbolTable() const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000526 return SymbolTable != child_end();
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000527}