Simplify the handling of the archive string table.
[oota-llvm.git] / lib / Object / Archive.cpp
1 //===- 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"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
21
22 using namespace llvm;
23 using namespace object;
24 using namespace llvm::support::endian;
25
26 static const char *const Magic = "!<arch>\n";
27 static const char *const ThinMagic = "!<thin>\n";
28
29 void Archive::anchor() { }
30
31 StringRef 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
46 ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const {
47   uint32_t Ret;
48   if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
49     return object_error::parse_failed;
50   return Ret;
51 }
52
53 sys::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
60 sys::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
71 unsigned 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
78 unsigned 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
85 Archive::Child::Child(const Archive *Parent, const char *Start)
86     : Parent(Parent) {
87   if (!Start)
88     return;
89
90   uint64_t Size = sizeof(ArchiveMemberHeader);
91   Data = StringRef(Start, Size);
92   if (!isThinMember()) {
93     Size += getRawSize();
94     Data = StringRef(Start, Size);
95   }
96
97   // Setup StartOfFile and PaddingBytes.
98   StartOfFile = sizeof(ArchiveMemberHeader);
99   // Don't include attached name.
100   StringRef Name = getRawName();
101   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
109 uint64_t Archive::Child::getSize() const {
110   if (Parent->IsThin) {
111     ErrorOr<uint32_t> Size = getHeader()->getSize();
112     if (Size.getError())
113       return 0;
114     return Size.get();
115   }
116   return Data.size() - StartOfFile;
117 }
118
119 uint64_t Archive::Child::getRawSize() const {
120   ErrorOr<uint32_t> Size = getHeader()->getSize();
121   if (Size.getError())
122     return 0;
123   return Size.get();
124 }
125
126 bool Archive::Child::isThinMember() const {
127   StringRef Name = getHeader()->getName();
128   return Parent->IsThin && Name != "/" && Name != "//";
129 }
130
131 ErrorOr<StringRef> Archive::Child::getBuffer() const {
132   if (!isThinMember())
133     return StringRef(Data.data() + StartOfFile, getSize());
134   ErrorOr<StringRef> Name = getName();
135   if (std::error_code EC = Name.getError())
136     return EC;
137   SmallString<128> FullName = sys::path::parent_path(
138       Parent->getMemoryBufferRef().getBufferIdentifier());
139   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
147 Archive::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.
156   if (NextLoc >= Parent->Data.getBufferEnd())
157     return Child(Parent, nullptr);
158
159   return Child(Parent, NextLoc);
160 }
161
162 uint64_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
169 ErrorOr<StringRef> Archive::Child::getName() const {
170   StringRef name = getRawName();
171   // Check if it's a special name.
172   if (name[0] == '/') {
173     if (name.size() == 1) // Linker member.
174       return name;
175     if (name.size() == 2 && name[1] == '/') // String table.
176       return name;
177     // It's a long name.
178     // Get the offset.
179     std::size_t offset;
180     if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
181       llvm_unreachable("Long name offset is not an integer");
182
183     // Verify it.
184     if (offset >= Parent->StringTable.size())
185       return object_error::parse_failed;
186     const char *addr = Parent->StringTable.begin() + offset;
187
188     // GNU long file names end with a "/\n".
189     if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
190       StringRef::size_type End = StringRef(addr).find('\n');
191       return StringRef(addr, End - 1);
192     }
193     return StringRef(addr);
194   } else if (name.startswith("#1/")) {
195     uint64_t name_size;
196     if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
197       llvm_unreachable("Long name length is not an ingeter");
198     return Data.substr(sizeof(ArchiveMemberHeader), name_size)
199         .rtrim(StringRef("\0", 1));
200   }
201   // It's a simple name.
202   if (name[name.size() - 1] == '/')
203     return name.substr(0, name.size() - 1);
204   return name;
205 }
206
207 ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
208   ErrorOr<StringRef> NameOrErr = getName();
209   if (std::error_code EC = NameOrErr.getError())
210     return EC;
211   StringRef Name = NameOrErr.get();
212   ErrorOr<StringRef> Buf = getBuffer();
213   if (std::error_code EC = Buf.getError())
214     return EC;
215   return MemoryBufferRef(*Buf, Name);
216 }
217
218 ErrorOr<std::unique_ptr<Binary>>
219 Archive::Child::getAsBinary(LLVMContext *Context) const {
220   ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
221   if (std::error_code EC = BuffOrErr.getError())
222     return EC;
223
224   return createBinary(BuffOrErr.get(), Context);
225 }
226
227 ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
228   std::error_code EC;
229   std::unique_ptr<Archive> Ret(new Archive(Source, EC));
230   if (EC)
231     return EC;
232   return std::move(Ret);
233 }
234
235 Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
236     : Binary(Binary::ID_Archive, Source), SymbolTable(child_end()),
237       FirstRegular(child_end()) {
238   StringRef Buffer = Data.getBuffer();
239   // Check for sufficient magic.
240   if (Buffer.startswith(ThinMagic)) {
241     IsThin = true;
242   } else if (Buffer.startswith(Magic)) {
243     IsThin = false;
244   } else {
245     ec = object_error::invalid_file_type;
246     return;
247   }
248
249   // Get the special members.
250   child_iterator i = child_begin(false);
251   child_iterator e = child_end();
252
253   if (i == e) {
254     ec = std::error_code();
255     return;
256   }
257
258   StringRef Name = i->getRawName();
259
260   // Below is the pattern that is used to figure out the archive format
261   // GNU archive format
262   //  First member : / (may exist, if it exists, points to the symbol table )
263   //  Second member : // (may exist, if it exists, points to the string table)
264   //  Note : The string table is used if the filename exceeds 15 characters
265   // BSD archive format
266   //  First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
267   //  There is no string table, if the filename exceeds 15 characters or has a
268   //  embedded space, the filename has #1/<size>, The size represents the size
269   //  of the filename that needs to be read after the archive header
270   // COFF archive format
271   //  First member : /
272   //  Second member : / (provides a directory of symbols)
273   //  Third member : // (may exist, if it exists, contains the string table)
274   //  Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
275   //  even if the string table is empty. However, lib.exe does not in fact
276   //  seem to create the third member if there's no member whose filename
277   //  exceeds 15 characters. So the third member is optional.
278
279   if (Name == "__.SYMDEF") {
280     Format = K_BSD;
281     SymbolTable = i;
282     ++i;
283     FirstRegular = i;
284     ec = std::error_code();
285     return;
286   }
287
288   if (Name.startswith("#1/")) {
289     Format = K_BSD;
290     // We know this is BSD, so getName will work since there is no string table.
291     ErrorOr<StringRef> NameOrErr = i->getName();
292     ec = NameOrErr.getError();
293     if (ec)
294       return;
295     Name = NameOrErr.get();
296     if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
297       SymbolTable = i;
298       ++i;
299     }
300     FirstRegular = i;
301     return;
302   }
303
304   // MIPS 64-bit ELF archives use a special format of a symbol table.
305   // This format is marked by `ar_name` field equals to "/SYM64/".
306   // For detailed description see page 96 in the following document:
307   // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
308
309   bool has64SymTable = false;
310   if (Name == "/" || Name == "/SYM64/") {
311     SymbolTable = i;
312     if (Name == "/SYM64/")
313       has64SymTable = true;
314
315     ++i;
316     if (i == e) {
317       ec = std::error_code();
318       return;
319     }
320     Name = i->getRawName();
321   }
322
323   if (Name == "//") {
324     Format = has64SymTable ? K_MIPS64 : K_GNU;
325     // The string table is never an external member, so we just assert on the
326     // ErrorOr.
327     StringTable = *i->getBuffer();
328     ++i;
329     FirstRegular = i;
330     ec = std::error_code();
331     return;
332   }
333
334   if (Name[0] != '/') {
335     Format = has64SymTable ? K_MIPS64 : K_GNU;
336     FirstRegular = i;
337     ec = std::error_code();
338     return;
339   }
340
341   if (Name != "/") {
342     ec = object_error::parse_failed;
343     return;
344   }
345
346   Format = K_COFF;
347   SymbolTable = i;
348
349   ++i;
350   if (i == e) {
351     FirstRegular = i;
352     ec = std::error_code();
353     return;
354   }
355
356   Name = i->getRawName();
357
358   if (Name == "//") {
359     // The string table is never an external member, so we just assert on the
360     // ErrorOr.
361     StringTable = *i->getBuffer();
362     ++i;
363   }
364
365   FirstRegular = i;
366   ec = std::error_code();
367 }
368
369 Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
370   if (Data.getBufferSize() == 8) // empty archive.
371     return child_end();
372
373   if (SkipInternal)
374     return FirstRegular;
375
376   const char *Loc = Data.getBufferStart() + strlen(Magic);
377   Child c(this, Loc);
378   return c;
379 }
380
381 Archive::child_iterator Archive::child_end() const {
382   return Child(this, nullptr);
383 }
384
385 StringRef Archive::Symbol::getName() const {
386   return Parent->getSymbolTable().begin() + StringIndex;
387 }
388
389 ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
390   const char *Buf = Parent->getSymbolTable().begin();
391   const char *Offsets = Buf;
392   if (Parent->kind() == K_MIPS64)
393     Offsets += sizeof(uint64_t);
394   else
395     Offsets += sizeof(uint32_t);
396   uint32_t Offset = 0;
397   if (Parent->kind() == K_GNU) {
398     Offset = read32be(Offsets + SymbolIndex * 4);
399   } else if (Parent->kind() == K_MIPS64) {
400     Offset = read64be(Offsets + SymbolIndex * 8);
401   } else if (Parent->kind() == K_BSD) {
402     // The SymbolIndex is an index into the ranlib structs that start at
403     // Offsets (the first uint32_t is the number of bytes of the ranlib
404     // structs).  The ranlib structs are a pair of uint32_t's the first
405     // being a string table offset and the second being the offset into
406     // the archive of the member that defines the symbol.  Which is what
407     // is needed here.
408     Offset = read32le(Offsets + SymbolIndex * 8 + 4);
409   } else {
410     // Skip offsets.
411     uint32_t MemberCount = read32le(Buf);
412     Buf += MemberCount * 4 + 4;
413
414     uint32_t SymbolCount = read32le(Buf);
415     if (SymbolIndex >= SymbolCount)
416       return object_error::parse_failed;
417
418     // Skip SymbolCount to get to the indices table.
419     const char *Indices = Buf + 4;
420
421     // Get the index of the offset in the file member offset table for this
422     // symbol.
423     uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
424     // Subtract 1 since OffsetIndex is 1 based.
425     --OffsetIndex;
426
427     if (OffsetIndex >= MemberCount)
428       return object_error::parse_failed;
429
430     Offset = read32le(Offsets + OffsetIndex * 4);
431   }
432
433   const char *Loc = Parent->getData().begin() + Offset;
434   child_iterator Iter(Child(Parent, Loc));
435   return Iter;
436 }
437
438 Archive::Symbol Archive::Symbol::getNext() const {
439   Symbol t(*this);
440   if (Parent->kind() == K_BSD) {
441     // t.StringIndex is an offset from the start of the __.SYMDEF or
442     // "__.SYMDEF SORTED" member into the string table for the ranlib
443     // struct indexed by t.SymbolIndex .  To change t.StringIndex to the
444     // offset in the string table for t.SymbolIndex+1 we subtract the
445     // its offset from the start of the string table for t.SymbolIndex
446     // and add the offset of the string table for t.SymbolIndex+1.
447
448     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
449     // which is the number of bytes of ranlib structs that follow.  The ranlib
450     // structs are a pair of uint32_t's the first being a string table offset
451     // and the second being the offset into the archive of the member that
452     // define the symbol. After that the next uint32_t is the byte count of
453     // the string table followed by the string table.
454     const char *Buf = Parent->getSymbolTable().begin();
455     uint32_t RanlibCount = 0;
456     RanlibCount = read32le(Buf) / 8;
457     // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
458     // don't change the t.StringIndex as we don't want to reference a ranlib
459     // past RanlibCount.
460     if (t.SymbolIndex + 1 < RanlibCount) {
461       const char *Ranlibs = Buf + 4;
462       uint32_t CurRanStrx = 0;
463       uint32_t NextRanStrx = 0;
464       CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
465       NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
466       t.StringIndex -= CurRanStrx;
467       t.StringIndex += NextRanStrx;
468     }
469   } else {
470     // Go to one past next null.
471     t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
472   }
473   ++t.SymbolIndex;
474   return t;
475 }
476
477 Archive::symbol_iterator Archive::symbol_begin() const {
478   if (!hasSymbolTable())
479     return symbol_iterator(Symbol(this, 0, 0));
480
481   const char *buf = getSymbolTable().begin();
482   if (kind() == K_GNU) {
483     uint32_t symbol_count = 0;
484     symbol_count = read32be(buf);
485     buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
486   } else if (kind() == K_MIPS64) {
487     uint64_t symbol_count = read64be(buf);
488     buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
489   } else if (kind() == K_BSD) {
490     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
491     // which is the number of bytes of ranlib structs that follow.  The ranlib
492     // structs are a pair of uint32_t's the first being a string table offset
493     // and the second being the offset into the archive of the member that
494     // define the symbol. After that the next uint32_t is the byte count of
495     // the string table followed by the string table.
496     uint32_t ranlib_count = 0;
497     ranlib_count = read32le(buf) / 8;
498     const char *ranlibs = buf + 4;
499     uint32_t ran_strx = 0;
500     ran_strx = read32le(ranlibs);
501     buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
502     // Skip the byte count of the string table.
503     buf += sizeof(uint32_t);
504     buf += ran_strx;
505   } else {
506     uint32_t member_count = 0;
507     uint32_t symbol_count = 0;
508     member_count = read32le(buf);
509     buf += 4 + (member_count * 4); // Skip offsets.
510     symbol_count = read32le(buf);
511     buf += 4 + (symbol_count * 2); // Skip indices.
512   }
513   uint32_t string_start_offset = buf - getSymbolTable().begin();
514   return symbol_iterator(Symbol(this, 0, string_start_offset));
515 }
516
517 Archive::symbol_iterator Archive::symbol_end() const {
518   return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
519 }
520
521 uint32_t Archive::getNumberOfSymbols() const {
522   if (!hasSymbolTable())
523     return 0;
524   const char *buf = getSymbolTable().begin();
525   if (kind() == K_GNU)
526     return read32be(buf);
527   if (kind() == K_MIPS64)
528     return read64be(buf);
529   if (kind() == K_BSD)
530     return read32le(buf) / 8;
531   uint32_t member_count = 0;
532   member_count = read32le(buf);
533   buf += 4 + (member_count * 4); // Skip offsets.
534   return read32le(buf);
535 }
536
537 Archive::child_iterator Archive::findSym(StringRef name) const {
538   Archive::symbol_iterator bs = symbol_begin();
539   Archive::symbol_iterator es = symbol_end();
540
541   for (; bs != es; ++bs) {
542     StringRef SymName = bs->getName();
543     if (SymName == name) {
544       ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
545       // FIXME: Should we really eat the error?
546       if (ResultOrErr.getError())
547         return child_end();
548       return ResultOrErr.get();
549     }
550   }
551   return child_end();
552 }
553
554 bool Archive::hasSymbolTable() const {
555   return SymbolTable != child_end();
556 }