comparison lib/Object/Archive.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===- Archive.cpp - ar File Format implementation ------------------------===// 1 //===- Archive.cpp - ar File Format implementation ------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file defines the ArchiveObjectFile class. 9 // This file defines the ArchiveObjectFile class.
11 // 10 //
173 Parent->getData().data(); 172 Parent->getData().data();
174 return malformedError("long name offset " + Twine(StringOffset) + " past " 173 return malformedError("long name offset " + Twine(StringOffset) + " past "
175 "the end of the string table for archive member " 174 "the end of the string table for archive member "
176 "header at offset " + Twine(ArchiveOffset)); 175 "header at offset " + Twine(ArchiveOffset));
177 } 176 }
178 const char *addr = Parent->getStringTable().begin() + StringOffset;
179 177
180 // GNU long file names end with a "/\n". 178 // GNU long file names end with a "/\n".
181 if (Parent->kind() == Archive::K_GNU || 179 if (Parent->kind() == Archive::K_GNU ||
182 Parent->kind() == Archive::K_GNU64) { 180 Parent->kind() == Archive::K_GNU64) {
183 StringRef::size_type End = StringRef(addr).find('\n'); 181 size_t End = Parent->getStringTable().find('\n', /*From=*/StringOffset);
184 return StringRef(addr, End - 1); 182 if (End == StringRef::npos || End < 1 ||
185 } 183 Parent->getStringTable()[End - 1] != '/') {
186 return addr; 184 return malformedError("string table at long name offset " +
185 Twine(StringOffset) + "not terminated");
186 }
187 return Parent->getStringTable().slice(StringOffset, End - 1);
188 }
189 return Parent->getStringTable().begin() + StringOffset;
187 } 190 }
188 191
189 if (Name.startswith("#1/")) { 192 if (Name.startswith("#1/")) {
190 uint64_t NameLength; 193 uint64_t NameLength;
191 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) { 194 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
218 221
219 // It's a simple name. 222 // It's a simple name.
220 return Name.drop_back(1); 223 return Name.drop_back(1);
221 } 224 }
222 225
223 Expected<uint32_t> ArchiveMemberHeader::getSize() const { 226 Expected<uint64_t> ArchiveMemberHeader::getSize() const {
224 uint32_t Ret; 227 uint64_t Ret;
225 if (StringRef(ArMemHdr->Size, 228 if (StringRef(ArMemHdr->Size,
226 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) { 229 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
227 std::string Buf; 230 std::string Buf;
228 raw_string_ostream OS(Buf); 231 raw_string_ostream OS(Buf);
229 OS.write_escaped(StringRef(ArMemHdr->Size, 232 OS.write_escaped(StringRef(ArMemHdr->Size,
506 if (!NameOrErr) 509 if (!NameOrErr)
507 return NameOrErr.takeError(); 510 return NameOrErr.takeError();
508 StringRef Name = NameOrErr.get(); 511 StringRef Name = NameOrErr.get();
509 Expected<StringRef> Buf = getBuffer(); 512 Expected<StringRef> Buf = getBuffer();
510 if (!Buf) 513 if (!Buf)
511 return Buf.takeError(); 514 return createFileError(Name, Buf.takeError());
512 return MemoryBufferRef(*Buf, Name); 515 return MemoryBufferRef(*Buf, Name);
513 } 516 }
514 517
515 Expected<std::unique_ptr<Binary>> 518 Expected<std::unique_ptr<Binary>>
516 Archive::Child::getAsBinary(LLVMContext *Context) const { 519 Archive::Child::getAsBinary(LLVMContext *Context) const {
773 bool SkipInternal) const { 776 bool SkipInternal) const {
774 if (isEmpty()) 777 if (isEmpty())
775 return child_end(); 778 return child_end();
776 779
777 if (SkipInternal) 780 if (SkipInternal)
778 return child_iterator(Child(this, FirstRegularData, 781 return child_iterator::itr(
779 FirstRegularStartOfFile), 782 Child(this, FirstRegularData, FirstRegularStartOfFile), Err);
780 &Err);
781 783
782 const char *Loc = Data.getBufferStart() + strlen(Magic); 784 const char *Loc = Data.getBufferStart() + strlen(Magic);
783 Child C(this, Loc, &Err); 785 Child C(this, Loc, &Err);
784 if (Err) 786 if (Err)
785 return child_end(); 787 return child_end();
786 return child_iterator(C, &Err); 788 return child_iterator::itr(C, Err);
787 } 789 }
788 790
789 Archive::child_iterator Archive::child_end() const { 791 Archive::child_iterator Archive::child_end() const {
790 return child_iterator(Child(nullptr, nullptr, nullptr), nullptr); 792 return child_iterator::end(Child(nullptr, nullptr, nullptr));
791 } 793 }
792 794
793 StringRef Archive::Symbol::getName() const { 795 StringRef Archive::Symbol::getName() const {
794 return Parent->getSymbolTable().begin() + StringIndex; 796 return Parent->getSymbolTable().begin() + StringIndex;
795 } 797 }