Archive.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. //===- Archive.cpp - ar File Format implementation ------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the ArchiveObjectFile class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/Archive.h"
  13. #include "llvm/ADT/Optional.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/Object/Binary.h"
  18. #include "llvm/Object/Error.h"
  19. #include "llvm/Support/Chrono.h"
  20. #include "llvm/Support/Endian.h"
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/ErrorOr.h"
  23. #include "llvm/Support/FileSystem.h"
  24. #include "llvm/Support/MathExtras.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. #include "llvm/Support/Path.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <cstddef>
  31. #include <cstdint>
  32. #include <cstring>
  33. #include <memory>
  34. #include <string>
  35. #include <system_error>
  36. using namespace llvm;
  37. using namespace object;
  38. using namespace llvm::support::endian;
  39. void Archive::anchor() {}
  40. static Error malformedError(Twine Msg) {
  41. std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
  42. return make_error<GenericBinaryError>(std::move(StringMsg),
  43. object_error::parse_failed);
  44. }
  45. static Error
  46. createMemberHeaderParseError(const AbstractArchiveMemberHeader *ArMemHeader,
  47. const char *RawHeaderPtr, uint64_t Size) {
  48. StringRef Msg("remaining size of archive too small for next archive "
  49. "member header ");
  50. Expected<StringRef> NameOrErr = ArMemHeader->getName(Size);
  51. if (NameOrErr)
  52. return malformedError(Msg + "for " + *NameOrErr);
  53. consumeError(NameOrErr.takeError());
  54. uint64_t Offset = RawHeaderPtr - ArMemHeader->Parent->getData().data();
  55. return malformedError(Msg + "at offset " + Twine(Offset));
  56. }
  57. template <class T, std::size_t N>
  58. StringRef getFieldRawString(const T (&Field)[N]) {
  59. return StringRef(Field, N).rtrim(" ");
  60. }
  61. template <class T>
  62. StringRef CommonArchiveMemberHeader<T>::getRawAccessMode() const {
  63. return getFieldRawString(ArMemHdr->AccessMode);
  64. }
  65. template <class T>
  66. StringRef CommonArchiveMemberHeader<T>::getRawLastModified() const {
  67. return getFieldRawString(ArMemHdr->LastModified);
  68. }
  69. template <class T> StringRef CommonArchiveMemberHeader<T>::getRawUID() const {
  70. return getFieldRawString(ArMemHdr->UID);
  71. }
  72. template <class T> StringRef CommonArchiveMemberHeader<T>::getRawGID() const {
  73. return getFieldRawString(ArMemHdr->GID);
  74. }
  75. template <class T> uint64_t CommonArchiveMemberHeader<T>::getOffset() const {
  76. return reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
  77. }
  78. template class object::CommonArchiveMemberHeader<UnixArMemHdrType>;
  79. template class object::CommonArchiveMemberHeader<BigArMemHdrType>;
  80. ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
  81. const char *RawHeaderPtr,
  82. uint64_t Size, Error *Err)
  83. : CommonArchiveMemberHeader<UnixArMemHdrType>(
  84. Parent, reinterpret_cast<const UnixArMemHdrType *>(RawHeaderPtr)) {
  85. if (RawHeaderPtr == nullptr)
  86. return;
  87. ErrorAsOutParameter ErrAsOutParam(Err);
  88. if (Size < getSizeOf()) {
  89. *Err = createMemberHeaderParseError(this, RawHeaderPtr, Size);
  90. return;
  91. }
  92. if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
  93. if (Err) {
  94. std::string Buf;
  95. raw_string_ostream OS(Buf);
  96. OS.write_escaped(
  97. StringRef(ArMemHdr->Terminator, sizeof(ArMemHdr->Terminator)));
  98. OS.flush();
  99. std::string Msg("terminator characters in archive member \"" + Buf +
  100. "\" not the correct \"`\\n\" values for the archive "
  101. "member header ");
  102. Expected<StringRef> NameOrErr = getName(Size);
  103. if (!NameOrErr) {
  104. consumeError(NameOrErr.takeError());
  105. uint64_t Offset = RawHeaderPtr - Parent->getData().data();
  106. *Err = malformedError(Msg + "at offset " + Twine(Offset));
  107. } else
  108. *Err = malformedError(Msg + "for " + NameOrErr.get());
  109. }
  110. return;
  111. }
  112. }
  113. BigArchiveMemberHeader::BigArchiveMemberHeader(const Archive *Parent,
  114. const char *RawHeaderPtr,
  115. uint64_t Size, Error *Err)
  116. : CommonArchiveMemberHeader<BigArMemHdrType>(
  117. Parent, reinterpret_cast<const BigArMemHdrType *>(RawHeaderPtr)) {
  118. if (RawHeaderPtr == nullptr)
  119. return;
  120. ErrorAsOutParameter ErrAsOutParam(Err);
  121. if (Size < getSizeOf())
  122. *Err = createMemberHeaderParseError(this, RawHeaderPtr, Size);
  123. }
  124. // This gets the raw name from the ArMemHdr->Name field and checks that it is
  125. // valid for the kind of archive. If it is not valid it returns an Error.
  126. Expected<StringRef> ArchiveMemberHeader::getRawName() const {
  127. char EndCond;
  128. auto Kind = Parent->kind();
  129. if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) {
  130. if (ArMemHdr->Name[0] == ' ') {
  131. uint64_t Offset =
  132. reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
  133. return malformedError("name contains a leading space for archive member "
  134. "header at offset " +
  135. Twine(Offset));
  136. }
  137. EndCond = ' ';
  138. } else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
  139. EndCond = ' ';
  140. else
  141. EndCond = '/';
  142. StringRef::size_type end =
  143. StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
  144. if (end == StringRef::npos)
  145. end = sizeof(ArMemHdr->Name);
  146. assert(end <= sizeof(ArMemHdr->Name) && end > 0);
  147. // Don't include the EndCond if there is one.
  148. return StringRef(ArMemHdr->Name, end);
  149. }
  150. Expected<uint64_t>
  151. getArchiveMemberDecField(Twine FieldName, const StringRef RawField,
  152. const Archive *Parent,
  153. const AbstractArchiveMemberHeader *MemHeader) {
  154. uint64_t Value;
  155. if (RawField.getAsInteger(10, Value)) {
  156. uint64_t Offset = MemHeader->getOffset();
  157. return malformedError("characters in " + FieldName +
  158. " field in archive member header are not "
  159. "all decimal numbers: '" +
  160. RawField +
  161. "' for the archive "
  162. "member header at offset " +
  163. Twine(Offset));
  164. }
  165. return Value;
  166. }
  167. Expected<uint64_t>
  168. getArchiveMemberOctField(Twine FieldName, const StringRef RawField,
  169. const Archive *Parent,
  170. const AbstractArchiveMemberHeader *MemHeader) {
  171. uint64_t Value;
  172. if (RawField.getAsInteger(8, Value)) {
  173. uint64_t Offset = MemHeader->getOffset();
  174. return malformedError("characters in " + FieldName +
  175. " field in archive member header are not "
  176. "all octal numbers: '" +
  177. RawField +
  178. "' for the archive "
  179. "member header at offset " +
  180. Twine(Offset));
  181. }
  182. return Value;
  183. }
  184. Expected<StringRef> BigArchiveMemberHeader::getRawName() const {
  185. Expected<uint64_t> NameLenOrErr = getArchiveMemberDecField(
  186. "NameLen", getFieldRawString(ArMemHdr->NameLen), Parent, this);
  187. if (!NameLenOrErr)
  188. // TODO: Out-of-line.
  189. return NameLenOrErr.takeError();
  190. uint64_t NameLen = NameLenOrErr.get();
  191. // If the name length is odd, pad with '\0' to get an even length. After
  192. // padding, there is the name terminator "`\n".
  193. uint64_t NameLenWithPadding = alignTo(NameLen, 2);
  194. StringRef NameTerminator = "`\n";
  195. StringRef NameStringWithNameTerminator =
  196. StringRef(ArMemHdr->Name, NameLenWithPadding + NameTerminator.size());
  197. if (!NameStringWithNameTerminator.endswith(NameTerminator)) {
  198. uint64_t Offset =
  199. reinterpret_cast<const char *>(ArMemHdr->Name + NameLenWithPadding) -
  200. Parent->getData().data();
  201. // TODO: Out-of-line.
  202. return malformedError(
  203. "name does not have name terminator \"`\\n\" for archive member"
  204. "header at offset " +
  205. Twine(Offset));
  206. }
  207. return StringRef(ArMemHdr->Name, NameLen);
  208. }
  209. // member including the header, so the size of any name following the header
  210. // is checked to make sure it does not overflow.
  211. Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
  212. // This can be called from the ArchiveMemberHeader constructor when the
  213. // archive header is truncated to produce an error message with the name.
  214. // Make sure the name field is not truncated.
  215. if (Size < offsetof(UnixArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
  216. uint64_t ArchiveOffset =
  217. reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
  218. return malformedError("archive header truncated before the name field "
  219. "for archive member header at offset " +
  220. Twine(ArchiveOffset));
  221. }
  222. // The raw name itself can be invalid.
  223. Expected<StringRef> NameOrErr = getRawName();
  224. if (!NameOrErr)
  225. return NameOrErr.takeError();
  226. StringRef Name = NameOrErr.get();
  227. // Check if it's a special name.
  228. if (Name[0] == '/') {
  229. if (Name.size() == 1) // Linker member.
  230. return Name;
  231. if (Name.size() == 2 && Name[1] == '/') // String table.
  232. return Name;
  233. // It's a long name.
  234. // Get the string table offset.
  235. std::size_t StringOffset;
  236. if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
  237. std::string Buf;
  238. raw_string_ostream OS(Buf);
  239. OS.write_escaped(Name.substr(1).rtrim(' '));
  240. OS.flush();
  241. uint64_t ArchiveOffset =
  242. reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
  243. return malformedError("long name offset characters after the '/' are "
  244. "not all decimal numbers: '" +
  245. Buf + "' for archive member header at offset " +
  246. Twine(ArchiveOffset));
  247. }
  248. // Verify it.
  249. if (StringOffset >= Parent->getStringTable().size()) {
  250. uint64_t ArchiveOffset =
  251. reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
  252. return malformedError("long name offset " + Twine(StringOffset) +
  253. " past the end of the string table for archive "
  254. "member header at offset " +
  255. Twine(ArchiveOffset));
  256. }
  257. // GNU long file names end with a "/\n".
  258. if (Parent->kind() == Archive::K_GNU ||
  259. Parent->kind() == Archive::K_GNU64) {
  260. size_t End = Parent->getStringTable().find('\n', /*From=*/StringOffset);
  261. if (End == StringRef::npos || End < 1 ||
  262. Parent->getStringTable()[End - 1] != '/') {
  263. return malformedError("string table at long name offset " +
  264. Twine(StringOffset) + "not terminated");
  265. }
  266. return Parent->getStringTable().slice(StringOffset, End - 1);
  267. }
  268. return Parent->getStringTable().begin() + StringOffset;
  269. }
  270. if (Name.startswith("#1/")) {
  271. uint64_t NameLength;
  272. if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
  273. std::string Buf;
  274. raw_string_ostream OS(Buf);
  275. OS.write_escaped(Name.substr(3).rtrim(' '));
  276. OS.flush();
  277. uint64_t ArchiveOffset =
  278. reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
  279. return malformedError("long name length characters after the #1/ are "
  280. "not all decimal numbers: '" +
  281. Buf + "' for archive member header at offset " +
  282. Twine(ArchiveOffset));
  283. }
  284. if (getSizeOf() + NameLength > Size) {
  285. uint64_t ArchiveOffset =
  286. reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
  287. return malformedError("long name length: " + Twine(NameLength) +
  288. " extends past the end of the member or archive "
  289. "for archive member header at offset " +
  290. Twine(ArchiveOffset));
  291. }
  292. return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
  293. NameLength)
  294. .rtrim('\0');
  295. }
  296. // It is not a long name so trim the blanks at the end of the name.
  297. if (Name[Name.size() - 1] != '/')
  298. return Name.rtrim(' ');
  299. // It's a simple name.
  300. return Name.drop_back(1);
  301. }
  302. Expected<StringRef> BigArchiveMemberHeader::getName(uint64_t Size) const {
  303. return getRawName();
  304. }
  305. Expected<uint64_t> ArchiveMemberHeader::getSize() const {
  306. return getArchiveMemberDecField("size", getFieldRawString(ArMemHdr->Size),
  307. Parent, this);
  308. }
  309. Expected<uint64_t> BigArchiveMemberHeader::getSize() const {
  310. Expected<uint64_t> SizeOrErr = getArchiveMemberDecField(
  311. "size", getFieldRawString(ArMemHdr->Size), Parent, this);
  312. if (!SizeOrErr)
  313. return SizeOrErr.takeError();
  314. Expected<uint64_t> NameLenOrErr = getRawNameSize();
  315. if (!NameLenOrErr)
  316. return NameLenOrErr.takeError();
  317. return *SizeOrErr + alignTo(*NameLenOrErr, 2);
  318. }
  319. Expected<uint64_t> BigArchiveMemberHeader::getRawNameSize() const {
  320. return getArchiveMemberDecField(
  321. "NameLen", getFieldRawString(ArMemHdr->NameLen), Parent, this);
  322. }
  323. Expected<uint64_t> BigArchiveMemberHeader::getNextOffset() const {
  324. return getArchiveMemberDecField(
  325. "NextOffset", getFieldRawString(ArMemHdr->NextOffset), Parent, this);
  326. }
  327. Expected<sys::fs::perms> AbstractArchiveMemberHeader::getAccessMode() const {
  328. Expected<uint64_t> AccessModeOrErr =
  329. getArchiveMemberOctField("AccessMode", getRawAccessMode(), Parent, this);
  330. if (!AccessModeOrErr)
  331. return AccessModeOrErr.takeError();
  332. return static_cast<sys::fs::perms>(*AccessModeOrErr);
  333. }
  334. Expected<sys::TimePoint<std::chrono::seconds>>
  335. AbstractArchiveMemberHeader::getLastModified() const {
  336. Expected<uint64_t> SecondsOrErr = getArchiveMemberDecField(
  337. "LastModified", getRawLastModified(), Parent, this);
  338. if (!SecondsOrErr)
  339. return SecondsOrErr.takeError();
  340. return sys::toTimePoint(*SecondsOrErr);
  341. }
  342. Expected<unsigned> AbstractArchiveMemberHeader::getUID() const {
  343. StringRef User = getRawUID();
  344. if (User.empty())
  345. return 0;
  346. return getArchiveMemberDecField("UID", User, Parent, this);
  347. }
  348. Expected<unsigned> AbstractArchiveMemberHeader::getGID() const {
  349. StringRef Group = getRawGID();
  350. if (Group.empty())
  351. return 0;
  352. return getArchiveMemberDecField("GID", Group, Parent, this);
  353. }
  354. Expected<bool> ArchiveMemberHeader::isThin() const {
  355. Expected<StringRef> NameOrErr = getRawName();
  356. if (!NameOrErr)
  357. return NameOrErr.takeError();
  358. StringRef Name = NameOrErr.get();
  359. return Parent->isThin() && Name != "/" && Name != "//" && Name != "/SYM64/";
  360. }
  361. Expected<const char *> ArchiveMemberHeader::getNextChildLoc() const {
  362. uint64_t Size = getSizeOf();
  363. Expected<bool> isThinOrErr = isThin();
  364. if (!isThinOrErr)
  365. return isThinOrErr.takeError();
  366. bool isThin = isThinOrErr.get();
  367. if (!isThin) {
  368. Expected<uint64_t> MemberSize = getSize();
  369. if (!MemberSize)
  370. return MemberSize.takeError();
  371. Size += MemberSize.get();
  372. }
  373. // If Size is odd, add 1 to make it even.
  374. const char *NextLoc =
  375. reinterpret_cast<const char *>(ArMemHdr) + alignTo(Size, 2);
  376. if (NextLoc == Parent->getMemoryBufferRef().getBufferEnd())
  377. return nullptr;
  378. return NextLoc;
  379. }
  380. Expected<const char *> BigArchiveMemberHeader::getNextChildLoc() const {
  381. if (getOffset() ==
  382. static_cast<const BigArchive *>(Parent)->getLastChildOffset())
  383. return nullptr;
  384. Expected<uint64_t> NextOffsetOrErr = getNextOffset();
  385. if (!NextOffsetOrErr)
  386. return NextOffsetOrErr.takeError();
  387. return Parent->getData().data() + NextOffsetOrErr.get();
  388. }
  389. Archive::Child::Child(const Archive *Parent, StringRef Data,
  390. uint16_t StartOfFile)
  391. : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {
  392. Header = Parent->createArchiveMemberHeader(Data.data(), Data.size(), nullptr);
  393. }
  394. Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
  395. : Parent(Parent) {
  396. if (!Start) {
  397. Header = nullptr;
  398. return;
  399. }
  400. Header = Parent->createArchiveMemberHeader(
  401. Start,
  402. Parent ? Parent->getData().size() - (Start - Parent->getData().data())
  403. : 0,
  404. Err);
  405. // If we are pointed to real data, Start is not a nullptr, then there must be
  406. // a non-null Err pointer available to report malformed data on. Only in
  407. // the case sentinel value is being constructed is Err is permitted to be a
  408. // nullptr.
  409. assert(Err && "Err can't be nullptr if Start is not a nullptr");
  410. ErrorAsOutParameter ErrAsOutParam(Err);
  411. // If there was an error in the construction of the Header
  412. // then just return with the error now set.
  413. if (*Err)
  414. return;
  415. uint64_t Size = Header->getSizeOf();
  416. Data = StringRef(Start, Size);
  417. Expected<bool> isThinOrErr = isThinMember();
  418. if (!isThinOrErr) {
  419. *Err = isThinOrErr.takeError();
  420. return;
  421. }
  422. bool isThin = isThinOrErr.get();
  423. if (!isThin) {
  424. Expected<uint64_t> MemberSize = getRawSize();
  425. if (!MemberSize) {
  426. *Err = MemberSize.takeError();
  427. return;
  428. }
  429. Size += MemberSize.get();
  430. Data = StringRef(Start, Size);
  431. }
  432. // Setup StartOfFile and PaddingBytes.
  433. StartOfFile = Header->getSizeOf();
  434. // Don't include attached name.
  435. Expected<StringRef> NameOrErr = getRawName();
  436. if (!NameOrErr) {
  437. *Err = NameOrErr.takeError();
  438. return;
  439. }
  440. StringRef Name = NameOrErr.get();
  441. if (Parent->kind() == Archive::K_AIXBIG) {
  442. // The actual start of the file is after the name and any necessary
  443. // even-alignment padding.
  444. StartOfFile += ((Name.size() + 1) >> 1) << 1;
  445. } else if (Name.startswith("#1/")) {
  446. uint64_t NameSize;
  447. StringRef RawNameSize = Name.substr(3).rtrim(' ');
  448. if (RawNameSize.getAsInteger(10, NameSize)) {
  449. uint64_t Offset = Start - Parent->getData().data();
  450. *Err = malformedError("long name length characters after the #1/ are "
  451. "not all decimal numbers: '" +
  452. RawNameSize +
  453. "' for archive member header at offset " +
  454. Twine(Offset));
  455. return;
  456. }
  457. StartOfFile += NameSize;
  458. }
  459. }
  460. Expected<uint64_t> Archive::Child::getSize() const {
  461. if (Parent->IsThin)
  462. return Header->getSize();
  463. return Data.size() - StartOfFile;
  464. }
  465. Expected<uint64_t> Archive::Child::getRawSize() const {
  466. return Header->getSize();
  467. }
  468. Expected<bool> Archive::Child::isThinMember() const { return Header->isThin(); }
  469. Expected<std::string> Archive::Child::getFullName() const {
  470. Expected<bool> isThin = isThinMember();
  471. if (!isThin)
  472. return isThin.takeError();
  473. assert(isThin.get());
  474. Expected<StringRef> NameOrErr = getName();
  475. if (!NameOrErr)
  476. return NameOrErr.takeError();
  477. StringRef Name = *NameOrErr;
  478. if (sys::path::is_absolute(Name))
  479. return std::string(Name);
  480. SmallString<128> FullName = sys::path::parent_path(
  481. Parent->getMemoryBufferRef().getBufferIdentifier());
  482. sys::path::append(FullName, Name);
  483. return std::string(FullName.str());
  484. }
  485. Expected<StringRef> Archive::Child::getBuffer() const {
  486. Expected<bool> isThinOrErr = isThinMember();
  487. if (!isThinOrErr)
  488. return isThinOrErr.takeError();
  489. bool isThin = isThinOrErr.get();
  490. if (!isThin) {
  491. Expected<uint64_t> Size = getSize();
  492. if (!Size)
  493. return Size.takeError();
  494. return StringRef(Data.data() + StartOfFile, Size.get());
  495. }
  496. Expected<std::string> FullNameOrErr = getFullName();
  497. if (!FullNameOrErr)
  498. return FullNameOrErr.takeError();
  499. const std::string &FullName = *FullNameOrErr;
  500. ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
  501. if (std::error_code EC = Buf.getError())
  502. return errorCodeToError(EC);
  503. Parent->ThinBuffers.push_back(std::move(*Buf));
  504. return Parent->ThinBuffers.back()->getBuffer();
  505. }
  506. Expected<Archive::Child> Archive::Child::getNext() const {
  507. Expected<const char *> NextLocOrErr = Header->getNextChildLoc();
  508. if (!NextLocOrErr)
  509. return NextLocOrErr.takeError();
  510. const char *NextLoc = *NextLocOrErr;
  511. // Check to see if this is at the end of the archive.
  512. if (NextLoc == nullptr)
  513. return Child(nullptr, nullptr, nullptr);
  514. // Check to see if this is past the end of the archive.
  515. if (NextLoc > Parent->Data.getBufferEnd()) {
  516. std::string Msg("offset to next archive member past the end of the archive "
  517. "after member ");
  518. Expected<StringRef> NameOrErr = getName();
  519. if (!NameOrErr) {
  520. consumeError(NameOrErr.takeError());
  521. uint64_t Offset = Data.data() - Parent->getData().data();
  522. return malformedError(Msg + "at offset " + Twine(Offset));
  523. } else
  524. return malformedError(Msg + NameOrErr.get());
  525. }
  526. Error Err = Error::success();
  527. Child Ret(Parent, NextLoc, &Err);
  528. if (Err)
  529. return std::move(Err);
  530. return Ret;
  531. }
  532. uint64_t Archive::Child::getChildOffset() const {
  533. const char *a = Parent->Data.getBuffer().data();
  534. const char *c = Data.data();
  535. uint64_t offset = c - a;
  536. return offset;
  537. }
  538. Expected<StringRef> Archive::Child::getName() const {
  539. Expected<uint64_t> RawSizeOrErr = getRawSize();
  540. if (!RawSizeOrErr)
  541. return RawSizeOrErr.takeError();
  542. uint64_t RawSize = RawSizeOrErr.get();
  543. Expected<StringRef> NameOrErr =
  544. Header->getName(Header->getSizeOf() + RawSize);
  545. if (!NameOrErr)
  546. return NameOrErr.takeError();
  547. StringRef Name = NameOrErr.get();
  548. return Name;
  549. }
  550. Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
  551. Expected<StringRef> NameOrErr = getName();
  552. if (!NameOrErr)
  553. return NameOrErr.takeError();
  554. StringRef Name = NameOrErr.get();
  555. Expected<StringRef> Buf = getBuffer();
  556. if (!Buf)
  557. return createFileError(Name, Buf.takeError());
  558. return MemoryBufferRef(*Buf, Name);
  559. }
  560. Expected<std::unique_ptr<Binary>>
  561. Archive::Child::getAsBinary(LLVMContext *Context) const {
  562. Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
  563. if (!BuffOrErr)
  564. return BuffOrErr.takeError();
  565. auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
  566. if (BinaryOrErr)
  567. return std::move(*BinaryOrErr);
  568. return BinaryOrErr.takeError();
  569. }
  570. Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
  571. Error Err = Error::success();
  572. std::unique_ptr<Archive> Ret;
  573. StringRef Buffer = Source.getBuffer();
  574. if (Buffer.startswith(BigArchiveMagic))
  575. Ret = std::make_unique<BigArchive>(Source, Err);
  576. else
  577. Ret = std::make_unique<Archive>(Source, Err);
  578. if (Err)
  579. return std::move(Err);
  580. return std::move(Ret);
  581. }
  582. std::unique_ptr<AbstractArchiveMemberHeader>
  583. Archive::createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size,
  584. Error *Err) const {
  585. ErrorAsOutParameter ErrAsOutParam(Err);
  586. if (kind() != K_AIXBIG)
  587. return std::make_unique<ArchiveMemberHeader>(this, RawHeaderPtr, Size, Err);
  588. return std::make_unique<BigArchiveMemberHeader>(this, RawHeaderPtr, Size,
  589. Err);
  590. }
  591. uint64_t Archive::getArchiveMagicLen() const {
  592. if (isThin())
  593. return sizeof(ThinArchiveMagic) - 1;
  594. if (Kind() == K_AIXBIG)
  595. return sizeof(BigArchiveMagic) - 1;
  596. return sizeof(ArchiveMagic) - 1;
  597. }
  598. void Archive::setFirstRegular(const Child &C) {
  599. FirstRegularData = C.Data;
  600. FirstRegularStartOfFile = C.StartOfFile;
  601. }
  602. Archive::Archive(MemoryBufferRef Source, Error &Err)
  603. : Binary(Binary::ID_Archive, Source) {
  604. ErrorAsOutParameter ErrAsOutParam(&Err);
  605. StringRef Buffer = Data.getBuffer();
  606. // Check for sufficient magic.
  607. if (Buffer.startswith(ThinArchiveMagic)) {
  608. IsThin = true;
  609. } else if (Buffer.startswith(ArchiveMagic)) {
  610. IsThin = false;
  611. } else if (Buffer.startswith(BigArchiveMagic)) {
  612. Format = K_AIXBIG;
  613. IsThin = false;
  614. return;
  615. } else {
  616. Err = make_error<GenericBinaryError>("file too small to be an archive",
  617. object_error::invalid_file_type);
  618. return;
  619. }
  620. // Make sure Format is initialized before any call to
  621. // ArchiveMemberHeader::getName() is made. This could be a valid empty
  622. // archive which is the same in all formats. So claiming it to be gnu to is
  623. // fine if not totally correct before we look for a string table or table of
  624. // contents.
  625. Format = K_GNU;
  626. // Get the special members.
  627. child_iterator I = child_begin(Err, false);
  628. if (Err)
  629. return;
  630. child_iterator E = child_end();
  631. // See if this is a valid empty archive and if so return.
  632. if (I == E) {
  633. Err = Error::success();
  634. return;
  635. }
  636. const Child *C = &*I;
  637. auto Increment = [&]() {
  638. ++I;
  639. if (Err)
  640. return true;
  641. C = &*I;
  642. return false;
  643. };
  644. Expected<StringRef> NameOrErr = C->getRawName();
  645. if (!NameOrErr) {
  646. Err = NameOrErr.takeError();
  647. return;
  648. }
  649. StringRef Name = NameOrErr.get();
  650. // Below is the pattern that is used to figure out the archive format
  651. // GNU archive format
  652. // First member : / (may exist, if it exists, points to the symbol table )
  653. // Second member : // (may exist, if it exists, points to the string table)
  654. // Note : The string table is used if the filename exceeds 15 characters
  655. // BSD archive format
  656. // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
  657. // There is no string table, if the filename exceeds 15 characters or has a
  658. // embedded space, the filename has #1/<size>, The size represents the size
  659. // of the filename that needs to be read after the archive header
  660. // COFF archive format
  661. // First member : /
  662. // Second member : / (provides a directory of symbols)
  663. // Third member : // (may exist, if it exists, contains the string table)
  664. // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
  665. // even if the string table is empty. However, lib.exe does not in fact
  666. // seem to create the third member if there's no member whose filename
  667. // exceeds 15 characters. So the third member is optional.
  668. if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
  669. if (Name == "__.SYMDEF")
  670. Format = K_BSD;
  671. else // Name == "__.SYMDEF_64"
  672. Format = K_DARWIN64;
  673. // We know that the symbol table is not an external file, but we still must
  674. // check any Expected<> return value.
  675. Expected<StringRef> BufOrErr = C->getBuffer();
  676. if (!BufOrErr) {
  677. Err = BufOrErr.takeError();
  678. return;
  679. }
  680. SymbolTable = BufOrErr.get();
  681. if (Increment())
  682. return;
  683. setFirstRegular(*C);
  684. Err = Error::success();
  685. return;
  686. }
  687. if (Name.startswith("#1/")) {
  688. Format = K_BSD;
  689. // We know this is BSD, so getName will work since there is no string table.
  690. Expected<StringRef> NameOrErr = C->getName();
  691. if (!NameOrErr) {
  692. Err = NameOrErr.takeError();
  693. return;
  694. }
  695. Name = NameOrErr.get();
  696. if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
  697. // We know that the symbol table is not an external file, but we still
  698. // must check any Expected<> return value.
  699. Expected<StringRef> BufOrErr = C->getBuffer();
  700. if (!BufOrErr) {
  701. Err = BufOrErr.takeError();
  702. return;
  703. }
  704. SymbolTable = BufOrErr.get();
  705. if (Increment())
  706. return;
  707. } else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
  708. Format = K_DARWIN64;
  709. // We know that the symbol table is not an external file, but we still
  710. // must check any Expected<> return value.
  711. Expected<StringRef> BufOrErr = C->getBuffer();
  712. if (!BufOrErr) {
  713. Err = BufOrErr.takeError();
  714. return;
  715. }
  716. SymbolTable = BufOrErr.get();
  717. if (Increment())
  718. return;
  719. }
  720. setFirstRegular(*C);
  721. return;
  722. }
  723. // MIPS 64-bit ELF archives use a special format of a symbol table.
  724. // This format is marked by `ar_name` field equals to "/SYM64/".
  725. // For detailed description see page 96 in the following document:
  726. // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
  727. bool has64SymTable = false;
  728. if (Name == "/" || Name == "/SYM64/") {
  729. // We know that the symbol table is not an external file, but we still
  730. // must check any Expected<> return value.
  731. Expected<StringRef> BufOrErr = C->getBuffer();
  732. if (!BufOrErr) {
  733. Err = BufOrErr.takeError();
  734. return;
  735. }
  736. SymbolTable = BufOrErr.get();
  737. if (Name == "/SYM64/")
  738. has64SymTable = true;
  739. if (Increment())
  740. return;
  741. if (I == E) {
  742. Err = Error::success();
  743. return;
  744. }
  745. Expected<StringRef> NameOrErr = C->getRawName();
  746. if (!NameOrErr) {
  747. Err = NameOrErr.takeError();
  748. return;
  749. }
  750. Name = NameOrErr.get();
  751. }
  752. if (Name == "//") {
  753. Format = has64SymTable ? K_GNU64 : K_GNU;
  754. // The string table is never an external member, but we still
  755. // must check any Expected<> return value.
  756. Expected<StringRef> BufOrErr = C->getBuffer();
  757. if (!BufOrErr) {
  758. Err = BufOrErr.takeError();
  759. return;
  760. }
  761. StringTable = BufOrErr.get();
  762. if (Increment())
  763. return;
  764. setFirstRegular(*C);
  765. Err = Error::success();
  766. return;
  767. }
  768. if (Name[0] != '/') {
  769. Format = has64SymTable ? K_GNU64 : K_GNU;
  770. setFirstRegular(*C);
  771. Err = Error::success();
  772. return;
  773. }
  774. if (Name != "/") {
  775. Err = errorCodeToError(object_error::parse_failed);
  776. return;
  777. }
  778. Format = K_COFF;
  779. // We know that the symbol table is not an external file, but we still
  780. // must check any Expected<> return value.
  781. Expected<StringRef> BufOrErr = C->getBuffer();
  782. if (!BufOrErr) {
  783. Err = BufOrErr.takeError();
  784. return;
  785. }
  786. SymbolTable = BufOrErr.get();
  787. if (Increment())
  788. return;
  789. if (I == E) {
  790. setFirstRegular(*C);
  791. Err = Error::success();
  792. return;
  793. }
  794. NameOrErr = C->getRawName();
  795. if (!NameOrErr) {
  796. Err = NameOrErr.takeError();
  797. return;
  798. }
  799. Name = NameOrErr.get();
  800. if (Name == "//") {
  801. // The string table is never an external member, but we still
  802. // must check any Expected<> return value.
  803. Expected<StringRef> BufOrErr = C->getBuffer();
  804. if (!BufOrErr) {
  805. Err = BufOrErr.takeError();
  806. return;
  807. }
  808. StringTable = BufOrErr.get();
  809. if (Increment())
  810. return;
  811. }
  812. setFirstRegular(*C);
  813. Err = Error::success();
  814. }
  815. Archive::child_iterator Archive::child_begin(Error &Err,
  816. bool SkipInternal) const {
  817. if (isEmpty())
  818. return child_end();
  819. if (SkipInternal)
  820. return child_iterator::itr(
  821. Child(this, FirstRegularData, FirstRegularStartOfFile), Err);
  822. const char *Loc = Data.getBufferStart() + getFirstChildOffset();
  823. Child C(this, Loc, &Err);
  824. if (Err)
  825. return child_end();
  826. return child_iterator::itr(C, Err);
  827. }
  828. Archive::child_iterator Archive::child_end() const {
  829. return child_iterator::end(Child(nullptr, nullptr, nullptr));
  830. }
  831. StringRef Archive::Symbol::getName() const {
  832. return Parent->getSymbolTable().begin() + StringIndex;
  833. }
  834. Expected<Archive::Child> Archive::Symbol::getMember() const {
  835. const char *Buf = Parent->getSymbolTable().begin();
  836. const char *Offsets = Buf;
  837. if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64)
  838. Offsets += sizeof(uint64_t);
  839. else
  840. Offsets += sizeof(uint32_t);
  841. uint64_t Offset = 0;
  842. if (Parent->kind() == K_GNU) {
  843. Offset = read32be(Offsets + SymbolIndex * 4);
  844. } else if (Parent->kind() == K_GNU64) {
  845. Offset = read64be(Offsets + SymbolIndex * 8);
  846. } else if (Parent->kind() == K_BSD) {
  847. // The SymbolIndex is an index into the ranlib structs that start at
  848. // Offsets (the first uint32_t is the number of bytes of the ranlib
  849. // structs). The ranlib structs are a pair of uint32_t's the first
  850. // being a string table offset and the second being the offset into
  851. // the archive of the member that defines the symbol. Which is what
  852. // is needed here.
  853. Offset = read32le(Offsets + SymbolIndex * 8 + 4);
  854. } else if (Parent->kind() == K_DARWIN64) {
  855. // The SymbolIndex is an index into the ranlib_64 structs that start at
  856. // Offsets (the first uint64_t is the number of bytes of the ranlib_64
  857. // structs). The ranlib_64 structs are a pair of uint64_t's the first
  858. // being a string table offset and the second being the offset into
  859. // the archive of the member that defines the symbol. Which is what
  860. // is needed here.
  861. Offset = read64le(Offsets + SymbolIndex * 16 + 8);
  862. } else {
  863. // Skip offsets.
  864. uint32_t MemberCount = read32le(Buf);
  865. Buf += MemberCount * 4 + 4;
  866. uint32_t SymbolCount = read32le(Buf);
  867. if (SymbolIndex >= SymbolCount)
  868. return errorCodeToError(object_error::parse_failed);
  869. // Skip SymbolCount to get to the indices table.
  870. const char *Indices = Buf + 4;
  871. // Get the index of the offset in the file member offset table for this
  872. // symbol.
  873. uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
  874. // Subtract 1 since OffsetIndex is 1 based.
  875. --OffsetIndex;
  876. if (OffsetIndex >= MemberCount)
  877. return errorCodeToError(object_error::parse_failed);
  878. Offset = read32le(Offsets + OffsetIndex * 4);
  879. }
  880. const char *Loc = Parent->getData().begin() + Offset;
  881. Error Err = Error::success();
  882. Child C(Parent, Loc, &Err);
  883. if (Err)
  884. return std::move(Err);
  885. return C;
  886. }
  887. Archive::Symbol Archive::Symbol::getNext() const {
  888. Symbol t(*this);
  889. if (Parent->kind() == K_BSD) {
  890. // t.StringIndex is an offset from the start of the __.SYMDEF or
  891. // "__.SYMDEF SORTED" member into the string table for the ranlib
  892. // struct indexed by t.SymbolIndex . To change t.StringIndex to the
  893. // offset in the string table for t.SymbolIndex+1 we subtract the
  894. // its offset from the start of the string table for t.SymbolIndex
  895. // and add the offset of the string table for t.SymbolIndex+1.
  896. // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
  897. // which is the number of bytes of ranlib structs that follow. The ranlib
  898. // structs are a pair of uint32_t's the first being a string table offset
  899. // and the second being the offset into the archive of the member that
  900. // define the symbol. After that the next uint32_t is the byte count of
  901. // the string table followed by the string table.
  902. const char *Buf = Parent->getSymbolTable().begin();
  903. uint32_t RanlibCount = 0;
  904. RanlibCount = read32le(Buf) / 8;
  905. // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
  906. // don't change the t.StringIndex as we don't want to reference a ranlib
  907. // past RanlibCount.
  908. if (t.SymbolIndex + 1 < RanlibCount) {
  909. const char *Ranlibs = Buf + 4;
  910. uint32_t CurRanStrx = 0;
  911. uint32_t NextRanStrx = 0;
  912. CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
  913. NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
  914. t.StringIndex -= CurRanStrx;
  915. t.StringIndex += NextRanStrx;
  916. }
  917. } else {
  918. // Go to one past next null.
  919. t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
  920. }
  921. ++t.SymbolIndex;
  922. return t;
  923. }
  924. Archive::symbol_iterator Archive::symbol_begin() const {
  925. if (!hasSymbolTable())
  926. return symbol_iterator(Symbol(this, 0, 0));
  927. const char *buf = getSymbolTable().begin();
  928. if (kind() == K_GNU) {
  929. uint32_t symbol_count = 0;
  930. symbol_count = read32be(buf);
  931. buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
  932. } else if (kind() == K_GNU64) {
  933. uint64_t symbol_count = read64be(buf);
  934. buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
  935. } else if (kind() == K_BSD) {
  936. // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
  937. // which is the number of bytes of ranlib structs that follow. The ranlib
  938. // structs are a pair of uint32_t's the first being a string table offset
  939. // and the second being the offset into the archive of the member that
  940. // define the symbol. After that the next uint32_t is the byte count of
  941. // the string table followed by the string table.
  942. uint32_t ranlib_count = 0;
  943. ranlib_count = read32le(buf) / 8;
  944. const char *ranlibs = buf + 4;
  945. uint32_t ran_strx = 0;
  946. ran_strx = read32le(ranlibs);
  947. buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
  948. // Skip the byte count of the string table.
  949. buf += sizeof(uint32_t);
  950. buf += ran_strx;
  951. } else if (kind() == K_DARWIN64) {
  952. // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
  953. // which is the number of bytes of ranlib_64 structs that follow. The
  954. // ranlib_64 structs are a pair of uint64_t's the first being a string
  955. // table offset and the second being the offset into the archive of the
  956. // member that define the symbol. After that the next uint64_t is the byte
  957. // count of the string table followed by the string table.
  958. uint64_t ranlib_count = 0;
  959. ranlib_count = read64le(buf) / 16;
  960. const char *ranlibs = buf + 8;
  961. uint64_t ran_strx = 0;
  962. ran_strx = read64le(ranlibs);
  963. buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
  964. // Skip the byte count of the string table.
  965. buf += sizeof(uint64_t);
  966. buf += ran_strx;
  967. } else {
  968. uint32_t member_count = 0;
  969. uint32_t symbol_count = 0;
  970. member_count = read32le(buf);
  971. buf += 4 + (member_count * 4); // Skip offsets.
  972. symbol_count = read32le(buf);
  973. buf += 4 + (symbol_count * 2); // Skip indices.
  974. }
  975. uint32_t string_start_offset = buf - getSymbolTable().begin();
  976. return symbol_iterator(Symbol(this, 0, string_start_offset));
  977. }
  978. Archive::symbol_iterator Archive::symbol_end() const {
  979. return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
  980. }
  981. uint32_t Archive::getNumberOfSymbols() const {
  982. if (!hasSymbolTable())
  983. return 0;
  984. const char *buf = getSymbolTable().begin();
  985. if (kind() == K_GNU)
  986. return read32be(buf);
  987. if (kind() == K_GNU64)
  988. return read64be(buf);
  989. if (kind() == K_BSD)
  990. return read32le(buf) / 8;
  991. if (kind() == K_DARWIN64)
  992. return read64le(buf) / 16;
  993. uint32_t member_count = 0;
  994. member_count = read32le(buf);
  995. buf += 4 + (member_count * 4); // Skip offsets.
  996. return read32le(buf);
  997. }
  998. Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
  999. Archive::symbol_iterator bs = symbol_begin();
  1000. Archive::symbol_iterator es = symbol_end();
  1001. for (; bs != es; ++bs) {
  1002. StringRef SymName = bs->getName();
  1003. if (SymName == name) {
  1004. if (auto MemberOrErr = bs->getMember())
  1005. return Child(*MemberOrErr);
  1006. else
  1007. return MemberOrErr.takeError();
  1008. }
  1009. }
  1010. return Optional<Child>();
  1011. }
  1012. // Returns true if archive file contains no member file.
  1013. bool Archive::isEmpty() const {
  1014. return Data.getBufferSize() == getArchiveMagicLen();
  1015. }
  1016. bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
  1017. BigArchive::BigArchive(MemoryBufferRef Source, Error &Err)
  1018. : Archive(Source, Err) {
  1019. ErrorAsOutParameter ErrAsOutParam(&Err);
  1020. StringRef Buffer = Data.getBuffer();
  1021. ArFixLenHdr = reinterpret_cast<const FixLenHdr *>(Buffer.data());
  1022. StringRef RawOffset = getFieldRawString(ArFixLenHdr->FirstChildOffset);
  1023. if (RawOffset.getAsInteger(10, FirstChildOffset))
  1024. // TODO: Out-of-line.
  1025. Err = malformedError("malformed AIX big archive: first member offset \"" +
  1026. RawOffset + "\" is not a number");
  1027. RawOffset = getFieldRawString(ArFixLenHdr->LastChildOffset);
  1028. if (RawOffset.getAsInteger(10, LastChildOffset))
  1029. // TODO: Out-of-line.
  1030. Err = malformedError("malformed AIX big archive: last member offset \"" +
  1031. RawOffset + "\" is not a number");
  1032. child_iterator I = child_begin(Err, false);
  1033. if (Err)
  1034. return;
  1035. child_iterator E = child_end();
  1036. if (I == E) {
  1037. Err = Error::success();
  1038. return;
  1039. }
  1040. setFirstRegular(*I);
  1041. Err = Error::success();
  1042. }